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/Node.js project. Let me explore the structure and understand the concepts involved - cron, LCM, session keys, etc.
Assistant
Let me understand the key concepts: LCM context, session keys, cron, compaction. Let me look at the engine, store, and conversation-store files.
Assistant
Let me look at the engine.test.ts file around line 3052, 3616, 3716, 3814, and 14274, which describe the "stable sessionKey" behavior. These are existing tests. The issue is about isolated cron runs leaking context when they reuse a stable session key.
Let me look at how the engine handles session keys, compaction, and conversation rotation.
Assistant
Now let me look at the engine.ts file, particularly the bootstrap and assemble functions, and how it handles session keys.
Assistant
Let me look at the engine.test.ts around line 5090 and 14274.
Assistant
Let me look at the test at line 14274 about deduplicating replay when runtime sessionId changes but stable sessionKey continues. And look for tests specifically about "isolated" cron runs leaking context.
Let me search the test files for "isolated" and "leak".
Assistant
Let me look at the rotate logic and how the engine handles resume on stable session keys.
Assistant
Let me look at the config to understand the difference between ignoreSessionPatterns and statelessSessionPatterns, and look for any cron-specific config.
Assistant
Let me search the test files for "isolated" to find relevant tests.
Assistant
Let me look at how the engine handles session replacement/rotation and where cron detection might happen.
Assistant
Now let me look at the bootstrap and assemble flow, and how the engine attaches to existing conversation by sessionKey.
Assistant
Let me look at
getOrCreateConversationin the conversation-store.Assistant
Let me look at the docs and the plugin config for cron-related settings.
Assistant
Let me check the default for ignoreSessionPatterns in the config resolver.
Assistant
So by default,
ignoreSessionPatternsis empty. Theagent:*:cron:**is just an example in docs. So cron sessions can be processed by LCM if not ignored.Now, the issue: "Isolated cron runs leak context when they reuse a stable session key". So the scenario is: a cron job runs periodically. Each run is a separate runtime session (different sessionId) but reuses the same stable sessionKey (e.g.
agent:main:cron:nightly). Currently, because the sessionKey is reused,getOrCreateConversationattaches the new run to the prior active conversation, so the prior run's LCM context (summaries, etc.) leaks into the new run. We want each isolated cron run to NOT share the active LCM context — the prior run's conversation should be archived, and a fresh conversation created for the new run.But wait, "compaction within the current run still works" — within a single cron run, compaction should work normally (i.e. compaction operates on the active conversation
Assistant
Assistant
Let me look at how cron sessions are identified in the codebase.
Assistant
So the cron sessions are keyed as
agent:main:cron:*. But the doctor cleaner hardcodesagent:main:cron:%. But that's a hardcoded specific host. The session key pattern in general might beagent:<profile>:cron:<name>.Hmm, but the issue says "No new public interfaces are introduced." So this isn't about adding a config option. It's about an internal behavior fix.
Let me reconsider. The issue is about "isolated cron runs". The key insight is the cron session keys. Let me look at the session-patterns more carefully.
Looking at the session-patterns test again:
- matches (base cron key, no run suffix)
Assistant
Let me look at how the conversation-store getConversationBySessionKey works and the run suffix. Let me look at the session-patterns test fully.
Assistant
Let me look at the plugin/index.ts cron reference and how cron runs are initiated.
Assistant
Let me look at how cron sessions are handled in the plugin handlers - the wirePluginHandlers and how session lifecycle hooks are invoked.
Assistant
Let me look at how
isSubagentSessionKeyis used, and whether there's a similar concept for cron. Let me search the engine forisSubagentandsubagent.Assistant
Let me look at the types.ts to understand the deps interface, and see if there's a cron-related concept. Let me also look at the conversation-scope.ts to understand how scope is determined.
Assistant
Now, let me look at the conversation-scope.ts which seems to determine conversation scope, including subagent detection.
Assistant
That's not the conversation-scope I expected. Let me look at the lcm-conversation-scope.ts tool file.
Assistant
Let me look at the test helpers at the top of engine.test.ts.
Assistant
Let me look at the afterTurn flow around line 6411.
Assistant
Let me search for
getOrCreateConversationusage in engine.ts.Assistant
Let me look at the afterTurn function start.
Assistant
Let me look at
reconcileTranscriptTailForAfterTurnwhich is the entry point that handles the ambiguous rollover.Assistant
Let me look at
reconcileTranscriptTailForAfterTurnInSessionQueuefully to understand the ambiguous rollover handling there.Assistant
This is getting very complex. Let me write a reproduction test to understand the actual behavior with cron sessions reusing a stable session key.
Assistant
I've reproduced the issue. Both cron runs reuse conversation 1, leaking context. Now let me understand where
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingis called so I know all the entry points.Assistant
Let me look at the assemble entry points.
Assistant
Let me look at the structure around the afterTurn reconcile rotation call (line 6418).
Assistant
Interesting - there's a
withTransactionblock that ONLY contains the rotateStale call, then closes. Then the rest of the function is OUTSIDE the transaction (theconst conversation = ...etc are at the same indentation but outside the transaction block). Wait, that's odd. Let me look more carefully at the indentation - theconst conversationis indented with 8 spaces, same as theawait this.conversationStore.withTransactioncontent. But the transaction block closed with});. Soconst conversationis actually... hmm, let me check the brace structure. Actually the indentation suggestsconst conversationis at 8 spaces, but it's after the});that closes the transaction. So it's outside the transaction but still inside the function. Actually wait - the function body is at 4 spaces. Theawait this.conversationStore.withTransaction(async () => {is at 4 spaces. Its body is at 6 spaces. Then});closes it at 4 spaces. Thenconst conversation = ...at 8 spaces?? That's inconsistent indentation. Let me look at the raw bytes more carefully.Assistant
Now I understand the structure. Let me implement the fix. First, let me add the helper methods near
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing. Let me view the exact lines.Assistant
Now let me add the helper methods. I'll insert them right before the
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingmethod.Assistant
Let me use the edit command instead. First, let me find the current line number.
Assistant
I'll insert the helper methods at line 6911 (the blank line after
}at 6910). Let me use the edit command.Assistant
I introduced a stray
}at line 6910. Let me fix it.Assistant
I have a duplicate line at 6912/6913. Let me fix that and also add a blank line before the
Recover lifecyclecomment at 6969.Assistant
Let me add a blank line before the
Recover lifecyclecomment at 6968.Assistant
I have a duplicate line at 6970/6971. Let me fix it.
Assistant
The helpers are in place. Now let me wire the cron rotation call into the three entry points. Let me find the bootstrap call site.
Assistant
Now let me add the cron rotation call before the rotateStale call in bootstrap.
Assistant
I have duplicate lines at 7199/7200 and 7208/7209. Let me fix them.
Assistant
There are leftover lines 7212-7215. Let me remove them.
Assistant
Now there's a duplicate
const ambiguousRollover =at 7212/7213. Let me fix.Assistant
There's still a duplicate at 7213/7214. The edit tool seems to be having issues with duplicates. Let me use a different approach with
sed.Assistant
Bootstrap is clean now. Now let me handle the afterTurn reconcile call site at line 6418.
Assistant
I need to navigate to the line first. Let me use the open command to navigate.
Assistant
Now let me handle the assemble call site. Let me find it.
Assistant
Now let me run the reproduction test to see if the fix works.
Assistant
The fix works. Now let me run the typecheck and the full engine test suite to ensure I didn't break anything.
Assistant
All 317 engine tests pass. Now let me run the broader test suite to make sure nothing else broke.
Assistant
All 50 test files pass (1068 tests). Now let me verify edge cases. Let me check how
handleSessionEndis invoked from the plugin, to make sure cron session_end doesn't conflict with my isolation logic.Assistant
All scenarios work. Now let me verify the within-run continuity edge case (compaction within a single cron run must keep the same conversation). Let me extend the repro test.
Assistant
All edge cases pass. Now let me add a changeset since this is a user-facing behavior fix. Let me check the changeset format.
Assistant
Let me check the docs for where to add a behavior note about cron isolation.
Assistant
Let me add a behavior note about cron isolation after the session pattern matching example.
Assistant
The AGENTS.md mentions keeping
skills/lossless-claw/references/config.mdconsistent with docs. Let me check whether it has a relevant section.Assistant
Now let me remove my temporary repro test file before submitting (the instructions say not to add new tests to the repository).
Assistant
Let me run the full test suite once more to confirm everything passes without the repro test, and confirm the typecheck.
Assistant
All tests pass (1067) and typecheck is clean. Let me review my final diff.
Assistant
The diff looks clean. Let me do a final sanity check for any accidental duplicate definitions or call sites.
Assistant
The implementation is complete with 1 definition each and 3 call sites (bootstrap, afterTurn, assemble). Let me verify the changeset file format.
Assistant
Everything is in order. Let me do a final review of the complete diff.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.