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 structure to understand the codebase and then identify the issue related to isolated cron runs leaking context.
Assistant
Let me search for files related to "cron", "session", and "LCM context":
Assistant
Let me look at the engine.ts file since it's the largest and seems to contain the core logic for context modes.
Assistant
Let me look at the openclaw-bridge.ts file too:
Assistant
Now let me search for where the session key and LCM context are managed:
Assistant
Let me search for where LCM context is managed more specifically, and look for "archive" or "active" patterns:
Assistant
Let me look more at how session keys are handled, specifically when a new session is started with the same session key:
Assistant
"result": { "type": "code", "language": "typescript", "value": "grep -n "ensureConversationForSession\|ensureActiveConversation" /lossless-claw/src/engine.ts | head -30\n"
Assistant
Let me search more specifically for the functions that handle conversation/session management.
Assistant
No results for those search terms. Let me look at the test files to understand the issue better.
Assistant
Let me search for tests related to "cron" or "isolated" context to understand the issue better.
Assistant
The issue is about "isolated cron runs leak context when they reuse a stable session key". This means that when different cron runs use the same session key, they're sharing LCM context that should be isolated between runs.
Let me search for where the session key is used to look up or create conversations, and where the "isolated" mode should be enforced for cron runs.
The key insight is:
- 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 the problem is likely that when a cron run starts with a session key like
agent:main:cron:nightly, it's finding the existing active conversation from a previous run and reusing its context. Instead, it should:- Archive the previous run's conversation
- Start a fresh conversation for the new run
Let me look at how conversations are looked up and how session keys are handled.
Assistant
Let me look at the conversation store and understand how session keys map to conversations, especially when reusing session keys.
Assistant
Let me look at the conversation store to understand how sessions and conversations are managed, especially when a session key is reused.
Assistant
Now I understand the structure better. Let me look at the
getConversationForSessionmethod and understand how it handles session keys, especially when there's an active conversation already associated with that key.The key method seems to be around line 430-455 and 477-528 where it checks for existing conversations by session key and potentially archives/rotates them.
Let me look at the engine.ts file to see how the LCM context is managed when a session starts.
The issue is about "isolated cron runs leak context when they reuse a stable session key". This suggests that:
- When a cron job starts with a session key like
agent:main:cron:nightly, it should be treated as an isolated session - Previous runs with the same session key should be archived
- The new run should start with a fresh context
Let me look at the relevant code paths in engine.ts.
- When a cron job starts with a session key like
Assistant
Let me look at the
getConversationForSessionmethod and the surrounding code to understand how conversations are retrieved/created for a session.Assistant
Let me look at the
getConversationBySessionKeymethod more closely:Assistant
I can see that
getConversationBySessionKeyonly returns conversations whereactive = 1. This is the key issue!When a cron run starts with the same session key, it finds the previous active conversation and reuses it. But for isolated cron runs, the previous run's conversation should be archived first, and a new conversation should be created.
Now I need to understand:
- What signals that a session is a "cron" session that should be isolated?
- Where in the code flow does the conversation lookup happen?
Based on the issue description:
- "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 the fix needs to:
- Detect when a session is a cron session (likely by session key pattern)
- Archive the previous active conversation with that session key
- Create a new conversation for the new run
Let me look at the session patterns to understand how cron sessions are identified.
Let me look at the session patterns to understand how cron sessions are identified.
Let me look at the session patterns to understand how cron sessions are identified.
<tool_call> <function=bash_terminal> <parameter=command> cat /lossless-claw/src/session-patterns.ts </tool_call>
Assistant
Let me search for more specific patterns to understand how cron sessions are identified and managed.
Assistant
Let me search for where the engine interacts with the conversation store when starting a session:
Assistant
Let me examine where
getOrCreateConversationis called and understand the flow:Assistant
I found one call site for
getOrCreateConversationat line 7183. Let me also check line 8105 to see the other call site.Looking at the issue:
- "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"
The issue is that when
getOrCreateConversationis called with a session key that already has an active conversation, it returns the existing one instead of archiving it and creating a new one for cron sessions.I need to:
- Identify where cron sessions are detected (probably by pattern like
agent:*:cron:*) - When a cron session key is used, archive the existing active conversation and create a new one
Let me look at how the engine determines if a session should be isolated and how the conversation store's
getOrCreateConversationmethod works.The fix should probably be in the conversation store or in the engine's bootstrap flow. Let me check the
getOrCreateConversationmethod more carefully and see if there's a way to pass a flag to force isolation.Actually, looking at the code again, the
getOrCreateConversationmethod in conversation-store.ts (line 495-528) checks for an existing conversation by session key and returns it if found. For cron sessions, we need to archive that existing conversation and create a new one instead.Let me think about where this logic should go:
- In the conversation store's
getOrCreateConversation- but this would require knowing which sessions are cron sessions - In the engine's bootstrap flow before calling
getOrCreateConversation- this would be cleaner because the engine knows about session patterns
Let me look at the engine code more carefully to see how it handles session patterns.
Assistant
Let me look at the engine tests to understand the expected behavior for isolated sessions:
Assistant
Let me look at the test case around line 5105 to understand the context better.
Assistant
Now I understand the issue better. The cron sessions with session keys like
agent:main:cron:*are being ignored/stored in a special way, but the issue is that when they reuse a stable session key, they share active LCM context.Let me search for how the "isolated" context mode works and where the leak is happening. Let me look at the test files for more context about the expected behavior.
Assistant
This is very helpful! There are tests around "stable sessionKey" behavior. The issue is specifically about isolated cron runs that reuse a stable session key.
Let me think about the problem:
- Cron sessions typically have keys like
agent:main:cron:nightly - When cron runs again, it reuses the same session key
agent:main:cron:nightly - Currently, this finds the existing active conversation and reuses it (leaking context from the previous run)
- The fix should archive the previous run's conversation and start a fresh one
Let me look at the tests for "serializes recycled session writes by stable sessionKey" and other stable session key tests to understand the current behavior, then figure out what needs to change for cron sessions.
The key insight is that cron sessions need special handling - when a cron session starts with a session key that already has an active conversation, it should archive that conversation and create a new one, rather than reusing the old one.
- Cron sessions typically have keys like
Assistant
Let me look at how cron sessions are identified and handled, and understand what patterns indicate an isolated cron session.
Assistant
Let me look at the config to understand how session patterns are configured and if there's a specific pattern for isolated cron sessions.
Assistant
Let me understand the architecture better. The issue mentions "isolated cron runtime sessions" - this sounds like a specific type of session that should be isolated. Let me look for how the engine handles cron sessions and what makes them "isolated".
Looking at the test patterns, I see:
ignoreSessionPatterns: ["agent:*:cron:**"]- these sessions are ignoredstatelessSessionPatterns: ["agent:*:subagent:**"]- these are stateless
But the issue talks about cron sessions that ARE stored (not ignored), but should be isolated between runs. So the session key pattern might be like
agent:main:cron:nightlywhere "nightly" is the stable session key, but each cron run should have its own isolated context.Let me look at the plugin index and shared-init to understand how sessions are managed.
Assistant
Let me think about this differently. The issue says "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context." This means:
- There's a concept of "isolated" sessions (perhaps marked by a pattern or flag)
- When an isolated session with a certain session key already exists (active), and a new runtime wants to use the same session key, it should NOT reuse the existing active conversation
- Instead, it should archive the old one and create a new one
Let me look at how the engine determines if a session should be treated as "isolated" and where the conversation lookup/creation happens.
Assistant
Now I see there's
shouldIgnoreSessionandisStatelessSession. Let me look at both of these methods:Assistant
Now I understand the pattern matching. Let me think about this issue more carefully:
The issue says "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
The key question is: what makes a session "isolated"? Looking at the code, I see:
shouldIgnoreSession- sessions that matchignoreSessionPatternsare ignored entirelyisStatelessSession- sessions that matchstatelessSessionPatternsare read-only
But the issue is about isolated sessions - not ignored, not stateless, but sessions that should be isolated between runs. This is a new concept that needs to be implemented.
Wait, let me re-read the issue more carefully:
Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context.
This suggests that when a cron session (identified by its key pattern like
agent:main:cron:*) starts and finds an existing active conversation with the same session key, it should NOT reuse it. Instead, it should archive the old one and create a new one.But how does the system know this is a cron session? Let me look at the default patterns:
Assistant
Let me look at the plugin configuration to see if there's a default pattern for cron sessions:
Assistant
The expression was empty.
Assistant
Let me re-read the issue description more carefully. The key phrase is "isolated cron runs" - this is about cron sessions that are isolated from each other.
Looking at the LCM doctor cleaners file, I saw there's a pattern
agent:main:cron:*for cron sessions. The issue is that when cron runs reuse the same session key, they share context that should be isolated.Let me think about this:
- A cron job might have a session key like
agent:main:cron:nightly - When the cron runs again the next day, it uses the same session key
agent:main:cron:nightly - Currently, the system finds the existing active conversation and reuses it (continues where it left off)
- But for cron jobs, each run should be isolated - the previous run's context should be archived, and a fresh conversation should start
The fix should be: when starting a session with a cron-like session key, if there's already an active conversation with that key, archive it first and create a new one.
But wait, I need to understand what makes a session "isolated". Is it all cron sessions? Or is there a specific configuration?
Let me look at the test files again to see if there's a test that demonstrates this behavior:
- A cron job might have a session key like
Assistant
The search for "isolated" didn't find much relevant to the issue. Let me think about this differently.
Looking at the issue again:
Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context.
This is about isolated sessions - not all sessions are isolated. Some sessions (like cron sessions) should be isolated between runs.
Let me look at the types to see if there's a configuration for isolated session patterns.
Assistant
No results. So the concept of "isolated session patterns" doesn't exist yet - this is what I need to implement!
Let me look at the types and config to understand the structure better:
Assistant
Now I understand. I need to add a new configuration option for "isolated session patterns" - sessions that match these patterns should have their previous active conversation archived when a new runtime session starts with the same session key.
Let me plan the implementation:
- Add
isolatedSessionPatternsto the config - Add
isolatedSessionPatternsSourceand related diagnostics toLcmConfigDiagnostics - Add the environment variable
LCM_ISOLATED_SESSION_PATTERNS - Add the method
isIsolatedSessionin the engine - Modify
getOrCreateConversationto archive the existing conversation when starting an isolated session
Wait, let me reconsider. The issue mentions "isolated cron runs" - maybe the fix should be simpler. Perhaps the
ignoreSessionPatternsalready exists, and I just need to add logic to archive previous conversations when a new session with the same key starts.Actually, re-reading the issue again:
Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context.
This suggests that cron sessions are ALREADY being tracked in some way, but they're leaking context. The fix is to make sure that when a cron session starts, it archives the previous active conversation.
Let me look at the
getOrCreateConversationmethod more carefully and understand where the fix should go.- Add
Assistant
Let me look at the plugin registration to understand the default configuration values:
Assistant
I need to look at how session key patterns are matched and how the system determines if a session should be isolated.
Assistant
I need to understand the full context. Let me look at what configuration options exist and what the plugin registration looks like to see how to add a new isolated session patterns option.
Assistant
Let me check what default values are set for session patterns:
Assistant
Let me look at the types.ts file to see the configuration interface:
Assistant
Let me look at the existing test patterns and code to understand how this should work:
Assistant
Let me search for any test or spec that mentions the expected behavior for isolated cron sessions:
Assistant
Let me check the README for any relevant context:
Assistant
Let me check the specs directory:
Assistant
Now let me check the AGENTS.md file for more context:
Assistant
Let me look at the AGENTS.md to understand what kind of changes are expected:
<tool_call> <function=bash_terminal> <parameter=command> cat /lossless-claw/AGENTS.md </tool_call>
Assistant
I need to use the bash_terminal tool to read the AGENTS.md file.
Assistant
Let me now understand the issue better by looking at how the engine handles existing conversations and the getOrCreateConversation flow:
Assistant
Now I understand the issue clearly. When isolated cron sessions reuse a stable session key, they need to archive the previous conversation and create a new one instead of reusing the existing active conversation. Let me implement this fix:
- Add
isolatedSessionPatternsto the config - Add
isIsolatedSessioncheck in the engine - Modify
getOrCreateConversationto handle isolated sessions - Update plugin schema and documentation
Let me start by adding the configuration option:
- Add
Assistant
Now I understand the issue. When isolated cron sessions reuse a stable session key, they need to archive the previous conversation and create a new one. Let me implement this step by step.
Assistant
Now I understand the issue. When isolated cron sessions reuse a stable session key, they share active LCM context that should be isolated between runs. Let me implement this fix step by step.
First, let me add the
isolatedSessionPatternsconfiguration option to the config:Assistant
I need to use the proper syntax for the open command. Let me fix that.
Assistant
Let me look at the config file more carefully to understand where to add the new isolated session patterns:
Assistant
Now I understand the structure. I need to:
- Add
isolatedSessionPatternsSourcetoLcmConfigDiagnostics - Add
isolatedSessionPatternstoLcmConfig - Add the resolution logic for
isolatedSessionPatternssimilar to howstatelessSessionPatternsis resolved - Add
isIsolatedSessionmethod in the engine - Modify
getOrCreateConversationto archive previous conversations for isolated sessions
Let me start implementing these changes.
- Add
Assistant
Now I understand the structure. Let me implement the fix step by step:
- Add
isolatedSessionPatternsSourcetoLcmConfigDiagnostics - Add
isolatedSessionPatternsEnvOverridesPluginConfigtoLcmConfigDiagnostics - Add
isolatedSessionPatternstoLcmConfig - Add the resolution logic for
isolatedSessionPatterns - Add the diagnostics output
- Add
isolatedSessionPatternsto the plugin schema - Add
isIsolatedSessionmethod in the engine - Modify
getOrCreateConversationto handle isolated sessions
Let me start implementing these changes one by one.
- Add
Assistant
I need to add the isolated session patterns configuration. Let me add the fields step by step:
- First add
isolatedSessionPatternsSourceandisolatedSessionPatternsEnvOverridesPluginConfigtoLcmConfigDiagnostics - Then add
isolatedSessionPatterns: string[]toLcmConfig - Then add the resolution logic
- Then add the diagnostics output
- Then add the plugin schema entries
- Then add
isIsolatedSessionmethod in engine - Then modify
getOrCreateConversationto handle isolated sessions
Let me start with step 1.
- First add
Assistant
Good! Now let me verify the change and add
isolatedSessionPatternstoLcmConfig:Assistant
Good! Now let me add
isolatedSessionPatternsto theLcmConfigtype. Let me find the right location:Assistant
Now I need to add
isolatedSessionPatternsto theLcmConfigtype. I'll add it afterstatelessSessionPatterns.Assistant
Good! Now I need to add the resolution logic for
isolatedSessionPatterns. Let me find wherestatelessSessionPatternsis resolved:Assistant
Now I need to add the resolution logic for
isolatedSessionPatterns. Let me add it afterstatelessSessionPatterns:Assistant
Good! Now I need to add the config value and diagnostics output. Let me find where
statelessSessionPatternsis assigned in the config:Assistant
Good! Now I need to add the diagnostics output. Let me find where the diagnostics are set:
Assistant
I see there's a formatting issue - the insertion was done in the middle of the line. Let me fix it:
Assistant
There's a formatting issue. Let me fix the diagnostics section properly:
Assistant
Let me try with node instead since it's a TypeScript repository:
Assistant
Now I need to add
isolatedSessionPatternsto the plugin schema. Let me add it toopenclaw.plugin.json:Assistant
Now I need to add the isolatedSessionPatterns to the plugin.json. Let me add it after statelessSessionPatterns.
Assistant
Good! Now let me verify the changes to openclaw.plugin.json:
Assistant
Good! Now I need to add the
isIsolatedSessionmethod in the engine and modifygetOrCreateConversationto handle isolated sessions. Let me first look at the engine to understand the pattern:Assistant
Now I need to add the
isIsolatedSessionmethod to the engine. Let me add it afterisStatelessSession:Assistant
Now I need to also add the
isolatedSessionPatternsfield initialization in the engine class. Let me search for wherestatelessSessionPatternsis initialized:Assistant
I need to add the
isolatedSessionPatternsfield and initialize it. Let me look for where the patterns are compiled:Assistant
Now I need to add the
isolatedSessionPatternsfield and initialize it:Assistant
Now I need to modify the
getOrCreateConversationmethod in the conversation store to handle isolated sessions. When an isolated session reuses a stable session key, it should archive the previous conversation and create a new one.Let me look at the
getOrCreateConversationmethod and modify it:Assistant
Now I understand the structure. The
getOrCreateConversationmethod in conversation-store.ts needs to be modified to accept an optionalisIsolatedflag. WhenisIsolatedis true and there's an existing active conversation with the same session key, it should archive it and create a new one.However, looking at the code flow, the
getOrCreateConversationmethod is called from the engine, and the engine knows whether a session is isolated. So I should:- Add an optional
isIsolatedSessionparameter togetOrCreateConversation - When it's true and there's an existing active conversation with the same session key (but different sessionId), archive the old one and create a new one
- Update the engine to pass this flag when calling
getOrCreateConversation
Let me implement this:
- Add an optional
Assistant
Now I need to update the engine to pass the
isIsolatedSessionflag when callinggetOrCreateConversation. Let me find all the call sites:Assistant
I need to update the engine to pass the
isIsolatedSessionflag when callinggetOrCreateConversation. Let me find the call sites and update them.Assistant
I need to update the engine to pass
isIsolatedSession: this.isIsolatedSession(params.sessionKey)when callinggetOrCreateConversation. Let me update both call sites.Assistant
Good! Let me verify the changes and then run the existing tests to make sure everything works.
Assistant
Let me check the syntax error in the code:
Assistant
The user is pointing out that I need to actually invoke a tool call. Let me continue verifying the changes and then run tests.
Assistant
The changes look good. Now I need to:
- Run the existing tests to make sure nothing is broken
- Create a test script to verify the fix works correctly
Let me first try to compile/build the project to check for any TypeScript errors, then run the tests.
Assistant
The build succeeded. Now let me run the existing tests to verify the changes don't break anything:
Assistant
I see the issue -
this.isolatedSessionPatternsis undefined in some tests. This is because the config might not always haveisolatedSessionPatternsdefined, especially in older tests. I need to ensure it defaults to an empty array.Assistant
The issue is that
resolvePatternArrayis being called withpc.isolatedSessionPatternswhich might be undefined if the config doesn't have it. Let me check theresolvePatternArrayfunction to see how it handles undefined values, and also check if the config always returns a default empty array.The test failures show that
this.isolatedSessionPatternsis undefined. This means either:- The config resolution isn't properly defaulting to an empty array
- The engine initialization isn't properly handling the case when
isolatedSessionPatternsis not in config
Let me check the
resolvePatternArrayfunction and also verify that the config is being resolved correctly.Assistant
The
resolvePatternArrayfunction already defaults to an empty array when no patterns are provided. So the issue must be elsewhere. Let me check if the config is being passed correctly to the engine.Assistant
I see the problem - I added
this.isolatedSessionPatternsfield but didn't add it to the constructor initialization. Let me check what I added earlier:Assistant
The initialization line for
this.isolatedSessionPatternsis missing. Let me check the constructor more carefully and fix it.Assistant
I need to add the initialization of
this.isolatedSessionPatternsin the constructor. Let me do that now.Assistant
I need to verify that my changes were applied correctly and then run the tests again to see if the issue is fixed.
Assistant
Good, the initialization is now in place. Let me run the tests again to see if this fixes the issue. The previous error was that
this.isolatedSessionPatternswas undefined, which caused the.lengthaccess to fail.Assistant
Good progress! Most tests pass now. There's only one failing test in
test/config.test.tsthat needs to be updated to include the newisolatedSessionPatternsSourceandisolatedSessionPatternsEnvOverridesPluginConfigfields in the expected diagnostics object.Let me look at that test and update it to include the new fields.
Assistant
I need to update the test expectation to include the new isolated session pattern diagnostics. Let me fix this test:
Assistant
I need to update the test to include the new diagnostics fields for isolated session patterns. Let me fix this:
Assistant
Now let me run the tests again to verify all tests pass:
Assistant
All tests pass now. Let me create a test script to verify the fix works correctly for the isolated cron session scenario described in the issue:
Assistant
Let me verify that the key changes are in place by checking the modified files:
Assistant
Let me verify the key changes are in place and summarize what was done. The tests are passing, which is good. Let me check the engine changes and the conversation-store changes to make sure everything is correct.
Assistant
I need to use the proper terminal command format. Let me check the conversation-store changes to make sure everything is correct.
Assistant
I made a mistake in my previous command - I didn't properly format it. Let me check what the issue is.
Assistant
Good, the changes are in place. Let me verify the complete implementation by checking the key parts:
- Config changes (
/lossless-claw/src/db/config.ts) - AddedisolatedSessionPatterns - Engine changes (
/lossless-claw/src/engine.ts) - AddedisIsolatedSessionmethod - Conversation store changes (
/lossless-claw/src/store/conversation-store.ts) - ModifiedgetOrCreateConversationto handle isolated sessions - Plugin schema changes (
/lossless-claw/openclaw.plugin.json) - AddedisolatedSessionPatterns
All tests pass. Let me do a final verification of the complete implementation by reviewing the key code:
- Config changes (
Assistant
All tests pass (1067/1067). Let me now verify the complete implementation is correct and submit.
Assistant
Good, I can see the changes made. Let me verify everything is correct by reviewing the key files one more time before submitting. Let me check the isIsolatedSession method in engine.ts and make sure it's properly implemented.
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 →