ISSUE DESCRIPTION Isolated cron runs leak context when they reuse a stable session key…
- User
ISSUE DESCRIPTION
Isolated cron runs leak context when they reuse a stable session key
Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context. Prior runs remain archived, compaction within the current run still works, and ordinary non-cron session continuity is unchanged.
Interface
No new public interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /lossless-claw, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
This is a TypeScript project. Let me explore the source structure, particularly around cron, session, and LCM context.
Assistant
This is a big file. Let me search for the relevant concepts: session key, cron, context, active context, stable session key.
Assistant
Let me search more specifically for "cron" and related concepts throughout the codebase.
Assistant
Now I understand. Cron session keys look like
agent:main:cron:nightly:run:run-123oragent:main:cron:nightly. The issue is about isolated cron runs that reuse a stable session key. Let me understand the conversation store and how conversations are resolved by session key.Let me look at the conversation store's
resolveConversationBySessionKeyor similar.Assistant
Now I understand the data model. Let me look at the engine's bootstrap and conversation resolution paths.
Assistant
Let me understand
isStatelessSessionand the session key structure for cron.Assistant
Let me look at the existing cron-related tests to understand the expected behavior.
Assistant
Let me check the default config for ignoreSessionPatterns and statelessSessionPatterns.
Assistant
So there's no default ignore pattern for cron in the config. The tests use
ignoreSessionPatterns: ["agent:*:cron:**"]to ignore cron sessions. But by default cron sessions are NOT ignored.Let me check the
resolvePatternArrayto see if there are defaults.Assistant
Let me look at the rollover/rotation logic for session keys.
Assistant
Let me check the specs directory and changesets for any specification about this issue.
Assistant
These changesets describe the existing behavior. The new issue is about cron runs that reuse a stable session key. Let me look at the docs/configuration.md for cron mentions.
Assistant
Let me check the lcm-doctor-cleaners.ts which identifies cron sessions.
Assistant
Let me check the git history and any related test that might already exist or hint at the intended approach.
Assistant
Let me see where the rotation methods are called in assemble and afterTurn.
Assistant
Let me look at the test helpers to write a reproduction script.
Assistant
Now let me write a reproduction script to confirm the context leak. Let me look at how bootstrap is tested with session files, and understand the assemble flow.
Assistant
Let me write a reproduction test. First, let me look at the afterTurn signature and an existing test that uses bootstrap + afterTurn together.
Assistant
Let me create a reproduction test file based on existing test patterns.
Assistant
Now let me create a reproduction test file.
Assistant
I've reproduced the issue. The current behavior fails closed with "ambiguous session-key runtime rollover" and leaves the prior run's conversation active. Now let me implement the fix. First, let me add a cron session key helper near the other module functions.
Assistant
Now let me add the cron helper constant and the
isCronSessionKeyfunction. Let me add it after the AMBIGUOUS constant:Assistant
Now let me add the module-level helper. Let me add it right after the
AMBIGUOUS_SESSION_KEY_RUNTIME_ROLLOVER_REASONconstant:Assistant
Now let me add the
rotateCronSessionKeyConversationOnNewRunmethod right afterrotateStaleSessionKeyConversationIfTrackedTranscriptMissing. Let me find its location now (line numbers shifted by +15).Assistant
Now let me add the new method right after
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing(beforefindAmbiguousSessionKeyRuntimeRollover):Assistant
Now let me wire the new method into the bootstrap, assemble, and afterTurn flows. Let me find the bootstrap call site.
Assistant
I need to use the
open/gototo navigate. Let me usegototo make the line visible, then runreplace.Assistant
Now the assemble call site. Let me view it:
Assistant
Now let me add the cron rotation to the assemble flow (inside the same transaction, before the stale rotation):
Assistant
Now the afterTurn call site (in
reconcileTranscriptTailForAfterTurnInSessionQueue):Assistant
Let me look at the broader context of this afterTurn reconcile method to understand its structure.
Assistant
The indentation here is interesting — the code after the
});at line 6440 is at 8 spaces indentation, which is the same as if it were inside a block. This is actually existing code (likely thewithTransactioncallback was originally larger and got refactored, leaving the deeper indentation). It's syntactically valid because it's just a sequence of statements at function-body level (the deeper indentation is cosmetic). I shouldn't worry about it.Now let me add the cron rotation call inside the transaction, before the stale rotation. Let me do the replace.
Assistant
Now let me run the typecheck and the reproduction test.
Assistant
Typecheck passes. Now let me run the reproduction test.
Assistant
Both tests pass. Now let me add a third test scenario: compaction within the current run still works, and prior runs remain archived. Let me also test the edge case where the cron run's transcript actually re-contains prior content (the original leak scenario). Let me first extend my repro test to also verify compaction works and that a third run archives the second.
Assistant
All 4 pass. Now let me run the broader engine test suite to ensure I haven't broken anything.
Assistant
All engine tests pass. Let me run the full test suite to be thorough.
Assistant
All 1067 tests pass. Now let me add a changeset following the repo convention.
Assistant
Let me verify the afterTurn reconcile method is called within a session queue lock (to be consistent with the other phases).
Assistant
The
reconcileTranscriptTailForAfterTurnInSessionQueueis called withinwithSessionQueue(viareconcileTranscriptTailForAfterTurn). And the rotation happens insidewithTransaction. So the cron rotation in afterTurn is within both the session queue and a transaction. Consistent with bootstrap (which is within withSessionQueue + transaction) and assemble (withSessionQueue + transaction). Good.Now, let me also check the other call site at 10669.
Assistant
This is the
reconcileRawTranscriptForRotatepath, used during rotate (a session storage rotate operation). This is within the same cron run (same sessionId), so the cron rotation would be a no-op (same sessionId). Good — no concern there. Even if it fires, it's safe.Now, let me also double-check there's no issue with the cron rotation being called during a
/rotatefor a cron session where the sessionId is the same. Same sessionId → no-op. Good.Let me now run the typecheck once more and verify the full diff.
Assistant
The changeset is untracked. The submit mechanism uses
git diffwhich captures tracked-file changes (the core engine.ts fix). The changeset is a repo-convention bonus. Let me do a final review of the complete diff.Assistant
Let me look at the rest of the afterTurn reconcile method to ensure a fresh replacement conversation created by the cron rotation is handled correctly.
Assistant
If the cron rotation creates a fresh replacement C2 (empty, no checkpoint), then in the afterTurn reconcile,
conversation= C2 (not null). It getscheckpoint= null (no bootstrap state for fresh C2). ThensessionFileStateis read.transcriptEpochShrank= false (checkpoint null). Theif (checkpoint && ...)block is skipped (checkpoint null). Then it falls through to... let me see what's after. There must be a fallback slow-path full read. Let me check the rest.Assistant
For a fresh replacement C2 (no checkpoint), the reconcile slow path with reason="checkpoint-missing" would read the full transcript and import messages. This is fine — the new run's transcript gets imported into the fresh conversation. So even if afterTurn is the first call (no bootstrap), the new run's messages get imported into the fresh isolated conversation.
Let me run the build to ensure the bundle still compiles.
Assistant
Build works. Let me confirm the dist output isn't accidentally tracked and check final status.
Assistant
dist/ is ignored, good. Let me do a final full test run to confirm everything passes after all edits.
Assistant
All 1067 tests pass. Let me verify the final state of my changes one more time and confirm the changeset content.
Assistant
All 1067 tests pass. Let me also check the
compactandmaintainphases to see if they could leak context for a cron run that hasn't bootstrapped yet (defensive completeness check).Assistant
The
compactandmaintainphases resolve conversations bysessionId/sessionKeybut, like the existingrotateStaleSessionKeyConversationIfTrackedTranscriptMissingguard, the cron rotation is applied in the three lifecycle entry points (bootstrap/assemble/afterTurn) that establish and persist a run's conversation. Compaction within a run operates on the conversation that bootstrap/afterTurn already isolated, so "compaction within the current run still works" holds, and my fix is consistent with the existing rollover-guard design (which also only guards those three phases).Let me do a final verification of the complete diff and run the typecheck once more.
Assistant
Everything is in order. Let me write up my final reasoning against the checklist before submitting.
Checklist review:
-
Reproduced the error: Yes. I wrote a reproduction test simulating two isolated cron runs (
agent:main:cron:nightly) reusing a stable session key under different runtime sessionIds. The current code failed withreason: "ambiguous session-key runtime rollover", bootstrapping nothing while leaving the prior run's conversation active under the shared key — i.e. context leakage.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.