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 and understand the codebase.
Assistant
I need to make single tool calls. Let me run them one at a time.
Assistant
Let me look at the engine's context/session management logic more deeply.
Assistant
Let me look at how the engine looks up active context/conversation by session key.
Assistant
Let me look at the conversation store's getConversationForSession to understand how conversation lookup by session key works.
Assistant
Let me look at the conversation store's full structure and the archive logic.
Assistant
Now let me understand the issue better. The key is: "Isolated cron runs leak context when they reuse a stable session key". Different isolated cron runtime sessions reuse a stable session key. They must not share active LCM context. Prior runs remain archived. Compaction within current run still works. Ordinary non-cron session continuity is unchanged.
So the cron session key is like
agent:main:cron:nightly(stable) but each run is a different "runtime session" (different sessionId?). The issue is that the second cron run reuses the stable session key and ends up reusing the same active conversation/context from the prior run, leaking context across runs.I need to find where the engine decides whether to reuse or archive a conversation. The key concept seems to be: when a cron run completes, the conversation should be archived so the next run starts fresh. But the issue says "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
So when a new cron run starts with the same session key as a prior run that has already completed, we should archive the old conversation and create a new one. But how do we detect a "new run"?
Let me look at the conversation-scope.ts which might track scopes.
Assistant
Let me look at the conversation-store index and the full schema. Let me check the migration to understand the conversations table.
Assistant
There's a unique index on session_key WHERE active=1. So only one active conversation per session key can exist. The issue is that the second cron run, reusing the same session key, finds the prior active conversation and reuses it instead of archiving the prior and starting fresh.
So I need to detect when a new cron run begins with a stable session key and archive the prior active conversation for that key, then create a new one.
Now, how do we detect a "new run"? The runtime session ID changes between runs even though the session key stays the same. So when a new cron run starts with a different
sessionIdbut the samesessionKey, we should archive the old conversation and create a new one.But wait - the existing
getOrCreateConversationupdates the sessionId of the existing conversation if it differs:if (byKey.sessionId !== sessionId) { this.db.prepare(`UPDATE conversations SET session_id = ?, ...`).run(sessionId, byKey.conversationId); byKey.sessionId = sessionId; } return byKey;So it reuses the conversation but updates the sessionId. That's the leak - the new cron run inherits the prior run's context.
So I need to: when the sessionKey is a "cron" (isolated) session, and the existing active conversation has a different sessionId, archive the old conversation and create a new one instead of reusing.
But the issue says "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context." The key concept: "isolated cron runtime sessions". This means sessions that are cron-based should be isolated per-run. The detection should be based on the session key pattern (cron).
Let me look at how the engine is invoked - the afterTurn/bootstrap flow. Where does the conversation get resolved when a new turn comes in?
Let me look at the
isStatelessSessionandshouldIgnoreSessionand how cron is detected.Assistant
Let me look at the config and the existing tests to understand the expected behavior for cron sessions.
Assistant
Let me look at the config to see how cron/isolated sessions are configured.
Assistant
Let me search for "isolated" in the codebase.
Assistant
Let me look at the openclaw-bridge.ts to understand the contextMode concept and how sessions are passed.
Assistant
Let me look at the "ambiguous session key runtime rollover" logic in the bootstrap flow.
Assistant
Let me look at the full ingest flow to see if it does rollover detection.
Assistant
Let me look at
applySessionReplacement.Assistant
Let me search the test file for tests about session rollover / reuse / replacement.
Assistant
Let me check the default ignoreSessionPatterns in the config.
Assistant
Let me check recent git history and changesets to understand the context.
Assistant
Let me look at the test at line 14274 about runtime sessionId changes.
Assistant
Let me look at the bootstrap flow where the ambiguous rollover check happens.
Assistant
Let me look at the full
ingestmethod.Assistant
Let me look at the test helpers to write a reproduction script.
Assistant
Now let me write a reproduction script. Let me create a test file that simulates two isolated cron runs reusing a stable session key.
Assistant
Let me search for cron detection helpers in the codebase.
Assistant
Let me look at the plugin/index.ts to understand how sessions are passed and whether there's a "cron lane" concept.
Assistant
Let me look at how the host wires bootstrap/afterTurn and how session identity is determined for cron lanes.
Assistant
Let me check the openclaw plugin manifest and docs for cron lane definitions.
Assistant
Let me write a reproduction test to confirm the leak. Let me create a temporary test file.
Assistant
Let me place the reproduction test in the test directory and run it.
Assistant
I've reproduced the leak. Now let me look at the afterTurn and assemble flows to understand where they resolve conversations.
Assistant
Let me look at the assemble flow.
Assistant
Let me look at where afterTurn ingests messages (the ingestBatch call in afterTurn).
Assistant
Let me look at the reconcile logic for a conversation with no checkpoint.
Assistant
Let me look at the compact flow to see if it resolves conversations by sessionKey.
Assistant
Let me look at the lifecycle event handling and how the host passes lane info.
Assistant
Now let me look at the exact location to insert the new helper method.
Assistant
Let me check the AmbiguousSessionKeyRuntimeRollover type definition.
Assistant
Now let me implement the fix. I'll add helper methods before
rotateStaleSessionKeyConversationIfTrackedTranscriptMissing.Assistant
Now let me insert these helpers into the engine. I'll edit at line 6907.
Assistant
Let me add a blank line between the methods for proper formatting.
Assistant
There's a duplicate comment line. Let me fix it.
Assistant
Now let me add a blank line before the rotateStale comment.
Assistant
There's a duplicate line in the comment. Let me fix it.
Assistant
The helpers are in place. Now let me add the calls in each entry point. Let me start with the bootstrap flow.
Assistant
There's a duplicate
rotateStaleline. Let me fix it.Assistant
Bootstrap section is now correct. Now let me add the cron rotation to the afterTurn reconcile path.
Assistant
There's a duplicate
withTransactionline. Let me remove it.Assistant
The
edittool keeps duplicating lines. Let me usesedto remove the duplicate line.Assistant
Now the afterTurn reconcile is correct. Now let me add the cron rotation to the assemble flow.
Assistant
Now let me add the cron rotation to ingest and ingestBatch. Let me find their current locations.
Assistant
Let me update the
ingestmethod to add the cron rotation.Assistant
Now let me add the cron rotation to
ingestBatch. Let me find the exact lines now.Assistant
Now let me add the cron rotation to
compact. Let me find the compact conversation resolution.Assistant
Now let me run the typecheck and the reproduction test.
Assistant
Typecheck passes. Now let me run the reproduction test.
Assistant
The fix works. Let me extend the repro test to verify prior runs remain archived (lossless) and ordinary non-cron continuity is unchanged.
Assistant
All tests pass. Now let me run the existing engine test suite to make sure I didn't break anything.
Assistant
All 317 engine tests pass. Let me run the full test suite to check for any regressions.
Assistant
All 1070 tests pass. Now let me add a changeset, as this is a user-facing behavior fix. Let me check the changeset format.
Assistant
Now let me remove the temporary reproduction test before submitting.
Assistant
Now let me review the final diff to ensure correctness.
Assistant
Let me restore the blank line in the bootstrap section for readability.
Assistant
That inserted the blank line in the wrong place. Let me fix it.
Assistant
Let me run the typecheck and full test suite once more to confirm everything is clean.
Assistant
Let me check for any other entry points that resolve conversations by session key that I might have missed.
Assistant
Let me verify the bootstrap method's early-return checks happen before the cron rotation (so ignored cron sessions aren't affected).
Assistant
Bootstrap correctly checks
shouldIgnoreSessionandisStatelessSessionbefore the cron rotation. The fix is complete. Let me do a final review of the full diff.
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 →