@oyasmi/pipiclaw 0.9.4 → 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 +8 -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
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type QueuedWork = () => Promise<void>;
|
|
2
|
+
/**
|
|
3
|
+
* The per-channel turn serialization point: a channel processes one inbound message at a time;
|
|
4
|
+
* later arrivals wait here. Busy state itself is owned by the runner's turn state machine
|
|
5
|
+
* (`AgentRunner.beginTurn`), not by this queue.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ChannelQueue {
|
|
8
|
+
private queue;
|
|
9
|
+
private processing;
|
|
10
|
+
private stopped;
|
|
11
|
+
enqueue(work: QueuedWork, ref?: string): void;
|
|
12
|
+
size(): number;
|
|
13
|
+
/** True only when no queued or in-flight work still owns this channel. */
|
|
14
|
+
isIdle(): boolean;
|
|
15
|
+
private processNext;
|
|
16
|
+
stop(): void;
|
|
17
|
+
clearPending(): {
|
|
18
|
+
count: number;
|
|
19
|
+
refs: string[];
|
|
20
|
+
};
|
|
21
|
+
/** Take one not-yet-started item back out by its `ref`; whether it was found. */
|
|
22
|
+
removeByRef(ref: string): boolean;
|
|
23
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The per-channel serial work queue for inbound turns (
|
|
2
|
+
* The per-channel serial work queue for inbound turns (the limits that apply to it live in
|
|
3
|
+
* `ingress.ts`).
|
|
3
4
|
*/
|
|
4
5
|
import * as log from "../log.js";
|
|
5
6
|
import { errorMessage } from "../shared/text-utils.js";
|
|
6
7
|
/**
|
|
7
|
-
* The per-channel turn serialization point: a channel processes one inbound
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* is owned by the runner's turn state machine (`AgentRunner.beginTurn`).
|
|
8
|
+
* The per-channel turn serialization point: a channel processes one inbound message at a time;
|
|
9
|
+
* later arrivals wait here. Busy state itself is owned by the runner's turn state machine
|
|
10
|
+
* (`AgentRunner.beginTurn`), not by this queue.
|
|
11
11
|
*/
|
|
12
12
|
export class ChannelQueue {
|
|
13
13
|
constructor() {
|
|
@@ -15,10 +15,10 @@ export class ChannelQueue {
|
|
|
15
15
|
this.processing = false;
|
|
16
16
|
this.stopped = false;
|
|
17
17
|
}
|
|
18
|
-
enqueue(work) {
|
|
18
|
+
enqueue(work, ref) {
|
|
19
19
|
if (this.stopped)
|
|
20
20
|
return;
|
|
21
|
-
this.queue.push(work);
|
|
21
|
+
this.queue.push({ work, ref });
|
|
22
22
|
this.processNext();
|
|
23
23
|
}
|
|
24
24
|
size() {
|
|
@@ -32,7 +32,7 @@ export class ChannelQueue {
|
|
|
32
32
|
if (this.processing || this.stopped || this.queue.length === 0)
|
|
33
33
|
return;
|
|
34
34
|
this.processing = true;
|
|
35
|
-
const work = this.queue.shift();
|
|
35
|
+
const { work } = this.queue.shift();
|
|
36
36
|
try {
|
|
37
37
|
await work();
|
|
38
38
|
}
|
|
@@ -50,8 +50,19 @@ export class ChannelQueue {
|
|
|
50
50
|
// burst of queued messages does not keep running after the user asked to halt.
|
|
51
51
|
// The in-flight item (already shifted) is unaffected; the caller aborts it.
|
|
52
52
|
clearPending() {
|
|
53
|
-
const dropped =
|
|
53
|
+
const dropped = {
|
|
54
|
+
count: this.queue.length,
|
|
55
|
+
refs: this.queue.flatMap((item) => (item.ref === undefined ? [] : [item.ref])),
|
|
56
|
+
};
|
|
54
57
|
this.queue = [];
|
|
55
58
|
return dropped;
|
|
56
59
|
}
|
|
60
|
+
/** Take one not-yet-started item back out by its `ref`; whether it was found. */
|
|
61
|
+
removeByRef(ref) {
|
|
62
|
+
const index = this.queue.findIndex((item) => item.ref === ref);
|
|
63
|
+
if (index < 0)
|
|
64
|
+
return false;
|
|
65
|
+
this.queue.splice(index, 1);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
57
68
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FileLockHandle } from "../shared/file-lock.js";
|
|
2
|
+
export declare const DAEMON_LOCK_FILE = "daemon.lock";
|
|
3
|
+
/** Take the lock if it is free; otherwise say who holds it. */
|
|
4
|
+
export declare function tryAcquireDaemonLock(appHomeDir: string): Promise<{
|
|
5
|
+
kind: "acquired";
|
|
6
|
+
handle: FileLockHandle;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "held";
|
|
9
|
+
holder: string;
|
|
10
|
+
}>;
|
|
11
|
+
/** Take the lock, or refuse to start and say who holds it. */
|
|
12
|
+
export declare function acquireDaemonLock(appHomeDir: string): Promise<FileLockHandle>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `state/daemon.lock` (spec 061, INV-11): one runtime process per app home. The daemon, or the
|
|
3
|
+
* terminal when it runs the runtime itself, holds it for its whole life.
|
|
4
|
+
*/
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { StartupRefusal } from "../desk/startup-error.js";
|
|
7
|
+
import { acquireFileLock } from "../shared/file-lock.js";
|
|
8
|
+
const lockPath = (appHomeDir) => join(appHomeDir, "state", DAEMON_LOCK_FILE);
|
|
9
|
+
export const DAEMON_LOCK_FILE = "daemon.lock";
|
|
10
|
+
/** Take the lock if it is free; otherwise say who holds it. */
|
|
11
|
+
export async function tryAcquireDaemonLock(appHomeDir) {
|
|
12
|
+
const result = await acquireFileLock(lockPath(appHomeDir));
|
|
13
|
+
if (result.kind === "acquired")
|
|
14
|
+
return result;
|
|
15
|
+
return {
|
|
16
|
+
kind: "held",
|
|
17
|
+
holder: result.heldBy
|
|
18
|
+
? `pid ${result.heldBy.pid} on ${result.heldBy.hostname} since ${result.heldBy.acquiredAt}`
|
|
19
|
+
: "another process",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Take the lock, or refuse to start and say who holds it. */
|
|
23
|
+
export async function acquireDaemonLock(appHomeDir) {
|
|
24
|
+
const result = await tryAcquireDaemonLock(appHomeDir);
|
|
25
|
+
if (result.kind === "acquired")
|
|
26
|
+
return result.handle;
|
|
27
|
+
throw new StartupRefusal(`Another Pipiclaw runtime is already using ${appHomeDir} (${result.holder}). Stop it first; if no Pipiclaw process is running, delete ${lockPath(appHomeDir)}.`);
|
|
28
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { BindOutcome } from "../desk/commands.js";
|
|
2
|
+
import type { BindingsFile, ConversationKind, CreateDeskOutcome, DeskConversation } from "../desk/types.js";
|
|
3
|
+
export type ChannelResolution = {
|
|
4
|
+
kind: "bound";
|
|
5
|
+
conversation: DeskConversation;
|
|
6
|
+
}
|
|
7
|
+
/** Bound to a desk that is missing or broken on disk: never degraded to "unbound" (D4). */
|
|
8
|
+
| {
|
|
9
|
+
kind: "unavailable";
|
|
10
|
+
desk: string;
|
|
11
|
+
reason: string;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "unbound";
|
|
14
|
+
};
|
|
15
|
+
/** The desk a resolution names, bound or not; `undefined` for a channel with no binding. */
|
|
16
|
+
export declare function resolvedDesk(resolution: ChannelResolution): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The one admission rule for a synthetic event (a wake, a scheduled occurrence): it is let in only
|
|
19
|
+
* while its channel is still bound to — and the desk is available for — the desk it was produced
|
|
20
|
+
* for (spec 060 D7, INV-5). Covers a wake queued before a restart or a rebind, and a malformed
|
|
21
|
+
* record that carries no owning desk.
|
|
22
|
+
*/
|
|
23
|
+
export declare function admitsSyntheticEvent(resolution: ChannelResolution, event: {
|
|
24
|
+
desk?: string;
|
|
25
|
+
}): boolean;
|
|
26
|
+
interface DeskWatcher {
|
|
27
|
+
start(): void;
|
|
28
|
+
stop(): void;
|
|
29
|
+
flush?(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export interface DeskRuntimeOptions {
|
|
32
|
+
workspaceDir: string;
|
|
33
|
+
/**
|
|
34
|
+
* The conversation kind a channel's transport gives it (names its `conversations/<kind>-<n>/`
|
|
35
|
+
* directory); `undefined` for an address no active transport owns.
|
|
36
|
+
*/
|
|
37
|
+
conversationKindOf: (channelId: string) => ConversationKind | undefined;
|
|
38
|
+
/** Repair interrupted turns in a desk's bound conversations; runs inside activation (D6 step 1). */
|
|
39
|
+
recover: (conversations: DeskConversation[]) => Promise<void>;
|
|
40
|
+
/** Build a desk's events watcher. It is started with the services and stopped at dormancy. */
|
|
41
|
+
createWatcher: (target: {
|
|
42
|
+
desk: string;
|
|
43
|
+
deskDir: string;
|
|
44
|
+
}) => DeskWatcher;
|
|
45
|
+
/** The transport's plain send, for background notices that pass the binding guard. */
|
|
46
|
+
send: (channelId: string, text: string) => Promise<boolean>;
|
|
47
|
+
/** Whether the channel has a turn in flight; `/desk bind` refuses then (D5 step 1). */
|
|
48
|
+
isChannelBusy: (channelId: string) => boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Drop the channel's cached runner and dispose it — its final reflect lands in the desk it was
|
|
51
|
+
* bound to (D5 step 5). Called only after the new binding is on disk.
|
|
52
|
+
*/
|
|
53
|
+
releaseRunner: (channelId: string) => Promise<void>;
|
|
54
|
+
/** A binding changed (the channel index rewrites its desk column). */
|
|
55
|
+
onBindingChanged?: (channelId: string) => void;
|
|
56
|
+
}
|
|
57
|
+
export declare class DeskRuntime {
|
|
58
|
+
private readonly options;
|
|
59
|
+
private bindings;
|
|
60
|
+
/** Every desk a binding points at, as last read from disk. */
|
|
61
|
+
private readonly records;
|
|
62
|
+
private readonly active;
|
|
63
|
+
private readonly watchers;
|
|
64
|
+
/** Activation and dormancy of one desk run in order, so interleaved calls settle on the last one. */
|
|
65
|
+
private readonly lifecycle;
|
|
66
|
+
/** Binds are read-modify-writes of the one bindings file: one at a time, whichever channel asks. */
|
|
67
|
+
private readonly bindingWrites;
|
|
68
|
+
private servicesStarted;
|
|
69
|
+
constructor(options: DeskRuntimeOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Read the bindings and every bound desk. A corrupt or unknown-version bindings file refuses
|
|
72
|
+
* startup (D4). A binding whose channel has no conversation in its desk yet — hand-edited, or
|
|
73
|
+
* left by an interrupted bind — gets one allocated now, so `resolve` never has to.
|
|
74
|
+
*/
|
|
75
|
+
load(): Promise<void>;
|
|
76
|
+
private refreshRecord;
|
|
77
|
+
resolve(channelId: string): ChannelResolution;
|
|
78
|
+
/** The producer- and admission-side check of INV-5: is this channel bound to this (available) desk? */
|
|
79
|
+
isBoundTo(channelId: string, desk: string): boolean;
|
|
80
|
+
/** The whole binding table, for `/desk list`. A copy: callers cannot change bindings through it. */
|
|
81
|
+
bindingTable(): BindingsFile["bindings"];
|
|
82
|
+
/** The desk a channel points at, available or not — for the channel index's desk column. */
|
|
83
|
+
boundDesk(channelId: string): string | undefined;
|
|
84
|
+
activeDesks(): Array<{
|
|
85
|
+
desk: string;
|
|
86
|
+
deskDir: string;
|
|
87
|
+
}>;
|
|
88
|
+
/** The memory scheduler's candidates (D12): every bound conversation of an active desk. */
|
|
89
|
+
boundConversations(): DeskConversation[];
|
|
90
|
+
/** The channel a conversation directory of this desk belongs to (for `session_search` labels). */
|
|
91
|
+
channelOfConversation(desk: string, conversation: string): string | undefined;
|
|
92
|
+
private boundChannelsOf;
|
|
93
|
+
/** Startup: activate every desk that has at least one binding. */
|
|
94
|
+
activateBound(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* D6 activation: repair the desk's bound conversations, then mark it active (the task driver and
|
|
97
|
+
* memory scheduler read the active set on every pass) and start its watcher once services run.
|
|
98
|
+
*/
|
|
99
|
+
activate(desk: string): Promise<void>;
|
|
100
|
+
/** D6 dormancy: stop the watcher and leave the background scans. Running runs and jobs are untouched. */
|
|
101
|
+
deactivate(desk: string): Promise<void>;
|
|
102
|
+
private startWatcher;
|
|
103
|
+
private stopWatcher;
|
|
104
|
+
/** `/desk new`: an empty desk, bound to nothing and therefore dormant (D5). */
|
|
105
|
+
create(name: string): Promise<CreateDeskOutcome>;
|
|
106
|
+
/** `/desk bind` from `channelId`, in D5's order. Throws only when the bindings write itself fails. */
|
|
107
|
+
bind(channelId: string, name: string): Promise<BindOutcome>;
|
|
108
|
+
/**
|
|
109
|
+
* Human ingress only: bind a channel that has no binding yet to `desk` (the sender's default).
|
|
110
|
+
* Background work and reads must never manufacture a binding.
|
|
111
|
+
*/
|
|
112
|
+
ensureDefaultBinding(channelId: string, desk: string): Promise<void>;
|
|
113
|
+
private bindNow;
|
|
114
|
+
/**
|
|
115
|
+
* The channel is gone for good (a Web API conversation was deleted): drop its binding, its
|
|
116
|
+
* runner, and — when it was the desk's last channel — the desk's activity. Its conversation
|
|
117
|
+
* directory stays in the desk as history. Whether there was a binding to drop.
|
|
118
|
+
*/
|
|
119
|
+
unbind(channelId: string): Promise<boolean>;
|
|
120
|
+
/** Start the event watchers of every active desk; later activations start theirs directly. */
|
|
121
|
+
startServices(): void;
|
|
122
|
+
/**
|
|
123
|
+
* D7's guard for every background notice that bypasses durable dispatch (run notices, stop and
|
|
124
|
+
* skip receipts, exhausted-dispatch notices): sent only while `channelId` is bound to `desk`.
|
|
125
|
+
*/
|
|
126
|
+
sendFromDesk(desk: string, channelId: string, text: string): Promise<boolean>;
|
|
127
|
+
/** Shutdown: stop every watcher (synchronously), then let `flush` drain their history. */
|
|
128
|
+
stop(): void;
|
|
129
|
+
flush(): Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
export {};
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's one authority on desks (spec 058, D6): which desk a channel is bound to, which desks
|
|
3
|
+
* are active, and how that changes. Disk I/O is `desk/registry.ts`'s; this joins it to the
|
|
4
|
+
* lifecycles that depend on it — each active desk's events watcher, the interrupted-turn repair a
|
|
5
|
+
* desk gets before its channels take traffic, and the binding guard every background notice
|
|
6
|
+
* passes through.
|
|
7
|
+
*
|
|
8
|
+
* `desk-bindings.json` is read once at startup and the in-memory copy is authoritative from then on
|
|
9
|
+
* (D4); it is written back on every change, by this process only.
|
|
10
|
+
*/
|
|
11
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { loadBindings, writeBindings } from "../desk/bindings.js";
|
|
14
|
+
import { getDeskDir, isDeskName, normalizeDeskName } from "../desk/paths.js";
|
|
15
|
+
import { allocateConversation, createDeskRecord, findConversation, readDeskRecord } from "../desk/registry.js";
|
|
16
|
+
import { StartupRefusal } from "../desk/startup-error.js";
|
|
17
|
+
import { parseScheduledEventContent } from "../events/parse.js";
|
|
18
|
+
import * as log from "../log.js";
|
|
19
|
+
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
20
|
+
import { errorMessage } from "../shared/text-utils.js";
|
|
21
|
+
import { readActiveTasks } from "../tasks/contract.js";
|
|
22
|
+
/** The desk a resolution names, bound or not; `undefined` for a channel with no binding. */
|
|
23
|
+
export function resolvedDesk(resolution) {
|
|
24
|
+
switch (resolution.kind) {
|
|
25
|
+
case "bound":
|
|
26
|
+
return resolution.conversation.desk;
|
|
27
|
+
case "unavailable":
|
|
28
|
+
return resolution.desk;
|
|
29
|
+
case "unbound":
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The one admission rule for a synthetic event (a wake, a scheduled occurrence): it is let in only
|
|
35
|
+
* while its channel is still bound to — and the desk is available for — the desk it was produced
|
|
36
|
+
* for (spec 060 D7, INV-5). Covers a wake queued before a restart or a rebind, and a malformed
|
|
37
|
+
* record that carries no owning desk.
|
|
38
|
+
*/
|
|
39
|
+
export function admitsSyntheticEvent(resolution, event) {
|
|
40
|
+
return resolution.kind === "bound" && event.desk !== undefined && resolution.conversation.desk === event.desk;
|
|
41
|
+
}
|
|
42
|
+
export class DeskRuntime {
|
|
43
|
+
constructor(options) {
|
|
44
|
+
this.options = options;
|
|
45
|
+
this.bindings = { version: 1, bindings: {} };
|
|
46
|
+
/** Every desk a binding points at, as last read from disk. */
|
|
47
|
+
this.records = new Map();
|
|
48
|
+
this.active = new Set();
|
|
49
|
+
this.watchers = new Map();
|
|
50
|
+
/** Activation and dormancy of one desk run in order, so interleaved calls settle on the last one. */
|
|
51
|
+
this.lifecycle = createSerialQueue();
|
|
52
|
+
/** Binds are read-modify-writes of the one bindings file: one at a time, whichever channel asks. */
|
|
53
|
+
this.bindingWrites = createSerialQueue();
|
|
54
|
+
this.servicesStarted = false;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Read the bindings and every bound desk. A corrupt or unknown-version bindings file refuses
|
|
58
|
+
* startup (D4). A binding whose channel has no conversation in its desk yet — hand-edited, or
|
|
59
|
+
* left by an interrupted bind — gets one allocated now, so `resolve` never has to.
|
|
60
|
+
*/
|
|
61
|
+
async load() {
|
|
62
|
+
try {
|
|
63
|
+
this.bindings = await loadBindings(this.options.workspaceDir);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new StartupRefusal(errorMessage(error));
|
|
67
|
+
}
|
|
68
|
+
for (const desk of new Set(Object.values(this.bindings.bindings).map((binding) => binding.desk))) {
|
|
69
|
+
await this.refreshRecord(desk);
|
|
70
|
+
}
|
|
71
|
+
for (const [channelId, binding] of Object.entries(this.bindings.bindings)) {
|
|
72
|
+
const record = this.records.get(binding.desk)?.record;
|
|
73
|
+
const kind = this.options.conversationKindOf(channelId);
|
|
74
|
+
if (!record || !kind || record.conversations[channelId])
|
|
75
|
+
continue;
|
|
76
|
+
await allocateConversation(this.options.workspaceDir, binding.desk, channelId, kind, new Date());
|
|
77
|
+
await this.refreshRecord(binding.desk);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async refreshRecord(desk) {
|
|
81
|
+
const summary = await readDeskRecord(this.options.workspaceDir, desk);
|
|
82
|
+
this.records.set(desk, summary);
|
|
83
|
+
if (summary.availability !== "available") {
|
|
84
|
+
log.logWarn("desk.record.unavailable", "Bound desk is unavailable", {
|
|
85
|
+
reason: summary.reason,
|
|
86
|
+
fields: { desk },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return summary;
|
|
90
|
+
}
|
|
91
|
+
resolve(channelId) {
|
|
92
|
+
const binding = this.bindings.bindings[channelId];
|
|
93
|
+
if (!binding)
|
|
94
|
+
return { kind: "unbound" };
|
|
95
|
+
const summary = this.records.get(binding.desk);
|
|
96
|
+
if (!summary?.record) {
|
|
97
|
+
return { kind: "unavailable", desk: binding.desk, reason: summary?.reason ?? "desk not loaded" };
|
|
98
|
+
}
|
|
99
|
+
const conversation = findConversation(summary.record, summary.deskDir, channelId);
|
|
100
|
+
if (!conversation) {
|
|
101
|
+
return { kind: "unavailable", desk: binding.desk, reason: `no conversation registered for ${channelId}` };
|
|
102
|
+
}
|
|
103
|
+
return { kind: "bound", conversation };
|
|
104
|
+
}
|
|
105
|
+
/** The producer- and admission-side check of INV-5: is this channel bound to this (available) desk? */
|
|
106
|
+
isBoundTo(channelId, desk) {
|
|
107
|
+
const resolution = this.resolve(channelId);
|
|
108
|
+
return resolution.kind === "bound" && resolution.conversation.desk === desk;
|
|
109
|
+
}
|
|
110
|
+
/** The whole binding table, for `/desk list`. A copy: callers cannot change bindings through it. */
|
|
111
|
+
bindingTable() {
|
|
112
|
+
return { ...this.bindings.bindings };
|
|
113
|
+
}
|
|
114
|
+
/** The desk a channel points at, available or not — for the channel index's desk column. */
|
|
115
|
+
boundDesk(channelId) {
|
|
116
|
+
return this.bindings.bindings[channelId]?.desk;
|
|
117
|
+
}
|
|
118
|
+
activeDesks() {
|
|
119
|
+
return [...this.active].sort().map((desk) => ({ desk, deskDir: getDeskDir(this.options.workspaceDir, desk) }));
|
|
120
|
+
}
|
|
121
|
+
/** The memory scheduler's candidates (D12): every bound conversation of an active desk. */
|
|
122
|
+
boundConversations() {
|
|
123
|
+
const conversations = [];
|
|
124
|
+
for (const channelId of Object.keys(this.bindings.bindings).sort()) {
|
|
125
|
+
const resolution = this.resolve(channelId);
|
|
126
|
+
if (resolution.kind === "bound" && this.active.has(resolution.conversation.desk)) {
|
|
127
|
+
conversations.push(resolution.conversation);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return conversations;
|
|
131
|
+
}
|
|
132
|
+
/** The channel a conversation directory of this desk belongs to (for `session_search` labels). */
|
|
133
|
+
channelOfConversation(desk, conversation) {
|
|
134
|
+
const record = this.records.get(desk)?.record;
|
|
135
|
+
if (!record)
|
|
136
|
+
return undefined;
|
|
137
|
+
return Object.entries(record.conversations).find(([, entry]) => entry.dir === conversation)?.[0];
|
|
138
|
+
}
|
|
139
|
+
boundChannelsOf(desk) {
|
|
140
|
+
return Object.entries(this.bindings.bindings)
|
|
141
|
+
.filter(([, binding]) => binding.desk === desk)
|
|
142
|
+
.map(([channelId]) => channelId);
|
|
143
|
+
}
|
|
144
|
+
/** Startup: activate every desk that has at least one binding. */
|
|
145
|
+
async activateBound() {
|
|
146
|
+
for (const desk of new Set(Object.values(this.bindings.bindings).map((binding) => binding.desk))) {
|
|
147
|
+
await this.activate(desk);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* D6 activation: repair the desk's bound conversations, then mark it active (the task driver and
|
|
152
|
+
* memory scheduler read the active set on every pass) and start its watcher once services run.
|
|
153
|
+
*/
|
|
154
|
+
activate(desk) {
|
|
155
|
+
return this.lifecycle.run(desk, async () => {
|
|
156
|
+
if (this.active.has(desk))
|
|
157
|
+
return;
|
|
158
|
+
const summary = await this.refreshRecord(desk);
|
|
159
|
+
if (!summary.record)
|
|
160
|
+
return;
|
|
161
|
+
const conversations = this.boundChannelsOf(desk).flatMap((channelId) => {
|
|
162
|
+
const resolution = this.resolve(channelId);
|
|
163
|
+
return resolution.kind === "bound" ? [resolution.conversation] : [];
|
|
164
|
+
});
|
|
165
|
+
try {
|
|
166
|
+
await this.options.recover(conversations);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
log.logWarn("desk.activate.recover_failed", "Interrupted-turn repair failed during activation", {
|
|
170
|
+
reason: errorMessage(error),
|
|
171
|
+
fields: { desk },
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
this.active.add(desk);
|
|
175
|
+
if (this.servicesStarted)
|
|
176
|
+
this.startWatcher(desk);
|
|
177
|
+
log.logInfoEvent("desk.activated", "Desk activated", { fields: { desk, channels: conversations.length } });
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** D6 dormancy: stop the watcher and leave the background scans. Running runs and jobs are untouched. */
|
|
181
|
+
deactivate(desk) {
|
|
182
|
+
return this.lifecycle.run(desk, async () => {
|
|
183
|
+
if (!this.active.has(desk))
|
|
184
|
+
return;
|
|
185
|
+
await this.stopWatcher(desk);
|
|
186
|
+
this.active.delete(desk);
|
|
187
|
+
log.logInfoEvent("desk.deactivated", "Desk went dormant: no bound channel", { fields: { desk } });
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
startWatcher(desk) {
|
|
191
|
+
if (this.watchers.has(desk))
|
|
192
|
+
return;
|
|
193
|
+
const watcher = this.options.createWatcher({ desk, deskDir: getDeskDir(this.options.workspaceDir, desk) });
|
|
194
|
+
this.watchers.set(desk, watcher);
|
|
195
|
+
watcher.start();
|
|
196
|
+
}
|
|
197
|
+
async stopWatcher(desk) {
|
|
198
|
+
const watcher = this.watchers.get(desk);
|
|
199
|
+
if (!watcher)
|
|
200
|
+
return;
|
|
201
|
+
this.watchers.delete(desk);
|
|
202
|
+
watcher.stop();
|
|
203
|
+
await watcher.flush?.().catch(() => undefined);
|
|
204
|
+
}
|
|
205
|
+
/** `/desk new`: an empty desk, bound to nothing and therefore dormant (D5). */
|
|
206
|
+
create(name) {
|
|
207
|
+
return Promise.resolve(createDeskRecord(this.options.workspaceDir, normalizeDeskName(name), new Date()));
|
|
208
|
+
}
|
|
209
|
+
/** `/desk bind` from `channelId`, in D5's order. Throws only when the bindings write itself fails. */
|
|
210
|
+
bind(channelId, name) {
|
|
211
|
+
return this.bindingWrites.run("bindings", () => this.bindNow(channelId, normalizeDeskName(name)));
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Human ingress only: bind a channel that has no binding yet to `desk` (the sender's default).
|
|
215
|
+
* Background work and reads must never manufacture a binding.
|
|
216
|
+
*/
|
|
217
|
+
async ensureDefaultBinding(channelId, desk) {
|
|
218
|
+
if (this.bindings.bindings[channelId])
|
|
219
|
+
return;
|
|
220
|
+
await this.bindingWrites.run("bindings", async () => {
|
|
221
|
+
// Another first message or an explicit bind may have committed while this one waited.
|
|
222
|
+
if (this.bindings.bindings[channelId])
|
|
223
|
+
return;
|
|
224
|
+
const outcome = await this.bindNow(channelId, desk);
|
|
225
|
+
// The channel stays unbound and every message gets the no-desk reply. A sender's default
|
|
226
|
+
// desk that does not exist is an operator's mistake (a Web API key's first desk): say so.
|
|
227
|
+
if (outcome.kind !== "bound") {
|
|
228
|
+
log.logWarn("desk.bind.default_failed", "Default desk binding failed", {
|
|
229
|
+
channelId,
|
|
230
|
+
reason: outcome.kind,
|
|
231
|
+
fields: { desk },
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async bindNow(channelId, desk) {
|
|
237
|
+
const kind = this.options.conversationKindOf(channelId);
|
|
238
|
+
if (!kind)
|
|
239
|
+
throw new Error(`Not a channel id: ${channelId}`);
|
|
240
|
+
// 1. Validate. A desk that went missing or broken since startup is caught here, not later.
|
|
241
|
+
if (!isDeskName(desk))
|
|
242
|
+
return { kind: "invalid", desk };
|
|
243
|
+
const summary = await readDeskRecord(this.options.workspaceDir, desk);
|
|
244
|
+
if (summary.availability === "missing")
|
|
245
|
+
return { kind: "unknown", desk };
|
|
246
|
+
if (!summary.record)
|
|
247
|
+
return { kind: "unavailable", desk };
|
|
248
|
+
if (this.options.isChannelBusy(channelId))
|
|
249
|
+
return { kind: "busy", desk };
|
|
250
|
+
const previous = this.bindings.bindings[channelId]?.desk;
|
|
251
|
+
if (previous === desk)
|
|
252
|
+
return { kind: "same", desk };
|
|
253
|
+
this.records.set(desk, summary);
|
|
254
|
+
// 2–3. Give the channel its conversation in the target, then commit the binding. Until the
|
|
255
|
+
// write succeeds nothing has changed for the channel and nothing in the target has started.
|
|
256
|
+
const now = new Date();
|
|
257
|
+
await allocateConversation(this.options.workspaceDir, desk, channelId, kind, now);
|
|
258
|
+
await this.refreshRecord(desk);
|
|
259
|
+
const next = {
|
|
260
|
+
...this.bindings,
|
|
261
|
+
bindings: { ...this.bindings.bindings, [channelId]: { desk, boundAt: now.toISOString() } },
|
|
262
|
+
};
|
|
263
|
+
await writeBindings(this.options.workspaceDir, next);
|
|
264
|
+
this.bindings = next;
|
|
265
|
+
log.logInfoEvent("desk.bound", "Channel bound to desk", { channelId, fields: { desk, previousDesk: previous } });
|
|
266
|
+
this.options.onBindingChanged?.(channelId);
|
|
267
|
+
// 4. Only now may the target produce work: its watcher's startup scan recovers overdue
|
|
268
|
+
// one-shots for this channel, which it would skip as `channel_not_bound` before the commit.
|
|
269
|
+
// The channel queue holds this channel's next message until `/desk bind` returns, so the
|
|
270
|
+
// activation's repair still runs before any turn opens the conversation. A failure here
|
|
271
|
+
// cannot undo the committed binding; the next restart activates the desk again.
|
|
272
|
+
try {
|
|
273
|
+
await this.activate(desk);
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
log.logWarn("desk.bind.activate_failed", "Activating the desk after a bind failed", {
|
|
277
|
+
channelId,
|
|
278
|
+
reason: errorMessage(error),
|
|
279
|
+
fields: { desk },
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
// 5. The old runner goes; a failure here cannot undo the committed binding.
|
|
283
|
+
try {
|
|
284
|
+
await this.options.releaseRunner(channelId);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
log.logWarn("desk.bind.release_failed", "Releasing the previous runner after a bind failed", {
|
|
288
|
+
channelId,
|
|
289
|
+
reason: errorMessage(error),
|
|
290
|
+
fields: { desk, previousDesk: previous },
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
// 6. The old desk: dormant if it lost its last channel, otherwise report what is now suspended.
|
|
294
|
+
if (!previous)
|
|
295
|
+
return { kind: "bound", desk };
|
|
296
|
+
if (this.boundChannelsOf(previous).length === 0) {
|
|
297
|
+
await this.deactivate(previous);
|
|
298
|
+
return { kind: "bound", desk, previousDesk: previous, previousDeskDormant: true };
|
|
299
|
+
}
|
|
300
|
+
const suspended = await countDeliveringTo(getDeskDir(this.options.workspaceDir, previous), channelId);
|
|
301
|
+
return {
|
|
302
|
+
kind: "bound",
|
|
303
|
+
desk,
|
|
304
|
+
previousDesk: previous,
|
|
305
|
+
suspendedTasks: suspended.tasks,
|
|
306
|
+
suspendedEvents: suspended.events,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The channel is gone for good (a Web API conversation was deleted): drop its binding, its
|
|
311
|
+
* runner, and — when it was the desk's last channel — the desk's activity. Its conversation
|
|
312
|
+
* directory stays in the desk as history. Whether there was a binding to drop.
|
|
313
|
+
*/
|
|
314
|
+
unbind(channelId) {
|
|
315
|
+
return this.bindingWrites.run("bindings", async () => {
|
|
316
|
+
const previous = this.bindings.bindings[channelId]?.desk;
|
|
317
|
+
if (previous === undefined)
|
|
318
|
+
return false;
|
|
319
|
+
const { [channelId]: _dropped, ...rest } = this.bindings.bindings;
|
|
320
|
+
const next = { ...this.bindings, bindings: rest };
|
|
321
|
+
await writeBindings(this.options.workspaceDir, next);
|
|
322
|
+
this.bindings = next;
|
|
323
|
+
log.logInfoEvent("desk.unbound", "Channel unbound from desk", { channelId, fields: { desk: previous } });
|
|
324
|
+
this.options.onBindingChanged?.(channelId);
|
|
325
|
+
try {
|
|
326
|
+
await this.options.releaseRunner(channelId);
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
log.logWarn("desk.unbind.release_failed", "Releasing the runner of an unbound channel failed", {
|
|
330
|
+
channelId,
|
|
331
|
+
reason: errorMessage(error),
|
|
332
|
+
fields: { desk: previous },
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
if (this.boundChannelsOf(previous).length === 0)
|
|
336
|
+
await this.deactivate(previous);
|
|
337
|
+
return true;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/** Start the event watchers of every active desk; later activations start theirs directly. */
|
|
341
|
+
startServices() {
|
|
342
|
+
this.servicesStarted = true;
|
|
343
|
+
for (const desk of this.active)
|
|
344
|
+
this.startWatcher(desk);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* D7's guard for every background notice that bypasses durable dispatch (run notices, stop and
|
|
348
|
+
* skip receipts, exhausted-dispatch notices): sent only while `channelId` is bound to `desk`.
|
|
349
|
+
*/
|
|
350
|
+
async sendFromDesk(desk, channelId, text) {
|
|
351
|
+
if (!this.isBoundTo(channelId, desk)) {
|
|
352
|
+
log.logInfoEvent("desk.notice.dropped", "Background notice dropped: channel not bound to its desk", {
|
|
353
|
+
channelId,
|
|
354
|
+
fields: { desk },
|
|
355
|
+
});
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
return this.options.send(channelId, text);
|
|
359
|
+
}
|
|
360
|
+
/** Shutdown: stop every watcher (synchronously), then let `flush` drain their history. */
|
|
361
|
+
stop() {
|
|
362
|
+
this.servicesStarted = false;
|
|
363
|
+
for (const watcher of this.watchers.values())
|
|
364
|
+
watcher.stop();
|
|
365
|
+
}
|
|
366
|
+
async flush() {
|
|
367
|
+
await Promise.all([...this.watchers.values()].map((watcher) => watcher.flush?.() ?? Promise.resolve()));
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Open tasks and events in a desk whose delivery channel is `channelId` — what a rebind away from
|
|
372
|
+
* the desk suspends (D8). Read-only, and best effort: an unreadable file is simply not counted.
|
|
373
|
+
*/
|
|
374
|
+
async function countDeliveringTo(deskDir, channelId) {
|
|
375
|
+
let tasks = 0;
|
|
376
|
+
try {
|
|
377
|
+
tasks = (await readActiveTasks(join(deskDir, "tasks"))).filter((entry) => entry.fields.channel === channelId).length;
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
// No readable tasks directory: nothing suspended there.
|
|
381
|
+
}
|
|
382
|
+
let events = 0;
|
|
383
|
+
const eventsDir = join(deskDir, "events");
|
|
384
|
+
let filenames = [];
|
|
385
|
+
try {
|
|
386
|
+
filenames = (await readdir(eventsDir)).filter((filename) => filename.endsWith(".json"));
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// No events directory.
|
|
390
|
+
}
|
|
391
|
+
for (const filename of filenames) {
|
|
392
|
+
try {
|
|
393
|
+
const event = parseScheduledEventContent(await readFile(join(eventsDir, filename), "utf8"), filename);
|
|
394
|
+
if (event.channelId === channelId)
|
|
395
|
+
events++;
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
// An invalid definition never fires anyway.
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return { tasks, events };
|
|
402
|
+
}
|