Release native chat runtimes on lifecycle changes
Ensure /new and chat close stop native foreground and background work before changing registry state. Preserve transcripts while surfacing release and recovery cleanup failures instead of reporting false success, with focused adapter and server regressions.
This commit is contained in:
@@ -23,8 +23,9 @@ type chatSessionReplacer interface {
|
||||
// The registry is the only place that knows "this chat": every other layer
|
||||
// (rooms, queues, transcripts, shells, exports, resume commands) is keyed by
|
||||
// the native id, so the row is re-keyed in one transaction rather than mapped.
|
||||
// The previous native session is not touched and remains resumable in a
|
||||
// terminal. A running turn is refused rather than abandoned. Every other
|
||||
// The previous native session is released after the fresh session is created;
|
||||
// its transcript remains resumable, but its agent and background work do not
|
||||
// continue. A running turn is refused rather than abandoned. Every other
|
||||
// connection receives the new row as chat_session_changed with PreviousID set,
|
||||
// so its list swaps the row in place and an open conversation follows it.
|
||||
func handleChatNewSession(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
|
||||
@@ -85,6 +86,20 @@ func handleChatNewSession(ctx context.Context, request protocol.Message, broker
|
||||
fail("session " + created.ID + " was started but manager bootstrap failed while activating pipelines-manager: " + err.Error())
|
||||
return
|
||||
}
|
||||
// Nothing queued for the old context may run after its native runtime is
|
||||
// released, even if the registry move below fails.
|
||||
hub.clearQueue(ctx, oldID)
|
||||
hub.invalidateRecovery(oldID)
|
||||
if releaser, ok := broker.(chatSessionReleaser); ok {
|
||||
if err := releaser.ReleaseSession(ctx, oldID); err != nil {
|
||||
fail("session " + oldID + " could not be released: " + err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := hub.clearRecovery(ctx, oldID); err != nil {
|
||||
fail("session " + oldID + " recovery state could not be cleared: " + err.Error())
|
||||
return
|
||||
}
|
||||
if err := replacer.ReplaceOperatorSession(ctx, oldID, created.ID); err != nil {
|
||||
if errors.Is(err, pipeline.ErrOperatorSessionReplaced) {
|
||||
fail(err.Error() + "; reopen the chat to continue it")
|
||||
@@ -93,9 +108,6 @@ func handleChatNewSession(ctx context.Context, request protocol.Message, broker
|
||||
fail("session " + created.ID + " was started but the chat could not move to it: " + err.Error())
|
||||
return
|
||||
}
|
||||
// Nothing queued for the old context may run in the new one; with no turn
|
||||
// in flight this is normally already empty.
|
||||
hub.clearQueue(ctx, oldID)
|
||||
row, found, err := registry.OperatorSession(ctx, created.ID)
|
||||
if err != nil || !found {
|
||||
fail("session " + created.ID + " was recorded but could not be read back")
|
||||
|
||||
Reference in New Issue
Block a user