Repository navigation
fix(gmi): manager shutdown waits for creations in flight, bounds each GMI and runs once - #222
Conversation
getOrCreateGMIForSession() checks the manager's initialization only when it starts. A call still waiting on the tier check, the cognitive memory factory or GMI.initialize() when shutdown() runs registers its new GMI afterwards, so the manager holds a READY GMI after shutdown and that GMI's memory is never closed. The case holds the creation for session-3 in the cognitive memory factory, starts shutdown(), then releases the factory. The call must fail with NOT_INITIALIZED, the GMI it built must be shut down, and both maps must end empty.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Reviewer's GuideAdds a deterministic real-manager regression test for a GMI created while shutdown starts, verifying the late-created GMI is rejected, shut down, and removed along with all existing GMIs. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (6)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
PR Summary by Qodotest(gmi): cover shutdown during in-flight GMI creation
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo
1.
|
…registered getOrCreateGMIForSession() checked the manager's initialization only when it started. A call still waiting on the tier check, the cognitive memory factory or GMI.initialize() when shutdown() ran registered its GMI after the maps were cleared, and that GMI stayed READY with its memory open. The call now checks again after every wait. A GMI it built and has not registered when shutdown has begun is shut down through the manager's per-GMI shutdown, and the call fails with NOT_INITIALIZED. A call that registered its GMI before shutdown began also fails once it resumes, and shutdown() shuts that GMI down with the others. shutdown() waits for every call still in flight before it clears the maps.
GMIManager.shutdown() waits for each GMI's shutdown in turn with no time limit. A GMI whose cognitive memory is still closing (a consolidation cycle waiting on its model call) holds the loop, as does one whose metaprompt drain runs its full 5 seconds, so AgentOS.shutdown() takes the sum over all GMIs, or never returns, before its later steps run. The case keeps the cognitive memory of three GMIs open and sets shutdownTimeoutMs to 50. Under fake timers every GMI must begin closing at once, shutdown() must resolve after one bound, one warning per session must be logged, and both maps must end empty.
…limit GMIManager.shutdown() waited for each GMI's shutdown in turn with no time limit. AgentOS.shutdown() therefore took the sum over all GMIs (a metaprompt drain alone can take 5 seconds), or never returned when a GMI's cognitive memory never finished closing, and its later steps never ran. shutdown() now shuts every GMI down at the same time. A new GMIManagerConfig option, shutdownTimeoutMs (default 8000: inside the 10 seconds docker stop waits, above the 5-second metaprompt drain), bounds each GMI's shutdown wherever the manager shuts one down. A GMI that has not finished by then is logged at warn with its session and finishes on its own; the maps are cleared either way. The wait for creations in flight has the same bound. docs/GMI.md describes the manager's shutdown.
shutdown() finds GMIs only through the session map. Two getOrCreateGMIForSession() calls for one new session that run at the same time both build a GMI, and the later registration takes the session entry from the earlier one, which then sits in the active map alone. shutdown() clears it from the map without shutting it down, so its metaprompt queue and memories stay open. The case races two creations for session-9 and checks that shutdown() shuts the GMI left without a session entry down once and waits for it.
shutdown() found GMIs only through the session map and then cleared the active map, so a GMI left there without a session entry (two creations for one new session raced, and the later registration took the entry) was dropped while still READY, with its metaprompt queue and memories open. shutdown() now also shuts down every GMI in the active map that no session maps to, in the same pass and within the same bound as the sessions, before it clears both maps.
GMI.shutdown() does not stop a turn that is running, and the turn keeps its hold on the lifecycle state. A turn waiting on its model call when the manager shuts down goes on once the call answers, and its finally block writes READY over SHUTDOWN, so a GMI that has been shut down reports READY. The case runs a turn against a provider that holds the call open until it is aborted or answered, shuts the manager down while the call is open, then lets the provider answer. The turn must end with the abort error and an ERROR chunk, and the GMI must read SHUTDOWN afterwards.
GMI.shutdown() did not stop a turn that was running, and the turn kept its hold on the lifecycle state. A turn waiting on its model call when the GMI shut down went on once the call answered, wrote to memories that were closed, and its finally block wrote READY over SHUTDOWN. Each turn now runs under a stop signal of its own, which is what the model call receives. The caller's abort signal (a session's close()) aborts it as before, and shutdown() aborts it too, so the model call in progress ends with the provider's abort chunk, or the gateway's, and the turn ends with the abort error. shutdown() takes the lifecycle state back before it stops the turns, so a turn that ends afterwards writes neither READY nor ERRORED, and it waits for the stopped turns at most the shutdown bound before it closes the memories. GMIBaseConfig takes an optional shutdownTimeoutMs (default 8000); GMIManager passes its own. docs/GMI.md describes the behaviour.
GMI.shutdown() returns early only once the state is SHUTDOWN, so a call made while another is running starts the drain and the memory close again. The manager reaches this whenever two shutdowns overlap: a second manager shutdown (SIGINT, then SIGTERM), or a shutdown during a session's deactivation, sees the session still mapped and shuts its GMI down a second time, and a cognitive memory from a factory that is not idempotent is closed twice. The case calls shutdown() twice at once on one GMI, then runs two manager shutdowns at once. Each GMI's memory must close once, every call must resolve, and both maps must end empty.
GMI.shutdown() returned early only once the state was SHUTDOWN, so a call made while another was running drained the metaprompt queue and closed the memories a second time. The manager reached this whenever two shutdowns overlapped: a second manager shutdown (SIGINT, then SIGTERM), or a shutdown during a session's deactivation, saw the session still mapped and shut its GMI down again, and a cognitive memory from a factory that is not idempotent was closed twice. One shutdown now runs at a time: a call made while one runs returns the same promise, so every caller waits for the same work and the memories are closed once. A call after the shutdown has finished returns at once, as before. The manager's deactivations, cleanup and shutdown all go through GMI.shutdown(), so they share it too. docs/GMI.md says so.
## [0.13.23](v0.13.22...v0.13.23) (2026-10-10) ### fix * **gmi:** follow-ups from the reviews of #193 and #199 (#217) ([2bacb69](2bacb69)) * **gmi:** manager shutdown waits for creations in flight, bounds each GMI and runs once (#222) ([d99b126](d99b126)) * **images:** the untrusted fetch settles on a protocol upgrade, and four review follow-ups (#227) ([9efb4b5](9efb4b5)) * **vision:** the TrOCR and Florence-2 tiers get a Buffer image as a Blob (#229) ([7fe444a](7fe444a))
Summary
Follow-ups from the review of #210, which made
GMIManager.shutdown()reach every GMI in the session map. Five paths still leave a GMI running after the manager shuts down, or let one GMI holdAgentOS.shutdown()open. Each fix lands after a regression test that CI ran red; every case drives a realGMIManagerwith real GMIs and a recorder in place of the cognitive memory (src/cognition/substrate/__tests__/GMIManager.shutdown.test.ts).getOrCreateGMIForSession()checks the manager's initialization again after every wait. A GMI it built and has not registered once shutdown has begun is shut down and the call fails withNOT_INITIALIZED;shutdown()waits for the calls in flight before it clears the maps. Done.shutdown()shuts every GMI down at the same time, and a newGMIManagerConfig.shutdownTimeoutMs(default 8000) bounds each one wherever the manager shuts a GMI down. A GMI that has not finished by then is logged at warn with its session and finishes on its own; the maps are cleared either way, soAgentOS.shutdown()takes about one bound, not a sum. Done.GMI.shutdown()stops the running turn and keeps theSHUTDOWNstate: each turn's model call runs under a stop signal that the caller's abort signal (a session'sclose()) andshutdown()both abort, so the call ends with the provider's or the gateway's abort chunk and the turn ends with the abort error.shutdown()takes the lifecycle state back first, so a turn that ends afterwards writes neitherREADYnorERRORED, and it waits for the stopped turns within the shutdown bound before it closes the memories.GMIBaseConfigtakes an optionalshutdownTimeoutMs(default 8000), which the manager sets to its own. Done.GMI.shutdown()returns the running shutdown's promise to a call made while it runs, so a second manager shutdown (SIGINT, then SIGTERM), a deactivation or a cleanup that overlaps it waits for the same work, and the memories are closed once. A call after the shutdown has finished returns at once, as before. Done.Linked issues
None.
Type of change
How this was verified
CI only. Each regression test was pushed alone and read red with only its own case failing, then the fix was pushed and read green:
The branch then merged master (2c41dac); the CI run on that head is the one this pull request merges on.
Migration notes
None. No signature changes; the new options have defaults.
Checklist
!before the colon for a change that breaks users (feat!:orfeat(api)!:).Summary by Sourcery
Make GMI shutdown bounded, concurrent, and idempotent while reliably cleaning up creations, active turns, and orphaned instances.
Bug Fixes:
Enhancements:
Documentation:
Tests:
Summary by CodeRabbit