@bastani/atomic 0.9.3-alpha.1 → 0.9.3-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/builtin/cursor/CHANGELOG.md +21 -0
  3. package/dist/builtin/cursor/README.md +2 -1
  4. package/dist/builtin/cursor/package.json +2 -2
  5. package/dist/builtin/cursor/src/cursor-models-raw.json +2 -9
  6. package/dist/builtin/cursor/src/model-mapper.ts +14 -3
  7. package/dist/builtin/cursor/src/proto/protobuf-codec-base64.ts +22 -0
  8. package/dist/builtin/cursor/src/proto/protobuf-codec-request.ts +53 -13
  9. package/dist/builtin/cursor/src/proto/protobuf-codec-wire.ts +24 -7
  10. package/dist/builtin/cursor/src/proto/protobuf-codec.ts +3 -2
  11. package/dist/builtin/cursor/src/stream.ts +5 -11
  12. package/dist/builtin/cursor/src/transport-types.ts +3 -0
  13. package/dist/builtin/cursor/src/transport.ts +1 -0
  14. package/dist/builtin/intercom/CHANGELOG.md +6 -0
  15. package/dist/builtin/intercom/package.json +1 -1
  16. package/dist/builtin/mcp/CHANGELOG.md +6 -0
  17. package/dist/builtin/mcp/package.json +1 -1
  18. package/dist/builtin/subagents/CHANGELOG.md +15 -0
  19. package/dist/builtin/subagents/package.json +1 -1
  20. package/dist/builtin/subagents/src/extension/fanout-child.ts +1 -0
  21. package/dist/builtin/subagents/src/extension/index.ts +6 -3
  22. package/dist/builtin/subagents/src/extension/schemas.ts +0 -5
  23. package/dist/builtin/subagents/src/runs/background/async-job-tracker.ts +1 -4
  24. package/dist/builtin/subagents/src/runs/foreground/subagent-executor-single.ts +15 -1
  25. package/dist/builtin/subagents/src/runs/foreground/subagent-executor.ts +35 -1
  26. package/dist/builtin/subagents/src/runs/shared/subagent-prompt-runtime.ts +4 -2
  27. package/dist/builtin/subagents/src/shared/types-async.ts +1 -0
  28. package/dist/builtin/subagents/src/slash/prompt-template-bridge.ts +27 -5
  29. package/dist/builtin/subagents/src/tui/render-layout.ts +27 -4
  30. package/dist/builtin/subagents/src/tui/render-result-animation.ts +22 -31
  31. package/dist/builtin/subagents/src/tui/render-result-compact.ts +6 -6
  32. package/dist/builtin/subagents/src/tui/render-result.ts +20 -19
  33. package/dist/builtin/subagents/src/tui/render-status-progress.ts +3 -3
  34. package/dist/builtin/subagents/src/tui/render-widget.ts +46 -7
  35. package/dist/builtin/subagents/src/tui/render.ts +2 -2
  36. package/dist/builtin/web-access/CHANGELOG.md +6 -0
  37. package/dist/builtin/web-access/package.json +1 -1
  38. package/dist/builtin/workflows/CHANGELOG.md +49 -0
  39. package/dist/builtin/workflows/README.md +1 -1
  40. package/dist/builtin/workflows/package.json +1 -1
  41. package/dist/builtin/workflows/src/authoring.d.ts +1 -1
  42. package/dist/builtin/workflows/src/durable/backend.ts +343 -0
  43. package/dist/builtin/workflows/src/durable/child-primitive.ts +79 -0
  44. package/dist/builtin/workflows/src/durable/dbos-backend.ts +421 -0
  45. package/dist/builtin/workflows/src/durable/dbos-envelope.ts +171 -0
  46. package/dist/builtin/workflows/src/durable/factory.ts +96 -0
  47. package/dist/builtin/workflows/src/durable/file-backend.ts +433 -0
  48. package/dist/builtin/workflows/src/durable/index.ts +73 -0
  49. package/dist/builtin/workflows/src/durable/resume-catalog.ts +217 -0
  50. package/dist/builtin/workflows/src/durable/resume-runtime.ts +299 -0
  51. package/dist/builtin/workflows/src/durable/scoped-backend.ts +171 -0
  52. package/dist/builtin/workflows/src/durable/stage-primitive.ts +284 -0
  53. package/dist/builtin/workflows/src/durable/tool-primitive.ts +180 -0
  54. package/dist/builtin/workflows/src/durable/types.ts +168 -0
  55. package/dist/builtin/workflows/src/durable/ui-primitive.ts +96 -0
  56. package/dist/builtin/workflows/src/engine/options.ts +3 -0
  57. package/dist/builtin/workflows/src/engine/primitives/parallel.ts +2 -2
  58. package/dist/builtin/workflows/src/engine/primitives/task.ts +4 -4
  59. package/dist/builtin/workflows/src/engine/primitives/ui.ts +22 -8
  60. package/dist/builtin/workflows/src/engine/primitives/workflow.ts +8 -0
  61. package/dist/builtin/workflows/src/engine/run-durable-finalize.ts +69 -0
  62. package/dist/builtin/workflows/src/engine/run-durable-stage-session.ts +31 -0
  63. package/dist/builtin/workflows/src/engine/run.ts +148 -6
  64. package/dist/builtin/workflows/src/engine/runtime.ts +8 -2
  65. package/dist/builtin/workflows/src/extension/extension-factory.ts +6 -12
  66. package/dist/builtin/workflows/src/extension/extension-lifecycle.ts +5 -1
  67. package/dist/builtin/workflows/src/extension/extension-runtime-state.ts +3 -0
  68. package/dist/builtin/workflows/src/extension/runtime.ts +48 -9
  69. package/dist/builtin/workflows/src/extension/workflow-run-control-command.ts +143 -4
  70. package/dist/builtin/workflows/src/runs/background/quit.ts +61 -0
  71. package/dist/builtin/workflows/src/runs/background/status.ts +1 -0
  72. package/dist/builtin/workflows/src/runs/foreground/executor-direct-helpers.ts +5 -5
  73. package/dist/builtin/workflows/src/runs/foreground/executor-stage-call.ts +74 -33
  74. package/dist/builtin/workflows/src/runs/foreground/executor-stage-context.ts +20 -1
  75. package/dist/builtin/workflows/src/runs/foreground/executor-stage-factory.ts +8 -7
  76. package/dist/builtin/workflows/src/runs/foreground/executor-stage-replay.ts +1 -0
  77. package/dist/builtin/workflows/src/runs/foreground/executor-stage-types.ts +1 -1
  78. package/dist/builtin/workflows/src/runs/foreground/executor-types.ts +19 -2
  79. package/dist/builtin/workflows/src/runs/foreground/stage-runner-context.ts +4 -0
  80. package/dist/builtin/workflows/src/runs/foreground/stage-runner-controller.ts +10 -10
  81. package/dist/builtin/workflows/src/runs/foreground/stage-runner-options.ts +5 -1
  82. package/dist/builtin/workflows/src/runs/foreground/stage-runner-send-user-message.ts +25 -0
  83. package/dist/builtin/workflows/src/runs/foreground/stage-runner-types.ts +3 -0
  84. package/dist/builtin/workflows/src/shared/authoring-contract-stage.d.ts +16 -0
  85. package/dist/builtin/workflows/src/shared/authoring-contract-stage.ts +20 -0
  86. package/dist/builtin/workflows/src/shared/authoring-contract-ui.d.ts +23 -1
  87. package/dist/builtin/workflows/src/shared/authoring-contract-ui.ts +30 -1
  88. package/dist/builtin/workflows/src/shared/store-public-types.ts +6 -2
  89. package/dist/builtin/workflows/src/shared/store-run-methods.ts +12 -6
  90. package/dist/builtin/workflows/src/shared/types.ts +55 -0
  91. package/dist/builtin/workflows/src/tui/graph-view-constants.ts +1 -1
  92. package/dist/builtin/workflows/src/tui/graph-view-graph-render.ts +41 -0
  93. package/dist/builtin/workflows/src/tui/graph-view-input.ts +82 -24
  94. package/dist/builtin/workflows/src/tui/graph-view-render.ts +7 -0
  95. package/dist/builtin/workflows/src/tui/graph-view-state.ts +22 -2
  96. package/dist/builtin/workflows/src/tui/graph-view-types.ts +4 -5
  97. package/dist/builtin/workflows/src/tui/overlay-adapter.ts +9 -11
  98. package/dist/builtin/workflows/src/tui/stage-chat-view-footer-status.ts +9 -3
  99. package/dist/builtin/workflows/src/tui/stage-chat-view-input.ts +11 -2
  100. package/dist/builtin/workflows/src/tui/stage-chat-view-live-events.ts +35 -0
  101. package/dist/builtin/workflows/src/tui/stage-chat-view-state.ts +51 -17
  102. package/dist/builtin/workflows/src/tui/stage-chat-view-status.ts +36 -0
  103. package/dist/builtin/workflows/src/tui/stage-chat-view-types.ts +5 -1
  104. package/dist/builtin/workflows/src/tui/stage-chat-view.ts +3 -1
  105. package/dist/builtin/workflows/src/tui/status-list.ts +14 -2
  106. package/dist/builtin/workflows/src/tui/widget.ts +23 -8
  107. package/dist/builtin/workflows/src/tui/workflow-attach-pane-types.ts +5 -4
  108. package/dist/builtin/workflows/src/tui/workflow-attach-pane.ts +8 -8
  109. package/dist/builtin/workflows/src/tui/workflow-resume-selector.ts +151 -0
  110. package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
  111. package/dist/core/extensions/loader-virtual-modules.js +47 -30
  112. package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
  113. package/dist/core/messages.d.ts +1 -0
  114. package/dist/core/messages.d.ts.map +1 -1
  115. package/dist/core/messages.js +46 -1
  116. package/dist/core/messages.js.map +1 -1
  117. package/dist/core/sdk.d.ts.map +1 -1
  118. package/dist/core/sdk.js +12 -0
  119. package/dist/core/sdk.js.map +1 -1
  120. package/dist/core/session-manager-core.d.ts +15 -7
  121. package/dist/core/session-manager-core.d.ts.map +1 -1
  122. package/dist/core/session-manager-core.js +20 -9
  123. package/dist/core/session-manager-core.js.map +1 -1
  124. package/dist/core/session-manager-entries.d.ts +2 -2
  125. package/dist/core/session-manager-entries.d.ts.map +1 -1
  126. package/dist/core/session-manager-entries.js +9 -3
  127. package/dist/core/session-manager-entries.js.map +1 -1
  128. package/dist/core/session-manager-history.d.ts.map +1 -1
  129. package/dist/core/session-manager-history.js +2 -1
  130. package/dist/core/session-manager-history.js.map +1 -1
  131. package/dist/core/session-manager-list.d.ts +3 -3
  132. package/dist/core/session-manager-list.d.ts.map +1 -1
  133. package/dist/core/session-manager-list.js +27 -8
  134. package/dist/core/session-manager-list.js.map +1 -1
  135. package/dist/core/session-manager-storage.d.ts +3 -1
  136. package/dist/core/session-manager-storage.d.ts.map +1 -1
  137. package/dist/core/session-manager-storage.js +55 -12
  138. package/dist/core/session-manager-storage.js.map +1 -1
  139. package/dist/core/session-manager-tool-dependencies.d.ts +10 -0
  140. package/dist/core/session-manager-tool-dependencies.d.ts.map +1 -0
  141. package/dist/core/session-manager-tool-dependencies.js +133 -0
  142. package/dist/core/session-manager-tool-dependencies.js.map +1 -0
  143. package/dist/core/session-manager-types.d.ts +22 -0
  144. package/dist/core/session-manager-types.d.ts.map +1 -1
  145. package/dist/core/session-manager-types.js.map +1 -1
  146. package/dist/core/session-manager.d.ts +2 -2
  147. package/dist/core/session-manager.d.ts.map +1 -1
  148. package/dist/core/session-manager.js +1 -1
  149. package/dist/core/session-manager.js.map +1 -1
  150. package/dist/modes/interactive/components/chat-session-host-runtime.d.ts +1 -0
  151. package/dist/modes/interactive/components/chat-session-host-runtime.d.ts.map +1 -1
  152. package/dist/modes/interactive/components/chat-session-host-runtime.js +12 -0
  153. package/dist/modes/interactive/components/chat-session-host-runtime.js.map +1 -1
  154. package/dist/modes/interactive/components/chat-session-host-terminal-cleanup.d.ts +4 -0
  155. package/dist/modes/interactive/components/chat-session-host-terminal-cleanup.d.ts.map +1 -0
  156. package/dist/modes/interactive/components/chat-session-host-terminal-cleanup.js +131 -0
  157. package/dist/modes/interactive/components/chat-session-host-terminal-cleanup.js.map +1 -0
  158. package/dist/modes/interactive/components/chat-session-host.d.ts +2 -0
  159. package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
  160. package/dist/modes/interactive/components/chat-session-host.js +7 -1
  161. package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
  162. package/dist/modes/interactive/components/chat-transcript.d.ts.map +1 -1
  163. package/dist/modes/interactive/components/chat-transcript.js +15 -4
  164. package/dist/modes/interactive/components/chat-transcript.js.map +1 -1
  165. package/dist/modes/interactive/components/tool-execution.d.ts +3 -0
  166. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  167. package/dist/modes/interactive/components/tool-execution.js +26 -0
  168. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  169. package/docs/compaction.md +2 -0
  170. package/docs/models.md +1 -1
  171. package/docs/providers.md +2 -1
  172. package/docs/session-format.md +6 -0
  173. package/docs/sessions.md +6 -0
  174. package/docs/workflows.md +105 -3
  175. package/package.json +4 -3
