971e555ceb
Add a server-side close for operator chats, so finishing with a chat takes it out of the active list for every client without deleting anything. The brokered protocol gains chat_close and chat_reopen; chat_close stops a running turn by reusing the existing broker interrupt (its result carries an interrupted flag, so a stream is never left running into nothing) and then sets closed_at on the session's operator_sessions row. The session, its transcript and its history survive, and because the closed state lives in the registry chat_sessions reports it to every client, not just the one that asked. An id that is not a recorded operator session, including a pipeline-owned run session, is refused before anything is written. chat_reopen clears the state again, and simply prompting a closed session also clears it (the store clears closed_at on touch). The registry gains closed_at for a fresh database and an idempotent migration for one created before it existed. The app renders closed sessions under a labelled Closed section with a Closed chip, offers a Close action with a confirmation that says it stops a running turn, marks the chat closed on every device and deletes nothing, and a Reopen action on each closed row. The device-local hide is renamed Hide on this device, with a snackbar saying the chat stays on the server, so it is never confused with the real close. No new dependency, the installer and Docker files are untouched, and no run is started from the app. Checked before committing: the packaged templates carry no chat surface and need no update, and internal/author/install.go's managed inventory needs none either (this adds no reference file). The packaged authoring skill reference internal/author/skill/references/operations.md and the shared docs/cli.md and android/README.md are stale because of this change, but an item no longer edits a shared document: what is stale and what should change there is recorded in docs/notes/chat-close.md for the docs-fold writer, which is committed here with the change. Checks: gofmt clean, go build ./... and go vet ./... clean, and go test ./internal/protocol/... ./internal/pipeline/... ./internal/server/... ./internal/author/... pass. The app side is verified with :app:assembleDebug and :app:testDebugUnitTest (--offline --no-daemon, BUILD SUCCESSFUL); the on-device close, second-client and reopen check is deferred to visual review. Not deployed; deploying is the operator's step.
125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestOperatorSessionsCloseReopenAndTouch proves the closed-state contract: a
|
|
// close records closed_at and the session is still listed as history, an explicit
|
|
// reopen clears it, a prompt (touch) clears it, re-recording clears it, and the
|
|
// session, its history and its created_at are never deleted or rewritten.
|
|
func TestOperatorSessionsCloseReopenAndTouch(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "close.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := store.CloseOperatorSession(ctx, "ses_a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
closed, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(closed) != 1 || closed[0].ClosedAt <= 0 {
|
|
t.Fatalf("close did not record closed_at: %+v", closed)
|
|
}
|
|
if closed[0].ID != before[0].ID || closed[0].Directory != before[0].Directory ||
|
|
closed[0].Title != before[0].Title || closed[0].Tag != before[0].Tag || closed[0].CreatedAt != before[0].CreatedAt {
|
|
t.Fatalf("close changed the session beyond closed_at:\nbefore %+v\nafter %+v", before[0], closed[0])
|
|
}
|
|
one, found, err := store.OperatorSession(ctx, "ses_a")
|
|
if err != nil || !found || one.ClosedAt <= 0 {
|
|
t.Fatalf("OperatorSession = %+v found=%v err=%v", one, found, err)
|
|
}
|
|
if _, found, err := store.OperatorSession(ctx, "ses_run"); err != nil || found {
|
|
t.Fatalf("a non-operator session was reported found=%v err=%v", found, err)
|
|
}
|
|
|
|
if err := store.ReopenOperatorSession(ctx, "ses_a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err := store.OperatorSessions(ctx); err != nil || len(rows) != 1 || rows[0].ClosedAt != 0 {
|
|
t.Fatalf("explicit reopen did not clear closed_at: %+v %v", rows, err)
|
|
}
|
|
|
|
// Prompting (touch) reopens too.
|
|
if err := store.CloseOperatorSession(ctx, "ses_a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.TouchOperatorSession(ctx, "ses_a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err := store.OperatorSessions(ctx); err != nil || len(rows) != 1 || rows[0].ClosedAt != 0 {
|
|
t.Fatalf("touch did not clear closed_at: %+v %v", rows, err)
|
|
}
|
|
|
|
// Re-recording (create/adopt) reopens too.
|
|
if err := store.CloseOperatorSession(ctx, "ses_a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err := store.OperatorSessions(ctx); err != nil || len(rows) != 1 || rows[0].ClosedAt != 0 {
|
|
t.Fatalf("re-record did not clear closed_at: %+v %v", rows, err)
|
|
}
|
|
|
|
// Closing a session id that is not an operator session writes nothing.
|
|
if err := store.CloseOperatorSession(ctx, "ses_run"); err != nil {
|
|
t.Fatalf("closing a non-operator session errored: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestOperatorSessionsMigrationAddsClosedAt proves an existing database created
|
|
// before the closed state opens cleanly: the column is added with a default, so
|
|
// every existing session stays open and Close/Reopen work afterwards.
|
|
func TestOperatorSessionsMigrationAddsClosedAt(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "migrate.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RecordOperatorSession(ctx, "ses_old", "/work/old", "Old", "operator"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Simulate the pre-closed schema by dropping the column, then reopen.
|
|
if _, err := store.db.ExecContext(ctx, "ALTER TABLE operator_sessions DROP COLUMN closed_at"); err != nil {
|
|
t.Fatalf("drop closed_at: %v", err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatalf("Open after drop: %v", err)
|
|
}
|
|
defer store.Close()
|
|
rows, err := store.OperatorSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rows) != 1 || rows[0].ID != "ses_old" || rows[0].ClosedAt != 0 {
|
|
t.Fatalf("migrated rows = %+v, want the old session open", rows)
|
|
}
|
|
if err := store.CloseOperatorSession(ctx, "ses_old"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err := store.OperatorSessions(ctx); err != nil || len(rows) != 1 || rows[0].ClosedAt <= 0 {
|
|
t.Fatalf("close after migration failed: %+v %v", rows, err)
|
|
}
|
|
}
|