@oyasmi/pipiclaw 0.9.4-beta.2 → 0.9.5-beta.2
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.en.md +28 -22
- package/README.md +27 -21
- package/dist/agent/builtin-commands.d.ts +7 -2
- package/dist/agent/builtin-commands.js +19 -23
- package/dist/agent/channel-runner.d.ts +13 -13
- package/dist/agent/channel-runner.js +93 -76
- package/dist/agent/maintenance-context.d.ts +6 -5
- package/dist/agent/maintenance-context.js +15 -15
- package/dist/agent/prompt/manifest.js +1 -1
- package/dist/agent/prompt/resources.d.ts +1 -1
- package/dist/agent/prompt/resources.js +1 -1
- package/dist/agent/prompt/sections.js +4 -4
- package/dist/agent/runner-factory.d.ts +6 -6
- package/dist/agent/runner-factory.js +6 -7
- package/dist/agent/session/compaction-input.d.ts +6 -0
- package/dist/agent/session/compaction-input.js +75 -0
- package/dist/agent/session/compaction-snapshot.d.ts +30 -0
- package/dist/agent/session/compaction-snapshot.js +154 -0
- package/dist/agent/session/compaction-summary.d.ts +39 -0
- package/dist/agent/session/compaction-summary.js +158 -0
- package/dist/agent/session/compaction.d.ts +35 -0
- package/dist/agent/session/compaction.js +294 -0
- package/dist/agent/session/context-input.d.ts +15 -0
- package/dist/agent/session/context-input.js +82 -0
- package/dist/agent/session/context-snapshot.d.ts +11 -0
- package/dist/agent/session/context-snapshot.js +72 -0
- package/dist/agent/session/event-guards.d.ts +2 -0
- package/dist/agent/session/event-guards.js +4 -0
- package/dist/agent/session/request-tools.d.ts +3 -0
- package/dist/agent/session/request-tools.js +20 -0
- package/dist/agent/session/session-adapter.d.ts +2 -1
- package/dist/agent/session/session-adapter.js +6 -3
- package/dist/agent/session/session-events.d.ts +5 -4
- package/dist/agent/session/session-events.js +46 -59
- package/dist/agent/session/session-host.d.ts +29 -41
- package/dist/agent/session/session-host.js +86 -69
- package/dist/agent/session/settings-diagnostics.d.ts +5 -0
- package/dist/agent/session/settings-diagnostics.js +21 -0
- package/dist/agent/status-render.js +5 -2
- package/dist/{runtime → agent}/stop-watchdog.d.ts +3 -1
- package/dist/{runtime → agent}/stop-watchdog.js +7 -3
- package/dist/agent/turn/progress-formatter.d.ts +1 -3
- package/dist/agent/turn/progress-formatter.js +3 -22
- package/dist/agent/turn/task-digest.d.ts +4 -2
- package/dist/agent/turn/task-digest.js +7 -4
- package/dist/agent/turn/turn-context.d.ts +11 -7
- package/dist/agent/turn/turn-context.js +26 -18
- package/dist/agent/turn/turn-finish.d.ts +17 -5
- package/dist/agent/turn/turn-finish.js +51 -77
- package/dist/agent/turn/turn-input.d.ts +2 -2
- package/dist/agent/turn/turn-input.js +4 -3
- package/dist/agent/turn/turn-output.d.ts +19 -0
- package/dist/agent/turn/turn-output.js +41 -0
- package/dist/agent/turn/turn-pipeline.d.ts +11 -8
- package/dist/agent/turn/turn-pipeline.js +54 -55
- package/dist/agent/turn/turn-prompt.d.ts +1 -1
- package/dist/agent/turn/turn-recovery.d.ts +11 -10
- package/dist/agent/turn/turn-recovery.js +14 -29
- package/dist/agent/types.d.ts +31 -12
- package/dist/agent/types.js +2 -1
- package/dist/app/auth-command.js +1 -1
- package/dist/app/home.js +31 -15
- package/dist/channel/active-session-store.d.ts +14 -12
- package/dist/channel/active-session-store.js +29 -25
- package/dist/channel/attachments.d.ts +25 -0
- package/dist/channel/attachments.js +56 -0
- package/dist/channel/channel-context.d.ts +87 -96
- package/dist/channel/channel-context.js +1 -66
- package/dist/channel/channel-event.d.ts +51 -41
- package/dist/channel/channel-event.js +6 -11
- package/dist/channel/channel-id.d.ts +22 -0
- package/dist/channel/channel-id.js +13 -0
- package/dist/channel/channel-index.d.ts +17 -34
- package/dist/channel/channel-index.js +41 -80
- package/dist/channel/config.d.ts +10 -0
- package/dist/channel/config.js +55 -0
- package/dist/channel/ingress.d.ts +101 -0
- package/dist/channel/ingress.js +5 -0
- package/dist/channel/store.d.ts +13 -15
- package/dist/channel/store.js +21 -25
- package/dist/channel/transport.d.ts +99 -0
- package/dist/channel/turn-context.d.ts +23 -0
- package/dist/channel/turn-context.js +1 -0
- package/dist/commands/catalog.d.ts +4 -7
- package/dist/commands/catalog.js +24 -31
- package/dist/commands/router.d.ts +23 -12
- package/dist/commands/router.js +22 -24
- package/dist/config/settings.d.ts +5 -4
- package/dist/desk/bindings.d.ts +6 -0
- package/dist/desk/bindings.js +105 -0
- package/dist/desk/commands.d.ts +81 -0
- package/dist/desk/commands.js +145 -0
- package/dist/desk/default.d.ts +4 -0
- package/dist/desk/default.js +11 -0
- package/dist/desk/paths.d.ts +41 -0
- package/dist/desk/paths.js +81 -0
- package/dist/desk/registry.d.ts +17 -0
- package/dist/desk/registry.js +191 -0
- package/dist/desk/startup-error.d.ts +4 -0
- package/dist/desk/startup-error.js +7 -0
- package/dist/desk/types.d.ts +72 -0
- package/dist/desk/types.js +7 -0
- package/dist/dingtalk/bot.d.ts +18 -35
- package/dist/dingtalk/bot.js +21 -204
- package/dist/dingtalk/channel-ids.d.ts +15 -0
- package/dist/dingtalk/channel-ids.js +39 -0
- package/dist/dingtalk/config.d.ts +6 -3
- package/dist/dingtalk/config.js +13 -25
- package/dist/dingtalk/connection.d.ts +1 -0
- package/dist/dingtalk/connection.js +5 -4
- package/dist/dingtalk/conversation-meta.d.ts +10 -5
- package/dist/dingtalk/conversation-meta.js +72 -50
- package/dist/dingtalk/delivery.d.ts +30 -14
- package/dist/dingtalk/delivery.js +189 -62
- package/dist/dingtalk/inbound-media.d.ts +2 -18
- package/dist/dingtalk/inbound-media.js +0 -53
- package/dist/dingtalk/inbound.d.ts +10 -11
- package/dist/dingtalk/inbound.js +12 -19
- package/dist/dingtalk/media.d.ts +9 -11
- package/dist/dingtalk/media.js +18 -31
- package/dist/dingtalk/progress.d.ts +8 -0
- package/dist/dingtalk/progress.js +20 -0
- package/dist/dingtalk/response-mode.d.ts +13 -0
- package/dist/dingtalk/response-mode.js +31 -0
- package/dist/dingtalk/transport.d.ts +47 -0
- package/dist/dingtalk/transport.js +71 -0
- package/dist/events/commands.d.ts +7 -2
- package/dist/events/commands.js +40 -22
- package/dist/events/pre-action-error.d.ts +10 -0
- package/dist/events/pre-action-error.js +10 -0
- package/dist/events/types.d.ts +20 -2
- package/dist/events/watcher.d.ts +14 -7
- package/dist/events/watcher.js +49 -26
- package/dist/index.d.ts +4 -2
- package/dist/jobs/completion-wake.d.ts +14 -0
- package/dist/jobs/completion-wake.js +36 -0
- package/dist/jobs/manager.d.ts +27 -22
- package/dist/jobs/manager.js +58 -91
- package/dist/jobs/record.d.ts +2 -0
- package/dist/jobs/record.js +2 -0
- package/dist/log.d.ts +0 -1
- package/dist/log.js +1 -1
- package/dist/main.js +10 -3
- package/dist/memory/boundary-work.d.ts +17 -0
- package/dist/memory/boundary-work.js +162 -0
- package/dist/memory/commands.d.ts +4 -3
- package/dist/memory/commands.js +16 -16
- package/dist/memory/desk-memory-queue.d.ts +18 -0
- package/dist/memory/desk-memory-queue.js +33 -0
- package/dist/memory/index-budget.d.ts +3 -3
- package/dist/memory/index-budget.js +3 -3
- package/dist/memory/journal.d.ts +6 -6
- package/dist/memory/journal.js +14 -14
- package/dist/memory/lifecycle.d.ts +14 -21
- package/dist/memory/lifecycle.js +73 -103
- package/dist/memory/maintenance-state.d.ts +14 -15
- package/dist/memory/maintenance-state.js +43 -42
- package/dist/memory/reflect-job.d.ts +5 -4
- package/dist/memory/reflect-job.js +21 -15
- package/dist/memory/reflect.d.ts +2 -1
- package/dist/memory/reflect.js +13 -12
- package/dist/memory/render.d.ts +1 -1
- package/dist/memory/render.js +2 -2
- package/dist/memory/review-log.d.ts +4 -4
- package/dist/memory/review-log.js +5 -5
- package/dist/memory/scheduler.d.ts +12 -19
- package/dist/memory/scheduler.js +28 -64
- package/dist/memory/session-corpus.d.ts +4 -1
- package/dist/memory/session-corpus.js +70 -29
- package/dist/memory/session-search.d.ts +11 -3
- package/dist/memory/session-search.js +14 -11
- package/dist/memory/sidecar-worker.d.ts +1 -0
- package/dist/memory/sidecar-worker.js +57 -76
- package/dist/memory/source-window.d.ts +0 -6
- package/dist/memory/source-window.js +0 -20
- package/dist/memory/store.d.ts +12 -12
- package/dist/memory/store.js +44 -44
- package/dist/paths.d.ts +2 -0
- package/dist/paths.js +2 -0
- package/dist/playbooks/agent-delegation.md +16 -16
- package/dist/playbooks/background-jobs.md +5 -3
- package/dist/playbooks/event-scheduling.md +7 -5
- package/dist/playbooks/memory-and-learning.md +16 -12
- package/dist/playbooks/outbound-media.md +1 -1
- package/dist/playbooks/runtime-orientation.md +35 -23
- package/dist/playbooks/task-lead.md +13 -11
- package/dist/runtime/bootstrap.d.ts +36 -13
- package/dist/runtime/bootstrap.js +310 -138
- package/dist/runtime/channel-queue.d.ts +23 -0
- package/dist/{dingtalk → runtime}/channel-queue.js +20 -9
- package/dist/runtime/daemon-lock.d.ts +12 -0
- package/dist/runtime/daemon-lock.js +28 -0
- package/dist/runtime/desk-runtime.d.ts +131 -0
- package/dist/runtime/desk-runtime.js +402 -0
- package/dist/runtime/durable-dispatch.d.ts +11 -5
- package/dist/runtime/durable-dispatch.js +20 -8
- package/dist/runtime/inbound-images.d.ts +32 -10
- package/dist/runtime/inbound-images.js +95 -21
- package/dist/runtime/ingress-busy.d.ts +26 -0
- package/dist/runtime/ingress-busy.js +211 -0
- package/dist/runtime/ingress-commands.d.ts +7 -0
- package/dist/runtime/ingress-commands.js +162 -0
- package/dist/runtime/ingress-deps.d.ts +62 -0
- package/dist/runtime/ingress-deps.js +1 -0
- package/dist/runtime/ingress.d.ts +22 -0
- package/dist/runtime/ingress.js +376 -0
- package/dist/runtime/request-execution.d.ts +19 -0
- package/dist/runtime/request-execution.js +88 -0
- package/dist/runtime/shutdown.d.ts +13 -5
- package/dist/runtime/shutdown.js +10 -4
- package/dist/runtime/task-driver.d.ts +38 -22
- package/dist/runtime/task-driver.js +142 -116
- package/dist/runtime/task-spawn.d.ts +4 -4
- package/dist/runtime/task-spawn.js +6 -5
- package/dist/runtime/task-wake.d.ts +6 -4
- package/dist/runtime/task-wake.js +7 -7
- package/dist/runtime/transports.d.ts +32 -0
- package/dist/runtime/transports.js +101 -0
- package/dist/runtime/turn-run.d.ts +18 -9
- package/dist/runtime/turn-run.js +302 -71
- package/dist/runtime/wake-coalescing.d.ts +11 -3
- package/dist/runtime/wake-coalescing.js +10 -0
- package/dist/security/config.js +5 -10
- package/dist/security/logger.js +1 -1
- package/dist/security/path-guard.js +7 -63
- package/dist/security/types.d.ts +27 -30
- package/dist/security/types.js +0 -1
- package/dist/shared/atomic-file.d.ts +2 -0
- package/dist/shared/atomic-file.js +14 -1
- package/dist/shared/file-lock.d.ts +35 -0
- package/dist/shared/file-lock.js +195 -0
- package/dist/shared/with-timeout.d.ts +2 -0
- package/dist/shared/with-timeout.js +21 -0
- package/dist/skills/loader.d.ts +1 -1
- package/dist/skills/loader.js +2 -2
- package/dist/skills/workspace.js +2 -2
- package/dist/subagents/completion-wake.d.ts +11 -2
- package/dist/subagents/completion-wake.js +22 -9
- package/dist/subagents/discovery.js +1 -1
- package/dist/subagents/dispatch.d.ts +0 -10
- package/dist/subagents/dispatch.js +10 -28
- package/dist/subagents/external/dispatch.js +1 -1
- package/dist/subagents/external/run.d.ts +1 -1
- package/dist/subagents/external/run.js +3 -3
- package/dist/subagents/internal/run.js +1 -1
- package/dist/subagents/internal/worker.js +6 -14
- package/dist/subagents/run-archive.d.ts +11 -0
- package/dist/subagents/run-archive.js +49 -0
- package/dist/subagents/run-record.d.ts +9 -8
- package/dist/subagents/run-record.js +3 -3
- package/dist/subagents/run-registry.d.ts +5 -3
- package/dist/subagents/run-registry.js +22 -45
- package/dist/subagents/runs.d.ts +23 -13
- package/dist/subagents/runs.js +75 -82
- package/dist/subagents/task-envelope.js +9 -11
- package/dist/subagents/types.d.ts +6 -5
- package/dist/tasks/board.d.ts +1 -1
- package/dist/tasks/board.js +2 -2
- package/dist/tasks/bound-work.d.ts +2 -2
- package/dist/tasks/bound-work.js +7 -7
- package/dist/tasks/brief.d.ts +1 -1
- package/dist/tasks/brief.js +9 -9
- package/dist/tasks/commands.d.ts +11 -2
- package/dist/tasks/commands.js +66 -41
- package/dist/tasks/contract.d.ts +0 -2
- package/dist/tasks/contract.js +1 -3
- package/dist/tasks/create.d.ts +3 -1
- package/dist/tasks/create.js +10 -4
- package/dist/tasks/frontmatter.d.ts +6 -2
- package/dist/tasks/frontmatter.js +10 -10
- package/dist/tasks/loop-log.d.ts +7 -13
- package/dist/tasks/loop-log.js +14 -17
- package/dist/tasks/mutation-lock.d.ts +7 -5
- package/dist/tasks/mutation-lock.js +32 -9
- package/dist/tasks/session-path.d.ts +3 -3
- package/dist/tasks/session-path.js +5 -5
- package/dist/tasks/steer.d.ts +5 -5
- package/dist/tasks/steer.js +42 -40
- package/dist/tasks/store.d.ts +11 -11
- package/dist/tasks/store.js +32 -29
- package/dist/tools/bash.d.ts +5 -5
- package/dist/tools/bash.js +19 -18
- package/dist/tools/contract/names.d.ts +7 -0
- package/dist/tools/contract/names.js +22 -0
- package/dist/tools/edit.js +1 -1
- package/dist/tools/event-manage.d.ts +3 -1
- package/dist/tools/event-manage.js +34 -33
- package/dist/tools/glob.js +5 -5
- package/dist/tools/grep.js +2 -2
- package/dist/tools/index.d.ts +9 -8
- package/dist/tools/index.js +12 -16
- package/dist/tools/job.d.ts +4 -2
- package/dist/tools/job.js +11 -8
- package/dist/tools/memory-manage.d.ts +5 -3
- package/dist/tools/memory-manage.js +24 -24
- package/dist/tools/read.js +1 -1
- package/dist/tools/registry.d.ts +12 -8
- package/dist/tools/registry.js +13 -9
- package/dist/tools/send-media.d.ts +2 -2
- package/dist/tools/send-media.js +3 -3
- package/dist/tools/session-search.d.ts +3 -1
- package/dist/tools/session-search.js +8 -6
- package/dist/tools/subagent-manage.d.ts +2 -8
- package/dist/tools/subagent-manage.js +19 -15
- package/dist/tools/subagent.js +1 -1
- package/dist/tools/task-manage/create.js +1 -1
- package/dist/tools/task-manage/factory.js +2 -2
- package/dist/tools/task-manage/lifecycle.js +3 -1
- package/dist/tools/task-manage/log-view.js +2 -2
- package/dist/tools/task-manage/shared.js +3 -3
- package/dist/tools/task-manage/step-end.js +3 -3
- package/dist/tools/task-manage/types.d.ts +8 -3
- package/dist/tools/web-cache.d.ts +3 -3
- package/dist/tools/web-cache.js +12 -12
- package/dist/tools/web-fetch.d.ts +2 -2
- package/dist/tools/web-fetch.js +7 -7
- package/dist/tools/write-content.js +1 -1
- package/dist/tui/cli.d.ts +17 -5
- package/dist/tui/cli.js +90 -27
- package/dist/tui/client.d.ts +26 -0
- package/dist/tui/client.js +157 -0
- package/dist/tui/colors.d.ts +0 -1
- package/dist/tui/colors.js +0 -1
- package/dist/tui/commands.d.ts +10 -51
- package/dist/tui/commands.js +16 -39
- package/dist/tui/frontend.d.ts +2 -2
- package/dist/tui/frontend.js +2 -2
- package/dist/tui/pitui-frontend.d.ts +0 -2
- package/dist/tui/pitui-frontend.js +1 -6
- package/dist/tui/render.d.ts +62 -0
- package/dist/tui/render.js +123 -0
- package/dist/tui/session.d.ts +92 -0
- package/dist/tui/session.js +317 -0
- package/dist/usage/ledger.d.ts +5 -0
- package/dist/usage/ledger.js +2 -0
- package/dist/usage/render.d.ts +6 -1
- package/dist/usage/render.js +18 -13
- package/dist/webapi/auth.d.ts +13 -0
- package/dist/webapi/auth.js +29 -0
- package/dist/webapi/config.d.ts +29 -0
- package/dist/webapi/config.js +100 -0
- package/dist/webapi/errors.d.ts +39 -0
- package/dist/webapi/errors.js +32 -0
- package/dist/webapi/http.d.ts +25 -0
- package/dist/webapi/http.js +4 -0
- package/dist/webapi/ids.d.ts +9 -0
- package/dist/webapi/ids.js +20 -0
- package/dist/webapi/input.d.ts +17 -0
- package/dist/webapi/input.js +229 -0
- package/dist/webapi/items.d.ts +37 -0
- package/dist/webapi/items.js +48 -0
- package/dist/webapi/key-command.d.ts +2 -0
- package/dist/webapi/key-command.js +110 -0
- package/dist/webapi/limits.d.ts +47 -0
- package/dist/webapi/limits.js +48 -0
- package/dist/webapi/request-policy.d.ts +17 -0
- package/dist/webapi/request-policy.js +160 -0
- package/dist/webapi/response-record.d.ts +68 -0
- package/dist/webapi/response-record.js +58 -0
- package/dist/webapi/response-state.d.ts +90 -0
- package/dist/webapi/response-state.js +347 -0
- package/dist/webapi/routes/conversations.d.ts +8 -0
- package/dist/webapi/routes/conversations.js +80 -0
- package/dist/webapi/routes/models.d.ts +3 -0
- package/dist/webapi/routes/models.js +12 -0
- package/dist/webapi/routes/responses.d.ts +8 -0
- package/dist/webapi/routes/responses.js +118 -0
- package/dist/webapi/routes/websocket.d.ts +9 -0
- package/dist/webapi/routes/websocket.js +328 -0
- package/dist/webapi/server.d.ts +24 -0
- package/dist/webapi/server.js +229 -0
- package/dist/webapi/service.d.ts +130 -0
- package/dist/webapi/service.js +588 -0
- package/dist/webapi/store.d.ts +46 -0
- package/dist/webapi/store.js +245 -0
- package/dist/webapi/stream.d.ts +13 -0
- package/dist/webapi/stream.js +76 -0
- package/dist/webapi/transport.d.ts +43 -0
- package/dist/webapi/transport.js +100 -0
- package/package.json +9 -2
- package/dist/channel/channel-paths.d.ts +0 -16
- package/dist/channel/channel-paths.js +0 -53
- package/dist/channel/commands.d.ts +0 -32
- package/dist/channel/commands.js +0 -109
- package/dist/channel/project-scope-store.d.ts +0 -51
- package/dist/channel/project-scope-store.js +0 -112
- package/dist/dingtalk/channel-queue.d.ts +0 -19
- package/dist/dingtalk/handler.d.ts +0 -90
- package/dist/memory/channel-maintenance-queue.d.ts +0 -5
- package/dist/memory/channel-maintenance-queue.js +0 -8
- package/dist/runtime/task-migration.d.ts +0 -30
- package/dist/runtime/task-migration.js +0 -465
- package/dist/runtime/turn-handler.d.ts +0 -47
- package/dist/runtime/turn-handler.js +0 -318
- package/dist/security/project-scope.d.ts +0 -45
- package/dist/security/project-scope.js +0 -105
- package/dist/tui/app.d.ts +0 -16
- package/dist/tui/app.js +0 -166
- package/dist/tui/terminal-context.d.ts +0 -27
- package/dist/tui/terminal-context.js +0 -84
- package/dist/tui/terminal-media-sender.d.ts +0 -9
- package/dist/tui/terminal-media-sender.js +0 -15
- package/dist/tui/turn-controller.d.ts +0 -64
- package/dist/tui/turn-controller.js +0 -311
- /package/dist/{dingtalk/handler.js → channel/transport.js} +0 -0
|
@@ -3,7 +3,7 @@ import { parseLocalTime } from "../shared/local-time.js";
|
|
|
3
3
|
import { recencyBoostByAge, tokenizeRecallText } from "./search.js";
|
|
4
4
|
import { buildSessionCorpus } from "./session-corpus.js";
|
|
5
5
|
import { runSidecarTask } from "./sidecar-worker.js";
|
|
6
|
-
const SESSION_SEARCH_SUMMARY_SYSTEM_PROMPT = `You summarize
|
|
6
|
+
const SESSION_SEARCH_SUMMARY_SYSTEM_PROMPT = `You summarize transcript search hits from one Pipiclaw desk's conversations.
|
|
7
7
|
|
|
8
8
|
Return plain text only.
|
|
9
9
|
|
|
@@ -116,7 +116,7 @@ ${query}
|
|
|
116
116
|
Transcript hit:
|
|
117
117
|
${fallback}`,
|
|
118
118
|
timeoutMs: request.timeoutMs,
|
|
119
|
-
usageContext: request.channelId ? { channelId: request.channelId } : undefined,
|
|
119
|
+
usageContext: request.channelId ? { channelId: request.channelId, desk: request.desk } : undefined,
|
|
120
120
|
parse: (text) => text.trim(),
|
|
121
121
|
});
|
|
122
122
|
return result.output || fallback;
|
|
@@ -128,7 +128,10 @@ ${fallback}`,
|
|
|
128
128
|
function toResult(request, document, score, matches, summary) {
|
|
129
129
|
return {
|
|
130
130
|
source: document.source,
|
|
131
|
-
path: relative(request.
|
|
131
|
+
path: relative(request.deskDir, document.path) || document.path,
|
|
132
|
+
...(document.conversation
|
|
133
|
+
? { conversation: document.conversation, channel: request.describeConversation?.(document.conversation) }
|
|
134
|
+
: {}),
|
|
132
135
|
when: document.timestamp,
|
|
133
136
|
role: document.role,
|
|
134
137
|
score: Number(score.toFixed(3)),
|
|
@@ -139,11 +142,11 @@ function toResult(request, document, score, matches, summary) {
|
|
|
139
142
|
const CORPUS_CACHE_TTL_MS = 30_000;
|
|
140
143
|
const CORPUS_CACHE_MAX_CHANNELS = 8;
|
|
141
144
|
const corpusCache = new Map();
|
|
142
|
-
function corpusCacheKey(
|
|
143
|
-
return JSON.stringify([
|
|
145
|
+
function corpusCacheKey(deskDir, maxFiles, maxCharsPerChunk) {
|
|
146
|
+
return JSON.stringify([deskDir, maxFiles, maxCharsPerChunk]);
|
|
144
147
|
}
|
|
145
|
-
async function getCachedCorpus(
|
|
146
|
-
const key = corpusCacheKey(
|
|
148
|
+
async function getCachedCorpus(deskDir, maxFiles, maxCharsPerChunk) {
|
|
149
|
+
const key = corpusCacheKey(deskDir, maxFiles, maxCharsPerChunk);
|
|
147
150
|
const cached = corpusCache.get(key);
|
|
148
151
|
if (cached && Date.now() - cached.timestamp < CORPUS_CACHE_TTL_MS) {
|
|
149
152
|
corpusCache.delete(key);
|
|
@@ -151,12 +154,12 @@ async function getCachedCorpus(channelDir, maxFiles, maxCharsPerChunk) {
|
|
|
151
154
|
return cached.documents;
|
|
152
155
|
}
|
|
153
156
|
const documents = await buildSessionCorpus({
|
|
154
|
-
|
|
157
|
+
deskDir,
|
|
155
158
|
maxFiles,
|
|
156
159
|
maxCharsPerDocument: maxCharsPerChunk,
|
|
157
160
|
});
|
|
158
161
|
corpusCache.delete(key);
|
|
159
|
-
corpusCache.set(key, {
|
|
162
|
+
corpusCache.set(key, { deskDir, maxFiles, maxCharsPerChunk, documents, timestamp: Date.now() });
|
|
160
163
|
if (corpusCache.size > CORPUS_CACHE_MAX_CHANNELS) {
|
|
161
164
|
const oldestKey = corpusCache.keys().next().value;
|
|
162
165
|
if (oldestKey)
|
|
@@ -164,12 +167,12 @@ async function getCachedCorpus(channelDir, maxFiles, maxCharsPerChunk) {
|
|
|
164
167
|
}
|
|
165
168
|
return documents;
|
|
166
169
|
}
|
|
167
|
-
export async function
|
|
170
|
+
export async function searchDeskSessions(request) {
|
|
168
171
|
const limit = clampInteger(request.limit, 1, 5);
|
|
169
172
|
const maxChunks = clampInteger(request.maxChunks, 1, 500);
|
|
170
173
|
const query = request.query.trim();
|
|
171
174
|
const roleFilter = normalizeRoleFilter(request.roleFilter);
|
|
172
|
-
const documents = (await getCachedCorpus(request.
|
|
175
|
+
const documents = (await getCachedCorpus(request.deskDir, request.maxFiles, request.maxCharsPerChunk))
|
|
173
176
|
.filter((document) => roleFilter.size === 0 || roleFilter.has(document.role))
|
|
174
177
|
.sort(sortRecentDocuments)
|
|
175
178
|
.slice(0, maxChunks);
|
|
@@ -3,6 +3,7 @@ import { streamSimple } from "@earendil-works/pi-ai/compat";
|
|
|
3
3
|
import { convertToLlm } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { formatModelReference } from "../models/reference.js";
|
|
5
5
|
import { extractAssistantText } from "../shared/text-utils.js";
|
|
6
|
+
import { withAbortSignal } from "../shared/with-timeout.js";
|
|
6
7
|
import { getUsageLedger } from "../usage/ledger.js";
|
|
7
8
|
const SIDE_CAR_RETRY_DELAY_MS = 2_000;
|
|
8
9
|
const SIDE_CAR_MAX_ATTEMPTS = 2;
|
|
@@ -64,6 +65,7 @@ function recordSidecarUsage(task, message) {
|
|
|
64
65
|
return;
|
|
65
66
|
getUsageLedger().record({
|
|
66
67
|
channelId: task.usageContext?.channelId ?? "",
|
|
68
|
+
desk: task.usageContext?.desk,
|
|
67
69
|
kind: "sidecar",
|
|
68
70
|
model: formatModelReference(task.model),
|
|
69
71
|
label: task.name,
|
|
@@ -85,87 +87,56 @@ function recordSidecarUsage(task, message) {
|
|
|
85
87
|
});
|
|
86
88
|
}
|
|
87
89
|
export async function runSidecarTask(task) {
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
tools: [],
|
|
95
|
-
},
|
|
96
|
-
convertToLlm,
|
|
97
|
-
getApiKey: async () => apiKey,
|
|
98
|
-
streamFn: streamSimple,
|
|
99
|
-
});
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const signal = task.signal ? AbortSignal.any([task.signal, controller.signal]) : controller.signal;
|
|
92
|
+
const timeout = task.timeoutMs && task.timeoutMs > 0
|
|
93
|
+
? setTimeout(() => controller.abort(new SidecarTimeoutError(task.name, task.timeoutMs)), task.timeoutMs)
|
|
94
|
+
: undefined;
|
|
95
|
+
let worker;
|
|
100
96
|
const abortWorker = () => {
|
|
101
97
|
try {
|
|
102
|
-
worker
|
|
98
|
+
worker?.abort();
|
|
103
99
|
}
|
|
104
100
|
catch {
|
|
105
|
-
/*
|
|
101
|
+
/* cancellation still releases the caller */
|
|
106
102
|
}
|
|
107
103
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
104
|
+
signal.addEventListener("abort", abortWorker, { once: true });
|
|
105
|
+
try {
|
|
106
|
+
return await withAbortSignal(signal, async () => {
|
|
107
|
+
// Auth is part of the same deadline as inference. A late credential cannot start a request.
|
|
108
|
+
const apiKey = await task.resolveApiKey(task.model);
|
|
109
|
+
signal.throwIfAborted();
|
|
110
|
+
worker = new Agent({
|
|
111
|
+
initialState: { systemPrompt: task.systemPrompt, model: task.model, thinkingLevel: "off", tools: [] },
|
|
112
|
+
convertToLlm,
|
|
113
|
+
getApiKey: async () => apiKey,
|
|
114
|
+
streamFn: streamSimple,
|
|
115
|
+
});
|
|
116
|
+
await worker.prompt(task.prompt);
|
|
117
|
+
await worker.waitForIdle();
|
|
118
|
+
const lastMessage = worker.state.messages[worker.state.messages.length - 1];
|
|
119
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
120
|
+
throw new Error(`Sidecar task "${task.name}" returned no assistant message`);
|
|
121
|
+
}
|
|
121
122
|
recordSidecarUsage(task, lastMessage);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const rawText = extractAssistantText(lastMessage);
|
|
126
|
-
try {
|
|
127
|
-
return {
|
|
128
|
-
output: task.parse(rawText),
|
|
129
|
-
rawText,
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
throw new SidecarParseError(task.name, rawText, error);
|
|
134
|
-
}
|
|
135
|
-
})();
|
|
136
|
-
void runPromise.catch(() => { });
|
|
137
|
-
const blockers = [];
|
|
138
|
-
if (task.timeoutMs && task.timeoutMs > 0) {
|
|
139
|
-
blockers.push(new Promise((_, reject) => {
|
|
140
|
-
timeoutHandle = setTimeout(() => {
|
|
141
|
-
abortWorker();
|
|
142
|
-
reject(new SidecarTimeoutError(task.name, task.timeoutMs));
|
|
143
|
-
}, task.timeoutMs);
|
|
144
|
-
}));
|
|
145
|
-
}
|
|
146
|
-
if (task.signal) {
|
|
147
|
-
const signal = task.signal;
|
|
148
|
-
blockers.push(new Promise((_, reject) => {
|
|
149
|
-
const abort = () => {
|
|
150
|
-
abortWorker();
|
|
151
|
-
reject(createAbortError(task.name, signal.reason));
|
|
152
|
-
};
|
|
153
|
-
if (signal.aborted) {
|
|
154
|
-
abort();
|
|
155
|
-
return;
|
|
123
|
+
signal.throwIfAborted();
|
|
124
|
+
if (lastMessage.stopReason === "error" || lastMessage.stopReason === "aborted") {
|
|
125
|
+
throw new Error(lastMessage.errorMessage || `Sidecar task "${task.name}" failed`);
|
|
156
126
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
127
|
+
const rawText = extractAssistantText(lastMessage);
|
|
128
|
+
try {
|
|
129
|
+
return { output: task.parse(rawText), rawText };
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
throw new SidecarParseError(task.name, rawText, error);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
163
135
|
}
|
|
164
136
|
finally {
|
|
165
|
-
if (
|
|
166
|
-
clearTimeout(
|
|
167
|
-
|
|
168
|
-
removeAbortListener();
|
|
137
|
+
if (timeout)
|
|
138
|
+
clearTimeout(timeout);
|
|
139
|
+
signal.removeEventListener("abort", abortWorker);
|
|
169
140
|
}
|
|
170
141
|
}
|
|
171
142
|
/** Appends a non-empty repair hint to the original prompt, or returns it unchanged. */
|
|
@@ -178,12 +149,16 @@ export async function runRetriedSidecarTask(task) {
|
|
|
178
149
|
// this prompt instead of the original.
|
|
179
150
|
let repairedPrompt;
|
|
180
151
|
let attempt = 0;
|
|
152
|
+
const deadline = task.timeoutMs && task.timeoutMs > 0 ? Date.now() + task.timeoutMs : undefined;
|
|
181
153
|
for (;;) {
|
|
182
154
|
attempt += 1;
|
|
183
155
|
if (isExternalAbort(task)) {
|
|
184
156
|
throw createAbortError(task.name, task.signal?.reason);
|
|
185
157
|
}
|
|
186
|
-
const
|
|
158
|
+
const remaining = deadline === undefined ? task.timeoutMs : deadline - Date.now();
|
|
159
|
+
if (remaining !== undefined && remaining <= 0)
|
|
160
|
+
throw new SidecarTimeoutError(task.name, task.timeoutMs);
|
|
161
|
+
const current = { ...task, prompt: repairedPrompt ?? task.prompt, timeoutMs: remaining };
|
|
187
162
|
try {
|
|
188
163
|
return await runSidecarTask(current);
|
|
189
164
|
}
|
|
@@ -191,17 +166,23 @@ export async function runRetriedSidecarTask(task) {
|
|
|
191
166
|
// A parse failure is recoverable exactly once when the caller supplies a
|
|
192
167
|
// repair hint: retry with the nudge appended so a malformed-but-fixable
|
|
193
168
|
// output (e.g. a misspelled JSON key) gets one targeted correction pass.
|
|
194
|
-
if (error instanceof SidecarParseError &&
|
|
169
|
+
if (error instanceof SidecarParseError &&
|
|
170
|
+
attempt < SIDE_CAR_MAX_ATTEMPTS &&
|
|
171
|
+
repairedPrompt === undefined &&
|
|
172
|
+
typeof task.repair === "function") {
|
|
195
173
|
repairedPrompt = withRepairSuffix(task.prompt, task.repair(error));
|
|
196
|
-
await delay(SIDE_CAR_RETRY_DELAY_MS, task);
|
|
174
|
+
await delay(Math.min(SIDE_CAR_RETRY_DELAY_MS, Math.max(0, (deadline ?? Number.POSITIVE_INFINITY) - Date.now())), task);
|
|
197
175
|
continue;
|
|
198
176
|
}
|
|
199
177
|
// Parse failures are otherwise fatal — a blind retry would reproduce the
|
|
200
178
|
// same bad output. Transient failures get the standard retry budget.
|
|
201
|
-
if (attempt >= SIDE_CAR_MAX_ATTEMPTS ||
|
|
179
|
+
if (attempt >= SIDE_CAR_MAX_ATTEMPTS ||
|
|
180
|
+
error instanceof SidecarParseError ||
|
|
181
|
+
error instanceof SidecarTimeoutError ||
|
|
182
|
+
isExternalAbort(task)) {
|
|
202
183
|
throw error;
|
|
203
184
|
}
|
|
204
|
-
await delay(SIDE_CAR_RETRY_DELAY_MS, task);
|
|
185
|
+
await delay(Math.min(SIDE_CAR_RETRY_DELAY_MS, Math.max(0, (deadline ?? Number.POSITIVE_INFINITY) - Date.now())), task);
|
|
205
186
|
}
|
|
206
187
|
}
|
|
207
188
|
}
|
|
@@ -16,9 +16,3 @@ export declare function buildIncrementalMemorySourceWindow(options: {
|
|
|
16
16
|
sourceKind: Extract<MemorySourceKind, "idle" | "new-session" | "shutdown">;
|
|
17
17
|
fallbackMessages?: AgentMessage[];
|
|
18
18
|
}): MemorySourceWindow;
|
|
19
|
-
export declare function buildCompactionMemorySourceWindow(options: {
|
|
20
|
-
entries: SessionEntry[];
|
|
21
|
-
messagesToSummarize: AgentMessage[];
|
|
22
|
-
firstKeptEntryId?: string;
|
|
23
|
-
lastEntryId?: string;
|
|
24
|
-
}): MemorySourceWindow;
|
|
@@ -37,23 +37,3 @@ export function buildIncrementalMemorySourceWindow(options) {
|
|
|
37
37
|
windowId: createWindowId(options.sourceKind, options.lastEntryId, throughEntryId, messages),
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
-
export function buildCompactionMemorySourceWindow(options) {
|
|
41
|
-
const boundaryIndex = options.firstKeptEntryId
|
|
42
|
-
? options.entries.findIndex((entry) => entry.id === options.firstKeptEntryId)
|
|
43
|
-
: -1;
|
|
44
|
-
const coveredEntries = boundaryIndex >= 0 ? options.entries.slice(0, boundaryIndex) : [];
|
|
45
|
-
const incrementalEntries = entriesSince(coveredEntries, options.lastEntryId);
|
|
46
|
-
const throughEntryId = incrementalEntries.at(-1)?.id ?? coveredEntries.at(-1)?.id;
|
|
47
|
-
const incrementalMessages = messagesFromEntries(incrementalEntries);
|
|
48
|
-
const messages = incrementalMessages.length > 0 || options.lastEntryId !== undefined
|
|
49
|
-
? incrementalMessages
|
|
50
|
-
: [...options.messagesToSummarize];
|
|
51
|
-
return {
|
|
52
|
-
sourceKind: "compaction",
|
|
53
|
-
fromEntryId: options.lastEntryId,
|
|
54
|
-
throughEntryId,
|
|
55
|
-
entries: incrementalEntries,
|
|
56
|
-
messages,
|
|
57
|
-
windowId: createWindowId("compaction", options.lastEntryId, throughEntryId, messages),
|
|
58
|
-
};
|
|
59
|
-
}
|
package/dist/memory/store.d.ts
CHANGED
|
@@ -40,9 +40,9 @@ export interface MemoryTombstoneRecord {
|
|
|
40
40
|
*/
|
|
41
41
|
revoked?: boolean;
|
|
42
42
|
}
|
|
43
|
-
export declare function
|
|
44
|
-
export declare function
|
|
45
|
-
export declare function getMemoryEntryPath(
|
|
43
|
+
export declare function getMemoryDir(deskDir: string): string;
|
|
44
|
+
export declare function getMemoryIndexPath(deskDir: string): string;
|
|
45
|
+
export declare function getMemoryEntryPath(deskDir: string, name: string): string;
|
|
46
46
|
export declare function isValidMemoryName(name: string): boolean;
|
|
47
47
|
/** Slugify free text into a memory name; falls back to `m-<hash6>` when nothing usable remains. */
|
|
48
48
|
export declare function slugifyMemoryName(text: string): string;
|
|
@@ -53,16 +53,16 @@ export declare function parseMemoryFile(name: string, raw: string, fallbackDate:
|
|
|
53
53
|
export declare function serializeMemoryEntry(entry: MemoryEntry): string;
|
|
54
54
|
/** Test seam: drop the parse cache so a fixture rewritten in place is re-read. */
|
|
55
55
|
export declare function clearMemoryStoreCache(): void;
|
|
56
|
-
export declare function listMemoryEntries(
|
|
57
|
-
export declare function readMemoryEntry(
|
|
56
|
+
export declare function listMemoryEntries(deskDir: string): Promise<MemoryEntry[]>;
|
|
57
|
+
export declare function readMemoryEntry(deskDir: string, name: string): Promise<MemoryEntry | undefined>;
|
|
58
58
|
export declare function renderMemoryIndex(entries: MemoryEntry[]): string;
|
|
59
59
|
export declare function renderIndexLine(entry: MemoryEntry): string;
|
|
60
|
-
export declare function rebuildMemoryIndex(
|
|
61
|
-
export declare function readMemoryTombstoneRecords(
|
|
62
|
-
export declare function appendMemoryTombstoneRecord(
|
|
63
|
-
export declare function isDescriptionTombstoned(
|
|
60
|
+
export declare function rebuildMemoryIndex(deskDir: string): Promise<MemoryEntry[]>;
|
|
61
|
+
export declare function readMemoryTombstoneRecords(deskDir: string): Promise<MemoryTombstoneRecord[]>;
|
|
62
|
+
export declare function appendMemoryTombstoneRecord(deskDir: string, record: MemoryTombstoneRecord): Promise<void>;
|
|
63
|
+
export declare function isDescriptionTombstoned(deskDir: string, description: string): Promise<boolean>;
|
|
64
64
|
/** Cancel the tombstone for `description` (append-only): a user re-authorized the fact. */
|
|
65
|
-
export declare function revokeMemoryTombstone(
|
|
65
|
+
export declare function revokeMemoryTombstone(deskDir: string, description: string, today?: string): Promise<void>;
|
|
66
66
|
/**
|
|
67
67
|
* Mechanical store mutations. Policy — per-pass caps, confidence bars, user-source protection —
|
|
68
68
|
* lives in the reflect pass (spec 050, D7), never here.
|
|
@@ -105,7 +105,7 @@ export interface ApplyMemoryOpsResult {
|
|
|
105
105
|
used: string;
|
|
106
106
|
}>;
|
|
107
107
|
}
|
|
108
|
-
export declare function applyMemoryOps(
|
|
108
|
+
export declare function applyMemoryOps(deskDir: string, ops: MemoryStoreOp[], options?: {
|
|
109
109
|
today?: string;
|
|
110
110
|
}): Promise<ApplyMemoryOpsResult>;
|
|
111
111
|
/**
|
|
@@ -114,4 +114,4 @@ export declare function applyMemoryOps(channelDir: string, ops: MemoryStoreOp[],
|
|
|
114
114
|
* tokens on an entry about to disappear. No tombstone: probation lapsing means "not needed
|
|
115
115
|
* right now", not "must never be learned again" (037, D8's rationale, carried over unchanged).
|
|
116
116
|
*/
|
|
117
|
-
export declare function expireProbationaryEntries(
|
|
117
|
+
export declare function expireProbationaryEntries(deskDir: string, today?: string): Promise<string[]>;
|
package/dist/memory/store.js
CHANGED
|
@@ -13,17 +13,17 @@ export const MEMORY_TYPE_ORDER = MEMORY_TYPES;
|
|
|
13
13
|
const NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
14
14
|
const MAX_NAME_LENGTH = 60;
|
|
15
15
|
const MAX_SLUG_WORDS = 6;
|
|
16
|
-
export function
|
|
17
|
-
return join(
|
|
16
|
+
export function getMemoryDir(deskDir) {
|
|
17
|
+
return join(deskDir, "memory");
|
|
18
18
|
}
|
|
19
|
-
export function
|
|
20
|
-
return join(
|
|
19
|
+
export function getMemoryIndexPath(deskDir) {
|
|
20
|
+
return join(deskDir, "MEMORY.md");
|
|
21
21
|
}
|
|
22
|
-
export function getMemoryEntryPath(
|
|
23
|
-
return join(
|
|
22
|
+
export function getMemoryEntryPath(deskDir, name) {
|
|
23
|
+
return join(getMemoryDir(deskDir), `${name}.md`);
|
|
24
24
|
}
|
|
25
|
-
function getTombstonesPath(
|
|
26
|
-
return join(
|
|
25
|
+
function getTombstonesPath(deskDir) {
|
|
26
|
+
return join(getMemoryDir(deskDir), ".tombstones.jsonl");
|
|
27
27
|
}
|
|
28
28
|
// -------------------------------------------------------------------------------------------------
|
|
29
29
|
// Names
|
|
@@ -157,8 +157,8 @@ const fileCache = new Map();
|
|
|
157
157
|
export function clearMemoryStoreCache() {
|
|
158
158
|
fileCache.clear();
|
|
159
159
|
}
|
|
160
|
-
export async function listMemoryEntries(
|
|
161
|
-
const dir =
|
|
160
|
+
export async function listMemoryEntries(deskDir) {
|
|
161
|
+
const dir = getMemoryDir(deskDir);
|
|
162
162
|
let names;
|
|
163
163
|
try {
|
|
164
164
|
names = await readdir(dir);
|
|
@@ -199,8 +199,8 @@ export async function listMemoryEntries(channelDir) {
|
|
|
199
199
|
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
200
200
|
return entries;
|
|
201
201
|
}
|
|
202
|
-
export async function readMemoryEntry(
|
|
203
|
-
const path = getMemoryEntryPath(
|
|
202
|
+
export async function readMemoryEntry(deskDir, name) {
|
|
203
|
+
const path = getMemoryEntryPath(deskDir, name);
|
|
204
204
|
const raw = await readOptionalTextFile(path);
|
|
205
205
|
if (!raw) {
|
|
206
206
|
return undefined;
|
|
@@ -218,7 +218,7 @@ export async function readMemoryEntry(channelDir, name) {
|
|
|
218
218
|
// Index generation
|
|
219
219
|
// -------------------------------------------------------------------------------------------------
|
|
220
220
|
export function renderMemoryIndex(entries) {
|
|
221
|
-
const lines = ["#
|
|
221
|
+
const lines = ["# Desk Memory", ""];
|
|
222
222
|
for (const type of MEMORY_TYPE_ORDER) {
|
|
223
223
|
const group = entries.filter((entry) => entry.type === type).sort((a, b) => a.name.localeCompare(b.name));
|
|
224
224
|
if (group.length === 0) {
|
|
@@ -231,7 +231,7 @@ export function renderMemoryIndex(entries) {
|
|
|
231
231
|
lines.push("");
|
|
232
232
|
}
|
|
233
233
|
if (lines.length === 2) {
|
|
234
|
-
lines.push("_No stored memory in this
|
|
234
|
+
lines.push("_No stored memory in this desk yet._", "");
|
|
235
235
|
}
|
|
236
236
|
return `${lines.join("\n").trimEnd()}\n`;
|
|
237
237
|
}
|
|
@@ -239,16 +239,16 @@ export function renderIndexLine(entry) {
|
|
|
239
239
|
const marker = entry.body.trim() ? " (+)" : "";
|
|
240
240
|
return `- ${entry.name} — ${entry.description}${marker}`;
|
|
241
241
|
}
|
|
242
|
-
export async function rebuildMemoryIndex(
|
|
243
|
-
const entries = await listMemoryEntries(
|
|
244
|
-
await writeFileAtomically(
|
|
242
|
+
export async function rebuildMemoryIndex(deskDir) {
|
|
243
|
+
const entries = await listMemoryEntries(deskDir);
|
|
244
|
+
await writeFileAtomically(getMemoryIndexPath(deskDir), renderMemoryIndex(entries));
|
|
245
245
|
return entries;
|
|
246
246
|
}
|
|
247
247
|
// -------------------------------------------------------------------------------------------------
|
|
248
248
|
// Tombstones
|
|
249
249
|
// -------------------------------------------------------------------------------------------------
|
|
250
|
-
export async function readMemoryTombstoneRecords(
|
|
251
|
-
const raw = await readOptionalTextFile(getTombstonesPath(
|
|
250
|
+
export async function readMemoryTombstoneRecords(deskDir) {
|
|
251
|
+
const raw = await readOptionalTextFile(getTombstonesPath(deskDir));
|
|
252
252
|
if (!raw) {
|
|
253
253
|
return [];
|
|
254
254
|
}
|
|
@@ -269,15 +269,15 @@ export async function readMemoryTombstoneRecords(channelDir) {
|
|
|
269
269
|
}
|
|
270
270
|
return records;
|
|
271
271
|
}
|
|
272
|
-
export async function appendMemoryTombstoneRecord(
|
|
273
|
-
const path = getTombstonesPath(
|
|
274
|
-
await mkdir(
|
|
272
|
+
export async function appendMemoryTombstoneRecord(deskDir, record) {
|
|
273
|
+
const path = getTombstonesPath(deskDir);
|
|
274
|
+
await mkdir(getMemoryDir(deskDir), { recursive: true });
|
|
275
275
|
await appendFile(path, `${JSON.stringify(record)}\n`, "utf-8");
|
|
276
276
|
}
|
|
277
|
-
export async function isDescriptionTombstoned(
|
|
277
|
+
export async function isDescriptionTombstoned(deskDir, description) {
|
|
278
278
|
const hash = hashMemoryContent(description);
|
|
279
279
|
let tombstoned = false;
|
|
280
|
-
for (const record of await readMemoryTombstoneRecords(
|
|
280
|
+
for (const record of await readMemoryTombstoneRecords(deskDir)) {
|
|
281
281
|
if (record.contentHash === hash) {
|
|
282
282
|
tombstoned = record.revoked !== true;
|
|
283
283
|
}
|
|
@@ -285,15 +285,15 @@ export async function isDescriptionTombstoned(channelDir, description) {
|
|
|
285
285
|
return tombstoned;
|
|
286
286
|
}
|
|
287
287
|
/** Cancel the tombstone for `description` (append-only): a user re-authorized the fact. */
|
|
288
|
-
export async function revokeMemoryTombstone(
|
|
289
|
-
await appendMemoryTombstoneRecord(
|
|
288
|
+
export async function revokeMemoryTombstone(deskDir, description, today = localDayKey()) {
|
|
289
|
+
await appendMemoryTombstoneRecord(deskDir, {
|
|
290
290
|
contentHash: hashMemoryContent(description),
|
|
291
291
|
deletedAt: today,
|
|
292
292
|
reason: "re-authorized by explicit user save",
|
|
293
293
|
revoked: true,
|
|
294
294
|
});
|
|
295
295
|
}
|
|
296
|
-
export async function applyMemoryOps(
|
|
296
|
+
export async function applyMemoryOps(deskDir, ops, options = {}) {
|
|
297
297
|
const today = options.today ?? localDayKey();
|
|
298
298
|
const result = {
|
|
299
299
|
added: [],
|
|
@@ -308,8 +308,8 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
308
308
|
if (ops.length === 0) {
|
|
309
309
|
return result;
|
|
310
310
|
}
|
|
311
|
-
await mkdir(
|
|
312
|
-
const existing = await listMemoryEntries(
|
|
311
|
+
await mkdir(getMemoryDir(deskDir), { recursive: true });
|
|
312
|
+
const existing = await listMemoryEntries(deskDir);
|
|
313
313
|
const byName = new Map(existing.map((entry) => [entry.name, entry]));
|
|
314
314
|
const liveNames = new Set(byName.keys());
|
|
315
315
|
for (const op of ops) {
|
|
@@ -322,7 +322,7 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
322
322
|
}
|
|
323
323
|
if (entry.expires) {
|
|
324
324
|
const next = { ...entry, expires: undefined, updated: today };
|
|
325
|
-
await writeEntryFile(
|
|
325
|
+
await writeEntryFile(deskDir, next);
|
|
326
326
|
byName.set(name, next);
|
|
327
327
|
}
|
|
328
328
|
result.touched.push(name);
|
|
@@ -343,13 +343,13 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
343
343
|
// Recording the tombstone before the delete means the worst case after a failure here is
|
|
344
344
|
// the reverse — file still present, already tombstoned — which is recoverable: the file
|
|
345
345
|
// is still there to retry deleting, and it is already protected from re-learning either way.
|
|
346
|
-
await appendMemoryTombstoneRecord(
|
|
346
|
+
await appendMemoryTombstoneRecord(deskDir, {
|
|
347
347
|
name: op.name,
|
|
348
348
|
contentHash: hashMemoryContent(entry.description),
|
|
349
349
|
deletedAt: today,
|
|
350
350
|
reason: op.reason?.trim() || "deleted",
|
|
351
351
|
});
|
|
352
|
-
await rm(getMemoryEntryPath(
|
|
352
|
+
await rm(getMemoryEntryPath(deskDir, op.name), { force: true });
|
|
353
353
|
byName.delete(op.name);
|
|
354
354
|
liveNames.delete(op.name);
|
|
355
355
|
result.deleted.push(op.name);
|
|
@@ -375,7 +375,7 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
375
375
|
expires: op.expires === undefined ? entry.expires : op.expires === null ? undefined : op.expires,
|
|
376
376
|
malformed: false,
|
|
377
377
|
};
|
|
378
|
-
await writeEntryFile(
|
|
378
|
+
await writeEntryFile(deskDir, next);
|
|
379
379
|
byName.set(op.name, next);
|
|
380
380
|
result.updated.push(op.name);
|
|
381
381
|
continue;
|
|
@@ -393,7 +393,7 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
393
393
|
// the user deleted. It must not also block the user's own explicit `memory_save` (source
|
|
394
394
|
// "user") — that path is the user re-authorizing the fact, and a successful save here
|
|
395
395
|
// revokes the tombstone so the background pass stops fighting it too.
|
|
396
|
-
const tombstoned = await isDescriptionTombstoned(
|
|
396
|
+
const tombstoned = await isDescriptionTombstoned(deskDir, description);
|
|
397
397
|
if (tombstoned && op.source !== "user") {
|
|
398
398
|
result.skippedTombstone++;
|
|
399
399
|
continue;
|
|
@@ -414,20 +414,20 @@ export async function applyMemoryOps(channelDir, ops, options = {}) {
|
|
|
414
414
|
body: op.details?.trim() ?? "",
|
|
415
415
|
malformed: false,
|
|
416
416
|
};
|
|
417
|
-
await writeEntryFile(
|
|
417
|
+
await writeEntryFile(deskDir, entry);
|
|
418
418
|
if (tombstoned && op.source === "user") {
|
|
419
|
-
await revokeMemoryTombstone(
|
|
419
|
+
await revokeMemoryTombstone(deskDir, description, today);
|
|
420
420
|
}
|
|
421
421
|
byName.set(name, entry);
|
|
422
422
|
liveNames.add(name);
|
|
423
423
|
result.added.push(name);
|
|
424
424
|
}
|
|
425
|
-
await rebuildMemoryIndex(
|
|
425
|
+
await rebuildMemoryIndex(deskDir);
|
|
426
426
|
return result;
|
|
427
427
|
}
|
|
428
|
-
async function writeEntryFile(
|
|
429
|
-
await mkdir(
|
|
430
|
-
await writeFileAtomically(getMemoryEntryPath(
|
|
428
|
+
async function writeEntryFile(deskDir, entry) {
|
|
429
|
+
await mkdir(getMemoryDir(deskDir), { recursive: true });
|
|
430
|
+
await writeFileAtomically(getMemoryEntryPath(deskDir, entry.name), serializeMemoryEntry(entry));
|
|
431
431
|
}
|
|
432
432
|
/**
|
|
433
433
|
* Spec 050, D6/D8: a probationary entry nobody touched before `expires` is removed —
|
|
@@ -435,15 +435,15 @@ async function writeEntryFile(channelDir, entry) {
|
|
|
435
435
|
* tokens on an entry about to disappear. No tombstone: probation lapsing means "not needed
|
|
436
436
|
* right now", not "must never be learned again" (037, D8's rationale, carried over unchanged).
|
|
437
437
|
*/
|
|
438
|
-
export async function expireProbationaryEntries(
|
|
439
|
-
const entries = await listMemoryEntries(
|
|
438
|
+
export async function expireProbationaryEntries(deskDir, today = localDayKey()) {
|
|
439
|
+
const entries = await listMemoryEntries(deskDir);
|
|
440
440
|
const expired = entries.filter((entry) => entry.expires && entry.expires < today);
|
|
441
441
|
if (expired.length === 0) {
|
|
442
442
|
return [];
|
|
443
443
|
}
|
|
444
444
|
for (const entry of expired) {
|
|
445
|
-
await rm(getMemoryEntryPath(
|
|
445
|
+
await rm(getMemoryEntryPath(deskDir, entry.name), { force: true });
|
|
446
446
|
}
|
|
447
|
-
await rebuildMemoryIndex(
|
|
447
|
+
await rebuildMemoryIndex(deskDir);
|
|
448
448
|
return expired.map((entry) => entry.name);
|
|
449
449
|
}
|
package/dist/paths.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export declare const DEFAULT_APP_HOME_DIR: string;
|
|
|
8
8
|
export declare const APP_HOME_DIR: string;
|
|
9
9
|
export declare const WORKSPACE_DIR: string;
|
|
10
10
|
export declare const STATE_DIR: string;
|
|
11
|
+
/** Where the daemon's Web API listens on this machine (spec 061, D4): the terminal connects here. */
|
|
12
|
+
export declare const webapiSocketPath: (appHomeDir: string) => string;
|
|
11
13
|
export declare const EVENT_STATE_DIR: string;
|
|
12
14
|
export declare const EVENT_HISTORY_PATH: string;
|
|
13
15
|
export declare const LOG_STATE_DIR: string;
|
package/dist/paths.js
CHANGED
|
@@ -26,6 +26,8 @@ export const DEFAULT_APP_HOME_DIR = join(homedir(), `.${APP_NAME}`);
|
|
|
26
26
|
export const APP_HOME_DIR = process.env.PIPICLAW_HOME ?? DEFAULT_APP_HOME_DIR;
|
|
27
27
|
export const WORKSPACE_DIR = join(APP_HOME_DIR, "workspace");
|
|
28
28
|
export const STATE_DIR = join(APP_HOME_DIR, "state");
|
|
29
|
+
/** Where the daemon's Web API listens on this machine (spec 061, D4): the terminal connects here. */
|
|
30
|
+
export const webapiSocketPath = (appHomeDir) => join(appHomeDir, "state", "webapi", "api.sock");
|
|
29
31
|
export const EVENT_STATE_DIR = join(STATE_DIR, "events");
|
|
30
32
|
export const EVENT_HISTORY_PATH = join(EVENT_STATE_DIR, "history.jsonl");
|
|
31
33
|
export const LOG_STATE_DIR = join(STATE_DIR, "logs");
|