@@ -1 +1 @@
1
- {"version":3,"file":"session-manager-core.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAIhF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAmCzE,OAAO,KAAK,EAEX,sBAAsB,EACtB,qBAAqB,EAErB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,E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iBAAiB,GACzB,cAAc,CAGhB;IAED,yCAAyC;IACzC,OAAa,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAE5G;IAED,wDAAwD;IACxD,OAAa,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/E,OAAa,OAAO,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAOpG","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { existsSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/**\n * Manages conversation sessions as append-only trees stored in JSONL files.\n *\n * Each session entry has an id and parentId forming a tree structure. The \"leaf\"\n * pointer tracks the current position. Appending creates a child of the current leaf.\n * Branching moves the leaf to an earlier entry, allowing new branches without\n * modifying history.\n *\n * Use buildSessionContext() to get the resolved message list for the LLM, which\n * applies context-deletion filtering and follows the path from root to current leaf.\n */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty or corrupted (no valid header), truncate and start fresh\n\t\t\t// to avoid appending messages without a session header (which breaks the session)\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(this.sessionId, this.cwd, timestamp, options?.parentSession);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session, or create new if none. */\n\tstatic continueRecent(cwd: string, sessionDir?: string): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd()): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List all sessions for a directory. */\n\tstatic async list(cwd: string, sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress);\n\t}\n\n\t/** List all sessions across all project directories. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress);\n\t}\n}\n"]}
1
+ {"version":3,"file":"session-manager-core.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAIhF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAmCzE,OAAO,KAAK,EAEX,sBAAsB,EACtB,qBAAqB,EAErB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,WAAW,EACX,mBAAmB,EACnB,eAAe,EACf,uBAAuB,EACvB,MAAM,4BAA4B,CAAC;AAGpC;;;;;;;;;;GAUG;AACH,qBAAa,cAAc;IAC1B,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,WAAW,CAAmB;IACtC,OAAO,CAAC,IAAI,CAAwC;IACpD,OAAO,CAAC,UAAU,CA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W,EAAE,CAAC,CAExB;IAED,sHAAsH;IACtH,OAAa,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/E,OAAa,OAAO,CACnB,UAAU,CAAC,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,mBAAmB,EAChC,OAAO,CAAC,EAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAE,GACrC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;CAQ1B","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { existsSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/**\n * Manages conversation sessions as append-only trees stored in JSONL files.\n *\n * Each session entry has an id and parentId forming a tree structure. The \"leaf\"\n * pointer tracks the current position. Appending creates a child of the current leaf.\n * Branching moves the leaf to an earlier entry, allowing new branches without\n * modifying history.\n *\n * Use buildSessionContext() to get the resolved message list for the LLM, which\n * applies context-deletion filtering and follows the path from root to current leaf.\n */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty or corrupted (no valid header), truncate and start fresh\n\t\t\t// to avoid appending messages without a session header (which breaks the session)\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session header as internal (e.g. workflow stage). Preserves an existing full marker on reattach. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || (header.internal && header.workflow)) return;\n\t\theader.internal = true;\n\t\tif (workflow) header.workflow = workflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd()): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
@@ -77,7 +77,7 @@ export class SessionManager {
77
77
  }
78
78
  this.sessionId = options?.id ?? createSessionId();
79
79
  const timestamp = new Date().toISOString();
80
- const header = createSessionHeader(this.sessionId, this.cwd, timestamp, options?.parentSession);
80
+ const header = createSessionHeader(this.sessionId, this.cwd, timestamp, options?.parentSession, options?.internal, options?.workflow);
81
81
  this.fileEntries = [header];
82
82
  this.byId.clear();
83
83
  this.labelsById.clear();
@@ -88,6 +88,17 @@ export class SessionManager {
88
88
  }
89
89
  return this.sessionFile;
90
90
  }
91
+ /** Mark the session header as internal (e.g. workflow stage). Preserves an existing full marker on reattach. */
92
+ markSessionInternal(workflow) {
93
+ const header = this.fileEntries.find((entry) => entry.type === "session");
94
+ if (!header || (header.internal && header.workflow))
95
+ return;
96
+ header.internal = true;
97
+ if (workflow)
98
+ header.workflow = workflow;
99
+ if (this.flushed)
100
+ this._rewriteFile();
101
+ }
91
102
  _buildIndex() {
92
103
  const index = buildSessionIndex(this.fileEntries);
93
104
  this.byId = index.byId;
@@ -319,11 +330,11 @@ export class SessionManager {
319
330
  const dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, "..");
320
331
  return new SessionManager(cwd, dir, resolvedPath, true);
321
332
  }
322
- /** Continue the most recent session, or create new if none. */
323
- static continueRecent(cwd, sessionDir) {
333
+ /** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */
334
+ static continueRecent(cwd, sessionDir, options) {
324
335
  const dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);
325
336
  const filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);
326
- const mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined);
337
+ const mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined, options?.includeInternal === true);
327
338
  return new SessionManager(cwd, dir, mostRecent ?? undefined, true);
328
339
  }
329
340
  /** Create an in-memory session (no file persistence) */
@@ -335,12 +346,12 @@ export class SessionManager {
335
346
  const forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);
336
347
  return new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);
337
348
  }
338
- /** List all sessions for a directory. */
339
- static async list(cwd, sessionDir, onProgress) {
340
- return listProjectSessions(cwd, sessionDir, onProgress);
349
+ /** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */
350
+ static async list(cwd, sessionDir, onProgress, options) {
351
+ return listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);
341
352
  }
342
- static async listAll(sessionDirOrOnProgress, onProgress) {
343
- return listAllSessions(sessionDirOrOnProgress, onProgress);
353
+ static async listAll(sessionDirOrOnProgress, onProgress, options) {
354
+ return listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);
344
355
  }
345
356
  }
346
357
  //# sourceMappingURL=session-manager-core.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"session-manager-core.js","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EACN,oBAAoB,EACpB,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,8BAA8B,EAC9B,iBAAiB,EACjB,wBAAwB,EACxB,gBAAgB,EAChB,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,EACtB,8BAA8B,EAC9B,oBAAoB,GACpB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AACvH,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MA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ACD","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { existsSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/**\n * Manages conversation sessions as append-only trees stored in JSONL files.\n *\n * Each session entry has an id and parentId forming a tree structure. The \"leaf\"\n * pointer tracks the current position. Appending creates a child of the current leaf.\n * Branching moves the leaf to an earlier entry, allowing new branches without\n * modifying history.\n *\n * Use buildSessionContext() to get the resolved message list for the LLM, which\n * applies context-deletion filtering and follows the path from root to current leaf.\n */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty or corrupted (no valid header), truncate and start fresh\n\t\t\t// to avoid appending messages without a session header (which breaks the session)\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(this.sessionId, this.cwd, timestamp, options?.parentSession);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session, or create new if none. */\n\tstatic continueRecent(cwd: string, sessionDir?: string): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(dir, filterCwd ? cwd : undefined);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd()): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List all sessions for a directory. */\n\tstatic async list(cwd: string, sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress);\n\t}\n\n\t/** List all sessions across all project directories. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress);\n\t}\n}\n"]}
