{"record":{"id":"0c4fb856e337a2ab","repo":"sipeed/picoclaw","slug":"sessions-not-initialized","errorCode":null,"errorMessage":"sessions not initialized","messagePattern":"sessions not initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/agent/context_legacy.go","lineNumber":70,"sourceCode":"\t\t\t)\n\t\t}\n\tcase ContextCompressReasonSummarize:\n\t\tm.maybeSummarize(req.SessionKey)\n\t}\n\treturn nil\n}\n\nfunc (m *legacyContextManager) Ingest(_ context.Context, _ *IngestRequest) error {\n\t// Legacy: no-op. Messages are persisted by Sessions JSONL.\n\treturn nil\n}\n\nfunc (m *legacyContextManager) Clear(_ context.Context, sessionKey string) error {\n\t// Routed (non-default) agents keep history in their own session store,\n\t// so resolve the owning agent instead of assuming the default one.\n\tagent := m.al.agentForSession(sessionKey)\n\tif agent == nil || agent.Sessions == nil {\n\t\treturn fmt.Errorf(\"sessions not initialized\")\n\t}\n\tagent.Sessions.SetHistory(sessionKey, []providers.Message{})\n\tagent.Sessions.SetSummary(sessionKey, \"\")\n\treturn agent.Sessions.Save(sessionKey)\n}\n\n// maybeSummarize triggers summarization if the session history exceeds thresholds.\n// It runs asynchronously in a goroutine.\nfunc (m *legacyContextManager) maybeSummarize(sessionKey string) {\n\tagent := m.al.registry.GetDefaultAgent()\n\tif agent == nil {\n\t\treturn\n\t}\n\n\tnewHistory := agent.Sessions.GetHistory(sessionKey)\n\ttokenEstimate := m.estimateTokens(newHistory)\n\tthreshold := agent.ContextWindow * agent.SummarizeTokenPercent / 100\n","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/sipeed/picoclaw/blob/49183d7e8daed0dba89ddbb6fcb60089401d9680/pkg/agent/context_legacy.go#L52-L88","documentation":"The legacy (default) context manager's Clear() must find the agent that owns the session key via agentForSession, then reset that agent's Sessions store. This error fires when the lookup returns nil (no registered agent owns that session key) or the owning agent was built with a nil Sessions store. It is a wiring/key-space mismatch, not an I/O failure.","triggerScenarios":"Clear(ctx, key) called with a hand-crafted or stale session key that no agent owns (e.g. after the owning agent was removed from config), or against an AgentInstance constructed without session persistence (nil agent.Sessions).","commonSituations":"A /clear-style command arriving for a session from a removed agent; multi-agent setups where old session keys linger; custom embeddings calling the ContextManager API with arbitrary key strings instead of keys produced by session.AllocateRouteSession/BuildMainSessionKey.","solutions":["Use session keys the agent subsystem itself produced (session.BuildMainSessionKey(agentID) or allocateRouteSession) instead of hand-built keys","If the key is stale because the agent was removed, treat this error as 'nothing to clear' at the call site and ack the user","Ensure agents are constructed with sessions enabled so agent.Sessions is non-nil (standard construction path does this)","Re-register the missing agent id if those sessions must stay clearable"],"exampleFix":"// before — arbitrary hand-built key\nif err := ctxMgr.Clear(ctx, \"user-42/main\"); err != nil {\n    return err\n}\n\n// after — key from the session subsystem\nkey := session.BuildMainSessionKey(agent.ID)\nif err := ctxMgr.Clear(ctx, key); err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"agent := al.GetRegistry().GetDefaultAgent() // or the agent you dispatched to\nif agent == nil || agent.Sessions == nil {\n    return fmt.Errorf(\"cannot clear: agent or session store missing\")\n}\nkey := session.BuildMainSessionKey(agent.ID)\nif err := ctxMgr.Clear(ctx, key); err != nil {\n    return err\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass session keys produced by session.BuildMainSessionKey/AllocateRouteSession to ContextManager APIs","Treat 'sessions not initialized' on stale keys as a no-op success at the command layer (nothing to clear)","When removing agents, expect their old session keys to become unclearable and handle gracefully"],"tags":["go","picoclaw","sessions","context-manager","registry","session-keys"],"backgroundTag":null,"analyzedSha":"49183d7e8daed0dba89ddbb6fcb60089401d9680","analyzedAt":"2026-08-15T21:55:41.315Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}