@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
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { discoverWorkspaceChannelIds } from "../channel/channel-index.js";
|
|
4
|
-
import { dedupeChannelIdsByDirectory, getChannelDir, isChannelId } from "../channel/channel-paths.js";
|
|
5
3
|
import { TASK_DRIVER_SETTINGS } from "../config/settings.js";
|
|
6
4
|
import * as log from "../log.js";
|
|
7
5
|
import { PLAYBOOKS_DIR } from "../paths.js";
|
|
@@ -17,37 +15,22 @@ const NUDGE_DEBOUNCE_MS = 50;
|
|
|
17
15
|
const MIN_SLEEP_MS = 250;
|
|
18
16
|
/** A task dispatched this recently is assumed still in flight; do not queue it twice. */
|
|
19
17
|
const REDISPATCH_GUARD_MS = 30_000;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
* its escaped directory name never displaces the real id the runner map already knows.
|
|
23
|
-
*/
|
|
24
|
-
export async function discoverTaskChannels(workspaceDir, knownChannelIds = [], options = {}) {
|
|
25
|
-
return dedupeChannelIdsByDirectory([
|
|
26
|
-
...[...knownChannelIds].filter(isChannelId),
|
|
27
|
-
...(await discoverWorkspaceChannelIds(workspaceDir, options)),
|
|
28
|
-
]);
|
|
29
|
-
}
|
|
30
|
-
function attemptKey(channelId, taskId) {
|
|
31
|
-
return `${channelId}\0${taskId}`;
|
|
32
|
-
}
|
|
33
|
-
function channelType(channelId) {
|
|
34
|
-
return channelId.startsWith("group_")
|
|
35
|
-
? { type: "group", conversationType: "2" }
|
|
36
|
-
: { type: "dm", conversationType: "1" };
|
|
18
|
+
function attemptKey(desk, taskId) {
|
|
19
|
+
return `${desk}\0${taskId}`;
|
|
37
20
|
}
|
|
38
21
|
/**
|
|
39
22
|
* Stable, provenance-derived dispatch id for a task step (spec 031, D1; spec 051 keeps the shape).
|
|
40
23
|
* Each step is genuinely new work, so the dispatch moment disambiguates it; collapsing two steps
|
|
41
24
|
* onto one durable record would silently drop the later one.
|
|
42
25
|
*/
|
|
43
|
-
function stepDispatchId(
|
|
44
|
-
return `task:${
|
|
26
|
+
function stepDispatchId(desk, entry, nowMs) {
|
|
27
|
+
return `task:${desk}:${entry.id}:${nowMs}`;
|
|
45
28
|
}
|
|
46
29
|
/** The task a `[TASK_DRIVER:<id>]` dispatch text names, if it is one. */
|
|
47
30
|
export function parseDriverTaskId(text) {
|
|
48
31
|
return /^\[TASK_DRIVER:([A-Za-z0-9._-]+)\]/.exec(text ?? "")?.[1];
|
|
49
32
|
}
|
|
50
|
-
export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
33
|
+
export function createTaskDriverEvent(desk, channelId, entry, nowMs) {
|
|
51
34
|
const repairOnly = !entry.readable;
|
|
52
35
|
const repair = repairOnly
|
|
53
36
|
? ` Task metadata is not readable; repair only the frontmatter in tasks/${entry.id}.md, then stop. ` +
|
|
@@ -63,10 +46,9 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
|
63
46
|
.filter(Boolean)
|
|
64
47
|
.join(" ");
|
|
65
48
|
return {
|
|
66
|
-
|
|
49
|
+
source: "wake",
|
|
67
50
|
channelId,
|
|
68
|
-
|
|
69
|
-
userName: "TASK_DRIVER",
|
|
51
|
+
producer: "TASK_DRIVER",
|
|
70
52
|
text: `[TASK_DRIVER:${entry.id}] Resume task ${entry.id}. ${capsule}${repair} ` +
|
|
71
53
|
`Open tasks/${entry.id}.md and read ${join(PLAYBOOKS_DIR, "task-lead.md")} before acting. ` +
|
|
72
54
|
(repairOnly
|
|
@@ -74,7 +56,8 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
|
74
56
|
: "Advance the next concrete step under the task's current goal, work items and acceptance state. ") +
|
|
75
57
|
"End the step with task_step_end (continue / park / done).",
|
|
76
58
|
ts: String(nowMs),
|
|
77
|
-
dispatchId: stepDispatchId(
|
|
59
|
+
dispatchId: stepDispatchId(desk, entry, nowMs),
|
|
60
|
+
desk,
|
|
78
61
|
};
|
|
79
62
|
}
|
|
80
63
|
/**
|
|
@@ -83,15 +66,14 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
|
83
66
|
* call to phrase that is exactly the kind of cost this design removes. A budget stop also says
|
|
84
67
|
* how to resume with more of the dimension that ran out (spec 053, D6).
|
|
85
68
|
*/
|
|
86
|
-
export function taskStopReceipt(channelId, entry, reason, nowMs, breach) {
|
|
69
|
+
export function taskStopReceipt(desk, channelId, entry, reason, nowMs, breach) {
|
|
87
70
|
const usage = entry.fields.usage ? ` · 已用 ${entry.fields.usage.steps} 步` : "";
|
|
88
71
|
const grant = breach === "steps" ? " +steps 10" : breach === "usd" ? " +usd 5" : "";
|
|
89
72
|
const resumeLabel = grant ? "继续并追加预算" : "继续";
|
|
90
73
|
return {
|
|
91
|
-
|
|
74
|
+
source: "wake",
|
|
92
75
|
channelId,
|
|
93
|
-
|
|
94
|
-
userName: "TASK_DRIVER",
|
|
76
|
+
producer: "TASK_DRIVER",
|
|
95
77
|
text: `🛑 任务 \`${entry.id}\`(${entry.title})已停止自动执行:${reason}。\n` +
|
|
96
78
|
`状态:${taskStateLabel(entry.fields.state)}${usage}\n` +
|
|
97
79
|
`查看:\`/tasks show ${entry.id}\`;${resumeLabel}:\`/tasks resume ${entry.id}${grant}\`\n` +
|
|
@@ -99,13 +81,14 @@ export function taskStopReceipt(channelId, entry, reason, nowMs, breach) {
|
|
|
99
81
|
ts: String(nowMs),
|
|
100
82
|
// Keyed on the cause, not the moment: re-detecting the same stop before the user has acted
|
|
101
83
|
// must not queue a second identical receipt, while a different cause still does.
|
|
102
|
-
dispatchId: `task:${
|
|
84
|
+
dispatchId: `task:${desk}:${entry.id}:stop:${createHash("sha256").update(reason).digest("hex").slice(0, 12)}`,
|
|
85
|
+
desk,
|
|
103
86
|
};
|
|
104
87
|
}
|
|
105
88
|
/**
|
|
106
89
|
* Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9; spec 052).
|
|
107
90
|
*
|
|
108
|
-
* Three jobs, in this order per
|
|
91
|
+
* Three jobs, in this order per desk: redeem due `time` tickets, apply the backstop to expired
|
|
109
92
|
* ones, and queue one step for a runnable task. `work` and `ask` tickets are never polled — their
|
|
110
93
|
* owners push them (a settling delegation or job, `/tasks reply`) — so the scan stays a cheap read
|
|
111
94
|
* of a handful of frontmatters. Recurring work is not the driver's business: an event spawns a
|
|
@@ -125,15 +108,16 @@ export class TaskDriver {
|
|
|
125
108
|
this.nudgeTimer = null;
|
|
126
109
|
this.loopActive = false;
|
|
127
110
|
this.running = false;
|
|
128
|
-
this.
|
|
111
|
+
this.nextDeskIndex = 0;
|
|
129
112
|
/** Last dispatch time per task, so one in-flight step is not queued again on the next tick. */
|
|
130
113
|
this.lastDispatchMs = new Map();
|
|
131
114
|
/**
|
|
132
|
-
* Last task id dispatched per
|
|
133
|
-
*
|
|
134
|
-
* otherwise monopolise.
|
|
115
|
+
* Last task id dispatched per desk. Starting the search just after it gives every ready task in
|
|
116
|
+
* a desk the same round-robin fairness an actively-progressing task would otherwise monopolise.
|
|
135
117
|
*/
|
|
136
118
|
this.lastDispatchedTaskId = new Map();
|
|
119
|
+
/** Tasks currently suspended (D8), so the transition is logged once rather than every scan. */
|
|
120
|
+
this.suspended = new Set();
|
|
137
121
|
}
|
|
138
122
|
observeDispatch(event, accepted) {
|
|
139
123
|
try {
|
|
@@ -213,9 +197,9 @@ export class TaskDriver {
|
|
|
213
197
|
this.nextWakeMs = candidate;
|
|
214
198
|
}
|
|
215
199
|
/** Stop one task and notify. Delivery failure is logged, never fatal: the durable stop already happened. */
|
|
216
|
-
async notifyStop(channelId, entry, reason, nowMs, breach) {
|
|
200
|
+
async notifyStop(desk, channelId, entry, reason, nowMs, breach) {
|
|
217
201
|
try {
|
|
218
|
-
await this.options.notify?.(taskStopReceipt(channelId, entry, reason, nowMs, breach));
|
|
202
|
+
await this.options.notify?.(taskStopReceipt(desk, channelId, entry, reason, nowMs, breach));
|
|
219
203
|
}
|
|
220
204
|
catch (error) {
|
|
221
205
|
log.logWarn("taskdriver.receipt.failed", "Task stop receipt failed", {
|
|
@@ -225,14 +209,14 @@ export class TaskDriver {
|
|
|
225
209
|
}
|
|
226
210
|
}
|
|
227
211
|
/**
|
|
228
|
-
* Deterministic, zero-token maintenance for one
|
|
229
|
-
* backstops. Runs whether or not the channel is busy — only model
|
|
230
|
-
*
|
|
212
|
+
* Deterministic, zero-token maintenance for one desk's live tasks: redeem due tickets and apply
|
|
213
|
+
* expiry backstops. Runs whether or not the delivery channel is busy — only model dispatch is
|
|
214
|
+
* gated on an idle channel.
|
|
231
215
|
*/
|
|
232
|
-
async maintain(
|
|
216
|
+
async maintain(desk, deskDir, entries, now) {
|
|
233
217
|
const nowMs = now.getTime();
|
|
234
218
|
let changed = false;
|
|
235
|
-
for (const entry of entries) {
|
|
219
|
+
for (const { entry, channelId } of entries) {
|
|
236
220
|
if (entry.fields.paused || entry.fields.state !== "parked")
|
|
237
221
|
continue;
|
|
238
222
|
const ticket = entry.fields.ticket;
|
|
@@ -240,12 +224,12 @@ export class TaskDriver {
|
|
|
240
224
|
continue;
|
|
241
225
|
// A due timer redeems into an open task.
|
|
242
226
|
if (entry.dueMs !== undefined && entry.dueMs <= nowMs) {
|
|
243
|
-
if (await redeemTicket(
|
|
227
|
+
if (await redeemTicket(deskDir, entry.id, (current) => current.kind === ticket.kind))
|
|
244
228
|
changed = true;
|
|
245
229
|
continue;
|
|
246
230
|
}
|
|
247
231
|
if (entry.expired) {
|
|
248
|
-
const expiry = await expireTicket(
|
|
232
|
+
const expiry = await expireTicket(deskDir, entry.id);
|
|
249
233
|
if (!expiry)
|
|
250
234
|
continue;
|
|
251
235
|
changed = true;
|
|
@@ -253,15 +237,15 @@ export class TaskDriver {
|
|
|
253
237
|
log.logWarn("taskdriver.task.paused", "Task paused: ticket expired twice", {
|
|
254
238
|
channelId: channelId,
|
|
255
239
|
reason: expiry.ticket,
|
|
256
|
-
fields: { taskId: entry.id, cause: "ticket_expired" },
|
|
240
|
+
fields: { taskId: entry.id, desk, cause: "ticket_expired" },
|
|
257
241
|
});
|
|
258
|
-
await this.notifyStop(channelId, entry, `等待条件一直未满足(${expiry.ticket}),已连续两次超过最晚检查时间`, nowMs);
|
|
242
|
+
await this.notifyStop(desk, channelId, entry, `等待条件一直未满足(${expiry.ticket}),已连续两次超过最晚检查时间`, nowMs);
|
|
259
243
|
}
|
|
260
244
|
else {
|
|
261
245
|
log.logInfoEvent("taskdriver.task.reopened", "Task reopened after its ticket expired", {
|
|
262
246
|
channelId: channelId,
|
|
263
247
|
reason: expiry.ticket,
|
|
264
|
-
fields: { taskId: entry.id },
|
|
248
|
+
fields: { taskId: entry.id, desk },
|
|
265
249
|
});
|
|
266
250
|
}
|
|
267
251
|
continue;
|
|
@@ -277,31 +261,70 @@ export class TaskDriver {
|
|
|
277
261
|
* each step actually did, so "no progress" is an observed fact rather than a fingerprint
|
|
278
262
|
* comparison a `echo x` could defeat.
|
|
279
263
|
*/
|
|
280
|
-
async detectIdleLoop(
|
|
264
|
+
async detectIdleLoop(desk, deskDir, entries, nowMs) {
|
|
281
265
|
let paused = false;
|
|
282
|
-
for (const entry of entries) {
|
|
266
|
+
for (const { entry, channelId } of entries) {
|
|
283
267
|
const usage = entry.fields.usage;
|
|
284
268
|
if (!usage || usage.steps < IDLE_STEP_LIMIT)
|
|
285
269
|
continue;
|
|
286
|
-
const steps = (await readTaskLog(
|
|
270
|
+
const steps = (await readTaskLog(deskDir, entry.id, { kinds: ["step"] })).slice(-IDLE_STEP_LIMIT);
|
|
287
271
|
if (steps.length < IDLE_STEP_LIMIT)
|
|
288
272
|
continue;
|
|
289
273
|
const idle = steps.every((record) => record.kind === "step" && record.tools.length === 0);
|
|
290
274
|
if (!idle)
|
|
291
275
|
continue;
|
|
292
276
|
const reason = `连续 ${IDLE_STEP_LIMIT} 步没有任何工具调用,已停下等你确认`;
|
|
293
|
-
if (await pauseTask(
|
|
277
|
+
if (await pauseTask(deskDir, entry.id, { by: "runtime", reason })) {
|
|
294
278
|
paused = true;
|
|
295
279
|
log.logWarn("taskdriver.task.paused", "Task paused: idle loop", {
|
|
296
280
|
channelId: channelId,
|
|
297
281
|
reason: reason,
|
|
298
|
-
fields: { taskId: entry.id, cause: "idle" },
|
|
282
|
+
fields: { taskId: entry.id, desk, cause: "idle" },
|
|
299
283
|
});
|
|
300
|
-
await this.notifyStop(channelId, entry, reason, nowMs);
|
|
284
|
+
await this.notifyStop(desk, channelId, entry, reason, nowMs);
|
|
301
285
|
}
|
|
302
286
|
}
|
|
303
287
|
return paused;
|
|
304
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* The tasks of one desk the driver may touch at all (spec 058, D8): those whose delivery channel
|
|
291
|
+
* is bound to this desk and reachable. Every other task — no `channel`, or a channel bound elsewhere or nowhere, or on a transport that is off
|
|
292
|
+
* — is frozen: no ticket redemption, no backstop, no budget or idle check, no step, no receipt.
|
|
293
|
+
* Freezing reads nothing back into the file, so a rebound task resumes from exactly where it was.
|
|
294
|
+
*/
|
|
295
|
+
liveTasks(desk, entries) {
|
|
296
|
+
const live = [];
|
|
297
|
+
for (const entry of entries) {
|
|
298
|
+
const key = attemptKey(desk, entry.id);
|
|
299
|
+
const channelId = entry.fields.channel;
|
|
300
|
+
if (channelId && this.options.isBoundTo(channelId, desk) && this.options.isReachable(channelId)) {
|
|
301
|
+
if (this.suspended.delete(key)) {
|
|
302
|
+
log.logInfoEvent("taskdriver.task.resumed", "Task resumed: delivery channel bound again", {
|
|
303
|
+
channelId,
|
|
304
|
+
fields: { taskId: entry.id, desk },
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
live.push({ entry, channelId });
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (!this.suspended.has(key)) {
|
|
311
|
+
this.suspended.add(key);
|
|
312
|
+
log.logInfoEvent("taskdriver.task.suspended", "Task suspended: delivery channel not usable", {
|
|
313
|
+
channelId,
|
|
314
|
+
fields: {
|
|
315
|
+
taskId: entry.id,
|
|
316
|
+
desk,
|
|
317
|
+
reason: !channelId
|
|
318
|
+
? "channel_missing"
|
|
319
|
+
: this.options.isBoundTo(channelId, desk)
|
|
320
|
+
? "channel_unreachable"
|
|
321
|
+
: "channel_not_bound",
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return live;
|
|
327
|
+
}
|
|
305
328
|
async runOnce(now = new Date()) {
|
|
306
329
|
if (this.options.isEnabled?.() === false || this.running)
|
|
307
330
|
return;
|
|
@@ -310,105 +333,108 @@ export class TaskDriver {
|
|
|
310
333
|
this.running = true;
|
|
311
334
|
this.nextWakeMs = undefined;
|
|
312
335
|
try {
|
|
313
|
-
const
|
|
314
|
-
if (
|
|
336
|
+
const desks = this.options.getActiveDesks();
|
|
337
|
+
if (desks.length === 0) {
|
|
315
338
|
this.lastDispatchMs.clear();
|
|
316
339
|
return;
|
|
317
340
|
}
|
|
318
341
|
const seen = new Set();
|
|
319
|
-
const start = this.
|
|
342
|
+
const start = this.nextDeskIndex % desks.length;
|
|
320
343
|
let dispatched = 0;
|
|
321
344
|
let lastDispatchOffset = -1;
|
|
322
|
-
for (let offset = 0; offset <
|
|
323
|
-
const
|
|
324
|
-
if (!
|
|
345
|
+
for (let offset = 0; offset < desks.length; offset++) {
|
|
346
|
+
const target = desks[(start + offset) % desks.length];
|
|
347
|
+
if (!target)
|
|
325
348
|
continue;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
let
|
|
330
|
-
for (const entry of
|
|
331
|
-
seen.add(attemptKey(
|
|
332
|
-
if (await this.maintain(
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (dispatched >= settings.maxDispatchesPerTick || this.options.isChannelActive(channelId))
|
|
349
|
+
const { desk, deskDir } = target;
|
|
350
|
+
const tasksDir = join(deskDir, "tasks");
|
|
351
|
+
const readLive = async () => this.liveTasks(desk, await readActiveTasks(tasksDir, nowMs));
|
|
352
|
+
let live = await readLive();
|
|
353
|
+
for (const { entry } of live)
|
|
354
|
+
seen.add(attemptKey(desk, entry.id));
|
|
355
|
+
if (await this.maintain(desk, deskDir, live, now))
|
|
356
|
+
live = await readLive();
|
|
357
|
+
if (dispatched >= settings.maxDispatchesPerTick)
|
|
336
358
|
continue;
|
|
337
359
|
// A task that has already run out of budget must not spend one more model call to
|
|
338
360
|
// discover that (D6). Checked before dispatch, and paired with a receipt so the user
|
|
339
361
|
// learns why it stopped and what to type to give it more rope.
|
|
340
362
|
const budgeted = [];
|
|
341
|
-
for (const
|
|
363
|
+
for (const task of live) {
|
|
364
|
+
const { entry, channelId } = task;
|
|
342
365
|
if (!entry.runnable)
|
|
343
366
|
continue;
|
|
344
367
|
const status = checkBudget(entry.fields);
|
|
345
368
|
if (!status.breach || !status.reason) {
|
|
346
|
-
budgeted.push(
|
|
369
|
+
budgeted.push(task);
|
|
347
370
|
continue;
|
|
348
371
|
}
|
|
349
|
-
if (await pauseTask(
|
|
372
|
+
if (await pauseTask(deskDir, entry.id, { by: "runtime", reason: status.reason })) {
|
|
350
373
|
log.logWarn("taskdriver.task.paused", "Task paused: budget exhausted", {
|
|
351
374
|
channelId: channelId,
|
|
352
375
|
reason: status.reason,
|
|
353
|
-
fields: { taskId: entry.id, cause: `budget_${status.breach}` },
|
|
376
|
+
fields: { taskId: entry.id, desk, cause: `budget_${status.breach}` },
|
|
354
377
|
});
|
|
355
|
-
await this.notifyStop(channelId, entry, status.reason, nowMs, status.breach);
|
|
378
|
+
await this.notifyStop(desk, channelId, entry, status.reason, nowMs, status.breach);
|
|
356
379
|
}
|
|
357
380
|
}
|
|
358
|
-
const idled = await this.detectIdleLoop(
|
|
359
|
-
const ready = idled
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const candidates = ready.filter((entry) => !entry.fields.paused);
|
|
381
|
+
const idled = await this.detectIdleLoop(desk, deskDir, budgeted, nowMs);
|
|
382
|
+
const ready = idled ? (await readLive()).filter(({ entry }) => entry.runnable) : budgeted;
|
|
383
|
+
// The busy check is per task: a desk's tasks may deliver to different channels.
|
|
384
|
+
const candidates = ready.filter(({ entry, channelId }) => !entry.fields.paused && !this.options.isChannelActive(channelId));
|
|
363
385
|
if (candidates.length === 0)
|
|
364
386
|
continue;
|
|
365
|
-
const lastId = this.lastDispatchedTaskId.get(
|
|
366
|
-
const lastIndex = lastId ? candidates.findIndex((
|
|
387
|
+
const lastId = this.lastDispatchedTaskId.get(desk);
|
|
388
|
+
const lastIndex = lastId ? candidates.findIndex(({ entry }) => entry.id === lastId) : -1;
|
|
367
389
|
const rotated = lastIndex >= 0
|
|
368
390
|
? [...candidates.slice(lastIndex + 1), ...candidates.slice(0, lastIndex + 1)]
|
|
369
391
|
: candidates;
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
392
|
+
// One step per delivery channel per pass: a desk's tasks may deliver to different channels,
|
|
393
|
+
// whose queues run independently, so one channel's step must not hold back another's.
|
|
394
|
+
// Tasks sharing a channel still take turns (D6 rotation).
|
|
395
|
+
const usedChannels = new Set();
|
|
396
|
+
for (const { entry, channelId } of rotated) {
|
|
397
|
+
if (dispatched >= settings.maxDispatchesPerTick)
|
|
398
|
+
break;
|
|
399
|
+
if (usedChannels.has(channelId))
|
|
400
|
+
continue;
|
|
401
|
+
const previous = this.lastDispatchMs.get(attemptKey(desk, entry.id));
|
|
402
|
+
if (previous !== undefined && nowMs - previous < REDISPATCH_GUARD_MS) {
|
|
403
|
+
this.noteHorizon(previous + REDISPATCH_GUARD_MS, nowMs);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
usedChannels.add(channelId);
|
|
407
|
+
const event = createTaskDriverEvent(desk, channelId, entry, nowMs);
|
|
408
|
+
const accepted = await this.options.dispatch(event);
|
|
409
|
+
this.observeDispatch(event, accepted);
|
|
410
|
+
this.lastDispatchMs.set(attemptKey(desk, entry.id), nowMs);
|
|
411
|
+
this.lastDispatchedTaskId.set(desk, entry.id);
|
|
412
|
+
if (accepted) {
|
|
413
|
+
dispatched++;
|
|
414
|
+
lastDispatchOffset = offset;
|
|
415
|
+
log.logDebugEvent("taskdriver.step.enqueued", "Task step enqueued", {
|
|
416
|
+
channelId: channelId,
|
|
417
|
+
fields: { taskId: entry.id, desk },
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
log.logWarn("taskdriver.step.enqueue_failed", "Task step not enqueued: channel queue unavailable", {
|
|
422
|
+
channelId: channelId,
|
|
423
|
+
fields: { taskId: entry.id, desk },
|
|
424
|
+
});
|
|
379
425
|
}
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
const event = createTaskDriverEvent(channelId, entry, nowMs);
|
|
383
|
-
const accepted = await this.options.dispatch(event);
|
|
384
|
-
this.observeDispatch(event, accepted);
|
|
385
|
-
this.lastDispatchMs.set(attemptKey(channelId, entry.id), nowMs);
|
|
386
|
-
this.lastDispatchedTaskId.set(channelId, entry.id);
|
|
387
|
-
if (accepted) {
|
|
388
|
-
dispatched++;
|
|
389
|
-
lastDispatchOffset = offset;
|
|
390
|
-
log.logDebugEvent("taskdriver.step.enqueued", "Task step enqueued", {
|
|
391
|
-
channelId: channelId,
|
|
392
|
-
fields: { taskId: entry.id },
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
log.logWarn("taskdriver.step.enqueue_failed", "Task step not enqueued: channel queue unavailable", {
|
|
397
|
-
channelId: channelId,
|
|
398
|
-
fields: { taskId: entry.id },
|
|
399
|
-
});
|
|
400
426
|
}
|
|
401
427
|
}
|
|
402
428
|
for (const key of this.lastDispatchMs.keys()) {
|
|
403
429
|
if (!seen.has(key))
|
|
404
430
|
this.lastDispatchMs.delete(key);
|
|
405
431
|
}
|
|
406
|
-
const
|
|
407
|
-
for (const
|
|
408
|
-
if (!
|
|
409
|
-
this.lastDispatchedTaskId.delete(
|
|
432
|
+
const deskSet = new Set(desks.map((target) => target.desk));
|
|
433
|
+
for (const desk of this.lastDispatchedTaskId.keys()) {
|
|
434
|
+
if (!deskSet.has(desk))
|
|
435
|
+
this.lastDispatchedTaskId.delete(desk);
|
|
410
436
|
}
|
|
411
|
-
this.
|
|
437
|
+
this.nextDeskIndex = (start + (lastDispatchOffset >= 0 ? lastDispatchOffset + 1 : 1)) % desks.length;
|
|
412
438
|
}
|
|
413
439
|
finally {
|
|
414
440
|
this.running = false;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { SpawnTaskRequest, SpawnTaskResult } from "../events/types.js";
|
|
2
2
|
export interface TaskSpawnerOptions {
|
|
3
|
-
/** The
|
|
4
|
-
|
|
3
|
+
/** The event's desk directory: the instance is created in its `tasks/`. */
|
|
4
|
+
getDeskDir: (desk: string) => string;
|
|
5
5
|
/** Wake the driver so the new instance's first step is queued promptly. */
|
|
6
6
|
nudge: () => void;
|
|
7
|
-
/** Direct, non-LLM
|
|
8
|
-
notify: (channelId: string, text: string) => Promise<unknown>;
|
|
7
|
+
/** Direct, non-LLM receipt to the event's channel, through the desk's binding guard (D7). */
|
|
8
|
+
notify: (desk: string, channelId: string, text: string) => Promise<unknown>;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* Create the task instance a template event asks for (spec 052, D2).
|
|
@@ -20,16 +20,16 @@ import { archivedTaskPath, taskPath, tasksDir } from "../tasks/store.js";
|
|
|
20
20
|
*/
|
|
21
21
|
export function createTaskSpawner(options) {
|
|
22
22
|
return async (request) => {
|
|
23
|
-
const
|
|
23
|
+
const deskDir = options.getDeskDir(request.desk);
|
|
24
24
|
const id = `${request.eventName}-${instanceStamp(request.occurrence)}`;
|
|
25
|
-
if (existsSync(taskPath(
|
|
25
|
+
if (existsSync(taskPath(deskDir, id)) || existsSync(archivedTaskPath(deskDir, id))) {
|
|
26
26
|
return { outcome: "exists", id };
|
|
27
27
|
}
|
|
28
|
-
const active = (await readActiveTasks(tasksDir(
|
|
28
|
+
const active = (await readActiveTasks(tasksDir(deskDir))).find((entry) => entry.fields.origin === request.eventName && !entry.fields.outcome);
|
|
29
29
|
if (active) {
|
|
30
30
|
const state = `${taskStateLabel(active.fields.state)}${active.fields.paused ? ",已暂停" : ""}`;
|
|
31
31
|
await options
|
|
32
|
-
.notify(request.channelId, `⚠️ 周期任务 \`${request.eventName}\` 本次(${instanceStamp(request.occurrence)})未启动:上一个实例 \`${active.id}\` 仍未结束(${state})。\n查看:\`/tasks show ${active.id}\``)
|
|
32
|
+
.notify(request.desk, request.channelId, `⚠️ 周期任务 \`${request.eventName}\` 本次(${instanceStamp(request.occurrence)})未启动:上一个实例 \`${active.id}\` 仍未结束(${state})。\n查看:\`/tasks show ${active.id}\``)
|
|
33
33
|
.catch((error) => {
|
|
34
34
|
log.logWarn("tasks.spawn.receipt_failed", "Skip receipt delivery failed", {
|
|
35
35
|
channelId: request.channelId,
|
|
@@ -40,9 +40,10 @@ export function createTaskSpawner(options) {
|
|
|
40
40
|
return { outcome: "skipped", id: active.id };
|
|
41
41
|
}
|
|
42
42
|
await createTaskDocument({
|
|
43
|
-
|
|
43
|
+
deskDir,
|
|
44
44
|
id,
|
|
45
45
|
input: request.template,
|
|
46
|
+
channel: request.channelId,
|
|
46
47
|
origin: request.eventName,
|
|
47
48
|
now: request.occurrence,
|
|
48
49
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Claiming a background job's or delegation run's completion wake before it may reopen a parked
|
|
3
|
-
* task (spec 040, D7/T9). Pure functions over an explicit `event`/`
|
|
3
|
+
* task (spec 040, D7/T9). Pure functions over an explicit `event`/`deskDir`/owner, with no
|
|
4
4
|
* dependency on `createRuntimeContext`'s closures.
|
|
5
5
|
*
|
|
6
6
|
* Trust comes from two things only: the producer-created `internalWake` envelope (DingTalk
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* settled, names this task, and that the dispatch id is exactly the one its own completion wake
|
|
10
10
|
* carries.
|
|
11
11
|
*/
|
|
12
|
-
import type {
|
|
13
|
-
/** The slice of `
|
|
12
|
+
import type { WakeEvent } from "../channel/channel-event.js";
|
|
13
|
+
/** The slice of `DeskJobManager` / `SubAgentRunManager` a wake claim needs. */
|
|
14
14
|
export interface WakeOwner {
|
|
15
15
|
beginWakeConsumption(id: string, taskId: string, dispatchId: string): Promise<boolean>;
|
|
16
16
|
finishWakeConsumption(id: string, dispatchId: string): Promise<void>;
|
|
@@ -43,4 +43,6 @@ export interface ClaimedWake {
|
|
|
43
43
|
* waiting for it, and reopening it here would be exactly the unverified resumption path spec 051
|
|
44
44
|
* exists to remove.
|
|
45
45
|
*/
|
|
46
|
-
export declare function claimVerifiedWake(event:
|
|
46
|
+
export declare function claimVerifiedWake(event: WakeEvent,
|
|
47
|
+
/** The admitted wake's desk directory — where the task it names lives. */
|
|
48
|
+
deskDir: string, owner: WakeOwner, hooks?: WakeTaskTransitionHooks): Promise<ClaimedWake | undefined>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { getChannelDir } from "../channel/channel-paths.js";
|
|
2
1
|
import { readStoredTask, redeemTicket } from "../tasks/store.js";
|
|
3
|
-
async function isTaskActivelyDriven(
|
|
4
|
-
const document = await readStoredTask(
|
|
2
|
+
async function isTaskActivelyDriven(deskDir, taskId) {
|
|
3
|
+
const document = await readStoredTask(deskDir, taskId).catch(() => undefined);
|
|
5
4
|
return document !== undefined && document.fields.state === "open" && !document.fields.paused;
|
|
6
5
|
}
|
|
7
6
|
/**
|
|
@@ -13,20 +12,21 @@ async function isTaskActivelyDriven(channelDir, taskId) {
|
|
|
13
12
|
* waiting for it, and reopening it here would be exactly the unverified resumption path spec 051
|
|
14
13
|
* exists to remove.
|
|
15
14
|
*/
|
|
16
|
-
export async function claimVerifiedWake(event,
|
|
15
|
+
export async function claimVerifiedWake(event,
|
|
16
|
+
/** The admitted wake's desk directory — where the task it names lives. */
|
|
17
|
+
deskDir, owner, hooks) {
|
|
17
18
|
const wake = event.internalWake;
|
|
18
19
|
if (!wake || typeof event.dispatchId !== "string")
|
|
19
20
|
return undefined;
|
|
20
21
|
if (!(await owner.beginWakeConsumption(wake.resourceId, wake.taskId, event.dispatchId)))
|
|
21
22
|
return undefined;
|
|
22
23
|
const dispatchId = event.dispatchId;
|
|
23
|
-
const channelDir = getChannelDir(workspaceDir, event.channelId);
|
|
24
24
|
hooks?.beforeActivation?.();
|
|
25
|
-
const redeemed = await redeemTicket(
|
|
25
|
+
const redeemed = await redeemTicket(deskDir, wake.taskId, (ticket) => ticket.kind === "work");
|
|
26
26
|
return {
|
|
27
27
|
taskId: wake.taskId,
|
|
28
28
|
activated: redeemed !== undefined,
|
|
29
|
-
taskStillDriven: redeemed !== undefined || (await isTaskActivelyDriven(
|
|
29
|
+
taskStillDriven: redeemed !== undefined || (await isTaskActivelyDriven(deskDir, wake.taskId)),
|
|
30
30
|
finish: () => owner.finishWakeConsumption(wake.resourceId, dispatchId),
|
|
31
31
|
};
|
|
32
32
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { BootstrapPaths } from "../app/home.js";
|
|
2
|
+
import type { Ingress } from "../channel/ingress.js";
|
|
3
|
+
import type { Transport } from "../channel/transport.js";
|
|
4
|
+
import type { ConversationKind } from "../desk/types.js";
|
|
5
|
+
import type { SecurityLogEvent } from "../security/types.js";
|
|
6
|
+
export interface TransportRegistry {
|
|
7
|
+
/** The transport that owns `channelId`; `undefined` when its transport is not hosted by this process. */
|
|
8
|
+
forChannel(channelId: string): Transport | undefined;
|
|
9
|
+
/** Whether a message can be delivered to `channelId` right now. */
|
|
10
|
+
isReachable(channelId: string): boolean;
|
|
11
|
+
all(): readonly Transport[];
|
|
12
|
+
conversationKindOf(channelId: string): ConversationKind | undefined;
|
|
13
|
+
/** How a channel is named to a user; an unreachable channel falls back to its id. */
|
|
14
|
+
describeChannel(channelId: string, knownName?: string): string;
|
|
15
|
+
startAll(ingress: Ingress): Promise<void>;
|
|
16
|
+
/** Every transport stops taking messages and keeps delivering (see `Transport.stopAccepting`). */
|
|
17
|
+
stopAcceptingAll(): Promise<void>;
|
|
18
|
+
stopAll(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export declare function createTransportRegistry(transports: readonly Transport[]): TransportRegistry;
|
|
21
|
+
/**
|
|
22
|
+
* Build the transports `channel.json` turns on. A transport's code is imported only when its turn
|
|
23
|
+
* comes, so a disabled transport costs nothing — not its module, not its dependencies, not a
|
|
24
|
+
* listener (INV-7). A configuration that cannot be used, or that turns nothing on, refuses to start.
|
|
25
|
+
*/
|
|
26
|
+
export declare function loadTransports(deps: {
|
|
27
|
+
paths: BootstrapPaths;
|
|
28
|
+
stateDir: string;
|
|
29
|
+
archiveBotResponse: (channelId: string, text: string) => Promise<void>;
|
|
30
|
+
/** The security audit log, for the transports that take requests from the network. */
|
|
31
|
+
audit: (event: SecurityLogEvent) => void;
|
|
32
|
+
}): Promise<Transport[]>;
|