@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
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
lstatSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
readdirSync,
|
|
10
|
+
renameSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
rmdirSync,
|
|
13
|
+
writeSync,
|
|
14
|
+
} from 'node:fs'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
import { resolveNamzuHome } from '../session/home.js'
|
|
17
|
+
import { lockedSync, removeSync, sleepSync, writeAtomicSync } from './fs-sync.js'
|
|
18
|
+
import { TEAM_LEAD_NAME, normalizeTeamName, teamNameProblem, teammateNameProblem } from './names.js'
|
|
19
|
+
import {
|
|
20
|
+
type MailDraft,
|
|
21
|
+
type MailEntry,
|
|
22
|
+
MailEntrySchema,
|
|
23
|
+
TEAM_CONFIG_VERSION,
|
|
24
|
+
type TeamBackendKind,
|
|
25
|
+
type TeamConfig,
|
|
26
|
+
TeamConfigSchema,
|
|
27
|
+
type TeamMember,
|
|
28
|
+
TeamStoreError,
|
|
29
|
+
} from './types.js'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The teams on this machine, under `<NAMZU_HOME>/teams/<team>/`:
|
|
33
|
+
*
|
|
34
|
+
* - `config.json`: the team and its members, changed only through this class, under a lock and by
|
|
35
|
+
* rename, so two processes naming a member at once cannot both succeed.
|
|
36
|
+
* - `inboxes/<name>.json`: one mailbox per member and one for `lead` (the name is folded to lower case,
|
|
37
|
+
* so two spellings of a name never share or split a file). A JSON array of mail entries.
|
|
38
|
+
*
|
|
39
|
+
* `active` on a member means it holds a place in the team: it is set when the name is reserved and
|
|
40
|
+
* cleared when the teammate ends, so the size limit counts what is running or about to.
|
|
41
|
+
*
|
|
42
|
+
* @experimental
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** Colours handed out in order, so members are told apart on a screen. */
|
|
46
|
+
export const TEAM_COLORS = [
|
|
47
|
+
'blue',
|
|
48
|
+
'green',
|
|
49
|
+
'purple',
|
|
50
|
+
'orange',
|
|
51
|
+
'pink',
|
|
52
|
+
'cyan',
|
|
53
|
+
'yellow',
|
|
54
|
+
'red',
|
|
55
|
+
] as const
|
|
56
|
+
|
|
57
|
+
/** One piece of mail may carry this much text. */
|
|
58
|
+
export const MAX_MAIL_TEXT_BYTES = 64 * 1024
|
|
59
|
+
/** A mailbox keeps this many entries; the oldest read ones go first. */
|
|
60
|
+
export const MAX_MAIL_ENTRIES = 500
|
|
61
|
+
/** A team directory with no readable config is left alone this long, so one being created is not swept. */
|
|
62
|
+
export const TEAM_CREATE_GRACE_MS = 60_000
|
|
63
|
+
|
|
64
|
+
export interface TeamStoreOptions {
|
|
65
|
+
/** The directory teams live in. Default `<NAMZU_HOME>/teams`. */
|
|
66
|
+
readonly rootDir?: string
|
|
67
|
+
/** Environment and OS home for resolving `NAMZU_HOME`; tests only. */
|
|
68
|
+
readonly env?: NodeJS.ProcessEnv
|
|
69
|
+
readonly home?: string
|
|
70
|
+
/** Tests only: replace the wall clock, the wait between lock retries and the liveness check. */
|
|
71
|
+
readonly now?: () => number
|
|
72
|
+
readonly sleep?: (ms: number) => void
|
|
73
|
+
readonly isProcessAlive?: (pid: number) => boolean
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface CreateTeamInput {
|
|
77
|
+
readonly name: string
|
|
78
|
+
readonly leadSessionId: string
|
|
79
|
+
readonly leadPid?: number
|
|
80
|
+
readonly project?: string
|
|
81
|
+
readonly cwd?: string
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ReserveMemberInput {
|
|
85
|
+
readonly name: string
|
|
86
|
+
readonly backend?: TeamBackendKind
|
|
87
|
+
readonly agentType?: string
|
|
88
|
+
readonly color?: string
|
|
89
|
+
/** Refuse with `team_full` when this many members already hold a place. */
|
|
90
|
+
readonly maxMembers?: number
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MailReadOptions {
|
|
94
|
+
readonly unreadOnly?: boolean
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function processAlive(pid: number): boolean {
|
|
98
|
+
try {
|
|
99
|
+
process.kill(pid, 0)
|
|
100
|
+
return true
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// EPERM: the process exists but is another user's.
|
|
103
|
+
return (error as NodeJS.ErrnoException).code === 'EPERM'
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function errnoOf(error: unknown): string | undefined {
|
|
108
|
+
return (error as NodeJS.ErrnoException | undefined)?.code
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const MAILBOX_SHAPE = /^[a-z0-9][a-z0-9_-]{0,63}$/
|
|
112
|
+
|
|
113
|
+
export class TeamStore {
|
|
114
|
+
readonly rootDir: string
|
|
115
|
+
private readonly now: () => number
|
|
116
|
+
private readonly sleep: (ms: number) => void
|
|
117
|
+
private readonly alive: (pid: number) => boolean
|
|
118
|
+
|
|
119
|
+
constructor(options: TeamStoreOptions = {}) {
|
|
120
|
+
this.rootDir =
|
|
121
|
+
options.rootDir ??
|
|
122
|
+
join(
|
|
123
|
+
resolveNamzuHome({
|
|
124
|
+
...(options.env ? { env: options.env } : {}),
|
|
125
|
+
...(options.home ? { home: options.home } : {}),
|
|
126
|
+
}),
|
|
127
|
+
'teams',
|
|
128
|
+
)
|
|
129
|
+
this.now = options.now ?? Date.now
|
|
130
|
+
this.sleep = options.sleep ?? sleepSync
|
|
131
|
+
this.alive = options.isProcessAlive ?? processAlive
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ─── paths ───────────────────────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
teamDir(team: string): string {
|
|
137
|
+
const problem = teamNameProblem(team)
|
|
138
|
+
if (problem) throw new TeamStoreError('bad_name', problem)
|
|
139
|
+
return join(this.rootDir, team)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
configPath(team: string): string {
|
|
143
|
+
return join(this.teamDir(team), 'config.json')
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
inboxPath(team: string, name: string): string {
|
|
147
|
+
const folded = normalizeTeamName(name)
|
|
148
|
+
// A mailbox name is a file name: the shape rule applies to every caller, not only the one that sends.
|
|
149
|
+
if (!MAILBOX_SHAPE.test(folded))
|
|
150
|
+
throw new TeamStoreError('bad_name', `${JSON.stringify(name)} is not a valid mailbox name.`)
|
|
151
|
+
return join(this.teamDir(team), 'inboxes', `${folded}.json`)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ─── teams ───────────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Create a team. Asking again with the same lead session returns the team that is there (a resumed
|
|
158
|
+
* lead); a different lead session is `team_exists`.
|
|
159
|
+
*/
|
|
160
|
+
createTeam(input: CreateTeamInput): TeamConfig {
|
|
161
|
+
const dir = this.teamDir(input.name)
|
|
162
|
+
mkdirSync(this.rootDir, { recursive: true, mode: 0o700 })
|
|
163
|
+
try {
|
|
164
|
+
mkdirSync(dir, { mode: 0o700 })
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (errnoOf(error) !== 'EEXIST') throw error
|
|
167
|
+
// The creator makes the directory before it writes the config; give it a moment to finish.
|
|
168
|
+
let existing = this.readTeam(input.name)
|
|
169
|
+
for (let attempt = 0; existing === undefined && attempt < 20; attempt++) {
|
|
170
|
+
this.sleep(5 + attempt * 5)
|
|
171
|
+
existing = this.readTeam(input.name)
|
|
172
|
+
}
|
|
173
|
+
if (existing && existing.leadSessionId === input.leadSessionId) return existing
|
|
174
|
+
throw new TeamStoreError('team_exists', `The team ${input.name} already exists.`)
|
|
175
|
+
}
|
|
176
|
+
mkdirSync(join(dir, 'inboxes'), { mode: 0o700 })
|
|
177
|
+
const config: TeamConfig = {
|
|
178
|
+
version: TEAM_CONFIG_VERSION,
|
|
179
|
+
name: input.name,
|
|
180
|
+
leadSessionId: input.leadSessionId,
|
|
181
|
+
leadPid: input.leadPid ?? process.pid,
|
|
182
|
+
...(input.project !== undefined ? { project: input.project } : {}),
|
|
183
|
+
...(input.cwd !== undefined ? { cwd: input.cwd } : {}),
|
|
184
|
+
createdAt: this.now(),
|
|
185
|
+
members: [],
|
|
186
|
+
}
|
|
187
|
+
this.writeConfig(input.name, config)
|
|
188
|
+
return config
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The team's config, or undefined when there is no such team or its file is unreadable. */
|
|
192
|
+
readTeam(team: string): TeamConfig | undefined {
|
|
193
|
+
let text: string
|
|
194
|
+
try {
|
|
195
|
+
text = readFileSync(this.configPath(team), 'utf8')
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (error instanceof TeamStoreError) throw error
|
|
198
|
+
return undefined
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
const parsed = TeamConfigSchema.safeParse(JSON.parse(text))
|
|
202
|
+
return parsed.success ? parsed.data : undefined
|
|
203
|
+
} catch {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
listTeams(): string[] {
|
|
209
|
+
let entries: string[]
|
|
210
|
+
try {
|
|
211
|
+
entries = readdirSync(this.rootDir)
|
|
212
|
+
} catch {
|
|
213
|
+
return []
|
|
214
|
+
}
|
|
215
|
+
return entries.filter((name) => !teamNameProblem(name) && this.readTeam(name) !== undefined)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Remove a team's directory. Returns false when there was none. Tasks and transcripts live elsewhere. */
|
|
219
|
+
deleteTeam(team: string): boolean {
|
|
220
|
+
const dir = this.teamDir(team)
|
|
221
|
+
let entry: ReturnType<typeof lstatSync>
|
|
222
|
+
try {
|
|
223
|
+
entry = lstatSync(dir)
|
|
224
|
+
} catch {
|
|
225
|
+
return false
|
|
226
|
+
}
|
|
227
|
+
if (entry.isSymbolicLink() || !entry.isDirectory()) return false
|
|
228
|
+
rmSync(dir, { recursive: true, force: true })
|
|
229
|
+
return true
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Remove the directories of teams whose lead is gone: the lead's process no longer exists, or the
|
|
234
|
+
* directory has no readable config and was last changed more than `graceMs` ago. A team that records
|
|
235
|
+
* no lead process is never removed here. Returns the names removed.
|
|
236
|
+
*/
|
|
237
|
+
sweepOrphans(options: { readonly graceMs?: number } = {}): string[] {
|
|
238
|
+
const grace = options.graceMs ?? TEAM_CREATE_GRACE_MS
|
|
239
|
+
let entries: string[]
|
|
240
|
+
try {
|
|
241
|
+
entries = readdirSync(this.rootDir)
|
|
242
|
+
} catch {
|
|
243
|
+
return []
|
|
244
|
+
}
|
|
245
|
+
const removed: string[] = []
|
|
246
|
+
for (const name of entries) {
|
|
247
|
+
if (teamNameProblem(name)) continue
|
|
248
|
+
const dir = join(this.rootDir, name)
|
|
249
|
+
let entry: ReturnType<typeof lstatSync>
|
|
250
|
+
try {
|
|
251
|
+
entry = lstatSync(dir)
|
|
252
|
+
} catch {
|
|
253
|
+
continue
|
|
254
|
+
}
|
|
255
|
+
if (entry.isSymbolicLink() || !entry.isDirectory()) continue
|
|
256
|
+
const config = this.readTeam(name)
|
|
257
|
+
const gone = config
|
|
258
|
+
? config.leadPid !== undefined && !this.alive(config.leadPid)
|
|
259
|
+
: this.now() - entry.mtimeMs > grace
|
|
260
|
+
if (!gone) continue
|
|
261
|
+
rmSync(dir, { recursive: true, force: true })
|
|
262
|
+
removed.push(name)
|
|
263
|
+
}
|
|
264
|
+
return removed
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ─── members ─────────────────────────────────────────────────────────
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Take a name in the team's file. Two callers asking for one name at once: exactly one gets it, the
|
|
271
|
+
* other gets `name_taken`. The member holds a place from now on; hand it back with
|
|
272
|
+
* {@link rollbackMember} if starting it fails, or let {@link withReservedMember} do both.
|
|
273
|
+
*/
|
|
274
|
+
reserveMember(team: string, input: ReserveMemberInput): TeamMember {
|
|
275
|
+
const problem = teammateNameProblem(input.name)
|
|
276
|
+
if (problem) throw new TeamStoreError('bad_name', problem)
|
|
277
|
+
return this.mutate(team, (config) => {
|
|
278
|
+
const folded = normalizeTeamName(input.name)
|
|
279
|
+
if (config.members.some((m) => normalizeTeamName(m.name) === folded))
|
|
280
|
+
throw new TeamStoreError('name_taken', `${input.name} is already a member of ${team}.`)
|
|
281
|
+
if (
|
|
282
|
+
input.maxMembers !== undefined &&
|
|
283
|
+
config.members.filter((m) => m.active).length >= input.maxMembers
|
|
284
|
+
)
|
|
285
|
+
throw new TeamStoreError(
|
|
286
|
+
'team_full',
|
|
287
|
+
`The team ${team} already has ${input.maxMembers} teammates at work.`,
|
|
288
|
+
)
|
|
289
|
+
const member: TeamMember = {
|
|
290
|
+
id: randomUUID(),
|
|
291
|
+
name: input.name,
|
|
292
|
+
color: input.color ?? this.nextColor(config),
|
|
293
|
+
joinedAt: this.now(),
|
|
294
|
+
backend: input.backend ?? 'in-process',
|
|
295
|
+
active: true,
|
|
296
|
+
...(input.agentType !== undefined ? { agentType: input.agentType } : {}),
|
|
297
|
+
}
|
|
298
|
+
return { config: { ...config, members: [...config.members, member] }, result: member }
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Give back a reservation that never started. Removes exactly the member with this id, so a later
|
|
304
|
+
* member that took the same name is never touched. False when it is already gone, or the team is.
|
|
305
|
+
*/
|
|
306
|
+
rollbackMember(team: string, memberId: string): boolean {
|
|
307
|
+
try {
|
|
308
|
+
return this.mutate(team, (config) => {
|
|
309
|
+
const keep = config.members.filter((m) => m.id !== memberId)
|
|
310
|
+
if (keep.length === config.members.length) return { config, result: false }
|
|
311
|
+
return { config: { ...config, members: keep }, result: true }
|
|
312
|
+
})
|
|
313
|
+
} catch (error) {
|
|
314
|
+
if (error instanceof TeamStoreError && error.code === 'team_not_found') return false
|
|
315
|
+
throw error
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Reserve a name, run `start`, and give the name back if `start` throws: a teammate is either
|
|
321
|
+
* running under its name or the name is free again.
|
|
322
|
+
*/
|
|
323
|
+
async withReservedMember<T>(
|
|
324
|
+
team: string,
|
|
325
|
+
input: ReserveMemberInput,
|
|
326
|
+
start: (member: TeamMember) => Promise<T>,
|
|
327
|
+
): Promise<{ readonly member: TeamMember; readonly result: T }> {
|
|
328
|
+
const member = this.reserveMember(team, input)
|
|
329
|
+
try {
|
|
330
|
+
return { member, result: await start(member) }
|
|
331
|
+
} catch (error) {
|
|
332
|
+
this.rollbackMember(team, member.id)
|
|
333
|
+
throw error
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Mark a member as holding a place (resumed) or not (ended). Resuming is refused over `maxMembers`. */
|
|
338
|
+
setMemberActive(team: string, name: string, active: boolean, maxMembers?: number): TeamMember {
|
|
339
|
+
return this.mutate(team, (config) => {
|
|
340
|
+
const folded = normalizeTeamName(name)
|
|
341
|
+
const at = config.members.findIndex((m) => normalizeTeamName(m.name) === folded)
|
|
342
|
+
const current = config.members[at]
|
|
343
|
+
if (current === undefined)
|
|
344
|
+
throw new TeamStoreError('member_not_found', `${name} is not a member of ${team}.`)
|
|
345
|
+
if (current.active === active) return { config, result: current }
|
|
346
|
+
if (
|
|
347
|
+
active &&
|
|
348
|
+
maxMembers !== undefined &&
|
|
349
|
+
config.members.filter((m) => m.active).length >= maxMembers
|
|
350
|
+
)
|
|
351
|
+
throw new TeamStoreError(
|
|
352
|
+
'team_full',
|
|
353
|
+
`The team ${team} already has ${maxMembers} teammates at work.`,
|
|
354
|
+
)
|
|
355
|
+
const next: TeamMember = { ...current, active }
|
|
356
|
+
const members = config.members.slice()
|
|
357
|
+
members[at] = next
|
|
358
|
+
return { config: { ...config, members }, result: next }
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Remove a member and its mailbox. False when there is no such member. */
|
|
363
|
+
removeMember(team: string, name: string): boolean {
|
|
364
|
+
const folded = normalizeTeamName(name)
|
|
365
|
+
this.inboxPath(team, name)
|
|
366
|
+
const removed = this.mutate(team, (config) => {
|
|
367
|
+
const keep = config.members.filter((m) => normalizeTeamName(m.name) !== folded)
|
|
368
|
+
if (keep.length === config.members.length) return { config, result: false }
|
|
369
|
+
return { config: { ...config, members: keep }, result: true }
|
|
370
|
+
})
|
|
371
|
+
if (removed) removeSync(this.inboxPath(team, name), this.sleep)
|
|
372
|
+
return removed
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ─── mail ────────────────────────────────────────────────────────────
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Add one piece of mail to a member's mailbox (or the lead's). The file is made if it is not there
|
|
379
|
+
* (create-exclusive), then read, added to and replaced by rename under a lock, so appends from many
|
|
380
|
+
* processes are all kept. Refused for a team that is gone, a recipient that is not in it, and text
|
|
381
|
+
* over {@link MAX_MAIL_TEXT_BYTES}.
|
|
382
|
+
*/
|
|
383
|
+
appendMail(team: string, to: string, draft: MailDraft): MailEntry {
|
|
384
|
+
if (Buffer.byteLength(draft.text, 'utf8') > MAX_MAIL_TEXT_BYTES)
|
|
385
|
+
throw new TeamStoreError(
|
|
386
|
+
'too_large',
|
|
387
|
+
`A message may be at most ${MAX_MAIL_TEXT_BYTES / 1024} KiB.`,
|
|
388
|
+
)
|
|
389
|
+
this.requireRecipient(team, to)
|
|
390
|
+
const path = this.inboxPath(team, to)
|
|
391
|
+
this.ensureInbox(team, path)
|
|
392
|
+
const entry: MailEntry = {
|
|
393
|
+
id: randomUUID(),
|
|
394
|
+
from: draft.from,
|
|
395
|
+
text: draft.text,
|
|
396
|
+
...(draft.summary !== undefined ? { summary: draft.summary } : {}),
|
|
397
|
+
at: new Date(this.now()).toISOString(),
|
|
398
|
+
...(draft.kind !== undefined ? { kind: draft.kind } : {}),
|
|
399
|
+
}
|
|
400
|
+
this.changeInbox(team, path, (entries) => [...entries, entry])
|
|
401
|
+
return entry
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** The valid entries in a mailbox, oldest first. An entry that does not match the schema is left out. */
|
|
405
|
+
readMail(team: string, name: string, options: MailReadOptions = {}): MailEntry[] {
|
|
406
|
+
const entries = this.readInbox(this.inboxPath(team, name)).entries
|
|
407
|
+
return options.unreadOnly ? entries.filter((e) => e.read !== true) : entries
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Take the unread mail and mark it read, in one step: no entry is handed out twice. */
|
|
411
|
+
drainMail(team: string, name: string): MailEntry[] {
|
|
412
|
+
const path = this.inboxPath(team, name)
|
|
413
|
+
if (!existsSync(path)) return []
|
|
414
|
+
let taken: MailEntry[] = []
|
|
415
|
+
this.changeInbox(team, path, (entries) => {
|
|
416
|
+
taken = entries.filter((e) => e.read !== true)
|
|
417
|
+
return entries.map((e) => (e.read === true ? e : { ...e, read: true }))
|
|
418
|
+
})
|
|
419
|
+
return taken
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
markRead(team: string, name: string, ids: readonly string[]): void {
|
|
423
|
+
const path = this.inboxPath(team, name)
|
|
424
|
+
if (!existsSync(path)) return
|
|
425
|
+
const wanted = new Set(ids)
|
|
426
|
+
this.changeInbox(team, path, (entries) =>
|
|
427
|
+
entries.map((e) => (wanted.has(e.id) && e.read !== true ? { ...e, read: true } : e)),
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Rewrite a mailbox without its invalid entries and with at most `keepRead` read ones (the newest).
|
|
433
|
+
* Unread mail is always kept. Returns how many entries were dropped.
|
|
434
|
+
*/
|
|
435
|
+
pruneMail(team: string, name: string, options: { readonly keepRead?: number } = {}): number {
|
|
436
|
+
const path = this.inboxPath(team, name)
|
|
437
|
+
if (!existsSync(path)) return 0
|
|
438
|
+
const keepRead = options.keepRead ?? 50
|
|
439
|
+
let dropped = 0
|
|
440
|
+
this.changeInbox(team, path, (entries, invalid) => {
|
|
441
|
+
const read = entries.filter((e) => e.read === true)
|
|
442
|
+
const excess = new Set(read.slice(0, Math.max(0, read.length - keepRead)).map((e) => e.id))
|
|
443
|
+
dropped = invalid + excess.size
|
|
444
|
+
return entries.filter((e) => !excess.has(e.id))
|
|
445
|
+
})
|
|
446
|
+
return dropped
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ─── internals ───────────────────────────────────────────────────────
|
|
450
|
+
|
|
451
|
+
private nextColor(config: TeamConfig): string {
|
|
452
|
+
const used = new Set(config.members.map((m) => m.color))
|
|
453
|
+
return (
|
|
454
|
+
TEAM_COLORS.find((c) => !used.has(c)) ??
|
|
455
|
+
TEAM_COLORS[config.members.length % TEAM_COLORS.length] ??
|
|
456
|
+
'blue'
|
|
457
|
+
)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private writeConfig(team: string, config: TeamConfig): void {
|
|
461
|
+
writeAtomicSync(this.configPath(team), `${JSON.stringify(config, null, 2)}\n`, this.sleep)
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** Remove a team directory this call may have recreated while taking a lock on a team that is gone. */
|
|
465
|
+
private dropRecreated(team: string): void {
|
|
466
|
+
try {
|
|
467
|
+
if (existsSync(this.configPath(team))) return
|
|
468
|
+
rmdirSync(join(this.teamDir(team), 'inboxes'))
|
|
469
|
+
} catch {
|
|
470
|
+
// Not empty, or not there: nothing to undo.
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
if (!existsSync(this.configPath(team))) rmdirSync(this.teamDir(team))
|
|
474
|
+
} catch {
|
|
475
|
+
// Not empty, or not there.
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** Change the config under its lock. Refused with `team_not_found` when the team has been removed. */
|
|
480
|
+
private mutate<T>(
|
|
481
|
+
team: string,
|
|
482
|
+
change: (config: TeamConfig) => { readonly config: TeamConfig; readonly result: T },
|
|
483
|
+
): T {
|
|
484
|
+
const path = this.configPath(team)
|
|
485
|
+
if (!existsSync(path)) throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
486
|
+
let found = true
|
|
487
|
+
try {
|
|
488
|
+
return lockedSync(path, this.sleep, () => {
|
|
489
|
+
const current = this.readTeam(team)
|
|
490
|
+
if (current === undefined) {
|
|
491
|
+
found = false
|
|
492
|
+
throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
493
|
+
}
|
|
494
|
+
const next = change(current)
|
|
495
|
+
if (next.config !== current) this.writeConfig(team, next.config)
|
|
496
|
+
return next.result
|
|
497
|
+
})
|
|
498
|
+
} finally {
|
|
499
|
+
if (!found) this.dropRecreated(team)
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
private requireRecipient(team: string, to: string): void {
|
|
504
|
+
const config = this.readTeam(team)
|
|
505
|
+
if (config === undefined)
|
|
506
|
+
throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
507
|
+
const folded = normalizeTeamName(to)
|
|
508
|
+
if (folded === TEAM_LEAD_NAME) return
|
|
509
|
+
if (!config.members.some((m) => normalizeTeamName(m.name) === folded))
|
|
510
|
+
throw new TeamStoreError('unknown_recipient', `${to} is not a member of ${team}.`)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private ensureInbox(team: string, path: string): void {
|
|
514
|
+
try {
|
|
515
|
+
mkdirSync(join(this.teamDir(team), 'inboxes'), { mode: 0o700 })
|
|
516
|
+
} catch (error) {
|
|
517
|
+
const code = errnoOf(error)
|
|
518
|
+
if (code === 'ENOENT') throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
519
|
+
if (code !== 'EEXIST') throw error
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
const fd = openSync(path, 'wx', 0o600)
|
|
523
|
+
try {
|
|
524
|
+
writeSync(fd, '[]\n')
|
|
525
|
+
} finally {
|
|
526
|
+
closeSync(fd)
|
|
527
|
+
}
|
|
528
|
+
} catch (error) {
|
|
529
|
+
if (errnoOf(error) !== 'EEXIST') throw error
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
private readInbox(path: string): { entries: MailEntry[]; invalid: number; corrupt?: boolean } {
|
|
534
|
+
let text: string
|
|
535
|
+
try {
|
|
536
|
+
text = readFileSync(path, 'utf8')
|
|
537
|
+
} catch {
|
|
538
|
+
return { entries: [], invalid: 0 }
|
|
539
|
+
}
|
|
540
|
+
// A zero-length file is what a crash between create and first write leaves: an empty mailbox.
|
|
541
|
+
if (text.trim() === '') return { entries: [], invalid: 0 }
|
|
542
|
+
let raw: unknown
|
|
543
|
+
try {
|
|
544
|
+
raw = JSON.parse(text)
|
|
545
|
+
} catch {
|
|
546
|
+
return { entries: [], invalid: 1, corrupt: true }
|
|
547
|
+
}
|
|
548
|
+
if (!Array.isArray(raw)) return { entries: [], invalid: 1, corrupt: true }
|
|
549
|
+
const entries: MailEntry[] = []
|
|
550
|
+
let invalid = 0
|
|
551
|
+
for (const item of raw) {
|
|
552
|
+
const parsed = MailEntrySchema.safeParse(item)
|
|
553
|
+
if (parsed.success) entries.push(parsed.data)
|
|
554
|
+
else invalid++
|
|
555
|
+
}
|
|
556
|
+
return { entries, invalid }
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Read, change and replace a mailbox under its lock. Invalid entries are dropped on the way, and the size is bounded. */
|
|
560
|
+
private changeInbox(
|
|
561
|
+
team: string,
|
|
562
|
+
path: string,
|
|
563
|
+
change: (entries: MailEntry[], invalid: number) => MailEntry[],
|
|
564
|
+
): void {
|
|
565
|
+
let found = true
|
|
566
|
+
try {
|
|
567
|
+
lockedSync(path, this.sleep, () => {
|
|
568
|
+
if (!existsSync(this.configPath(team))) {
|
|
569
|
+
found = false
|
|
570
|
+
throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
571
|
+
}
|
|
572
|
+
const { entries, invalid, corrupt } = this.readInbox(path)
|
|
573
|
+
if (corrupt) {
|
|
574
|
+
// Keep what was there for a person to look at rather than replacing it silently.
|
|
575
|
+
try {
|
|
576
|
+
renameSync(path, `${path}.corrupt-${this.now()}`)
|
|
577
|
+
} catch {
|
|
578
|
+
// The file went away: nothing to keep.
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
let next = change(entries, invalid)
|
|
582
|
+
if (next.length > MAX_MAIL_ENTRIES) {
|
|
583
|
+
let over = next.length - MAX_MAIL_ENTRIES
|
|
584
|
+
next = next.filter((e) => {
|
|
585
|
+
if (over > 0 && e.read === true) {
|
|
586
|
+
over--
|
|
587
|
+
return false
|
|
588
|
+
}
|
|
589
|
+
return true
|
|
590
|
+
})
|
|
591
|
+
if (next.length > MAX_MAIL_ENTRIES) next = next.slice(next.length - MAX_MAIL_ENTRIES)
|
|
592
|
+
}
|
|
593
|
+
try {
|
|
594
|
+
writeAtomicSync(path, `${JSON.stringify(next)}\n`, this.sleep)
|
|
595
|
+
} catch (error) {
|
|
596
|
+
if (errnoOf(error) === 'ENOENT') {
|
|
597
|
+
found = false
|
|
598
|
+
throw new TeamStoreError('team_not_found', `There is no team ${team}.`)
|
|
599
|
+
}
|
|
600
|
+
throw error
|
|
601
|
+
}
|
|
602
|
+
})
|
|
603
|
+
} finally {
|
|
604
|
+
if (!found) this.dropRecreated(team)
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a team keeps on disk, under `<NAMZU_HOME>/teams/<team>/`.
|
|
5
|
+
*
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const TEAM_CONFIG_VERSION = 1
|
|
10
|
+
|
|
11
|
+
/** Where a teammate runs. Only `in-process` exists today; the others are named so a later backend plugs in. */
|
|
12
|
+
export const TeamBackendKindSchema = z.enum(['in-process', 'process', 'pane'])
|
|
13
|
+
export type TeamBackendKind = z.infer<typeof TeamBackendKindSchema>
|
|
14
|
+
|
|
15
|
+
export const TeamMemberSchema = z
|
|
16
|
+
.object({
|
|
17
|
+
/** Stable identity of this member in this team; a rollback removes exactly this one. */
|
|
18
|
+
id: z.string().min(1),
|
|
19
|
+
name: z.string().min(1),
|
|
20
|
+
color: z.string().min(1),
|
|
21
|
+
/** Milliseconds since the epoch. */
|
|
22
|
+
joinedAt: z.number().int().nonnegative(),
|
|
23
|
+
backend: TeamBackendKindSchema,
|
|
24
|
+
/** False from the moment a name is reserved until the teammate has started, and again once it has ended. */
|
|
25
|
+
active: z.boolean(),
|
|
26
|
+
agentType: z.string().min(1).optional(),
|
|
27
|
+
})
|
|
28
|
+
.strict()
|
|
29
|
+
export type TeamMember = z.infer<typeof TeamMemberSchema>
|
|
30
|
+
|
|
31
|
+
export const TeamConfigSchema = z
|
|
32
|
+
.object({
|
|
33
|
+
version: z.literal(TEAM_CONFIG_VERSION),
|
|
34
|
+
name: z.string().min(1),
|
|
35
|
+
leadSessionId: z.string().min(1),
|
|
36
|
+
/** The lead's process, so a later start can tell a team whose lead is gone. */
|
|
37
|
+
leadPid: z.number().int().positive().optional(),
|
|
38
|
+
project: z.string().optional(),
|
|
39
|
+
cwd: z.string().optional(),
|
|
40
|
+
createdAt: z.number().int().nonnegative(),
|
|
41
|
+
members: z.array(TeamMemberSchema),
|
|
42
|
+
})
|
|
43
|
+
.strict()
|
|
44
|
+
export type TeamConfig = z.infer<typeof TeamConfigSchema>
|
|
45
|
+
|
|
46
|
+
export const MAIL_KINDS = [
|
|
47
|
+
'message',
|
|
48
|
+
'idle',
|
|
49
|
+
'shutdown_request',
|
|
50
|
+
'shutdown_response',
|
|
51
|
+
'plan_request',
|
|
52
|
+
'plan_response',
|
|
53
|
+
'permission_request',
|
|
54
|
+
'permission_response',
|
|
55
|
+
] as const
|
|
56
|
+
export const MailKindSchema = z.enum(MAIL_KINDS)
|
|
57
|
+
export type MailKind = z.infer<typeof MailKindSchema>
|
|
58
|
+
|
|
59
|
+
/** One piece of mail in a member's inbox file. */
|
|
60
|
+
export const MailEntrySchema = z
|
|
61
|
+
.object({
|
|
62
|
+
id: z.string().min(1),
|
|
63
|
+
/** A member's name, or `lead`. */
|
|
64
|
+
from: z.string().min(1),
|
|
65
|
+
text: z.string(),
|
|
66
|
+
summary: z.string().optional(),
|
|
67
|
+
/** ISO-8601 time the entry was written. */
|
|
68
|
+
at: z.string().min(1),
|
|
69
|
+
read: z.boolean().optional(),
|
|
70
|
+
kind: MailKindSchema.optional(),
|
|
71
|
+
})
|
|
72
|
+
.strict()
|
|
73
|
+
export type MailEntry = z.infer<typeof MailEntrySchema>
|
|
74
|
+
|
|
75
|
+
/** What a caller gives for a new piece of mail; the store adds the id and the time. */
|
|
76
|
+
export interface MailDraft {
|
|
77
|
+
readonly from: string
|
|
78
|
+
readonly text: string
|
|
79
|
+
readonly summary?: string
|
|
80
|
+
readonly kind?: MailKind
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type TeamStoreErrorCode =
|
|
84
|
+
| 'bad_name'
|
|
85
|
+
| 'team_exists'
|
|
86
|
+
| 'team_not_found'
|
|
87
|
+
| 'name_taken'
|
|
88
|
+
| 'team_full'
|
|
89
|
+
| 'member_not_found'
|
|
90
|
+
| 'unknown_recipient'
|
|
91
|
+
| 'too_large'
|
|
92
|
+
| 'busy'
|
|
93
|
+
|
|
94
|
+
export class TeamStoreError extends Error {
|
|
95
|
+
override readonly name = 'TeamStoreError'
|
|
96
|
+
constructor(
|
|
97
|
+
readonly code: TeamStoreErrorCode,
|
|
98
|
+
message: string,
|
|
99
|
+
) {
|
|
100
|
+
super(message)
|
|
101
|
+
}
|
|
102
|
+
}
|