1
+ {"version":3,"file":"session-manager-core.js","sourceRoot":"","sources":["../../src/core/session-manager-core.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EACN,oBAAoB,EACpB,0BAA0B,EAC1B,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,8BAA8B,EAC9B,iBAAiB,EACjB,wBAAwB,EACxB,gBAAgB,EAChB,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,EACtB,8BAA8B,EAC9B,oBAAoB,GACpB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AACvH,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAC5F,OAAO,EACN,oBAAoB,EACpB,kBAAkB,EAClB,eAAe,EACf,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GACnB,MAAM,8BAA8B,CAAC;AAetC,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAExF;;;;;;;;;;GAUG;AACH,MAAM,OAAO,cAAc;IAa1B,YACC,GAAW,EACX,UAAkB,EAClB,WAA+B,EAC/B,OAAgB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B,EACnB,UAAgC,EAChC,OAAuC;QAEvC,OAAO,mBAAmB,CAAC,GAAG,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC,CAAC;IAC5F,CAAC;IASD,MAAM,CAAC,KAAK,CAAC,OAAO,CACnB,sBAAqD,EACrD,UAAgC,EAChC,OAAuC;QAEvC,OAAO,eAAe,CAAC,sBAAsB,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC,CAAC;IAC/F,CAAC;CACD","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { existsSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { normalizePath, resolvePath } from \"../utils/paths.ts\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tcreateBackupSnapshot,\n\tcreateBranchedSessionState,\n\tforkSessionFromFile,\n} from \"./session-manager-archive.ts\";\nimport {\n\tcreateBranchSummaryEntry,\n\tcreateContextCompactionEntry,\n\tcreateContextWindowChangeEntry,\n\tcreateCustomEntry,\n\tcreateCustomMessageEntry,\n\tcreateLabelEntry,\n\tgetEntriesWithoutHeader,\n\tcreateMessageEntry,\n\tcreateModelChangeEntry,\n\tcreateSessionFilePath,\n\tcreateSessionHeader,\n\tcreateSessionInfoEntry,\n\tcreateThinkingLevelChangeEntry,\n\tgetLatestSessionName,\n} from \"./session-manager-entries.ts\";\nimport { buildSessionContext, buildSessionIndex, buildSessionTree, getBranchPath } from \"./session-manager-history.ts\";\nimport { listAllSessions, listProjectSessions } from \"./session-manager-list.ts\";\nimport { migrateToCurrentVersion } from \"./session-manager-migrations.ts\";\nimport { getDefaultSessionDir, getDefaultSessionDirPath } from \"./session-manager-paths.ts\";\nimport {\n\tappendSessionEntries,\n\tappendSessionEntry,\n\tensureDirectory,\n\tfindMostRecentSession,\n\thasAssistantMessage,\n\tloadEntriesFromFile,\n\twriteSessionEntries,\n} from \"./session-manager-storage.ts\";\nimport type {\n\tBranchSummaryEntry,\n\tContextCompactionStats,\n\tContextDeletionTarget,\n\tFileEntry,\n\tNewSessionOptions,\n\tSessionContext,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionTreeNode,\n\tSessionWorkflowMetadata,\n} from \"./session-manager-types.ts\";\nimport { assertValidSessionId, createSessionId } from \"./session-manager-validation.ts\";\n\n/**\n * Manages conversation sessions as append-only trees stored in JSONL files.\n *\n * Each session entry has an id and parentId forming a tree structure. The \"leaf\"\n * pointer tracks the current position. Appending creates a child of the current leaf.\n * Branching moves the leaf to an earlier entry, allowing new branches without\n * modifying history.\n *\n * Use buildSessionContext() to get the resolved message list for the LLM, which\n * applies context-deletion filtering and follows the path from root to current leaf.\n */\nexport class SessionManager {\n\tprivate sessionId: string = \"\";\n\tprivate sessionFile: string | undefined;\n\tprivate sessionDir: string;\n\tprivate cwd: string;\n\tprivate persist: boolean;\n\tprivate flushed: boolean = false;\n\tprivate fileEntries: FileEntry[] = [];\n\tprivate byId: Map<string, SessionEntry> = new Map();\n\tprivate labelsById: Map<string, string> = new Map();\n\tprivate labelTimestampsById: Map<string, string> = new Map();\n\tprivate leafId: string | null = null;\n\n\tprivate constructor(\n\t\tcwd: string,\n\t\tsessionDir: string,\n\t\tsessionFile: string | undefined,\n\t\tpersist: boolean,\n\t\tnewSessionOptions?: NewSessionOptions,\n\t) {\n\t\tthis.cwd = resolvePath(cwd);\n\t\tthis.sessionDir = normalizePath(sessionDir);\n\t\tthis.persist = persist;\n\t\tif (persist && this.sessionDir) {\n\t\t\tensureDirectory(this.sessionDir);\n\t\t}\n\n\t\tif (sessionFile) {\n\t\t\tthis.setSessionFile(sessionFile);\n\t\t} else {\n\t\t\tthis.newSession(newSessionOptions);\n\t\t}\n\t}\n\n\t/** Switch to a different session file (used for resume and branching) */\n\tsetSessionFile(sessionFile: string): void {\n\t\tthis.sessionFile = resolvePath(sessionFile);\n\t\tif (existsSync(this.sessionFile)) {\n\t\t\tthis.fileEntries = loadEntriesFromFile(this.sessionFile);\n\n\t\t\t// If file was empty or corrupted (no valid header), truncate and start fresh\n\t\t\t// to avoid appending messages without a session header (which breaks the session)\n\t\t\tif (this.fileEntries.length === 0) {\n\t\t\t\tconst explicitPath = this.sessionFile;\n\t\t\t\tthis.newSession();\n\t\t\t\tthis.sessionFile = explicitPath;\n\t\t\t\tthis._rewriteFile();\n\t\t\t\tthis.flushed = true;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\t\tthis.sessionId = header?.id ?? createSessionId();\n\n\t\t\tif (migrateToCurrentVersion(this.fileEntries)) {\n\t\t\t\tthis._rewriteFile();\n\t\t\t}\n\n\t\t\tthis._buildIndex();\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tconst explicitPath = this.sessionFile;\n\t\t\tthis.newSession();\n\t\t\tthis.sessionFile = explicitPath; // preserve explicit path from --session flag\n\t\t}\n\t}\n\n\tnewSession(options?: NewSessionOptions): string | undefined {\n\t\tif (options?.id !== undefined) {\n\t\t\tassertValidSessionId(options.id);\n\t\t}\n\t\tthis.sessionId = options?.id ?? createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst header = createSessionHeader(\n\t\t\tthis.sessionId,\n\t\t\tthis.cwd,\n\t\t\ttimestamp,\n\t\t\toptions?.parentSession,\n\t\t\toptions?.internal,\n\t\t\toptions?.workflow,\n\t\t);\n\t\tthis.fileEntries = [header];\n\t\tthis.byId.clear();\n\t\tthis.labelsById.clear();\n\t\tthis.leafId = null;\n\t\tthis.flushed = false;\n\n\t\tif (this.persist) {\n\t\t\tthis.sessionFile = createSessionFilePath(this.getSessionDir(), timestamp, this.sessionId);\n\t\t}\n\t\treturn this.sessionFile;\n\t}\n\n\t/** Mark the session header as internal (e.g. workflow stage). Preserves an existing full marker on reattach. */\n\tmarkSessionInternal(workflow?: SessionWorkflowMetadata): void {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!header || (header.internal && header.workflow)) return;\n\t\theader.internal = true;\n\t\tif (workflow) header.workflow = workflow;\n\t\tif (this.flushed) this._rewriteFile();\n\t}\n\n\tprivate _buildIndex(): void {\n\t\tconst index = buildSessionIndex(this.fileEntries);\n\t\tthis.byId = index.byId;\n\t\tthis.labelsById = index.labelsById;\n\t\tthis.labelTimestampsById = index.labelTimestampsById;\n\t\tthis.leafId = index.leafId;\n\t}\n\n\tprivate _rewriteFile(): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\t\twriteSessionEntries(this.sessionFile, this.fileEntries);\n\t}\n\n\tisPersisted(): boolean {\n\t\treturn this.persist;\n\t}\n\n\tgetCwd(): string {\n\t\treturn this.cwd;\n\t}\n\n\tgetSessionDir(): string {\n\t\treturn this.sessionDir;\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn this.sessionDir === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tgetSessionId(): string {\n\t\treturn this.sessionId;\n\t}\n\n\tgetSessionFile(): string | undefined {\n\t\treturn this.sessionFile;\n\t}\n\n\t_persist(entry: SessionEntry): void {\n\t\tif (!this.persist || !this.sessionFile) return;\n\n\t\tif (!hasAssistantMessage(this.fileEntries)) {\n\t\t\t// Mark as not flushed so when assistant arrives, all entries get written\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tappendSessionEntries(this.sessionFile, this.fileEntries);\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappendSessionEntry(this.sessionFile, entry);\n\t\t}\n\t}\n\n\tprivate _appendEntry(entry: SessionEntry): void {\n\t\tthis.fileEntries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tthis.leafId = entry.id;\n\t\tthis._persist(entry);\n\t}\n\n\t/** Append a message as child of current leaf, then advance leaf. Returns entry id. */\n\tappendMessage(message: Message | CustomMessage | BashExecutionMessage): string {\n\t\tconst entry = createMessageEntry(message, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendThinkingLevelChange(thinkingLevel: string): string {\n\t\tconst entry = createThinkingLevelChangeEntry(thinkingLevel, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a context window change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendContextWindowChange(contextWindow: number): string {\n\t\tconst entry = createContextWindowChangeEntry(contextWindow, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a model change as child of current leaf, then advance leaf. Returns entry id. */\n\tappendModelChange(provider: string, modelId: string): string {\n\t\tconst entry = createModelChangeEntry(provider, modelId, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append logical deletion metadata for deletion-only context compaction. */\n\tappendContextCompaction(\n\t\tdeletedTargets: ContextDeletionTarget[],\n\t\tprotectedEntryIds: string[],\n\t\tstats: ContextCompactionStats,\n\t\tbackupPath?: string,\n\t): string {\n\t\tconst entry = createContextCompactionEntry(deletedTargets, protectedEntryIds, stats, backupPath, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Write a recoverable snapshot of the current session entries without mutating the active JSONL. */\n\twriteBackupSnapshot(label = \"compact\"): string | undefined {\n\t\tif (!this.persist) return undefined;\n\t\treturn createBackupSnapshot(this.sessionFile, this.fileEntries, label);\n\t}\n\n\t/** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */\n\tappendCustomEntry(customType: string, data?: unknown): string {\n\t\tconst entry = createCustomEntry(customType, data, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Append a session info entry (e.g., display name). Returns entry id. */\n\tappendSessionInfo(name: string): string {\n\t\tconst entry = createSessionInfoEntry(name, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Get the current session name from the latest session_info entry, if any. */\n\tgetSessionName(): string | undefined {\n\t\treturn getLatestSessionName(this.getEntries());\n\t}\n\n\t/** Append a custom message entry (for extensions) that participates in LLM context unless excluded. */\n\tappendCustomMessageEntry<T = unknown>(\n\t\tcustomType: string,\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\tdisplay: boolean,\n\t\tdetails?: T,\n\t\texcludeFromContext?: boolean,\n\t): string {\n\t\tconst entry = createCustomMessageEntry(customType, content, display, details, excludeFromContext, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\tgetLeafId(): string | null {\n\t\treturn this.leafId;\n\t}\n\n\tgetLeafEntry(): SessionEntry | undefined {\n\t\treturn this.leafId ? this.byId.get(this.leafId) : undefined;\n\t}\n\n\tgetEntry(id: string): SessionEntry | undefined {\n\t\treturn this.byId.get(id);\n\t}\n\n\t/** Get all direct children of an entry. */\n\tgetChildren(parentId: string): SessionEntry[] {\n\t\tconst children: SessionEntry[] = [];\n\t\tfor (const entry of this.byId.values()) {\n\t\t\tif (entry.parentId === parentId) {\n\t\t\t\tchildren.push(entry);\n\t\t\t}\n\t\t}\n\t\treturn children;\n\t}\n\n\t/** Get the label for an entry, if any. */\n\tgetLabel(id: string): string | undefined {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\t/** Set or clear a label on an entry. */\n\tappendLabelChange(targetId: string, label: string | undefined): string {\n\t\tif (!this.byId.has(targetId)) {\n\t\t\tthrow new Error(`Entry ${targetId} not found`);\n\t\t}\n\t\tconst entry = createLabelEntry(targetId, label, this.byId, this.leafId);\n\t\tthis._appendEntry(entry);\n\t\tif (label) {\n\t\t\tthis.labelsById.set(targetId, label);\n\t\t\tthis.labelTimestampsById.set(targetId, entry.timestamp);\n\t\t} else {\n\t\t\tthis.labelsById.delete(targetId);\n\t\t\tthis.labelTimestampsById.delete(targetId);\n\t\t}\n\t\treturn entry.id;\n\t}\n\n\t/** Walk from entry to root, returning all entries in path order. */\n\tgetBranch(fromId?: string): SessionEntry[] {\n\t\treturn getBranchPath(fromId ?? this.leafId, this.byId);\n\t}\n\n\t/** Build the session context (what gets sent to the LLM). */\n\tbuildSessionContext(): SessionContext {\n\t\treturn buildSessionContext(this.getEntries(), this.leafId, this.byId);\n\t}\n\n\t/** Get session header. */\n\tgetHeader(): SessionHeader | null {\n\t\tconst header = this.fileEntries.find((entry) => entry.type === \"session\");\n\t\treturn header ? (header as SessionHeader) : null;\n\t}\n\n\t/** Get all session entries (excludes header). Returns a shallow copy. */\n\tgetEntries(): SessionEntry[] {\n\t\treturn getEntriesWithoutHeader(this.fileEntries);\n\t}\n\n\t/** Get the session as a tree structure. Returns a shallow defensive copy of all entries. */\n\tgetTree(): SessionTreeNode[] {\n\t\treturn buildSessionTree(this.getEntries(), this.labelsById, this.labelTimestampsById);\n\t}\n\n\t/** Start a new branch from an earlier entry. */\n\tbranch(branchFromId: string): void {\n\t\tif (!this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t}\n\n\t/** Reset the leaf pointer to null (before any entries). */\n\tresetLeaf(): void {\n\t\tthis.leafId = null;\n\t}\n\n\t/** Start a new branch with a summary of the abandoned path. */\n\tbranchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromHook?: boolean): string {\n\t\tif (branchFromId !== null && !this.byId.has(branchFromId)) {\n\t\t\tthrow new Error(`Entry ${branchFromId} not found`);\n\t\t}\n\t\tthis.leafId = branchFromId;\n\t\tconst entry: BranchSummaryEntry = createBranchSummaryEntry(branchFromId, summary, details, fromHook, this.byId);\n\t\tthis._appendEntry(entry);\n\t\treturn entry.id;\n\t}\n\n\t/** Create a new session file containing only the path from root to the specified leaf. */\n\tcreateBranchedSession(leafId: string): string | undefined {\n\t\tconst result = createBranchedSessionState({\n\t\t\tleafId,\n\t\t\tpersist: this.persist,\n\t\t\tsessionDir: this.getSessionDir(),\n\t\t\tcwd: this.cwd,\n\t\t\tpreviousSessionFile: this.sessionFile,\n\t\t\tpath: this.getBranch(leafId),\n\t\t\tlabelsById: this.labelsById,\n\t\t\tlabelTimestampsById: this.labelTimestampsById,\n\t\t});\n\t\tthis.fileEntries = result.fileEntries;\n\t\tthis.sessionId = result.sessionId;\n\t\tif (result.sessionFile) this.sessionFile = result.sessionFile;\n\t\tthis._buildIndex();\n\n\t\tif (this.persist) {\n\t\t\tif (result.shouldRewriteFile) this._rewriteFile();\n\t\t\tthis.flushed = result.flushed ?? false;\n\t\t\treturn result.sessionFile;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/** Create a new session. */\n\tstatic create(cwd: string, sessionDir?: string, options?: NewSessionOptions): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\treturn new SessionManager(cwd, dir, undefined, true, options);\n\t}\n\n\t/** Open a specific session file. */\n\tstatic open(path: string, sessionDir?: string, cwdOverride?: string): SessionManager {\n\t\tconst resolvedPath = resolvePath(path);\n\t\tconst entries = loadEntriesFromFile(resolvedPath);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = cwdOverride ?? header?.cwd ?? process.cwd();\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : resolve(resolvedPath, \"..\");\n\t\treturn new SessionManager(cwd, dir, resolvedPath, true);\n\t}\n\n\t/** Continue the most recent session (skips internal workflow sessions unless `includeInternal: true`). */\n\tstatic continueRecent(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: { includeInternal?: boolean },\n\t): SessionManager {\n\t\tconst dir = sessionDir ? normalizePath(sessionDir) : getDefaultSessionDir(cwd);\n\t\tconst filterCwd = sessionDir !== undefined && dir !== getDefaultSessionDirPath(cwd);\n\t\tconst mostRecent = findMostRecentSession(\n\t\t\tdir,\n\t\t\tfilterCwd ? cwd : undefined,\n\t\t\toptions?.includeInternal === true,\n\t\t);\n\t\treturn new SessionManager(cwd, dir, mostRecent ?? undefined, true);\n\t}\n\n\t/** Create an in-memory session (no file persistence) */\n\tstatic inMemory(cwd: string = process.cwd()): SessionManager {\n\t\treturn new SessionManager(cwd, \"\", undefined, false);\n\t}\n\n\t/** Fork a session from another project directory into the current project. */\n\tstatic forkFrom(\n\t\tsourcePath: string,\n\t\ttargetCwd: string,\n\t\tsessionDir?: string,\n\t\toptions?: NewSessionOptions,\n\t): SessionManager {\n\t\tconst forked = forkSessionFromFile(sourcePath, targetCwd, sessionDir, options);\n\t\treturn new SessionManager(forked.cwd, forked.sessionDir, forked.sessionFile, true);\n\t}\n\n\t/** List sessions for a directory. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async list(\n\t\tcwd: string,\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listProjectSessions(cwd, sessionDir, onProgress, options?.includeInternal === true);\n\t}\n\n\t/** List sessions across all directories. Internal (workflow) sessions are excluded unless `includeInternal: true`. */\n\tstatic async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDir?: string,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]>;\n\tstatic async listAll(\n\t\tsessionDirOrOnProgress?: string | SessionListProgress,\n\t\tonProgress?: SessionListProgress,\n\t\toptions?: { includeInternal?: boolean },\n\t): Promise<SessionInfo[]> {\n\t\treturn listAllSessions(sessionDirOrOnProgress, onProgress, options?.includeInternal === true);\n\t}\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import type { ImageContent, Message, TextContent } from "@earendil-works/pi-ai";
2
2
  import type { BashExecutionMessage, CustomMessage } from "./messages.ts";
3
- import { type BranchSummaryEntry, type ContextCompactionEntry, type ContextCompactionStats, type ContextDeletionTarget, type ContextWindowChangeEntry, type CustomEntry, type CustomMessageEntry, type FileEntry, type LabelEntry, type ModelChangeEntry, type SessionEntry, type SessionHeader, type SessionInfoEntry, type SessionMessageEntry, type ThinkingLevelChangeEntry } from "./session-manager-types.ts";
4
- export declare function createSessionHeader(id: string, cwd: string, timestamp?: string, parentSession?: string): SessionHeader;
3
+ import { type BranchSummaryEntry, type ContextCompactionEntry, type ContextCompactionStats, type ContextDeletionTarget, type ContextWindowChangeEntry, type CustomEntry, type CustomMessageEntry, type FileEntry, type LabelEntry, type ModelChangeEntry, type SessionEntry, type SessionHeader, type SessionInfoEntry, type SessionMessageEntry, type SessionWorkflowMetadata, type ThinkingLevelChangeEntry } from "./session-manager-types.ts";
4
+ export declare function createSessionHeader(id: string, cwd: string, timestamp?: string, parentSession?: string, internal?: boolean, workflow?: SessionWorkflowMetadata): SessionHeader;
5
5
  export declare function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string;
6
6
  export declare function createMessageEntry(message: Message | CustomMessage | BashExecutionMessage, byId: {
7
7
  has(id: string): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"session-manager-entries.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-entries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAEN,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAC3B,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,EAC7B,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAEjB,KAAK,aAAa,EAClB,KAAK,gBAAgB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,SAAS,CAAC,EAAE,MAAM,GAChB,UAAU,CAQZ;AAED,wBAAgB,wBAAwB,CACvC,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,OAAO,GAAG,SAAS,EAC7B,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAChC,kBAAkB,CASpB;AAED,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,SAAS,EAAE,GAAG,YAAY,EAAE,CAEhF","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { join } from \"path\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tCURRENT_SESSION_VERSION,\n\ttype BranchSummaryEntry,\n\ttype ContextCompactionEntry,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype ContextWindowChangeEntry,\n\ttype CustomEntry,\n\ttype CustomMessageEntry,\n\ttype FileEntry,\n\ttype LabelEntry,\n\ttype ModelChangeEntry,\n\ttype SessionEntry,\n\ttype SessionEntryBase,\n\ttype SessionHeader,\n\ttype SessionInfoEntry,\n\ttype SessionMessageEntry,\n\ttype ThinkingLevelChangeEntry,\n} from \"./session-manager-types.ts\";\nimport { generateId } from \"./session-manager-validation.ts\";\n\nfunction entryBase(byId: { has(id: string): boolean }, parentId: string | null): Pick<SessionEntryBase, \"id\" | \"parentId\" | \"timestamp\"> {\n\treturn {\n\t\tid: generateId(byId),\n\t\tparentId,\n\t\ttimestamp: new Date().toISOString(),\n\t};\n}\n\nexport function createSessionHeader(\n\tid: string,\n\tcwd: string,\n\ttimestamp: string = new Date().toISOString(),\n\tparentSession?: string,\n): SessionHeader {\n\treturn {\n\t\ttype: \"session\",\n\t\tversion: CURRENT_SESSION_VERSION,\n\t\tid,\n\t\ttimestamp,\n\t\tcwd,\n\t\tparentSession,\n\t};\n}\n\nexport function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function createMessageEntry(\n\tmessage: Message | CustomMessage | BashExecutionMessage,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionMessageEntry {\n\treturn {\n\t\ttype: \"message\",\n\t\t...entryBase(byId, parentId),\n\t\tmessage,\n\t};\n}\n\nexport function createThinkingLevelChangeEntry(\n\tthinkingLevel: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ThinkingLevelChangeEntry {\n\treturn {\n\t\ttype: \"thinking_level_change\",\n\t\t...entryBase(byId, parentId),\n\t\tthinkingLevel,\n\t};\n}\n\nexport function createContextWindowChangeEntry(\n\tcontextWindow: number,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextWindowChangeEntry {\n\treturn {\n\t\ttype: \"context_window_change\",\n\t\t...entryBase(byId, parentId),\n\t\tcontextWindow,\n\t};\n}\n\nexport function createModelChangeEntry(\n\tprovider: string,\n\tmodelId: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ModelChangeEntry {\n\treturn {\n\t\ttype: \"model_change\",\n\t\t...entryBase(byId, parentId),\n\t\tprovider,\n\t\tmodelId,\n\t};\n}\n\nexport function createContextCompactionEntry(\n\tdeletedTargets: ContextDeletionTarget[],\n\tprotectedEntryIds: string[],\n\tstats: ContextCompactionStats,\n\tbackupPath: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextCompactionEntry {\n\treturn {\n\t\ttype: \"context_compaction\",\n\t\t...entryBase(byId, parentId),\n\t\tpromptVersion: 1,\n\t\tdeletedTargets,\n\t\tprotectedEntryIds,\n\t\tstats,\n\t\tbackupPath,\n\t};\n}\n\nexport function createCustomEntry(\n\tcustomType: string,\n\tdata: unknown,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomEntry {\n\treturn {\n\t\ttype: \"custom\",\n\t\tcustomType,\n\t\tdata,\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createSessionInfoEntry(\n\tname: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionInfoEntry {\n\treturn {\n\t\ttype: \"session_info\",\n\t\t...entryBase(byId, parentId),\n\t\tname: name.trim(),\n\t};\n}\n\nexport function getLatestSessionName(entries: SessionEntry[]): string | undefined {\n\t// Walk entries in reverse to find the latest session_info entry.\n\t// Empty names explicitly clear the session title.\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"session_info\") {\n\t\t\treturn entry.name?.trim() || undefined;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport function createCustomMessageEntry<T = unknown>(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: T | undefined,\n\texcludeFromContext: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomMessageEntry<T> {\n\treturn {\n\t\ttype: \"custom_message\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\t...(excludeFromContext === true ? { excludeFromContext: true } : {}),\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createLabelEntry(\n\ttargetId: string,\n\tlabel: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n\ttimestamp?: string,\n): LabelEntry {\n\treturn {\n\t\ttype: \"label\",\n\t\t...entryBase(byId, parentId),\n\t\t...(timestamp !== undefined ? { timestamp } : {}),\n\t\ttargetId,\n\t\tlabel,\n\t};\n}\n\nexport function createBranchSummaryEntry(\n\tbranchFromId: string | null,\n\tsummary: string,\n\tdetails: unknown,\n\tfromHook: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n): BranchSummaryEntry {\n\treturn {\n\t\ttype: \"branch_summary\",\n\t\t...entryBase(byId, branchFromId),\n\t\tfromId: branchFromId ?? \"root\",\n\t\tsummary,\n\t\tdetails,\n\t\tfromHook,\n\t};\n}\n\nexport function getEntriesWithoutHeader(fileEntries: FileEntry[]): SessionEntry[] {\n\treturn fileEntries.filter((entry): entry is SessionEntry => entry.type !== \"session\");\n}\n"]}
1
+ {"version":3,"file":"session-manager-entries.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-entries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAEN,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAC3B,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,EAC7B,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAEjB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,uBAAuB,EAC5B,KAAK,wBAAwB,EAC7B,MAAM,4BAA4B,CAAC;AAWpC,wBAAgB,mBAAmB,CAClC,EAAE,EAAE,MAAM,EACV,GAAG,EAAE,MAAM,EACX,SAAS,GAAE,MAAiC,EAC5C,aAAa,CAAC,EAAE,MAAM,EACtB,QAAQ,CAAC,EAAE,OAAO,EAClB,QAAQ,CAAC,EAAE,uBAAuB,GAChC,aAAa,CAYf;AAED,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAGtG;AAED,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,OAAO,GAAG,aAAa,GAAG,oBAAoB,EACvD,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,EAAE,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,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAClC,QAAQ,EAAE,MAAM,GAAG,IAAI,GACrB,gBAAgB,CAMlB;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM,GAAG,SAAS,CAUhF;AAED,wBAAgB,wBAAwB,CAAC,CAAC,GAAG,OAAO,EACnD,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,GAAG,CAAC,WAAW,GAAG,YAAY,CAAC,EAAE,EAChD,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,CAAC,GAAG,SAAS,EACtB,kBAAkB,EAAE,OAAO,GAAG,SAAS,EACvC,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAClC,QAAQ,EAAE,MAAM,GAAG,IAAI,GACrB,kBAAkB,CAAC,CAAC,CAAC,CAUvB;AAED,wBAAgB,gBAAgB,CAC/B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAClC,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,SAAS,CAAC,EAAE,MAAM,GAChB,UAAU,CAQZ;AAED,wBAAgB,wBAAwB,CACvC,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,OAAO,GAAG,SAAS,EAC7B,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAChC,kBAAkB,CASpB;AAED,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,SAAS,EAAE,GAAG,YAAY,EAAE,CAEhF","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { join } from \"path\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tCURRENT_SESSION_VERSION,\n\ttype BranchSummaryEntry,\n\ttype ContextCompactionEntry,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype ContextWindowChangeEntry,\n\ttype CustomEntry,\n\ttype CustomMessageEntry,\n\ttype FileEntry,\n\ttype LabelEntry,\n\ttype ModelChangeEntry,\n\ttype SessionEntry,\n\ttype SessionEntryBase,\n\ttype SessionHeader,\n\ttype SessionInfoEntry,\n\ttype SessionMessageEntry,\n\ttype SessionWorkflowMetadata,\n\ttype ThinkingLevelChangeEntry,\n} from \"./session-manager-types.ts\";\nimport { generateId } from \"./session-manager-validation.ts\";\n\nfunction entryBase(byId: { has(id: string): boolean }, parentId: string | null): Pick<SessionEntryBase, \"id\" | \"parentId\" | \"timestamp\"> {\n\treturn {\n\t\tid: generateId(byId),\n\t\tparentId,\n\t\ttimestamp: new Date().toISOString(),\n\t};\n}\n\nexport function createSessionHeader(\n\tid: string,\n\tcwd: string,\n\ttimestamp: string = new Date().toISOString(),\n\tparentSession?: string,\n\tinternal?: boolean,\n\tworkflow?: SessionWorkflowMetadata,\n): SessionHeader {\n\tconst header: SessionHeader = {\n\t\ttype: \"session\",\n\t\tversion: CURRENT_SESSION_VERSION,\n\t\tid,\n\t\ttimestamp,\n\t\tcwd,\n\t};\n\tif (parentSession !== undefined) header.parentSession = parentSession;\n\tif (internal) header.internal = true;\n\tif (workflow) header.workflow = workflow;\n\treturn header;\n}\n\nexport function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function createMessageEntry(\n\tmessage: Message | CustomMessage | BashExecutionMessage,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionMessageEntry {\n\treturn {\n\t\ttype: \"message\",\n\t\t...entryBase(byId, parentId),\n\t\tmessage,\n\t};\n}\n\nexport function createThinkingLevelChangeEntry(\n\tthinkingLevel: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ThinkingLevelChangeEntry {\n\treturn {\n\t\ttype: \"thinking_level_change\",\n\t\t...entryBase(byId, parentId),\n\t\tthinkingLevel,\n\t};\n}\n\nexport function createContextWindowChangeEntry(\n\tcontextWindow: number,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextWindowChangeEntry {\n\treturn {\n\t\ttype: \"context_window_change\",\n\t\t...entryBase(byId, parentId),\n\t\tcontextWindow,\n\t};\n}\n\nexport function createModelChangeEntry(\n\tprovider: string,\n\tmodelId: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ModelChangeEntry {\n\treturn {\n\t\ttype: \"model_change\",\n\t\t...entryBase(byId, parentId),\n\t\tprovider,\n\t\tmodelId,\n\t};\n}\n\nexport function createContextCompactionEntry(\n\tdeletedTargets: ContextDeletionTarget[],\n\tprotectedEntryIds: string[],\n\tstats: ContextCompactionStats,\n\tbackupPath: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextCompactionEntry {\n\treturn {\n\t\ttype: \"context_compaction\",\n\t\t...entryBase(byId, parentId),\n\t\tpromptVersion: 1,\n\t\tdeletedTargets,\n\t\tprotectedEntryIds,\n\t\tstats,\n\t\tbackupPath,\n\t};\n}\n\nexport function createCustomEntry(\n\tcustomType: string,\n\tdata: unknown,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomEntry {\n\treturn {\n\t\ttype: \"custom\",\n\t\tcustomType,\n\t\tdata,\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createSessionInfoEntry(\n\tname: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionInfoEntry {\n\treturn {\n\t\ttype: \"session_info\",\n\t\t...entryBase(byId, parentId),\n\t\tname: name.trim(),\n\t};\n}\n\nexport function getLatestSessionName(entries: SessionEntry[]): string | undefined {\n\t// Walk entries in reverse to find the latest session_info entry.\n\t// Empty names explicitly clear the session title.\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"session_info\") {\n\t\t\treturn entry.name?.trim() || undefined;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport function createCustomMessageEntry<T = unknown>(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: T | undefined,\n\texcludeFromContext: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomMessageEntry<T> {\n\treturn {\n\t\ttype: \"custom_message\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\t...(excludeFromContext === true ? { excludeFromContext: true } : {}),\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createLabelEntry(\n\ttargetId: string,\n\tlabel: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n\ttimestamp?: string,\n): LabelEntry {\n\treturn {\n\t\ttype: \"label\",\n\t\t...entryBase(byId, parentId),\n\t\t...(timestamp !== undefined ? { timestamp } : {}),\n\t\ttargetId,\n\t\tlabel,\n\t};\n}\n\nexport function createBranchSummaryEntry(\n\tbranchFromId: string | null,\n\tsummary: string,\n\tdetails: unknown,\n\tfromHook: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n): BranchSummaryEntry {\n\treturn {\n\t\ttype: \"branch_summary\",\n\t\t...entryBase(byId, branchFromId),\n\t\tfromId: branchFromId ?? \"root\",\n\t\tsummary,\n\t\tdetails,\n\t\tfromHook,\n\t};\n}\n\nexport function getEntriesWithoutHeader(fileEntries: FileEntry[]): SessionEntry[] {\n\treturn fileEntries.filter((entry): entry is SessionEntry => entry.type !== \"session\");\n}\n"]}
@@ -8,15 +8,21 @@ function entryBase(byId, parentId) {
8
8
  timestamp: new Date().toISOString(),
9
9
  };
10
10
  }
