loopctl-mcp-server 3.44.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 +16 -0
- package/index.js +579 -2
- package/lib/agents.js +110 -0
- package/lib/api-keys.js +138 -29
- package/lib/audit-inclusion.js +46 -0
- package/lib/bulk-verdicts.js +125 -0
- package/lib/channel-quarantine.js +157 -0
- package/lib/secret-file.js +40 -0
- package/lib/story-claims.js +90 -0
- package/lib/tenant-admin.js +139 -0
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tenant's own settings and its authenticator labels: `update_tenant`
|
|
3
|
+
* (`PATCH /api/v1/tenants/me`, `TenantController.update`), `list_authenticators` and
|
|
4
|
+
* `rename_authenticator` (`GET /api/v1/tenants/:id/authenticators`,
|
|
5
|
+
* `PATCH /api/v1/tenants/:id/authenticators/:auth_id`, `TenantAuthenticatorController.index` /
|
|
6
|
+
* `.rename`), and `list_trusted_endpoints` (`GET /api/v1/egress/trusted-endpoints`,
|
|
7
|
+
* `EgressController.list_trusted`), loopctl #872.
|
|
8
|
+
*
|
|
9
|
+
* KEYS, from the controllers' plugs:
|
|
10
|
+
*
|
|
11
|
+
* - update_tenant is `role: :user` WITH the hierarchy (403 insufficient_role below it), not
|
|
12
|
+
* human-anchored and not custody surface: index.js `userKeyApiCall`, LOOPCTL_USER_KEY when
|
|
13
|
+
* set. It casts `name`, `email`, `settings`, `default_story_budget_millicents` and
|
|
14
|
+
* `token_data_retention_days`; `settings` is MERGED into the stored map key by key, never
|
|
15
|
+
* replaced. 422 for a malformed or taken email, a `settings` that is not an object or whose
|
|
16
|
+
* `knowledge_auto_extract` / `kb_curation_log` is not a boolean, a budget that is not above
|
|
17
|
+
* 0, and a retention that is not an integer of at least 30 (null turns retention off).
|
|
18
|
+
* - the authenticator list and rename are `role: :user` plus tenant ownership: 403 (insufficient_role
|
|
19
|
+
* below user, a bare `Forbidden` when `tenant_id` is not the key's own tenant). Neither
|
|
20
|
+
* verifies a WebAuthn assertion, unlike enroll and revoke, so a tool can complete both. The
|
|
21
|
+
* list carries ids, labels and `credential_fingerprint` (the identity to confirm before
|
|
22
|
+
* `revoke_authenticator`; a label is rewritable by any user key), never credential
|
|
23
|
+
* material. Rename refusals: 400 without a string `friendly_name`, 404 for an unknown
|
|
24
|
+
* authenticator, 422 friendly_name_blank / friendly_name_invalid / friendly_name_too_long
|
|
25
|
+
* (over 120 bytes after trimming), 429 past the per-tenant rename budget (300 an hour), 500
|
|
26
|
+
* when the rename or its audit entry could not be written.
|
|
27
|
+
* - the trusted-endpoint list has no role plug: any key of the tenant, the session's own.
|
|
28
|
+
* Every entry is an UNVERIFIED tenant attestation, not network locality.
|
|
29
|
+
*
|
|
30
|
+
* CHECKED HERE, because the server would answer them WRONGLY rather than refuse:
|
|
31
|
+
*
|
|
32
|
+
* - update_tenant's `slug`: the update never casts it (it keys the audit-key secret, rls-02),
|
|
33
|
+
* so a body carrying one answers 200 with the slug unchanged;
|
|
34
|
+
* - an update with no field at all, which answers 200 having changed nothing;
|
|
35
|
+
* - a `tenant_id` in upper case: ownership compares the id as a STRING against the key's
|
|
36
|
+
* lower-case tenant id, so the right tenant in the wrong case answers 403. It is sent lower
|
|
37
|
+
* case, which names the same tenant;
|
|
38
|
+
* - non-UUID ids and a label the server would refuse, so a call that cannot succeed sends
|
|
39
|
+
* nothing.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { uuid } from "./delivery-loop.js";
|
|
43
|
+
|
|
44
|
+
export const TENANT_UPDATE_FIELDS = [
|
|
45
|
+
"name",
|
|
46
|
+
"email",
|
|
47
|
+
"settings",
|
|
48
|
+
"default_story_budget_millicents",
|
|
49
|
+
"token_data_retention_days",
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
export const MAX_FRIENDLY_NAME_BYTES = 120;
|
|
53
|
+
|
|
54
|
+
function refuse(body) {
|
|
55
|
+
return { error: true, status: 0, body };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
59
|
+
export function tenantMePath() {
|
|
60
|
+
return `/api/v1/tenants/me`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function authenticatorsPath(tenantId) {
|
|
64
|
+
return `/api/v1/tenants/${encodeURIComponent(tenantId)}/authenticators`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function authenticatorPath(tenantId, authenticatorId) {
|
|
68
|
+
return `/api/v1/tenants/${encodeURIComponent(tenantId)}/authenticators/${encodeURIComponent(authenticatorId)}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function trustedEndpointsPath() {
|
|
72
|
+
return `/api/v1/egress/trusted-endpoints`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function updateTenant(args = {}, { apiCall } = {}) {
|
|
76
|
+
if (Object.hasOwn(args, "slug")) {
|
|
77
|
+
return refuse(
|
|
78
|
+
"`slug` is immutable: it keys the tenant's audit-key secret, and loopctl ignores it on " +
|
|
79
|
+
"update and answers 200 with the slug unchanged. Nothing was sent.",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const body = {};
|
|
84
|
+
for (const field of TENANT_UPDATE_FIELDS) {
|
|
85
|
+
if (args[field] !== undefined) body[field] = args[field];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (Object.keys(body).length === 0) {
|
|
89
|
+
return refuse(`Pass at least one of: ${TENANT_UPDATE_FIELDS.join(", ")}.`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (
|
|
93
|
+
body.settings !== undefined &&
|
|
94
|
+
(body.settings === null || typeof body.settings !== "object" || Array.isArray(body.settings))
|
|
95
|
+
) {
|
|
96
|
+
return refuse("`settings` must be an object; its keys are merged into the stored settings.");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return apiCall("PATCH", tenantMePath(), body);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function tenantIdOf(tenant_id) {
|
|
103
|
+
return uuid(tenant_id, "tenant_id") ?? { ok: tenant_id.toLowerCase() };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function listAuthenticators({ tenant_id } = {}, { apiCall } = {}) {
|
|
107
|
+
const tenant = tenantIdOf(tenant_id);
|
|
108
|
+
if (!tenant.ok) return tenant;
|
|
109
|
+
|
|
110
|
+
return apiCall("GET", authenticatorsPath(tenant.ok), null);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function renameAuthenticator(
|
|
114
|
+
{ tenant_id, authenticator_id, friendly_name } = {},
|
|
115
|
+
{ apiCall } = {},
|
|
116
|
+
) {
|
|
117
|
+
const tenant = tenantIdOf(tenant_id);
|
|
118
|
+
if (!tenant.ok) return tenant;
|
|
119
|
+
|
|
120
|
+
const bad = uuid(authenticator_id, "authenticator_id");
|
|
121
|
+
if (bad) return bad;
|
|
122
|
+
|
|
123
|
+
if (typeof friendly_name !== "string" || friendly_name.trim() === "") {
|
|
124
|
+
return refuse("`friendly_name` is required: the new label, not blank.");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (Buffer.byteLength(friendly_name.trim(), "utf8") > MAX_FRIENDLY_NAME_BYTES) {
|
|
128
|
+
return refuse(
|
|
129
|
+
`\`friendly_name\` is at most ${MAX_FRIENDLY_NAME_BYTES} bytes after trimming ` +
|
|
130
|
+
"(a multi-byte character counts each byte).",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return apiCall("PATCH", authenticatorPath(tenant.ok, authenticator_id), { friendly_name });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function listTrustedEndpoints(_args = {}, { apiCall } = {}) {
|
|
138
|
+
return apiCall("GET", trustedEndpointsPath(), null);
|
|
139
|
+
}
|