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 the issue at hand.
Assistant
Let me explore the source code structure to understand the project better.
Assistant
Let me search for files related to "cron", "session", "context", "lcm", and "session key" to understand the issue better.
Assistant
Let me look at the cron-related files more closely.
Assistant
This is a large file. Let me search for "cron" specifically to understand the cron-related functionality.
Assistant
Now I understand that the issue is about cron sessions. Let me look at the test files and the engine to understand how session keys and context work.
Assistant
Let me look at the engine and how sessions/conversations are managed.
Assistant
The output got truncated. Let me look at specific sections of the engine.ts file to understand how sessions and context work. Let me search for the term "isolated" which is mentioned in the issue.
Assistant
Now I need to look for how context is managed for sessions. Let me search for "active" conversations and how the context is retrieved.
Assistant
Let me look at how the context is resolved. Let me search for "resolveConversationIdForSessionKey" and understand how conversations are resolved.
Assistant
Let me look at the conversation store to understand how conversations are managed. The issue is about cron runs reusing session keys and leaking context.
Assistant
I need to understand the conversation store better. Let me look at the methods for getting conversations and how active/archived state works. The issue mentions that "isolated cron runs leak context when they reuse a stable session key". I need to find where conversations are looked up and how the active state is managed.
Let me search for the methods that get conversations by session key.
Assistant
The key is
getConversationBySessionKey. Let me look at that method to understand how it handles archived vs active conversations.Assistant
Now I understand the issue! The method
getConversationBySessionKeyfilters byactive = 1. This means it only returns active conversations. However, the problem is that when a cron session ends and its conversation gets archived, a new cron run with the same session key should start fresh instead of reusing the archived context.Let me look at the engine to see how context is assembled and how it interacts with conversations:
Assistant
Let me look at how the engine handles the prompt to understand the context flow:
Assistant
Let me look at the tests to understand the expected behavior better:
Assistant
Let me look for tests related to cron or session isolation:
Assistant
Let me look at the test related to session isolation in the engine tests:
Assistant
Let me look at the test about archived conversation lookup:
Assistant
This test confirms the expected behavior: when a conversation is archived, a new
getOrCreateConversationcall with the same session key should create a fresh active conversation, not reuse the archived one. The test verifies this already works.Now let me understand the issue more deeply. 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."
This suggests that when cron sessions end, their context should be archived properly, and new cron runs with the same session key should start fresh. Let me look at how session lifecycle events are handled in the engine:
Assistant
Now let me look at
applySessionReplacement:Assistant
Now let me look at the
createConversationmethod to understand the issue:Assistant
Now let me look at the
getOrCreateConversationmethod, which is critical:Assistant
Now I have a clearer picture. Let me look at how the context is assembled. The issue is about context leaking when cron runs reuse a stable session key. Let me look at the
assemblemethod:Assistant
Let me search for the
assemblemethod definition in the engine:Assistant
I need to continue looking at the assemble method to understand how context is retrieved and whether there's an issue with context leaking between cron sessions.
Assistant
Let me look at the
afterTurnmethod which is where conversations are typically created:Assistant
Let me search for where conversations are created or looked up to understand the flow better:
Assistant
Let me check the database schema to understand the constraints:
Assistant
Let me look at the
rotateStaleSessionKeyConversationIfTrackedTranscriptMissingmethod which seems relevant:Assistant
Now I'm starting to understand the issue better. The problem seems to be related to isolated cron sessions. When an isolated cron session ends, if it doesn't properly archive its conversation, a new cron run with the same session key could reuse the old conversation and its context.
Let me look at how the
afterTurnmethod handles conversation creation and whether it properly handles the case when a cron session ends without proper lifecycle hooks:Assistant
Let me continue reading the afterTurn method to understand how it handles conversation creation and whether there's a context leak issue.
Assistant
Let me look at the
ingestBatchmethod to understand how conversations are created:Assistant
Let me now look at the
ingestSinglemethod to understand how conversations are created when ingesting messages:Assistant
Now I understand the flow better. The key method is
getOrCreateConversation. Let me look at how this works when a conversation is archived:Looking at
getOrCreateConversation:- First it checks
getConversationBySessionKey(which only returns active conversations) - If found, it returns it (and may update sessionId)
- Otherwise it checks
getActiveConversationBySessionId - If neither is found, it creates a new conversation
So when a conversation is archived,
getConversationBySessionKeyreturnsnullbecause it filters byactive = 1. Then it falls back togetActiveConversationBySessionIdwhich also filters byactive = 1.The issue is likely that for cron sessions with the same session key, when a new run starts:
- The old cron session's conversation should be archived
- But if it's not properly archived (e.g., the
session_endlifecycle hook wasn't called), the old conversation remains active - The new cron run would then reuse the old active conversation and its context
Let me look for tests related to this scenario:
- First it checks
Assistant
Looking at the codebase, I need to understand the issue better. Let me examine the
getOrCreateConversationmethod in the conversation store since it's key to how conversations are managed when session keys are reused:Assistant
Now I understand the issue! Looking at
getOrCreateConversation:- If a session key is provided, it looks up the conversation by session key (only active ones)
- If found, it returns that conversation - even if the
sessionIdhas changed (i.e., a new runtime session with the same session key)
The problem is in lines 496-503:
if (byKey.sessionId !== sessionId) { this.db.prepare(`UPDATE conversations SET session_id = ?, ...`).run(sessionId, byKey.conversationId); byKey.sessionId = sessionId; } return byKey;When a new cron run starts with the same session key but a different sessionId, it:
- Finds the old active conversation by session key
- Updates the old conversation's sessionId to the new sessionId
- Returns the old conversation (with its existing context!)
This means the new cron session inherits all the context from the prior cron run, which is the context leak!
The fix should be: when the
sessionIddiffers (meaning this is a different runtime session), the old conversation should be first, and a fresh conversation should be created for the new session.Assistant
Now I understand the full picture. The
applySessionReplacementmethod properly archives the old conversation and creates a new one. But thegetOrCreateConversationmethod in the conversation store doesn't follow this pattern when the sessionId differs.The issue is: when a stable session key is reused by a different runtime session (different
sessionId),getOrCreateConversationupdates the old conversation'ssessionIdinstead of archiving it and creating a fresh one. This causes context leaking.The fix should be in
getOrCreateConversation: when a session key is found but the sessionId differs, it should:- Archive the old conversation
- Create a new conversation for the new session
Let me check if there are any existing tests that would fail with this change:
Assistant
Now I need to write a test to reproduce this issue first. Let me create a test script that demonstrates the context leak problem.
The issue is:
- Cron session A with session key "agent:main:cron:nightly" and sessionId "session-a" starts
- Messages are added, context is built up
- Cron session A ends (but no
session_endlifecycle hook is called - or the conversation isn't properly archived) - Cron session B with the same session key "agent:main:cron:nightly" but different sessionId "session-b" starts
getOrCreateConversation("session-b", { sessionKey: "agent:main:cron:nightly" })is called- It finds the old conversation by session key, updates the sessionId, and returns the old conversation
- Session B now has access to session A's context - THIS IS THE LEAK!
The fix should be: when the session key matches but the sessionId differs, archive the old conversation and create a new one.
Let me first create a reproduction script to confirm the issue.
Assistant
Let me run the existing tests first to make sure I understand the codebase, and then create a reproduction script to confirm the issue before fixing it.
Assistant
Let me create a reproduction script to confirm the issue before implementing the fix.
Assistant
I need to use the correct format for creating a file. Let me use a cat heredoc to create the test script.
Assistant
I need to run the reproduction script to confirm the issue exists.
Assistant
I've confirmed the bug. Now let me look at the fix. The issue is in the
getOrCreateConversationmethod inconversation-store.ts. When a session key matches but the sessionId differs (meaning a new runtime session), the current code just updates the sessionId and returns the old conversation. Instead, it should archive the old conversation and create a new one.Let me also check if there's a similar issue when looking up by sessionId (non-keyed sessions).
Assistant
Looking at the code more carefully, I need to understand the intended behavior:
-
When a session key is provided and found: If the
sessionIddiffers, it means this is a new runtime session using the same stable session key. In this case, the old conversation should be archived and a new one created. -
When looking up by sessionId (non-keyed): If an active conversation exists with the same sessionId, it should be reused - this is the "ordinary non-cron session continuity" that should remain unchanged.
The fix needs to be:
- When
byKeyis found butbyKey.sessionId !== sessionId, archive the old conversation and create a new one for the new session.
This aligns with the issue description: "Prior runs remain archived" and "ordinary non-cron session continuity is unchanged."
Let me now implement the fix.
-
Assistant
Let me look at the
getOrCreateConversationmethod and fix it. The issue is that when a session key matches but the sessionId differs, it should archive the old conversation and create a new one instead of just updating the sessionId.Let me navigate to the specific method and fix it.
Assistant
Now I understand the issue clearly. Let me implement the fix. The key change is: when a session key matches but the sessionId differs, archive the old conversation and create a new one instead of just updating the sessionId.
Assistant
I notice there are duplicate lines (lines 527-530 have extra content that shouldn't be there). Let me fix this by removing the duplicate lines.
Assistant
I see the issue - I removed too much. The closing brace
}for thegetOrCreateConversationmethod was also removed. Let me fix this properly:Assistant
The fix is now properly in place. Let me verify the changes look correct by viewing the entire modified method:
Assistant
I see the issue - I need to continue with my plan. Let me run the reproduction script to verify that my fix works correctly.
Assistant
The fix works. Now let me run the existing tests to make sure I haven't broken anything.
Assistant
I need to analyze the test failures more carefully. The test failures indicate that my fix is too aggressive - it's archiving conversations when it shouldn't. Let me look at the failing tests to understand the expected behavior:
test/regression-2026-03-17.test.ts- "reuses conversation across multiple sequential resets"test/engine.test.ts- "deduplicates replay when runtime sessionId changes but stable sessionKey continues"
These tests seem to expect that when a sessionId changes but the sessionKey stays the same, the conversation should be reused. But this contradicts the issue description which says different isolated cron sessions should NOT share context.
Let me look at these tests more carefully to understand what the expected behavior should be.
Assistant
I see now - these tests are testing "session key continuity" which is specifically for non-cron sessions (like
agent:main:main). The issue is that my fix is too broad - it applies to ALL session keys, not just cron sessions.Let me look at the other failing test to understand better:
Assistant
Now I understand the issue better. The tests show that:
-
Session key continuity (e.g.,
agent:main:main) - When a user does/resetor/new, the sessionId changes but the sessionKey stays the same. The conversation should be reused because this is the same ongoing chat session, just with a new "chapter". -
Cron sessions (e.g.,
agent:main:cron:nightly) - When a cron job runs multiple times, each run should be isolated with its own context. Prior runs should be archived.
The key difference is that for normal sessions, sessionId changes but sessionKey continuity means the same user conversation continues. For cron sessions, each run with the same sessionKey should be isolated.
Looking at the issue description again: "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
The issue is specifically about cron sessions, not all sessions. But the
getOrCreateConversationmethod doesn't know whether a session is a "cron" session or a regular session.Let me look at how the codebase identifies cron sessions:
-
Assistant
...
Assistant
...
Assistant
I see now that there's no specific "cron session" flag. The issue is that cron sessions are identified by their session key pattern (e.g.,
agent:main:cron:*), but this is handled throughignoreSessionPatternswhich completely ignores them, not through any isolation mechanism.Looking at the issue description again: "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
The key insight is that the issue is about context isolation, not about ignoring sessions. The fix should ensure that when a new runtime session (different sessionId) uses the same session key, the old conversation should be archived and a new one created.
Assistant
This test already confirms the expected behavior: when a conversation is archived, a new
getOrCreateConversationcall should create a new conversation, not reuse the archived one. This is exactly what my fix does!The difference is:
- Normal sessions: When the conversation is active (not archived), the session key lookup should reuse the existing conversation and update the sessionId. This is for session continuity.
- Cron sessions: When the conversation is active (not archived) but the sessionId is different, the old conversation should be archived and a new one created. This is for isolation.
The question is: how do we distinguish between "normal" sessions and "cron" sessions?
Looking at the issue description more carefully: "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
Assistant
...
Assistant
Assistant
...
Actually, let me reconsider this problem. Looking at the failing tests:
regression-2026-03-17.test.ts- Testsagent:main:mainsessions being reused across resets with different UUIDsengine.test.ts- Testsagent:main:mainsessions being reused when runtime sessionId changes
These tests are using
agent:main:mainwhich is NOT a cron session. The fix should only apply to sessions where isolation is needed.But the
ConversationStoredoesn't have information about whether a session is a "cron" session. It only knows the sessionId and sessionKey.Wait - maybe the issue is that I need to look at this from a different angle. The issue says "Different isolated cron runtime sessions that reuse a stable session key must not share active LCM context."
The word "isolated" is key here. Maybe there's a specific pattern or flag that indicates a session should be isolated.
Let me look at the
statelessSessionPatternsandignoreSessionPatterns:<tool_call> <function=bash_terminal> <parameter=command> grep -n "statelessSessionPatterns|cron|ignoreSessionPatterns" /lossless-claw/src/db/config.ts | head -30 </tool_call>
Assistant
Let me continue investigating the issue. I need to understand how to distinguish between "cron" sessions and "normal" sessions, or find a different approach to fix the context leak issue.
Assistant
Let me understand the issue better by looking at the session lifecycle more closely.
Assistant
Assistant
Assistant
The previous command didn't execute. Let me try again.
Assistant
Assistant
Let me reconsider the problem and implement a more targeted fix. The issue is specifically about cron sessions, not all sessions. I need to revert my previous change and implement a fix that only affects cron sessions.
Assistant
Looking at this more carefully, I need to distinguish between cron sessions (which should be isolated) and normal sessions (which should maintain continuity). Let me implement a targeted fix that only archives the old conversation for cron sessions.
Assistant
Looking at this problem more carefully, I need a targeted fix that only applies to cron sessions (those with
:cron:in the session key) while preserving normal session continuity. Let me implement this:Assistant
Now I need to implement a targeted fix. The issue is about cron sessions leaking context when reusing a stable session key. The fix should only apply to cron sessions (those with
:cron:in the session key), while preserving normal session continuity for non-cron sessions.Assistant
I notice there are duplicate lines (539-541) that need to be removed. Let me fix that.
Assistant
I notice the closing brace
}for thegetOrCreateConversationmethod is missing. Let me add it back.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.