fix: don't resurrect completed sub-agents from history rescan (v0.2.4)#2
Merged
fix: don't resurrect completed sub-agents from history rescan (v0.2.4)#2
Conversation
scanSessionState re-walks every message part on every reactive tick. For background tool parts already in status='completed' (i.e. the launch already finished), we never observed the prior pending/running events because either the plugin wasn't loaded yet or this is a TUI restart. The previous code unconditionally set up an entry with status='running' and startedAt = part.time.start (potentially hours old). On the next tick handleBackgroundStatusText would match the same message's [BACKGROUND TASK COMPLETED] / [ALL BACKGROUND TASKS COMPLETE] system reminder text and call completeEntry with completedAt = Date.now() (system reminders carry no time field). Result: a phantom 'Done 1230m' row that lingered for retention before being pruned. v0.2.3 froze re-stamping after the first stamp, but the very first stamp itself was bogus on rescans. This patch closes that hole: - upsertToolPart's background-completed branch now early-returns when no in-flight entry exists. Live launches always pass through pending/running first, so missing those states means historical replay; resurrecting is wrong by construction. - handleBackgroundStatusText's [ALL BACKGROUND TASKS COMPLETE] new-entry fallback is removed for the same reason.
There was a problem hiding this comment.
Pull request overview
Fixes a sidebar UI bug where completed background sub-agents could be temporarily “resurrected” during session history rescans (e.g., after plugin load / TUI restart), causing phantom “Done 1230m” rows until retention pruning removed them.
Changes:
- Prevent
upsertToolPartfrom creating a new background entry when it observesstatus="completed"without any in-flight tracking entry (treat as historical replay). - Prevent
handleBackgroundStatusTextfrom fabricating completed background entries from summary reminder text when no in-flight entry exists. - Bump plugin/package version to
v0.2.4and regeneratedist/outputs.
Reviewed changes
Copilot reviewed 2 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/tui.ts | Adds guards to avoid resurrecting historical completed background entries during rescans; bumps plugin version constant. |
| package.json | Version bump to 0.2.4. |
| dist/tui.js | Regenerated build reflecting the rescans/guard changes and version bump. |
| dist/tui.js.map | Regenerated sourcemap output. |
| dist/tui.d.ts.map | Regenerated types sourcemap output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root cause
`scanSessionState` re-walks every message part of the session on every reactive tick (it's invoked from inside the panel's `insert(...)` accessor, which depends on `now()` and `version()`). It was designed to bootstrap state on plugin load.
For a background tool part already in `status: "completed"` — meaning the launch finished before this scan ran — we never observed the prior `pending` / `running` events. The old code did three things in sequence on rescan:
Result: `elapsed = Date.now() - startedAt` ≈ 1230 minutes. The v0.2.3 "stamp once" fix kept the row from getting worse, but the very first stamp itself was bogus on history rescans.
Why early-return is safe
Live launches deterministically pass through `pending` → `running` → `completed`. By the time a `status: "completed"` event is observed live, the entry is already in the `active` map (created by the `pending` / `running` branch a few hundred ms earlier). The only path that hits "`completed` with no existing entry" is a history rescan, where the entry has no business existing on the sidebar in the first place.
The same reasoning applies to `handleBackgroundStatusText`'s prior fallback that fabricated a row from an `[ALL BACKGROUND TASKS COMPLETE]` summary line alone.
Change
```diff
const bgKey = makeKey("background", bgID);
const promoted = active.get(bgKey);
if (promoted) return touchEntry(promoted, agent, description);
-active.set(bgKey, {
-});
-return true;
+// Live launches always pass through pending/running before reaching
+// completed, so observing status="completed" with no in-flight entry
+// means scanSessionState is replaying a historical message. Resurrecting
+// here would stamp startedAt to part.time.start (potentially hours old)
+// and let handleBackgroundStatusText finalize completedAt = Date.now()
+// on the next tick, producing a spurious "Done 1230m" row before
+// retention prunes it.
+return false;
```
Verification
Files changed
Related