loopctl-mcp-server 3.22.1 → 3.32.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.
@@ -237,15 +237,20 @@ export async function storyStage({ story_id } = {}, { apiCall } = {}) {
237
237
  export const RESOLUTION_TARGETS = ["detected", "queued", "done", "failed"];
238
238
 
239
239
  /**
240
- * `POST /api/v1/stories/:id/stage/resolve`: move an escalated story off `escalated`, as a
241
- * human. `to` is one of `RESOLUTION_TARGETS`.
240
+ * `POST /api/v1/stories/:id/stage/resolve`: move an escalated story off `escalated`. `to` is
241
+ * one of `RESOLUTION_TARGETS`. A human (LOOPCTL_USER_KEY) may send it anywhere; a HOLDER
242
+ * (LOOPCTL_AGENT_KEY, US-52.31) may send it back to `queued` only, and never one it raised
243
+ * itself — loopctl refuses that `409 self_resolve_blocked`. `apiCall` is bound to whichever
244
+ * key the caller has, the user key first.
242
245
  */
243
- export async function resolveEscalation({ story_id, to, reason } = {}, { userKey, apiCall } = {}) {
244
- if (!userKey) {
246
+ export async function resolveEscalation(
247
+ { story_id, to, reason } = {},
248
+ { userKey, agentKey, apiCall } = {},
249
+ ) {
250
+ if (!userKey && !agentKey) {
245
251
  return refuse(
246
- "LOOPCTL_USER_KEY is required: only a human principal — a user key no dispatch " +
247
- "minted — may resolve an escalation. That is the separation that stops a session " +
248
- "clearing the escalation it raised.",
252
+ "LOOPCTL_USER_KEY or LOOPCTL_AGENT_KEY is required: a human may resolve an escalation " +
253
+ "to any target, and a holder credential may send one it did not raise back to queued.",
249
254
  );
250
255
  }
251
256
 
package/lib/factory.js ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * `factory_overview` (loopctl US-52.30): `GET /api/v1/factory/overview`, per project and for
3
+ * the whole fleet, what is in flight, waiting (with its oldest age), delivered and failing.
4
+ * Every count is read from loopctl's stage machine at request time; drill into one item with
5
+ * `work_item_timeline`. READ-ONLY on any authenticated key.
6
+ *
7
+ * `window_days` is checked here for shape and range so an obvious mistake costs no round trip;
8
+ * the server owns the bound and answers 400 naming it.
9
+ */
10
+
11
+ import { uuid } from "./delivery-loop.js";
12
+
13
+ export function overviewPath() {
14
+ return `/api/v1/factory/overview`;
15
+ }
16
+
17
+ function refuse(body) {
18
+ return { error: true, status: 0, body };
19
+ }
20
+
21
+ export async function readFactoryOverview({ project_id, window_days } = {}, { apiCall } = {}) {
22
+ if (project_id !== undefined && project_id !== null) {
23
+ const bad = uuid(project_id, "project_id");
24
+ if (bad) return bad;
25
+ }
26
+
27
+ if (
28
+ window_days !== undefined &&
29
+ window_days !== null &&
30
+ !(Number.isInteger(window_days) && window_days >= 1 && window_days <= 30)
31
+ ) {
32
+ return refuse("`window_days` must be an integer from 1 to 30.");
33
+ }
34
+
35
+ const query = new URLSearchParams();
36
+ if (typeof project_id === "string") query.set("project_id", project_id);
37
+ if (Number.isInteger(window_days)) query.set("window_days", String(window_days));
38
+
39
+ const qs = query.toString();
40
+ return apiCall("GET", overviewPath() + (qs ? `?${qs}` : ""), null);
41
+ }
42
+
43
+ /**
44
+ * `intake_pause` / `intake_resume` (loopctl US-52.33): `POST /api/v1/projects/:id/intake/pause`
45
+ * and `/resume` on the project's one live intake source. A pause stops PROMOTION and TRIAGE
46
+ * only: the webhook keeps recording, and work past detected carries on. Each call carries an
47
+ * `intervention_id`, generated when absent; pass the first one again to retry after a lost
48
+ * response.
49
+ */
50
+ export function intakePausePath(projectId) {
51
+ return `/api/v1/projects/${encodeURIComponent(projectId)}/intake/pause`;
52
+ }
53
+
54
+ export function intakeResumePath(projectId) {
55
+ return `/api/v1/projects/${encodeURIComponent(projectId)}/intake/resume`;
56
+ }
57
+
58
+ function interventionId(intervention_id, randomUUID) {
59
+ if (intervention_id === undefined || intervention_id === null || intervention_id === "") {
60
+ return { id: randomUUID() };
61
+ }
62
+ const bad = uuid(intervention_id, "intervention_id");
63
+ return bad ? { bad } : { id: intervention_id };
64
+ }
65
+
66
+ export async function pauseIntake(
67
+ { project_id, reason, intervention_id } = {},
68
+ { apiCall, randomUUID } = {},
69
+ ) {
70
+ const bad = uuid(project_id, "project_id");
71
+ if (bad) return bad;
72
+
73
+ if (typeof reason !== "string" || reason.trim() === "") {
74
+ return refuse("`reason` is required: why the intake is paused, in words a person can read.");
75
+ }
76
+
77
+ const id = interventionId(intervention_id, randomUUID);
78
+ if (id.bad) return id.bad;
79
+
80
+ return apiCall("POST", `/api/v1/projects/${encodeURIComponent(project_id)}/intake/pause`, {
81
+ reason,
82
+ intervention_id: id.id,
83
+ });
84
+ }
85
+
86
+ export async function resumeIntake(
87
+ { project_id, intervention_id } = {},
88
+ { apiCall, randomUUID } = {},
89
+ ) {
90
+ const bad = uuid(project_id, "project_id");
91
+ if (bad) return bad;
92
+
93
+ const id = interventionId(intervention_id, randomUUID);
94
+ if (id.bad) return id.bad;
95
+
96
+ return apiCall("POST", `/api/v1/projects/${encodeURIComponent(project_id)}/intake/resume`, {
97
+ intervention_id: id.id,
98
+ });
99
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * `work_item_requeue` / `work_item_retriage` / `work_item_cancel` / `work_item_close`
3
+ * (loopctl US-52.32): the intervention verbs on one work item —
4
+ * `POST /api/v1/stories/:id/{requeue,retriage,cancel,close}`.
5
+ *
6
+ * KEY: the session's own key (`agentKey()` in index.js, the holder credential once the
7
+ * startup register succeeded). The server compares that key's holder with the item's: the
8
+ * holder that holds the item is refused 409 self_intervention_blocked, and a key with no
9
+ * holder that is not a human's is 409 holder_required.
10
+ *
11
+ * Each call carries an `intervention_id`; one is generated when the caller gives none, so a
12
+ * caller that retries after a lost response should pass the id it used the first time. The
13
+ * server writes nothing for a repeated id and answers the item's current stage row.
14
+ */
15
+
16
+ import crypto from "node:crypto";
17
+
18
+ import { uuid } from "./delivery-loop.js";
19
+
20
+ export const INTERVENTION_VERBS = ["requeue", "retriage", "cancel", "close"];
21
+
22
+ function refuse(body) {
23
+ return { error: true, status: 0, body };
24
+ }
25
+
26
+ export function interventionPath(storyId, verb) {
27
+ return `/api/v1/stories/${encodeURIComponent(storyId)}/${verb}`;
28
+ }
29
+
30
+ // One apiCall per verb with its path written out, never `/${verb}`: test/route_coverage.test.js
31
+ // resolves each call site's path statically, and an interpolated final segment reaches no
32
+ // route it can name.
33
+ function postIntervention(apiCall, verb, storyId, body) {
34
+ const id = encodeURIComponent(storyId);
35
+
36
+ switch (verb) {
37
+ case "requeue":
38
+ return apiCall("POST", `/api/v1/stories/${id}/requeue`, body);
39
+ case "retriage":
40
+ return apiCall("POST", `/api/v1/stories/${id}/retriage`, body);
41
+ case "cancel":
42
+ return apiCall("POST", `/api/v1/stories/${id}/cancel`, body);
43
+ case "close":
44
+ return apiCall("POST", `/api/v1/stories/${id}/close`, body);
45
+ }
46
+ }
47
+
48
+ /** `POST /api/v1/stories/:id/<verb>`. */
49
+ export async function intervene(
50
+ verb,
51
+ { story_id, reason, intervention_id } = {},
52
+ { apiCall, randomUUID = () => crypto.randomUUID() } = {},
53
+ ) {
54
+ if (!INTERVENTION_VERBS.includes(verb)) {
55
+ return refuse(`unknown intervention verb ${verb}`);
56
+ }
57
+
58
+ const bad = uuid(story_id, "story_id");
59
+ if (bad) return bad;
60
+
61
+ if (typeof reason !== "string" || reason.trim() === "") {
62
+ return refuse("`reason` is required: why, in words a person can read.");
63
+ }
64
+
65
+ let id = intervention_id;
66
+ if (id === undefined || id === null || id === "") {
67
+ id = randomUUID();
68
+ } else {
69
+ const badId = uuid(id, "intervention_id");
70
+ if (badId) return badId;
71
+ }
72
+
73
+ return postIntervention(apiCall, verb, story_id, { reason, intervention_id: id });
74
+ }
@@ -0,0 +1,233 @@
1
+ /**
2
+ * The factory rulebook tools (loopctl US-52.32b): the rules that decide what happens to every
3
+ * escalated item in the delivery loop, their groups, the retry ceiling, and a dry-run check.
4
+ *
5
+ * Reads and `rulebook_check` take any key (agent+). Every write is `role: :orchestrator` on a
6
+ * human-anchored tenant and is judged against the WHOLE active rulebook as it would stand
7
+ * after it: loopctl refuses `422 ambiguous_rule`, `shadowed_rule`, `non_terminating`,
8
+ * `action_not_applicable` or `group_change_conflict`, with every error in `error.details`.
9
+ * Writes go out on LOOPCTL_ORCH_KEY (else LOOPCTL_USER_KEY), as the epic writes do.
10
+ *
11
+ * Each function takes `{apiCall}`; index.js hands the write functions the orchestrator-pinned call, so the tests inject a
12
+ * recording fake. Paths are written out as literals: the route sweep resolves them.
13
+ */
14
+
15
+ export const ACTIONS = ["requeue", "retriage", "reassign_stronger_holder", "close_with_evidence"];
16
+ export const DISPATCH_KINDS = ["implement", "triage", "verify", "review"];
17
+ export const STATUSES = ["draft", "active", "retired"];
18
+ export const CHECK_OPS = ["create", "update", "retire", "put_group", "delete_group", "set_retry_ceiling"];
19
+
20
+ const RULE_FIELDS = [
21
+ "action",
22
+ "rationale",
23
+ "priority",
24
+ "failure_reason",
25
+ "stage_state",
26
+ "dispatch_kind",
27
+ "attempts_min",
28
+ "attempts_max",
29
+ "repos",
30
+ "groups",
31
+ ];
32
+
33
+ const GROUP_NAME = /^[^/?#]{1,100}$/;
34
+
35
+ function refuse(body) {
36
+ return { error: true, status: 0, body };
37
+ }
38
+
39
+ function validRuleId(id) {
40
+ return Number.isInteger(id) && id > 0;
41
+ }
42
+
43
+ function ruleFields(args) {
44
+ const out = {};
45
+ for (const field of RULE_FIELDS) {
46
+ if (args[field] !== undefined) out[field] = args[field];
47
+ }
48
+ return out;
49
+ }
50
+
51
+ /**
52
+ * `GET /api/v1/rulebook/rules/:id` when `rule_id` is given, else `GET /api/v1/rulebook/rules`,
53
+ * optionally by status.
54
+ */
55
+ export async function rulebookRules({ rule_id, status } = {}, { apiCall } = {}) {
56
+ if (rule_id !== undefined && rule_id !== null) {
57
+ if (!validRuleId(rule_id)) return refuse("`rule_id` must be the rule's integer id.");
58
+ return apiCall("GET", `/api/v1/rulebook/rules/${rule_id}`, null);
59
+ }
60
+ if (status !== undefined && status !== null && !STATUSES.includes(status)) {
61
+ return refuse(`\`status\` must be one of: ${STATUSES.join(", ")}.`);
62
+ }
63
+ if (typeof status === "string") return apiCall("GET", `/api/v1/rulebook/rules?status=${status}`, null);
64
+ return apiCall("GET", "/api/v1/rulebook/rules", null);
65
+ }
66
+
67
+ /** `POST /api/v1/rulebook/rules`. */
68
+ export async function rulebookRuleCreate(args = {}, { apiCall } = {}) {
69
+ if (!ACTIONS.includes(args.action)) {
70
+ return refuse(`\`action\` is required, one of: ${ACTIONS.join(", ")}.`);
71
+ }
72
+ if (typeof args.rationale !== "string" || args.rationale.trim() === "") {
73
+ return refuse("`rationale` is required: why this rule exists, in a sentence.");
74
+ }
75
+ return apiCall("POST", "/api/v1/rulebook/rules", ruleFields(args));
76
+ }
77
+
78
+ /** `PATCH /api/v1/rulebook/rules/:id`. */
79
+ export async function rulebookRuleUpdate(args = {}, { apiCall } = {}) {
80
+ if (!validRuleId(args.rule_id)) return refuse("`rule_id` is required: the rule's integer id.");
81
+ const fields = ruleFields(args);
82
+ if (Object.keys(fields).length === 0) return refuse("Give at least one field to change.");
83
+ return apiCall("PATCH", `/api/v1/rulebook/rules/${args.rule_id}`, fields);
84
+ }
85
+
86
+ /** `POST /api/v1/rulebook/rules/:id/retire`. */
87
+ export async function rulebookRuleRetire({ rule_id } = {}, { apiCall } = {}) {
88
+ if (!validRuleId(rule_id)) return refuse("`rule_id` is required: the rule's integer id.");
89
+ return apiCall("POST", `/api/v1/rulebook/rules/${rule_id}/retire`, {});
90
+ }
91
+
92
+ /** `GET /api/v1/rulebook/groups`. */
93
+ export async function rulebookGroups(_args = {}, { apiCall } = {}) {
94
+ return apiCall("GET", "/api/v1/rulebook/groups", null);
95
+ }
96
+
97
+ /** `PUT /api/v1/rulebook/groups/:name` with `{members}`. */
98
+ export async function rulebookGroupPut({ name, members } = {}, { apiCall } = {}) {
99
+ if (typeof name !== "string" || !GROUP_NAME.test(name)) {
100
+ return refuse("`name` is required: 1-100 characters, no '/', '?' or '#'.");
101
+ }
102
+ if (!Array.isArray(members) || !members.every((m) => typeof m === "string")) {
103
+ return refuse("`members` is required: an array of project UUIDs (may be empty).");
104
+ }
105
+ return apiCall("PUT", `/api/v1/rulebook/groups/${encodeURIComponent(name)}`, { members });
106
+ }
107
+
108
+ /** `DELETE /api/v1/rulebook/groups/:name`. */
109
+ export async function rulebookGroupDelete({ name } = {}, { apiCall } = {}) {
110
+ if (typeof name !== "string" || !GROUP_NAME.test(name)) {
111
+ return refuse("`name` is required: the group's name.");
112
+ }
113
+ return apiCall("DELETE", `/api/v1/rulebook/groups/${encodeURIComponent(name)}`, null);
114
+ }
115
+
116
+ /** `PUT /api/v1/rulebook/retry_ceiling` with `{retry_ceiling}`. */
117
+ export async function rulebookRetryCeiling({ retry_ceiling } = {}, { apiCall } = {}) {
118
+ if (!(Number.isInteger(retry_ceiling) && retry_ceiling >= 1)) {
119
+ return refuse("`retry_ceiling` is required: an integer of at least 1.");
120
+ }
121
+ return apiCall("PUT", "/api/v1/rulebook/retry_ceiling", { retry_ceiling });
122
+ }
123
+
124
+ /** `POST /api/v1/rulebook/check`: the write's judgement, nothing written. */
125
+ export async function rulebookCheck(args = {}, { apiCall } = {}) {
126
+ if (!CHECK_OPS.includes(args.op)) {
127
+ return refuse(`\`op\` is required, one of: ${CHECK_OPS.join(", ")}.`);
128
+ }
129
+ const body = { op: args.op };
130
+ for (const field of ["rule_id", "attrs", "name", "members", "retry_ceiling"]) {
131
+ if (args[field] !== undefined) body[field] = args[field];
132
+ }
133
+ return apiCall("POST", "/api/v1/rulebook/check", body);
134
+ }
135
+
136
+ // US-52.34a: the rulebook's decisions and the nightly replay's health. Read-only, any key.
137
+
138
+ export const EXECUTIONS = ACTIONS;
139
+ export const DECISIONS_PATH = "/api/v1/rulebook/decisions";
140
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
141
+
142
+ function present(value) {
143
+ return value !== undefined && value !== null && value !== "";
144
+ }
145
+
146
+ /** `GET /api/v1/stories/:id/rulebook_decisions`: one item's decisions, newest first. */
147
+ export async function workItemRulebookDecisions({ story_id } = {}, { apiCall } = {}) {
148
+ if (typeof story_id !== "string" || !UUID.test(story_id)) {
149
+ return refuse("`story_id` is required: the work item's UUID.");
150
+ }
151
+ return apiCall("GET", `/api/v1/stories/${story_id}/rulebook_decisions`, null);
152
+ }
153
+
154
+ /** `GET /api/v1/rulebook/decisions`, one page, newest first. */
155
+ export async function rulebookDecisions(
156
+ { rule_id, fallback, executed_as, since, cursor, limit } = {},
157
+ { apiCall } = {},
158
+ ) {
159
+ if (present(rule_id) && !validRuleId(rule_id)) {
160
+ return refuse("`rule_id` must be the winning rule's integer id.");
161
+ }
162
+ if (present(fallback) && typeof fallback !== "boolean") {
163
+ return refuse("`fallback` must be true or false.");
164
+ }
165
+ if (present(executed_as) && !EXECUTIONS.includes(executed_as)) {
166
+ return refuse(`\`executed_as\` must be one of: ${EXECUTIONS.join(", ")}.`);
167
+ }
168
+ if (present(since) && (typeof since !== "string" || Number.isNaN(Date.parse(since)))) {
169
+ return refuse("`since` must be an ISO8601 timestamp, e.g. 2026-11-21T00:00:00Z.");
170
+ }
171
+ if (present(cursor) && typeof cursor !== "string") {
172
+ return refuse("`cursor` must be the string a previous page returned as next_cursor.");
173
+ }
174
+ if (present(limit) && !(Number.isInteger(limit) && limit >= 1 && limit <= 200)) {
175
+ return refuse("`limit` must be an integer from 1 to 200.");
176
+ }
177
+
178
+ const query = new URLSearchParams();
179
+ if (present(rule_id)) query.set("rule_id", String(rule_id));
180
+ if (present(fallback)) query.set("fallback", String(fallback));
181
+ if (present(executed_as)) query.set("executed_as", executed_as);
182
+ if (present(since)) query.set("since", since);
183
+ if (present(cursor)) query.set("cursor", cursor);
184
+ if (present(limit)) query.set("limit", String(limit));
185
+
186
+ // A template on the constant: route_coverage's resolver reads a template's leading constant.
187
+ const qs = query.toString();
188
+ const path = qs ? `${DECISIONS_PATH}?${qs}` : DECISIONS_PATH;
189
+ return apiCall("GET", path, null);
190
+ }
191
+
192
+ // US-52.34b: draft rules. Any key may propose (human-anchored tenant) and read; there is no
193
+ // promote tool because there is no promote route: the hourly eval worker alone promotes.
194
+
195
+ /** `POST /api/v1/rulebook/drafts`. */
196
+ export async function rulebookDraftCreate(args = {}, { apiCall } = {}) {
197
+ if (!ACTIONS.includes(args.action)) {
198
+ return refuse(`\`action\` is required, one of: ${ACTIONS.join(", ")}.`);
199
+ }
200
+ if (typeof args.rationale !== "string" || args.rationale.trim() === "") {
201
+ return refuse("`rationale` is required: why this rule should exist, in a sentence.");
202
+ }
203
+ if (typeof args.read_until !== "string" || Number.isNaN(Date.parse(args.read_until))) {
204
+ return refuse("`read_until` is required: ISO8601, the newest decided_at your cycle read.");
205
+ }
206
+ if (present(args.supersedes_rule_id) && !validRuleId(args.supersedes_rule_id)) {
207
+ return refuse("`supersedes_rule_id` must be an active rule's integer id.");
208
+ }
209
+ const body = ruleFields(args);
210
+ body.read_until = args.read_until;
211
+ if (present(args.supersedes_rule_id)) body.supersedes_rule_id = args.supersedes_rule_id;
212
+ return apiCall("POST", "/api/v1/rulebook/drafts", body);
213
+ }
214
+
215
+ /** `GET /api/v1/rulebook/drafts/:id` when `rule_id` is given, else `GET /api/v1/rulebook/drafts`. */
216
+ export async function rulebookDrafts({ rule_id } = {}, { apiCall } = {}) {
217
+ if (present(rule_id)) {
218
+ if (!validRuleId(rule_id)) return refuse("`rule_id` must be the draft's integer id.");
219
+ return apiCall("GET", `/api/v1/rulebook/drafts/${rule_id}`, null);
220
+ }
221
+ return apiCall("GET", "/api/v1/rulebook/drafts", null);
222
+ }
223
+
224
+ /** `POST /api/v1/rulebook/drafts/:id/withdraw`. */
225
+ export async function rulebookDraftWithdraw({ rule_id } = {}, { apiCall } = {}) {
226
+ if (!validRuleId(rule_id)) return refuse("`rule_id` is required: the draft's integer id.");
227
+ return apiCall("POST", `/api/v1/rulebook/drafts/${rule_id}/withdraw`, {});
228
+ }
229
+
230
+ /** `GET /api/v1/rulebook/health`. */
231
+ export async function rulebookHealth(_args = {}, { apiCall } = {}) {
232
+ return apiCall("GET", "/api/v1/rulebook/health", null);
233
+ }
package/lib/work-items.js CHANGED
@@ -80,3 +80,34 @@ export async function listWorkItemNotes({ story_id, section, limit, after } = {}
80
80
  const qs = query.toString();
81
81
  return apiCall("GET", notesPath(story_id) + (qs ? `?${qs}` : ""), null);
82
82
  }
83
+
84
+ /**
85
+ * `work_item_timeline` (loopctl US-52.29): `GET /api/v1/stories/:id/timeline`, the item now
86
+ * (stage and its age, holder, lease, issue, PR, runner, newest CI runs, thread) and one page of
87
+ * its event log and audit log merged oldest first. READ-ONLY on any authenticated key; it never
88
+ * moves a thread cursor. Every event reason and message body is session or reporter text and
89
+ * arrives flagged `untrusted: true`.
90
+ */
91
+ export function timelinePath(storyId) {
92
+ return `/api/v1/stories/${encodeURIComponent(storyId)}/timeline`;
93
+ }
94
+
95
+ export async function readWorkItemTimeline({ story_id, cursor, limit } = {}, { apiCall } = {}) {
96
+ const bad = uuid(story_id, "story_id");
97
+ if (bad) return bad;
98
+
99
+ if (limit !== undefined && limit !== null && !(Number.isInteger(limit) && limit >= 1 && limit <= 500)) {
100
+ return refuse("`limit` must be an integer from 1 to 500.");
101
+ }
102
+
103
+ if (cursor !== undefined && cursor !== null && typeof cursor !== "string") {
104
+ return refuse("`cursor` must be the string a previous page returned as next_cursor.");
105
+ }
106
+
107
+ const query = new URLSearchParams();
108
+ if (typeof cursor === "string" && cursor !== "") query.set("cursor", cursor);
109
+ if (Number.isInteger(limit)) query.set("limit", String(limit));
110
+
111
+ const qs = query.toString();
112
+ return apiCall("GET", timelinePath(story_id) + (qs ? `?${qs}` : ""), null);
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loopctl-mcp-server",
3
- "version": "3.22.1",
3
+ "version": "3.32.0",
4
4
  "description": "MCP server for loopctl — structural trust for AI development loops",
5
5
  "type": "module",
6
6
  "main": "index.js",