loopctl-mcp-server 3.21.2 → 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.
- package/README.md +31 -6
- package/index.js +792 -20
- package/lib/delivery-loop.js +12 -7
- package/lib/factory.js +99 -0
- package/lib/holder-key-file.js +135 -0
- package/lib/holder.js +50 -2
- package/lib/inbox.js +102 -0
- package/lib/interventions.js +74 -0
- package/lib/machine-name.js +40 -0
- package/lib/messages.js +88 -0
- package/lib/rulebook.js +233 -0
- package/lib/work-items.js +31 -0
- package/package.json +1 -1
package/lib/rulebook.js
ADDED
|
@@ -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
|
+
}
|