#12042·qwen-code

core/cli: record provenance does not survive the api-history projection, so two notification shapes are still mis-classified after PR #12007

Author: yiliang114Created Sep 16, 2026Updated Sep 30, 2026
Labelspriority/P2type/bugcategory/corescope/session-management

What

detectTurnInterruption() classifies from Content[], projected from the persisted ChatRecord[]. The record's authoritative provenance field ('system' for notification/cron records, 'real_user' for anything a client actually submitted) does not survive that projection — packages/core/src/services/session-api-history.ts reads subtype for four specific cases and never reads provenance at all — so the classifier can only fall back to a text-shape heuristic (isSystemNotificationContent in packages/core/src/core/turn-interruption.ts) to decide which trailing entries are system-injected and must be trimmed before the verdict is read.

PR #12007 narrowed that heuristic and closed the two ways it could certify a wrong result. Two shapes remain that no predicate at this layer can reach. Both were measured during that PR's round-2 review (threads R2-2 and R2-3) and are written down in the code (turn-interruption.ts:136, "Residual, by shape alone") and in docs/design/session-crash-recovery/session-crash-recovery-interruption-detection.md:205 ("Not covered yet"). Filing so they stay searchable after that PR merges — one of the two threads is already resolved.

A. A failed live notification turn with no reminders is certified clean

An automatic notification turn builds one user entry {role:'user', parts:[...notificationReminders, activeTodoReminder?, ...notificationParts]} and LlmClient.sendMessageStream pushes it into live history once, before any attempt. If the stream then fails mid-turn with no functionCall delivered, willPersistToHistory is false, so no model entry is pushed and that notification entry is the history tail. The catch in Session.ts emits [notification error] and calls finishBackgroundNotificationTurn('end_turn', true) without re-queueing — the only notificationQueue.unshift sites are 10720, 11005 and 11055, none of them on that path — and the finally clears notificationAbortController, so #hasActiveTurn() is false.

Recovery then reports {kind:'clean', canContinue:false}: no Continue, and nothing else re-drives the turn, because enqueueBackgroundNotification short-circuits on persistedBackgroundNotificationTaskIds (Session.ts:10592), which collectSessionTurnState primes from the transcript's own notification records (Session.ts:4554). The background agent's terminal result reaches the model only if the user happens to send another prompt.

This is the textbook interrupted_prompt the module's own doc comment describes — a turn that ran and errored.

Reachable in practice whenever the entry carries at least one reminder part (plan mode, an output style, an active todo_write chain): with a reminder present the narrowed predicate does not trim it, and it recovers correctly as interrupted_prompt. The residual case is a session with none of those, where the entry is a single bare envelope — shape-identical to a cold notification record.

B. A real prompt whose whole text is an envelope loses its recovery banner

A client submits a prompt whose entire text is a <task-notification> envelope — a user pasting one out of a transcript to ask about it, or a channel/SDK client forwarding one verbatim. createBaseRecord('user') stamps it provenance:'real_user', so the ledger's attribution loop counts it as the target turn's own write; but the classifier, seeing a user-role entry whose every part is structural, trims it and exposes the previous turn's model text as the tail.

The ledger half is fixed in #12007: prompt-terminal-ledger.ts now tracks the last visible non-system record type and fails closed instead of appending {terminal:'completed', stopReason:'reconstructed_from_transcript'} for a prompt the model never answered. The recovery half is not: buildSessionRecoveryPlan still reports kind:'clean', canContinue:false, so the orphaned prompt is silently dropped with no banner and no Retry, where on main it recovered as interrupted_prompt with canContinue:true.

Both A and B fail in the safe direction now (a missing Continue rather than a fabricated terminal), which is why they are split out rather than blocking.

Proposed direction

Have SessionApiHistoryAccumulator also report, for each projected entry, whether it came from a provenance:'system' record — a per-entry parallel flag, not an index set, because appendApiHistoryRecord can fold a record into an existing entry via the mid_turn_user_message merge. Then let both buildSessionRecoveryPlan and prompt-terminal-ledger.ts take the classifiable end from that authoritative channel, keeping the text matcher only as a metadata-free fallback for the live-history callers that have nothing but Content[].

That removes the classifier's dependence on text shape entirely for cold-restored sessions, which is where both A and B live. The alternative for A specifically is a daemon-side re-drive of a failed notification turn, which does not need the projection change but does need a decision about retry budget.

Why this was not done in #12007

It is a change to a shared exported accumulator contract with three consumers, against a PR that was already at its size budget and whose central claim had been A/B-proven at the previous head.

Environment

Measured at PR #12007 head 63e45915140c1d26d30916ec14a2739dbbc4812f, base main at b8def02aadfc384ecb860909155d196267c4fa0c.

Refs #11995 (the report #12007 fixes), #12038 (the envelope emitter/matcher duplication split out of the same review).