Repository navigation
fix(restart): wait for session event queue before clearing active run - #41547
pingfanfan wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes a restart race condition where The fix introduces a new
Confidence Score: 4/5
Last reviewed commit: 4dc806c |
| describe("waitForAgentSessionEventQueue", () => { | ||
| it("waits for the queued session event work to finish", async () => { | ||
| const resolved: string[] = []; | ||
| let releaseQueue: (() => void) | undefined; | ||
| const queue = new Promise<void>((resolve) => { | ||
| releaseQueue = () => { | ||
| resolved.push("queue"); | ||
| resolve(); | ||
| }; | ||
| }); | ||
|
|
||
| const waitPromise = waitForAgentSessionEventQueue({ | ||
| session: { _agentEventQueue: queue }, | ||
| onTimeout: () => { | ||
| resolved.push("timeout"); | ||
| }, | ||
| }).then(() => { | ||
| resolved.push("wait"); | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(resolved).toEqual([]); | ||
|
|
||
| releaseQueue?.(); | ||
| await waitPromise; | ||
|
|
||
| expect(resolved).toEqual(["queue", "wait"]); | ||
| }); | ||
|
|
||
| it("returns immediately when no event queue is exposed", async () => { | ||
| await expect( | ||
| waitForAgentSessionEventQueue({ | ||
| session: {}, | ||
| }), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("times out instead of hanging forever on a stuck queue", async () => { | ||
| vi.useFakeTimers(); | ||
| try { | ||
| const events: string[] = []; | ||
| const waitPromise = waitForAgentSessionEventQueue({ | ||
| session: { _agentEventQueue: new Promise(() => {}) }, | ||
| timeoutMs: 250, | ||
| onTimeout: () => { | ||
| events.push("timeout"); | ||
| }, | ||
| }).then(() => { | ||
| events.push("wait"); | ||
| }); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(250); | ||
| await waitPromise; | ||
|
|
||
| expect(events).toEqual(["timeout", "wait"]); | ||
| } finally { | ||
| await vi.runOnlyPendingTimersAsync(); | ||
| vi.useRealTimers(); | ||
| } | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Missing test for rejected queue
waitForAgentSessionEventQueue explicitly guards against a rejecting queue with .catch(() => undefined) (line 27 of session-event-queue.ts), but there is no test covering that path. Without a test, a future refactor could silently remove that guard and introduce an unhandled rejection at cleanup time.
Consider adding a case like:
it("resolves without throwing when the event queue rejects", async () => {
await expect(
waitForAgentSessionEventQueue({
session: { _agentEventQueue: Promise.reject(new Error("queue error")) },
}),
).resolves.toBeUndefined();
});Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/session-event-queue.test.ts
Line: 4-64
Comment:
**Missing test for rejected queue**
`waitForAgentSessionEventQueue` explicitly guards against a rejecting queue with `.catch(() => undefined)` (line 27 of `session-event-queue.ts`), but there is no test covering that path. Without a test, a future refactor could silently remove that guard and introduce an unhandled rejection at cleanup time.
Consider adding a case like:
```ts
it("resolves without throwing when the event queue rejects", async () => {
await expect(
waitForAgentSessionEventQueue({
session: { _agentEventQueue: Promise.reject(new Error("queue error")) },
}),
).resolves.toBeUndefined();
});
```
How can I resolve this? If you propose a fix, please make it concise.|
Closing this to reduce my active PR queue and keep review load focused. I may resubmit later after the current set is resolved. |
|
For skills not loading - check skill path exists and has SKILL.md file. Happy to help debug - email [email protected] |
Summary
Fixes a restart race where embedded runs could be removed from the active-run registry before the pi-coding-agent session event queue had finished draining. That let
update.run/gateway restartemitSIGUSR1while transcript/session persistence was still settling.Root cause
runEmbeddedAttempt()clearedACTIVE_EMBEDDED_RUNSin its cleanup path immediately after unsubscribing. ButAgentSessionpersists transcript/session updates through its internal_agentEventQueue, and that queue can still be draining after the run itself looks finished.That meant restart deferral could observe
getActiveEmbeddedRunCount() === 0too early and restart the gateway before transcript writes had finished.Changes
waitForAgentSessionEventQueue()to wait for the pi-coding-agent session event queue to settle with a bounded timeoutrunEmbeddedAttempt()beforeclearActiveEmbeddedRun(...)Testing
corepack pnpm exec vitest run src/agents/pi-embedded-runner/session-event-queue.test.ts src/agents/pi-embedded-runner/run/attempt.test.ts src/agents/pi-embedded-runner/runs.test.tscorepack pnpm exec oxlint --type-aware src/agents/pi-embedded-runner/run/attempt.ts src/agents/pi-embedded-runner/session-event-queue.ts src/agents/pi-embedded-runner/session-event-queue.test.tsAI-assisted. I reviewed the changed code and ran the targeted tests and lint above locally.
Fixes #41538