From 2025dcb2d334fa591c8ef458f2d01e6e0cfedb2a Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 20 Nov 2026 07:17:07 -0500 Subject: [PATCH] fix(acp): answer a compaction once when the session is lost under it Marking a session lost answers its running compaction with the failure and leaves the activity finishing until that answer is written. If the compaction call returned, or the editor cancelled, before the write, completeCompact and cancel answered the same JSON-RPC request again: the editor saw two responses to one request. Both now leave a finishing compaction to its write. Found by the generated session-state histories. --- cli/internal/acp/state.go | 6 ++++-- cli/internal/acp/state_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cli/internal/acp/state.go b/cli/internal/acp/state.go index 3ca2a6bcd..a14f27239 100644 --- a/cli/internal/acp/state.go +++ b/cli/internal/acp/state.go @@ -257,7 +257,9 @@ func (st *sessionState) cancel() []effect { effects = append(effects, respondEffect{request: st.queued.request, result: promptCancelled()}) st.queued = nil } - if st.active != nil && st.active.kind == compactActivity { + // A compaction already answered (the session was lost) waits only for + // that answer's write. + if st.active != nil && st.active.kind == compactActivity && st.active.phase != activityFinishing { effects = append(effects, respondEffect{request: st.active.request, result: promptCancelled()}) st.active = nil } @@ -421,7 +423,7 @@ func (st *sessionState) turnCompleted(turnID string, submissionIDs []string, out } func (st *sessionState) completeCompact(a *activity, result daemon.CompactionResult, err error, sessionID string, compactionEnabled bool) []effect { - if st.active != a || a.kind != compactActivity { + if st.active != a || a.kind != compactActivity || a.phase == activityFinishing { return nil } st.active = nil diff --git a/cli/internal/acp/state_test.go b/cli/internal/acp/state_test.go index d340b92f2..718717014 100644 --- a/cli/internal/acp/state_test.go +++ b/cli/internal/acp/state_test.go @@ -96,6 +96,36 @@ func TestCancelAnswersCompactWithoutWaitingForTheDaemon(t *testing.T) { } } +// A session lost during a compaction answers the compaction with the +// failure. The compaction's own result, or a cancel, arriving before that +// answer is written must not answer the request again. +func TestALostCompactionIsAnsweredOnce(t *testing.T) { + for _, late := range []string{"result", "cancel"} { + var st sessionState + effects, _ := st.acceptCompact(requestID("1")) + compaction := effects[0].(compactEffect).activity + answers := 0 + count := func(effects []effect) { + for _, item := range effects { + switch item.(type) { + case respondEffect, finishEffect: + answers++ + } + } + } + count(st.markLost("observation_uncertain", "stream closed")) + if late == "result" { + count(st.completeCompact(compaction, daemon.CompactionResult{}, nil, "s", true)) + } else { + count(st.cancel()) + } + count(st.responseWritten(compaction)) + if answers != 1 || st.active != nil { + t.Fatalf("%s after loss: %d answers, active %v", late, answers, st.active) + } + } +} + func TestFinishingActivityDefersTheNextPrompt(t *testing.T) { var st sessionState st.active = &activity{request: requestID("1"), phase: activityFinishing} -- knotmirror 0.0.1