@namzu/sdk 69.0.0 → 69.2.0
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/CHANGELOG.md +68 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +2 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/authorization/skill-grant.d.ts.map +1 -1
- package/dist/authorization/skill-grant.js +1 -0
- package/dist/authorization/skill-grant.js.map +1 -1
- package/dist/bridge/acp/permission.d.ts +10 -0
- package/dist/bridge/acp/permission.d.ts.map +1 -1
- package/dist/bridge/acp/permission.js.map +1 -1
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +1 -0
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/connector/index.d.ts +2 -2
- package/dist/connector/index.d.ts.map +1 -1
- package/dist/connector/index.js +1 -1
- package/dist/connector/index.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +29 -0
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +59 -2
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/mcp/errors.d.ts +7 -0
- package/dist/connector/mcp/errors.d.ts.map +1 -1
- package/dist/connector/mcp/errors.js +14 -0
- package/dist/connector/mcp/errors.js.map +1 -1
- package/dist/connector/mcp/index.d.ts +5 -1
- package/dist/connector/mcp/index.d.ts.map +1 -1
- package/dist/connector/mcp/index.js +3 -1
- package/dist/connector/mcp/index.js.map +1 -1
- package/dist/connector/mcp/skill-source.d.ts +144 -0
- package/dist/connector/mcp/skill-source.d.ts.map +1 -0
- package/dist/connector/mcp/skill-source.js +375 -0
- package/dist/connector/mcp/skill-source.js.map +1 -0
- package/dist/connector/mcp/skills.d.ts +137 -0
- package/dist/connector/mcp/skills.d.ts.map +1 -0
- package/dist/connector/mcp/skills.js +303 -0
- package/dist/connector/mcp/skills.js.map +1 -0
- package/dist/network/audit.d.ts +1 -1
- package/dist/network/audit.d.ts.map +1 -1
- package/dist/network/audit.js +2 -0
- package/dist/network/audit.js.map +1 -1
- package/dist/network/computer.d.ts +107 -0
- package/dist/network/computer.d.ts.map +1 -0
- package/dist/network/computer.js +250 -0
- package/dist/network/computer.js.map +1 -0
- package/dist/network/index.d.ts +2 -0
- package/dist/network/index.d.ts.map +1 -1
- package/dist/network/index.js +1 -0
- package/dist/network/index.js.map +1 -1
- package/dist/network/join.d.ts +3 -1
- package/dist/network/join.d.ts.map +1 -1
- package/dist/network/join.js +6 -3
- package/dist/network/join.js.map +1 -1
- package/dist/network/roster.d.ts +5 -0
- package/dist/network/roster.d.ts.map +1 -1
- package/dist/network/roster.js.map +1 -1
- package/dist/network/service.d.ts +12 -2
- package/dist/network/service.d.ts.map +1 -1
- package/dist/network/service.js +377 -52
- package/dist/network/service.js.map +1 -1
- package/dist/network/store.d.ts +10 -0
- package/dist/network/store.d.ts.map +1 -1
- package/dist/network/store.js +25 -3
- package/dist/network/store.js.map +1 -1
- package/dist/network/wire.d.ts +2 -0
- package/dist/network/wire.d.ts.map +1 -1
- package/dist/network/wire.js +5 -0
- package/dist/network/wire.js.map +1 -1
- package/dist/persona/assembler.d.ts.map +1 -1
- package/dist/persona/assembler.js +7 -1
- package/dist/persona/assembler.js.map +1 -1
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
- package/dist/plugin/shell-hook-tool-names.js +1 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +8 -3
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +132 -7
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +4 -0
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/idle-timeout.d.ts.map +1 -1
- package/dist/provider/idle-timeout.js +3 -0
- package/dist/provider/idle-timeout.js.map +1 -1
- package/dist/provider/model-window-suffix.d.ts.map +1 -1
- package/dist/provider/model-window-suffix.js +1 -0
- package/dist/provider/model-window-suffix.js.map +1 -1
- package/dist/provider/retry.d.ts.map +1 -1
- package/dist/provider/retry.js +3 -0
- package/dist/provider/retry.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -6
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +11 -4
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +7 -4
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +9 -3
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +3 -1
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.js +10 -1
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +12 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +43 -9
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +4 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +32 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +2 -2
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +2 -2
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/shell-line-escalation.d.ts +24 -0
- package/dist/runtime/query/shell-line-escalation.d.ts.map +1 -0
- package/dist/runtime/query/shell-line-escalation.js +38 -0
- package/dist/runtime/query/shell-line-escalation.js.map +1 -0
- package/dist/runtime/query/tooling.d.ts +7 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +2 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +11 -0
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/scheduler/local.d.ts.map +1 -1
- package/dist/scheduler/local.js +3 -1
- package/dist/scheduler/local.js.map +1 -1
- package/dist/skills/index.d.ts +4 -0
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +2 -0
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/inline-shell-host.d.ts +103 -0
- package/dist/skills/inline-shell-host.d.ts.map +1 -0
- package/dist/skills/inline-shell-host.js +238 -0
- package/dist/skills/inline-shell-host.js.map +1 -0
- package/dist/skills/inline-shell.d.ts +176 -0
- package/dist/skills/inline-shell.d.ts.map +1 -0
- package/dist/skills/inline-shell.js +646 -0
- package/dist/skills/inline-shell.js.map +1 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +1 -0
- package/dist/store/index.js.map +1 -1
- package/dist/store/task/file.d.ts +124 -0
- package/dist/store/task/file.d.ts.map +1 -0
- package/dist/store/task/file.js +498 -0
- package/dist/store/task/file.js.map +1 -0
- package/dist/teams/backend.d.ts +28 -0
- package/dist/teams/backend.d.ts.map +1 -0
- package/dist/teams/backend.js +2 -0
- package/dist/teams/backend.js.map +1 -0
- package/dist/teams/fs-sync.d.ts +19 -0
- package/dist/teams/fs-sync.d.ts.map +1 -0
- package/dist/teams/fs-sync.js +81 -0
- package/dist/teams/fs-sync.js.map +1 -0
- package/dist/teams/index.d.ts +14 -0
- package/dist/teams/index.d.ts.map +1 -0
- package/dist/teams/index.js +11 -0
- package/dist/teams/index.js.map +1 -0
- package/dist/teams/names.d.ts +15 -0
- package/dist/teams/names.d.ts.map +1 -0
- package/dist/teams/names.js +43 -0
- package/dist/teams/names.js.map +1 -0
- package/dist/teams/store.d.ts +133 -0
- package/dist/teams/store.d.ts.map +1 -0
- package/dist/teams/store.js +536 -0
- package/dist/teams/store.js.map +1 -0
- package/dist/teams/types.d.ts +97 -0
- package/dist/teams/types.d.ts.map +1 -0
- package/dist/teams/types.js +70 -0
- package/dist/teams/types.js.map +1 -0
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +9 -1
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/atomic-write-file.d.ts +19 -3
- package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
- package/dist/tools/builtins/atomic-write-file.js +56 -3
- package/dist/tools/builtins/atomic-write-file.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +56 -2
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +604 -90
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +1 -1
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts +31 -0
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +232 -88
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/task/create.d.ts.map +1 -1
- package/dist/tools/task/create.js +11 -2
- package/dist/tools/task/create.js.map +1 -1
- package/dist/tools/task/get.d.ts +8 -0
- package/dist/tools/task/get.d.ts.map +1 -0
- package/dist/tools/task/get.js +51 -0
- package/dist/tools/task/get.js.map +1 -0
- package/dist/tools/task/index.d.ts +7 -0
- package/dist/tools/task/index.d.ts.map +1 -1
- package/dist/tools/task/index.js +10 -2
- package/dist/tools/task/index.js.map +1 -1
- package/dist/tools/task/list.d.ts.map +1 -1
- package/dist/tools/task/list.js +10 -2
- package/dist/tools/task/list.js.map +1 -1
- package/dist/tools/task/number.d.ts +13 -0
- package/dist/tools/task/number.d.ts.map +1 -0
- package/dist/tools/task/number.js +30 -0
- package/dist/tools/task/number.js.map +1 -0
- package/dist/tools/task/present.d.ts.map +1 -1
- package/dist/tools/task/present.js +17 -3
- package/dist/tools/task/present.js.map +1 -1
- package/dist/tools/task/update.d.ts +2 -1
- package/dist/tools/task/update.d.ts.map +1 -1
- package/dist/tools/task/update.js +86 -19
- package/dist/tools/task/update.js.map +1 -1
- package/dist/types/acp/index.d.ts +10 -0
- package/dist/types/acp/index.d.ts.map +1 -1
- package/dist/types/agent/base.d.ts +4 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/agent/scheduler.d.ts +9 -0
- package/dist/types/agent/scheduler.d.ts.map +1 -1
- package/dist/types/computer-use/index.d.ts +7 -0
- package/dist/types/computer-use/index.d.ts.map +1 -1
- package/dist/types/computer-use/index.js.map +1 -1
- package/dist/types/connector/mcp.d.ts +10 -0
- package/dist/types/connector/mcp.d.ts.map +1 -1
- package/dist/types/permission/index.d.ts +20 -0
- package/dist/types/permission/index.d.ts.map +1 -1
- package/dist/types/permission/index.js +25 -0
- package/dist/types/permission/index.js.map +1 -1
- package/dist/types/plugin/index.d.ts +47 -3
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +4 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/provider/interface.d.ts +6 -0
- package/dist/types/provider/interface.d.ts.map +1 -1
- package/dist/types/skills/index.d.ts +7 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/dist/types/skills/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +28 -2
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +4 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +2 -0
- package/src/authorization/skill-grant.ts +1 -0
- package/src/bridge/acp/permission.ts +11 -1
- package/src/bridge/acp/server.ts +1 -0
- package/src/connector/index.ts +27 -0
- package/src/connector/mcp/client.ts +71 -2
- package/src/connector/mcp/errors.ts +14 -0
- package/src/connector/mcp/index.ts +36 -0
- package/src/connector/mcp/skill-source.ts +499 -0
- package/src/connector/mcp/skills.ts +367 -0
- package/src/network/audit.ts +2 -0
- package/src/network/computer.ts +347 -0
- package/src/network/index.ts +20 -0
- package/src/network/join.ts +4 -2
- package/src/network/roster.ts +5 -0
- package/src/network/service.ts +492 -75
- package/src/network/store.ts +29 -3
- package/src/network/wire.ts +5 -0
- package/src/persona/assembler.ts +7 -1
- package/src/plugin/shell-hook-tool-names.ts +1 -0
- package/src/plugin/shell-hook.ts +144 -7
- package/src/provider/fallback.ts +4 -0
- package/src/provider/idle-timeout.ts +3 -0
- package/src/provider/model-window-suffix.ts +1 -0
- package/src/provider/retry.ts +3 -0
- package/src/public-runtime.ts +99 -0
- package/src/public-tools.ts +15 -0
- package/src/public-types.ts +28 -0
- package/src/runtime/query/executor/tool-call-admission.ts +10 -1
- package/src/runtime/query/executor.ts +57 -8
- package/src/runtime/query/index.ts +43 -0
- package/src/runtime/query/review-policy.ts +7 -1
- package/src/runtime/query/shell-line-escalation.ts +49 -0
- package/src/runtime/query/tooling.ts +6 -0
- package/src/runtime/query/turn/outcome-ledger.ts +11 -0
- package/src/scheduler/local.ts +3 -1
- package/src/skills/index.ts +43 -0
- package/src/skills/inline-shell-host.ts +303 -0
- package/src/skills/inline-shell.ts +765 -0
- package/src/store/index.ts +8 -0
- package/src/store/task/file.ts +641 -0
- package/src/teams/backend.ts +29 -0
- package/src/teams/fs-sync.ts +91 -0
- package/src/teams/index.ts +47 -0
- package/src/teams/names.ts +49 -0
- package/src/teams/store.ts +607 -0
- package/src/teams/types.ts +102 -0
- package/src/tools/admit-facts.ts +9 -1
- package/src/tools/builtins/atomic-write-file.ts +73 -5
- package/src/tools/builtins/computer-use.ts +689 -95
- package/src/tools/builtins/index.ts +1 -0
- package/src/tools/builtins/skill.ts +291 -95
- package/src/tools/task/create.ts +11 -2
- package/src/tools/task/get.ts +53 -0
- package/src/tools/task/index.ts +14 -2
- package/src/tools/task/list.ts +10 -2
- package/src/tools/task/number.ts +32 -0
- package/src/tools/task/present.ts +18 -3
- package/src/tools/task/update.ts +93 -19
- package/src/types/acp/index.ts +11 -1
- package/src/types/agent/base.ts +6 -0
- package/src/types/agent/scheduler.ts +9 -0
- package/src/types/computer-use/index.ts +7 -0
- package/src/types/connector/mcp.ts +10 -0
- package/src/types/permission/index.ts +29 -0
- package/src/types/plugin/index.ts +95 -39
- package/src/types/provider/interface.ts +7 -0
- package/src/types/skills/index.ts +8 -0
- package/src/types/tool/index.ts +23 -2
- package/src/types/tool/presentation.ts +2 -0
package/src/store/index.ts
CHANGED
|
@@ -7,6 +7,14 @@ export type { ActivityEvent, ActivityEventListener } from './activity/memory.js'
|
|
|
7
7
|
export { InMemoryTaskStore } from './task/memory.js'
|
|
8
8
|
export { DiskTaskStore } from './task/disk.js'
|
|
9
9
|
export type { DiskTaskStoreConfig } from './task/disk.js'
|
|
10
|
+
export { FileTaskStore, TaskListError, isClaimableTaskStore } from './task/file.js'
|
|
11
|
+
export type {
|
|
12
|
+
ClaimableTaskStore,
|
|
13
|
+
FileTaskStoreConfig,
|
|
14
|
+
TaskClaimFailure,
|
|
15
|
+
TaskClaimResult,
|
|
16
|
+
TaskListErrorCode,
|
|
17
|
+
} from './task/file.js'
|
|
10
18
|
|
|
11
19
|
export { InMemoryMemoryIndex } from './memory/index.js'
|
|
12
20
|
export { InMemoryMemoryStore } from './memory/memory.js'
|
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { NAMZU } from '../../constants/telemetry/index.js'
|
|
4
|
+
import { resolveNamzuHome } from '../../session/home.js'
|
|
5
|
+
import {
|
|
6
|
+
LOCK_ATTEMPTS,
|
|
7
|
+
jitterDelay,
|
|
8
|
+
removeSync,
|
|
9
|
+
sleepSync,
|
|
10
|
+
writeAtomicSync,
|
|
11
|
+
} from '../../teams/fs-sync.js'
|
|
12
|
+
import type { SessionId, TaskId, TenantId } from '../../types/ids/index.js'
|
|
13
|
+
import type {
|
|
14
|
+
CreateTaskParams,
|
|
15
|
+
Task,
|
|
16
|
+
TaskEvent,
|
|
17
|
+
TaskEventListener,
|
|
18
|
+
TaskStatus,
|
|
19
|
+
TaskStore,
|
|
20
|
+
UpdateTaskParams,
|
|
21
|
+
} from '../../types/task/index.js'
|
|
22
|
+
import { isTerminalTaskStatus } from '../../types/task/index.js'
|
|
23
|
+
import { FileLockError, lockPathFor, runLocked } from '../../utils/file-lock.js'
|
|
24
|
+
import { asSessionId, asTaskId, asTurnId, generateTaskId } from '../../utils/id.js'
|
|
25
|
+
import { SCOPE_ATTRIBUTE } from '../../utils/log/types.js'
|
|
26
|
+
import { type Logger, resolveLogger } from '../../utils/logger.js'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A task list that several processes share, kept at `<NAMZU_HOME>/tasks/<list id>/`: one JSON file
|
|
30
|
+
* per task, a `.highwatermark` file that issues each task its place in the list (a place is never
|
|
31
|
+
* handed out twice, not even after the task is deleted), and lock files beside what they guard.
|
|
32
|
+
*
|
|
33
|
+
* Unlike {@link DiskTaskStore}, whose locks live in one process, every change here runs under a lock
|
|
34
|
+
* file (the shared `utils/file-lock` protocol: create-exclusive, owner pid recorded, a lock whose owner
|
|
35
|
+
* has exited is taken over). Two terminals, Desktop and a later process backend can therefore claim from
|
|
36
|
+
* one list and each task is claimed exactly once. Lock order is fixed: the list lock first, then task
|
|
37
|
+
* locks in sorted id order, so two changes cannot wait on each other.
|
|
38
|
+
*
|
|
39
|
+
* The list id is the team name when the list belongs to a team, else the session id. Task ids stay the
|
|
40
|
+
* usual entity ids; the order of a list is the `seq` the high-water mark issued, which is what "the
|
|
41
|
+
* lowest task" means.
|
|
42
|
+
*
|
|
43
|
+
* Events reach listeners in the process that made the change, not in other processes.
|
|
44
|
+
*
|
|
45
|
+
* @experimental
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
export type TaskClaimFailure = 'not_found' | 'already_claimed' | 'already_resolved' | 'blocked'
|
|
49
|
+
|
|
50
|
+
export type TaskClaimResult =
|
|
51
|
+
| { readonly ok: true; readonly task: Task }
|
|
52
|
+
| {
|
|
53
|
+
readonly ok: false
|
|
54
|
+
readonly code: TaskClaimFailure
|
|
55
|
+
readonly task?: Task
|
|
56
|
+
/** For `blocked`: the blockers that are not completed. */
|
|
57
|
+
readonly blockedBy?: readonly TaskId[]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type TaskListErrorCode =
|
|
61
|
+
| 'cycle'
|
|
62
|
+
| 'not_found'
|
|
63
|
+
| 'not_all_completed'
|
|
64
|
+
| 'bad_list_id'
|
|
65
|
+
| 'owner_conflict'
|
|
66
|
+
| 'blocked'
|
|
67
|
+
|
|
68
|
+
export class TaskListError extends Error {
|
|
69
|
+
override readonly name = 'TaskListError'
|
|
70
|
+
constructor(
|
|
71
|
+
readonly code: TaskListErrorCode,
|
|
72
|
+
message: string,
|
|
73
|
+
) {
|
|
74
|
+
super(message)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A store whose tasks several sessions share: it can say why a claim was refused. */
|
|
79
|
+
export interface ClaimableTaskStore extends TaskStore {
|
|
80
|
+
readonly shared: true
|
|
81
|
+
claimTask(id: TaskId, owner: string): Promise<TaskClaimResult>
|
|
82
|
+
/** Tasks that are not completed and block this one. */
|
|
83
|
+
unresolvedBlockers(task: Task): Promise<TaskId[]>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function isClaimableTaskStore(store: TaskStore): store is ClaimableTaskStore {
|
|
87
|
+
return (store as Partial<ClaimableTaskStore>).shared === true
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface FileTaskStoreConfig {
|
|
91
|
+
/** The team name or the session id the list belongs to. */
|
|
92
|
+
listId: string
|
|
93
|
+
/** Default `<NAMZU_HOME>/tasks`. */
|
|
94
|
+
rootDir?: string
|
|
95
|
+
tenantId?: TenantId
|
|
96
|
+
/**
|
|
97
|
+
* Injected so a test never depends on the clock. Used only for the short waits inside a file write
|
|
98
|
+
* on a platform that refuses a rename for a moment; default blocks the thread for that moment.
|
|
99
|
+
*/
|
|
100
|
+
sleep?: (ms: number) => void
|
|
101
|
+
/** The wait between tries on a lock another process holds; default yields to the event loop. */
|
|
102
|
+
lockWait?: (attempt: number) => Promise<void>
|
|
103
|
+
logger?: Logger
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface Stored extends Task {
|
|
107
|
+
/** The place the high-water mark issued; the order of the list. */
|
|
108
|
+
seq: number
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const STATUS_ORDER: Record<TaskStatus, number> = {
|
|
112
|
+
pending: 0,
|
|
113
|
+
in_progress: 1,
|
|
114
|
+
completed: 2,
|
|
115
|
+
failed: 2,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const LIST_ID = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$/
|
|
119
|
+
|
|
120
|
+
const HIGH_WATER = '.highwatermark'
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Changes made by this process to one list run one after another. The file lock still keeps other
|
|
124
|
+
* processes out, but waiting for it here would block the event loop of a terminal, so callers of one
|
|
125
|
+
* process queue on a promise and the lock is taken (with a wait that yields) only when it is theirs.
|
|
126
|
+
*/
|
|
127
|
+
const gates = new Map<string, Promise<unknown>>()
|
|
128
|
+
|
|
129
|
+
function serialised<T>(key: string, run: () => Promise<T>): Promise<T> {
|
|
130
|
+
const before = gates.get(key) ?? Promise.resolve()
|
|
131
|
+
const turn = before.then(run, run)
|
|
132
|
+
const tail = turn.catch(() => undefined)
|
|
133
|
+
gates.set(key, tail)
|
|
134
|
+
void tail.then(() => {
|
|
135
|
+
if (gates.get(key) === tail) gates.delete(key)
|
|
136
|
+
})
|
|
137
|
+
return turn
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function strip(stored: Stored): Task {
|
|
141
|
+
const { seq: _seq, ...task } = stored
|
|
142
|
+
return task as Task
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export class FileTaskStore implements ClaimableTaskStore {
|
|
146
|
+
readonly shared = true as const
|
|
147
|
+
readonly listId: string
|
|
148
|
+
private readonly dir: string
|
|
149
|
+
private readonly tenantId?: TenantId
|
|
150
|
+
private readonly sleep: (ms: number) => void
|
|
151
|
+
private readonly lockWait: (attempt: number) => Promise<void>
|
|
152
|
+
private readonly log: Logger
|
|
153
|
+
private listeners: TaskEventListener[] = []
|
|
154
|
+
|
|
155
|
+
constructor(config: FileTaskStoreConfig) {
|
|
156
|
+
if (!LIST_ID.test(config.listId) || config.listId.includes('..')) {
|
|
157
|
+
throw new TaskListError(
|
|
158
|
+
'bad_list_id',
|
|
159
|
+
`A task list id is letters, digits, "_", "-" and "." (up to 128, not starting with a symbol); got ${JSON.stringify(config.listId)}.`,
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
this.listId = config.listId
|
|
163
|
+
this.dir = join(config.rootDir ?? join(resolveNamzuHome(), 'tasks'), config.listId)
|
|
164
|
+
this.tenantId = config.tenantId
|
|
165
|
+
this.sleep = config.sleep ?? sleepSync
|
|
166
|
+
this.lockWait =
|
|
167
|
+
config.lockWait ?? (() => new Promise((done) => setTimeout(done, jitterDelay())))
|
|
168
|
+
this.log = resolveLogger(config.logger).child({ [SCOPE_ATTRIBUTE]: 'store/task/file' })
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The directory this list lives in. */
|
|
172
|
+
get directory(): string {
|
|
173
|
+
return this.dir
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
on(listener: TaskEventListener): () => void {
|
|
177
|
+
this.listeners.push(listener)
|
|
178
|
+
return () => {
|
|
179
|
+
this.listeners = this.listeners.filter((l) => l !== listener)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private emit(event: TaskEvent): void {
|
|
184
|
+
for (const listener of this.listeners) {
|
|
185
|
+
try {
|
|
186
|
+
listener(event)
|
|
187
|
+
} catch (err) {
|
|
188
|
+
this.log.warn('Task event listener threw', {
|
|
189
|
+
'exception.message': err instanceof Error ? err.message : String(err),
|
|
190
|
+
'namzu.event.type': event.type,
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ── files and locks ─────────────────────────────────────────────
|
|
197
|
+
|
|
198
|
+
private ensureDir(): void {
|
|
199
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 })
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private taskFile(id: TaskId): string {
|
|
203
|
+
// The id is checked as an entity id before it names a file.
|
|
204
|
+
return join(this.dir, `${asTaskId(id)}.json`)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private listLockTarget(): string {
|
|
208
|
+
return join(this.dir, '.list')
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private lockOf<T>(file: string, operation: () => T | Promise<T>): Promise<T> {
|
|
212
|
+
return runLocked(
|
|
213
|
+
lockPathFor(file),
|
|
214
|
+
operation,
|
|
215
|
+
{ attempts: LOCK_ATTEMPTS, wait: this.lockWait },
|
|
216
|
+
(lockPath) =>
|
|
217
|
+
new FileLockError(
|
|
218
|
+
lockPath,
|
|
219
|
+
`${file} is in use by another process and did not free up; try again.`,
|
|
220
|
+
),
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Run `operation` alone in this process and holding the list lock. */
|
|
225
|
+
private inList<T>(operation: () => T | Promise<T>): Promise<T> {
|
|
226
|
+
this.ensureDir()
|
|
227
|
+
return serialised(this.dir, () => this.lockOf(this.listLockTarget(), operation))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Run `operation` holding the lock of each task, taken in sorted order. `nested` is true inside
|
|
232
|
+
* {@link inList}, which already holds this process's place in the queue.
|
|
233
|
+
*/
|
|
234
|
+
private inTasks<T>(
|
|
235
|
+
ids: readonly TaskId[],
|
|
236
|
+
operation: () => T | Promise<T>,
|
|
237
|
+
nested = false,
|
|
238
|
+
): Promise<T> {
|
|
239
|
+
this.ensureDir()
|
|
240
|
+
const sorted = [...new Set(ids)].sort()
|
|
241
|
+
const step = async (i: number): Promise<T> => {
|
|
242
|
+
const id = sorted[i]
|
|
243
|
+
if (id === undefined) return operation()
|
|
244
|
+
return this.lockOf(this.taskFile(id), () => step(i + 1))
|
|
245
|
+
}
|
|
246
|
+
return nested ? step(0) : serialised(this.dir, () => step(0))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private readStored(id: TaskId): Stored | undefined {
|
|
250
|
+
let raw: string
|
|
251
|
+
try {
|
|
252
|
+
raw = readFileSync(this.taskFile(id), 'utf8')
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined
|
|
255
|
+
throw error
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const parsed = JSON.parse(raw) as Stored
|
|
259
|
+
if (typeof parsed?.id !== 'string' || typeof parsed.seq !== 'number') return undefined
|
|
260
|
+
return parsed
|
|
261
|
+
} catch {
|
|
262
|
+
this.log.error('Corrupt task JSON on disk', { 'namzu.task.id': id })
|
|
263
|
+
return undefined
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private writeStored(stored: Stored): void {
|
|
268
|
+
writeAtomicSync(this.taskFile(stored.id), `${JSON.stringify(stored)}\n`, this.sleep)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private allStored(): Stored[] {
|
|
272
|
+
let names: string[]
|
|
273
|
+
try {
|
|
274
|
+
names = readdirSync(this.dir)
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []
|
|
277
|
+
throw error
|
|
278
|
+
}
|
|
279
|
+
const out: Stored[] = []
|
|
280
|
+
for (const name of names) {
|
|
281
|
+
if (!name.endsWith('.json')) continue
|
|
282
|
+
const stored = this.readStored(name.slice(0, -'.json'.length) as TaskId)
|
|
283
|
+
if (stored) out.push(stored)
|
|
284
|
+
}
|
|
285
|
+
return out.sort((a, b) => a.seq - b.seq || a.id.localeCompare(b.id))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Issue the next place in the list. Caller holds the list lock. */
|
|
289
|
+
private issueSeq(): number {
|
|
290
|
+
const file = join(this.dir, HIGH_WATER)
|
|
291
|
+
let last = 0
|
|
292
|
+
try {
|
|
293
|
+
last = Number.parseInt(readFileSync(file, 'utf8').trim(), 10)
|
|
294
|
+
if (!Number.isInteger(last) || last < 0) last = 0
|
|
295
|
+
} catch {
|
|
296
|
+
last = 0
|
|
297
|
+
}
|
|
298
|
+
// A lost or damaged mark never makes a place repeat: it is raised to the largest one in use.
|
|
299
|
+
for (const stored of this.allStored()) last = Math.max(last, stored.seq)
|
|
300
|
+
const next = last + 1
|
|
301
|
+
writeAtomicSync(file, `${next}\n`, this.sleep)
|
|
302
|
+
return next
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ── reads ───────────────────────────────────────────────────────
|
|
306
|
+
|
|
307
|
+
async get(id: TaskId): Promise<Task | undefined> {
|
|
308
|
+
const stored = this.readStored(id)
|
|
309
|
+
return stored ? strip(stored) : undefined
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Every task of the list, in list order. The list belongs to a team or session by its id, so a
|
|
314
|
+
* `sessionId` filter is not applied: tasks of every session that works from this list are shown.
|
|
315
|
+
*/
|
|
316
|
+
async list(filter?: { status?: TaskStatus; owner?: string; sessionId?: SessionId }): Promise<
|
|
317
|
+
Task[]
|
|
318
|
+
> {
|
|
319
|
+
let tasks = this.allStored().map(strip)
|
|
320
|
+
if (filter?.status) tasks = tasks.filter((t) => t.status === filter.status)
|
|
321
|
+
if (filter?.owner) tasks = tasks.filter((t) => t.owner === filter.owner)
|
|
322
|
+
return tasks
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async unresolvedBlockers(task: Task): Promise<TaskId[]> {
|
|
326
|
+
const out: TaskId[] = []
|
|
327
|
+
for (const id of task.blockedBy) {
|
|
328
|
+
const blocker = this.readStored(id)
|
|
329
|
+
// A blocker that no longer exists was deleted, which removed the edge; a dangling one is not a wait.
|
|
330
|
+
if (blocker && blocker.status !== 'completed') out.push(id)
|
|
331
|
+
}
|
|
332
|
+
return out
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The lowest task nobody owns that is pending and unblocked: what an idle member takes next.
|
|
337
|
+
*/
|
|
338
|
+
async nextClaimable(): Promise<Task | undefined> {
|
|
339
|
+
for (const stored of this.allStored()) {
|
|
340
|
+
if (stored.status !== 'pending' || stored.owner !== undefined) continue
|
|
341
|
+
if ((await this.unresolvedBlockers(stored)).length > 0) continue
|
|
342
|
+
return strip(stored)
|
|
343
|
+
}
|
|
344
|
+
return undefined
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ── changes ─────────────────────────────────────────────────────
|
|
348
|
+
|
|
349
|
+
async create(params: CreateTaskParams): Promise<Task> {
|
|
350
|
+
const blockers = [...new Set(params.blockedBy ?? [])]
|
|
351
|
+
const created = await this.inList(() =>
|
|
352
|
+
this.inTasks(
|
|
353
|
+
blockers,
|
|
354
|
+
() => {
|
|
355
|
+
const touched: Stored[] = []
|
|
356
|
+
for (const blockerId of blockers) {
|
|
357
|
+
const blocker = this.readStored(blockerId)
|
|
358
|
+
if (!blocker)
|
|
359
|
+
throw new TaskListError('not_found', `Task ${blockerId} is not in this list.`)
|
|
360
|
+
}
|
|
361
|
+
const id = generateTaskId()
|
|
362
|
+
const stored: Stored = {
|
|
363
|
+
id,
|
|
364
|
+
seq: this.issueSeq(),
|
|
365
|
+
sessionId: asSessionId(params.sessionId),
|
|
366
|
+
turnId: asTurnId(params.turnId),
|
|
367
|
+
tenantId: params.tenantId ?? this.tenantId,
|
|
368
|
+
subject: params.subject,
|
|
369
|
+
description: params.description,
|
|
370
|
+
activeForm: params.activeForm,
|
|
371
|
+
status: 'pending',
|
|
372
|
+
owner: params.owner,
|
|
373
|
+
blocks: [],
|
|
374
|
+
blockedBy: blockers,
|
|
375
|
+
metadata: params.metadata ? { ...params.metadata } : undefined,
|
|
376
|
+
createdAt: Date.now(),
|
|
377
|
+
}
|
|
378
|
+
for (const blockerId of blockers) {
|
|
379
|
+
const blocker = this.readStored(blockerId)
|
|
380
|
+
if (blocker && !blocker.blocks.includes(id)) {
|
|
381
|
+
blocker.blocks.push(id)
|
|
382
|
+
this.writeStored(blocker)
|
|
383
|
+
touched.push(blocker)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
this.writeStored(stored)
|
|
387
|
+
return { stored, touched }
|
|
388
|
+
},
|
|
389
|
+
true,
|
|
390
|
+
),
|
|
391
|
+
)
|
|
392
|
+
for (const blocker of created.touched) {
|
|
393
|
+
this.emit({
|
|
394
|
+
type: 'task.updated',
|
|
395
|
+
taskId: blocker.id,
|
|
396
|
+
task: strip(blocker),
|
|
397
|
+
timestamp: Date.now(),
|
|
398
|
+
})
|
|
399
|
+
}
|
|
400
|
+
const task = strip(created.stored)
|
|
401
|
+
this.log.info('Task created', {
|
|
402
|
+
'namzu.task.id': task.id,
|
|
403
|
+
'namzu.store.subject': params.subject,
|
|
404
|
+
[NAMZU.TURN_ID]: task.turnId,
|
|
405
|
+
})
|
|
406
|
+
this.emit({ type: 'task.created', taskId: task.id, task, timestamp: Date.now() })
|
|
407
|
+
return task
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async update(id: TaskId, updates: UpdateTaskParams): Promise<Task | undefined> {
|
|
411
|
+
const result = await this.inTasks([id], () => {
|
|
412
|
+
const task = this.readStored(id)
|
|
413
|
+
if (!task) return undefined
|
|
414
|
+
const previousStatus = task.status
|
|
415
|
+
// A change here never takes a task from its owner (that is what a claim is for), and a task
|
|
416
|
+
// that still waits on an unfinished blocker is not completed.
|
|
417
|
+
if (updates.owner !== undefined && task.owner !== undefined && updates.owner !== task.owner) {
|
|
418
|
+
throw new TaskListError(
|
|
419
|
+
'owner_conflict',
|
|
420
|
+
`Task ${id} is owned by ${task.owner}; it is not reassigned to ${updates.owner}.`,
|
|
421
|
+
)
|
|
422
|
+
}
|
|
423
|
+
if (updates.status === 'completed' && previousStatus !== 'completed') {
|
|
424
|
+
const waiting = task.blockedBy.filter((b) => {
|
|
425
|
+
const blocker = this.readStored(b)
|
|
426
|
+
return blocker !== undefined && blocker.status !== 'completed'
|
|
427
|
+
})
|
|
428
|
+
if (waiting.length > 0) {
|
|
429
|
+
throw new TaskListError(
|
|
430
|
+
'blocked',
|
|
431
|
+
`Task ${id} is blocked by ${waiting.join(', ')}; finish those first.`,
|
|
432
|
+
)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (updates.subject !== undefined) task.subject = updates.subject
|
|
436
|
+
if (updates.description !== undefined) task.description = updates.description
|
|
437
|
+
if (updates.activeForm !== undefined) task.activeForm = updates.activeForm
|
|
438
|
+
if (updates.owner !== undefined) task.owner = updates.owner
|
|
439
|
+
if (updates.metadata !== undefined) {
|
|
440
|
+
task.metadata = task.metadata
|
|
441
|
+
? { ...task.metadata, ...updates.metadata }
|
|
442
|
+
: { ...updates.metadata }
|
|
443
|
+
}
|
|
444
|
+
if (
|
|
445
|
+
updates.status !== undefined &&
|
|
446
|
+
updates.status !== previousStatus &&
|
|
447
|
+
STATUS_ORDER[updates.status] > STATUS_ORDER[previousStatus]
|
|
448
|
+
) {
|
|
449
|
+
task.status = updates.status
|
|
450
|
+
if (updates.status === 'in_progress' && !task.startedAt) task.startedAt = Date.now()
|
|
451
|
+
if (isTerminalTaskStatus(updates.status)) task.completedAt = Date.now()
|
|
452
|
+
}
|
|
453
|
+
this.writeStored(task)
|
|
454
|
+
return { task, previousStatus }
|
|
455
|
+
})
|
|
456
|
+
if (!result) return undefined
|
|
457
|
+
const task = strip(result.task)
|
|
458
|
+
this.emit({
|
|
459
|
+
type: 'task.updated',
|
|
460
|
+
taskId: id,
|
|
461
|
+
task,
|
|
462
|
+
previousStatus: result.previousStatus,
|
|
463
|
+
timestamp: Date.now(),
|
|
464
|
+
})
|
|
465
|
+
return task
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
async claimTask(id: TaskId, owner: string): Promise<TaskClaimResult> {
|
|
469
|
+
const outcome = await this.inTasks([id], (): TaskClaimResult => {
|
|
470
|
+
const task = this.readStored(id)
|
|
471
|
+
if (!task) return { ok: false, code: 'not_found' }
|
|
472
|
+
if (isTerminalTaskStatus(task.status)) {
|
|
473
|
+
return { ok: false, code: 'already_resolved', task: strip(task) }
|
|
474
|
+
}
|
|
475
|
+
if (task.owner !== undefined && task.owner !== owner) {
|
|
476
|
+
return { ok: false, code: 'already_claimed', task: strip(task) }
|
|
477
|
+
}
|
|
478
|
+
// The same owner claiming again is not a second claim: it changes nothing.
|
|
479
|
+
if (task.owner === owner && task.status === 'in_progress') {
|
|
480
|
+
return { ok: true, task: strip(task) }
|
|
481
|
+
}
|
|
482
|
+
const waiting: TaskId[] = []
|
|
483
|
+
for (const blockerId of task.blockedBy) {
|
|
484
|
+
const blocker = this.readStored(blockerId)
|
|
485
|
+
if (blocker && blocker.status !== 'completed') waiting.push(blockerId)
|
|
486
|
+
}
|
|
487
|
+
if (waiting.length > 0) {
|
|
488
|
+
return { ok: false, code: 'blocked', task: strip(task), blockedBy: waiting }
|
|
489
|
+
}
|
|
490
|
+
task.owner = owner
|
|
491
|
+
task.status = 'in_progress'
|
|
492
|
+
task.startedAt ??= Date.now()
|
|
493
|
+
this.writeStored(task)
|
|
494
|
+
return { ok: true, task: strip(task) }
|
|
495
|
+
})
|
|
496
|
+
if (outcome.ok) {
|
|
497
|
+
this.emit({ type: 'task.claimed', taskId: id, task: outcome.task, timestamp: Date.now() })
|
|
498
|
+
}
|
|
499
|
+
return outcome
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async claim(id: TaskId, owner: string): Promise<Task | undefined> {
|
|
503
|
+
const result = await this.claimTask(id, owner)
|
|
504
|
+
return result.ok ? result.task : undefined
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async block(blockerId: TaskId, blockedId: TaskId): Promise<void> {
|
|
508
|
+
const changed = await this.inList(() =>
|
|
509
|
+
this.inTasks(
|
|
510
|
+
[blockerId, blockedId],
|
|
511
|
+
() => {
|
|
512
|
+
const blocker = this.readStored(blockerId)
|
|
513
|
+
const blocked = this.readStored(blockedId)
|
|
514
|
+
if (!blocker || !blocked) {
|
|
515
|
+
throw new TaskListError(
|
|
516
|
+
'not_found',
|
|
517
|
+
`Task ${blocker ? blockedId : blockerId} is not in this list.`,
|
|
518
|
+
)
|
|
519
|
+
}
|
|
520
|
+
if (blockerId === blockedId || this.reaches(blocked, blockerId)) {
|
|
521
|
+
throw new TaskListError(
|
|
522
|
+
'cycle',
|
|
523
|
+
`Task ${blockedId} already waits on task ${blockerId}, directly or through other tasks; adding this would make them wait on each other.`,
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
const out: Stored[] = []
|
|
527
|
+
if (!blocker.blocks.includes(blockedId)) {
|
|
528
|
+
blocker.blocks.push(blockedId)
|
|
529
|
+
this.writeStored(blocker)
|
|
530
|
+
out.push(blocker)
|
|
531
|
+
}
|
|
532
|
+
if (!blocked.blockedBy.includes(blockerId)) {
|
|
533
|
+
blocked.blockedBy.push(blockerId)
|
|
534
|
+
this.writeStored(blocked)
|
|
535
|
+
out.push(blocked)
|
|
536
|
+
}
|
|
537
|
+
return out
|
|
538
|
+
},
|
|
539
|
+
true,
|
|
540
|
+
),
|
|
541
|
+
)
|
|
542
|
+
const now = Date.now()
|
|
543
|
+
for (const stored of changed) {
|
|
544
|
+
this.emit({ type: 'task.updated', taskId: stored.id, task: strip(stored), timestamp: now })
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** Whether `target` is reached from `from` by following `blocks` edges (the caller holds the list lock). */
|
|
549
|
+
private reaches(from: Stored, target: TaskId): boolean {
|
|
550
|
+
const seen = new Set<TaskId>([from.id])
|
|
551
|
+
const queue = [...from.blocks]
|
|
552
|
+
while (queue.length > 0) {
|
|
553
|
+
const id = queue.shift() as TaskId
|
|
554
|
+
if (id === target) return true
|
|
555
|
+
if (seen.has(id)) continue
|
|
556
|
+
seen.add(id)
|
|
557
|
+
const next = this.readStored(id)
|
|
558
|
+
if (next) queue.push(...next.blocks)
|
|
559
|
+
}
|
|
560
|
+
return false
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async delete(id: TaskId): Promise<boolean> {
|
|
564
|
+
const outcome = await this.inList(() => {
|
|
565
|
+
const preview = this.readStored(id)
|
|
566
|
+
if (!preview) return undefined
|
|
567
|
+
return this.inTasks(
|
|
568
|
+
[id, ...preview.blockedBy, ...preview.blocks],
|
|
569
|
+
() => {
|
|
570
|
+
const task = this.readStored(id)
|
|
571
|
+
if (!task) return undefined
|
|
572
|
+
const touched: Stored[] = []
|
|
573
|
+
for (const other of task.blockedBy) {
|
|
574
|
+
const stored = other === id ? undefined : this.readStored(other)
|
|
575
|
+
if (stored?.blocks.includes(id)) {
|
|
576
|
+
stored.blocks = stored.blocks.filter((x) => x !== id)
|
|
577
|
+
this.writeStored(stored)
|
|
578
|
+
touched.push(stored)
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
for (const other of task.blocks) {
|
|
582
|
+
const stored = other === id ? undefined : this.readStored(other)
|
|
583
|
+
if (stored?.blockedBy.includes(id)) {
|
|
584
|
+
stored.blockedBy = stored.blockedBy.filter((x) => x !== id)
|
|
585
|
+
this.writeStored(stored)
|
|
586
|
+
touched.push(stored)
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
removeSync(this.taskFile(id), this.sleep)
|
|
590
|
+
return { task, touched }
|
|
591
|
+
},
|
|
592
|
+
true,
|
|
593
|
+
)
|
|
594
|
+
})
|
|
595
|
+
if (!outcome) return false
|
|
596
|
+
for (const stored of outcome.touched) {
|
|
597
|
+
this.emit({
|
|
598
|
+
type: 'task.updated',
|
|
599
|
+
taskId: stored.id,
|
|
600
|
+
task: strip(stored),
|
|
601
|
+
timestamp: Date.now(),
|
|
602
|
+
})
|
|
603
|
+
}
|
|
604
|
+
this.emit({
|
|
605
|
+
type: 'task.deleted',
|
|
606
|
+
taskId: id,
|
|
607
|
+
task: strip(outcome.task),
|
|
608
|
+
timestamp: Date.now(),
|
|
609
|
+
})
|
|
610
|
+
return true
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Empty the list, only when every task in it is completed: other work is never thrown away by a
|
|
615
|
+
* reset. The high-water mark stays, so places are not reissued.
|
|
616
|
+
*/
|
|
617
|
+
async reset(): Promise<void> {
|
|
618
|
+
await this.inList(() => {
|
|
619
|
+
const all = this.allStored()
|
|
620
|
+
const open = all.filter((t) => t.status !== 'completed')
|
|
621
|
+
if (open.length > 0) {
|
|
622
|
+
throw new TaskListError(
|
|
623
|
+
'not_all_completed',
|
|
624
|
+
`${open.length} task${open.length === 1 ? ' is' : 's are'} not completed; the list is only reset when all of them are.`,
|
|
625
|
+
)
|
|
626
|
+
}
|
|
627
|
+
return this.inTasks(
|
|
628
|
+
all.map((t) => t.id),
|
|
629
|
+
() => {
|
|
630
|
+
for (const t of all) removeSync(this.taskFile(t.id), this.sleep)
|
|
631
|
+
},
|
|
632
|
+
true,
|
|
633
|
+
)
|
|
634
|
+
})
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/** Whether the list's directory exists yet. */
|
|
638
|
+
exists(): boolean {
|
|
639
|
+
return existsSync(this.dir)
|
|
640
|
+
}
|
|
641
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MailEntry, TeamBackendKind, TeamMember } from './types.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where teammates run. The in-process backend (a long-lived child session inside the lead's process)
|
|
5
|
+
* is the first; a separate-process or pane backend plugs in later behind the same four calls, so the
|
|
6
|
+
* rest of a team (names, mailboxes, the shared task list) does not change.
|
|
7
|
+
*
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
export interface TeammateStartSpec {
|
|
11
|
+
readonly team: string
|
|
12
|
+
/** The reserved member: its name and id are already in the team's `config.json`. */
|
|
13
|
+
readonly member: TeamMember
|
|
14
|
+
/** The first thing the teammate is told. The lead's conversation is not carried over. */
|
|
15
|
+
readonly prompt: string
|
|
16
|
+
readonly cwd?: string
|
|
17
|
+
readonly model?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TeammateBackend {
|
|
21
|
+
readonly kind: TeamBackendKind
|
|
22
|
+
/** Start the teammate. A rejection means nothing runs and the reservation is rolled back. */
|
|
23
|
+
start(spec: TeammateStartSpec): Promise<void>
|
|
24
|
+
/** Deliver mail to a running teammate now; the mailbox file is the record either way. */
|
|
25
|
+
send(team: string, name: string, mail: MailEntry): Promise<void>
|
|
26
|
+
/** End the teammate. */
|
|
27
|
+
stop(team: string, name: string, reason?: string): Promise<void>
|
|
28
|
+
isAlive(team: string, name: string): boolean
|
|
29
|
+
}
|