Preserve background shell visibility across readback races
Reconcile authoritative shell snapshots with bounded lifecycle journals so concurrent exits and creates cannot be overwritten, coalesce observation setup, and retain verified ownership across reconnects. Add deterministic reconciliation and cancellation regressions plus stable limited-visibility handling.\n\nChecked packaged templates and authoring skill references: none need updating. The task-owned docs note records the stale shared chat documentation for docs-fold; shared documentation was not edited.
This commit is contained in:
@@ -395,3 +395,45 @@ func TestOpenChatRejectsUnrunnableCLI(t *testing.T) {
|
||||
t.Fatal("a missing CLI must fail detection, not open an endpoint")
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatShellReadbackSeedsReplacementStream proves an authoritative shell
|
||||
// readback survives a later stream replacement: shell.exited has no session id,
|
||||
// so it must still clear the verified shell after reconnect.
|
||||
func TestChatShellReadbackSeedsReplacementStream(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/shell":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"sh_1","status":"running","command":"sleep 30","metadata":{"sessionID":"ses_1"}},{"id":"sh_other","status":"running","command":"sleep 30","metadata":{"sessionID":"ses_other"}}]}`))
|
||||
case "/api/event":
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"shell.exited\",\"data\":{\"id\":\"sh_1\",\"status\":\"exited\",\"exit\":0}}\n\n"))
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
shells, err := chat.Shells(context.Background(), "ses_1")
|
||||
if err != nil || len(shells) != 1 || shells[0].ID != "sh_1" {
|
||||
t.Fatalf("Shells = %+v, %v", shells, err)
|
||||
}
|
||||
var got []protocol.ChatEventFrame
|
||||
if err := chat.Stream(context.Background(), "ses_1", nil, func(frame protocol.ChatEventFrame) error { got = append(got, frame); return nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Shell == nil || got[0].Shell.ID != "sh_1" || got[0].Shell.Status != protocol.ChatShellExited {
|
||||
t.Fatalf("exit after readback = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatReleaseShellsBoundsDisconnectedState(t *testing.T) {
|
||||
chat := NewChat(&Connection{})
|
||||
state := chat.streamState("ses_1")
|
||||
state.markShell("sh_1")
|
||||
chat.ReleaseShells("ses_1")
|
||||
chat.shellMu.Lock()
|
||||
_, retained := chat.shells["ses_1"]
|
||||
chat.shellMu.Unlock()
|
||||
if retained {
|
||||
t.Fatal("released session shell state was retained")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user