@cortex-agent/server 2026.9.15 → 2026.9.25
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.
- package/README.md +3 -2
- package/defaults/config/hooks/08-agents-md-injector-post-tool.json +10 -0
- package/defaults/config/hooks/{10-cortex-md-injector-session-start.json → 10-agents-md-injector-session-start.json} +3 -3
- package/defaults/config/hooks/{CORTEX.md → AGENTS.md} +2 -2
- package/defaults/config/thread-templates/agents/creative.json +17 -0
- package/defaults/hooks/{CORTEX.md → AGENTS.md} +1 -1
- package/defaults/hooks/{cortex-md-injector.mjs → agents-md-injector.mjs} +117 -26
- package/defaults/hooks/cortex-hook-api.mjs +2 -2
- package/defaults/hooks/new-session-hook.mjs +2 -2
- package/defaults/hooks/rules-loader.mjs +2 -2
- package/defaults/hooks/status-md-guard.mjs +8 -8
- package/defaults/hooks/task-status-check.mjs +18 -76
- package/defaults/hooks/tasks-yaml-guard.mjs +2 -2
- package/defaults/plugins/cortex-feishu/skills/feishu-doc/SKILL.md +1 -1
- package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +2 -2
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +4 -4
- package/defaults/plugins/cortex-system/skills/task/SKILL.md +1 -1
- package/defaults/prompts/systemPrompts/creative.md +13 -0
- package/defaults/prompts/systemPrompts/direct.md +1 -1
- package/defaults/rules/{cortex-md.md → agents-md.md} +4 -4
- package/defaults/rules/decision-records.md +1 -1
- package/defaults/rules/meta-conventions.md +4 -4
- package/defaults/rules/mission-md.md +1 -1
- package/dist/agent-adapter/claude/adapter.d.ts +12 -0
- package/dist/agent-adapter/claude/adapter.js +19 -1
- package/dist/agent-adapter/claude/hooks-builder.js +2 -2
- package/dist/agent-adapter/claude/spawn-args.d.ts +7 -0
- package/dist/agent-adapter/claude/spawn-args.js +14 -0
- package/dist/agent-adapter/claude/turn-machine.d.ts +11 -0
- package/dist/agent-adapter/claude/turn-machine.js +46 -5
- package/dist/agent-adapter/normalize/prompt-builder.d.ts +16 -1
- package/dist/agent-adapter/normalize/prompt-builder.js +24 -6
- package/dist/agent-adapter/pi/child-runner.d.ts +4 -2
- package/dist/agent-adapter/pi/child-runner.js +5 -1
- package/dist/agent-adapter/pi/event-parser.d.ts +3 -2
- package/dist/agent-adapter/pi/event-parser.js +46 -2
- package/dist/agent-adapter/pi/extensions.d.ts +7 -1
- package/dist/agent-adapter/pi/extensions.js +10 -3
- package/dist/agent-adapter/pi/mcp-bridge.js +8 -8
- package/dist/agent-adapter/pi/pi-session.js +5 -0
- package/dist/agent-adapter/pi/runtime.js +3 -0
- package/dist/agent-adapter/pi/session-options.js +5 -1
- package/dist/agent-adapter/pi/subagent.d.ts +15 -3
- package/dist/agent-adapter/pi/subagent.js +18 -0
- package/dist/agent-adapter/pi/tool-shims.d.ts +6 -1
- package/dist/agent-adapter/pi/tool-shims.js +34 -1
- package/dist/agent-adapter/types.d.ts +6 -0
- package/dist/core/agents/subagent/types.d.ts +3 -0
- package/dist/core/config-generator.js +17 -2
- package/dist/core/debug-mode.d.ts +11 -10
- package/dist/core/debug-mode.js +12 -18
- package/dist/core/exec-async.d.ts +26 -0
- package/dist/core/exec-async.js +47 -0
- package/dist/core/locales/en.d.ts +14 -1
- package/dist/core/locales/slices/commands.d.ts +12 -1
- package/dist/core/locales/slices/commands.js +24 -2
- package/dist/core/locales/slices/startup.d.ts +2 -0
- package/dist/core/locales/slices/startup.js +4 -0
- package/dist/core/mcp-tool-gate.js +1 -0
- package/dist/core/media-types.d.ts +10 -0
- package/dist/core/media-types.js +34 -0
- package/dist/core/platform-settings-spec.js +1 -1
- package/dist/core/rebuild-progress.d.ts +58 -0
- package/dist/core/rebuild-progress.js +103 -0
- package/dist/core/resilient-watch.js +5 -3
- package/dist/core/run-registry.d.ts +7 -0
- package/dist/core/run-registry.js +6 -0
- package/dist/core/settings-spec.d.ts +21 -0
- package/dist/core/settings-spec.js +28 -1
- package/dist/core/types/agent-types.d.ts +3 -1
- package/dist/core/types/thread-types.d.ts +23 -4
- package/dist/core/utils.d.ts +1 -1
- package/dist/core/utils.js +1 -1
- package/dist/domain/agents/agent-state.d.ts +7 -0
- package/dist/domain/agents/agent-state.js +9 -1
- package/dist/domain/agents/agent-switch.d.ts +38 -0
- package/dist/domain/agents/agent-switch.js +106 -0
- package/dist/domain/agents/config.d.ts +20 -2
- package/dist/domain/agents/config.js +58 -4
- package/dist/domain/agents/index.d.ts +1 -0
- package/dist/domain/agents/index.js +1 -0
- package/dist/domain/agents/profile-manager.d.ts +6 -0
- package/dist/domain/agents/profile-manager.js +14 -1
- package/dist/domain/executions/registry.d.ts +3 -2
- package/dist/domain/executions/registry.js +84 -10
- package/dist/domain/mcp/bundled-server.js +4 -3
- package/dist/domain/mcp/feishu/file.js +37 -18
- package/dist/domain/mcp/feishu/types.d.ts +3 -0
- package/dist/domain/mcp/feishu-server.js +1 -1
- package/dist/domain/mcp/tools/{cortex-md.d.ts → agents-md.d.ts} +6 -6
- package/dist/domain/mcp/tools/{cortex-md.js → agents-md.js} +6 -6
- package/dist/domain/mcp/tools/remote-file.d.ts +20 -0
- package/dist/domain/mcp/tools/remote-file.js +44 -0
- package/dist/domain/mcp/tools/slack.d.ts +2 -0
- package/dist/domain/mcp/tools/slack.js +22 -7
- package/dist/domain/mcp/tools/task-ops.js +5 -13
- package/dist/domain/mcp/tools/ui-file.js +23 -10
- package/dist/domain/mcp/tools/waitpoint.d.ts +3 -0
- package/dist/domain/mcp/tools/waitpoint.js +122 -0
- package/dist/domain/memory/agents-md-injector.d.ts +24 -0
- package/dist/domain/memory/{cortex-md-injector.js → agents-md-injector.js} +10 -7
- package/dist/domain/plugins/runtime.js +1 -1
- package/dist/domain/plugins/skill-projection.d.ts +8 -2
- package/dist/domain/plugins/skill-projection.js +76 -19
- package/dist/domain/projects/project-store.d.ts +1 -1
- package/dist/domain/projects/project-store.js +3 -3
- package/dist/domain/remote/client-bootstrap.js +1 -1
- package/dist/domain/remote/client-hot-reload.d.ts +1 -1
- package/dist/domain/remote/client-hot-reload.js +30 -24
- package/dist/domain/remote/client-manager.js +39 -119
- package/dist/domain/remote/cortex-client-config.d.ts +1 -1
- package/dist/domain/remote/cortex-client-config.js +1 -1
- package/dist/domain/remote/device-file.d.ts +51 -0
- package/dist/domain/remote/device-file.js +193 -0
- package/dist/domain/remote/device-port.js +3 -0
- package/dist/domain/remote/reverse-stream.d.ts +18 -7
- package/dist/domain/remote/reverse-stream.js +19 -6
- package/dist/domain/runs/engine-spec.js +4 -0
- package/dist/domain/runs/request.d.ts +6 -0
- package/dist/domain/runs/run.js +1 -0
- package/dist/domain/runs/spec-loader.d.ts +4 -3
- package/dist/domain/runs/spec-loader.js +4 -3
- package/dist/domain/scheduling/jobs/sync-public.js +6 -6
- package/dist/domain/scheduling/scheduler.js +14 -12
- package/dist/domain/sessions/session-lifecycle.d.ts +5 -0
- package/dist/domain/sessions/session-lifecycle.js +13 -1
- package/dist/domain/system/rebuild-hold.d.ts +44 -0
- package/dist/domain/system/rebuild-hold.js +91 -0
- package/dist/domain/system/server-update-check.d.ts +2 -1
- package/dist/domain/system/server-update-check.js +15 -13
- package/dist/domain/tasks/archiver.js +21 -13
- package/dist/domain/tasks/claim-recovery.js +2 -2
- package/dist/domain/tasks/dispatch-utils.d.ts +10 -1
- package/dist/domain/tasks/dispatch-utils.js +46 -1
- package/dist/domain/tasks/dispatcher.js +4 -5
- package/dist/domain/tasks/mutator.d.ts +2 -2
- package/dist/domain/tasks/mutator.js +62 -61
- package/dist/domain/tasks/system/task-cli.js +2 -2
- package/dist/domain/tasks/system/task-completion.d.ts +7 -2
- package/dist/domain/tasks/system/task-completion.js +69 -6
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +6 -1
- package/dist/domain/tasks/system/task-lifecycle-edit.js +21 -2
- package/dist/domain/tasks/system/task-lock.d.ts +23 -0
- package/dist/domain/tasks/system/task-lock.js +29 -1
- package/dist/domain/tasks/system/task-mutations.d.ts +14 -1
- package/dist/domain/tasks/system/task-mutations.js +25 -2
- package/dist/domain/tasks/system/task-state.d.ts +12 -1
- package/dist/domain/tasks/system/task-state.js +20 -3
- package/dist/domain/threads/prompt-builder.d.ts +9 -6
- package/dist/domain/threads/prompt-builder.js +28 -10
- package/dist/domain/threads/runner.js +10 -4
- package/dist/domain/threads/state-machine.js +15 -11
- package/dist/domain/threads/template-loader.js +6 -4
- package/dist/domain/threads/template-validate.d.ts +43 -6
- package/dist/domain/threads/template-validate.js +12 -0
- package/dist/domain/ui-service/app-router.d.ts +34 -3
- package/dist/domain/ui-service/app-router.js +9 -14
- package/dist/domain/ui-service/input-schemas.d.ts +28 -5
- package/dist/domain/ui-service/input-schemas.js +24 -9
- package/dist/domain/ui-service/mutate/platform-settings.js +1 -1
- package/dist/domain/ui-service/mutate/sessions.d.ts +6 -1
- package/dist/domain/ui-service/mutate/sessions.js +54 -0
- package/dist/domain/ui-service/mutate/tasks.js +4 -3
- package/dist/domain/ui-service/mutate/waitpoints.d.ts +2 -0
- package/dist/domain/ui-service/mutate/waitpoints.js +20 -0
- package/dist/domain/ui-service/platform-settings.js +1 -1
- package/dist/domain/ui-service/plugins-shared.js +1 -1
- package/dist/domain/ui-service/query/config.js +20 -2
- package/dist/domain/ui-service/query/executions.js +0 -2
- package/dist/domain/ui-service/query/machine-detail.js +3 -8
- package/dist/domain/ui-service/query/memory.js +29 -35
- package/dist/domain/ui-service/query/sessions.js +96 -12
- package/dist/domain/ui-service/query/system.js +19 -0
- package/dist/domain/ui-service/query/threads.js +1 -54
- package/dist/domain/ui-service/query/waitpoints.d.ts +30 -0
- package/dist/domain/ui-service/query/waitpoints.js +117 -0
- package/dist/domain/ui-service/query-input-schemas.d.ts +4 -0
- package/dist/domain/ui-service/query-input-schemas.js +5 -0
- package/dist/domain/ui-service/trpc.js +1 -1
- package/dist/domain/ui-service/types.d.ts +229 -51
- package/dist/domain/ui-service/types.js +1 -1
- package/dist/domain/ui-service/ui-service.js +8 -31
- package/dist/domain/waitpoints/notices.d.ts +21 -0
- package/dist/domain/waitpoints/notices.js +115 -0
- package/dist/domain/waitpoints/service.d.ts +91 -0
- package/dist/domain/waitpoints/service.js +243 -0
- package/dist/entry/app.js +52 -11
- package/dist/entry/auth-login-cli.js +1 -1
- package/dist/entry/auth-login-terminal.js +1 -1
- package/dist/entry/daemon-notice.d.ts +10 -1
- package/dist/entry/daemon-notice.js +16 -1
- package/dist/entry/daemon.d.ts +43 -2
- package/dist/entry/daemon.js +163 -9
- package/dist/entry/init-auth.js +1 -1
- package/dist/entry/init.js +6 -6
- package/dist/entry/signal-cli.d.ts +30 -0
- package/dist/entry/signal-cli.js +207 -0
- package/dist/entry/start-ui-http.d.ts +1 -1
- package/dist/entry/start-ui-http.js +2 -2
- package/dist/events/event-types.d.ts +0 -7
- package/dist/hooks/{cortex-md-injector.mjs → agents-md-injector.mjs} +117 -26
- package/dist/hooks/cortex-hook-api.mjs +2 -2
- package/dist/hooks/new-session-hook.mjs +2 -2
- package/dist/hooks/rules-loader.mjs +2 -2
- package/dist/hooks/status-md-guard.mjs +8 -8
- package/dist/hooks/task-status-check.mjs +18 -76
- package/dist/hooks/tasks-yaml-guard.mjs +2 -2
- package/dist/orchestration/agent-file-send.d.ts +9 -0
- package/dist/orchestration/agent-file-send.js +21 -2
- package/dist/orchestration/agent-runner.d.ts +3 -2
- package/dist/orchestration/agent-runner.js +33 -12
- package/dist/orchestration/attachments-store.d.ts +31 -0
- package/dist/orchestration/attachments-store.js +184 -0
- package/dist/orchestration/conversation-request.d.ts +37 -0
- package/dist/orchestration/conversation-request.js +91 -22
- package/dist/orchestration/interactions/plan-approvals.js +1 -1
- package/dist/orchestration/mid-turn-inject.d.ts +3 -0
- package/dist/orchestration/mid-turn-inject.js +9 -4
- package/dist/orchestration/outputs-store.d.ts +11 -0
- package/dist/orchestration/outputs-store.js +32 -6
- package/dist/orchestration/resume-dispatcher.js +4 -2
- package/dist/orchestration/routing/commands/mode.js +114 -63
- package/dist/orchestration/routing/file-handler.d.ts +23 -2
- package/dist/orchestration/routing/file-handler.js +22 -5
- package/dist/orchestration/routing/webhook.js +193 -39
- package/dist/orchestration/session-gateway.d.ts +1 -1
- package/dist/orchestration/session-gateway.js +1 -0
- package/dist/orchestration/thread-executor.d.ts +2 -0
- package/dist/orchestration/thread-executor.js +24 -1
- package/dist/orchestration/thread-input.d.ts +3 -2
- package/dist/orchestration/thread-input.js +8 -9
- package/dist/orchestration/turn/turn-prep.d.ts +15 -1
- package/dist/orchestration/turn/turn-prep.js +19 -8
- package/dist/orchestration/turn/turn.js +1 -0
- package/dist/orchestration/waitpoint-ingress.d.ts +41 -0
- package/dist/orchestration/waitpoint-ingress.js +172 -0
- package/dist/orchestration/waitpoint-notifier.d.ts +21 -0
- package/dist/orchestration/waitpoint-notifier.js +119 -0
- package/dist/orchestration/waitpoint-remote-drain.d.ts +20 -0
- package/dist/orchestration/waitpoint-remote-drain.js +147 -0
- package/dist/orchestration/waitpoint-sweep.d.ts +42 -0
- package/dist/orchestration/waitpoint-sweep.js +87 -0
- package/dist/platform/adapters/feishu-image.d.ts +19 -0
- package/dist/platform/adapters/feishu-image.js +71 -0
- package/dist/platform/adapters/feishu.d.ts +2 -0
- package/dist/platform/adapters/feishu.js +26 -16
- package/dist/platform/ui-http/listening-ports.d.ts +1 -1
- package/dist/platform/ui-http/listening-ports.js +1 -1
- package/dist/platform/ui-http/ui-session.js +1 -1
- package/dist/store/conversation-display-projection.d.ts +9 -1
- package/dist/store/conversation-display-projection.js +13 -6
- package/dist/store/conversation-history-reader.d.ts +19 -0
- package/dist/store/conversation-history-reader.js +163 -44
- package/dist/store/conversation-history-repo.d.ts +37 -10
- package/dist/store/conversation-history-repo.js +161 -139
- package/dist/store/conversation-ledger-repo.js +1 -1
- package/dist/store/execution-repo.d.ts +1 -19
- package/dist/store/execution-repo.js +1 -18
- package/dist/store/project-notes-repo.js +11 -2
- package/dist/store/prompt-migration-replacements.js +1 -1
- package/dist/store/session-registry-import.js +1 -1
- package/dist/store/session-registry-journal.d.ts +4 -0
- package/dist/store/session-registry-journal.js +5 -1
- package/dist/store/session-registry-repo.d.ts +2 -1
- package/dist/store/session-registry-repo.js +3 -1
- package/dist/store/task-repo.d.ts +6 -3
- package/dist/store/task-repo.js +26 -10
- package/dist/store/version-migrations.d.ts +13 -3
- package/dist/store/version-migrations.js +254 -24
- package/dist/store/waitpoint-repo.d.ts +108 -0
- package/dist/store/waitpoint-repo.js +94 -0
- package/package.json +3 -2
- package/web/dist/AGENTS.md +10 -0
- package/web/dist/assets/index-B1dBi8Za.css +1 -0
- package/web/dist/assets/{index-CEZ0DSei.js → index-BAeLybF0.js} +1 -1
- package/web/dist/assets/index-BcQI8Fh5.js +445 -0
- package/web/dist/index.html +12 -4
- package/web/dist/theme.css +211 -40
- package/defaults/config/hooks/08-cortex-md-injector-post-tool.json +0 -10
- package/dist/domain/executions/log-tailer.d.ts +0 -71
- package/dist/domain/executions/log-tailer.js +0 -194
- package/dist/domain/memory/cortex-md-injector.d.ts +0 -24
- package/dist/domain/memory/cortex-md-scanner.d.ts +0 -10
- package/dist/domain/memory/cortex-md-scanner.js +0 -66
- package/dist/domain/remote/cortex-client.d.ts +0 -1
- package/dist/domain/remote/cortex-client.js +0 -662
- package/dist/domain/tasks/system/cortex-run.d.ts +0 -25
- package/dist/domain/tasks/system/cortex-run.js +0 -360
- package/web/dist/assets/index-CRDJo39X.css +0 -1
- package/web/dist/assets/index-DjuPJVZE.js +0 -444
- /package/defaults/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/config/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/context/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/context/ideas/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/context/projects/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/context/scans/{CORTEX.md → AGENTS.md} +0 -0
- /package/defaults/context/user/{CORTEX.md → AGENTS.md} +0 -0
|
@@ -224,7 +224,10 @@ export declare class ConversationHistoryRepo {
|
|
|
224
224
|
private readonly historyDir;
|
|
225
225
|
/** Per-session serial write chain — keeps concurrent appends from interleaving a line. */
|
|
226
226
|
private writeChains;
|
|
227
|
-
|
|
227
|
+
/** Bumped only when a session's file is REWRITTEN (rewind / clear) — never by an append. */
|
|
228
|
+
private rewriteEpochs;
|
|
229
|
+
/** In-flight model refresh per session, so concurrent reads share one fold instead of racing. */
|
|
230
|
+
private refreshChains;
|
|
228
231
|
private compactCache;
|
|
229
232
|
private compactCacheBytes;
|
|
230
233
|
private dirReady;
|
|
@@ -233,22 +236,37 @@ export declare class ConversationHistoryRepo {
|
|
|
233
236
|
private readonly compactHistoryAccumulatorReader;
|
|
234
237
|
constructor(historyDir?: string, options?: ConversationHistoryRepoOptions);
|
|
235
238
|
private ensureDir;
|
|
236
|
-
private
|
|
237
|
-
private
|
|
239
|
+
private currentEpoch;
|
|
240
|
+
private bumpEpoch;
|
|
238
241
|
private awaitWriteChain;
|
|
239
242
|
private dropCompactCache;
|
|
240
|
-
private readCompactCache;
|
|
241
|
-
private markCompactCacheGeneration;
|
|
242
243
|
private storeCompactCache;
|
|
243
244
|
private trimCompactCache;
|
|
244
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Bring the session's resident model level with the file on disk and return it.
|
|
247
|
+
*
|
|
248
|
+
* Cold: fold the whole file once. Warm: fold only the bytes appended since the model's cursor —
|
|
249
|
+
* which is the whole point, because a chat session's transcript is re-read at event rate and
|
|
250
|
+
* re-parsing it end to end is what saturated the event loop.
|
|
251
|
+
*
|
|
252
|
+
* Refreshes are serialized per session rather than shared: a caller that has already awaited an
|
|
253
|
+
* append must not be handed a fold that started before it. Queueing costs nothing once warm —
|
|
254
|
+
* the refresh behind it finds the model current and only stats the file — and it keeps
|
|
255
|
+
* read-your-writes, which a shared in-flight promise would quietly break.
|
|
256
|
+
*
|
|
257
|
+
* A rewrite landing mid-fold is caught by the epoch check and the fold is discarded (one retry,
|
|
258
|
+
* then the caller gets whatever the retry saw).
|
|
259
|
+
*/
|
|
260
|
+
private ensureModel;
|
|
261
|
+
private refreshModel;
|
|
262
|
+
/** The compact projection of a current model, recomputed only when the fold actually moved. */
|
|
263
|
+
private compactOf;
|
|
264
|
+
/** Append one line. The resident model is deliberately NOT updated here: the file is the single
|
|
265
|
+
* source of truth and the next read folds the new bytes off it, so an in-memory copy can never
|
|
266
|
+
* drift from — or double-count against — what a concurrent read already folded. */
|
|
245
267
|
private append;
|
|
246
268
|
private readHistoryFile;
|
|
247
|
-
private readStreamHistory;
|
|
248
269
|
private rewriteTruncatedSession;
|
|
249
|
-
private scanCompactHistory;
|
|
250
|
-
private stableCompactHistory;
|
|
251
|
-
private serializedCompactHistory;
|
|
252
270
|
/** Append a user message — starts a new turn (turn boundaries are derived on read).
|
|
253
271
|
* An optional `ts` override lets the caller share a single timestamp with the
|
|
254
272
|
* EventBus event so the web UI's content-based de-dup produces identical keys. */
|
|
@@ -369,6 +387,15 @@ export declare class ConversationHistoryRepo {
|
|
|
369
387
|
*/
|
|
370
388
|
getHistory(sessionId: string, options?: HistoryReadOptions): Promise<SessionHistory | null>;
|
|
371
389
|
getCompactHistory(sessionId: string): Promise<CompactConversationHistory | null>;
|
|
390
|
+
/**
|
|
391
|
+
* The compact projection plus the cursor a caller needs to ask for the next delta. The cursor is
|
|
392
|
+
* `<epoch>:<revision>`: the epoch changes when the session is rewritten, which is precisely when
|
|
393
|
+
* a held cursor stops meaning anything and the caller must take a full snapshot again.
|
|
394
|
+
*/
|
|
395
|
+
getCompactHistoryAt(sessionId: string): Promise<{
|
|
396
|
+
value: CompactConversationHistory | null;
|
|
397
|
+
cursor: string;
|
|
398
|
+
}>;
|
|
372
399
|
getSubagentHistory(sessionId: string, subagentId: string): Promise<SubagentConversationHistory>;
|
|
373
400
|
/** Load one tool's lossless DEBUG payload without attaching every result to the transcript. */
|
|
374
401
|
getToolDebugDetails(sessionId: string, toolRef: string): Promise<HistoryDebugDetails | null>;
|
|
@@ -2,22 +2,17 @@ import * as path from 'path';
|
|
|
2
2
|
import { createReadStream, promises as fs } from 'fs';
|
|
3
3
|
import { createInterface } from 'node:readline';
|
|
4
4
|
import { STORE_DIR } from '../core/paths.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { isOverDebugToolWarningChars } from '../core/debug-mode.js';
|
|
6
|
+
import { projectCompactHistory, projectSubagentHistory, } from './conversation-display-projection.js';
|
|
7
|
+
import { readHistoryAccumulator, readHistoryStream, resumeHistoryAccumulator, } from './conversation-history-reader.js';
|
|
7
8
|
import { parseTodoSnapshot, renderTodoProgress } from '../agent-adapter/normalize/todo.js';
|
|
8
9
|
const HISTORY_DIR = path.join(STORE_DIR, 'conversation-history');
|
|
9
10
|
const DEFAULT_COMPACT_CACHE_ENTRIES = 32;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
function appendedCacheBytes(serializedLine) {
|
|
17
|
-
if (serializedLine.startsWith('{"type":"debug-tool-result"'))
|
|
18
|
-
return 0;
|
|
19
|
-
return Buffer.byteLength(serializedLine, 'utf8') + 256;
|
|
20
|
-
}
|
|
11
|
+
/** Total resident budget for folded transcripts. Sized to hold several long sessions at once:
|
|
12
|
+
* a session whose model is dropped here is re-folded from byte 0 on every read (~1s of
|
|
13
|
+
* synchronous JSON parsing for a 40MB transcript, measured), so the budget is the difference
|
|
14
|
+
* between a warm read (~1ms) and a repeated full scan. */
|
|
15
|
+
const DEFAULT_COMPACT_CACHE_BYTES = 128 * 1024 * 1024;
|
|
21
16
|
function nowIso() {
|
|
22
17
|
return new Date().toISOString();
|
|
23
18
|
}
|
|
@@ -112,7 +107,10 @@ export class ConversationHistoryRepo {
|
|
|
112
107
|
historyDir;
|
|
113
108
|
/** Per-session serial write chain — keeps concurrent appends from interleaving a line. */
|
|
114
109
|
writeChains = new Map();
|
|
115
|
-
|
|
110
|
+
/** Bumped only when a session's file is REWRITTEN (rewind / clear) — never by an append. */
|
|
111
|
+
rewriteEpochs = new Map();
|
|
112
|
+
/** In-flight model refresh per session, so concurrent reads share one fold instead of racing. */
|
|
113
|
+
refreshChains = new Map();
|
|
116
114
|
compactCache = new Map();
|
|
117
115
|
compactCacheBytes = 0;
|
|
118
116
|
dirReady = false;
|
|
@@ -131,12 +129,12 @@ export class ConversationHistoryRepo {
|
|
|
131
129
|
await fs.mkdir(this.historyDir, { recursive: true });
|
|
132
130
|
this.dirReady = true;
|
|
133
131
|
}
|
|
134
|
-
|
|
135
|
-
return this.
|
|
132
|
+
currentEpoch(sessionId) {
|
|
133
|
+
return this.rewriteEpochs.get(sessionId) ?? 0;
|
|
136
134
|
}
|
|
137
|
-
|
|
138
|
-
const next = this.
|
|
139
|
-
this.
|
|
135
|
+
bumpEpoch(sessionId) {
|
|
136
|
+
const next = this.currentEpoch(sessionId) + 1;
|
|
137
|
+
this.rewriteEpochs.set(sessionId, next);
|
|
140
138
|
return next;
|
|
141
139
|
}
|
|
142
140
|
async awaitWriteChain(sessionId) {
|
|
@@ -149,35 +147,17 @@ export class ConversationHistoryRepo {
|
|
|
149
147
|
this.compactCacheBytes -= cached.bytes;
|
|
150
148
|
this.compactCache.delete(sessionId);
|
|
151
149
|
}
|
|
152
|
-
|
|
153
|
-
const cached = this.compactCache.get(sessionId);
|
|
154
|
-
if (!cached)
|
|
155
|
-
return undefined;
|
|
156
|
-
if (cached.generation !== this.currentGeneration(sessionId)) {
|
|
157
|
-
this.dropCompactCache(sessionId);
|
|
158
|
-
return undefined;
|
|
159
|
-
}
|
|
160
|
-
this.compactCache.delete(sessionId);
|
|
161
|
-
this.compactCache.set(sessionId, cached);
|
|
162
|
-
return cached.value;
|
|
163
|
-
}
|
|
164
|
-
markCompactCacheGeneration(sessionId, generation) {
|
|
165
|
-
const cached = this.compactCache.get(sessionId);
|
|
166
|
-
if (!cached)
|
|
167
|
-
return;
|
|
168
|
-
cached.generation = generation;
|
|
169
|
-
this.compactCache.delete(sessionId);
|
|
170
|
-
this.compactCache.set(sessionId, cached);
|
|
171
|
-
}
|
|
172
|
-
storeCompactCache(sessionId, value, generation, accumulator, estimatedBytes) {
|
|
173
|
-
if (value === null || !accumulator)
|
|
174
|
-
return this.dropCompactCache(sessionId);
|
|
175
|
-
const bytes = estimatedBytes ?? estimateCacheEntryBytes(value, accumulator);
|
|
150
|
+
storeCompactCache(sessionId, entry) {
|
|
176
151
|
this.dropCompactCache(sessionId);
|
|
177
|
-
|
|
152
|
+
// A transcript that alone exceeds the WHOLE budget is not retained. This is the total-budget
|
|
153
|
+
// invariant, not an independent per-file size cap: without it `trimCompactCache` (which walks
|
|
154
|
+
// insertion order) would evict every other session trying to make room and then evict this
|
|
155
|
+
// entry too, leaving the cache empty — measurably worse than not admitting it. Raising the
|
|
156
|
+
// budget is what makes longer sessions cacheable; this line only bounds a single outlier.
|
|
157
|
+
if (entry.bytes > this.compactCacheByteLimit)
|
|
178
158
|
return;
|
|
179
|
-
this.compactCache.set(sessionId,
|
|
180
|
-
this.compactCacheBytes += bytes;
|
|
159
|
+
this.compactCache.set(sessionId, entry);
|
|
160
|
+
this.compactCacheBytes += entry.bytes;
|
|
181
161
|
this.trimCompactCache();
|
|
182
162
|
}
|
|
183
163
|
trimCompactCache() {
|
|
@@ -189,34 +169,109 @@ export class ConversationHistoryRepo {
|
|
|
189
169
|
this.dropCompactCache(oldest);
|
|
190
170
|
}
|
|
191
171
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Bring the session's resident model level with the file on disk and return it.
|
|
174
|
+
*
|
|
175
|
+
* Cold: fold the whole file once. Warm: fold only the bytes appended since the model's cursor —
|
|
176
|
+
* which is the whole point, because a chat session's transcript is re-read at event rate and
|
|
177
|
+
* re-parsing it end to end is what saturated the event loop.
|
|
178
|
+
*
|
|
179
|
+
* Refreshes are serialized per session rather than shared: a caller that has already awaited an
|
|
180
|
+
* append must not be handed a fold that started before it. Queueing costs nothing once warm —
|
|
181
|
+
* the refresh behind it finds the model current and only stats the file — and it keeps
|
|
182
|
+
* read-your-writes, which a shared in-flight promise would quietly break.
|
|
183
|
+
*
|
|
184
|
+
* A rewrite landing mid-fold is caught by the epoch check and the fold is discarded (one retry,
|
|
185
|
+
* then the caller gets whatever the retry saw).
|
|
186
|
+
*/
|
|
187
|
+
ensureModel(sessionId) {
|
|
188
|
+
const prev = this.refreshChains.get(sessionId) ?? Promise.resolve(null);
|
|
189
|
+
const run = prev
|
|
190
|
+
.catch(() => null)
|
|
191
|
+
.then(() => this.refreshModel(sessionId))
|
|
192
|
+
.finally(() => {
|
|
193
|
+
if (this.refreshChains.get(sessionId) === run)
|
|
194
|
+
this.refreshChains.delete(sessionId);
|
|
195
|
+
});
|
|
196
|
+
this.refreshChains.set(sessionId, run);
|
|
197
|
+
return run;
|
|
198
|
+
}
|
|
199
|
+
async refreshModel(sessionId) {
|
|
200
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
201
|
+
const epoch = this.currentEpoch(sessionId);
|
|
202
|
+
const filePath = sessionFilePath(this.historyDir, sessionId);
|
|
203
|
+
let size;
|
|
204
|
+
try {
|
|
205
|
+
size = (await fs.stat(filePath)).size;
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
this.dropCompactCache(sessionId); // absent file — nothing to model
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
let entry = this.compactCache.get(sessionId);
|
|
212
|
+
// A file that SHRANK was rewritten behind our back: the fold is not a prefix of it any more.
|
|
213
|
+
if (entry && (entry.epoch !== epoch || size < entry.accumulator.bytesConsumed))
|
|
214
|
+
entry = undefined;
|
|
215
|
+
// Take the entry OUT of the budget before touching it. `bytes` grows as the fold advances, so
|
|
216
|
+
// an in-place update would later be subtracted at its new size and leave the total adrift.
|
|
203
217
|
this.dropCompactCache(sessionId);
|
|
218
|
+
try {
|
|
219
|
+
if (!entry) {
|
|
220
|
+
const accumulator = await this.compactHistoryAccumulatorReader(sessionId, filePath, { includeToolDebug: false });
|
|
221
|
+
// A reader that does not report a cursor cannot be resumed — folding from 0 next time
|
|
222
|
+
// would replay every row into the same model. Serve it, but never keep it.
|
|
223
|
+
if (accumulator.bytesConsumed === 0 && size > 0)
|
|
224
|
+
return { epoch, bytes: size, accumulator, projection: null };
|
|
225
|
+
entry = { epoch, bytes: accumulator.bytesConsumed, accumulator, projection: null };
|
|
226
|
+
}
|
|
227
|
+
else if (size > entry.accumulator.bytesConsumed) {
|
|
228
|
+
await resumeHistoryAccumulator(entry.accumulator, filePath);
|
|
229
|
+
entry.bytes = entry.accumulator.bytesConsumed;
|
|
230
|
+
entry.projection = null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
this.dropCompactCache(sessionId);
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
if (this.currentEpoch(sessionId) !== epoch) {
|
|
238
|
+
this.dropCompactCache(sessionId);
|
|
239
|
+
continue; // rewritten while we folded — rebuild against the new file
|
|
240
|
+
}
|
|
241
|
+
// An empty session has nothing to keep warm and costs nothing to re-fold; leaving it out
|
|
242
|
+
// keeps the budget for transcripts where resuming actually saves work.
|
|
243
|
+
if (entry.accumulator.eventCount === 0) {
|
|
244
|
+
this.dropCompactCache(sessionId);
|
|
245
|
+
return entry;
|
|
246
|
+
}
|
|
247
|
+
this.storeCompactCache(sessionId, entry); // re-inserting is also what refreshes LRU order
|
|
248
|
+
return entry; // returned even when the budget refused it: correct, just not retained
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
/** The compact projection of a current model, recomputed only when the fold actually moved. */
|
|
253
|
+
compactOf(entry) {
|
|
254
|
+
const revision = entry.accumulator.revision;
|
|
255
|
+
if (entry.projection && entry.projection.revision === revision)
|
|
256
|
+
return entry.projection.value;
|
|
257
|
+
const history = entry.accumulator.snapshot();
|
|
258
|
+
if (!history) {
|
|
259
|
+
entry.projection = null;
|
|
260
|
+
return null;
|
|
204
261
|
}
|
|
262
|
+
const value = projectCompactHistory(history, entry.accumulator.eventRevisions());
|
|
263
|
+
entry.projection = { revision, value };
|
|
264
|
+
return value;
|
|
205
265
|
}
|
|
266
|
+
/** Append one line. The resident model is deliberately NOT updated here: the file is the single
|
|
267
|
+
* source of truth and the next read folds the new bytes off it, so an in-memory copy can never
|
|
268
|
+
* drift from — or double-count against — what a concurrent read already folded. */
|
|
206
269
|
append(sessionId, ev) {
|
|
207
|
-
const generation = this.bumpGeneration(sessionId);
|
|
208
270
|
const serializedLine = JSON.stringify(ev) + '\n';
|
|
209
271
|
const prev = this.writeChains.get(sessionId) ?? Promise.resolve();
|
|
210
272
|
const next = prev.catch(() => { }).then(async () => {
|
|
211
273
|
await this.ensureDir();
|
|
212
|
-
|
|
213
|
-
await fs.appendFile(sessionFilePath(this.historyDir, sessionId), serializedLine, 'utf8');
|
|
214
|
-
}
|
|
215
|
-
catch (error) {
|
|
216
|
-
this.dropCompactCache(sessionId);
|
|
217
|
-
throw error;
|
|
218
|
-
}
|
|
219
|
-
this.updateCompactCacheAfterAppend(sessionId, serializedLine, generation);
|
|
274
|
+
await fs.appendFile(sessionFilePath(this.historyDir, sessionId), serializedLine, 'utf8');
|
|
220
275
|
});
|
|
221
276
|
this.writeChains.set(sessionId, next);
|
|
222
277
|
return next;
|
|
@@ -229,15 +284,6 @@ export class ConversationHistoryRepo {
|
|
|
229
284
|
return null;
|
|
230
285
|
}
|
|
231
286
|
}
|
|
232
|
-
async readStreamHistory(sessionId) {
|
|
233
|
-
const filePath = sessionFilePath(this.historyDir, sessionId);
|
|
234
|
-
try {
|
|
235
|
-
return await readHistoryStream(sessionId, filePath, { includeToolDebug: false });
|
|
236
|
-
}
|
|
237
|
-
catch {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
287
|
async rewriteTruncatedSession(sessionId, turnIndex) {
|
|
242
288
|
const raw = await this.readHistoryFile(sessionId);
|
|
243
289
|
if (raw === null)
|
|
@@ -248,49 +294,6 @@ export class ConversationHistoryRepo {
|
|
|
248
294
|
await fs.writeFile(sessionFilePath(this.historyDir, sessionId), kept.length ? kept.join('\n') + '\n' : '', 'utf8');
|
|
249
295
|
return removed;
|
|
250
296
|
}
|
|
251
|
-
async scanCompactHistory(sessionId) {
|
|
252
|
-
const filePath = sessionFilePath(this.historyDir, sessionId);
|
|
253
|
-
try {
|
|
254
|
-
const accumulator = await this.compactHistoryAccumulatorReader(sessionId, filePath, { includeToolDebug: false });
|
|
255
|
-
const history = accumulator.snapshot();
|
|
256
|
-
return history ? { accumulator, value: projectCompactHistory(history) } : null;
|
|
257
|
-
}
|
|
258
|
-
catch {
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
async stableCompactHistory(sessionId) {
|
|
263
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
264
|
-
const generation = this.currentGeneration(sessionId);
|
|
265
|
-
const history = await this.scanCompactHistory(sessionId);
|
|
266
|
-
if (this.currentGeneration(sessionId) !== generation) {
|
|
267
|
-
await this.awaitWriteChain(sessionId);
|
|
268
|
-
const cached = this.readCompactCache(sessionId);
|
|
269
|
-
if (cached !== undefined)
|
|
270
|
-
return cached;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
this.storeCompactCache(sessionId, history?.value ?? null, generation, history?.accumulator);
|
|
274
|
-
return history?.value ?? null;
|
|
275
|
-
}
|
|
276
|
-
await this.awaitWriteChain(sessionId);
|
|
277
|
-
const cached = this.readCompactCache(sessionId);
|
|
278
|
-
if (cached !== undefined)
|
|
279
|
-
return cached;
|
|
280
|
-
return this.serializedCompactHistory(sessionId);
|
|
281
|
-
}
|
|
282
|
-
async serializedCompactHistory(sessionId) {
|
|
283
|
-
let history = null;
|
|
284
|
-
const generation = this.currentGeneration(sessionId);
|
|
285
|
-
const prev = this.writeChains.get(sessionId) ?? Promise.resolve();
|
|
286
|
-
const next = prev.catch(() => { }).then(async () => { history = await this.scanCompactHistory(sessionId); });
|
|
287
|
-
this.writeChains.set(sessionId, next);
|
|
288
|
-
await next;
|
|
289
|
-
if (this.currentGeneration(sessionId) === generation) {
|
|
290
|
-
this.storeCompactCache(sessionId, history?.value ?? null, generation, history?.accumulator);
|
|
291
|
-
}
|
|
292
|
-
return history?.value ?? null;
|
|
293
|
-
}
|
|
294
297
|
/** Append a user message — starts a new turn (turn boundaries are derived on read).
|
|
295
298
|
* An optional `ts` override lets the caller share a single timestamp with the
|
|
296
299
|
* EventBus event so the web UI's content-based de-dup produces identical keys. */
|
|
@@ -389,7 +392,6 @@ export class ConversationHistoryRepo {
|
|
|
389
392
|
* (for the edit marker + attachment reuse), or null when the turn does not exist.
|
|
390
393
|
*/
|
|
391
394
|
async truncateFromTurn(sessionId, turnIndex) {
|
|
392
|
-
const generation = this.bumpGeneration(sessionId);
|
|
393
395
|
let removed = null;
|
|
394
396
|
const prev = this.writeChains.get(sessionId) ?? Promise.resolve();
|
|
395
397
|
const next = prev.catch(() => { }).then(async () => {
|
|
@@ -397,13 +399,15 @@ export class ConversationHistoryRepo {
|
|
|
397
399
|
removed = await this.rewriteTruncatedSession(sessionId, turnIndex);
|
|
398
400
|
}
|
|
399
401
|
catch (error) {
|
|
402
|
+
this.bumpEpoch(sessionId);
|
|
400
403
|
this.dropCompactCache(sessionId);
|
|
401
404
|
throw error;
|
|
402
405
|
}
|
|
403
|
-
|
|
406
|
+
// An out-of-range rewind rewrote nothing, so the resident fold is still a valid prefix.
|
|
407
|
+
if (removed) {
|
|
408
|
+
this.bumpEpoch(sessionId);
|
|
404
409
|
this.dropCompactCache(sessionId);
|
|
405
|
-
|
|
406
|
-
this.markCompactCacheGeneration(sessionId, generation);
|
|
410
|
+
}
|
|
407
411
|
});
|
|
408
412
|
this.writeChains.set(sessionId, next);
|
|
409
413
|
await next;
|
|
@@ -416,12 +420,14 @@ export class ConversationHistoryRepo {
|
|
|
416
420
|
*/
|
|
417
421
|
async getHistory(sessionId, options = {}) {
|
|
418
422
|
await this.awaitWriteChain(sessionId);
|
|
419
|
-
//
|
|
420
|
-
//
|
|
421
|
-
//
|
|
422
|
-
//
|
|
423
|
-
|
|
424
|
-
|
|
423
|
+
// The resident model is folded WITHOUT tool DEBUG payloads, which is what every transcript read
|
|
424
|
+
// asks for — those reads share it and pay only for the bytes appended since the last one.
|
|
425
|
+
// A DEBUG read wants the lossless inputs the model dropped, so it still streams the file whole;
|
|
426
|
+
// it is rare (server DEBUG only) and never on the UI's hot path.
|
|
427
|
+
if (options.includeToolDebug === false) {
|
|
428
|
+
const entry = await this.ensureModel(sessionId);
|
|
429
|
+
return entry?.accumulator.snapshot() ?? null;
|
|
430
|
+
}
|
|
425
431
|
try {
|
|
426
432
|
return await readHistoryStream(sessionId, sessionFilePath(this.historyDir, sessionId), options);
|
|
427
433
|
}
|
|
@@ -431,13 +437,25 @@ export class ConversationHistoryRepo {
|
|
|
431
437
|
}
|
|
432
438
|
async getCompactHistory(sessionId) {
|
|
433
439
|
await this.awaitWriteChain(sessionId);
|
|
434
|
-
const
|
|
435
|
-
return
|
|
440
|
+
const entry = await this.ensureModel(sessionId);
|
|
441
|
+
return entry ? this.compactOf(entry) : null;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* The compact projection plus the cursor a caller needs to ask for the next delta. The cursor is
|
|
445
|
+
* `<epoch>:<revision>`: the epoch changes when the session is rewritten, which is precisely when
|
|
446
|
+
* a held cursor stops meaning anything and the caller must take a full snapshot again.
|
|
447
|
+
*/
|
|
448
|
+
async getCompactHistoryAt(sessionId) {
|
|
449
|
+
await this.awaitWriteChain(sessionId);
|
|
450
|
+
const entry = await this.ensureModel(sessionId);
|
|
451
|
+
if (!entry)
|
|
452
|
+
return { value: null, cursor: `${this.currentEpoch(sessionId)}:0` };
|
|
453
|
+
return { value: this.compactOf(entry), cursor: `${entry.epoch}:${entry.accumulator.revision}` };
|
|
436
454
|
}
|
|
437
455
|
async getSubagentHistory(sessionId, subagentId) {
|
|
438
456
|
await this.awaitWriteChain(sessionId);
|
|
439
|
-
const
|
|
440
|
-
const projected = projectSubagentHistory(
|
|
457
|
+
const entry = await this.ensureModel(sessionId);
|
|
458
|
+
const projected = projectSubagentHistory(entry?.accumulator.snapshot() ?? null, subagentId);
|
|
441
459
|
return { ...projected, sessionId };
|
|
442
460
|
}
|
|
443
461
|
/** Load one tool's lossless DEBUG payload without attaching every result to the transcript. */
|
|
@@ -446,6 +464,10 @@ export class ConversationHistoryRepo {
|
|
|
446
464
|
const stream = createReadStream(sessionFilePath(this.historyDir, sessionId), { encoding: 'utf8' });
|
|
447
465
|
const lines = createInterface({ input: stream, crlfDelay: Infinity });
|
|
448
466
|
let details = null;
|
|
467
|
+
// Same size policy as the fold: the rows we already read ARE the payload, so their combined
|
|
468
|
+
// line length is the measure. Nothing is re-serialized to weigh it.
|
|
469
|
+
let chars = 0;
|
|
470
|
+
const stamp = (value) => isOverDebugToolWarningChars(chars) ? { ...value, overCharacterThreshold: true } : value;
|
|
449
471
|
try {
|
|
450
472
|
for await (const line of lines) {
|
|
451
473
|
if (!line.includes(toolRef))
|
|
@@ -460,16 +482,18 @@ export class ConversationHistoryRepo {
|
|
|
460
482
|
if (event.toolUseId !== toolRef)
|
|
461
483
|
continue;
|
|
462
484
|
if (event.type === 'tool') {
|
|
485
|
+
chars += line.length;
|
|
463
486
|
details = { ...(details ?? {}), toolRef, toolInput: event.fullInput };
|
|
464
487
|
}
|
|
465
488
|
else if (event.type === 'debug-tool-result') {
|
|
466
|
-
|
|
489
|
+
chars += line.length;
|
|
490
|
+
return stamp({
|
|
467
491
|
...(details ?? {}), toolRef,
|
|
468
492
|
toolResult: { content: event.text ?? '', isError: event.isError === true },
|
|
469
|
-
};
|
|
493
|
+
});
|
|
470
494
|
}
|
|
471
495
|
}
|
|
472
|
-
return details;
|
|
496
|
+
return details ? stamp(details) : null;
|
|
473
497
|
}
|
|
474
498
|
catch {
|
|
475
499
|
return null;
|
|
@@ -535,15 +559,14 @@ export class ConversationHistoryRepo {
|
|
|
535
559
|
return found ?? null;
|
|
536
560
|
}
|
|
537
561
|
async clear(sessionId) {
|
|
538
|
-
const generation = this.bumpGeneration(sessionId);
|
|
539
562
|
const prev = this.writeChains.get(sessionId) ?? Promise.resolve();
|
|
540
563
|
const next = prev.catch(() => { }).then(async () => {
|
|
564
|
+
this.bumpEpoch(sessionId);
|
|
541
565
|
this.dropCompactCache(sessionId);
|
|
542
566
|
try {
|
|
543
567
|
await fs.unlink(sessionFilePath(this.historyDir, sessionId));
|
|
544
568
|
}
|
|
545
569
|
catch { /* already gone */ }
|
|
546
|
-
this.markCompactCacheGeneration(sessionId, generation);
|
|
547
570
|
});
|
|
548
571
|
this.writeChains.set(sessionId, next);
|
|
549
572
|
await next;
|
|
@@ -557,10 +580,10 @@ export class ConversationHistoryRepo {
|
|
|
557
580
|
return removed;
|
|
558
581
|
}
|
|
559
582
|
async clearOneSession(sessionId) {
|
|
560
|
-
const generation = this.bumpGeneration(sessionId);
|
|
561
583
|
let removed = 0;
|
|
562
584
|
const prev = this.writeChains.get(sessionId) ?? Promise.resolve();
|
|
563
585
|
const next = prev.catch(() => { }).then(async () => {
|
|
586
|
+
this.bumpEpoch(sessionId);
|
|
564
587
|
this.dropCompactCache(sessionId);
|
|
565
588
|
try {
|
|
566
589
|
await fs.unlink(sessionFilePath(this.historyDir, sessionId));
|
|
@@ -570,7 +593,6 @@ export class ConversationHistoryRepo {
|
|
|
570
593
|
if (error.code !== 'ENOENT')
|
|
571
594
|
throw error;
|
|
572
595
|
}
|
|
573
|
-
this.markCompactCacheGeneration(sessionId, generation);
|
|
574
596
|
});
|
|
575
597
|
this.writeChains.set(sessionId, next);
|
|
576
598
|
await next;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// ledger API (getConversation / beginTurn / rollbackTo / …) while the registry owns the data.
|
|
4
4
|
// pos: Compatibility surface for turn-history callers; NO storage of its own (no JSON file — the
|
|
5
5
|
// registry journal is the only store)
|
|
6
|
-
// >>> Once I am updated, be sure to update my header comment and the parent folder
|
|
6
|
+
// >>> Once I am updated, be sure to update my header comment and the parent folder AGENTS.md <<<
|
|
7
7
|
//
|
|
8
8
|
// The ledger no longer has a JSON file. Every method maps onto the registry's channel-keyed
|
|
9
9
|
// conversation header + turn history, most under a single `registry.batch(...)` critical section so a
|
|
@@ -7,16 +7,6 @@ export interface DispatchInfo {
|
|
|
7
7
|
sessionName: string | null;
|
|
8
8
|
tmuxName: string | null;
|
|
9
9
|
pid: string | null;
|
|
10
|
-
/** cortex-run `--name` (log-ref): resolves the run's output.log for the live log tailer (B2-C). */
|
|
11
|
-
runName: string | null;
|
|
12
|
-
}
|
|
13
|
-
/** The GPU actually selected for a cortex-run/dispatch execution (DR-0018 §6.3 B2-followup).
|
|
14
|
-
* Captured by the client watcher (`resolveGpuSelection`, incl. `--gpu auto`) and delivered via
|
|
15
|
-
* task-callback. `indices` = CUDA device ordinals; `memoryMb` = the selected GPU's total memory
|
|
16
|
-
* (auto-pick only — null for an explicit `--gpu N`). Null on the record ⇒ unknown / not captured. */
|
|
17
|
-
export interface ExecutionGpuInfo {
|
|
18
|
-
indices: number[];
|
|
19
|
-
memoryMb: number | null;
|
|
20
10
|
}
|
|
21
11
|
export interface ExecutionRecord {
|
|
22
12
|
id: string;
|
|
@@ -56,7 +46,6 @@ export interface ExecutionRecord {
|
|
|
56
46
|
numTurns: number | null;
|
|
57
47
|
durationS: number | null;
|
|
58
48
|
};
|
|
59
|
-
gpu: ExecutionGpuInfo | null;
|
|
60
49
|
text: {
|
|
61
50
|
label: string | null;
|
|
62
51
|
finalOutput: string | null;
|
|
@@ -113,7 +102,7 @@ declare class ExecutionRepo {
|
|
|
113
102
|
threadId?: string | null;
|
|
114
103
|
agentSlotId?: string | null;
|
|
115
104
|
}): ExecutionRecord;
|
|
116
|
-
registerDispatchExecution({ taskId, machine, channel, project, scheduleTaskId, taskText, taskHash, sessionName, tmuxName, pid,
|
|
105
|
+
registerDispatchExecution({ taskId, machine, channel, project, scheduleTaskId, taskText, taskHash, sessionName, tmuxName, pid, backend, billingMode }: {
|
|
117
106
|
taskId: string;
|
|
118
107
|
machine?: string | null;
|
|
119
108
|
channel?: string | null;
|
|
@@ -124,7 +113,6 @@ declare class ExecutionRepo {
|
|
|
124
113
|
sessionName?: string | null;
|
|
125
114
|
tmuxName?: string | null;
|
|
126
115
|
pid?: string | null;
|
|
127
|
-
runName?: string | null;
|
|
128
116
|
backend?: string;
|
|
129
117
|
billingMode?: string;
|
|
130
118
|
}): ExecutionRecord | null;
|
|
@@ -135,12 +123,6 @@ declare class ExecutionRepo {
|
|
|
135
123
|
session?: Partial<ExecutionRecord['session']>;
|
|
136
124
|
[key: string]: unknown;
|
|
137
125
|
}): ExecutionRecord | null;
|
|
138
|
-
/** Record the per-execution GPU onto the dispatch record keyed by taskId (DR-0018 §6.3
|
|
139
|
-
* B2-followup). GPU is write-once metadata delivered by the terminal task-callback, so —
|
|
140
|
-
* unlike touchExecution — this bypasses the terminal-status guard (the dispatch record is
|
|
141
|
-
* usually still 'running' at callback time, but a re-dispatched/terminal one must still learn
|
|
142
|
-
* its GPU). Returns null when no execution is registered for the task. */
|
|
143
|
-
setExecutionGpuByTaskId(taskId: string, gpu: ExecutionGpuInfo | null): ExecutionRecord | null;
|
|
144
126
|
completeExecution(id: string, metrics?: {
|
|
145
127
|
costUsd?: number | null;
|
|
146
128
|
numTurns?: number | null;
|
|
@@ -48,7 +48,6 @@ function createBaseRecord({ kind, channel, project, trigger, backend, billingMod
|
|
|
48
48
|
numTurns: null,
|
|
49
49
|
durationS: null,
|
|
50
50
|
},
|
|
51
|
-
gpu: null,
|
|
52
51
|
text: {
|
|
53
52
|
label: label || null,
|
|
54
53
|
finalOutput: null,
|
|
@@ -180,7 +179,7 @@ class ExecutionRepo {
|
|
|
180
179
|
this.queuePersist();
|
|
181
180
|
return record;
|
|
182
181
|
}
|
|
183
|
-
registerDispatchExecution({ taskId, machine, channel, project, scheduleTaskId, taskText, taskHash, sessionName, tmuxName, pid,
|
|
182
|
+
registerDispatchExecution({ taskId, machine, channel, project, scheduleTaskId, taskText, taskHash, sessionName, tmuxName, pid, backend, billingMode }) {
|
|
184
183
|
const existing = this.getExecutionByTaskId(taskId);
|
|
185
184
|
if (existing && !TERMINAL_STATUSES.has(existing.status)) {
|
|
186
185
|
return this.updateRecordInternal(existing.id, (record) => ({
|
|
@@ -200,7 +199,6 @@ class ExecutionRepo {
|
|
|
200
199
|
sessionName: sessionName || record.dispatch?.sessionName || null,
|
|
201
200
|
tmuxName: tmuxName || record.dispatch?.tmuxName || null,
|
|
202
201
|
pid: pid || record.dispatch?.pid || null,
|
|
203
|
-
runName: runName || record.dispatch?.runName || null,
|
|
204
202
|
},
|
|
205
203
|
}));
|
|
206
204
|
}
|
|
@@ -222,7 +220,6 @@ class ExecutionRepo {
|
|
|
222
220
|
sessionName: sessionName || null,
|
|
223
221
|
tmuxName: tmuxName || null,
|
|
224
222
|
pid: pid || null,
|
|
225
|
-
runName: runName || null,
|
|
226
223
|
};
|
|
227
224
|
this.map.set(record.id, record);
|
|
228
225
|
this.queuePersist();
|
|
@@ -238,20 +235,6 @@ class ExecutionRepo {
|
|
|
238
235
|
session: patch.session ? { ...record.session, ...patch.session } : record.session,
|
|
239
236
|
}));
|
|
240
237
|
}
|
|
241
|
-
/** Record the per-execution GPU onto the dispatch record keyed by taskId (DR-0018 §6.3
|
|
242
|
-
* B2-followup). GPU is write-once metadata delivered by the terminal task-callback, so —
|
|
243
|
-
* unlike touchExecution — this bypasses the terminal-status guard (the dispatch record is
|
|
244
|
-
* usually still 'running' at callback time, but a re-dispatched/terminal one must still learn
|
|
245
|
-
* its GPU). Returns null when no execution is registered for the task. */
|
|
246
|
-
setExecutionGpuByTaskId(taskId, gpu) {
|
|
247
|
-
const record = this.getExecutionByTaskId(taskId);
|
|
248
|
-
if (!record)
|
|
249
|
-
return null;
|
|
250
|
-
const next = { ...record, gpu };
|
|
251
|
-
this.map.set(record.id, next);
|
|
252
|
-
this.queuePersist();
|
|
253
|
-
return next;
|
|
254
|
-
}
|
|
255
238
|
completeExecution(id, metrics = {}) {
|
|
256
239
|
return this.updateRecordInternal(id, (record) => finalizeExecution(record, 'completed', metrics));
|
|
257
240
|
}
|