loopctl-mcp-server 3.20.3 → 3.21.1
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.md +16 -13
- package/index.js +392 -110
- package/lib/branch-item.js +110 -0
- package/lib/custody-key.js +2 -2
- package/lib/default-key.js +17 -0
- package/lib/delivery-loop.js +4 -2
- package/lib/holder.js +255 -0
- package/lib/work-items.js +82 -0
- package/package.json +1 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `work_item_open` and the heartbeat's branch open (loopctl US-52.18): an interactive
|
|
3
|
+
* session's branch becomes a work item it holds — `POST /api/v1/work_items`.
|
|
4
|
+
*
|
|
5
|
+
* KEY: the session's holder credential. The route refuses any other key 403
|
|
6
|
+
* holder_credential_required, so the automatic open runs only while this process holds one.
|
|
7
|
+
*
|
|
8
|
+
* ## The automatic open
|
|
9
|
+
*
|
|
10
|
+
* On each holder heartbeat this reads the worktree's branch through an injected git runner.
|
|
11
|
+
* When the branch is neither the remote's default branch nor a detached HEAD, and this holder
|
|
12
|
+
* has not opened it yet, it calls the route once. What it remembers is per HOLDER: a session
|
|
13
|
+
* that re-registers as a new holder opens its branch again, which loopctl answers by taking
|
|
14
|
+
* the unheld item back. A 409 branch_held (another session's item) and any other refusal the
|
|
15
|
+
* server will repeat (4xx) are logged to stderr and not retried for that branch; a network
|
|
16
|
+
* error, a 429 or a 5xx is retried on the next heartbeat. Nothing is created locally, and the
|
|
17
|
+
* open is idempotent on the server, so a lost response costs one repeated call.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export const WORK_ITEMS_PATH = "/api/v1/work_items";
|
|
21
|
+
|
|
22
|
+
function refuse(body) {
|
|
23
|
+
return { error: true, status: 0, body };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function present(value) {
|
|
27
|
+
return typeof value === "string" && value.trim() !== "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `POST /api/v1/work_items`: open (or reopen) the item for a branch. */
|
|
31
|
+
export async function openWorkItem({ repo_url, branch, title, epic_id } = {}, { apiCall } = {}) {
|
|
32
|
+
if (!present(repo_url)) return refuse("`repo_url` is required: the repo's remote URL.");
|
|
33
|
+
if (!present(branch)) return refuse("`branch` is required: the branch the work is on.");
|
|
34
|
+
if (title !== undefined && title !== null && typeof title !== "string") {
|
|
35
|
+
return refuse("`title` must be a string.");
|
|
36
|
+
}
|
|
37
|
+
if (epic_id !== undefined && epic_id !== null && typeof epic_id !== "string") {
|
|
38
|
+
return refuse("`epic_id` must be a UUID string.");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const payload = { repo_url, branch };
|
|
42
|
+
if (present(title)) payload.title = title;
|
|
43
|
+
if (present(epic_id)) payload.epic_id = epic_id;
|
|
44
|
+
return apiCall("POST", WORK_ITEMS_PATH, payload);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// A refusal the server will give again for the same branch: not retried.
|
|
48
|
+
function final(result) {
|
|
49
|
+
if (!result || !result.error) return true;
|
|
50
|
+
const status = Number(result.status) || 0;
|
|
51
|
+
return status >= 400 && status < 500 && status !== 408 && status !== 429;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {{
|
|
56
|
+
* apiCall: Function, // (method, path, body, key, opts) => result
|
|
57
|
+
* git: (args: string[]) => Promise<string|null>, // stdout trimmed, null on failure
|
|
58
|
+
* worktree: () => string,
|
|
59
|
+
* log?: (line: string) => void,
|
|
60
|
+
* }} deps
|
|
61
|
+
*/
|
|
62
|
+
export function createBranchItemOpener(deps) {
|
|
63
|
+
const { apiCall, git, worktree, log = (line) => process.stderr.write(line + "\n") } = deps;
|
|
64
|
+
let holderId = null;
|
|
65
|
+
let done = new Set();
|
|
66
|
+
|
|
67
|
+
async function defaultBranch(wt) {
|
|
68
|
+
const ref = await git(["-C", wt, "symbolic-ref", "refs/remotes/origin/HEAD"]);
|
|
69
|
+
return present(ref) ? ref.trim().replace(/^refs\/remotes\/origin\//, "") : null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* One heartbeat's look at the worktree. `holder` is the live holder, `credential` its raw
|
|
74
|
+
* key. Returns what it did: "skipped", "opened", "refused" or "retry".
|
|
75
|
+
*/
|
|
76
|
+
async function tick(holder, credential) {
|
|
77
|
+
if (!holder || !credential) return "skipped";
|
|
78
|
+
if (holder.id !== holderId) {
|
|
79
|
+
holderId = holder.id;
|
|
80
|
+
done = new Set();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const wt = worktree();
|
|
84
|
+
const branch = ((await git(["-C", wt, "rev-parse", "--abbrev-ref", "HEAD"])) || "").trim();
|
|
85
|
+
if (branch === "" || branch === "HEAD" || done.has(branch)) return "skipped";
|
|
86
|
+
|
|
87
|
+
const fallbackDefaults = ["main", "master"];
|
|
88
|
+
const known = await defaultBranch(wt);
|
|
89
|
+
if (known ? branch === known : fallbackDefaults.includes(branch)) return "skipped";
|
|
90
|
+
|
|
91
|
+
const repoUrl = ((await git(["-C", wt, "remote", "get-url", "origin"])) || "").trim();
|
|
92
|
+
if (repoUrl === "") return "skipped";
|
|
93
|
+
|
|
94
|
+
const result = await openWorkItem(
|
|
95
|
+
{ repo_url: repoUrl, branch },
|
|
96
|
+
{ apiCall: (method, path, body) => apiCall(method, path, body, credential, { exactKey: true }) },
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (!final(result)) return "retry";
|
|
100
|
+
done.add(branch);
|
|
101
|
+
if (result && result.error) {
|
|
102
|
+
const code = result.body && result.body.error ? result.body.error.code : result.status;
|
|
103
|
+
log(`loopctl: work item for branch ${branch} not opened (${code}); not retrying it`);
|
|
104
|
+
return "refused";
|
|
105
|
+
}
|
|
106
|
+
return "opened";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { tick };
|
|
110
|
+
}
|
package/lib/custody-key.js
CHANGED
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
* excused an assertion at the far end of the very block that disproves the claim.
|
|
28
28
|
*
|
|
29
29
|
* What the pinning IS for is an operator's expressed configuration. `resolveKey` in
|
|
30
|
-
* `index.js` reads
|
|
31
|
-
* override wins over the key the tool names. So an operator who set `LOOPCTL_ORCH_KEY`
|
|
30
|
+
* `index.js` reads LOOPCTL_API_KEY before the tool's override (`lib/default-key.js`) — the
|
|
31
|
+
* global override wins over the key the tool names. So an operator who set `LOOPCTL_ORCH_KEY`
|
|
32
32
|
* specifically for these verbs AND a `LOOPCTL_API_KEY` of some other role for everything else
|
|
33
33
|
* had the orchestrator key silently discarded on precisely the calls they set it for, and paid a
|
|
34
34
|
* round trip to learn it. Honouring it costs nothing and is what they asked for.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The key a request sends when its tool pins none (`apiCall` without `exactKey`).
|
|
3
|
+
*
|
|
4
|
+
* Order: LOOPCTL_API_KEY, then the caller's override (a tool that names the agent or user
|
|
5
|
+
* key), then LOOPCTL_ORCH_KEY, then this session's agent key (its holder credential once
|
|
6
|
+
* registered, else LOOPCTL_AGENT_KEY).
|
|
7
|
+
*
|
|
8
|
+
* The last step is what lets a session that holds ONLY an agent key read at all. Without
|
|
9
|
+
* it, list_stories and every other default-key read answered "No API key configured" in a
|
|
10
|
+
* holder session, the shape the custody rule asks for (one key per process, never the
|
|
11
|
+
* implementer's and the reviewer's together). Falling back to a key the process already
|
|
12
|
+
* holds grants nothing: loopctl decides what that key's role may do, and a write it may not
|
|
13
|
+
* make is still refused there.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveDefaultKey({ env, keyOverride, agentKey }) {
|
|
16
|
+
return env.LOOPCTL_API_KEY || keyOverride || env.LOOPCTL_ORCH_KEY || agentKey() || undefined;
|
|
17
|
+
}
|
package/lib/delivery-loop.js
CHANGED
|
@@ -319,13 +319,15 @@ export async function resolveEscalation({ story_id, to, reason } = {}, { userKey
|
|
|
319
319
|
* the orchestrator role" (`require_role.ex:112-128`), so the request never reaches the
|
|
320
320
|
* controller and never reaches the custody 409s in `Progress`. The role error names the role.
|
|
321
321
|
*/
|
|
322
|
-
export async function forceUnclaimStory({ story_id } = {}, { orchKey, apiCall } = {}) {
|
|
322
|
+
export async function forceUnclaimStory({ story_id, reason } = {}, { orchKey, apiCall } = {}) {
|
|
323
323
|
if (!orchKey) return refuse(MISSING_ORCH_KEY);
|
|
324
324
|
|
|
325
325
|
const bad = uuid(story_id, "story_id");
|
|
326
326
|
if (bad) return bad;
|
|
327
327
|
|
|
328
|
-
|
|
328
|
+
// US-52.16: the optional reason goes on the story's event log; no reason, no body.
|
|
329
|
+
const body = typeof reason === "string" && reason.trim() !== "" ? { reason } : null;
|
|
330
|
+
return apiCall("POST", forceUnclaimPath(story_id), body);
|
|
329
331
|
}
|
|
330
332
|
|
|
331
333
|
export function mergePreconditionPath(storyId) {
|
package/lib/holder.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session holder (US-52.13): this MCP process registers its Claude Code session with
|
|
3
|
+
* loopctl, sends every agent-key request with the holder's own credential, heartbeats the
|
|
4
|
+
* holder's lease, and ends it on shutdown.
|
|
5
|
+
*
|
|
6
|
+
* Before this, every fleet session sent the same LOOPCTL_AGENT_KEY, so loopctl could not
|
|
7
|
+
* tell two sessions of one agent apart (loopctl#707).
|
|
8
|
+
*
|
|
9
|
+
* ## What happens when loopctl is slow, down, or refuses
|
|
10
|
+
*
|
|
11
|
+
* The holder is an IMPROVEMENT on the shared key, never a precondition for working: when the
|
|
12
|
+
* register fails for any reason (an older server answering 404, an agent-rooted tenant
|
|
13
|
+
* answering 403, a network error, the startup timeout), `agentKey()` keeps answering
|
|
14
|
+
* LOOPCTL_AGENT_KEY and the session works exactly as it did before. A heartbeat refused
|
|
15
|
+
* 401/404/409 means the credential or holder is gone (a laptop slept past the lease, the
|
|
16
|
+
* holder was ended): the next tick registers again, which loopctl answers with the SAME
|
|
17
|
+
* holder while it is live. A heartbeat refused 409 holder_lease_expired (US-52.19) ended the
|
|
18
|
+
* holder and released its claims, so the register makes a NEW holder, and the lapse is
|
|
19
|
+
* logged to stderr. A network error leaves the credential in place for the next tick. A
|
|
20
|
+
* register that fails TRANSIENTLY (no answer, a timeout, 429 or 5xx, at startup or after a
|
|
21
|
+
* refused heartbeat) arms the timer anyway, so the session registers once loopctl answers
|
|
22
|
+
* (US-52.13's partition answer). A refusal (any other 4xx: an older server's 404, 403
|
|
23
|
+
* custody_tier_required, 403 holder_mint_forbidden, 409 session_id_held) is final for the
|
|
24
|
+
* process: it stays on LOOPCTL_AGENT_KEY and never asks again.
|
|
25
|
+
*
|
|
26
|
+
* An end is final for every call already in flight: each register and heartbeat remembers
|
|
27
|
+
* the generation it started in, and one that lands after an end adopts nothing, so a
|
|
28
|
+
* heartbeat refused because the end revoked its credential never registers the session
|
|
29
|
+
* back to life. An explicit register after the end starts a new generation and is honoured.
|
|
30
|
+
*
|
|
31
|
+
* ## The session id
|
|
32
|
+
*
|
|
33
|
+
* The claim path's discriminator (`envClaimSessionId`, `lib/claim-session.js`), which prefers
|
|
34
|
+
* CLAUDE_CODE_SESSION_ID because CLAUDE_SESSION_ID is inherited by a headless subsession: two
|
|
35
|
+
* concurrent sessions reading one value would share a holder, the wrong merge #779 exists to
|
|
36
|
+
* prevent. A uuid minted once per process stands in when neither is set.
|
|
37
|
+
*
|
|
38
|
+
* The raw credential lives only in this process's memory and is never returned by a tool.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import { envClaimSessionId } from "./claim-session.js";
|
|
42
|
+
|
|
43
|
+
export const HOLDERS_PATH = "/api/v1/holders";
|
|
44
|
+
|
|
45
|
+
/** @param {string} id */
|
|
46
|
+
export function heartbeatPath(id) {
|
|
47
|
+
return `${HOLDERS_PATH}/${encodeURIComponent(id)}/heartbeat`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {string} id */
|
|
51
|
+
export function endPath(id) {
|
|
52
|
+
return `${HOLDERS_PATH}/${encodeURIComponent(id)}/end`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const GONE = new Set([401, 404, 409]);
|
|
56
|
+
|
|
57
|
+
// The retry interval before loopctl has named one: the server's own heartbeat interval
|
|
58
|
+
// (HOLDER_HEARTBEAT_SECONDS' default), so an unregistered session retries no faster than a
|
|
59
|
+
// registered one beats.
|
|
60
|
+
const DEFAULT_HEARTBEAT_SECONDS = 60;
|
|
61
|
+
|
|
62
|
+
/** A failed call worth retrying: no answer at all, rate limited, or a server error. */
|
|
63
|
+
function transient(result) {
|
|
64
|
+
const status = result && result.status;
|
|
65
|
+
return !result || !status || status === 429 || status >= 500;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The `error.code` of a refusal body, parsed or raw. */
|
|
69
|
+
function errorCode(body) {
|
|
70
|
+
let parsed = body;
|
|
71
|
+
if (typeof body === "string") {
|
|
72
|
+
try {
|
|
73
|
+
parsed = JSON.parse(body);
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return (parsed && parsed.error && parsed.error.code) || null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {{
|
|
83
|
+
* apiCall: Function,
|
|
84
|
+
* env: Record<string, string|undefined>,
|
|
85
|
+
* hostname: () => string,
|
|
86
|
+
* worktree: () => string,
|
|
87
|
+
* randomUUID: () => string,
|
|
88
|
+
* setInterval?: Function,
|
|
89
|
+
* clearInterval?: Function,
|
|
90
|
+
* }} deps
|
|
91
|
+
*/
|
|
92
|
+
export function createHolderSession(deps) {
|
|
93
|
+
const {
|
|
94
|
+
apiCall,
|
|
95
|
+
env,
|
|
96
|
+
hostname,
|
|
97
|
+
worktree,
|
|
98
|
+
randomUUID,
|
|
99
|
+
setInterval: setTimer = globalThis.setInterval,
|
|
100
|
+
clearInterval: clearTimer = globalThis.clearInterval,
|
|
101
|
+
// US-52.18: run after every register or heartbeat that leaves a live credential (the
|
|
102
|
+
// branch open, lib/branch-item.js). Its failure never fails the heartbeat.
|
|
103
|
+
afterBeat = null,
|
|
104
|
+
// US-52.19: where a lapse is reported. stderr, never stdout: stdout is the MCP stream.
|
|
105
|
+
log = (line) => process.stderr.write(`${line}\n`),
|
|
106
|
+
} = deps;
|
|
107
|
+
|
|
108
|
+
async function beat(result) {
|
|
109
|
+
if (afterBeat && holder && credential && result && !result.error) {
|
|
110
|
+
try {
|
|
111
|
+
await afterBeat(holder, credential);
|
|
112
|
+
} catch {
|
|
113
|
+
// The next heartbeat tries again.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const sessionId = envClaimSessionId(env) || randomUUID();
|
|
120
|
+
let holder = null;
|
|
121
|
+
let credential = null;
|
|
122
|
+
let timer = null;
|
|
123
|
+
let ended = false;
|
|
124
|
+
// Bumped by end(): a call started before it adopts nothing that lands after it.
|
|
125
|
+
let generation = 0;
|
|
126
|
+
|
|
127
|
+
function plainKey() {
|
|
128
|
+
return env.LOOPCTL_AGENT_KEY;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The key every agent-key request is sent with. */
|
|
132
|
+
function agentKey() {
|
|
133
|
+
return credential || plainKey();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function stopTimer() {
|
|
137
|
+
if (timer !== null) {
|
|
138
|
+
clearTimer(timer);
|
|
139
|
+
timer = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function arm(seconds) {
|
|
144
|
+
stopTimer();
|
|
145
|
+
const ms = Math.max(1, Number(seconds) || 60) * 1000;
|
|
146
|
+
timer = setTimer(() => {
|
|
147
|
+
heartbeat().catch(() => {});
|
|
148
|
+
}, ms);
|
|
149
|
+
if (timer && typeof timer.unref === "function") timer.unref();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function register() {
|
|
153
|
+
const key = plainKey();
|
|
154
|
+
if (!key) return { error: true, status: 0, body: "LOOPCTL_AGENT_KEY is not set" };
|
|
155
|
+
|
|
156
|
+
const gen = generation;
|
|
157
|
+
const result = await apiCall(
|
|
158
|
+
"POST",
|
|
159
|
+
HOLDERS_PATH,
|
|
160
|
+
{ kind: "interactive", session_id: sessionId, host: hostname(), worktree: worktree() },
|
|
161
|
+
key,
|
|
162
|
+
{ exactKey: true },
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
if (gen !== generation) return result;
|
|
166
|
+
|
|
167
|
+
if (result && !result.error && result.holder) {
|
|
168
|
+
holder = result.holder;
|
|
169
|
+
// `credential: null` means the key we sent is already this holder's; keep what we hold.
|
|
170
|
+
if (result.credential && result.credential.raw_key) credential = result.credential.raw_key;
|
|
171
|
+
ended = false;
|
|
172
|
+
arm(result.heartbeat_seconds);
|
|
173
|
+
} else if (transient(result)) {
|
|
174
|
+
// Not registered for a reason that passes: keep ticking, the next tick registers again.
|
|
175
|
+
if (timer === null) arm(DEFAULT_HEARTBEAT_SECONDS);
|
|
176
|
+
} else {
|
|
177
|
+
// Refused: a timer left from an earlier holder would ask again every tick.
|
|
178
|
+
stopTimer();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return beat(result);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function heartbeat() {
|
|
185
|
+
if (!holder || !credential) return register();
|
|
186
|
+
|
|
187
|
+
const gen = generation;
|
|
188
|
+
const result = await apiCall("POST", heartbeatPath(holder.id), null, credential, {
|
|
189
|
+
exactKey: true,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
if (gen !== generation) return result;
|
|
193
|
+
|
|
194
|
+
if (result && result.error && GONE.has(result.status)) {
|
|
195
|
+
// US-52.19: a heartbeat after the lease ran out ended the holder and released its
|
|
196
|
+
// claims, so this session comes back as a NEW holder and must re-claim what it held.
|
|
197
|
+
if (errorCode(result.body) === "holder_lease_expired") {
|
|
198
|
+
log(
|
|
199
|
+
`loopctl: holder ${holder.id} lapsed (its lease ran out before this heartbeat); ` +
|
|
200
|
+
"its claims were released. Registering a new holder.",
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
credential = null;
|
|
204
|
+
holder = null;
|
|
205
|
+
return register();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (result && !result.error && result.lease_until) {
|
|
209
|
+
holder = { ...holder, lease_until: result.lease_until };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return beat(result);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function end() {
|
|
216
|
+
stopTimer();
|
|
217
|
+
generation += 1;
|
|
218
|
+
if (!holder || !credential || ended) return { ended: true, holder };
|
|
219
|
+
|
|
220
|
+
const id = holder.id;
|
|
221
|
+
const key = credential;
|
|
222
|
+
ended = true;
|
|
223
|
+
credential = null;
|
|
224
|
+
|
|
225
|
+
const result = await apiCall("POST", endPath(id), null, key, { exactKey: true });
|
|
226
|
+
if (result && !result.error && result.holder) holder = result.holder;
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Registers at startup, waiting at most `timeoutMs` so a slow loopctl never blocks startup. */
|
|
231
|
+
async function start({ timeoutMs = 5000 } = {}) {
|
|
232
|
+
let timeout;
|
|
233
|
+
const cap = new Promise((resolve) => {
|
|
234
|
+
timeout = setTimeout(() => resolve({ error: true, status: 0, body: "register timed out" }), timeoutMs);
|
|
235
|
+
if (typeof timeout.unref === "function") timeout.unref();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
try {
|
|
239
|
+
return await Promise.race([register().catch((err) => ({ error: true, body: err.message })), cap]);
|
|
240
|
+
} finally {
|
|
241
|
+
clearTimeout(timeout);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** What a tool may show: never the raw credential. */
|
|
246
|
+
function status() {
|
|
247
|
+
return {
|
|
248
|
+
session_id: sessionId,
|
|
249
|
+
holder,
|
|
250
|
+
using_holder_credential: Boolean(credential),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return { agentKey, register, heartbeat, end, start, status, sessionId };
|
|
255
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `work_item_note` / `work_item_notes` (loopctl US-52.17): a work item's notes, the holder's
|
|
3
|
+
* plan, progress, decisions and open questions, filed by section beside the story —
|
|
4
|
+
* `POST` / `GET /api/v1/stories/:id/notes`.
|
|
5
|
+
*
|
|
6
|
+
* KEY: the session's own key (`agentKey()` in index.js — the holder credential once the
|
|
7
|
+
* startup register succeeded, else LOOPCTL_AGENT_KEY). The write is accepted from the story's
|
|
8
|
+
* CURRENT holder, so it must travel on the key that holds it: another session of the same
|
|
9
|
+
* agent is refused 409 held_by_other_holder, like every claimant verb (US-52.14). An
|
|
10
|
+
* orchestrator+ key may also write any item's note through the API; this tool does not send
|
|
11
|
+
* one, because the session writing progress is the session holding the work.
|
|
12
|
+
*
|
|
13
|
+
* Section and body are checked here only for shape. The server owns the bounds
|
|
14
|
+
* (`Loopctl.WorkItems.Note`: four sections, 1 to 16384 bytes, no credential-shaped body) and
|
|
15
|
+
* answers 422 with the reason, so the two can never disagree.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { uuid } from "./delivery-loop.js";
|
|
19
|
+
|
|
20
|
+
export const NOTE_SECTIONS = ["plan", "progress", "decision", "question"];
|
|
21
|
+
|
|
22
|
+
function refuse(body) {
|
|
23
|
+
return { error: true, status: 0, body };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function notesPath(storyId) {
|
|
27
|
+
return `/api/v1/stories/${encodeURIComponent(storyId)}/notes`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `POST /api/v1/stories/:id/notes`: append one note. */
|
|
31
|
+
export async function writeWorkItemNote(
|
|
32
|
+
{ story_id, section, body, idempotency_key } = {},
|
|
33
|
+
{ apiCall } = {},
|
|
34
|
+
) {
|
|
35
|
+
const bad = uuid(story_id, "story_id");
|
|
36
|
+
if (bad) return bad;
|
|
37
|
+
|
|
38
|
+
if (!NOTE_SECTIONS.includes(section)) {
|
|
39
|
+
return refuse(`\`section\` is one of: ${NOTE_SECTIONS.join(", ")}.`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof body !== "string" || body.trim() === "") {
|
|
43
|
+
return refuse("`body` is required: the note's text.");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (idempotency_key !== undefined && idempotency_key !== null && typeof idempotency_key !== "string") {
|
|
47
|
+
return refuse("`idempotency_key` must be a string.");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const payload = { section, body };
|
|
51
|
+
if (typeof idempotency_key === "string" && idempotency_key !== "") {
|
|
52
|
+
payload.idempotency_key = idempotency_key;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return apiCall("POST", notesPath(story_id), payload);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `GET /api/v1/stories/:id/notes`: one page, oldest first, `current` on the first page. */
|
|
59
|
+
export async function listWorkItemNotes({ story_id, section, limit, after } = {}, { apiCall } = {}) {
|
|
60
|
+
const bad = uuid(story_id, "story_id");
|
|
61
|
+
if (bad) return bad;
|
|
62
|
+
|
|
63
|
+
if (section !== undefined && section !== null && !NOTE_SECTIONS.includes(section)) {
|
|
64
|
+
return refuse(`\`section\` is one of: ${NOTE_SECTIONS.join(", ")}.`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (limit !== undefined && limit !== null && !(Number.isInteger(limit) && limit > 0)) {
|
|
68
|
+
return refuse("`limit` must be a positive integer (at most 200).");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (after !== undefined && after !== null && typeof after !== "string") {
|
|
72
|
+
return refuse("`after` must be the string a previous page returned as next_cursor.");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const query = new URLSearchParams();
|
|
76
|
+
if (typeof section === "string") query.set("section", section);
|
|
77
|
+
if (Number.isInteger(limit)) query.set("limit", String(limit));
|
|
78
|
+
if (typeof after === "string" && after !== "") query.set("after", after);
|
|
79
|
+
|
|
80
|
+
const qs = query.toString();
|
|
81
|
+
return apiCall("GET", notesPath(story_id) + (qs ? `?${qs}` : ""), null);
|
|
82
|
+
}
|