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
I'll start by exploring the repository to understand the codebase and locate relevant files. Let's first get an overview.
- Assistant
Let me look at the top-level structure of the repository first.
- Assistant
Let me look at the AGENTS.md for more context:
- Assistant
Let me explore the src directory to understand the codebase structure:
- Assistant
Let me look at the structure more comprehensively to understand the codebase.
- Assistant
Now let me search for terms related to session management, context, and cron:
- Assistant
Let me search for "cron" and "context":
- Assistant
Now let me search for "context":
- Assistant
Let me look at the engine.ts file since it seems to be central to session management:
- Assistant
I need to look for the session management and how sessions are isolated. Let me search for "stable session key" and "isolated" or "reuse" or similar concepts.
- Assistant
Let me read the engine.ts file to understand how sessions and context are managed:
- Assistant
Let me search for key patterns in the engine:
- Assistant
Let me search for "context" and "LCM" together:
- Assistant
Let me look for how conversations are created and how session keys map to conversations:
- Assistant
Let me look at the
resolveConversationIdForSessionKeymethod: - Assistant
Let me look for the conversation store and how it handles session keys:
- Assistant
Let me look at the
getConversationForSessionmethod to understand how it works: - Assistant
Let me look at the shared-init.ts file that was mentioned in the grep results:
- Assistant
Let me look at the plugin/index.ts file to see how the plugin is registered and how it handles sessions:
- Assistant
Let me search for how the engine handles bootstrap and initialization for cron sessions:
- Assistant
Let me search for the bootstrap method in the engine:
- Assistant
Let me look at the bootstrap method:
- Assistant
Let me search for "isolated" in the codebase:
- Assistant
Let me look at the spawnSubagent method:
- Assistant
Let me look for the spawn method in the engine:
- Assistant
Let me look at the prepareSubagentSpawn method:
- Assistant
Let me look at the
applySessionReplacementmethod: - Assistant
Let me look at the
shouldIgnoreSessionmethod: - Assistant
Let me look at how
isStatelessSessionworks: - Assistant
Let me search for how the engine handles cron sessions specifically:
- Assistant
Let me look at the test file for engine:
- Assistant
Let me look at the test around line 5090:
- Assistant
Let me look at the recent git history to understand what changes were made recently:
- Assistant
Let me search for "leak" or "context leak":
- Assistant
Let me look at the test file for engine to understand the cron context leak issue better:
- Assistant
Let me look at the
handleSessionEndmethod and how it's called: - Assistant
Let me look at the engine's bootstrap method to understand how it handles session key reuse:
- Assistant
Let me search for
findAmbiguousSessionKeyRuntimeRollovermethod: - Assistant
Let me look at the
findAmbiguousSessionKeyRuntimeRollovermethod: - Assistant
Let me search for how the plugin handles session end events:
- Assistant
Let me look at the session_end handler:
- Assistant
Let me search for the
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingmethod: - Assistant
Let me look at the
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingmethod: - Assistant
Let me look at the
applySessionReplacementmethod more closely: - Assistant
Let me look at the
ingestmethod and how it handles context: - Assistant
Let me now look at the tests to understand the expected behavior:
- Assistant
Let me look at the test that mentions "cron" to see what the expected behavior is:
- Assistant
Let me look at the test that mentions "cron" to understand the expected behavior:
- Assistant
Let me look for any tests related to session key reuse for cron sessions:
- Assistant
Let me look at the test about "serializes recycled session writes by stable sessionKey":
- Assistant
Let me look at the test about "preserves conversation history when the session file rotates across a stable sessionKey":
- Assistant
Let me look at the
getConversationBySessionKeymethod and how it handles active vs archived conversations: - Assistant
Let me look at how
getConversationBySessionKeyworks: - Assistant
Let me search for test cases that specifically test cron isolation or stable session key isolation:
- Assistant
Let me look at the assemble method to understand how context is assembled:
- Assistant
Let me search for the assemble method in the engine:
- Assistant
Let me look at the assemble method:
- Assistant
Let me look at the
afterTurnmethod to understand how it handles cron sessions: - Assistant
Let me look at the afterTurn method:
- Assistant
Let me look at the
reconcileTranscriptTailForAfterTurnmethod: - Assistant
Let me look at this method:
- Assistant
Let me look at the
reconcileSessionTailmethod: - Assistant
Let me look at the
session-patterns.tsfile: - Assistant
Let me look at the existing tests more carefully for cron-related patterns:
- Assistant
Now let me understand how
getOrCreateConversationhandles session key reuse: - Assistant
Let me look at the test files to see if there are any tests that specifically test the scenario described in the issue:
- Assistant
Let me look for the test that tests what happens when a cron session with a stable key is reused:
- Assistant
Let me look at the test around line 14274 that was mentioned earlier about "deduplicates replay when runtime sessionId changes but stable sessionKey continues":
- Assistant
Let me now look at the git log and recent issues more carefully. Let me understand what the issue actually is by looking at more related code:
- Assistant
Let me now think more carefully about the problem. Let me re-read the issue and look at the code flow for cron runs that use a stable session key. Let me trace through what happens:
- Assistant
Now let me think about this more carefully. The issue is about "isolated cron runs" that "leak context when they reuse a stable session key." Let me look at how
getOrCreateConversationhandles the case where the sessionId changes but the sessionKey stays the same: - Assistant
Let me now run the existing tests to understand the baseline:
- Assistant
Let me run the existing tests first to understand the baseline.
- Assistant
Let me look at the reconcile method to understand how it handles ambiguous rollover:
- Assistant
Let me look at lines 6700-6730 in the reconcile method:
- Assistant
Let me look at the
checkpointIsPastTranscriptEoffunction and the bootstrap flow for handling file shrink: - Assistant
Let me look at the function and the context around it:
- Assistant
Now I have a clear understanding of the issue. Let me look at the key flow in the
findAmbiguousSessionKeyRuntimeRollovermethod: - Assistant
Let me look more carefully at the bootstrap flow and the assemble flow to understand how the context leak happens:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the scenario:
-
Cron run 1:
sessionId="run1",sessionKey="agent:main:cron:nightly",sessionFile="file1"→ creates conversation C1, bootstraps, creates context items -
Cron run 2:
sessionId="run2",sessionKey="agent:main:cron:nightly",sessionFile="file2"→findAmbiguousSessionKeyRuntimeRolloverdetects rollover (different sessionId, different file, old file exists)- Tail anchor check fails (fresh cron run file doesn't contain old conversation's tail)
- Bootstrap returns early with
AMBIGUOUS_SESSION_KEY_RUNTIME_ROLLOVER_REASON - But C1 is NOT archived! It stays active.
- Subsequent
assemblecalls detect rollover → returns safeFallback (no leak) - BUT:
reconcileTranscriptTailForAfterTurnin afterTurn, when file is the same, won't detect rollover
-
Same-file scenario: When the session file path is the SAME across runs:
findAmbiguousSessionKeyRuntimeRolloverreturnsnullbecausetrackedSessionFile === params.sessionFilegetOrCreateConversationfinds C1 by sessionKey, updates sessionId- Context items from C1 leak into run 2!
The fix needs to:
- Remove the same-file early return in
findAmbiguousSessionKeyRuntimeRolloverso the tail anchor check is always performed - When the rollover is detected and tail anchor check fails, archive the prior conversation and continue bootstrap with a fresh conversation
Let me implement the fix:
-
- Assistant
Let me look at the bootstrap flow around the rollover check:
- Assistant
Now let me look at the afterTurn reconcile flow for the rollover check:
- Assistant
Now I understand the issue clearly. Let me implement the fix. The key changes are:
- In
findAmbiguousSessionKeyRuntimeRollover: Remove the same-file early return (lines 7002-7004) so the rollover detection covers same-file cases - In bootstrap: When the rollover is detected and tail anchor check fails, archive the old conversation and continue (instead of returning early)
- In afterTurn reconcile: When the rollover is detected and tail anchor check fails, also archive the old conversation
Let me make the first change:
- In
- Assistant
Let me use the
replacecommand instead: - Assistant
Let me use the
editcommand with proper syntax: - Assistant
Wait, I need to think more carefully. The same-file early return being removed means that for the case where sessions with different sessionIds use the same session file, the rollover detection would trigger. But this would affect normal sessions too (not just cron runs). Let me think about a better approach.
The core issue is that
getOrCreateConversationreuses the existing conversation for a stable session key, even when the sessionId changes. For cron runs, this means the new run inherits the old conversation's context.The fix should be in the bootstrap/afterTurn/assemble flows: when we detect that a new sessionId is using a stable session key that belongs to a prior conversation, and the new session file doesn't contain the prior conversation's messages (tail anchor check fails), we should archive the prior conversation and create a fresh one.
Let me revert my change to
findAmbiguousSessionKeyRuntimeRolloverand instead make targeted changes in bootstrap, afterTurn, and assemble: - Assistant
The diff shows only the same-file check removal so far. Let me revert and think about this more carefully. Let me first understand the full picture:
- Assistant
OK, let me revert that and think about the proper approach. The same-file early return removal in
findAmbiguousSessionKeyRuntimeRolloverwould affectassemblewhich doesn't have a tail anchor check available. Instead, I should make targeted changes in bootstrap and afterTurn to handle the same-file rollover case.Let me now implement the proper fix. First, let me modify the bootstrap flow:
- Assistant
Now let me make the bootstrap changes. First, change the
!hasFrontierAnchorbranch to archive and continue instead of returning early: - Assistant
Let me use a different approach to make the edit:
- Assistant
Let me use sed to make the edit:
- Assistant
Let me verify the change:
- Assistant
Good. Now I need to add the same-file rollover check after the
ambiguousRolloverblock. Let me add it: - Assistant
Now let me add the same-file rollover check between the ambiguous rollover block and the
getOrCreateConversationcall: - Assistant
Let me verify the change:
- Assistant
Good, the bootstrap changes are in place. Now I need to make similar changes in the afterTurn reconcile flow. Let me look at the afterTurn reconcile code:
- Assistant
Now let me modify the afterTurn reconcile flow. First, change the
!hasFrontierAnchorbranch to archive the old conversation: - Assistant
Let me verify the change:
- Assistant
Good. Now I need to add the same-file rollover check after the
reason === "path-mismatch"block. Let me add it: - Assistant
Let me verify the change:
- Assistant
Good. Now let me handle the assemble flow. Let me look at the assemble code:
- Assistant
Let me look at the assemble rollover detection:
- Assistant
Let me modify the assemble rollover detection to also archive the old conversation:
- Assistant
Let me verify the change:
- Assistant
Good. Now let me run the existing tests to make sure my changes don't break anything:
- Assistant
Let me look at the failing tests to understand what they expect:
- Assistant
Let me look at the failing tests:
- Assistant
I see the issue. The tests expect the old conversation to remain active after the rollover is detected. But my changes archive the old conversation and create a fresh replacement. I need to update the tests to reflect the new behavior.
Let me look at all three failing tests and update them:
- Assistant
Let me update the bootstrap test to reflect the new behavior:
- Assistant
Let me use sed to make the edit:
- Assistant
Let me verify the change:
- Assistant
Good. Now let me update the afterTurn test. Let me look at it:
- Assistant
Let me update the afterTurn test:
- Assistant
Let me verify the change and then update the assemble test:
- Assistant
Now let me look at the assemble test:
- Assistant
Let me update the assemble test:
- Assistant
Let me verify the change and then run the tests again:
- Assistant
Good. Now let me run the tests again:
- Assistant
The remaining test failures are because the fresh replacement now has the new sessionId, so
getConversationBySessionId(secondSessionId)finds it. Let me update the test expectations: - Assistant
The tests expect that
getConversationBySessionId(secondSessionId)returns null. But with the fresh replacement, the conversation has the new sessionId, sogetConversationBySessionId(secondSessionId)would find it. I need to update the test to expect thatgetConversationBySessionId(secondSessionId)returns the fresh conversation.Let me update both failing tests.
- Assistant
Let me verify the changes and run the tests again:
- Assistant
Let me check the actual lines around those to see if the sed command worked correctly:
- Assistant
Let me check the other test:
- Assistant
Good. Now let me run the tests again:
- Assistant
All 1067 tests pass. Now let me write a reproduction script to verify that the fix actually resolves the issue:
- Assistant
Let me put the test file in the test directory instead:
- Assistant
Let me look at how the existing tests create the engine:
- Assistant
Let me look at the
createTestConfigandcreateTestDepsfunctions: - Assistant
Let me clean up the temporary test file and verify the full test suite passes:
- Assistant
All 1067 tests pass. Now let me review the complete diff to make sure everything is correct:
- Assistant
Let me look at the full diff:
- Assistant
The diff looks correct. Let me review the changes:
-
In afterTurn reconcile: When rollover detected (different file or same file), archive old conversation and create fresh replacement before returning.
-
In bootstrap: When rollover detected (different file or same file), archive old conversation and create fresh replacement, then continue with bootstrap.
-
In assemble: When rollover detected, archive old conversation and create fresh replacement before returning safeFallback.
-
Test updates: Updated three tests to expect the new behavior (archived old conversation, fresh replacement).
Now let me do a final verification that all tests pass:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →