loopctl-mcp-server 3.43.0 → 3.45.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 +37 -0
- package/index.js +1096 -2
- package/lib/agents.js +110 -0
- package/lib/api-keys.js +138 -29
- package/lib/audit-feed.js +184 -0
- package/lib/audit-inclusion.js +46 -0
- package/lib/bulk-verdicts.js +125 -0
- package/lib/channel-quarantine.js +157 -0
- package/lib/dependencies.js +108 -0
- package/lib/dispatch-reads.js +75 -0
- package/lib/secret-file.js +40 -0
- package/lib/story-claims.js +90 -0
- package/lib/story-evidence.js +102 -0
- package/lib/tenant-admin.js +139 -0
- package/lib/work-breakdown-reads.js +82 -0
- package/package.json +1 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bulk_verify` / `bulk_reject` — `POST /api/v1/stories/bulk/verify` and
|
|
3
|
+
* `POST /api/v1/stories/bulk/reject` (`BulkOperationsController.verify` / `.reject`),
|
|
4
|
+
* loopctl #872.
|
|
5
|
+
*
|
|
6
|
+
* KEY: these are custody verdicts, mounted `exact_role: :orchestrator` with NO hierarchy
|
|
7
|
+
* (`bulk_operations_controller.ex`), the same gate as `verify_story`, `reject_story` and
|
|
8
|
+
* `bulk_mark_complete`. index.js selects the key through `orchestratorKeyArgs`
|
|
9
|
+
* (`lib/custody-key.js`): LOOPCTL_ORCH_KEY sent exactly when set, else a global
|
|
10
|
+
* LOOPCTL_API_KEY. A user or superadmin key is 403 insufficient_role, so the user key is never
|
|
11
|
+
* reached for. Exposing them adds no key to this process: the orchestrator key is the one
|
|
12
|
+
* `verify_story` already sends, and loopctl runs the same lineage comparison per story that it
|
|
13
|
+
* runs for one (`Progress.ensure_verify_allowed/4`, the caller's lineage resolved from the key).
|
|
14
|
+
*
|
|
15
|
+
* WHAT THE ENDPOINTS DO, read from the controller and `Loopctl.BulkOperations`:
|
|
16
|
+
*
|
|
17
|
+
* - `stories` is at most 50 entries (422 when empty, too long or not a list). Each entry is
|
|
18
|
+
* processed in one transaction under row locks, partial success: 200 with `results` when any
|
|
19
|
+
* story succeeded, 422 with `results` when none did. Each result is
|
|
20
|
+
* `{story_id, status, reason?}`, and a story's custody refusal (`self_verify_blocked`,
|
|
21
|
+
* `caller_lineage_required`, `unresolvable_dispatch_lineage`, a chg landed-review or
|
|
22
|
+
* trunk-evidence refusal, `Story not found`, ...) is that story's `reason`.
|
|
23
|
+
* - Whole-batch refusals: 400 when the orchestrator key is linked to no agent, 409
|
|
24
|
+
* chg_root_cannot_verify for a chg-rooted key, 403 custody_tier_required on an agent-rooted
|
|
25
|
+
* tenant, 503 tenant_halted under a custody halt (custody surface). Verify only: under the
|
|
26
|
+
* LCP-1 `signed` profile an enrolled caller is refused 401 bulk_signature_unsupported (or 403
|
|
27
|
+
* agent_enrollment_required), because a set cannot carry a per-story claim signature. Reject
|
|
28
|
+
* only: 500 audit_chain_append_failed when a release's escalation could not append its chain
|
|
29
|
+
* entry, and the whole batch rolled back.
|
|
30
|
+
* - verify reads each entry's `summary`, `review_type` and `findings`; reject reads `reason`
|
|
31
|
+
* (required, non-empty: a per-story `reason_required` otherwise), `review_type` and
|
|
32
|
+
* `findings`. The published request schema names a `notes` field: the server never reads it.
|
|
33
|
+
*
|
|
34
|
+
* CHECKED HERE, because the server would answer them WRONGLY rather than refuse:
|
|
35
|
+
*
|
|
36
|
+
* - a non-string `summary`/`review_type`/`reason` or a non-object `findings`: the
|
|
37
|
+
* verification_result insert fails its changeset, the failure is only LOGGED, and the story
|
|
38
|
+
* is verified or rejected anyway with no result row — a verdict whose record went missing;
|
|
39
|
+
* - a duplicate story id: the per-story parameters are a map keyed by id, so the last entry's
|
|
40
|
+
* fields silently replace the first's;
|
|
41
|
+
* - a non-UUID story id, which fails the lock query's cast instead of reading as one error.
|
|
42
|
+
*
|
|
43
|
+
* The batch bounds and a missing reject `reason` are refused too, so a call that cannot succeed
|
|
44
|
+
* sends nothing.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { badStoryIdBatch } from "./story-claims.js";
|
|
48
|
+
|
|
49
|
+
function refuse(body) {
|
|
50
|
+
return { error: true, status: 0, body };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
54
|
+
export function bulkVerifyPath() {
|
|
55
|
+
return `/api/v1/stories/bulk/verify`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function bulkRejectPath() {
|
|
59
|
+
return `/api/v1/stories/bulk/reject`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function badEntries(stories, { requireReason }) {
|
|
63
|
+
if (!Array.isArray(stories) || stories.length === 0) {
|
|
64
|
+
return refuse("`stories` is required: a non-empty array of `{story_id, ...}` entries.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
for (const [i, entry] of stories.entries()) {
|
|
68
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
|
|
69
|
+
return refuse(`\`stories[${i}]\` must be an object with a \`story_id\`.`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const batch = badStoryIdBatch(
|
|
74
|
+
stories.map((s) => s.story_id),
|
|
75
|
+
"stories",
|
|
76
|
+
(i) => `stories[${i}].story_id`,
|
|
77
|
+
);
|
|
78
|
+
if (batch) return batch;
|
|
79
|
+
|
|
80
|
+
for (const [i, entry] of stories.entries()) {
|
|
81
|
+
for (const field of ["summary", "review_type", "reason"]) {
|
|
82
|
+
if (entry[field] != null && typeof entry[field] !== "string") {
|
|
83
|
+
return refuse(`\`stories[${i}].${field}\` must be a string.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
entry.findings != null &&
|
|
89
|
+
(typeof entry.findings !== "object" || Array.isArray(entry.findings))
|
|
90
|
+
) {
|
|
91
|
+
return refuse(`\`stories[${i}].findings\` must be an object.`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (requireReason && (typeof entry.reason !== "string" || entry.reason.trim() === "")) {
|
|
95
|
+
return refuse(`\`stories[${i}].reason\` is required: why the story is rejected.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Only the fields the server reads, so nothing a caller adds rides along unread.
|
|
103
|
+
function pick(entry, fields) {
|
|
104
|
+
const out = { story_id: entry.story_id };
|
|
105
|
+
for (const field of fields) {
|
|
106
|
+
if (entry[field] != null) out[field] = entry[field];
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function bulkVerifyStories({ stories } = {}, { apiCall } = {}) {
|
|
112
|
+
const bad = badEntries(stories, { requireReason: false });
|
|
113
|
+
if (bad) return bad;
|
|
114
|
+
|
|
115
|
+
const body = { stories: stories.map((s) => pick(s, ["summary", "review_type", "findings"])) };
|
|
116
|
+
return apiCall("POST", bulkVerifyPath(), body);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function bulkRejectStories({ stories } = {}, { apiCall } = {}) {
|
|
120
|
+
const bad = badEntries(stories, { requireReason: true });
|
|
121
|
+
if (bad) return bad;
|
|
122
|
+
|
|
123
|
+
const body = { stories: stories.map((s) => pick(s, ["reason", "review_type", "findings"])) };
|
|
124
|
+
return apiCall("POST", bulkRejectPath(), body);
|
|
125
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `channel_quarantine_list` / `channel_quarantine_release` — `GET /api/v1/channel/posts/quarantined` and
|
|
3
|
+
* `POST /api/v1/channel/posts/:id/release` (`ChannelPostController.quarantined` / `.release`,
|
|
4
|
+
* issue #499), loopctl #872.
|
|
5
|
+
*
|
|
6
|
+
* KEY: both are `role: :user` WITH the hierarchy plus `RequireHumanAnchor`, so an agent or
|
|
7
|
+
* orchestrator key is 403 insufficient_role and an agent-rooted tenant 403
|
|
8
|
+
* custody_tier_required. They travel on LOOPCTL_USER_KEY when set, else the default key (index.js
|
|
9
|
+
* `userKeyApiCall`). Neither is custody surface, so a custody halt does not block them.
|
|
10
|
+
*
|
|
11
|
+
* WHY THE BODIES GO TO A FILE. The quarantine read is the ONE endpoint that returns the full
|
|
12
|
+
* text of posts the secret rescan flagged as carrying a credential shape, and it is gated at
|
|
13
|
+
* user role precisely so that no agent reads one back. A tool result lands in the session
|
|
14
|
+
* transcript, so returning those fields would copy each suspected credential into the
|
|
15
|
+
* transcript of whichever session asked. `api_key_create` keeps a raw key out of the transcript
|
|
16
|
+
* the same way: the full rows are written to `rows_file` (an absolute path that must not exist
|
|
17
|
+
* yet, created 0600 before the request), and the result carries every row with the fields the
|
|
18
|
+
* rescan scans REMOVED — `quarantine_reason` names which of them tripped. A person reads the
|
|
19
|
+
* file, judges, then redacts (`channel_delete`) or releases (`channel_quarantine_release`), and deletes
|
|
20
|
+
* the file.
|
|
21
|
+
*
|
|
22
|
+
* The release answers with the released post; its scanned fields are removed from the result
|
|
23
|
+
* for the same reason (a mistaken release is still a credential), and `channel_get` reads it
|
|
24
|
+
* back. Release refusals: 404, byte-identical, for a post that is missing, another tenant's,
|
|
25
|
+
* malformed or not quarantined; 500 audit_write_failed when the audit entry could not be written
|
|
26
|
+
* (the release rolled back and the post is still quarantined).
|
|
27
|
+
*
|
|
28
|
+
* CHECKED HERE, because the server would answer it WRONGLY rather than refuse: a `limit`
|
|
29
|
+
* outside 1..100 (0 or less reads as the default 25, more is clamped to 100). A `project_id` is
|
|
30
|
+
* an id, or a slug or repo name `ResolveProjectRef` rewrites to one; a reference that resolves
|
|
31
|
+
* to no project is not refused by the server, it matches nothing and answers an empty list, and
|
|
32
|
+
* nothing here can tell that apart from an empty quarantine, so the description says so. A
|
|
33
|
+
* non-UUID post id is refused: it answers the same 404 as a post that is not quarantined.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { isAbsolute } from "node:path";
|
|
37
|
+
import { uuid } from "./delivery-loop.js";
|
|
38
|
+
import { createSecretFile } from "./secret-file.js";
|
|
39
|
+
|
|
40
|
+
// `ChannelPost.secret_fields/1`'s scanned set (`@scanned_text_fields` + `refs`), as the
|
|
41
|
+
// controller's `quarantined_json/1` returns them.
|
|
42
|
+
export const SCANNED_FIELDS = [
|
|
43
|
+
"body",
|
|
44
|
+
"key",
|
|
45
|
+
"session_id",
|
|
46
|
+
"host",
|
|
47
|
+
"to_host",
|
|
48
|
+
"to_capability",
|
|
49
|
+
"idempotency_key",
|
|
50
|
+
"refs",
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
function refuse(body) {
|
|
54
|
+
return { error: true, status: 0, body };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
58
|
+
export function quarantinedPath(query = "") {
|
|
59
|
+
return `/api/v1/channel/posts/quarantined${query}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function releasePath(postId) {
|
|
63
|
+
return `/api/v1/channel/posts/${encodeURIComponent(postId)}/release`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function withoutScannedFields(row) {
|
|
67
|
+
if (row === null || typeof row !== "object" || Array.isArray(row)) return row;
|
|
68
|
+
const out = {};
|
|
69
|
+
for (const [field, value] of Object.entries(row)) {
|
|
70
|
+
if (!SCANNED_FIELDS.includes(field)) out[field] = value;
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* `deps.apiCall(method, path, body)` is the user-key caller; `deps.openRows(path)` creates the
|
|
77
|
+
* file and returns `{ writeSecret(text), discardSecret() }` (injected so a test owns the disk it touches).
|
|
78
|
+
*/
|
|
79
|
+
export async function listQuarantinedPosts(
|
|
80
|
+
{ project_id, limit, rows_file } = {},
|
|
81
|
+
{ apiCall, openRows = createSecretFile } = {},
|
|
82
|
+
) {
|
|
83
|
+
if (project_id != null && (typeof project_id !== "string" || project_id.trim() === "")) {
|
|
84
|
+
return refuse("`project_id` must be a non-empty string: a project's id, slug or repo name.");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (limit != null && !(Number.isInteger(limit) && limit >= 1 && limit <= 100)) {
|
|
88
|
+
return refuse("`limit` must be an integer from 1 to 100 (default 25).");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof rows_file !== "string" || !isAbsolute(rows_file)) {
|
|
92
|
+
return refuse(
|
|
93
|
+
"`rows_file` must be an absolute path that does not exist yet. The quarantined posts' " +
|
|
94
|
+
"full fields are written there (mode 0600) and never returned, because a tool result " +
|
|
95
|
+
"lands in the transcript and these posts were quarantined as suspected credentials.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let sink;
|
|
100
|
+
try {
|
|
101
|
+
sink = openRows(rows_file);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return refuse(
|
|
104
|
+
`${rows_file} could not be created (${error.code || error.message}); nothing was read. ` +
|
|
105
|
+
"Pass a path that does not exist yet, in a directory that does.",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const params = new URLSearchParams();
|
|
110
|
+
if (project_id != null) params.set("project_id", project_id);
|
|
111
|
+
if (limit != null) params.set("limit", String(limit));
|
|
112
|
+
const qs = params.toString();
|
|
113
|
+
|
|
114
|
+
let result;
|
|
115
|
+
try {
|
|
116
|
+
result = await apiCall("GET", quarantinedPath(qs === "" ? "" : `?${qs}`), null);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
sink.discardSecret();
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!result || result.error || !Array.isArray(result.data)) {
|
|
123
|
+
sink.discardSecret();
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
sink.writeSecret(JSON.stringify(result, null, 2) + "\n");
|
|
129
|
+
} catch (error) {
|
|
130
|
+
sink.discardSecret();
|
|
131
|
+
return refuse(
|
|
132
|
+
`${rows_file} could not be written (${error.code || error.message}); the posts were read ` +
|
|
133
|
+
"and are not returned here. Read again with a new rows_file.",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
data: result.data.map(withoutScannedFields),
|
|
139
|
+
meta: result.meta,
|
|
140
|
+
rows_file,
|
|
141
|
+
withheld_fields: SCANNED_FIELDS,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function releaseQuarantinedPost({ post_id } = {}, { apiCall } = {}) {
|
|
146
|
+
const bad = uuid(post_id, "post_id");
|
|
147
|
+
if (bad) return bad;
|
|
148
|
+
|
|
149
|
+
const result = await apiCall("POST", releasePath(post_id), null);
|
|
150
|
+
|
|
151
|
+
if (result && !result.error && result.post) {
|
|
152
|
+
return { ...result, post: withoutScannedFields(result.post), withheld_fields: SCANNED_FIELDS };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dependency surface (loopctl #872): `dependency_graph`
|
|
3
|
+
* (`GET /api/v1/projects/:id/dependency_graph`, `DependencyGraphController.graph`), and the
|
|
4
|
+
* epic and story dependency edges — `epic_dependency_add` / `_remove` / `_list`
|
|
5
|
+
* (`EpicDependencyController`) and `story_dependency_add` / `_remove` / `_list`
|
|
6
|
+
* (`StoryDependencyController`).
|
|
7
|
+
*
|
|
8
|
+
* KEYS, from the controllers' plugs (all `role:`, WITH the hierarchy):
|
|
9
|
+
*
|
|
10
|
+
* - reads — the graph and both lists — are `role: :agent`, on the session's own key
|
|
11
|
+
* (`agentKey()` in index.js).
|
|
12
|
+
* - create is `role: :orchestrator`, human-anchored: a constructive work-breakdown write, on
|
|
13
|
+
* LOOPCTL_ORCH_KEY, else LOOPCTL_USER_KEY, sent verbatim when set
|
|
14
|
+
* (`orchestratorPinnedApiCall`), as `create_epic` / `update_story` are.
|
|
15
|
+
* - delete is `role: :user`, human-anchored ("destructive" in the controllers' own
|
|
16
|
+
* moduledoc), on LOOPCTL_USER_KEY sent verbatim when set (`userKeyApiCall`), as
|
|
17
|
+
* `delete_epic` is. 204 with no body.
|
|
18
|
+
*
|
|
19
|
+
* REFUSALS a create can meet, from `Dependencies.create_*_dependency/3`: 404 when either end is
|
|
20
|
+
* not in the tenant; 409 when the edge already exists (the unique constraint); 422 for a
|
|
21
|
+
* self-dependency, ends in different projects, a cycle, or a CROSS-LEVEL DEADLOCK — an epic
|
|
22
|
+
* edge A -> B while a story in B already depends on a story in A, or a story edge whose epics
|
|
23
|
+
* already depend the other way. The self-dependency is also refused here, before any call.
|
|
24
|
+
*
|
|
25
|
+
* Every id is checked for UUID shape: a malformed id reaches the same 404 as a well-formed one
|
|
26
|
+
* naming nothing, which reads as "that row is gone" when the argument is what is wrong.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { uuid } from "./delivery-loop.js";
|
|
30
|
+
|
|
31
|
+
function refuse(body) {
|
|
32
|
+
return { error: true, status: 0, body };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
36
|
+
export function dependencyGraphPath(projectId) {
|
|
37
|
+
return `/api/v1/projects/${encodeURIComponent(projectId)}/dependency_graph`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function epicDependenciesPath() {
|
|
41
|
+
return `/api/v1/epic_dependencies`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function epicDependencyPath(dependencyId) {
|
|
45
|
+
return `/api/v1/epic_dependencies/${encodeURIComponent(dependencyId)}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function projectEpicDependenciesPath(projectId) {
|
|
49
|
+
return `/api/v1/projects/${encodeURIComponent(projectId)}/epic_dependencies`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function storyDependenciesPath() {
|
|
53
|
+
return `/api/v1/story_dependencies`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function storyDependencyPath(dependencyId) {
|
|
57
|
+
return `/api/v1/story_dependencies/${encodeURIComponent(dependencyId)}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function epicStoryDependenciesPath(epicId) {
|
|
61
|
+
return `/api/v1/epics/${encodeURIComponent(epicId)}/story_dependencies`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function readDependencyGraph({ project_id } = {}, { apiCall } = {}) {
|
|
65
|
+
return uuid(project_id, "project_id") ?? apiCall("GET", dependencyGraphPath(project_id), null);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function addEpicDependency({ epic_id, depends_on_epic_id } = {}, { apiCall } = {}) {
|
|
69
|
+
const bad = uuid(epic_id, "epic_id") ?? uuid(depends_on_epic_id, "depends_on_epic_id");
|
|
70
|
+
if (bad) return bad;
|
|
71
|
+
|
|
72
|
+
if (epic_id.toLowerCase() === depends_on_epic_id.toLowerCase()) {
|
|
73
|
+
return refuse("An epic cannot depend on itself: `epic_id` and `depends_on_epic_id` are the same.");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return apiCall("POST", epicDependenciesPath(), { epic_id, depends_on_epic_id });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function removeEpicDependency({ dependency_id } = {}, { apiCall } = {}) {
|
|
80
|
+
return uuid(dependency_id, "dependency_id") ?? apiCall("DELETE", epicDependencyPath(dependency_id), null);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function listEpicDependencies({ project_id } = {}, { apiCall } = {}) {
|
|
84
|
+
return (
|
|
85
|
+
uuid(project_id, "project_id") ?? apiCall("GET", projectEpicDependenciesPath(project_id), null)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function addStoryDependency({ story_id, depends_on_story_id } = {}, { apiCall } = {}) {
|
|
90
|
+
const bad = uuid(story_id, "story_id") ?? uuid(depends_on_story_id, "depends_on_story_id");
|
|
91
|
+
if (bad) return bad;
|
|
92
|
+
|
|
93
|
+
if (story_id.toLowerCase() === depends_on_story_id.toLowerCase()) {
|
|
94
|
+
return refuse("A story cannot depend on itself: `story_id` and `depends_on_story_id` are the same.");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return apiCall("POST", storyDependenciesPath(), { story_id, depends_on_story_id });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function removeStoryDependency({ dependency_id } = {}, { apiCall } = {}) {
|
|
101
|
+
return (
|
|
102
|
+
uuid(dependency_id, "dependency_id") ?? apiCall("DELETE", storyDependencyPath(dependency_id), null)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function listStoryDependencies({ epic_id } = {}, { apiCall } = {}) {
|
|
107
|
+
return uuid(epic_id, "epic_id") ?? apiCall("GET", epicStoryDependenciesPath(epic_id), null);
|
|
108
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dispatch_list` / `dispatch_get` — `GET /api/v1/dispatches` and `GET /api/v1/dispatches/:id`
|
|
3
|
+
* (`DispatchController.index` / `.show`, loopctl #872).
|
|
4
|
+
*
|
|
5
|
+
* WHY THEY EXIST. `dispatch` mints and `revoke_dispatch` revokes, and nothing read the rows in
|
|
6
|
+
* between: a session holding an `implementer_dispatch_id` off a story, or a
|
|
7
|
+
* `remediation.your_dispatch_id` off a refusal, could not see that dispatch's lineage, role,
|
|
8
|
+
* expiry or revocation without `iex` on the production node.
|
|
9
|
+
*
|
|
10
|
+
* KEY: the session's own key (`agentKey()` in index.js). Both actions are `role: :agent` WITH
|
|
11
|
+
* the hierarchy (`dispatch_controller.ex`), so any key of the tenant reads them; neither is
|
|
12
|
+
* human-anchored and a custody halt never blocks a read.
|
|
13
|
+
*
|
|
14
|
+
* WHAT THE ENDPOINTS ACCEPT, read from the controller and `Dispatches.list_dispatches/2`:
|
|
15
|
+
*
|
|
16
|
+
* - index: `role` (an `Ecto.Enum` of agent, orchestrator, user), `active_only` (only the
|
|
17
|
+
* literal "true" filters: unrevoked and unexpired), `limit` (default 50, clamped to 1..100)
|
|
18
|
+
* and `offset` (default 0). A non-integer `limit`/`offset` is silently dropped by
|
|
19
|
+
* `parse_int/1`, and a role outside the enum is compared against an enum column, so all
|
|
20
|
+
* three are checked here rather than sent.
|
|
21
|
+
* - show: a dispatch id. A non-UUID reaches `LoopctlWeb.CastErrorHandler` and answers the
|
|
22
|
+
* same 404 as a well-formed id naming no dispatch, so it is refused locally.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { uuid } from "./delivery-loop.js";
|
|
26
|
+
|
|
27
|
+
export const DISPATCH_ROLES = ["agent", "orchestrator", "user"];
|
|
28
|
+
|
|
29
|
+
function refuse(body) {
|
|
30
|
+
return { error: true, status: 0, body };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ONE template literal per builder: `test/tool-surface.js` resolves it to the route it names,
|
|
34
|
+
// and the query string after `/dispatches` contributes nothing to the route.
|
|
35
|
+
export function dispatchesPath(query = {}) {
|
|
36
|
+
return `/api/v1/dispatches${dispatchesQuery(query)}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function dispatchPath(dispatchId) {
|
|
40
|
+
return `/api/v1/dispatches/${encodeURIComponent(dispatchId)}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function dispatchesQuery({ role, active_only, limit, offset } = {}) {
|
|
44
|
+
const params = new URLSearchParams();
|
|
45
|
+
if (role != null) params.set("role", role);
|
|
46
|
+
if (active_only === true) params.set("active_only", "true");
|
|
47
|
+
if (limit != null) params.set("limit", String(limit));
|
|
48
|
+
if (offset != null) params.set("offset", String(offset));
|
|
49
|
+
const qs = params.toString();
|
|
50
|
+
return qs === "" ? "" : `?${qs}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function listDispatches({ role, active_only, limit, offset } = {}, { apiCall } = {}) {
|
|
54
|
+
if (role != null && !DISPATCH_ROLES.includes(role)) {
|
|
55
|
+
return refuse(`\`role\` is one of: ${DISPATCH_ROLES.join(", ")}.`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (active_only != null && typeof active_only !== "boolean") {
|
|
59
|
+
return refuse("`active_only` must be a boolean.");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (limit != null && !(Number.isInteger(limit) && limit >= 1 && limit <= 100)) {
|
|
63
|
+
return refuse("`limit` must be an integer from 1 to 100 (default 50).");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (offset != null && !(Number.isInteger(offset) && offset >= 0)) {
|
|
67
|
+
return refuse("`offset` must be a non-negative integer.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return apiCall("GET", dispatchesPath({ role, active_only, limit, offset }), null);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function getDispatchById({ dispatch_id } = {}, { apiCall } = {}) {
|
|
74
|
+
return uuid(dispatch_id, "dispatch_id") ?? apiCall("GET", dispatchPath(dispatch_id), null);
|
|
75
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one sink for a secret a tool must keep out of the session transcript (a raw API key,
|
|
3
|
+
* quarantined channel rows). The file is created exclusively with mode 0600 BEFORE the request
|
|
4
|
+
* that produces the secret, so a path that exists or a missing directory is refused with nothing
|
|
5
|
+
* sent. Any hardening of how a secret reaches disk belongs here, not in a caller.
|
|
6
|
+
*
|
|
7
|
+
* Returns `{ writeSecret(text), discardSecret() }`: the first writes once and closes; the
|
|
8
|
+
* second closes and unlinks, and is safe to call after a write or twice. The method names are
|
|
9
|
+
* deliberately not `write`/`discard`: test/tool-surface.js reaches functions by NAME, and a
|
|
10
|
+
* bare `write(` call would be credited with the holder journal's `write` and its routes.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { closeSync, openSync, unlinkSync, writeSync } from "node:fs";
|
|
14
|
+
|
|
15
|
+
export function createSecretFile(path) {
|
|
16
|
+
const fd = openSync(path, "wx", 0o600);
|
|
17
|
+
let open = true;
|
|
18
|
+
|
|
19
|
+
const close = () => {
|
|
20
|
+
if (open) {
|
|
21
|
+
open = false;
|
|
22
|
+
closeSync(fd);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
writeSecret: (text) => {
|
|
28
|
+
writeSync(fd, text);
|
|
29
|
+
close();
|
|
30
|
+
},
|
|
31
|
+
discardSecret: () => {
|
|
32
|
+
close();
|
|
33
|
+
try {
|
|
34
|
+
unlinkSync(path);
|
|
35
|
+
} catch {
|
|
36
|
+
// already gone
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bulk_claim` / `unclaim_story` — `POST /api/v1/stories/bulk/claim`
|
|
3
|
+
* (`BulkOperationsController.claim`) and `POST /api/v1/stories/:id/unclaim`
|
|
4
|
+
* (`StoryStatusController.unclaim`), loopctl #872.
|
|
5
|
+
*
|
|
6
|
+
* KEY: the session's own key (`agentKey()` in index.js, the holder credential when this session
|
|
7
|
+
* has one, else LOOPCTL_AGENT_KEY). Both actions are `exact_role: :agent` with NO hierarchy, so
|
|
8
|
+
* an orchestrator, user or superadmin key is 403 insufficient_role like any non-member, and the
|
|
9
|
+
* user key must never be reached for. Both are human-anchored (403 custody_tier_required on an
|
|
10
|
+
* agent-rooted tenant) and custody surface (`LoopctlWeb.CustodySurface`: `stories/bulk/claim`
|
|
11
|
+
* and `stories/:id/unclaim`), so a custody halt answers 503 tenant_halted.
|
|
12
|
+
*
|
|
13
|
+
* WHAT THE ENDPOINTS DO, read from the controllers and `Loopctl.BulkOperations.bulk_claim/4`:
|
|
14
|
+
*
|
|
15
|
+
* - bulk claim takes `story_ids` (at most 50; 422 when empty, too long or not a list) and
|
|
16
|
+
* claims each `contracted` story for the key's agent and holder, one transaction, sorted
|
|
17
|
+
* ids, partial success: 200 with `results` when any story was claimed, 422 with `results`
|
|
18
|
+
* when none was. Each result is `{story_id, status, reason?}`; per-story reasons include a
|
|
19
|
+
* story not in `contracted`, `story_held`, `dependencies_not_met` and `Story not found`.
|
|
20
|
+
* 409 chg_root_review_only refuses the whole batch for a chg-rooted key. Unlike
|
|
21
|
+
* `claim_story` it mints NO start capability: `start_story` recovers one through
|
|
22
|
+
* `recover-cap`.
|
|
23
|
+
* - unclaim releases the caller's own claim. 403 when the key's agent is not the assignee,
|
|
24
|
+
* 409 for a story in no claimable state or 409 held_by_other_holder (another session holds
|
|
25
|
+
* it), 404 for another tenant's story, 422 when the release's write or audit entry was
|
|
26
|
+
* rejected, 500 audit_chain_append_failed when the release reached the retry ceiling and the
|
|
27
|
+
* escalation's chain entry was refused (the whole release rolled back). A delivery story is
|
|
28
|
+
* re-contracted below the retry ceiling and escalated over `attempts_exhausted` at it.
|
|
29
|
+
*
|
|
30
|
+
* CHECKED HERE: a non-UUID id (a story id is a `:binary_id`, and a bad one in the batch fails
|
|
31
|
+
* the lock query's cast instead of reading as one story's error), a duplicate in the batch
|
|
32
|
+
* (claimed once, then reported as a second story not in `contracted`), and the batch bounds the
|
|
33
|
+
* server would refuse anyway, so a call that cannot succeed sends nothing.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { uuid } from "./delivery-loop.js";
|
|
37
|
+
|
|
38
|
+
export const BULK_MAX = 50;
|
|
39
|
+
|
|
40
|
+
function refuse(body) {
|
|
41
|
+
return { error: true, status: 0, body };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
45
|
+
export function bulkClaimPath() {
|
|
46
|
+
return `/api/v1/stories/bulk/claim`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function unclaimPath(storyId) {
|
|
50
|
+
return `/api/v1/stories/${encodeURIComponent(storyId)}/unclaim`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Refuses a story-id batch the server would refuse or misread; `null` when it is sendable.
|
|
55
|
+
* `field` names the batch in a message and `label(i)` names one of its ids.
|
|
56
|
+
*/
|
|
57
|
+
export function badStoryIdBatch(ids, field, label = (i) => `${field}[${i}]`) {
|
|
58
|
+
if (!Array.isArray(ids) || ids.length === 0) {
|
|
59
|
+
return refuse(`\`${field}\` is required: a non-empty array.`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (ids.length > BULK_MAX) {
|
|
63
|
+
return refuse(`\`${field}\` carries ${ids.length} stories; loopctl takes at most ${BULK_MAX}.`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const [i, id] of ids.entries()) {
|
|
67
|
+
const bad = uuid(id, label(i));
|
|
68
|
+
if (bad) return bad;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const seen = new Set();
|
|
72
|
+
for (const id of ids) {
|
|
73
|
+
const key = id.toLowerCase();
|
|
74
|
+
if (seen.has(key)) return refuse(`\`${field}\` names story ${id} twice; name each story once.`);
|
|
75
|
+
seen.add(key);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function bulkClaimStories({ story_ids } = {}, { apiCall } = {}) {
|
|
82
|
+
const bad = badStoryIdBatch(story_ids, "story_ids");
|
|
83
|
+
if (bad) return bad;
|
|
84
|
+
|
|
85
|
+
return apiCall("POST", bulkClaimPath(), { story_ids });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function unclaimStory({ story_id } = {}, { apiCall } = {}) {
|
|
89
|
+
return uuid(story_id, "story_id") ?? apiCall("POST", unclaimPath(story_id), null);
|
|
90
|
+
}
|