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 explore the repository structure to understand what we're working with.
Assistant
Let me understand the issue better. The issue is about isolated cron runs leaking context when they reuse a stable session key. Let me look at the cron sessions more closely.
The issue says:
- 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
- Ordinary non-cron session continuity is unchanged
So there's a concept of "cron" sessions. The cron runs reuse a stable session key but should be isolated. Let me look at the session keys structure.
Let me look at the lcm-doctor-cleaners.ts which mentions
agent:main:cron:*.Assistant
Let me look at the engine's session handling, especially around session keys, archiving, and how conversations are loaded by session key.
Assistant
Now I understand the structure. The key concept: conversations are resolved by session key (active=1). When a cron run reuses a stable session key like
agent:main:cron:nightly, the active conversation is reused, leaking context between runs.The issue says: "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."
So the solution: When a cron session starts a new run (new runtime session id), the prior active conversation (from a previous cron run with the same session key) should be archived, and a fresh active conversation created. This is essentially session replacement at the start of a cron run.
Let me look at how the engine starts a session / bootstraps. Let me look at the bootstrap flow and the lifecycle hooks (handleBeforeReset, etc.). Let me look for where the engine is told a run is starting.
Let me look at the plugin index.ts to understand the lifecycle hooks available.
Assistant
Now I understand the lifecycle. There are hooks:
before_reset,session_end,before_prompt_build. Let me understand how cron runs work. The issue says "isolated cron runs leak context when they reuse a stable session key."Let me think about the cron session keys. The pattern is
agent:main:cron:nightly:run:run-123from the session-patterns test. So a cron run key includes a:run:run-123segment. But the issue says "reuse a stable session key."Wait, let me re-read. "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
Hmm, so maybe the issue is that cron runs reuse a stable session key (without the run segment), but they should be isolated. Or, the issue could be that they DO have unique run segments but the active conversation matching by session_key continues.
Wait, but the test in session-patterns.test.ts has
agent:main:cron:nightly:run:run-123. Let me look at how cron sessions are actually keyed. Let me check the parseAgentSessionKey function.Assistant
Let me look at the issue from the perspective of the existing tests and code. Let me look at the regression test to understand the testing approach. Let me look at how cron sessions are tested. Let me search the test files.
Assistant
Now let me understand the issue better. The key is "isolated cron runs leak context when they reuse a stable session key." So cron runs reuse a stable session key. The cron session key is something like
agent:main:cron:nightly. Each run gets a new runtime session id but reuses the stable session key. The issue is that since the session key matches an existing active conversation, the new run reattaches to the old conversation, leaking context.Let me look at how
bootstrapresolves the conversation. Let me look at the bootstrap flow which calls getOrCreateConversation / resolveConversationIdForSessionKey.Assistant
Let me look at the
findAmbiguousSessionKeyRuntimeRolloverfunction androtateStaleSessionKeyConversationIfTrackedTranscriptMissing, which seem to handle runtime rollover cases.Assistant
Now I have a good understanding. There are existing guards for runtime rollover when session keys are reused. But the cron case is different: cron runs reuse a stable session key but each run is isolated. The existing
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingonly rotates when the tracked transcript file is missing. Let me check the config to see if there's a cron-specific pattern config.Assistant
Let me check git history and the changesets for context on the issue.
Assistant
So there's existing logic. Let me look at the lcm-command test and config test for cron-related content.
Assistant
Let me look at the docs/specs for cron context and the AGENTS.md.
Assistant
Now I understand the structure. Let me look at the exact call sites of the rollover checks in bootstrap, assemble, and afterTurn.
Assistant
Let me look at the engine constructor and field declarations to add a cron pattern field.
Assistant
Let me look at the afterTurn call to
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing.Assistant
Now let me look at the test helpers to understand how to create an engine and write a reproduction script.
Assistant
Let me look at the
ingest,ingestBatch, andassembleentry points to understand conversation resolution.Assistant
Let me look at
ingestSinglewhich usesgetOrCreateConversationat line 8105.Assistant
Let me check when
ingestis called in the lifecycle and whether there's a session-start hook.Assistant
Let me check the context engine interface to understand which methods are part of the agent-run lifecycle.
Assistant
Let me check the ContextEngine interface definition to see which methods are part of the agent-run lifecycle.
Assistant
Let me check the compact path and whether it has rollover guards.
Assistant
Now I have a clear understanding. Let me write a reproduction test to confirm the leak. Let me create a temporary test file.
Assistant
The leak is confirmed: run 2 reattached to C1 (sessionId updated to cron-run-2, conversationId still 1, still active). Now let me implement the fix.
First, add the cron pattern field and constructor initialization:
Assistant
Now let me add the cron pattern constant and field. First, let me look at the imports for
compileSessionPatternsandmatchesSessionPattern:Assistant
Let me find the AMBIGUOUS_SESSION_KEY_RUNTIME_ROLLOVER_REASON constant to place the cron pattern near it.
Assistant
No python. Let me use the edit command. First, open the file at the relevant line.
Assistant
Now let me add the cron pattern field and constructor initialization. Let me find the field declarations again (they shifted by 10 lines).
Assistant
Now add the constructor initialization for
cronSessionPatterns:Assistant
Now let me add the
isCronSessionKeyhelper nearisStatelessSession. Let me find it.Assistant
I introduced a duplicate return line. Let me fix it.
Assistant
Now let me modify
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingto add the cron branch. Let me find it again.Assistant
Now let me add the cron branch right after the
activeByKeysessionId check.Assistant
There's a duplicate leftover from the edit. Let me fix lines 6992-6993.
Assistant
The code looks clean now. Let me run the typecheck and the reproduction test.
Assistant
Typecheck passes. Now let me run the reproduction test.
Assistant
The same-file scenario now works (C1 archived, C2 fresh). Let me add a different-file scenario test case to verify that path too.
Assistant
Both scenarios pass. Now let me find and run the existing tests related to ambiguous rollover and stale session keys to ensure no regressions.
Assistant
These ambiguous-rollover tests use keys like
agent:main:test:...which don't match the cron pattern, so they should be unaffected. Let me run the full engine test suite and related tests.Assistant
All 317 engine tests pass. Let me run the broader test suite to check for any regressions.
Assistant
All 1069 tests pass. Now let me verify the "compaction within the current run still works" requirement with a quick check, and also verify non-cron continuity. Let me add temporary checks to the repro.
Assistant
Compaction did run on the fresh conversation (
compacted: true); theok: falseis just the unrealistictokenBudget: 1with the stub summarizer. Let me fix the assertion to verify compaction resolved and operated on the fresh conversation.Assistant
All three repro tests pass. Now let me add a changeset following the repository convention, since the repo uses changesets.
Assistant
Now let me update the documentation to mention cron isolation behavior. Let me check the configuration.md behavior notes section.
Assistant
Let me consider whether the
ingestpath also needs the cron guard. Let me look atingestSingleto see if it's worth protecting against non-compliant call ordering.Assistant
The fix in
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingcovers bootstrap, assemble, and afterTurn — the agent-run lifecycle entry points. Per the host contract, bootstrap runs first, so the fresh conversation exists before ingest/compact. I'll keep the change minimal to avoid interactions with internal replay paths.Now let me remove the temporary reproduction test (I must not add new tests to the repo) and run the full suite to confirm no regressions.
Assistant
All 1067 tests pass and typecheck is clean. Let me also verify the build works.
Assistant
Build succeeds. Let me review the final diff to ensure everything is clean.
Assistant
The diff looks clean. Let me check git status for untracked files (changeset, build artifact).
Assistant
Let me verify the bootstrap call ordering is still correct (rotateStale before findAmbiguous and getOrCreateConversation) after the edits.
Assistant
The ordering is correct: in bootstrap,
rotateStale(line 7196, with the cron branch) runs beforefindAmbiguous(7203) andgetOrCreateConversation(7236). The cron branch archives the prior conversation and creates a fresh one, so the subsequent checks find the fresh conversation bound to the current run.Let me do a final full test run to be completely sure everything passes.
Assistant
All tests pass (1067/1067), typecheck is clean, and the build succeeds. Let me review the final complete set of changes.
Assistant
All changes are in place and verified. Let me submit.
Summary
Issue: Isolated cron runs that reuse a stable session key (e.g.
agent:main:cron:nightly) leaked active LCM context — a new cron runtime session (newsessionId) reusing the lane's stable key reattached to the prior run's active conversation (viagetOrCreateConversation/getConversationForSessionkeyed bysession_key), so the new run inherited the prior run's summaries/context items.Root cause: The existing runtime-rollover guards only rotated the prior conversation when its tracked transcript file was missing (
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing) or fail-closed when the file still existed but differed (findAmbiguousSessionKeyRuntimeRollover). For cron lanes, a new runtime session is always a new isolated run regardless of transcript-file state (the lane may reuse the same path or keep old run files), so neither guard applied — the new run simply reattached to the prior active conversation.Fix (in
src/engine.ts):- Added an intrinsic cron-lane pattern
agent:*:cron:**(compiled once at construction, independent of user-configured ignore/stateless patterns — no new public interface). - Added
isCronSessionKey()helper. - Added a cron branch in
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing(called by bootstrap, assemble, and afterTurn): when a cron key's active conversation is owned by a different runtime session, archive the prior run's conversation (it stays archived for recall) and bind a fresh conversation to the current run — unconditionally, regardless of transcript-file existence/path. This runs before the ambiguous-rollover fail-closed check, so cron runs never hit it.
Result:
- Prior runs remain archived (and recallable via
lcm_grep/lcm_describe/lcm_expand). - The current run owns a fresh active conversation, so no context leaks forward.
- Compaction within the current run resolves and operates on the fresh conversation.
- Non-cron session continuity is unchanged (the cron branch only matches
agent:*:cron:**; existing ambiguous-rollover/stale-rollover tests for non-cron keys still pass).
Verification: Reproduced the leak with a temporary test (same-file and distinct-file scenarios), confirmed the fix resolves it and that compaction targets the fresh conversation, then removed the temporary test. Full suite (1067 tests) passes,
tsc --noEmitis clean, and the build succeeds. Added a changeset and a docs section.Checklist
- Reproduced the error — temporary repro test confirmed run 2 reattached to run 1's active conversation (same
conversationId,sessionIdoverwritten, still active). - Confirmed the fix resolves the error — after the fix, run 2 gets a fresh
conversationId, run 1 is archived (active=false), for both same-file and distinct-file cases; compaction resolves the fresh conversation. - Edge cases — same-sessionId re-bootstrap (no rotation), first run (no prior → no rotation), cron in ignore/stateless patterns (excluded/read-only), multiple accumulated priors (only active rotated), non-cron keys (unchanged), assemble/afterTurn
createReplacement:falsesemantics. - Ran existing tests — full suite (1067/1067) passes; typecheck and build pass; the non-cron ambiguous/stale-rollover tests are unaffected.
- Added an intrinsic cron-lane pattern
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 →