loopctl-mcp-server 3.32.1 → 3.34.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/README.md +15 -12
- package/index.js +527 -474
- package/lib/checkpoints.js +138 -0
- package/lib/escalation-playbook.js +0 -1
- package/lib/factory-policy.js +17 -0
- package/lib/holder-key-file.js +1 -1
- package/lib/holder.js +4 -4
- package/lib/{claim-session.js → lock-session.js} +28 -32
- package/lib/messages.js +12 -12
- package/lib/review-rounds.js +107 -0
- package/lib/work-item-move.js +61 -0
- package/lib/work-item-pull.js +286 -0
- package/lib/worktree-diff.js +120 -0
- package/package.json +1 -1
- package/lib/handoff.js +0 -427
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `work_item_checkpoint` / `work_item_checkpoints` (loopctl US-52.41): a holder records on its
|
|
3
|
+
* work item the branch, the head it pushed and its uncommitted diff, and anyone reads them —
|
|
4
|
+
* `POST`, `GET /api/v1/stories/:id/checkpoints` and `GET .../checkpoints/:checkpoint_id`.
|
|
5
|
+
*
|
|
6
|
+
* The diff is computed HERE from the worktree the caller names (lib/worktree-diff.js), so a
|
|
7
|
+
* session never pastes a diff into a tool call. It travels base64 in the JSON body.
|
|
8
|
+
*
|
|
9
|
+
* KEY: the session's own key (`agentKey()` in index.js, its holder credential). The server
|
|
10
|
+
* accepts the write only from the story's CURRENT holder at the story's claim_epoch with its
|
|
11
|
+
* lease live (exact_role agent), so it must travel on the credential that holds the item.
|
|
12
|
+
* When the caller passes no `claim_epoch`, the tool reads the story's: the epoch fences a
|
|
13
|
+
* stale sender, and the holder check still decides.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { uuid } from "./delivery-loop.js";
|
|
17
|
+
import { headCommit, worktreeDiff } from "./worktree-diff.js";
|
|
18
|
+
|
|
19
|
+
// A checkpoint body can be several megabytes; give the upload more than the default.
|
|
20
|
+
const UPLOAD_TIMEOUT_MS = 120_000;
|
|
21
|
+
|
|
22
|
+
function refuse(body) {
|
|
23
|
+
return { error: true, status: 0, body };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function checkpointsPath(storyId) {
|
|
27
|
+
return `/api/v1/stories/${encodeURIComponent(storyId)}/checkpoints`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function currentEpoch(storyId, apiCall) {
|
|
31
|
+
const res = await apiCall("GET", `/api/v1/stories/${encodeURIComponent(storyId)}`, null);
|
|
32
|
+
if (res?.error) return { error: res };
|
|
33
|
+
const epoch = res?.story?.claim_epoch;
|
|
34
|
+
return Number.isInteger(epoch) ? { epoch } : { error: refuse("The story carries no claim_epoch.") };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `POST /api/v1/stories/:id/checkpoints`, with the diff computed from `worktree`. */
|
|
38
|
+
export async function recordCheckpoint(
|
|
39
|
+
{ story_id, worktree, done, next, wip_branch, claim_epoch, checkpoint_id } = {},
|
|
40
|
+
{ apiCall, randomUUID, diffOf = worktreeDiff, headOf = headCommit } = {},
|
|
41
|
+
) {
|
|
42
|
+
const bad = uuid(story_id, "story_id");
|
|
43
|
+
if (bad) return bad;
|
|
44
|
+
|
|
45
|
+
if (typeof worktree !== "string" || worktree.trim() === "") {
|
|
46
|
+
return refuse("`worktree` is required: the path of the git worktree to checkpoint.");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof done !== "string" || done.trim() === "") {
|
|
50
|
+
return refuse("`done` is required: what this session did since the last checkpoint.");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (checkpoint_id !== undefined && checkpoint_id !== null) {
|
|
54
|
+
const badId = uuid(checkpoint_id, "checkpoint_id");
|
|
55
|
+
if (badId) return badId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (claim_epoch !== undefined && claim_epoch !== null && !Number.isInteger(claim_epoch)) {
|
|
59
|
+
return refuse("`claim_epoch` must be an integer: the epoch the claim returned.");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let state;
|
|
63
|
+
try {
|
|
64
|
+
state = typeof wip_branch === "string" && wip_branch !== "" ? await headOf(worktree) : await diffOf(worktree);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return refuse(err.message);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let epoch = claim_epoch;
|
|
70
|
+
if (!Number.isInteger(epoch)) {
|
|
71
|
+
const read = await currentEpoch(story_id, apiCall);
|
|
72
|
+
if (read.error) return read.error;
|
|
73
|
+
epoch = read.epoch;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const payload = {
|
|
77
|
+
id: checkpoint_id || randomUUID(),
|
|
78
|
+
claim_epoch: epoch,
|
|
79
|
+
branch: state.branch,
|
|
80
|
+
head_sha: state.head_sha,
|
|
81
|
+
done,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (typeof next === "string" && next !== "") payload.next = next;
|
|
85
|
+
|
|
86
|
+
if (typeof wip_branch === "string" && wip_branch !== "") {
|
|
87
|
+
payload.wip_branch = wip_branch;
|
|
88
|
+
} else {
|
|
89
|
+
payload.diff = state.diff.toString("base64");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return apiCall("POST", checkpointsPath(story_id), payload, { timeoutMs: UPLOAD_TIMEOUT_MS });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The newest checkpoints (metadata), or with `fenced: "unresolved"` the fenced ones nobody
|
|
97
|
+
* resolved yet (US-52.42), or one with its diff when `checkpoint_id` is given.
|
|
98
|
+
*/
|
|
99
|
+
export async function readCheckpoints({ story_id, checkpoint_id, fenced } = {}, { apiCall } = {}) {
|
|
100
|
+
const bad = uuid(story_id, "story_id");
|
|
101
|
+
if (bad) return bad;
|
|
102
|
+
|
|
103
|
+
if (fenced !== undefined && fenced !== null && fenced !== "unresolved") {
|
|
104
|
+
return refuse("`fenced` is \"unresolved\" or absent.");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (checkpoint_id === undefined || checkpoint_id === null || checkpoint_id === "") {
|
|
108
|
+
const query = fenced === "unresolved" ? "?fenced=unresolved" : "";
|
|
109
|
+
return apiCall("GET", `${checkpointsPath(story_id)}${query}`, null);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const badId = uuid(checkpoint_id, "checkpoint_id");
|
|
113
|
+
if (badId) return badId;
|
|
114
|
+
|
|
115
|
+
return apiCall("GET", `${checkpointsPath(story_id)}/${encodeURIComponent(checkpoint_id)}`, null);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const RESOLUTIONS = ["applied", "ignored"];
|
|
119
|
+
|
|
120
|
+
/** `POST .../checkpoints/:checkpoint_id/resolve`: the current holder applies or ignores one. */
|
|
121
|
+
export async function resolveCheckpoint(
|
|
122
|
+
{ story_id, checkpoint_id, resolution, reason } = {},
|
|
123
|
+
{ apiCall } = {},
|
|
124
|
+
) {
|
|
125
|
+
const bad = uuid(story_id, "story_id") || uuid(checkpoint_id, "checkpoint_id");
|
|
126
|
+
if (bad) return bad;
|
|
127
|
+
|
|
128
|
+
if (!RESOLUTIONS.includes(resolution)) {
|
|
129
|
+
return refuse("`resolution` is \"applied\" or \"ignored\".");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (typeof reason !== "string" || reason.trim() === "") {
|
|
133
|
+
return refuse("`reason` is required: why you applied or ignored it.");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const path = `${checkpointsPath(story_id)}/${encodeURIComponent(checkpoint_id)}/resolve`;
|
|
137
|
+
return apiCall("POST", path, { resolution, reason });
|
|
138
|
+
}
|
|
@@ -33,7 +33,6 @@ export const TAKE_OVER_STEPS = Object.freeze([
|
|
|
33
33
|
"If the row's run.work_left_behind is \"true\", the runner left unpushed commits on run.branch. runner_list gives the runner's host; the commits are in <repo>/.claude/worktrees/loop-story-<story_id> there. Push that branch or cherry-pick from it rather than starting over.",
|
|
34
34
|
"Do the work through the normal delivery loop in the story's repo: branch from the repo's base branch (the intake source's base_branch in intake_source_list, the trunk the runner used), tests, the review round, a PR whose body says \"Fixes #<issue>\" so GitHub closes the issue, merge on green.",
|
|
35
35
|
"Close the story: resolve_escalation {story_id, to: \"done\", reason: \"fixed by <owner/repo>#<PR>\"}. That needs LOOPCTL_USER_KEY, an operator key no dispatch minted; an interactive fleet session holds one, so the session that took the story over resolves it itself, never Mark. If the work turns out not to be wanted, use to: \"failed\" instead.",
|
|
36
|
-
"Finish the escalation handoff loopctl posted for the story on its repo channel (handoff:<repo>:escalation-<story_id>-g<N>): claim it before you start (step 1 of its body). The resolve marks that claim done itself, so do not channel_done it afterwards (that answers 404); channel_done it only if you stop without resolving, and loopctl then posts a new generation while the story is still escalated.",
|
|
37
36
|
]);
|
|
38
37
|
|
|
39
38
|
const ALREADY_MERGED =
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `factory_policy` (loopctl US-52.51): `GET /api/v1/factory/policy`, every delivery-loop rule
|
|
3
|
+
* value (review-round ceilings, open PRs per session, epics per repo, PR-body sections, review
|
|
4
|
+
* levels, the epic-merge tip age, protected branches) as loopctl compiled it from
|
|
5
|
+
* `factory/policy.toml`. The same for every tenant; `version` is the sha256 of that file.
|
|
6
|
+
*
|
|
7
|
+
* Any authenticated key reads it, and it takes no arguments, so there is nothing to refuse
|
|
8
|
+
* locally.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function policyPath() {
|
|
12
|
+
return `/api/v1/factory/policy`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function readFactoryPolicy(_args = {}, { apiCall } = {}) {
|
|
16
|
+
return apiCall("GET", policyPath(), null);
|
|
17
|
+
}
|
package/lib/holder-key-file.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* ## The file name is the holder's session id
|
|
9
9
|
*
|
|
10
|
-
* It is the session id the holder registered with (`
|
|
10
|
+
* It is the session id the holder registered with (`envLockSessionId`,
|
|
11
11
|
* CLAUDE_CODE_SESSION_ID first), never CLAUDE_SESSION_ID read on its own: a headless
|
|
12
12
|
* subsession inherits its parent's CLAUDE_SESSION_ID, so keying on that would let the
|
|
13
13
|
* subsession's MCP process overwrite the parent's file with its own credential and delete it
|
package/lib/holder.js
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
*
|
|
31
31
|
* ## The session id
|
|
32
32
|
*
|
|
33
|
-
* The
|
|
33
|
+
* The lock path's discriminator (`envLockSessionId`, `lib/lock-session.js`), which prefers
|
|
34
34
|
* CLAUDE_CODE_SESSION_ID because CLAUDE_SESSION_ID is inherited by a headless subsession: two
|
|
35
35
|
* concurrent sessions reading one value would share a holder, the wrong merge #779 exists to
|
|
36
36
|
* prevent. A uuid minted once per process stands in when neither is set.
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
* session stays on LOOPCTL_AGENT_KEY, so the branch open (lib/branch-item.js) never runs.
|
|
50
50
|
*/
|
|
51
51
|
|
|
52
|
-
import {
|
|
52
|
+
import { envLockSessionId } from "./lock-session.js";
|
|
53
53
|
import { createHolderKeyFile, holderKeyDir } from "./holder-key-file.js";
|
|
54
54
|
|
|
55
55
|
function present(value) {
|
|
@@ -135,7 +135,7 @@ export function createHolderSession(deps) {
|
|
|
135
135
|
return result;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
const sessionId =
|
|
138
|
+
const sessionId = envLockSessionId(env) || randomUUID();
|
|
139
139
|
let holder = null;
|
|
140
140
|
let credential = null;
|
|
141
141
|
let timer = null;
|
|
@@ -145,7 +145,7 @@ export function createHolderSession(deps) {
|
|
|
145
145
|
|
|
146
146
|
// US-52.28: named by the session's own marker only. A generated uuid names a file no hook
|
|
147
147
|
// can find, so a process without a marker gets a no-op file.
|
|
148
|
-
const keyFile = createHolderKeyFile({ dir: keyFileDir, sessionId:
|
|
148
|
+
const keyFile = createHolderKeyFile({ dir: keyFileDir, sessionId: envLockSessionId(env) });
|
|
149
149
|
// The key this process last wrote, so a remove deletes only its own file.
|
|
150
150
|
let writtenKey = null;
|
|
151
151
|
|
|
@@ -1,30 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The SESSION discriminator used by the
|
|
2
|
+
* The SESSION discriminator used by the advisory file-lock paths (issue #779).
|
|
3
3
|
*
|
|
4
4
|
* Extracted into its own module for the same reason `witness-sth.js` was: this is a
|
|
5
5
|
* predictable-temp-file consumer, its guards are security guards (CWE-59), and guards
|
|
6
6
|
* that live inline in `index.js` cannot be tested against an injected fs.
|
|
7
7
|
*
|
|
8
|
-
* ## Why the
|
|
8
|
+
* ## Why the lock paths need a different id from the post path
|
|
9
9
|
*
|
|
10
10
|
* `channel_post` stamps a session id once and nothing compares it again, so a
|
|
11
|
-
* process-lifetime uuid is fine there (US-454 pins it).
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* work into a `409 claim_session_mismatch` (or a byte-identical `404` on unlock),
|
|
17
|
-
* recoverable only by discovering `force: true`.
|
|
11
|
+
* process-lifetime uuid is fine there (US-454 pins it). An advisory file LOCK is compared
|
|
12
|
+
* again: the server resolves both the in-place refresh and the release by the
|
|
13
|
+
* `(tenant, project, agent, session, key)` slot. With a process-lifetime id, every npx
|
|
14
|
+
* respawn or `/mcp` reconnect turns the session's OWN live lock into a byte-identical
|
|
15
|
+
* `404` on unlock and a SECOND row on refresh.
|
|
18
16
|
*
|
|
19
17
|
* ## The two failure directions are NOT symmetric
|
|
20
18
|
*
|
|
21
|
-
* A WRONG SPLIT — this session reading its own live
|
|
22
|
-
*
|
|
23
|
-
* value — lets a peer's `
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* key for the sake of stability.
|
|
19
|
+
* A WRONG SPLIT — this session reading its own live lock as a peer's — strands a stale
|
|
20
|
+
* hint until the lock's short TTL ends. A WRONG MERGE — two concurrent sessions stamping
|
|
21
|
+
* one value — lets a peer's `channel_unlock` release a lock it does not hold, with an
|
|
22
|
+
* audit row byte-identical to the owner releasing its own. That is the shape of KB
|
|
23
|
+
* `07f5e839`'s incident. So: prefer the most SPECIFIC session marker available, and never
|
|
24
|
+
* widen the key for the sake of stability.
|
|
28
25
|
*/
|
|
29
26
|
|
|
30
27
|
/** A minted id is always a v4 uuid; anything else in the file is foreign. */
|
|
@@ -42,12 +39,12 @@ const UUID_RE =
|
|
|
42
39
|
* subsession (a `bin/review-worktree.sh` session and the session that launched it both
|
|
43
40
|
* read one value, with their own `CLAUDE_CODE_SESSION_ID`s). Preferring the inherited one
|
|
44
41
|
* is exactly the WRONG MERGE above. Both live in the environment, so a respawned proxy
|
|
45
|
-
* under the same session re-reads the same value — the property the
|
|
42
|
+
* under the same session re-reads the same value — the property the lock path needs.
|
|
46
43
|
*
|
|
47
44
|
* @param {Record<string, string|undefined>} env
|
|
48
45
|
* @returns {string|null}
|
|
49
46
|
*/
|
|
50
|
-
export function
|
|
47
|
+
export function envLockSessionId(env) {
|
|
51
48
|
for (const name of ["CLAUDE_CODE_SESSION_ID", "CLAUDE_SESSION_ID"]) {
|
|
52
49
|
const value = (env[name] || "").trim();
|
|
53
50
|
if (value) return value;
|
|
@@ -63,7 +60,7 @@ export function envClaimSessionId(env) {
|
|
|
63
60
|
* @param {{ tmpdir: string, hostname: string, cwd: string, createHash: Function, join: Function }} deps
|
|
64
61
|
* @returns {string}
|
|
65
62
|
*/
|
|
66
|
-
export function
|
|
63
|
+
export function durableLockSessionPath({
|
|
67
64
|
tmpdir,
|
|
68
65
|
hostname,
|
|
69
66
|
cwd,
|
|
@@ -74,7 +71,7 @@ export function durableClaimSessionPath({
|
|
|
74
71
|
.update(`${hostname}\n${cwd}`)
|
|
75
72
|
.digest("hex")
|
|
76
73
|
.slice(0, 32);
|
|
77
|
-
return join(tmpdir, `loopctl-mcp-
|
|
74
|
+
return join(tmpdir, `loopctl-mcp-lock-session-${key}.id`);
|
|
78
75
|
}
|
|
79
76
|
|
|
80
77
|
/**
|
|
@@ -85,19 +82,18 @@ export function durableClaimSessionPath({
|
|
|
85
82
|
* user on a shared host can pre-plant a file or a symlink there. `lstat` first and refuse
|
|
86
83
|
* a symlink or a foreign uid — otherwise the read follows the link and the CONTENTS OF
|
|
87
84
|
* ANY FILE THIS USER CAN READ become the session id, which is then sent to the server,
|
|
88
|
-
* stored
|
|
89
|
-
* `GET /channel/claims`.
|
|
85
|
+
* stored on the lock row and echoed to every peer session by `GET /channel/locks`.
|
|
90
86
|
*
|
|
91
87
|
* SHAPE GUARD: only a v4 uuid is adopted. Without it, an attacker CHOOSES this proxy's
|
|
92
|
-
* discriminator, and any oversized or credential-shaped content wedges every
|
|
93
|
-
*
|
|
88
|
+
* discriminator, and any oversized or credential-shaped content wedges every lock and
|
|
89
|
+
* unlock from this directory behind a permanent 422 (the server-side cap, NUL and
|
|
94
90
|
* denylist rules) with nothing pointing at `/tmp`.
|
|
95
91
|
*
|
|
96
92
|
* @param {string} filePath
|
|
97
93
|
* @param {{ fs: { readFileSync: Function, lstatSync?: Function }, getuid?: () => number }} deps
|
|
98
94
|
* @returns {string|null}
|
|
99
95
|
*/
|
|
100
|
-
export function
|
|
96
|
+
export function readDurableLockSessionId(filePath, { fs, getuid }) {
|
|
101
97
|
try {
|
|
102
98
|
if (typeof fs.lstatSync === "function") {
|
|
103
99
|
const st = fs.lstatSync(filePath);
|
|
@@ -128,17 +124,17 @@ export function readDurableClaimSessionId(filePath, { fs, getuid }) {
|
|
|
128
124
|
* writes THROUGH a symlink at `filePath`, so a planted symlink is replaced rather than
|
|
129
125
|
* followed — no arbitrary-file clobber — and the torn-read race is gone. The re-read
|
|
130
126
|
* afterwards makes two proxies starting together CONVERGE on the winner's value instead
|
|
131
|
-
* of each keeping a divergent in-memory one, which would
|
|
127
|
+
* of each keeping a divergent in-memory one, which would 404 the loser's own unlocks.
|
|
132
128
|
*
|
|
133
129
|
* @param {string} filePath
|
|
134
130
|
* @param {{ fs: object, getuid?: () => number, randomUUID: () => string, randomBytes: (n: number) => {toString: Function}, pid?: number }} deps
|
|
135
131
|
* @returns {string}
|
|
136
132
|
*/
|
|
137
|
-
export function
|
|
133
|
+
export function durableLockSessionId(
|
|
138
134
|
filePath,
|
|
139
135
|
{ fs, getuid, randomUUID, randomBytes, pid = 0 },
|
|
140
136
|
) {
|
|
141
|
-
const existing =
|
|
137
|
+
const existing = readDurableLockSessionId(filePath, { fs, getuid });
|
|
142
138
|
if (existing) return existing;
|
|
143
139
|
|
|
144
140
|
const minted = randomUUID();
|
|
@@ -159,7 +155,7 @@ export function durableClaimSessionId(
|
|
|
159
155
|
return minted;
|
|
160
156
|
}
|
|
161
157
|
|
|
162
|
-
return
|
|
158
|
+
return readDurableLockSessionId(filePath, { fs, getuid }) || minted;
|
|
163
159
|
}
|
|
164
160
|
|
|
165
161
|
/**
|
|
@@ -168,9 +164,9 @@ export function durableClaimSessionId(
|
|
|
168
164
|
* @param {object} deps
|
|
169
165
|
* @returns {string}
|
|
170
166
|
*/
|
|
171
|
-
export function
|
|
167
|
+
export function resolveLockSessionId(deps) {
|
|
172
168
|
return (
|
|
173
|
-
|
|
174
|
-
|
|
169
|
+
envLockSessionId(deps.env) ||
|
|
170
|
+
durableLockSessionId(durableLockSessionPath(deps), deps)
|
|
175
171
|
);
|
|
176
172
|
}
|
package/lib/messages.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `message_send` (loopctl US-52.23): send one message to a work item's thread (`story_id`), a
|
|
3
3
|
* session's DMs (`to_holder_id`) or a repo channel (`project_id`) — `POST /api/v1/messages`.
|
|
4
|
-
* The
|
|
5
|
-
* free-text channel_post.
|
|
4
|
+
* The local refusal of a `channel_post` that retires nothing lives here too, because
|
|
5
|
+
* message_send is what replaces free-text channel_post.
|
|
6
6
|
*
|
|
7
7
|
* KEY: the session's own key (`agentKey()` in index.js — the holder credential once the startup
|
|
8
8
|
* register succeeded, else LOOPCTL_AGENT_KEY). The server stamps the author from it: agent_id
|
|
@@ -21,11 +21,12 @@ export const MESSAGE_TARGETS = ["story_id", "to_holder_id", "project_id"];
|
|
|
21
21
|
/** Transport retries after the first attempt, each with the same idempotency_key. */
|
|
22
22
|
export const MESSAGE_SEND_RETRIES = 2;
|
|
23
23
|
|
|
24
|
-
export const
|
|
25
|
-
"channel_post now
|
|
26
|
-
"
|
|
27
|
-
"posts the same message to the repo channel, and message_send also reaches a work
|
|
28
|
-
"thread (story_id) or a session's DMs (to_holder_id).
|
|
24
|
+
export const CHANNEL_POST_REFUSAL =
|
|
25
|
+
"channel_post now only RETIRES a post: pass supersedes (the stale post's id) with its " +
|
|
26
|
+
"replacement body. Free-text messages go through message_send: message_send {project_id, " +
|
|
27
|
+
"body} posts the same message to the repo channel, and message_send also reaches a work " +
|
|
28
|
+
"item's thread (story_id) or a session's DMs (to_holder_id). Handoffs are work items " +
|
|
29
|
+
"(work_item_open, work_item_move). Nothing was sent.";
|
|
29
30
|
|
|
30
31
|
function refuse(body) {
|
|
31
32
|
return { error: true, status: 0, body };
|
|
@@ -36,12 +37,11 @@ function present(value) {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
|
-
* The local refusal for a channel_post
|
|
40
|
-
* ahead.
|
|
41
|
-
* keyed_slot_retired with its own remedy.
|
|
40
|
+
* The local refusal for a channel_post that names no post to retire, or null when the call may
|
|
41
|
+
* go ahead (US-52.49: keyed handoff slots are gone, so a supersession is all channel_post does).
|
|
42
42
|
*/
|
|
43
|
-
export function
|
|
44
|
-
return present(
|
|
43
|
+
export function refuseChannelPost({ supersedes } = {}) {
|
|
44
|
+
return present(supersedes) ? null : refuse(CHANNEL_POST_REFUSAL);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/** True for the `{error, status: 0}` shape index.js's apiCall returns when no HTTP answer came. */
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Review rounds, counted on the work item (loopctl US-52.53).
|
|
3
|
+
*
|
|
4
|
+
* - `review_round_open`: `POST /api/v1/review_rounds` — count one round on the work item for
|
|
5
|
+
* `repo_url` and `branch`. 201 `{round, normal, ceiling, verdict, message}`; a repeated
|
|
6
|
+
* `round_id` is 200 with the first answer; a round past the ceiling is 409
|
|
7
|
+
* `review_ceiling_reached` and nothing is counted. `kind: "security"` records a
|
|
8
|
+
* /security-review at `head_sha` instead, never counted against the ceiling; `ancestor_shas`
|
|
9
|
+
* refuses a round whose branch carries another unmerged item's capped head (409
|
|
10
|
+
* `inherited_ceiling_reached`).
|
|
11
|
+
* - `review_round_void`: `POST /api/v1/review_rounds/:round_id/void` — that round did not
|
|
12
|
+
* review; it stops counting. 404 `review_round_not_found` for an unknown round.
|
|
13
|
+
* - `review_round_status`: `GET /api/v1/review_rounds?repo_url=&branch=` — the item's rounds
|
|
14
|
+
* and the counted total, its security records, `inherited` for `ancestor_shas`, and the
|
|
15
|
+
* project's capped items (`capped`) for the carry-over check. With `ancestor_shas` it is
|
|
16
|
+
* `POST /api/v1/review_rounds/status`, the same read with the list in a body.
|
|
17
|
+
*
|
|
18
|
+
* The writes need a holder credential (the round's actor is the key's holder, resolved
|
|
19
|
+
* server-side); the read takes any agent key. Nothing here is decided locally beyond the
|
|
20
|
+
* argument shape: the server owns the count and every refusal.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const OPEN_FIELDS = [
|
|
24
|
+
"repo_url",
|
|
25
|
+
"branch",
|
|
26
|
+
"round_id",
|
|
27
|
+
"kind",
|
|
28
|
+
"diff_kind",
|
|
29
|
+
"head_sha",
|
|
30
|
+
"ancestor_shas",
|
|
31
|
+
"attested",
|
|
32
|
+
"reason",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
// A local refusal has the shape every tool here answers one with (status 0, a sentence); the
|
|
36
|
+
// server owns every other bound and answers it with its own code.
|
|
37
|
+
function required(args, names) {
|
|
38
|
+
const missing = names.filter((n) => typeof args?.[n] !== "string" || args[n].trim() === "");
|
|
39
|
+
if (missing.length > 0) {
|
|
40
|
+
return { error: true, status: 0, body: `Required: ${missing.map((n) => `\`${n}\``).join(", ")}.` };
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function reviewRoundsPath() {
|
|
46
|
+
return "/api/v1/review_rounds";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function reviewRoundVoidPath(roundId) {
|
|
50
|
+
return `/api/v1/review_rounds/${encodeURIComponent(roundId)}/void`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The plain read: repo_url, branch and diff_kind only. ancestor_shas never goes in a URL. */
|
|
54
|
+
export function reviewRoundStatusPath({ repo_url, branch, diff_kind } = {}) {
|
|
55
|
+
const query = new URLSearchParams({ repo_url, branch });
|
|
56
|
+
if (typeof diff_kind === "string" && diff_kind !== "") query.set("diff_kind", diff_kind);
|
|
57
|
+
return `/api/v1/review_rounds?${query.toString()}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The same read with its arguments in a body, for ancestor_shas. */
|
|
61
|
+
export function reviewRoundStatusBodyPath() {
|
|
62
|
+
return "/api/v1/review_rounds/status";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// The ancestors as a list: an array as given, a comma-separated string split. The server checks
|
|
66
|
+
// each sha and the bound of 500.
|
|
67
|
+
function ancestorList(value) {
|
|
68
|
+
if (Array.isArray(value)) return value;
|
|
69
|
+
if (typeof value === "string") return value.split(",").map((s) => s.trim()).filter((s) => s !== "");
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function openReviewRound(args = {}, { apiCall } = {}) {
|
|
74
|
+
// A security record needs no diff kind; the server refuses one with no head.
|
|
75
|
+
const names = ["repo_url", "branch", "round_id"];
|
|
76
|
+
if (args?.kind !== "security") names.push("diff_kind");
|
|
77
|
+
const refused = required(args, names);
|
|
78
|
+
if (refused) return refused;
|
|
79
|
+
|
|
80
|
+
const body = {};
|
|
81
|
+
for (const field of OPEN_FIELDS) {
|
|
82
|
+
if (args[field] !== undefined && args[field] !== null) body[field] = args[field];
|
|
83
|
+
}
|
|
84
|
+
return apiCall("POST", reviewRoundsPath(), body);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function voidReviewRound(args = {}, { apiCall } = {}) {
|
|
88
|
+
const refused = required(args, ["round_id"]);
|
|
89
|
+
if (refused) return refused;
|
|
90
|
+
|
|
91
|
+
const body = typeof args.reason === "string" && args.reason !== "" ? { reason: args.reason } : {};
|
|
92
|
+
return apiCall("POST", reviewRoundVoidPath(args.round_id), body);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function readReviewRoundStatus(args = {}, { apiCall } = {}) {
|
|
96
|
+
const refused = required(args, ["repo_url", "branch"]);
|
|
97
|
+
if (refused) return refused;
|
|
98
|
+
|
|
99
|
+
// 500 ancestor shas are ~33 KB, past the server's 10,000-byte request line: they travel in a
|
|
100
|
+
// POST body, to the same read. Without them the read stays a GET.
|
|
101
|
+
const ancestors = ancestorList(args.ancestor_shas);
|
|
102
|
+
if (ancestors.length === 0) return apiCall("GET", reviewRoundStatusPath(args), null);
|
|
103
|
+
|
|
104
|
+
const body = { repo_url: args.repo_url, branch: args.branch, ancestor_shas: ancestors };
|
|
105
|
+
if (typeof args.diff_kind === "string" && args.diff_kind !== "") body.diff_kind = args.diff_kind;
|
|
106
|
+
return apiCall("POST", reviewRoundStatusBodyPath(), body);
|
|
107
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `work_item_move` (loopctl US-52.43): move a work item to a host, a holder, the cloud or back
|
|
3
|
+
* to the loop — `POST /api/v1/stories/:id/move`.
|
|
4
|
+
*
|
|
5
|
+
* KEY: the session's own key (`agentKey()` in index.js, its holder credential). The server
|
|
6
|
+
* decides the path from who calls: the item's holder releases it at once, anyone else
|
|
7
|
+
* requests the move of the holder, and an item nobody holds is released at once.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { uuid } from "./delivery-loop.js";
|
|
11
|
+
|
|
12
|
+
const KINDS = ["host", "holder", "loop", "cloud"];
|
|
13
|
+
|
|
14
|
+
function refuse(body) {
|
|
15
|
+
return { error: true, status: 0, body };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function target({ kind, host, holder_id } = {}) {
|
|
19
|
+
if (!KINDS.includes(kind)) return { error: "`target.kind` is host, holder, loop or cloud." };
|
|
20
|
+
if (kind === "host") {
|
|
21
|
+
return typeof host === "string" && host.trim() !== ""
|
|
22
|
+
? { value: { kind, host } }
|
|
23
|
+
: { error: "A host target names `target.host`, a runner's name." };
|
|
24
|
+
}
|
|
25
|
+
if (kind === "holder") {
|
|
26
|
+
return uuid(holder_id, "target.holder_id")
|
|
27
|
+
? { error: "A holder target names `target.holder_id`, a holder's UUID." }
|
|
28
|
+
: { value: { kind, holder_id } };
|
|
29
|
+
}
|
|
30
|
+
return { value: { kind } };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `POST /api/v1/stories/:id/move`. A missing `move_id` is generated, so a retry should pass one. */
|
|
34
|
+
export async function moveWorkItem(
|
|
35
|
+
{ story_id, move_id, target: wanted, reason, checkpoint_id } = {},
|
|
36
|
+
{ apiCall, randomUUID } = {},
|
|
37
|
+
) {
|
|
38
|
+
const bad = uuid(story_id, "story_id");
|
|
39
|
+
if (bad) return bad;
|
|
40
|
+
|
|
41
|
+
const id = move_id ?? randomUUID();
|
|
42
|
+
const badId = uuid(id, "move_id");
|
|
43
|
+
if (badId) return badId;
|
|
44
|
+
|
|
45
|
+
const resolved = target(wanted);
|
|
46
|
+
if (resolved.error) return refuse(resolved.error);
|
|
47
|
+
|
|
48
|
+
if (typeof reason !== "string" || reason.trim() === "") {
|
|
49
|
+
return refuse("`reason` is required: why the item moves.");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (checkpoint_id !== undefined && checkpoint_id !== null) {
|
|
53
|
+
const badCheckpoint = uuid(checkpoint_id, "checkpoint_id");
|
|
54
|
+
if (badCheckpoint) return badCheckpoint;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const body = { move_id: id, target: resolved.value, reason };
|
|
58
|
+
if (checkpoint_id) body.checkpoint_id = checkpoint_id;
|
|
59
|
+
|
|
60
|
+
return apiCall("POST", `/api/v1/stories/${encodeURIComponent(story_id)}/move`, body);
|
|
61
|
+
}
|