11
- export function createSessionHeader(id, cwd, timestamp = new Date().toISOString(), parentSession) {
12
- return {
11
+ export function createSessionHeader(id, cwd, timestamp = new Date().toISOString(), parentSession, internal, workflow) {
12
+ const header = {
13
13
  type: "session",
14
14
  version: CURRENT_SESSION_VERSION,
15
15
  id,
16
16
  timestamp,
17
17
  cwd,
18
- parentSession,
19
18
  };
19
+ if (parentSession !== undefined)
20
+ header.parentSession = parentSession;
21
+ if (internal)
22
+ header.internal = true;
23
+ if (workflow)
24
+ header.workflow = workflow;
25
+ return header;
20
26
  }
21
27
  export function createSessionFilePath(sessionDir, timestamp, sessionId) {
22
28
  const fileTimestamp = timestamp.replace(/[:.]/g, "-");
@@ -1 +1 @@
1
- {"version":3,"file":"session-manager-entries.js","sourceRoot":"","sources":["../../src/core/session-manager-entries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EACN,uBAAuB,GAiBvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,SAAS,SAAS,CAAC,IAAkC,EAAE,QAAuB;IAC7E,OAAO;QACN,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC;QACpB,QAAQ;QACR,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACnC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAClC,EAAU,EACV,GAAW,EACX,SAAS,GAAW,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAC5C,aAAsB;IAEtB,OAAO;QACN,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,uBAAuB;QAChC,EAAE;QACF,SAAS;QACT,GAAG;QACH,aAAa;KACb,CAAC;AACH,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,UAAkB,EAAE,SAAiB,EAAE,SAAiB;IAC7F,MAAM,aAAa,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IACtD,OAA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vB,SAAkB;IAElB,OAAO;QACN,IAAI,EAAE,OAAO;QACb,GAAG,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;QAC5B,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjD,QAAQ;QACR,KAAK;KACL,CAAC;AACH,CAAC;AAED,MAAM,UAAU,wBAAwB,CACvC,YAA2B,EAC3B,OAAe,EACf,OAAgB,EAChB,QAA6B,EAC7B,IAAkC;IAElC,OAAO;QACN,IAAI,EAAE,gBAAgB;QACtB,GAAG,SAAS,CAAC,IAAI,EAAE,YAAY,CAAC;QAChC,MAAM,EAAE,YAAY,IAAI,MAAM;QAC9B,OAAO;QACP,OAAO;QACP,QAAQ;KACR,CAAC;AACH,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,WAAwB;IAC/D,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AACvF,CAAC","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { join } from \"path\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tCURRENT_SESSION_VERSION,\n\ttype BranchSummaryEntry,\n\ttype ContextCompactionEntry,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype ContextWindowChangeEntry,\n\ttype CustomEntry,\n\ttype CustomMessageEntry,\n\ttype FileEntry,\n\ttype LabelEntry,\n\ttype ModelChangeEntry,\n\ttype SessionEntry,\n\ttype SessionEntryBase,\n\ttype SessionHeader,\n\ttype SessionInfoEntry,\n\ttype SessionMessageEntry,\n\ttype ThinkingLevelChangeEntry,\n} from \"./session-manager-types.ts\";\nimport { generateId } from \"./session-manager-validation.ts\";\n\nfunction entryBase(byId: { has(id: string): boolean }, parentId: string | null): Pick<SessionEntryBase, \"id\" | \"parentId\" | \"timestamp\"> {\n\treturn {\n\t\tid: generateId(byId),\n\t\tparentId,\n\t\ttimestamp: new Date().toISOString(),\n\t};\n}\n\nexport function createSessionHeader(\n\tid: string,\n\tcwd: string,\n\ttimestamp: string = new Date().toISOString(),\n\tparentSession?: string,\n): SessionHeader {\n\treturn {\n\t\ttype: \"session\",\n\t\tversion: CURRENT_SESSION_VERSION,\n\t\tid,\n\t\ttimestamp,\n\t\tcwd,\n\t\tparentSession,\n\t};\n}\n\nexport function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function createMessageEntry(\n\tmessage: Message | CustomMessage | BashExecutionMessage,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionMessageEntry {\n\treturn {\n\t\ttype: \"message\",\n\t\t...entryBase(byId, parentId),\n\t\tmessage,\n\t};\n}\n\nexport function createThinkingLevelChangeEntry(\n\tthinkingLevel: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ThinkingLevelChangeEntry {\n\treturn {\n\t\ttype: \"thinking_level_change\",\n\t\t...entryBase(byId, parentId),\n\t\tthinkingLevel,\n\t};\n}\n\nexport function createContextWindowChangeEntry(\n\tcontextWindow: number,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextWindowChangeEntry {\n\treturn {\n\t\ttype: \"context_window_change\",\n\t\t...entryBase(byId, parentId),\n\t\tcontextWindow,\n\t};\n}\n\nexport function createModelChangeEntry(\n\tprovider: string,\n\tmodelId: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ModelChangeEntry {\n\treturn {\n\t\ttype: \"model_change\",\n\t\t...entryBase(byId, parentId),\n\t\tprovider,\n\t\tmodelId,\n\t};\n}\n\nexport function createContextCompactionEntry(\n\tdeletedTargets: ContextDeletionTarget[],\n\tprotectedEntryIds: string[],\n\tstats: ContextCompactionStats,\n\tbackupPath: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextCompactionEntry {\n\treturn {\n\t\ttype: \"context_compaction\",\n\t\t...entryBase(byId, parentId),\n\t\tpromptVersion: 1,\n\t\tdeletedTargets,\n\t\tprotectedEntryIds,\n\t\tstats,\n\t\tbackupPath,\n\t};\n}\n\nexport function createCustomEntry(\n\tcustomType: string,\n\tdata: unknown,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomEntry {\n\treturn {\n\t\ttype: \"custom\",\n\t\tcustomType,\n\t\tdata,\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createSessionInfoEntry(\n\tname: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionInfoEntry {\n\treturn {\n\t\ttype: \"session_info\",\n\t\t...entryBase(byId, parentId),\n\t\tname: name.trim(),\n\t};\n}\n\nexport function getLatestSessionName(entries: SessionEntry[]): string | undefined {\n\t// Walk entries in reverse to find the latest session_info entry.\n\t// Empty names explicitly clear the session title.\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"session_info\") {\n\t\t\treturn entry.name?.trim() || undefined;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport function createCustomMessageEntry<T = unknown>(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: T | undefined,\n\texcludeFromContext: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomMessageEntry<T> {\n\treturn {\n\t\ttype: \"custom_message\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\t...(excludeFromContext === true ? { excludeFromContext: true } : {}),\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createLabelEntry(\n\ttargetId: string,\n\tlabel: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n\ttimestamp?: string,\n): LabelEntry {\n\treturn {\n\t\ttype: \"label\",\n\t\t...entryBase(byId, parentId),\n\t\t...(timestamp !== undefined ? { timestamp } : {}),\n\t\ttargetId,\n\t\tlabel,\n\t};\n}\n\nexport function createBranchSummaryEntry(\n\tbranchFromId: string | null,\n\tsummary: string,\n\tdetails: unknown,\n\tfromHook: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n): BranchSummaryEntry {\n\treturn {\n\t\ttype: \"branch_summary\",\n\t\t...entryBase(byId, branchFromId),\n\t\tfromId: branchFromId ?? \"root\",\n\t\tsummary,\n\t\tdetails,\n\t\tfromHook,\n\t};\n}\n\nexport function getEntriesWithoutHeader(fileEntries: FileEntry[]): SessionEntry[] {\n\treturn fileEntries.filter((entry): entry is SessionEntry => entry.type !== \"session\");\n}\n"]}
1
+ {"version":3,"file":"session-manager-entries.js","sourceRoot":"","sources":["../../src/core/session-manager-entries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EACN,uBAAuB,GAkBvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,SAAS,SAAS,CAAC,IAAkC,EAAE,QAAuB;IAC7E,OAAO;QACN,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC;QACpB,QAAQ;QACR,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACnC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAClC,EAAU,EACV,GAAW,EACX,SAAS,GAAW,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAC5C,aAAsB,EACtB,QAAkB,EAClB,QAAkC;IAElC,MAAM,MAAM,GAAkB;QAC7B,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,uBAAuB;QAChC,EAAE;QACF,SAAS;QACT,GAAG;KACH,CAAC;IACF,IAAI,aAAa,KAAK,SAAS;QAAE,MAAM,C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;QACN,IAAI,EAAE,gBAAgB;QACtB,UAAU;QACV,OAAO;QACP,OAAO;QACP,OAAO;QACP,GAAG,CAAC,kBAAkB,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACpE,GAAG,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;KAC5B,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC/B,QAAgB,EAChB,KAAyB,EACzB,IAAkC,EAClC,QAAuB,EACvB,SAAkB;IAElB,OAAO;QACN,IAAI,EAAE,OAAO;QACb,GAAG,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;QAC5B,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjD,QAAQ;QACR,KAAK;KACL,CAAC;AACH,CAAC;AAED,MAAM,UAAU,wBAAwB,CACvC,YAA2B,EAC3B,OAAe,EACf,OAAgB,EAChB,QAA6B,EAC7B,IAAkC;IAElC,OAAO;QACN,IAAI,EAAE,gBAAgB;QACtB,GAAG,SAAS,CAAC,IAAI,EAAE,YAAY,CAAC;QAChC,MAAM,EAAE,YAAY,IAAI,MAAM;QAC9B,OAAO;QACP,OAAO;QACP,QAAQ;KACR,CAAC;AACH,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,WAAwB;IAC/D,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AACvF,CAAC","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@earendil-works/pi-ai\";\nimport { join } from \"path\";\nimport type { BashExecutionMessage, CustomMessage } from \"./messages.ts\";\nimport {\n\tCURRENT_SESSION_VERSION,\n\ttype BranchSummaryEntry,\n\ttype ContextCompactionEntry,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype ContextWindowChangeEntry,\n\ttype CustomEntry,\n\ttype CustomMessageEntry,\n\ttype FileEntry,\n\ttype LabelEntry,\n\ttype ModelChangeEntry,\n\ttype SessionEntry,\n\ttype SessionEntryBase,\n\ttype SessionHeader,\n\ttype SessionInfoEntry,\n\ttype SessionMessageEntry,\n\ttype SessionWorkflowMetadata,\n\ttype ThinkingLevelChangeEntry,\n} from \"./session-manager-types.ts\";\nimport { generateId } from \"./session-manager-validation.ts\";\n\nfunction entryBase(byId: { has(id: string): boolean }, parentId: string | null): Pick<SessionEntryBase, \"id\" | \"parentId\" | \"timestamp\"> {\n\treturn {\n\t\tid: generateId(byId),\n\t\tparentId,\n\t\ttimestamp: new Date().toISOString(),\n\t};\n}\n\nexport function createSessionHeader(\n\tid: string,\n\tcwd: string,\n\ttimestamp: string = new Date().toISOString(),\n\tparentSession?: string,\n\tinternal?: boolean,\n\tworkflow?: SessionWorkflowMetadata,\n): SessionHeader {\n\tconst header: SessionHeader = {\n\t\ttype: \"session\",\n\t\tversion: CURRENT_SESSION_VERSION,\n\t\tid,\n\t\ttimestamp,\n\t\tcwd,\n\t};\n\tif (parentSession !== undefined) header.parentSession = parentSession;\n\tif (internal) header.internal = true;\n\tif (workflow) header.workflow = workflow;\n\treturn header;\n}\n\nexport function createSessionFilePath(sessionDir: string, timestamp: string, sessionId: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function createMessageEntry(\n\tmessage: Message | CustomMessage | BashExecutionMessage,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionMessageEntry {\n\treturn {\n\t\ttype: \"message\",\n\t\t...entryBase(byId, parentId),\n\t\tmessage,\n\t};\n}\n\nexport function createThinkingLevelChangeEntry(\n\tthinkingLevel: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ThinkingLevelChangeEntry {\n\treturn {\n\t\ttype: \"thinking_level_change\",\n\t\t...entryBase(byId, parentId),\n\t\tthinkingLevel,\n\t};\n}\n\nexport function createContextWindowChangeEntry(\n\tcontextWindow: number,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextWindowChangeEntry {\n\treturn {\n\t\ttype: \"context_window_change\",\n\t\t...entryBase(byId, parentId),\n\t\tcontextWindow,\n\t};\n}\n\nexport function createModelChangeEntry(\n\tprovider: string,\n\tmodelId: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ModelChangeEntry {\n\treturn {\n\t\ttype: \"model_change\",\n\t\t...entryBase(byId, parentId),\n\t\tprovider,\n\t\tmodelId,\n\t};\n}\n\nexport function createContextCompactionEntry(\n\tdeletedTargets: ContextDeletionTarget[],\n\tprotectedEntryIds: string[],\n\tstats: ContextCompactionStats,\n\tbackupPath: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): ContextCompactionEntry {\n\treturn {\n\t\ttype: \"context_compaction\",\n\t\t...entryBase(byId, parentId),\n\t\tpromptVersion: 1,\n\t\tdeletedTargets,\n\t\tprotectedEntryIds,\n\t\tstats,\n\t\tbackupPath,\n\t};\n}\n\nexport function createCustomEntry(\n\tcustomType: string,\n\tdata: unknown,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomEntry {\n\treturn {\n\t\ttype: \"custom\",\n\t\tcustomType,\n\t\tdata,\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createSessionInfoEntry(\n\tname: string,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): SessionInfoEntry {\n\treturn {\n\t\ttype: \"session_info\",\n\t\t...entryBase(byId, parentId),\n\t\tname: name.trim(),\n\t};\n}\n\nexport function getLatestSessionName(entries: SessionEntry[]): string | undefined {\n\t// Walk entries in reverse to find the latest session_info entry.\n\t// Empty names explicitly clear the session title.\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"session_info\") {\n\t\t\treturn entry.name?.trim() || undefined;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport function createCustomMessageEntry<T = unknown>(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: T | undefined,\n\texcludeFromContext: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n): CustomMessageEntry<T> {\n\treturn {\n\t\ttype: \"custom_message\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\t...(excludeFromContext === true ? { excludeFromContext: true } : {}),\n\t\t...entryBase(byId, parentId),\n\t};\n}\n\nexport function createLabelEntry(\n\ttargetId: string,\n\tlabel: string | undefined,\n\tbyId: { has(id: string): boolean },\n\tparentId: string | null,\n\ttimestamp?: string,\n): LabelEntry {\n\treturn {\n\t\ttype: \"label\",\n\t\t...entryBase(byId, parentId),\n\t\t...(timestamp !== undefined ? { timestamp } : {}),\n\t\ttargetId,\n\t\tlabel,\n\t};\n}\n\nexport function createBranchSummaryEntry(\n\tbranchFromId: string | null,\n\tsummary: string,\n\tdetails: unknown,\n\tfromHook: boolean | undefined,\n\tbyId: { has(id: string): boolean },\n): BranchSummaryEntry {\n\treturn {\n\t\ttype: \"branch_summary\",\n\t\t...entryBase(byId, branchFromId),\n\t\tfromId: branchFromId ?? \"root\",\n\t\tsummary,\n\t\tdetails,\n\t\tfromHook,\n\t};\n}\n\nexport function getEntriesWithoutHeader(fileEntries: FileEntry[]): SessionEntry[] {\n\treturn fileEntries.filter((entry): entry is SessionEntry => entry.type !== \"session\");\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"session-manager-history.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-history.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,sBAAsB,EACtB,sBAAsB,EACtB,SAAS,EACT,cAAc,EACd,YAAY,EAEZ,eAAe,EACf,MAAM,4BAA4B,CAAC;AAEpC,wBAAgB,gCA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import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport { createBranchSummaryMessage, createCustomMessage } from \"./messages.ts\";\nimport { contentArrayHasAssistantThinkingBlock } from \"./thinking-blocks.ts\";\nimport type {\n\tContextCompactionEntry,\n\tContextDeletionFilters,\n\tFileEntry,\n\tSessionContext,\n\tSessionEntry,\n\tSessionMessageEntry,\n\tSessionTreeNode,\n} from \"./session-manager-types.ts\";\n\nexport function getLatestCompactionBoundaryEntry(entries: SessionEntry[]): ContextCompactionEntry | null {\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"context_compaction\") {\n\t\t\treturn entry;\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Build raw deletion filters from persisted context_compaction entries.\n *\n * These raw filters do not apply replay-safety repair for latest assistant\n * thinking/redacted_thinking blocks or their paired tool results. Production\n * context rebuild paths should prefer `buildEffectiveContextDeletionFilters`\n * or `buildContextDeletionFilteredPath(path)` unless they intentionally need\n * the un-repaired historical deletion plan for diagnostics.\n */\nexport function buildContextDeletionFilters(path: SessionEntry[]): ContextDeletionFilters {\n\tconst deletedEntryIds = new Set<string>();\n\tconst deletedContentBlocks = new Map<string, Set<number>>();\n\n\tfor (const entry of path) {\n\t\tif (entry.type !== \"context_compaction\") continue;\n\t\tfor (const target of entry.deletedTargets) {\n\t\t\tif (target.kind === \"entry\") {\n\t\t\t\tdeletedEntryIds.add(target.entryId);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst existing = deletedContentBlocks.get(target.entryId) ?? new Set<number>();\n\t\t\texisting.add(target.blockIndex);\n\t\t\tdeletedContentBlocks.set(target.entryId, existing);\n\t\t}\n\t}\n\n\treturn { deletedEntryIds, deletedContentBlocks };\n}\n\nfunction getToolCallContentBlockId(block: unknown): string | undefined {\n\tif (!block || typeof block !== \"object\") return undefined;\n\tconst candidate = block as { type?: unknown; id?: unknown };\n\treturn candidate.type === \"toolCall\" && typeof candidate.id === \"string\" ? candidate.id : undefined;\n}\n\nfunction getToolResultCallId(message: AgentMessage): string | undefined {\n\tif (message.role !== \"toolResult\") return undefined;\n\tconst toolCallId = (message as { toolCallId?: unknown }).toolCallId;\n\treturn typeof toolCallId === \"string\" ? toolCallId : undefined;\n}\n\nfunction collectToolCallContentBlockIds(content: readonly unknown[]): Set<string> {\n\tconst toolCallIds = new Set<string>();\n\tfor (const block of content) {\n\t\tconst toolCallId = getToolCallContentBlockId(block);\n\t\tif (toolCallId) toolCallIds.add(toolCallId);\n\t}\n\treturn toolCallIds;\n}\n\nfunction addDeletionTarget(filters: ContextDeletionFilters, target: ContextCompactionEntry[\"deletedTargets\"][number]): void {\n\tif (target.kind === \"entry\") {\n\t\tfilters.deletedEntryIds.add(target.entryId);\n\t\treturn;\n\t}\n\tconst existing = filters.deletedContentBlocks.get(target.entryId) ?? new Set<number>();\n\texisting.add(target.blockIndex);\n\tfilters.deletedContentBlocks.set(target.entryId, existing);\n}\n\nfunction buildToolResultEntryIdsByCallId(path: SessionEntry[]): Map<string, Set<string>> {\n\tconst toolResultEntryIdsByCallId = new Map<string, Set<string>>();\n\tfor (const entry of path) {\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst toolCallId = getToolResultCallId(entry.message);\n\t\tif (!toolCallId) continue;\n\t\tconst existing = toolResultEntryIdsByCallId.get(toolCallId) ?? new Set<string>();\n\t\texisting.add(entry.id);\n\t\ttoolResultEntryIdsByCallId.set(toolCallId, existing);\n\t}\n\treturn toolResultEntryIdsByCallId;\n}\n\nfunction findRetainedThinkingAssistants(\n\tpath: SessionEntry[],\n\tdeletedEntryIds: ReadonlySet<string>,\n): SessionMessageEntry[] {\n\treturn path.filter((entry): entry is SessionMessageEntry => {\n\t\tif (entry.type !== \"message\") return false;\n\t\tif (deletedEntryIds.has(entry.id)) return false;\n\t\tif (entry.message.role !== \"assistant\") return false;\n\t\treturn contentArrayHasAssistantThinkingBlock(entry.message.content);\n\t});\n}\n\nexport function buildEffectiveContextDeletionFilters(path: SessionEntry[]): ContextDeletionFilters {\n\tconst filters = buildContextDeletionFilters(path);\n\tif (!path.some((entry) => entry.type === \"context_compaction\")) return filters;\n\n\tconst rawDeletedEntryIds = new Set<string>();\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"entry\") rawDeletedEntryIds.add(target.entryId);\n\t\t}\n\t}\n\tconst retainedThinkingAssistants = findRetainedThinkingAssistants(path, rawDeletedEntryIds);\n\tconst retainedThinkingAssistantIds = new Set(retainedThinkingAssistants.map((entry) => entry.id));\n\tconst retainedThinkingAssistantById = new Map(retainedThinkingAssistants.map((entry) => [entry.id, entry]));\n\tconst toolResultEntryIdsByCallId = buildToolResultEntryIdsByCallId(path);\n\tconst effectiveFilters: ContextDeletionFilters = {\n\t\tdeletedEntryIds: new Set<string>(),\n\t\tdeletedContentBlocks: new Map<string, Set<number>>(),\n\t};\n\tconst allRestoredToolResultEntryIds = new Set<string>();\n\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind !== \"content_block\") continue;\n\t\t\tconst retainedThinkingAssistant = retainedThinkingAssistantById.get(target.entryId);\n\t\t\tif (!retainedThinkingAssistant) continue;\n\t\t\tconst content = (retainedThinkingAssistant.message as { content: readonly unknown[] }).content;\n\t\t\tfor (const toolCallId of collectToolCallContentBlockIds(content)) {\n\t\t\t\tfor (const entryId of toolResultEntryIdsByCallId.get(toolCallId) ?? []) {\n\t\t\t\t\tallRestoredToolResultEntryIds.add(entryId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tlet restoresRetainedThinkingAssistant = false;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"content_block\" && retainedThinkingAssistantIds.has(target.entryId)) {\n\t\t\t\trestoresRetainedThinkingAssistant = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"content_block\" && retainedThinkingAssistantIds.has(target.entryId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// When a stale persisted plan tried to partially filter a retained\n\t\t\t// thinking-bearing assistant, treat the same compaction entry as one\n\t\t\t// unsafe unit and restore its paired tool results. Later compaction\n\t\t\t// entries may still trim those restored multi-block results normally,\n\t\t\t// but whole-entry deletion of those paired results remains unsafe in any\n\t\t\t// later compaction because the assistant tool call is retained.\n\t\t\tif (restoresRetainedThinkingAssistant && allRestoredToolResultEntryIds.has(target.entryId)) continue;\n\t\t\tif (target.kind === \"entry\" && allRestoredToolResultEntryIds.has(target.entryId)) continue;\n\t\t\taddDeletionTarget(effectiveFilters, target);\n\t\t}\n\t}\n\n\treturn effectiveFilters;\n}\n\nfunction filterContentArray<T>(content: T[], deletedBlocks: ReadonlySet<number>): T[] {\n\treturn content.filter((_, index) => !deletedBlocks.has(index));\n}\n\nfunction filterMessageContentBlocks(\n\tmessage: AgentMessage,\n\tdeletedBlocks: ReadonlySet<number> | undefined,\n): AgentMessage | undefined {\n\tif (!deletedBlocks || deletedBlocks.size === 0) return message;\n\n\tswitch (message.role) {\n\t\tcase \"user\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"assistant\": {\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"toolResult\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"custom\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"bashExecution\":\n\t\tcase \"branchSummary\":\n\t\t\treturn message;\n\t}\n}\n\n/**\n * Return the active branch path after applying logical context-deletion entries.\n * Whole-entry deletions remove the entry from the path. Content-block deletions\n * clone only affected message/custom-message entries so retained blocks stay verbatim.\n * The optional filters parameter is for callers that already computed effective\n * filters with `buildEffectiveContextDeletionFilters(path)` and want to avoid\n * repeating the repair pass.\n */\nexport function buildContextDeletionFilteredPath(\n\tpath: SessionEntry[],\n\teffectiveFilters: ContextDeletionFilters = buildEffectiveContextDeletionFilters(path),\n): SessionEntry[] {\n\tconst filteredPath: SessionEntry[] = [];\n\n\tfor (const entry of path) {\n\t\tif (effectiveFilters.deletedEntryIds.has(entry.id)) continue;\n\n\t\tconst deletedBlocks = effectiveFilters.deletedContentBlocks.get(entry.id);\n\t\tif (!deletedBlocks || deletedBlocks.size === 0) {\n\t\t\tfilteredPath.push(entry);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"message\") {\n\t\t\tconst message = filterMessageContentBlocks(entry.message, deletedBlocks);\n\t\t\tif (message) filteredPath.push({ ...entry, message });\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"custom_message\" && Array.isArray(entry.content)) {\n\t\t\tconst content = filterContentArray(entry.content, deletedBlocks);\n\t\t\tif (content.length > 0) filteredPath.push({ ...entry, content });\n\t\t\tcontinue;\n\t\t}\n\n\t\tfilteredPath.push(entry);\n\t}\n\n\treturn filteredPath;\n}\n\n/**\n * Build the session context from entries using tree traversal.\n * If leafId is provided, walks from that entry to root.\n * Applies context-deletion filtering and includes branch summaries along the path.\n */\nexport function buildSessionContext(\n\tentries: SessionEntry[],\n\tleafId?: string | null,\n\tbyId?: Map<string, SessionEntry>,\n): SessionContext {\n\t// Build uuid index if not available\n\tif (!byId) {\n\t\tbyId = new Map<string, SessionEntry>();\n\t\tfor (const entry of entries) {\n\t\t\tbyId.set(entry.id, entry);\n\t\t}\n\t}\n\n\t// Find leaf\n\tlet leaf: SessionEntry | undefined;\n\tif (leafId === null) {\n\t\t// Explicitly null - return no messages (navigated to before first entry)\n\t\treturn { messages: [], thinkingLevel: \"off\", contextWindow: undefined, model: null };\n\t}\n\tif (leafId) {\n\t\tleaf = byId.get(leafId);\n\t}\n\tif (!leaf) {\n\t\t// Fallback to last entry (when leafId is undefined)\n\t\tleaf = entries[entries.length - 1];\n\t}\n\n\tif (!leaf) {\n\t\treturn { messages: [], thinkingLevel: \"off\", contextWindow: undefined, model: null };\n\t}\n\n\t// Walk from leaf to root, collecting path\n\tconst path = getBranchPath(leaf.id, byId);\n\n\t// Extract settings\n\tlet thinkingLevel = \"off\";\n\tlet contextWindow: number | undefined;\n\tlet model: { provider: string; modelId: string } | null = null;\n\n\tfor (const entry of path) {\n\t\tif (entry.type === \"thinking_level_change\") {\n\t\t\tthinkingLevel = entry.thinkingLevel;\n\t\t} else if (entry.type === \"context_window_change\") {\n\t\t\tcontextWindow = entry.contextWindow;\n\t\t} else if (entry.type === \"model_change\") {\n\t\t\tmodel = { provider: entry.provider, modelId: entry.modelId };\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tmodel = { provider: entry.message.provider, modelId: entry.message.model };\n\t\t}\n\t}\n\n\tconst filteredPath = buildContextDeletionFilteredPath(path);\n\n\t// Build active context messages from the filtered path. Legacy \"compaction\"\n\t// entries are archival metadata and intentionally inert here.\n\tconst messages: AgentMessage[] = [];\n\n\tconst appendMessage = (entry: SessionEntry) => {\n\t\tlet message: AgentMessage | undefined;\n\t\tif (entry.type === \"message\") {\n\t\t\tmessage = entry.message;\n\t\t} else if (entry.type === \"custom_message\") {\n\t\t\tmessage = createCustomMessage(\n\t\t\t\tentry.customType,\n\t\t\t\tentry.content,\n\t\t\t\tentry.display,\n\t\t\t\tentry.details,\n\t\t\t\tentry.timestamp,\n\t\t\t\tentry.excludeFromContext,\n\t\t\t);\n\t\t} else if (entry.type === \"branch_summary\" && entry.summary) {\n\t\t\tmessage = createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t\t}\n\n\t\tif (message) messages.push(message);\n\t};\n\n\tfor (const entry of filteredPath) {\n\t\tappendMessage(entry);\n\t}\n\n\treturn { messages, thinkingLevel, contextWindow, model };\n}\n\nexport interface SessionIndex {\n\tbyId: Map<string, SessionEntry>;\n\tlabelsById: Map<string, string>;\n\tlabelTimestampsById: Map<string, string>;\n\tleafId: string | null;\n}\n\nexport function buildSessionIndex(fileEntries: FileEntry[]): SessionIndex {\n\tconst byId = new Map<string, SessionEntry>();\n\tconst labelsById = new Map<string, string>();\n\tconst labelTimestampsById = new Map<string, string>();\n\tlet leafId: string | null = null;\n\n\tfor (const entry of fileEntries) {\n\t\tif (entry.type === \"session\") continue;\n\t\tbyId.set(entry.id, entry);\n\t\tleafId = entry.id;\n\t\tif (entry.type === \"label\") {\n\t\t\tif (entry.label) {\n\t\t\t\tlabelsById.set(entry.targetId, entry.label);\n\t\t\t\tlabelTimestampsById.set(entry.targetId, entry.timestamp);\n\t\t\t} else {\n\t\t\t\tlabelsById.delete(entry.targetId);\n\t\t\t\tlabelTimestampsById.delete(entry.targetId);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { byId, labelsById, labelTimestampsById, leafId };\n}\n\nexport function getBranchPath(fromId: string | null | undefined, byId: Map<string, SessionEntry>): SessionEntry[] {\n\tconst path: SessionEntry[] = [];\n\tlet current = fromId ? byId.get(fromId) : undefined;\n\twhile (current) {\n\t\tpath.push(current);\n\t\tcurrent = current.parentId ? byId.get(current.parentId) : undefined;\n\t}\n\tpath.reverse();\n\treturn path;\n}\n\nexport function buildSessionTree(\n\tentries: SessionEntry[],\n\tlabelsById: ReadonlyMap<string, string>,\n\tlabelTimestampsById: ReadonlyMap<string, string>,\n): SessionTreeNode[] {\n\tconst nodeMap = new Map<string, SessionTreeNode>();\n\tconst roots: SessionTreeNode[] = [];\n\n\t// Create nodes with resolved labels\n\tfor (const entry of entries) {\n\t\tconst label = labelsById.get(entry.id);\n\t\tconst labelTimestamp = labelTimestampsById.get(entry.id);\n\t\tnodeMap.set(entry.id, { entry, children: [], label, labelTimestamp });\n\t}\n\n\t// Build tree\n\tfor (const entry of entries) {\n\t\tconst node = nodeMap.get(entry.id)!;\n\t\tif (entry.parentId === null || entry.parentId === entry.id) {\n\t\t\troots.push(node);\n\t\t} else {\n\t\t\tconst parent = nodeMap.get(entry.parentId);\n\t\t\tif (parent) {\n\t\t\t\tparent.children.push(node);\n\t\t\t} else {\n\t\t\t\t// Orphan - treat as root\n\t\t\t\troots.push(node);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Sort children by timestamp (oldest first, newest at bottom)\n\t// Use iterative approach to avoid stack overflow on deep trees\n\tconst stack: SessionTreeNode[] = [...roots];\n\twhile (stack.length > 0) {\n\t\tconst node = stack.pop()!;\n\t\tnode.children.sort((a, b) => new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime());\n\t\tstack.push(...node.children);\n\t}\n\n\treturn roots;\n}\n"]}
1
+ {"version":3,"file":"session-manager-history.d.ts","sourceRoot":"","sources":["../../src/core/session-manager-history.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,sBAAsB,EACtB,sBAAsB,EACtB,SAAS,EACT,cAAc,EACd,YAAY,EAEZ,eAAe,EACf,MAAM,4BAA4B,CAAC;AAEpC,wBAAgB,gCAAgC,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,sBAAsB,GAAG,IAAI,CAQvG;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG,sBAAsB,CAkBxF;AA0DD,wBAAgB,oCAAoC,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG,sBAAsB,CA+DjG;AA0CD;;;;;;;GAOG;AACH,wBAAgB,gCAAgC,CAC/C,IAAI,EAAE,YAAY,EAAE,EACpB,gBAAgB,GAAE,sBAAmE,GACnF,YAAY,EAAE,CA4BhB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,YAAY,EAAE,EACvB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,EACtB,IAAI,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,GAC9B,cAAc,CA8EhB;AAED,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAChC,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,SAAS,EAAE,GAAG,YAAY,CAsBxE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,YAAY,EAAE,CAShH;AAED,wBAAgB,gBAAgB,CAC/B,OAAO,EAAE,YAAY,EAAE,EACvB,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EACvC,mBAAmB,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9C,eAAe,EAAE,CAqCnB","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport { createBranchSummaryMessage, createCustomMessage } from \"./messages.ts\";\nimport { contentArrayHasAssistantThinkingBlock } from \"./thinking-blocks.ts\";\nimport { reconcilePersistedToolDependencyFilters } from \"./session-manager-tool-dependencies.ts\";\nimport type {\n\tContextCompactionEntry,\n\tContextDeletionFilters,\n\tFileEntry,\n\tSessionContext,\n\tSessionEntry,\n\tSessionMessageEntry,\n\tSessionTreeNode,\n} from \"./session-manager-types.ts\";\n\nexport function getLatestCompactionBoundaryEntry(entries: SessionEntry[]): ContextCompactionEntry | null {\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"context_compaction\") {\n\t\t\treturn entry;\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Build raw deletion filters from persisted context_compaction entries.\n *\n * These raw filters do not apply replay-safety repair for latest assistant\n * thinking/redacted_thinking blocks or their paired tool results. Production\n * context rebuild paths should prefer `buildEffectiveContextDeletionFilters`\n * or `buildContextDeletionFilteredPath(path)` unless they intentionally need\n * the un-repaired historical deletion plan for diagnostics.\n */\nexport function buildContextDeletionFilters(path: SessionEntry[]): ContextDeletionFilters {\n\tconst deletedEntryIds = new Set<string>();\n\tconst deletedContentBlocks = new Map<string, Set<number>>();\n\n\tfor (const entry of path) {\n\t\tif (entry.type !== \"context_compaction\") continue;\n\t\tfor (const target of entry.deletedTargets) {\n\t\t\tif (target.kind === \"entry\") {\n\t\t\t\tdeletedEntryIds.add(target.entryId);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst existing = deletedContentBlocks.get(target.entryId) ?? new Set<number>();\n\t\t\texisting.add(target.blockIndex);\n\t\t\tdeletedContentBlocks.set(target.entryId, existing);\n\t\t}\n\t}\n\n\treturn { deletedEntryIds, deletedContentBlocks };\n}\n\nfunction getToolCallContentBlockId(block: unknown): string | undefined {\n\tif (!block || typeof block !== \"object\") return undefined;\n\tconst candidate = block as { type?: unknown; id?: unknown };\n\treturn candidate.type === \"toolCall\" && typeof candidate.id === \"string\" ? candidate.id : undefined;\n}\n\nfunction getToolResultCallId(message: AgentMessage): string | undefined {\n\tif (message.role !== \"toolResult\") return undefined;\n\tconst toolCallId = (message as { toolCallId?: unknown }).toolCallId;\n\treturn typeof toolCallId === \"string\" ? toolCallId : undefined;\n}\n\nfunction collectToolCallContentBlockIds(content: readonly unknown[]): Set<string> {\n\tconst toolCallIds = new Set<string>();\n\tfor (const block of content) {\n\t\tconst toolCallId = getToolCallContentBlockId(block);\n\t\tif (toolCallId) toolCallIds.add(toolCallId);\n\t}\n\treturn toolCallIds;\n}\n\nfunction addDeletionTarget(filters: ContextDeletionFilters, target: ContextCompactionEntry[\"deletedTargets\"][number]): void {\n\tif (target.kind === \"entry\") {\n\t\tfilters.deletedEntryIds.add(target.entryId);\n\t\treturn;\n\t}\n\tconst existing = filters.deletedContentBlocks.get(target.entryId) ?? new Set<number>();\n\texisting.add(target.blockIndex);\n\tfilters.deletedContentBlocks.set(target.entryId, existing);\n}\n\nfunction buildToolResultEntryIdsByCallId(path: SessionEntry[]): Map<string, Set<string>> {\n\tconst toolResultEntryIdsByCallId = new Map<string, Set<string>>();\n\tfor (const entry of path) {\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst toolCallId = getToolResultCallId(entry.message);\n\t\tif (!toolCallId) continue;\n\t\tconst existing = toolResultEntryIdsByCallId.get(toolCallId) ?? new Set<string>();\n\t\texisting.add(entry.id);\n\t\ttoolResultEntryIdsByCallId.set(toolCallId, existing);\n\t}\n\treturn toolResultEntryIdsByCallId;\n}\n\nfunction findRetainedThinkingAssistants(\n\tpath: SessionEntry[],\n\tdeletedEntryIds: ReadonlySet<string>,\n): SessionMessageEntry[] {\n\treturn path.filter((entry): entry is SessionMessageEntry => {\n\t\tif (entry.type !== \"message\") return false;\n\t\tif (deletedEntryIds.has(entry.id)) return false;\n\t\tif (entry.message.role !== \"assistant\") return false;\n\t\treturn contentArrayHasAssistantThinkingBlock(entry.message.content);\n\t});\n}\n\nexport function buildEffectiveContextDeletionFilters(path: SessionEntry[]): ContextDeletionFilters {\n\tconst filters = buildContextDeletionFilters(path);\n\tif (!path.some((entry) => entry.type === \"context_compaction\")) return filters;\n\n\tconst rawDeletedEntryIds = new Set<string>();\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"entry\") rawDeletedEntryIds.add(target.entryId);\n\t\t}\n\t}\n\tconst retainedThinkingAssistants = findRetainedThinkingAssistants(path, rawDeletedEntryIds);\n\tconst retainedThinkingAssistantIds = new Set(retainedThinkingAssistants.map((entry) => entry.id));\n\tconst retainedThinkingAssistantById = new Map(retainedThinkingAssistants.map((entry) => [entry.id, entry]));\n\tconst toolResultEntryIdsByCallId = buildToolResultEntryIdsByCallId(path);\n\tconst effectiveFilters: ContextDeletionFilters = {\n\t\tdeletedEntryIds: new Set<string>(),\n\t\tdeletedContentBlocks: new Map<string, Set<number>>(),\n\t};\n\tconst allRestoredToolResultEntryIds = new Set<string>();\n\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind !== \"content_block\") continue;\n\t\t\tconst retainedThinkingAssistant = retainedThinkingAssistantById.get(target.entryId);\n\t\t\tif (!retainedThinkingAssistant) continue;\n\t\t\tconst content = (retainedThinkingAssistant.message as { content: readonly unknown[] }).content;\n\t\t\tfor (const toolCallId of collectToolCallContentBlockIds(content)) {\n\t\t\t\tfor (const entryId of toolResultEntryIdsByCallId.get(toolCallId) ?? []) {\n\t\t\t\t\tallRestoredToolResultEntryIds.add(entryId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (const compaction of path) {\n\t\tif (compaction.type !== \"context_compaction\") continue;\n\t\tlet restoresRetainedThinkingAssistant = false;\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"content_block\" && retainedThinkingAssistantIds.has(target.entryId)) {\n\t\t\t\trestoresRetainedThinkingAssistant = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tfor (const target of compaction.deletedTargets) {\n\t\t\tif (target.kind === \"content_block\" && retainedThinkingAssistantIds.has(target.entryId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// When a stale persisted plan tried to partially filter a retained\n\t\t\t// thinking-bearing assistant, treat the same compaction entry as one\n\t\t\t// unsafe unit and restore its paired tool results. Later compaction\n\t\t\t// entries may still trim those restored multi-block results normally,\n\t\t\t// but whole-entry deletion of those paired results remains unsafe in any\n\t\t\t// later compaction because the assistant tool call is retained.\n\t\t\tif (restoresRetainedThinkingAssistant && allRestoredToolResultEntryIds.has(target.entryId)) continue;\n\t\t\tif (target.kind === \"entry\" && allRestoredToolResultEntryIds.has(target.entryId)) continue;\n\t\t\taddDeletionTarget(effectiveFilters, target);\n\t\t}\n\t}\n\n\treturn reconcilePersistedToolDependencyFilters(path, effectiveFilters);\n}\n\nfunction filterContentArray<T>(content: T[], deletedBlocks: ReadonlySet<number>): T[] {\n\treturn content.filter((_, index) => !deletedBlocks.has(index));\n}\n\nfunction filterMessageContentBlocks(\n\tmessage: AgentMessage,\n\tdeletedBlocks: ReadonlySet<number> | undefined,\n): AgentMessage | undefined {\n\tif (!deletedBlocks || deletedBlocks.size === 0) return message;\n\n\tswitch (message.role) {\n\t\tcase \"user\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"assistant\": {\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"toolResult\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"custom\": {\n\t\t\tif (!Array.isArray(message.content)) return message;\n\t\t\tconst content = filterContentArray(message.content, deletedBlocks);\n\t\t\tif (content.length === 0) return undefined;\n\t\t\treturn { ...message, content };\n\t\t}\n\t\tcase \"bashExecution\":\n\t\tcase \"branchSummary\":\n\t\t\treturn message;\n\t}\n}\n\n/**\n * Return the active branch path after applying logical context-deletion entries.\n * Whole-entry deletions remove the entry from the path. Content-block deletions\n * clone only affected message/custom-message entries so retained blocks stay verbatim.\n * The optional filters parameter is for callers that already computed effective\n * filters with `buildEffectiveContextDeletionFilters(path)` and want to avoid\n * repeating the repair pass.\n */\nexport function buildContextDeletionFilteredPath(\n\tpath: SessionEntry[],\n\teffectiveFilters: ContextDeletionFilters = buildEffectiveContextDeletionFilters(path),\n): SessionEntry[] {\n\tconst filteredPath: SessionEntry[] = [];\n\n\tfor (const entry of path) {\n\t\tif (effectiveFilters.deletedEntryIds.has(entry.id)) continue;\n\n\t\tconst deletedBlocks = effectiveFilters.deletedContentBlocks.get(entry.id);\n\t\tif (!deletedBlocks || deletedBlocks.size === 0) {\n\t\t\tfilteredPath.push(entry);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"message\") {\n\t\t\tconst message = filterMessageContentBlocks(entry.message, deletedBlocks);\n\t\t\tif (message) filteredPath.push({ ...entry, message });\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (entry.type === \"custom_message\" && Array.isArray(entry.content)) {\n\t\t\tconst content = filterContentArray(entry.content, deletedBlocks);\n\t\t\tif (content.length > 0) filteredPath.push({ ...entry, content });\n\t\t\tcontinue;\n\t\t}\n\n\t\tfilteredPath.push(entry);\n\t}\n\n\treturn filteredPath;\n}\n\n/**\n * Build the session context from entries using tree traversal.\n * If leafId is provided, walks from that entry to root.\n * Applies context-deletion filtering and includes branch summaries along the path.\n */\nexport function buildSessionContext(\n\tentries: SessionEntry[],\n\tleafId?: string | null,\n\tbyId?: Map<string, SessionEntry>,\n): SessionContext {\n\t// Build uuid index if not available\n\tif (!byId) {\n\t\tbyId = new Map<string, SessionEntry>();\n\t\tfor (const entry of entries) {\n\t\t\tbyId.set(entry.id, entry);\n\t\t}\n\t}\n\n\t// Find leaf\n\tlet leaf: SessionEntry | undefined;\n\tif (leafId === null) {\n\t\t// Explicitly null - return no messages (navigated to before first entry)\n\t\treturn { messages: [], thinkingLevel: \"off\", contextWindow: undefined, model: null };\n\t}\n\tif (leafId) {\n\t\tleaf = byId.get(leafId);\n\t}\n\tif (!leaf) {\n\t\t// Fallback to last entry (when leafId is undefined)\n\t\tleaf = entries[entries.length - 1];\n\t}\n\n\tif (!leaf) {\n\t\treturn { messages: [], thinkingLevel: \"off\", contextWindow: undefined, model: null };\n\t}\n\n\t// Walk from leaf to root, collecting path\n\tconst path = getBranchPath(leaf.id, byId);\n\n\t// Extract settings\n\tlet thinkingLevel = \"off\";\n\tlet contextWindow: number | undefined;\n\tlet model: { provider: string; modelId: string } | null = null;\n\n\tfor (const entry of path) {\n\t\tif (entry.type === \"thinking_level_change\") {\n\t\t\tthinkingLevel = entry.thinkingLevel;\n\t\t} else if (entry.type === \"context_window_change\") {\n\t\t\tcontextWindow = entry.contextWindow;\n\t\t} else if (entry.type === \"model_change\") {\n\t\t\tmodel = { provider: entry.provider, modelId: entry.modelId };\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tmodel = { provider: entry.message.provider, modelId: entry.message.model };\n\t\t}\n\t}\n\n\tconst filteredPath = buildContextDeletionFilteredPath(path);\n\n\t// Build active context messages from the filtered path. Legacy \"compaction\"\n\t// entries are archival metadata and intentionally inert here.\n\tconst messages: AgentMessage[] = [];\n\n\tconst appendMessage = (entry: SessionEntry) => {\n\t\tlet message: AgentMessage | undefined;\n\t\tif (entry.type === \"message\") {\n\t\t\tmessage = entry.message;\n\t\t} else if (entry.type === \"custom_message\") {\n\t\t\tmessage = createCustomMessage(\n\t\t\t\tentry.customType,\n\t\t\t\tentry.content,\n\t\t\t\tentry.display,\n\t\t\t\tentry.details,\n\t\t\t\tentry.timestamp,\n\t\t\t\tentry.excludeFromContext,\n\t\t\t);\n\t\t} else if (entry.type === \"branch_summary\" && entry.summary) {\n\t\t\tmessage = createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t\t}\n\n\t\tif (message) messages.push(message);\n\t};\n\n\tfor (const entry of filteredPath) {\n\t\tappendMessage(entry);\n\t}\n\n\treturn { messages, thinkingLevel, contextWindow, model };\n}\n\nexport interface SessionIndex {\n\tbyId: Map<string, SessionEntry>;\n\tlabelsById: Map<string, string>;\n\tlabelTimestampsById: Map<string, string>;\n\tleafId: string | null;\n}\n\nexport function buildSessionIndex(fileEntries: FileEntry[]): SessionIndex {\n\tconst byId = new Map<string, SessionEntry>();\n\tconst labelsById = new Map<string, string>();\n\tconst labelTimestampsById = new Map<string, string>();\n\tlet leafId: string | null = null;\n\n\tfor (const entry of fileEntries) {\n\t\tif (entry.type === \"session\") continue;\n\t\tbyId.set(entry.id, entry);\n\t\tleafId = entry.id;\n\t\tif (entry.type === \"label\") {\n\t\t\tif (entry.label) {\n\t\t\t\tlabelsById.set(entry.targetId, entry.label);\n\t\t\t\tlabelTimestampsById.set(entry.targetId, entry.timestamp);\n\t\t\t} else {\n\t\t\t\tlabelsById.delete(entry.targetId);\n\t\t\t\tlabelTimestampsById.delete(entry.targetId);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { byId, labelsById, labelTimestampsById, leafId };\n}\n\nexport function getBranchPath(fromId: string | null | undefined, byId: Map<string, SessionEntry>): SessionEntry[] {\n\tconst path: SessionEntry[] = [];\n\tlet current = fromId ? byId.get(fromId) : undefined;\n\twhile (current) {\n\t\tpath.push(current);\n\t\tcurrent = current.parentId ? byId.get(current.parentId) : undefined;\n\t}\n\tpath.reverse();\n\treturn path;\n}\n\nexport function buildSessionTree(\n\tentries: SessionEntry[],\n\tlabelsById: ReadonlyMap<string, string>,\n\tlabelTimestampsById: ReadonlyMap<string, string>,\n): SessionTreeNode[] {\n\tconst nodeMap = new Map<string, SessionTreeNode>();\n\tconst roots: SessionTreeNode[] = [];\n\n\t// Create nodes with resolved labels\n\tfor (const entry of entries) {\n\t\tconst label = labelsById.get(entry.id);\n\t\tconst labelTimestamp = labelTimestampsById.get(entry.id);\n\t\tnodeMap.set(entry.id, { entry, children: [], label, labelTimestamp });\n\t}\n\n\t// Build tree\n\tfor (const entry of entries) {\n\t\tconst node = nodeMap.get(entry.id)!;\n\t\tif (entry.parentId === null || entry.parentId === entry.id) {\n\t\t\troots.push(node);\n\t\t} else {\n\t\t\tconst parent = nodeMap.get(entry.parentId);\n\t\t\tif (parent) {\n\t\t\t\tparent.children.push(node);\n\t\t\t} else {\n\t\t\t\t// Orphan - treat as root\n\t\t\t\troots.push(node);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Sort children by timestamp (oldest first, newest at bottom)\n\t// Use iterative approach to avoid stack overflow on deep trees\n\tconst stack: SessionTreeNode[] = [...roots];\n\twhile (stack.length > 0) {\n\t\tconst node = stack.pop()!;\n\t\tnode.children.sort((a, b) => new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime());\n\t\tstack.push(...node.children);\n\t}\n\n\treturn roots;\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import { createBranchSummaryMessage, createCustomMessage } from "./messages.js";
2
2
  import { contentArrayHasAssistantThinkingBlock } from "./thinking-blocks.js";
3
+ import { reconcilePersistedToolDependencyFilters } from "./session-manager-tool-dependencies.js";
3
4
  export function getLatestCompactionBoundaryEntry(entries) {
4
5
  for (let i = entries.length - 1; i >= 0; i--) {
5
6
  const entry = entries[i];
@@ -157,7 +158,7 @@ export function buildEffectiveContextDeletionFilters(path) {
157
158
  addDeletionTarget(effectiveFilters, target);
158
159
  }
159
160
  }
160
- return effectiveFilters;
161
+ return reconcilePersistedToolDependencyFilters(path, effectiveFilters);
161
162
  }
162
163
  function filterContentArray(content, deletedBlocks) {
163
164
  return content.filter((_, index) => !deletedBlocks.has(index));