Skip to content

fix: don't append the same exit summary twice - #53

Open
MiFaZhan wants to merge 1 commit into
jayzeng:mainfrom
MiFaZhan:fix/exit-summary-duplicate
Open

MiFaZhan wants to merge 1 commit into
jayzeng:mainfrom
MiFaZhan:fix/exit-summary-duplicate

Conversation

@MiFaZhan

Copy link
Copy Markdown

Problem

Exit summaries snapshot the whole session branch, not "messages since the last summary". So quitting, resuming the same session, and quitting again summarizes the identical transcript a second time and appends it to the daily log again.

This is not just cosmetic noise. Today's daily log is re-injected into the system prompt on every new session, so the duplicate is paid for in context on every subsequent turn — and it keeps resurfacing through memory_search.

Observed in the wild: one session id appearing twice in a single daily log, ~4 minutes apart, with byte-similar content in two different languages:

<!-- 2026-10-15 13:03:46 [01a13d57] -->
## Session Summary (auto, exit: session-end)
### Decisions
- 4 条非数字 chan_key ... 列入禁止改动清单

<!-- 2026-10-15 13:07:07 [01a13d57] -->
## Session Summary (auto, exit: session-end)
### Decisions
- **4 non-numeric chan_key channels are frozen**: ...

The only entries added to the branch between those two summaries were three plan-mode-state rows. The session file ends at 13:07:07, and pi calls process.exit(0) after session_shutdown, so the second summary could only have come from a resume of the same session.

Fix

Before generating a summary, compare the newest message entry in the branch against the last summary already recorded for that session, and skip the LLM call entirely when nothing was added.

  • The scan reads <!-- <ts> [<sid>] --> headers from today's and yesterday's daily logs, so a session resumed across midnight is still covered.
  • Pre-compaction handoffs use the same header shape and also snapshot the whole branch, so they count as prior snapshots too.
  • Only message entries count. Bookkeeping entries (plan-mode-state, compact-thinking-duration, agent-summary, …) are appended on every session transition and would otherwise defeat the check — in the observed case above, those three plan-mode-state rows were exactly what a naive timestamp comparison would trip over.
  • Fails open. An unidentifiable session, missing branch, or unparseable timestamp summarizes as before, because losing a summary is worse than writing a duplicate. getSessionIdSafe() also keeps session_shutdown from throwing on teardown paths where the context is bare — the rest of that handler already treats a missing session manager as "no data, carry on".

Verification

197 → 208 tests, 0 failures. tsc -p tsconfig.json --noEmit and biome check . are both clean.

The 11 new tests cover timestamp parsing round-trips, session-id matching (including non-hex prefixes, since shortSessionId() only slices 8 chars), the bookkeeping-only tail, fail-open cases, and two end-to-end session_shutdown assertions — one that the LLM call is skipped, one that it still fires when the branch moved on.

The tests genuinely detect the bug: forcing hasNewMessagesSince() to always return true makes 3 of them fail.

I also replayed the real captured session (01a13d57, 370 messages) against the patched handler with the daily log truncated to its pre-duplicate state: summary count 4 → 4, handler returns in 4 ms with no LLM call.

Note on /quit

While investigating I noticed the input handler at the bottom of the file that sets exitSummaryReason = "slash-quit" appears to be dead code: interactive-mode.js intercepts /quit and calls shutdown() directly, so the text never reaches session_input → _runInputHandlers → the input event. In a real daily log, every entry reads exit: session-end — none read /quit. I left it alone since it's cosmetic and out of scope, but it may be worth either removing or fixing separately.

Exit summaries snapshot the whole session branch, not "messages since the
last summary". Quitting, resuming the same session, and quitting again
therefore summarizes the identical transcript twice and appends it to the
daily log twice. That is not just noise: today's daily log is re-injected
into every new session, so the duplicate is paid for in context on every
subsequent turn.

Compare the newest message in the branch against the last summary already
recorded for the session, and skip the LLM call entirely when nothing was
added. The scan reads the `<!-- <ts> [<sid>] -->` headers in today's and
yesterday's daily logs; pre-compaction handoffs use the same header shape and
also snapshot the whole branch, so they count as well.

Only `message` entries count. Bookkeeping entries (plan-mode-state,
compact-thinking-duration, agent-summary, ...) are appended on every session
transition and would otherwise defeat the check — in the observed duplicate,
the only entries between the two summaries were three plan-mode-state rows.

The guard fails open: an unidentifiable session, missing branch, or
unparseable timestamp summarizes as before, because losing a summary is worse
than writing a duplicate.

Signed-off-by: MiFaZhan <198522268+MiFaZhan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant