feat: defer session creation until first user+assistant message exchange

- Sessions are no longer created immediately on startup
- Session files only created after at least 1 user message and 1 assistant response
- Prevents empty session files when agent is launched and immediately quit
- Messages are queued until session is initialized
- Continue/resume modes properly mark sessions as already initialized
This commit is contained in:
Mario Zechner 2025-11-12 22:06:02 +01:00
parent 5e988b444b
commit 812f2f43cd
2 changed files with 55 additions and 6 deletions

View file

@ -425,8 +425,8 @@ export async function main(args: string[]) {
}
}
// Start session
sessionManager.startSession(agent.state);
// Note: Session will be started lazily after first user+assistant message exchange
// (unless continuing/resuming, in which case it's already initialized)
// Inject project context (AGENT.md/CLAUDE.md) if not continuing/resuming
if (!parsed.continue && !parsed.resume) {
@ -454,6 +454,11 @@ export async function main(args: string[]) {
// Save messages on completion
if (event.type === "message_end") {
sessionManager.saveMessage(event.message);
// Check if we should initialize session now (after first user+assistant exchange)
if (sessionManager.shouldInitializeSession(agent.state.messages)) {
sessionManager.startSession(agent.state);
}
}
});