@oyasmi/pipiclaw 0.9.5 → 0.9.6-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 +300 -142
- package/README.md +295 -140
- package/dist/agent/builtin-commands.d.ts +1 -1
- package/dist/agent/builtin-commands.js +1 -1
- package/dist/agent/channel-runner.d.ts +4 -3
- package/dist/agent/channel-runner.js +17 -28
- package/dist/agent/maintenance-context.js +2 -2
- package/dist/agent/prompt/builder.d.ts +1 -1
- package/dist/agent/prompt/builder.js +1 -1
- package/dist/agent/prompt/extension.d.ts +1 -1
- package/dist/agent/prompt/extension.js +1 -1
- package/dist/agent/prompt/manifest.d.ts +6 -1
- package/dist/agent/prompt/manifest.js +12 -5
- package/dist/agent/prompt/resources.d.ts +0 -3
- package/dist/agent/prompt/resources.js +0 -6
- package/dist/agent/prompt/sections.d.ts +2 -2
- package/dist/agent/prompt/sections.js +36 -26
- package/dist/agent/prompt/types.d.ts +3 -2
- package/dist/agent/runner-factory.js +1 -1
- package/dist/agent/session/command-extension.js +3 -1
- package/dist/agent/session/compaction-summary.js +3 -6
- package/dist/agent/session/compaction.d.ts +2 -0
- package/dist/agent/session/compaction.js +22 -14
- package/dist/agent/session/event-guards.d.ts +2 -1
- package/dist/agent/session/event-guards.js +6 -0
- package/dist/agent/session/model-selection.d.ts +2 -1
- package/dist/agent/session/model-selection.js +5 -5
- package/dist/agent/session/session-events.js +31 -4
- package/dist/agent/session/session-host.d.ts +5 -3
- package/dist/agent/session/session-host.js +20 -16
- package/dist/agent/status-render.js +3 -0
- package/dist/agent/stop-watchdog.js +5 -1
- package/dist/agent/turn/task-digest.d.ts +2 -5
- package/dist/agent/turn/task-digest.js +25 -45
- package/dist/agent/turn/turn-context.js +4 -6
- package/dist/agent/turn/turn-error.d.ts +1 -1
- package/dist/agent/turn/turn-error.js +9 -0
- package/dist/agent/turn/turn-pipeline.d.ts +10 -5
- package/dist/agent/turn/turn-pipeline.js +36 -7
- package/dist/agent/turn/turn-prompt.d.ts +2 -0
- package/dist/agent/turn/turn-prompt.js +2 -1
- package/dist/agent/turn/turn-recovery.js +4 -1
- package/dist/agent/turn/turn-state.d.ts +2 -0
- package/dist/agent/turn/turn-state.js +2 -1
- package/dist/agent/types.d.ts +23 -3
- package/dist/agent/types.js +2 -0
- package/dist/app/auth-command.d.ts +25 -0
- package/dist/app/auth-command.js +200 -67
- package/dist/app/home.d.ts +7 -8
- package/dist/app/home.js +143 -56
- package/dist/channel/attachments.d.ts +4 -0
- package/dist/channel/attachments.js +4 -0
- package/dist/channel/channel-context.d.ts +6 -0
- package/dist/channel/config.d.ts +3 -0
- package/dist/channel/config.js +11 -5
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +42 -10
- package/dist/commands/router.js +5 -1
- package/dist/desk/commands.d.ts +44 -5
- package/dist/desk/commands.js +162 -28
- package/dist/desk/paths.d.ts +6 -0
- package/dist/desk/paths.js +21 -1
- package/dist/desk/registry.d.ts +17 -2
- package/dist/desk/registry.js +122 -12
- package/dist/desk/types.d.ts +83 -1
- package/dist/dingtalk/bot.d.ts +1 -1
- package/dist/dingtalk/bot.js +4 -3
- package/dist/dingtalk/config.js +10 -10
- package/dist/dingtalk/delivery.js +15 -8
- package/dist/dingtalk/inbound-media.d.ts +36 -14
- package/dist/dingtalk/inbound-media.js +71 -12
- package/dist/dingtalk/inbound.d.ts +9 -2
- package/dist/dingtalk/inbound.js +17 -2
- package/dist/dingtalk/media.d.ts +4 -10
- package/dist/dingtalk/media.js +58 -32
- package/dist/events/commands.d.ts +2 -0
- package/dist/events/commands.js +13 -8
- package/dist/events/history.d.ts +9 -0
- package/dist/events/history.js +66 -0
- package/dist/events/parse.js +7 -3
- package/dist/events/types.d.ts +3 -0
- package/dist/events/watcher.d.ts +4 -8
- package/dist/events/watcher.js +53 -58
- package/dist/jobs/kill.d.ts +23 -0
- package/dist/jobs/kill.js +65 -0
- package/dist/jobs/manager.d.ts +19 -18
- package/dist/jobs/manager.js +36 -103
- package/dist/main.js +15 -3
- package/dist/memory/boundary-work.d.ts +2 -1
- package/dist/memory/boundary-work.js +1 -1
- package/dist/memory/lifecycle.d.ts +2 -1
- package/dist/memory/lifecycle.js +2 -2
- package/dist/memory/reflect-job.d.ts +2 -1
- package/dist/memory/reflect-job.js +1 -1
- package/dist/memory/reflect.d.ts +2 -1
- package/dist/memory/reflect.js +25 -4
- package/dist/memory/scheduler.d.ts +2 -1
- package/dist/memory/scheduler.js +1 -1
- package/dist/memory/session-corpus.d.ts +1 -1
- package/dist/memory/session-corpus.js +76 -1
- package/dist/memory/session-search.d.ts +2 -1
- package/dist/memory/session-search.js +14 -12
- package/dist/memory/sidecar-worker.d.ts +2 -1
- package/dist/memory/sidecar-worker.js +2 -4
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +19 -9
- package/dist/models/credential-work.d.ts +7 -0
- package/dist/models/credential-work.js +36 -0
- package/dist/models/login-ui.js +4 -4
- package/dist/models/provider-login.js +7 -6
- package/dist/models/requests.d.ts +8 -0
- package/dist/models/requests.js +69 -0
- package/dist/models/runtime.d.ts +3 -1
- package/dist/models/runtime.js +9 -2
- package/dist/playbooks/agent-delegation.md +20 -45
- package/dist/playbooks/background-jobs.md +1 -1
- package/dist/playbooks/catalog.js +1 -1
- package/dist/playbooks/delegation-inline.md +25 -0
- package/dist/playbooks/delegation-recovery.md +28 -0
- package/dist/playbooks/event-scheduling.md +18 -37
- package/dist/playbooks/event-sensors.md +26 -0
- package/dist/playbooks/event-task-template.md +22 -0
- package/dist/playbooks/learning-and-skills.md +26 -0
- package/dist/playbooks/memory-operations.md +36 -0
- package/dist/playbooks/outbound-media.md +2 -2
- package/dist/playbooks/runtime-files.md +29 -0
- package/dist/playbooks/runtime-operations.md +25 -0
- package/dist/playbooks/runtime-orientation.md +18 -53
- package/dist/playbooks/task-lead.md +22 -68
- package/dist/playbooks/task-manage.md +38 -0
- package/dist/playbooks/task-recovery.md +24 -0
- package/dist/playbooks/task-verify.md +22 -0
- package/dist/runtime/bootstrap.d.ts +10 -0
- package/dist/runtime/bootstrap.js +93 -29
- package/dist/runtime/daemon-lock.js +3 -3
- package/dist/runtime/desk-runtime.d.ts +40 -7
- package/dist/runtime/desk-runtime.js +330 -44
- package/dist/runtime/durable-dispatch.d.ts +14 -2
- package/dist/runtime/durable-dispatch.js +207 -65
- package/dist/runtime/ingress-busy.js +17 -8
- package/dist/runtime/ingress-commands.js +24 -4
- package/dist/runtime/ingress-deps.d.ts +4 -1
- package/dist/runtime/ingress.js +6 -1
- package/dist/runtime/request-execution.js +7 -6
- package/dist/runtime/shutdown.d.ts +4 -1
- package/dist/runtime/shutdown.js +88 -35
- package/dist/runtime/task-close.d.ts +1 -1
- package/dist/runtime/task-close.js +6 -3
- package/dist/runtime/task-control.d.ts +2 -3
- package/dist/runtime/task-control.js +38 -11
- package/dist/runtime/task-input.js +1 -1
- package/dist/runtime/task-lane-types.d.ts +12 -2
- package/dist/runtime/task-lane-types.js +9 -0
- package/dist/runtime/task-lane.d.ts +2 -0
- package/dist/runtime/task-lane.js +38 -20
- package/dist/runtime/task-step.d.ts +3 -3
- package/dist/runtime/task-step.js +34 -16
- package/dist/runtime/transports.js +6 -5
- package/dist/runtime/turn-run.js +103 -66
- package/dist/security/command-guard.d.ts +1 -1
- package/dist/security/command-guard.js +259 -314
- package/dist/security/path-guard.js +48 -7
- package/dist/security/shell-parse.d.ts +10 -0
- package/dist/security/shell-parse.js +224 -0
- package/dist/security/types.d.ts +2 -0
- package/dist/subagents/commands.d.ts +1 -1
- package/dist/subagents/commands.js +14 -5
- package/dist/subagents/completion-wake.js +6 -0
- package/dist/subagents/detached-discovery.d.ts +1 -1
- package/dist/subagents/discovery.d.ts +18 -199
- package/dist/subagents/discovery.js +35 -289
- package/dist/subagents/dispatch.d.ts +1 -2
- package/dist/subagents/dispatch.js +50 -29
- package/dist/subagents/external/antigravity-cli.d.ts +2 -0
- package/dist/subagents/external/antigravity-cli.js +517 -0
- package/dist/subagents/external/claude-code.js +8 -17
- package/dist/subagents/external/codex-cli.js +8 -18
- package/dist/subagents/external/cursor-cli.js +34 -31
- package/dist/subagents/external/dispatch.d.ts +21 -7
- package/dist/subagents/external/dispatch.js +146 -12
- package/dist/subagents/external/exec.js +3 -0
- package/dist/subagents/external/harness.d.ts +57 -33
- package/dist/subagents/external/harness.js +69 -40
- package/dist/subagents/external/reconciliation.js +1 -0
- package/dist/subagents/external/registry.js +4 -1
- package/dist/subagents/external/role-warnings.d.ts +4 -13
- package/dist/subagents/external/role-warnings.js +4 -28
- package/dist/subagents/external/run.d.ts +1 -2
- package/dist/subagents/external/run.js +16 -26
- package/dist/subagents/external/settlement.d.ts +2 -0
- package/dist/subagents/external/settlement.js +13 -3
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/internal/outcome.d.ts +1 -2
- package/dist/subagents/internal/outcome.js +19 -12
- package/dist/subagents/internal/provider-retry.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.js +2 -4
- package/dist/subagents/internal/run.d.ts +2 -3
- package/dist/subagents/internal/run.js +27 -4
- package/dist/subagents/internal/state.d.ts +2 -5
- package/dist/subagents/internal/state.js +1 -2
- package/dist/subagents/internal/worker.d.ts +1 -2
- package/dist/subagents/internal/worker.js +20 -10
- package/dist/subagents/resolve.d.ts +56 -0
- package/dist/subagents/resolve.js +202 -0
- package/dist/subagents/result.d.ts +6 -4
- package/dist/subagents/result.js +39 -6
- package/dist/subagents/run-record.d.ts +54 -2
- package/dist/subagents/run-record.js +7 -0
- package/dist/subagents/runs.d.ts +6 -31
- package/dist/subagents/runs.js +26 -31
- package/dist/subagents/task-envelope.d.ts +1 -2
- package/dist/subagents/task-envelope.js +13 -4
- package/dist/subagents/task-settlement.js +1 -0
- package/dist/subagents/types.d.ts +105 -6
- package/dist/subagents/types.js +16 -1
- package/dist/subagents/workspace-lease.d.ts +41 -24
- package/dist/subagents/workspace-lease.js +159 -24
- package/dist/subagents/worktree.d.ts +52 -0
- package/dist/subagents/worktree.js +363 -0
- package/dist/tasks/archive.js +4 -0
- package/dist/tasks/brief.d.ts +5 -1
- package/dist/tasks/brief.js +19 -5
- package/dist/tasks/commands.d.ts +6 -1
- package/dist/tasks/commands.js +32 -7
- package/dist/tasks/control.d.ts +11 -0
- package/dist/tasks/delivery.d.ts +2 -0
- package/dist/tasks/delivery.js +141 -21
- package/dist/tasks/loop-log.d.ts +12 -0
- package/dist/tasks/loop-log.js +10 -1
- package/dist/tasks/receipts.d.ts +7 -3
- package/dist/tasks/receipts.js +34 -5
- package/dist/tools/bash.d.ts +3 -0
- package/dist/tools/bash.js +29 -15
- package/dist/tools/contract/presentation.js +7 -5
- package/dist/tools/edit-diff.d.ts +11 -0
- package/dist/tools/edit-diff.js +148 -0
- package/dist/tools/edit-match.d.ts +18 -0
- package/dist/tools/edit-match.js +242 -0
- package/dist/tools/edit.d.ts +10 -2
- package/dist/tools/edit.js +91 -361
- package/dist/tools/event-manage.d.ts +2 -1
- package/dist/tools/event-manage.js +46 -39
- package/dist/tools/file-observations.d.ts +9 -0
- package/dist/tools/file-observations.js +50 -0
- package/dist/tools/glob-match.d.ts +3 -1
- package/dist/tools/glob-match.js +40 -17
- package/dist/tools/glob.d.ts +1 -0
- package/dist/tools/glob.js +39 -19
- package/dist/tools/grep-command.d.ts +13 -0
- package/dist/tools/grep-command.js +77 -0
- package/dist/tools/grep.js +96 -201
- package/dist/tools/index.d.ts +6 -2
- package/dist/tools/index.js +9 -4
- package/dist/tools/job.js +12 -11
- package/dist/tools/read.d.ts +2 -0
- package/dist/tools/read.js +10 -2
- package/dist/tools/registry.d.ts +16 -11
- package/dist/tools/registry.js +32 -12
- package/dist/tools/send-media.js +9 -5
- package/dist/tools/session-search.d.ts +2 -1
- package/dist/tools/session-search.js +15 -5
- package/dist/tools/skill.js +13 -12
- package/dist/tools/subagent-manage.d.ts +1 -1
- package/dist/tools/subagent-manage.js +25 -152
- package/dist/tools/subagent.d.ts +24 -2
- package/dist/tools/subagent.js +24 -18
- package/dist/tools/task-manage/control-tools.js +1 -1
- package/dist/tools/task-manage/factory.d.ts +2 -2
- package/dist/tools/task-manage/factory.js +40 -4
- package/dist/tools/task-manage/lifecycle.d.ts +3 -3
- package/dist/tools/task-manage/lifecycle.js +56 -13
- package/dist/tools/task-manage/schema.d.ts +4 -1
- package/dist/tools/task-manage/schema.js +4 -1
- package/dist/tools/task-manage/shared.d.ts +1 -1
- package/dist/tools/task-manage/types.d.ts +5 -1
- package/dist/tools/write-content.d.ts +7 -1
- package/dist/tools/write-content.js +18 -6
- package/dist/tools/write.d.ts +3 -0
- package/dist/tools/write.js +6 -2
- package/dist/tui/cli.d.ts +6 -6
- package/dist/tui/cli.js +120 -28
- package/dist/tui/client.d.ts +7 -0
- package/dist/tui/client.js +67 -16
- package/dist/tui/frontend.d.ts +2 -0
- package/dist/tui/pitui-frontend.d.ts +4 -0
- package/dist/tui/pitui-frontend.js +27 -0
- package/dist/tui/render.d.ts +1 -0
- package/dist/tui/render.js +5 -0
- package/dist/tui/session.js +6 -0
- package/dist/usage/ledger.d.ts +1 -1
- package/dist/usage/render.js +1 -1
- package/dist/web/client.d.ts +1 -0
- package/dist/web/client.js +11 -0
- package/dist/web/fetch.js +6 -3
- package/dist/webapi/auth.d.ts +0 -3
- package/dist/webapi/auth.js +3 -7
- package/dist/webapi/config.js +17 -14
- package/dist/webapi/key-command.js +41 -41
- package/dist/webapi/response-state.d.ts +7 -1
- package/dist/webapi/response-state.js +166 -3
- package/dist/webapi/routes/websocket.js +24 -1
- package/dist/webapi/server.d.ts +3 -1
- package/dist/webapi/server.js +32 -1
- package/dist/webapi/service.js +8 -0
- package/dist/webapi/transport.d.ts +2 -0
- package/dist/webapi/transport.js +11 -1
- package/examples/sub-agents/README.md +43 -75
- package/examples/sub-agents/agy-main.md +21 -0
- package/examples/sub-agents/claude-high.md +21 -0
- package/examples/sub-agents/claude-main.md +21 -0
- package/examples/sub-agents/codex-flash.md +21 -0
- package/examples/sub-agents/codex-high.md +21 -0
- package/examples/sub-agents/codex-main.md +21 -0
- package/examples/sub-agents/codex-review.md +20 -0
- package/examples/sub-agents/cursor-high.md +20 -0
- package/examples/sub-agents/cursor-main.md +20 -0
- package/examples/sub-agents/glm-flash.md +21 -0
- package/examples/sub-agents/glm-high.md +21 -0
- package/examples/sub-agents/pi-scout.md +20 -0
- package/package.json +11 -12
- package/dist/agent/session/request-tools.d.ts +0 -3
- package/dist/agent/session/request-tools.js +0 -20
- package/dist/models/api-keys.d.ts +0 -11
- package/dist/models/api-keys.js +0 -22
- package/dist/playbooks/memory-and-learning.md +0 -55
- package/examples/sub-agents/agents/claude-high.md +0 -23
- package/examples/sub-agents/agents/claude-main.md +0 -23
- package/examples/sub-agents/agents/codex-flash.md +0 -23
- package/examples/sub-agents/agents/codex-high.md +0 -23
- package/examples/sub-agents/agents/codex-main.md +0 -23
- package/examples/sub-agents/agents/codex-review.md +0 -23
- package/examples/sub-agents/agents/cursor-high.md +0 -22
- package/examples/sub-agents/agents/cursor-main.md +0 -22
- package/examples/sub-agents/agents/glm-flash.md +0 -23
- package/examples/sub-agents/agents/glm-high.md +0 -23
- package/examples/sub-agents/agents/scout.md +0 -30
- package/examples/sub-agents/roles/builder-hard.md +0 -34
- package/examples/sub-agents/roles/builder.md +0 -34
- package/examples/sub-agents/roles/explorer.md +0 -39
- package/examples/sub-agents/roles/git-committer.md +0 -43
- package/examples/sub-agents/roles/planner.md +0 -33
- package/examples/sub-agents/roles/reviewer.md +0 -33
- package/examples/sub-agents/roles/verifier.md +0 -35
- package/examples/sub-agents/roles/worker.md +0 -34
|
@@ -11,14 +11,17 @@
|
|
|
11
11
|
import { readdir, readFile } from "node:fs/promises";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { loadBindings, writeBindings } from "../desk/bindings.js";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import { buildDeskList } from "../desk/commands.js";
|
|
15
|
+
import { DEFAULT_DESK, DESK_RECORD_FILENAME, getDeskDir, isDeskName, normalizeDeskName } from "../desk/paths.js";
|
|
16
|
+
import { allocateConversation, createDeskRecord, findConversation, listDesks, readDeskRecord, readDeskRecordSync, setDeskArchived, setDeskCwd, validateCustomCwd, } from "../desk/registry.js";
|
|
16
17
|
import { StartupRefusal } from "../desk/startup-error.js";
|
|
17
18
|
import { parseScheduledEventContent } from "../events/parse.js";
|
|
18
19
|
import * as log from "../log.js";
|
|
20
|
+
import { fileStamp } from "../shared/format.js";
|
|
19
21
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
20
22
|
import { errorMessage } from "../shared/text-utils.js";
|
|
21
23
|
import { readActiveTasks } from "../tasks/contract.js";
|
|
24
|
+
import { readTaskDeliveries } from "../tasks/delivery.js";
|
|
22
25
|
/** The desk a resolution names, bound or not; `undefined` for a channel with no binding. */
|
|
23
26
|
export function resolvedDesk(resolution) {
|
|
24
27
|
switch (resolution.kind) {
|
|
@@ -43,8 +46,9 @@ export class DeskRuntime {
|
|
|
43
46
|
constructor(options) {
|
|
44
47
|
this.options = options;
|
|
45
48
|
this.bindings = { version: 1, bindings: {} };
|
|
46
|
-
/** Every desk a binding
|
|
49
|
+
/** Every desk a binding or scan has touched, kept fresh when `desk.json`'s stamp changes. */
|
|
47
50
|
this.records = new Map();
|
|
51
|
+
this.recordStamps = new Map();
|
|
48
52
|
this.active = new Set();
|
|
49
53
|
this.watchers = new Map();
|
|
50
54
|
/** Activation and dormancy of one desk run in order, so interleaved calls settle on the last one. */
|
|
@@ -80,6 +84,8 @@ export class DeskRuntime {
|
|
|
80
84
|
async refreshRecord(desk) {
|
|
81
85
|
const summary = await readDeskRecord(this.options.workspaceDir, desk);
|
|
82
86
|
this.records.set(desk, summary);
|
|
87
|
+
if (summary.deskDir)
|
|
88
|
+
this.recordStamps.set(desk, deskRecordStamp(summary.deskDir));
|
|
83
89
|
if (summary.availability !== "available") {
|
|
84
90
|
log.logWarn("desk.record.unavailable", "Bound desk is unavailable", {
|
|
85
91
|
reason: summary.reason,
|
|
@@ -88,11 +94,33 @@ export class DeskRuntime {
|
|
|
88
94
|
}
|
|
89
95
|
return summary;
|
|
90
96
|
}
|
|
97
|
+
syncRecordIfChanged(desk) {
|
|
98
|
+
if (!isDeskName(desk))
|
|
99
|
+
return this.records.get(desk);
|
|
100
|
+
const deskDir = getDeskDir(this.options.workspaceDir, desk);
|
|
101
|
+
const stamp = deskRecordStamp(deskDir);
|
|
102
|
+
if (this.recordStamps.get(desk) === stamp && this.records.has(desk)) {
|
|
103
|
+
return this.records.get(desk);
|
|
104
|
+
}
|
|
105
|
+
const prevCwd = this.records.get(desk)?.record?.cwd;
|
|
106
|
+
const summary = readDeskRecordSync(this.options.workspaceDir, desk);
|
|
107
|
+
this.records.set(desk, summary);
|
|
108
|
+
this.recordStamps.set(desk, stamp);
|
|
109
|
+
const nextCwd = summary.record?.cwd;
|
|
110
|
+
if (prevCwd !== nextCwd && this.active.has(desk) && this.servicesStarted) {
|
|
111
|
+
void this.stopWatcher(desk).then(() => {
|
|
112
|
+
if (this.active.has(desk) && this.servicesStarted && !summary.record?.archivedAt) {
|
|
113
|
+
this.startWatcher(desk);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return summary;
|
|
118
|
+
}
|
|
91
119
|
resolve(channelId) {
|
|
92
120
|
const binding = this.bindings.bindings[channelId];
|
|
93
121
|
if (!binding)
|
|
94
122
|
return { kind: "unbound" };
|
|
95
|
-
const summary = this.
|
|
123
|
+
const summary = this.syncRecordIfChanged(binding.desk);
|
|
96
124
|
if (!summary?.record) {
|
|
97
125
|
return { kind: "unavailable", desk: binding.desk, reason: summary?.reason ?? "desk not loaded" };
|
|
98
126
|
}
|
|
@@ -102,6 +130,29 @@ export class DeskRuntime {
|
|
|
102
130
|
}
|
|
103
131
|
return { kind: "bound", conversation };
|
|
104
132
|
}
|
|
133
|
+
/** Effective working directory of `desk`, independent of whether any channel is currently bound to it. */
|
|
134
|
+
deskCwd(desk) {
|
|
135
|
+
const summary = this.syncRecordIfChanged(desk);
|
|
136
|
+
return summary?.record ? (summary.record.cwd ?? summary.deskDir) : undefined;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Build the `DeskConversation` a task runner in `desk` needs for `channelId`, even after
|
|
140
|
+
* `channelId` has rebound to another desk (Direction 1.2: background task execution is decoupled
|
|
141
|
+
* from current chat binding; `desk.json` retains the registered conversation record).
|
|
142
|
+
*/
|
|
143
|
+
taskConversation(desk, channelId) {
|
|
144
|
+
const summary = this.syncRecordIfChanged(desk);
|
|
145
|
+
if (!summary?.record || summary.record.archivedAt)
|
|
146
|
+
return undefined;
|
|
147
|
+
return (findConversation(summary.record, summary.deskDir, channelId) ?? {
|
|
148
|
+
desk,
|
|
149
|
+
deskDir: summary.deskDir,
|
|
150
|
+
cwd: summary.record.cwd ?? summary.deskDir,
|
|
151
|
+
channelId,
|
|
152
|
+
conversation: "task",
|
|
153
|
+
conversationDir: join(summary.deskDir, "tasks", ".sessions"),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
105
156
|
/** The producer- and admission-side check of INV-5: is this channel bound to this (available) desk? */
|
|
106
157
|
isBoundTo(channelId, desk) {
|
|
107
158
|
const resolution = this.resolve(channelId);
|
|
@@ -141,9 +192,22 @@ export class DeskRuntime {
|
|
|
141
192
|
.filter(([, binding]) => binding.desk === desk)
|
|
142
193
|
.map(([channelId]) => channelId);
|
|
143
194
|
}
|
|
144
|
-
/** Startup: activate every desk that has
|
|
195
|
+
/** Startup: activate every non-archived desk that has a binding or retained background work. */
|
|
145
196
|
async activateBound() {
|
|
146
|
-
|
|
197
|
+
const candidates = new Set(Object.values(this.bindings.bindings).map((binding) => binding.desk));
|
|
198
|
+
for (const summary of await listDesks(this.options.workspaceDir)) {
|
|
199
|
+
this.records.set(summary.name, summary);
|
|
200
|
+
if (summary.deskDir)
|
|
201
|
+
this.recordStamps.set(summary.name, deskRecordStamp(summary.deskDir));
|
|
202
|
+
if (summary.availability === "available" &&
|
|
203
|
+
summary.record &&
|
|
204
|
+
!summary.record.archivedAt &&
|
|
205
|
+
!candidates.has(summary.name) &&
|
|
206
|
+
(await hasRetainedBackgroundWork(summary.deskDir))) {
|
|
207
|
+
candidates.add(summary.name);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
for (const desk of candidates) {
|
|
147
211
|
await this.activate(desk);
|
|
148
212
|
}
|
|
149
213
|
}
|
|
@@ -156,20 +220,22 @@ export class DeskRuntime {
|
|
|
156
220
|
if (this.active.has(desk))
|
|
157
221
|
return;
|
|
158
222
|
const summary = await this.refreshRecord(desk);
|
|
159
|
-
if (!summary.record)
|
|
223
|
+
if (!summary.record || summary.record.archivedAt)
|
|
160
224
|
return;
|
|
161
225
|
const conversations = this.boundChannelsOf(desk).flatMap((channelId) => {
|
|
162
226
|
const resolution = this.resolve(channelId);
|
|
163
227
|
return resolution.kind === "bound" ? [resolution.conversation] : [];
|
|
164
228
|
});
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
229
|
+
if (conversations.length > 0) {
|
|
230
|
+
try {
|
|
231
|
+
await this.options.recover(conversations);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
log.logWarn("desk.activate.recover_failed", "Interrupted-turn repair failed during activation", {
|
|
235
|
+
reason: errorMessage(error),
|
|
236
|
+
fields: { desk },
|
|
237
|
+
});
|
|
238
|
+
}
|
|
173
239
|
}
|
|
174
240
|
this.active.add(desk);
|
|
175
241
|
if (this.servicesStarted)
|
|
@@ -187,10 +253,27 @@ export class DeskRuntime {
|
|
|
187
253
|
log.logInfoEvent("desk.deactivated", "Desk went dormant: no bound channel", { fields: { desk } });
|
|
188
254
|
});
|
|
189
255
|
}
|
|
256
|
+
/** Put an unbound desk to sleep once its last runnable task and task-spawning event are gone. */
|
|
257
|
+
reconcileActivity(desk) {
|
|
258
|
+
if (!this.active.has(desk) || this.boundChannelsOf(desk).length > 0)
|
|
259
|
+
return Promise.resolve();
|
|
260
|
+
return this.lifecycle.run(desk, async () => {
|
|
261
|
+
if (!this.active.has(desk) || this.boundChannelsOf(desk).length > 0)
|
|
262
|
+
return;
|
|
263
|
+
const summary = await this.refreshRecord(desk);
|
|
264
|
+
if (!summary.record || summary.record.archivedAt || !(await hasRetainedBackgroundWork(summary.deskDir))) {
|
|
265
|
+
await this.stopWatcher(desk);
|
|
266
|
+
this.active.delete(desk);
|
|
267
|
+
log.logInfoEvent("desk.deactivated", "Desk went dormant: background work finished", { fields: { desk } });
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
190
271
|
startWatcher(desk) {
|
|
191
272
|
if (this.watchers.has(desk))
|
|
192
273
|
return;
|
|
193
|
-
const
|
|
274
|
+
const deskDir = getDeskDir(this.options.workspaceDir, desk);
|
|
275
|
+
const cwd = this.records.get(desk)?.record?.cwd ?? deskDir;
|
|
276
|
+
const watcher = this.options.createWatcher({ desk, deskDir, cwd });
|
|
194
277
|
this.watchers.set(desk, watcher);
|
|
195
278
|
watcher.start();
|
|
196
279
|
}
|
|
@@ -203,8 +286,160 @@ export class DeskRuntime {
|
|
|
203
286
|
await watcher.flush?.().catch(() => undefined);
|
|
204
287
|
}
|
|
205
288
|
/** `/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()));
|
|
289
|
+
create(name, cwd) {
|
|
290
|
+
return Promise.resolve(createDeskRecord(this.options.workspaceDir, normalizeDeskName(name), new Date(), cwd));
|
|
291
|
+
}
|
|
292
|
+
/** `/desk list` rows enriched with live background task and pending-delivery counts. */
|
|
293
|
+
async listDeskItems(askingChannelId, labelOf) {
|
|
294
|
+
const desks = await listDesks(this.options.workspaceDir);
|
|
295
|
+
const activity = new Map();
|
|
296
|
+
await Promise.all(desks.map(async (summary) => {
|
|
297
|
+
if (summary.availability !== "available" || !summary.record || summary.record.archivedAt)
|
|
298
|
+
return;
|
|
299
|
+
const activeTasks = (await readActiveTasks(join(summary.deskDir, "tasks")).catch(() => [])).filter((entry) => !entry.fields.outcome && entry.fields.state !== "paused").length;
|
|
300
|
+
const pendingDeliveries = (await readTaskDeliveries(summary.deskDir).catch(() => [])).filter((item) => !item.sentAt).length;
|
|
301
|
+
activity.set(summary.name, {
|
|
302
|
+
activeTasks,
|
|
303
|
+
pendingDeliveries,
|
|
304
|
+
active: this.active.has(summary.name),
|
|
305
|
+
});
|
|
306
|
+
}));
|
|
307
|
+
return buildDeskList(desks, this.bindingTable(), this.boundDesk(askingChannelId), labelOf, activity);
|
|
308
|
+
}
|
|
309
|
+
/** `/desk cwd`: view or update the working directory of `channelId`'s bound desk. */
|
|
310
|
+
async cwd(channelId, input) {
|
|
311
|
+
const resolution = this.resolve(channelId);
|
|
312
|
+
if (resolution.kind !== "bound") {
|
|
313
|
+
return { kind: "unavailable", desk: resolvedDesk(resolution) ?? "" };
|
|
314
|
+
}
|
|
315
|
+
const { desk, deskDir } = resolution.conversation;
|
|
316
|
+
const summary = await this.refreshRecord(desk);
|
|
317
|
+
if (!summary.record)
|
|
318
|
+
return { kind: "unavailable", desk };
|
|
319
|
+
const currentEffective = summary.record.cwd ?? deskDir;
|
|
320
|
+
if (!input.cwd && !input.reset) {
|
|
321
|
+
return { kind: "ok", desk, cwd: currentEffective, isDefault: summary.record.cwd === undefined };
|
|
322
|
+
}
|
|
323
|
+
let targetEffective = deskDir;
|
|
324
|
+
if (!input.reset) {
|
|
325
|
+
const checked = validateCustomCwd(input.cwd ?? "");
|
|
326
|
+
if (!checked.ok)
|
|
327
|
+
return { kind: "invalid_cwd", cwd: input.cwd ?? "", reason: checked.reason };
|
|
328
|
+
targetEffective = checked.cwd;
|
|
329
|
+
}
|
|
330
|
+
if (targetEffective === currentEffective) {
|
|
331
|
+
return { kind: "ok", desk, cwd: currentEffective, isDefault: summary.record.cwd === undefined };
|
|
332
|
+
}
|
|
333
|
+
if (this.boundChannelsOf(desk).some((boundChannel) => this.options.isChannelBusy(boundChannel))) {
|
|
334
|
+
return { kind: "busy", desk, reason: "有聊天回合正在运行" };
|
|
335
|
+
}
|
|
336
|
+
const busyReason = this.options.isDeskBusy?.(desk, currentEffective);
|
|
337
|
+
if (busyReason) {
|
|
338
|
+
return { kind: "busy", desk, reason: busyReason };
|
|
339
|
+
}
|
|
340
|
+
const outcome = await setDeskCwd(this.options.workspaceDir, desk, input.reset ? null : targetEffective);
|
|
341
|
+
if (outcome.kind !== "ok")
|
|
342
|
+
return outcome;
|
|
343
|
+
await this.refreshRecord(desk);
|
|
344
|
+
if (this.active.has(desk) && this.servicesStarted) {
|
|
345
|
+
await this.stopWatcher(desk);
|
|
346
|
+
this.startWatcher(desk);
|
|
347
|
+
}
|
|
348
|
+
for (const boundChannel of this.boundChannelsOf(desk)) {
|
|
349
|
+
if (!this.options.isChannelBusy(boundChannel)) {
|
|
350
|
+
await this.options.releaseRunner(boundChannel).catch(() => undefined);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return outcome;
|
|
354
|
+
}
|
|
355
|
+
/** `/desk archive <name>`: retire an idle desk, unbinding stale terminal sessions and stopping events. */
|
|
356
|
+
archive(askingChannelId, name) {
|
|
357
|
+
return this.bindingWrites.run("bindings", async () => {
|
|
358
|
+
const desk = normalizeDeskName(name);
|
|
359
|
+
if (!isDeskName(desk))
|
|
360
|
+
return { kind: "invalid", desk };
|
|
361
|
+
if (desk === DEFAULT_DESK)
|
|
362
|
+
return { kind: "default" };
|
|
363
|
+
const summary = await readDeskRecord(this.options.workspaceDir, desk);
|
|
364
|
+
if (summary.availability === "missing")
|
|
365
|
+
return { kind: "unknown", desk };
|
|
366
|
+
if (!summary.record)
|
|
367
|
+
return { kind: "unavailable", desk };
|
|
368
|
+
if (summary.record.archivedAt)
|
|
369
|
+
return { kind: "already_archived", desk };
|
|
370
|
+
const bound = this.boundChannelsOf(desk);
|
|
371
|
+
if (askingChannelId && bound.includes(askingChannelId)) {
|
|
372
|
+
return { kind: "current_channel", desk };
|
|
373
|
+
}
|
|
374
|
+
const nonTerminals = bound.filter((channelId) => this.options.conversationKindOf(channelId) !== "webapi");
|
|
375
|
+
if (nonTerminals.length > 0) {
|
|
376
|
+
return {
|
|
377
|
+
kind: "bound_channels",
|
|
378
|
+
desk,
|
|
379
|
+
channels: nonTerminals.map((channelId) => this.options.describeChannel?.(channelId) ?? channelId),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const terminals = bound.filter((channelId) => this.options.conversationKindOf(channelId) === "webapi");
|
|
383
|
+
if (terminals.some((channelId) => this.options.isChannelBusy(channelId))) {
|
|
384
|
+
return { kind: "busy", desk, reason: "有正在运行的终端回合" };
|
|
385
|
+
}
|
|
386
|
+
const busyReason = this.options.isDeskBusy?.(desk, summary.record.cwd ?? summary.deskDir);
|
|
387
|
+
if (busyReason)
|
|
388
|
+
return { kind: "busy", desk, reason: busyReason };
|
|
389
|
+
const openTasks = (await readActiveTasks(join(summary.deskDir, "tasks")).catch(() => [])).filter((entry) => !entry.fields.outcome);
|
|
390
|
+
if (openTasks.length > 0) {
|
|
391
|
+
return { kind: "open_tasks", desk, count: openTasks.length, sampleId: openTasks[0].id };
|
|
392
|
+
}
|
|
393
|
+
if (terminals.length > 0) {
|
|
394
|
+
const nextBindings = { ...this.bindings.bindings };
|
|
395
|
+
for (const channelId of terminals)
|
|
396
|
+
delete nextBindings[channelId];
|
|
397
|
+
const next = { ...this.bindings, bindings: nextBindings };
|
|
398
|
+
await writeBindings(this.options.workspaceDir, next);
|
|
399
|
+
this.bindings = next;
|
|
400
|
+
for (const channelId of terminals) {
|
|
401
|
+
log.logInfoEvent("desk.unbound", "Terminal channel unbound on desk archive", {
|
|
402
|
+
channelId,
|
|
403
|
+
fields: { desk },
|
|
404
|
+
});
|
|
405
|
+
this.options.onBindingChanged?.(channelId);
|
|
406
|
+
await this.options.releaseRunner(channelId).catch(() => undefined);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const stoppedEvents = await countScheduledEvents(summary.deskDir);
|
|
410
|
+
await setDeskArchived(this.options.workspaceDir, desk, new Date().toISOString());
|
|
411
|
+
await this.refreshRecord(desk);
|
|
412
|
+
await this.deactivate(desk);
|
|
413
|
+
log.logInfoEvent("desk.archived", "Desk archived", {
|
|
414
|
+
fields: { desk, unboundTerminals: terminals.length, stoppedEvents },
|
|
415
|
+
});
|
|
416
|
+
return { kind: "archived", desk, unboundTerminals: terminals.length, stoppedEvents };
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
/** `/desk unarchive <name>`: clear `archivedAt` and re-activate if work is waiting. */
|
|
420
|
+
unarchive(name) {
|
|
421
|
+
return this.bindingWrites.run("bindings", async () => {
|
|
422
|
+
const desk = normalizeDeskName(name);
|
|
423
|
+
if (!isDeskName(desk))
|
|
424
|
+
return { kind: "invalid", desk };
|
|
425
|
+
const summary = await readDeskRecord(this.options.workspaceDir, desk);
|
|
426
|
+
if (summary.availability === "missing")
|
|
427
|
+
return { kind: "unknown", desk };
|
|
428
|
+
if (!summary.record)
|
|
429
|
+
return { kind: "unavailable", desk };
|
|
430
|
+
if (!summary.record.archivedAt)
|
|
431
|
+
return { kind: "not_archived", desk };
|
|
432
|
+
await setDeskArchived(this.options.workspaceDir, desk, null);
|
|
433
|
+
await this.refreshRecord(desk);
|
|
434
|
+
if (this.boundChannelsOf(desk).length > 0 || (await hasRetainedBackgroundWork(summary.deskDir))) {
|
|
435
|
+
await this.activate(desk);
|
|
436
|
+
this.options.onActivated?.(desk);
|
|
437
|
+
}
|
|
438
|
+
log.logInfoEvent("desk.unarchived", "Desk unarchived", {
|
|
439
|
+
fields: { desk, activated: this.active.has(desk) },
|
|
440
|
+
});
|
|
441
|
+
return { kind: "unarchived", desk, activated: this.active.has(desk) };
|
|
442
|
+
});
|
|
208
443
|
}
|
|
209
444
|
/** `/desk bind` from `channelId`, in D5's order. Throws only when the bindings write itself fails. */
|
|
210
445
|
bind(channelId, name) {
|
|
@@ -248,8 +483,13 @@ export class DeskRuntime {
|
|
|
248
483
|
if (this.options.isChannelBusy(channelId))
|
|
249
484
|
return { kind: "busy", desk };
|
|
250
485
|
const previous = this.bindings.bindings[channelId]?.desk;
|
|
251
|
-
if (previous === desk)
|
|
486
|
+
if (previous === desk && !summary.record.archivedAt)
|
|
252
487
|
return { kind: "same", desk };
|
|
488
|
+
let unarchived = false;
|
|
489
|
+
if (summary.record.archivedAt) {
|
|
490
|
+
await setDeskArchived(this.options.workspaceDir, desk, null);
|
|
491
|
+
unarchived = true;
|
|
492
|
+
}
|
|
253
493
|
this.records.set(desk, summary);
|
|
254
494
|
// 2–3. Give the channel its conversation in the target, then commit the binding. Until the
|
|
255
495
|
// write succeeds nothing has changed for the channel and nothing in the target has started.
|
|
@@ -271,6 +511,7 @@ export class DeskRuntime {
|
|
|
271
511
|
// cannot undo the committed binding; the next restart activates the desk again.
|
|
272
512
|
try {
|
|
273
513
|
await this.activate(desk);
|
|
514
|
+
this.options.onActivated?.(desk);
|
|
274
515
|
}
|
|
275
516
|
catch (error) {
|
|
276
517
|
log.logWarn("desk.bind.activate_failed", "Activating the desk after a bind failed", {
|
|
@@ -290,26 +531,36 @@ export class DeskRuntime {
|
|
|
290
531
|
fields: { desk, previousDesk: previous },
|
|
291
532
|
});
|
|
292
533
|
}
|
|
293
|
-
// 6. The old desk:
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
534
|
+
// 6. The old desk: stays active while it still has open tasks or task-spawning events; goes
|
|
535
|
+
// dormant only when both bound channels and runnable background work are gone.
|
|
536
|
+
if (!previous || previous === desk) {
|
|
537
|
+
return { kind: "bound", desk, ...(unarchived ? { unarchived: true } : {}) };
|
|
538
|
+
}
|
|
539
|
+
const previousDir = getDeskDir(this.options.workspaceDir, previous);
|
|
540
|
+
const departure = await inspectChannelDeparture(previousDir, channelId);
|
|
541
|
+
if (this.boundChannelsOf(previous).length === 0 && !(await hasRetainedBackgroundWork(previousDir))) {
|
|
297
542
|
await this.deactivate(previous);
|
|
298
|
-
return {
|
|
543
|
+
return {
|
|
544
|
+
kind: "bound",
|
|
545
|
+
desk,
|
|
546
|
+
...(unarchived ? { unarchived: true } : {}),
|
|
547
|
+
previousDesk: previous,
|
|
548
|
+
previousDeskDormant: true,
|
|
549
|
+
};
|
|
299
550
|
}
|
|
300
|
-
const suspended = await countDeliveringTo(getDeskDir(this.options.workspaceDir, previous), channelId);
|
|
301
551
|
return {
|
|
302
552
|
kind: "bound",
|
|
303
553
|
desk,
|
|
554
|
+
...(unarchived ? { unarchived: true } : {}),
|
|
304
555
|
previousDesk: previous,
|
|
305
|
-
|
|
306
|
-
suspendedEvents:
|
|
556
|
+
...(departure.backgroundTasks > 0 ? { backgroundTasks: departure.backgroundTasks } : {}),
|
|
557
|
+
...(departure.suspendedEvents > 0 ? { suspendedEvents: departure.suspendedEvents } : {}),
|
|
307
558
|
};
|
|
308
559
|
}
|
|
309
560
|
/**
|
|
310
561
|
* 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
|
|
312
|
-
* directory stays in the desk as history. Whether there was a binding to drop.
|
|
562
|
+
* runner, and — when it was the desk's last channel and no background work remains — the desk's
|
|
563
|
+
* activity. Its conversation directory stays in the desk as history. Whether there was a binding to drop.
|
|
313
564
|
*/
|
|
314
565
|
unbind(channelId) {
|
|
315
566
|
return this.bindingWrites.run("bindings", async () => {
|
|
@@ -332,8 +583,10 @@ export class DeskRuntime {
|
|
|
332
583
|
fields: { desk: previous },
|
|
333
584
|
});
|
|
334
585
|
}
|
|
335
|
-
if (this.boundChannelsOf(previous).length === 0
|
|
586
|
+
if (this.boundChannelsOf(previous).length === 0 &&
|
|
587
|
+
!(await hasRetainedBackgroundWork(getDeskDir(this.options.workspaceDir, previous)))) {
|
|
336
588
|
await this.deactivate(previous);
|
|
589
|
+
}
|
|
337
590
|
return true;
|
|
338
591
|
});
|
|
339
592
|
}
|
|
@@ -367,36 +620,69 @@ export class DeskRuntime {
|
|
|
367
620
|
await Promise.all([...this.watchers.values()].map((watcher) => watcher.flush?.() ?? Promise.resolve()));
|
|
368
621
|
}
|
|
369
622
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
*/
|
|
374
|
-
async function
|
|
375
|
-
let tasks = 0;
|
|
623
|
+
function deskRecordStamp(deskDir) {
|
|
624
|
+
return fileStamp(join(deskDir, DESK_RECORD_FILENAME)) ?? "missing";
|
|
625
|
+
}
|
|
626
|
+
/** Whether `deskDir` still owns work that can execute without any bound chat channel. */
|
|
627
|
+
async function hasRetainedBackgroundWork(deskDir) {
|
|
376
628
|
try {
|
|
377
|
-
|
|
629
|
+
const closing = await readdir(join(deskDir, "tasks", ".closing"));
|
|
630
|
+
if (closing.some((name) => name.endsWith(".json")))
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
catch {
|
|
634
|
+
// No `.closing` directory.
|
|
635
|
+
}
|
|
636
|
+
try {
|
|
637
|
+
const open = await readActiveTasks(join(deskDir, "tasks"));
|
|
638
|
+
if (open.some((entry) => !entry.fields.outcome && entry.fields.state !== "paused"))
|
|
639
|
+
return true;
|
|
378
640
|
}
|
|
379
641
|
catch {
|
|
380
|
-
// No readable tasks directory
|
|
642
|
+
// No readable tasks directory.
|
|
381
643
|
}
|
|
382
|
-
|
|
644
|
+
for (const event of await readValidScheduledEvents(deskDir)) {
|
|
645
|
+
if (event.task)
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
async function readValidScheduledEvents(deskDir) {
|
|
383
651
|
const eventsDir = join(deskDir, "events");
|
|
384
652
|
let filenames = [];
|
|
385
653
|
try {
|
|
386
654
|
filenames = (await readdir(eventsDir)).filter((filename) => filename.endsWith(".json"));
|
|
387
655
|
}
|
|
388
656
|
catch {
|
|
389
|
-
|
|
657
|
+
return [];
|
|
390
658
|
}
|
|
659
|
+
const events = [];
|
|
391
660
|
for (const filename of filenames) {
|
|
392
661
|
try {
|
|
393
|
-
|
|
394
|
-
if (event.channelId === channelId)
|
|
395
|
-
events++;
|
|
662
|
+
events.push(parseScheduledEventContent(await readFile(join(eventsDir, filename), "utf8"), filename));
|
|
396
663
|
}
|
|
397
664
|
catch {
|
|
398
665
|
// An invalid definition never fires anyway.
|
|
399
666
|
}
|
|
400
667
|
}
|
|
401
|
-
return
|
|
668
|
+
return events;
|
|
669
|
+
}
|
|
670
|
+
async function countScheduledEvents(deskDir) {
|
|
671
|
+
return (await readValidScheduledEvents(deskDir)).length;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Open tasks in `deskDir` that keep running in the background while `channelId` is away, plus
|
|
675
|
+
* chat-wake events delivering to `channelId` that pause until `channelId` binds back.
|
|
676
|
+
*/
|
|
677
|
+
async function inspectChannelDeparture(deskDir, channelId) {
|
|
678
|
+
let backgroundTasks = 0;
|
|
679
|
+
try {
|
|
680
|
+
backgroundTasks = (await readActiveTasks(join(deskDir, "tasks"))).filter((entry) => !entry.fields.outcome && entry.fields.state !== "paused" && entry.fields.channel === channelId).length;
|
|
681
|
+
}
|
|
682
|
+
catch {
|
|
683
|
+
// No readable tasks directory.
|
|
684
|
+
}
|
|
685
|
+
const events = await readValidScheduledEvents(deskDir);
|
|
686
|
+
const suspendedEvents = events.filter((event) => event.channelId === channelId && !event.task).length;
|
|
687
|
+
return { backgroundTasks, suspendedEvents };
|
|
402
688
|
}
|
|
@@ -11,6 +11,8 @@ export interface DurableDispatchRecord {
|
|
|
11
11
|
/** Set by `markRetryable` to back off a repeatedly-failing structured wake instead of
|
|
12
12
|
* redelivering on the very next drain tick (spec 031's redelivery has no such gate). */
|
|
13
13
|
nextAttemptAt?: string;
|
|
14
|
+
/** Set when the record hits `MAX_DELIVERIES`; starts the dead-letter retention window. */
|
|
15
|
+
exhaustedAt?: string;
|
|
14
16
|
}
|
|
15
17
|
export interface DurableDispatchOptions {
|
|
16
18
|
stateDir: string;
|
|
@@ -23,10 +25,12 @@ export interface DurableDispatchOptions {
|
|
|
23
25
|
isReachable?: (channelId: string) => boolean;
|
|
24
26
|
leaseMs?: number;
|
|
25
27
|
intervalMs?: number;
|
|
28
|
+
/** Retention window for `exhausted` dead-letter records before `drainOnce` unlinks them (default 7 days). */
|
|
29
|
+
exhaustedRetentionMs?: number;
|
|
26
30
|
/** Called once, at most, when a dispatch id hits `MAX_DELIVERIES` without ever completing —
|
|
27
31
|
* e.g. a structured wake whose claim/finish step fails deterministically (disk full,
|
|
28
|
-
* permissions). The record is
|
|
29
|
-
* caller is expected to notify the channel. */
|
|
32
|
+
* permissions). The record is kept on disk for the retention window so the failure is inspectable;
|
|
33
|
+
* the caller is expected to notify the channel. */
|
|
30
34
|
onExhausted?: (record: DurableDispatchRecord) => void | Promise<void>;
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
@@ -46,7 +50,13 @@ export declare class DurableDispatchService {
|
|
|
46
50
|
private readonly capacityRejected;
|
|
47
51
|
private readonly capacityDrains;
|
|
48
52
|
private readonly queue;
|
|
53
|
+
private readonly pendingWrites;
|
|
54
|
+
private run;
|
|
55
|
+
/** stop() closes background admission; after active turns settle, wait for started writes
|
|
56
|
+
* before another process can acquire the daemon lock. A hung write keeps that lock retained. */
|
|
57
|
+
flush(): Promise<void>;
|
|
49
58
|
private readonly leaseMs;
|
|
59
|
+
private readonly exhaustedRetentionMs;
|
|
50
60
|
private timer;
|
|
51
61
|
/**
|
|
52
62
|
* Dispatch ids whose turn this process is still running (spec 031, D2).
|
|
@@ -123,6 +133,8 @@ export declare class DurableDispatchService {
|
|
|
123
133
|
markRetryable(id: string | undefined): Promise<void>;
|
|
124
134
|
drainOnce(now?: number): Promise<void>;
|
|
125
135
|
private drainRecord;
|
|
136
|
+
private idFromFilename;
|
|
137
|
+
private reportRecordFault;
|
|
126
138
|
private read;
|
|
127
139
|
private write;
|
|
128
140
|
}
|