Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
Adjusts subagent orphan cleanup/recovery behavior to avoid prematurely pruning in-flight sessions and to improve testability of orphan recovery logic.
Changes:
- Adds an orphan-pruning grace window (
ORPHAN_GRACE_MS) to reduce false-positive orphan detection during startup reconciliation. - Reorders context-engine “subagent ended” notification to occur after a
sessions.deleteattempt in the sweeper. - Introduces a small DI seam (
OrphanRecoveryDeps) to allow injectingloadConfig/callGatewayin orphan recovery code.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/agents/subagent-registry.ts | Moves context-engine notification later in the sweep flow, but sweep deletion/retry behavior still needs correction. |
| src/agents/subagent-registry-helpers.ts | Adds ORPHAN_GRACE_MS and grace-window logic to resolveSubagentRunOrphanReason. |
| src/agents/subagent-orphan-recovery.ts | Adds dependency-injection seam for loadConfig and callGateway and threads it through recovery flow. |
Comments suppressed due to low confidence (1)
src/agents/subagent-registry.ts:476
sweepSubagentRuns()still deletes the run record (subagentRuns.delete) before callingsessions.delete. Ifsessions.deletefails (gateway down/timeout), the run record is already gone and there’s no retry path, leaving an orphaned session-store entry. To match the PR intent, only delete/persist the run record (and consider attachment cleanup) aftersessions.deletesucceeds; on failure keep the run entry so the next sweep can retry.
clearPendingLifecycleError(runId);
subagentRuns.delete(runId);
mutated = true;
// Archive/purge is terminal for the run record; remove any retained attachments too.
await safeRemoveAttachmentsDir(entry);
Greptile SummaryThis PR adds three safeguards around subagent orphan handling: a 5-minute grace window ( The grace window and DI seam changes look correct and well-structured. However, the
This means the retry-on-failure path described for the sweep loop does not exist, and a Confidence Score: 2/5Not safe to merge as-is: the sweep loop's described retry-on-failure behavior is unimplemented, leaving a class of gateway session leaks unaddressed. The grace window and DI seam are implemented correctly, but the core sweep ordering fix described in the PR summary was not actually made. The run record is still deleted before the gateway call and failures are silently swallowed — this is not a regression from
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/subagent-registry.ts
Line: 472-496
Comment:
**Run-record deletion is not actually reordered**
The PR description states: *"`sweepSubagentRuns()` now deletes run record only after gateway `sessions.delete` succeeds. On failure, logs warning and retries next sweep cycle."* But the diff shows none of that was implemented.
`subagentRuns.delete(runId)` is still at line 473, unconditionally, **before** the `callGateway` call at line 478 — the same position as in the pre-PR code. The `catch` block still contains only `// ignore` with no warning and no retry: if `sessions.delete` fails, the in-memory run record is already gone and the failure is silently swallowed. There is no mechanism to retry on the next sweep cycle.
The one real change here — moving `notifyContextEngineSubagentEnded` to fire after `sessions.delete` rather than before — is a genuine improvement. But the described run-record reordering and retry-on-failure behaviour are absent.
If the intent is to only delete the run record after the gateway confirms the session is gone and retry on failure, the sweep body should be restructured so that `subagentRuns.delete` / `mutated = true` / `notifyContextEngineSubagentEnded` are inside the success path, and the `catch` block logs a warning and leaves the record intact for the next sweep cycle:
```ts
try {
await subagentRegistryDeps.callGateway({
method: "sessions.delete",
params: { key: entry.childSessionKey, deleteTranscript: true, emitLifecycleHooks: false },
timeoutMs: 10_000,
});
// Only clean up after gateway confirms deletion
subagentRuns.delete(runId);
mutated = true;
await safeRemoveAttachmentsDir(entry);
void notifyContextEngineSubagentEnded({
childSessionKey: entry.childSessionKey,
reason: "swept",
workspaceDir: entry.workspaceDir,
});
} catch (err) {
log.warn(`sweepSubagentRuns: sessions.delete failed for ${runId}, will retry next cycle: ${String(err)}`);
// Leave the record intact; archiveAtMs already elapsed so next sweep retries.
}
```
Alternatively, if the intention was only to reorder the notification (the actual change), the PR description should be corrected so reviewers and future readers are not misled about what #49004 guarantees.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(agents): improve orphan reconciliati..." | Re-trigger Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d765b86238
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Related issues: This PR also relates to #61741 (race condition in subagent/session cleanup causes late child stdout to hit cleared active run, leading to announce timeout and orphaned child processes), #44925 (subagent completion silently lost — no retry, no notification, no auto-restart on timeout), and #53810 (subagent premature announce bug — model-specific tool call handling issues). These share the same subagent lifecycle and orphan reconciliation code paths. Note: the original linked issue #49004 is now closed. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6c969be97
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0584c223fe
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Refreshed this PR on current What is in scope now:
What is intentionally out of scope now:
Those startup/orphan protections are being split into a separate follow-up branch so they can be reviewed independently from the sweep retry fix. Targeted validation:
|
|
Merged via squash.
Thanks @100yenadmin! |
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
…penclaw#61801) Merged via squash. Prepared head SHA: 1152c26 Co-authored-by: 100yenadmin <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
Summary
sessions.deletesucceedsWhat this fixes
When delete-mode subagent cleanup reaches the archive sweep path, a failed
sessions.deletecall should not permanently lose the run record. This PR keeps the archived run entry around for retry and prevents a second sweep pass from overlapping the same in-flight delete.What changed
sessions.deletebefore deleting the archived run record during sweepsessions.deletefails so the next sweep can retrysweepInProgressguard so overlapping sweep intervals do not duplicate delete attemptsScope
Files changed
src/agents/subagent-registry.tssrc/agents/subagent-registry.archive.e2e.test.tssrc/agents/subagent-registry.persistence.test.tsValidation
pnpm test src/agents/subagent-registry.persistence.test.tspnpm test src/agents/subagent-registry.archive.e2e.test.ts