@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
package/dist/tasks/commands.js
CHANGED
|
@@ -7,7 +7,6 @@ import { readdir } from "node:fs/promises";
|
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { renderSubcommandUsage } from "../commands/catalog.js";
|
|
9
9
|
import { capReply, flattenMarkdownForChat } from "../commands/reply-limits.js";
|
|
10
|
-
import { docsUrl } from "../paths.js";
|
|
11
10
|
import { formatSpanZh } from "../shared/format.js";
|
|
12
11
|
import { parseLocalTime } from "../shared/local-time.js";
|
|
13
12
|
import { errorMessage } from "../shared/text-utils.js";
|
|
@@ -80,8 +79,8 @@ export function parseTasksCommand(args) {
|
|
|
80
79
|
}
|
|
81
80
|
throw new Error(`未知子命令 \`${action}\`。`);
|
|
82
81
|
}
|
|
83
|
-
function tasksDir(
|
|
84
|
-
return join(
|
|
82
|
+
function tasksDir(deskDir) {
|
|
83
|
+
return join(deskDir, "tasks");
|
|
85
84
|
}
|
|
86
85
|
/** `(最晚 3 小时后重新检查)` — when the runtime looks at a parked task again no matter what. */
|
|
87
86
|
function backstopHint(by, now) {
|
|
@@ -102,8 +101,21 @@ function ticketSummary(entry, now) {
|
|
|
102
101
|
const ticket = entry.fields.ticket;
|
|
103
102
|
return ticket ? `${describeTicketForUser(ticket)}${backstopHint(ticket.by, now)}` : undefined;
|
|
104
103
|
}
|
|
105
|
-
|
|
104
|
+
/** The delivery-channel note for a task, or `undefined` when it delivers here and is live. */
|
|
105
|
+
function deliveryNote(channel, options) {
|
|
106
|
+
if (!channel)
|
|
107
|
+
return "已暂停:未指定投递频道";
|
|
108
|
+
const described = options.describeChannel(channel);
|
|
109
|
+
if (!described.boundHere)
|
|
110
|
+
return `已暂停:投递频道未绑定到本工位(${described.label})`;
|
|
111
|
+
return channel === options.currentChannelId ? undefined : `投递到${described.label}`;
|
|
112
|
+
}
|
|
113
|
+
function renderListLine(entry, options, now) {
|
|
114
|
+
const { deskDir } = options;
|
|
106
115
|
const bits = [`\`${entry.id}\` ${entry.title}`, taskStateLabel(entry.fields.state)];
|
|
116
|
+
const delivery = deliveryNote(entry.fields.channel, options);
|
|
117
|
+
if (delivery)
|
|
118
|
+
bits.push(delivery);
|
|
107
119
|
if (entry.fields.paused)
|
|
108
120
|
bits.push(`已暂停(${entry.fields.paused.reason})`);
|
|
109
121
|
const ticket = ticketSummary(entry, now);
|
|
@@ -116,21 +128,21 @@ function renderListLine(entry, channelDir, now) {
|
|
|
116
128
|
const usage = usageSummary(entry);
|
|
117
129
|
if (usage)
|
|
118
130
|
bits.push(usage);
|
|
119
|
-
if (hasTaskSteer(
|
|
131
|
+
if (hasTaskSteer(deskDir, entry.id))
|
|
120
132
|
bits.push("有待处理的指示");
|
|
121
133
|
if (!entry.readable)
|
|
122
134
|
bits.push("⚠️ 文件头(frontmatter)不可读");
|
|
123
135
|
return `- ${bits.join(" · ")}`;
|
|
124
136
|
}
|
|
125
|
-
async function listTasks(
|
|
137
|
+
async function listTasks(options) {
|
|
126
138
|
const now = Date.now();
|
|
127
|
-
const entries = (await readActiveTasks(tasksDir(
|
|
139
|
+
const entries = (await readActiveTasks(tasksDir(options.deskDir), now)).filter((entry) => !entry.fields.outcome);
|
|
128
140
|
if (entries.length === 0) {
|
|
129
141
|
return "暂无进行中的任务。\n在对话里告诉我目标,并说明作为长程任务推进。";
|
|
130
142
|
}
|
|
131
143
|
const report = [
|
|
132
144
|
`**任务(${entries.length})**`,
|
|
133
|
-
...entries.map((entry) => renderListLine(entry,
|
|
145
|
+
...entries.map((entry) => renderListLine(entry, options, now)),
|
|
134
146
|
].join("\n");
|
|
135
147
|
return capReply(report, { nextStepHint: "用 `/tasks show <id>` 查看单个任务" }).text;
|
|
136
148
|
}
|
|
@@ -148,15 +160,23 @@ function contractExcerpt(body, maxChars) {
|
|
|
148
160
|
const openFence = (head.match(/^```/gm)?.length ?? 0) % 2 === 1;
|
|
149
161
|
return { text: openFence ? `${head}\n\`\`\`` : head, clipped: true };
|
|
150
162
|
}
|
|
151
|
-
async function showTask(
|
|
163
|
+
async function showTask(options, idInput) {
|
|
164
|
+
const { deskDir } = options;
|
|
152
165
|
const id = normalizeTaskId(idInput);
|
|
153
|
-
const document = (await readStoredTask(
|
|
166
|
+
const document = (await readStoredTask(deskDir, id, true)) ?? undefined;
|
|
154
167
|
if (!document)
|
|
155
168
|
return notFound(id);
|
|
156
169
|
const now = Date.now();
|
|
157
|
-
const entry = (await readActiveTasks(tasksDir(
|
|
170
|
+
const entry = (await readActiveTasks(tasksDir(deskDir), now)).find((candidate) => candidate.id === id);
|
|
158
171
|
const head = [`**任务 ${id}**`];
|
|
159
172
|
head.push(`- 状态:${taskStateLabel(document.fields.state)}`);
|
|
173
|
+
const channel = document.fields.channel;
|
|
174
|
+
if (!document.fields.outcome) {
|
|
175
|
+
const note = deliveryNote(channel, options);
|
|
176
|
+
head.push(channel
|
|
177
|
+
? `- 投递频道(channel):${options.describeChannel(channel).label}${note?.startsWith("已暂停") ? `(${note})` : ""}`
|
|
178
|
+
: `- 投递频道:未指定(${note})`);
|
|
179
|
+
}
|
|
160
180
|
if (document.fields.paused) {
|
|
161
181
|
const by = document.fields.paused.by === "user" ? "由你暂停" : "由系统暂停";
|
|
162
182
|
head.push(`- 已暂停(${by}):${document.fields.paused.reason}`);
|
|
@@ -170,14 +190,14 @@ async function showTask(channelDir, idInput) {
|
|
|
170
190
|
const usage = entry ? usageSummary(entry) : undefined;
|
|
171
191
|
if (usage)
|
|
172
192
|
head.push(`- 用量:${usage}`);
|
|
173
|
-
const board = await buildTaskBoard(
|
|
193
|
+
const board = await buildTaskBoard(deskDir, id, document.body);
|
|
174
194
|
const boardLines = board ? ["", "**工作项**", renderTaskBoardForUser(board, id, now)] : [];
|
|
175
|
-
const log = await readTaskLog(
|
|
176
|
-
kinds: ["step", "expired", "close"
|
|
195
|
+
const log = await readTaskLog(deskDir, id, {
|
|
196
|
+
kinds: ["step", "expired", "close"],
|
|
177
197
|
limit: SHOW_LOG_LINES,
|
|
178
198
|
});
|
|
179
199
|
const logLines = log.length > 0 ? ["", "**最近记录**", ...log.map(renderTaskLogLineForUser)] : [];
|
|
180
|
-
const path = existsSync(taskPath(
|
|
200
|
+
const path = existsSync(taskPath(deskDir, id)) ? taskPath(deskDir, id) : archivedTaskPath(deskDir, id);
|
|
181
201
|
const contract = contractExcerpt(document.body, TASK_SHOW_CONTRACT_CHARS);
|
|
182
202
|
const report = [
|
|
183
203
|
...head,
|
|
@@ -193,8 +213,8 @@ async function showTask(channelDir, idInput) {
|
|
|
193
213
|
return capReply(report, { nextStepHint: `用 \`/tasks log ${id}\` 查看记录,完整任务说明在 \`${path}\`` }).text;
|
|
194
214
|
}
|
|
195
215
|
const ARCHIVE_OUTCOME = { completed: "已完成", cancelled: "已取消" };
|
|
196
|
-
async function listArchive(
|
|
197
|
-
const archiveDir = join(tasksDir(
|
|
216
|
+
async function listArchive(deskDir) {
|
|
217
|
+
const archiveDir = join(tasksDir(deskDir), "archive");
|
|
198
218
|
if (!existsSync(archiveDir))
|
|
199
219
|
return "暂无已归档任务。\n任务完成或取消后会出现在这里。";
|
|
200
220
|
const files = (await readdir(archiveDir)).filter((name) => name.endsWith(".md")).sort();
|
|
@@ -203,7 +223,7 @@ async function listArchive(channelDir) {
|
|
|
203
223
|
const lines = [];
|
|
204
224
|
for (const filename of files.slice(0, ARCHIVE_LIST_LIMIT)) {
|
|
205
225
|
const id = filename.slice(0, -".md".length);
|
|
206
|
-
const document = await readStoredTask(
|
|
226
|
+
const document = await readStoredTask(deskDir, id, true).catch(() => undefined);
|
|
207
227
|
const outcome = document?.fields.outcome;
|
|
208
228
|
const bits = [`\`${id}\``];
|
|
209
229
|
if (outcome)
|
|
@@ -219,13 +239,13 @@ async function listArchive(channelDir) {
|
|
|
219
239
|
nextStepHint: `完整列表在 \`${archiveDir}\``,
|
|
220
240
|
}).text;
|
|
221
241
|
}
|
|
222
|
-
async function showTaskLog(
|
|
242
|
+
async function showTaskLog(deskDir, idInput) {
|
|
223
243
|
const id = normalizeTaskId(idInput);
|
|
224
|
-
const all = await readTaskLog(
|
|
244
|
+
const all = await readTaskLog(deskDir, id);
|
|
225
245
|
if (all.length === 0)
|
|
226
246
|
return `任务 \`${id}\` 暂无记录。\n任务执行第一步后会出现在这里。`;
|
|
227
247
|
const records = all.slice(-LOG_LINES);
|
|
228
|
-
const path = taskLogIsArchived(
|
|
248
|
+
const path = taskLogIsArchived(deskDir, id) ? taskArchiveLogPath(deskDir, id) : taskLogPath(deskDir, id);
|
|
229
249
|
const lines = [
|
|
230
250
|
`**任务 ${id} 的记录(最近 ${records.length} 条,共 ${all.length} 条)**`,
|
|
231
251
|
...records.map(renderTaskLogLineForUser),
|
|
@@ -236,7 +256,7 @@ async function showTaskLog(channelDir, idInput) {
|
|
|
236
256
|
}
|
|
237
257
|
async function pauseTask(options, idInput) {
|
|
238
258
|
const id = normalizeTaskId(idInput);
|
|
239
|
-
const document = await pauseTaskDocument(options.
|
|
259
|
+
const document = await pauseTaskDocument(options.deskDir, id, {
|
|
240
260
|
by: "user",
|
|
241
261
|
reason: "用户通过 /tasks pause 暂停。",
|
|
242
262
|
});
|
|
@@ -246,12 +266,12 @@ async function pauseTask(options, idInput) {
|
|
|
246
266
|
}
|
|
247
267
|
export async function resumeTask(options, idInput, grants = {}) {
|
|
248
268
|
const id = normalizeTaskId(idInput);
|
|
249
|
-
const document = await resumeTaskDocument(options.
|
|
269
|
+
const document = await resumeTaskDocument(options.deskDir, id);
|
|
250
270
|
if (!document)
|
|
251
271
|
return notFound(id);
|
|
252
272
|
const granted = [];
|
|
253
273
|
if (Object.keys(grants).length > 0) {
|
|
254
|
-
await updateStoredTask(options.
|
|
274
|
+
await updateStoredTask(options.deskDir, id, (task) => {
|
|
255
275
|
const budget = { ...task.fields.budget };
|
|
256
276
|
for (const key of ["steps", "usd"]) {
|
|
257
277
|
const extra = grants[key];
|
|
@@ -271,10 +291,10 @@ export async function resumeTask(options, idInput, grants = {}) {
|
|
|
271
291
|
}
|
|
272
292
|
async function steerTask(options, idInput, text) {
|
|
273
293
|
const id = normalizeTaskId(idInput);
|
|
274
|
-
const document = await readStoredTask(options.
|
|
294
|
+
const document = await readStoredTask(options.deskDir, id);
|
|
275
295
|
if (!document)
|
|
276
296
|
return notFound(id);
|
|
277
|
-
await queueTaskSteer(options.
|
|
297
|
+
await queueTaskSteer(options.deskDir, id, text);
|
|
278
298
|
return `已把这条指示排给任务 \`${id}\` 的下一步,不会打断正在执行的这一步。`;
|
|
279
299
|
}
|
|
280
300
|
/**
|
|
@@ -284,11 +304,11 @@ async function steerTask(options, idInput, text) {
|
|
|
284
304
|
*/
|
|
285
305
|
async function replyToTask(options, idInput, text) {
|
|
286
306
|
const id = normalizeTaskId(idInput);
|
|
287
|
-
const document = await readStoredTask(options.
|
|
307
|
+
const document = await readStoredTask(options.deskDir, id);
|
|
288
308
|
if (!document)
|
|
289
309
|
return notFound(id);
|
|
290
|
-
await queueTaskSteer(options.
|
|
291
|
-
const redeemed = await redeemTicket(options.
|
|
310
|
+
await queueTaskSteer(options.deskDir, id, text, "用户回答");
|
|
311
|
+
const redeemed = await redeemTicket(options.deskDir, id, (ticket) => ticket.kind === "ask");
|
|
292
312
|
if (!redeemed) {
|
|
293
313
|
return `任务 \`${id}\` 当前并没有在等你的回答;内容已记下,下一步会看到。`;
|
|
294
314
|
}
|
|
@@ -303,20 +323,23 @@ function issue(problem, nextStep) {
|
|
|
303
323
|
* `/tasks doctor`, deliberately small (spec 051, D11; spec 052, D11).
|
|
304
324
|
*
|
|
305
325
|
* Most of what older doctors looked for cannot be produced any more: the frontmatter normalizer
|
|
306
|
-
* and `resolveTicket` reject those states at write time. What is left is what a *hand edit
|
|
307
|
-
*
|
|
326
|
+
* and `resolveTicket` reject those states at write time. What is left is what a *hand edit* can
|
|
327
|
+
* still create.
|
|
308
328
|
*/
|
|
309
329
|
async function doctor(options) {
|
|
310
330
|
const now = Date.now();
|
|
311
|
-
const entries = await readActiveTasks(tasksDir(options.
|
|
331
|
+
const entries = await readActiveTasks(tasksDir(options.deskDir), now);
|
|
312
332
|
const problems = [];
|
|
313
333
|
for (const entry of entries) {
|
|
314
334
|
if (!entry.readable) {
|
|
315
|
-
problems.push(issue(`\`${entry.id}\` 的文件头(frontmatter)不可读`, `让我修复 \`${taskPath(options.
|
|
335
|
+
problems.push(issue(`\`${entry.id}\` 的文件头(frontmatter)不可读`, `让我修复 \`${taskPath(options.deskDir, entry.id)}\` 的文件头,或手动修改`));
|
|
316
336
|
continue;
|
|
317
337
|
}
|
|
318
|
-
if (entry.
|
|
319
|
-
problems.push(issue(`\`${entry.id}\`
|
|
338
|
+
if (!entry.fields.channel) {
|
|
339
|
+
problems.push(issue(`\`${entry.id}\` 没有投递频道(channel),已暂停`, `在 \`${taskPath(options.deskDir, entry.id)}\` 的文件头加一行 \`channel: <频道 ID>\``));
|
|
340
|
+
}
|
|
341
|
+
else if (!options.describeChannel(entry.fields.channel).boundHere) {
|
|
342
|
+
problems.push(issue(`\`${entry.id}\` 的投递频道没有绑定到本工位,已暂停`, "在那个频道里把它绑定回本工位,或把文件头的 `channel:` 改成本工位已绑定的频道"));
|
|
320
343
|
}
|
|
321
344
|
const by = entry.fields.ticket ? parseLocalTime(entry.fields.ticket.by) : undefined;
|
|
322
345
|
if (entry.expired && !entry.fields.paused && by !== undefined && by < now - EXPIRED_GRACE_MS) {
|
|
@@ -325,7 +348,9 @@ async function doctor(options) {
|
|
|
325
348
|
}
|
|
326
349
|
if (problems.length === 0)
|
|
327
350
|
return `检查了 ${entries.length} 个任务,未发现问题。`;
|
|
328
|
-
return [`**任务体检(${problems.length} 项)**`, ...problems].join("\n")
|
|
351
|
+
return capReply([`**任务体检(${problems.length} 项)**`, ...problems].join("\n"), {
|
|
352
|
+
nextStepHint: "逐个修复后再运行 `/tasks doctor` 查看剩余的问题",
|
|
353
|
+
}).text;
|
|
329
354
|
}
|
|
330
355
|
export async function handleTasksCommand(options) {
|
|
331
356
|
let command;
|
|
@@ -337,7 +362,7 @@ export async function handleTasksCommand(options) {
|
|
|
337
362
|
}
|
|
338
363
|
try {
|
|
339
364
|
if ("id" in command && command.id && command.action !== "show" && command.action !== "log") {
|
|
340
|
-
return await withTaskMutation(options.
|
|
365
|
+
return await withTaskMutation(options.deskDir, command.id, () => dispatchTasksCommand(options, command));
|
|
341
366
|
}
|
|
342
367
|
return await dispatchTasksCommand(options, command);
|
|
343
368
|
}
|
|
@@ -348,13 +373,13 @@ export async function handleTasksCommand(options) {
|
|
|
348
373
|
async function dispatchTasksCommand(options, command) {
|
|
349
374
|
switch (command.action) {
|
|
350
375
|
case "list":
|
|
351
|
-
return await listTasks(options
|
|
376
|
+
return await listTasks(options);
|
|
352
377
|
case "show":
|
|
353
|
-
return await showTask(options
|
|
378
|
+
return await showTask(options, command.id);
|
|
354
379
|
case "log":
|
|
355
|
-
return await showTaskLog(options.
|
|
380
|
+
return await showTaskLog(options.deskDir, command.id);
|
|
356
381
|
case "archive":
|
|
357
|
-
return await listArchive(options.
|
|
382
|
+
return await listArchive(options.deskDir);
|
|
358
383
|
case "pause":
|
|
359
384
|
return await pauseTask(options, command.id);
|
|
360
385
|
case "resume":
|
package/dist/tasks/contract.d.ts
CHANGED
|
@@ -17,8 +17,6 @@ export interface TaskLedgerEntry {
|
|
|
17
17
|
fields: TaskFrontmatter;
|
|
18
18
|
/** false => frontmatter could not be read; the task is surfaced rather than skipped. */
|
|
19
19
|
readable: boolean;
|
|
20
|
-
/** True when this file still carries v3 frontmatter the conversion should have removed; it is never run. */
|
|
21
|
-
legacy: boolean;
|
|
22
20
|
/** The driver may schedule a step for this task right now. */
|
|
23
21
|
runnable: boolean;
|
|
24
22
|
/** When a driver-polled `time` ticket comes due, if it has not already. */
|
package/dist/tasks/contract.js
CHANGED
|
@@ -295,7 +295,6 @@ function toEntry(id, content) {
|
|
|
295
295
|
title: extractTaskTitle(content, id),
|
|
296
296
|
fields: parsed.fields,
|
|
297
297
|
readable: parsed.readable,
|
|
298
|
-
legacy: parsed.legacy,
|
|
299
298
|
items: parseTaskItems(content),
|
|
300
299
|
};
|
|
301
300
|
}
|
|
@@ -305,7 +304,7 @@ function withClock(entry, now) {
|
|
|
305
304
|
const ticket = entry.fields.ticket;
|
|
306
305
|
return {
|
|
307
306
|
...entry,
|
|
308
|
-
runnable: isTaskRunnable(entry.fields)
|
|
307
|
+
runnable: isTaskRunnable(entry.fields),
|
|
309
308
|
dueMs: ticket ? ticketDueMs(ticket) : undefined,
|
|
310
309
|
expired: ticket !== undefined && !entry.fields.paused && ticketExpired(ticket, nowDate),
|
|
311
310
|
};
|
|
@@ -355,7 +354,6 @@ export async function readActiveTasks(tasksDir, now = Date.now()) {
|
|
|
355
354
|
title: id,
|
|
356
355
|
fields: { state: "open" },
|
|
357
356
|
readable: false,
|
|
358
|
-
legacy: false,
|
|
359
357
|
runnable: true,
|
|
360
358
|
expired: false,
|
|
361
359
|
});
|
package/dist/tasks/create.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { TaskCreateInput } from "./contract-input.js";
|
|
2
2
|
import type { StoredTaskDocument } from "./store.js";
|
|
3
3
|
export interface CreateTaskDocumentOptions {
|
|
4
|
-
|
|
4
|
+
deskDir: string;
|
|
5
5
|
id: string;
|
|
6
6
|
input: TaskCreateInput;
|
|
7
|
+
/** The delivery channel (spec 058, D13): the creating turn's channel, or the spawning event's. */
|
|
8
|
+
channel: string;
|
|
7
9
|
/** The event whose template spawned this instance. */
|
|
8
10
|
origin?: string;
|
|
9
11
|
now?: Date;
|
package/dist/tasks/create.js
CHANGED
|
@@ -5,29 +5,35 @@ import { formatLocalTime } from "../shared/local-time.js";
|
|
|
5
5
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
6
6
|
import { createUsage } from "./budget.js";
|
|
7
7
|
import { normalizeTaskId, renderStandardTaskBody, renderTaskDocument } from "./contract.js";
|
|
8
|
+
import { withTaskMutation } from "./mutation-lock.js";
|
|
8
9
|
import { archivedTaskPath, taskPath, tasksDir } from "./store.js";
|
|
9
10
|
/**
|
|
10
11
|
* Write a new task file, `open`, with its usage clock started. Shared by `task_create` and the
|
|
11
12
|
* event-template spawner so both produce byte-identical documents.
|
|
12
13
|
*/
|
|
13
|
-
export
|
|
14
|
+
export function createTaskDocument(options) {
|
|
15
|
+
// Locked like every other task write, so two processes cannot both pass the existence check.
|
|
16
|
+
return withTaskMutation(options.deskDir, options.id, () => writeNewTask(options));
|
|
17
|
+
}
|
|
18
|
+
async function writeNewTask(options) {
|
|
14
19
|
const id = normalizeTaskId(options.id);
|
|
15
|
-
const path = taskPath(options.
|
|
20
|
+
const path = taskPath(options.deskDir, id);
|
|
16
21
|
if (existsSync(path)) {
|
|
17
22
|
throw new RecoverableToolError(`Task "${id}" already exists; use task_update or edit the body instead.`);
|
|
18
23
|
}
|
|
19
|
-
if (existsSync(archivedTaskPath(options.
|
|
24
|
+
if (existsSync(archivedTaskPath(options.deskDir, id))) {
|
|
20
25
|
throw new RecoverableToolError(`Archived task "${id}" already exists; choose a new id or restore it manually first.`);
|
|
21
26
|
}
|
|
22
27
|
const now = options.now ?? new Date();
|
|
23
28
|
const fields = {
|
|
24
29
|
state: "open",
|
|
30
|
+
channel: options.channel,
|
|
25
31
|
origin: options.origin,
|
|
26
32
|
usage: createUsage(now),
|
|
27
33
|
budget: options.input.budget,
|
|
28
34
|
};
|
|
29
35
|
const body = renderStandardTaskBody(options.input);
|
|
30
|
-
await writeFileAtomically(join(tasksDir(options.
|
|
36
|
+
await writeFileAtomically(join(tasksDir(options.deskDir), `${id}.md`), renderTaskDocument(fields, body));
|
|
31
37
|
return { id, path, fields, body };
|
|
32
38
|
}
|
|
33
39
|
/** The local-time stamp spawned instances carry in their id: `YYYYMMDD-HHmm`. */
|
|
@@ -31,6 +31,12 @@ export type TaskArchiveOutcome = "completed" | "cancelled";
|
|
|
31
31
|
export interface TaskFrontmatter {
|
|
32
32
|
state: TaskState;
|
|
33
33
|
paused?: TaskPaused;
|
|
34
|
+
/**
|
|
35
|
+
* The delivery channel: where every step, stop receipt and report of this task goes (spec 058,
|
|
36
|
+
* D13). Plain text so a human can retarget it by hand. Missing, or not bound to this task's desk,
|
|
37
|
+
* means the task is suspended — the driver leaves it frozen until the channel is bound again.
|
|
38
|
+
*/
|
|
39
|
+
channel?: string;
|
|
34
40
|
/** Present iff `state === "parked"` (INV-1). */
|
|
35
41
|
ticket?: Ticket;
|
|
36
42
|
usage?: TaskUsage;
|
|
@@ -45,8 +51,6 @@ export interface ParsedTaskFrontmatter {
|
|
|
45
51
|
fields: TaskFrontmatter;
|
|
46
52
|
/** false => the block could not be read at all; the task is surfaced rather than skipped. */
|
|
47
53
|
readable: boolean;
|
|
48
|
-
/** True when a v3 line (`status`, `control`, `schedule`…) is present — the file is not converted, so it never runs. */
|
|
49
|
-
legacy: boolean;
|
|
50
54
|
}
|
|
51
55
|
/**
|
|
52
56
|
* The one user-facing name for a task state (spec 053, D8). `/tasks`, the stop receipt and the
|
|
@@ -11,9 +11,7 @@ export function taskStateLabel(state) {
|
|
|
11
11
|
return STATE_LABELS[state] ?? state;
|
|
12
12
|
}
|
|
13
13
|
/** Fixed render order, so a diff of a task file reads the same way every time. */
|
|
14
|
-
const FIELD_ORDER = ["state", "paused", "origin", "ticket", "usage", "budget"];
|
|
15
|
-
/** v3 frontmatter keys; their presence means the one-time conversion has not run on this file. */
|
|
16
|
-
const LEGACY_KEYS = new Set(["status", "enabled", "control", "wake", "schedule"]);
|
|
14
|
+
const FIELD_ORDER = ["state", "paused", "channel", "origin", "ticket", "usage", "budget"];
|
|
17
15
|
function parseJsonObject(raw) {
|
|
18
16
|
try {
|
|
19
17
|
const value = JSON.parse(raw);
|
|
@@ -67,12 +65,11 @@ function parseBudget(raw) {
|
|
|
67
65
|
*/
|
|
68
66
|
export function parseTaskFrontmatter(content) {
|
|
69
67
|
if (!content.startsWith("---"))
|
|
70
|
-
return { fields: { state: "open" }, readable: false
|
|
68
|
+
return { fields: { state: "open" }, readable: false };
|
|
71
69
|
const end = content.indexOf("\n---", 3);
|
|
72
70
|
if (end === -1)
|
|
73
|
-
return { fields: { state: "open" }, readable: false
|
|
71
|
+
return { fields: { state: "open" }, readable: false };
|
|
74
72
|
const fields = { state: "open" };
|
|
75
|
-
let legacy = false;
|
|
76
73
|
let sawState = false;
|
|
77
74
|
for (const line of content.slice(3, end).split("\n")) {
|
|
78
75
|
const idx = line.indexOf(":");
|
|
@@ -102,6 +99,9 @@ export function parseTaskFrontmatter(content) {
|
|
|
102
99
|
case "origin":
|
|
103
100
|
fields.origin = value || undefined;
|
|
104
101
|
break;
|
|
102
|
+
case "channel":
|
|
103
|
+
fields.channel = value || undefined;
|
|
104
|
+
break;
|
|
105
105
|
case "outcome":
|
|
106
106
|
if (value === "completed" || value === "cancelled")
|
|
107
107
|
fields.outcome = value;
|
|
@@ -110,16 +110,14 @@ export function parseTaskFrontmatter(content) {
|
|
|
110
110
|
fields.closedAt = value || undefined;
|
|
111
111
|
break;
|
|
112
112
|
default:
|
|
113
|
-
if (LEGACY_KEYS.has(key))
|
|
114
|
-
legacy = true;
|
|
115
113
|
break;
|
|
116
114
|
}
|
|
117
115
|
}
|
|
118
116
|
if (fields.outcome)
|
|
119
117
|
fields.state = "done";
|
|
120
118
|
// A file with no recognizable marker is not frontmatter we understand.
|
|
121
|
-
const readable = sawState || fields.outcome !== undefined
|
|
122
|
-
return { fields: normalizeTaskFrontmatter(fields), readable
|
|
119
|
+
const readable = sawState || fields.outcome !== undefined;
|
|
120
|
+
return { fields: normalizeTaskFrontmatter(fields), readable };
|
|
123
121
|
}
|
|
124
122
|
/**
|
|
125
123
|
* Enforce INV-1 on every write and read: `parked` requires a ticket, and a ticket only means
|
|
@@ -148,6 +146,8 @@ export function renderTaskFrontmatter(fields) {
|
|
|
148
146
|
lines.push(`outcome: ${document.outcome}`);
|
|
149
147
|
if (document.closedAt)
|
|
150
148
|
lines.push(`closedAt: ${document.closedAt}`);
|
|
149
|
+
if (document.channel)
|
|
150
|
+
lines.push(`channel: ${document.channel}`);
|
|
151
151
|
if (document.origin)
|
|
152
152
|
lines.push(`origin: ${document.origin}`);
|
|
153
153
|
if (document.usage)
|
package/dist/tasks/loop-log.d.ts
CHANGED
|
@@ -78,26 +78,20 @@ export interface TaskCloseRecord {
|
|
|
78
78
|
steps: number;
|
|
79
79
|
usd: number;
|
|
80
80
|
}
|
|
81
|
-
|
|
82
|
-
export
|
|
83
|
-
ts: string;
|
|
84
|
-
kind: "note";
|
|
85
|
-
note: string;
|
|
86
|
-
}
|
|
87
|
-
export type TaskLogRecord = TaskStepRecord | TaskDispatchRecord | TaskSettleRecord | TaskExpiredRecord | TaskCloseRecord | TaskNoteRecord;
|
|
88
|
-
export declare function taskLogPath(channelDir: string, id: string): string;
|
|
81
|
+
export type TaskLogRecord = TaskStepRecord | TaskDispatchRecord | TaskSettleRecord | TaskExpiredRecord | TaskCloseRecord;
|
|
82
|
+
export declare function taskLogPath(deskDir: string, id: string): string;
|
|
89
83
|
/** Where `archiveTask` moves a closed task's `.jsonl`. */
|
|
90
|
-
export declare function taskArchiveLogPath(
|
|
84
|
+
export declare function taskArchiveLogPath(deskDir: string, id: string): string;
|
|
91
85
|
/** True when the loop log for `id` lives only in the archive (the task is closed). */
|
|
92
|
-
export declare function taskLogIsArchived(
|
|
86
|
+
export declare function taskLogIsArchived(deskDir: string, id: string): boolean;
|
|
93
87
|
/** Test seam: drop cached appenders so a temp-dir fixture does not leak into the next case. */
|
|
94
88
|
export declare function resetTaskLogAppenders(): Promise<void>;
|
|
95
|
-
export type TaskLogInput = Omit<TaskStepRecord, "ts"> | Omit<TaskDispatchRecord, "ts"> | Omit<TaskSettleRecord, "ts"> | Omit<TaskExpiredRecord, "ts"> | Omit<TaskCloseRecord, "ts"
|
|
89
|
+
export type TaskLogInput = Omit<TaskStepRecord, "ts"> | Omit<TaskDispatchRecord, "ts"> | Omit<TaskSettleRecord, "ts"> | Omit<TaskExpiredRecord, "ts"> | Omit<TaskCloseRecord, "ts">;
|
|
96
90
|
/**
|
|
97
91
|
* Append one record. `appendStrict` rather than `append`: the loop log is the only durable trace
|
|
98
92
|
* of what a step did, so a dropped record would make the next brief lie about the task's history.
|
|
99
93
|
*/
|
|
100
|
-
export declare function appendTaskLog(
|
|
94
|
+
export declare function appendTaskLog(deskDir: string, id: string, record: TaskLogInput & {
|
|
101
95
|
ts?: string;
|
|
102
96
|
}): Promise<void>;
|
|
103
97
|
export interface ReadTaskLogOptions {
|
|
@@ -114,7 +108,7 @@ export interface ReadTaskLogOptions {
|
|
|
114
108
|
* current file made rotated-out history invisible to `task_log`, briefs, and and the board
|
|
115
109
|
* alike even though it was still on disk.
|
|
116
110
|
*/
|
|
117
|
-
export declare function readTaskLog(
|
|
111
|
+
export declare function readTaskLog(deskDir: string, id: string, options?: ReadTaskLogOptions): Promise<TaskLogRecord[]>;
|
|
118
112
|
/** One line per record, for the step brief and `task_log` (model-facing; `/tasks` uses `renderTaskLogLineForUser`). */
|
|
119
113
|
export declare function renderTaskLogLine(record: TaskLogRecord): string;
|
|
120
114
|
/** A run id is `run_…`; anything else bound to a task is a background job. */
|
package/dist/tasks/loop-log.js
CHANGED
|
@@ -6,19 +6,20 @@ import { formatLocalTime } from "../shared/local-time.js";
|
|
|
6
6
|
import { clipText, truncate } from "../shared/text-utils.js";
|
|
7
7
|
import { isPlainObject } from "../shared/type-guards.js";
|
|
8
8
|
import { normalizeTaskId } from "./contract.js";
|
|
9
|
+
import { withTaskMutation } from "./mutation-lock.js";
|
|
9
10
|
/** Same rotation shape as the channel archive: the log is working history, not an audit trail. */
|
|
10
11
|
const MAX_LOG_BYTES = 4 * 1024 * 1024;
|
|
11
12
|
const NOTE_MAX_CHARS = 4_000;
|
|
12
|
-
export function taskLogPath(
|
|
13
|
-
return join(
|
|
13
|
+
export function taskLogPath(deskDir, id) {
|
|
14
|
+
return join(deskDir, "tasks", `${normalizeTaskId(id)}.jsonl`);
|
|
14
15
|
}
|
|
15
16
|
/** Where `archiveTask` moves a closed task's `.jsonl`. */
|
|
16
|
-
export function taskArchiveLogPath(
|
|
17
|
-
return join(
|
|
17
|
+
export function taskArchiveLogPath(deskDir, id) {
|
|
18
|
+
return join(deskDir, "tasks", "archive", `${normalizeTaskId(id)}.jsonl`);
|
|
18
19
|
}
|
|
19
20
|
/** True when the loop log for `id` lives only in the archive (the task is closed). */
|
|
20
|
-
export function taskLogIsArchived(
|
|
21
|
-
return !existsSync(taskLogPath(
|
|
21
|
+
export function taskLogIsArchived(deskDir, id) {
|
|
22
|
+
return !existsSync(taskLogPath(deskDir, id)) && existsSync(taskArchiveLogPath(deskDir, id));
|
|
22
23
|
}
|
|
23
24
|
const appenders = new Map();
|
|
24
25
|
function appenderFor(path) {
|
|
@@ -39,16 +40,18 @@ export async function resetTaskLogAppenders() {
|
|
|
39
40
|
* Append one record. `appendStrict` rather than `append`: the loop log is the only durable trace
|
|
40
41
|
* of what a step did, so a dropped record would make the next brief lie about the task's history.
|
|
41
42
|
*/
|
|
42
|
-
export async function appendTaskLog(
|
|
43
|
+
export async function appendTaskLog(deskDir, id, record) {
|
|
43
44
|
const withTs = { ts: record.ts ?? formatLocalTime(), ...record };
|
|
44
45
|
if (withTs.kind === "step") {
|
|
45
46
|
withTs.note = clipText(withTs.note, NOTE_MAX_CHARS);
|
|
46
47
|
if (withTs.report)
|
|
47
48
|
withTs.report = clipText(withTs.report, NOTE_MAX_CHARS);
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
+
// Under the task's mutation lock: the appender rotates by size, and two processes rotating
|
|
51
|
+
// one log at once would each move the other's fresh records aside.
|
|
52
|
+
await withTaskMutation(deskDir, id, () => appenderFor(taskLogPath(deskDir, id)).appendStrict(withTs));
|
|
50
53
|
}
|
|
51
|
-
const RECORD_KINDS = ["step", "dispatch", "settle", "expired", "close"
|
|
54
|
+
const RECORD_KINDS = ["step", "dispatch", "settle", "expired", "close"];
|
|
52
55
|
function toRecord(value) {
|
|
53
56
|
if (!isPlainObject(value) || typeof value.ts !== "string")
|
|
54
57
|
return undefined;
|
|
@@ -90,12 +93,10 @@ function parseLogLines(content, options, into) {
|
|
|
90
93
|
* current file made rotated-out history invisible to `task_log`, briefs, and and the board
|
|
91
94
|
* alike even though it was still on disk.
|
|
92
95
|
*/
|
|
93
|
-
export async function readTaskLog(
|
|
96
|
+
export async function readTaskLog(deskDir, id, options = {}) {
|
|
94
97
|
// Fall back to the archived copy so `task_log` on a completed task returns its history rather
|
|
95
98
|
// than "暂无日志" — a task that finished still has a loop log worth reading.
|
|
96
|
-
const basePath = existsSync(taskLogPath(
|
|
97
|
-
? taskLogPath(channelDir, id)
|
|
98
|
-
: taskArchiveLogPath(channelDir, id);
|
|
99
|
+
const basePath = existsSync(taskLogPath(deskDir, id)) ? taskLogPath(deskDir, id) : taskArchiveLogPath(deskDir, id);
|
|
99
100
|
const paths = shardPaths(basePath);
|
|
100
101
|
if (paths.length === 0)
|
|
101
102
|
return [];
|
|
@@ -125,8 +126,6 @@ export function renderTaskLogLine(record) {
|
|
|
125
126
|
return `- [${record.ts}] 等待票过期(${record.ticket})→ ${record.action}`;
|
|
126
127
|
case "close":
|
|
127
128
|
return `- [${record.ts}] ${record.outcome === "done" ? "完成" : "取消"}:${record.note}`;
|
|
128
|
-
case "note":
|
|
129
|
-
return `- [${record.ts}] ${record.note}`;
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
/** `2026-10-11T14:02:33.000+08:00` → `10-11 14:02`: read straight off the stored wall-clock text, never re-zoned. */
|
|
@@ -181,7 +180,5 @@ export function renderTaskLogLineForUser(record) {
|
|
|
181
180
|
return `- ${at} 等待超时(${oneLine(record.ticket, 80)}),${record.action === "reopened" ? "已重新开始" : "已暂停"}`;
|
|
182
181
|
case "close":
|
|
183
182
|
return `- ${at} ${record.outcome === "done" ? "任务完成" : "任务取消"}:${oneLine(record.note, 160)}`;
|
|
184
|
-
case "note":
|
|
185
|
-
return `- ${at} ${oneLine(record.note, 160)}`;
|
|
186
183
|
}
|
|
187
184
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Serialize mutations of one task inside this process
|
|
2
|
+
* Serialize mutations of one task: inside this process with a serial queue, and across processes
|
|
3
|
+
* with the task's file lock (spec 058, D6) — the daemon and a TUI can open the same desk, and both
|
|
4
|
+
* may write its tasks. Every writer of a task's contract, loop log or steer/notice files goes
|
|
5
|
+
* through here.
|
|
3
6
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* store helper for the same task.
|
|
7
|
+
* Re-entrant: a command/tool transaction may call a lower-level store helper for the same task,
|
|
8
|
+
* and the inner call then runs inside the outer one's hold on both layers.
|
|
7
9
|
*/
|
|
8
|
-
export declare function withTaskMutation<T>(
|
|
10
|
+
export declare function withTaskMutation<T>(deskDir: string, id: string, mutate: () => Promise<T>): Promise<T>;
|
|
@@ -1,23 +1,46 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { FileLockTimeoutError, withFileLock } from "../shared/file-lock.js";
|
|
4
|
+
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
3
5
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
4
6
|
import { normalizeTaskId } from "./contract.js";
|
|
5
7
|
const mutationQueue = createSerialQueue();
|
|
6
8
|
const heldKeys = new AsyncLocalStorage();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A task mutation is a few small file writes; waiting longer than this means the other process
|
|
11
|
+
* is stuck, and the caller is better off retrying than hanging a tool call or a driver tick.
|
|
12
|
+
*/
|
|
13
|
+
const TASK_LOCK_TIMEOUT_MS = 30_000;
|
|
14
|
+
function taskMutationKey(deskDir, id) {
|
|
15
|
+
return join(deskDir, "tasks", `${normalizeTaskId(id)}.md`);
|
|
16
|
+
}
|
|
17
|
+
/** `<deskDir>/.locks/task-<id>.lock` — the layout `desk/paths.ts` defines (`LOCKS_DIRNAME`, which must stay
|
|
18
|
+
* in step with the literal below): tasks stays a leaf and does not import it. */
|
|
19
|
+
function taskLockPath(deskDir, id) {
|
|
20
|
+
return join(deskDir, ".locks", `task-${normalizeTaskId(id)}.lock`);
|
|
9
21
|
}
|
|
10
22
|
/**
|
|
11
|
-
* Serialize mutations of one task inside this process
|
|
23
|
+
* Serialize mutations of one task: inside this process with a serial queue, and across processes
|
|
24
|
+
* with the task's file lock (spec 058, D6) — the daemon and a TUI can open the same desk, and both
|
|
25
|
+
* may write its tasks. Every writer of a task's contract, loop log or steer/notice files goes
|
|
26
|
+
* through here.
|
|
12
27
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* store helper for the same task.
|
|
28
|
+
* Re-entrant: a command/tool transaction may call a lower-level store helper for the same task,
|
|
29
|
+
* and the inner call then runs inside the outer one's hold on both layers.
|
|
16
30
|
*/
|
|
17
|
-
export function withTaskMutation(
|
|
18
|
-
const key = taskMutationKey(
|
|
31
|
+
export function withTaskMutation(deskDir, id, mutate) {
|
|
32
|
+
const key = taskMutationKey(deskDir, id);
|
|
19
33
|
const current = heldKeys.getStore();
|
|
20
34
|
if (current?.has(key))
|
|
21
35
|
return mutate();
|
|
22
|
-
return mutationQueue.run(key, () => heldKeys.run(new Set([...(current ?? []), key]),
|
|
36
|
+
return mutationQueue.run(key, () => heldKeys.run(new Set([...(current ?? []), key]), async () => {
|
|
37
|
+
try {
|
|
38
|
+
return await withFileLock(taskLockPath(deskDir, id), mutate, { timeoutMs: TASK_LOCK_TIMEOUT_MS });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (!(error instanceof FileLockTimeoutError))
|
|
42
|
+
throw error;
|
|
43
|
+
throw new RecoverableToolError(`Task "${normalizeTaskId(id)}" is being modified by another Pipiclaw process${error.heldBy ? ` (pid ${error.heldBy.pid})` : ""}; retry this call in a moment.`);
|
|
44
|
+
}
|
|
45
|
+
}));
|
|
23
46
|
}
|