loopctl-mcp-server 3.50.1 → 3.58.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 +13 -7
- package/index.js +361 -58
- package/lib/agent-outcomes.js +55 -0
- package/lib/agent-replay-set.js +51 -0
- package/lib/chg-findings.js +4 -2
- package/lib/fly-metrics-credential.js +105 -0
- package/lib/intake-sources.js +38 -28
- package/lib/knowledge-reads.js +87 -0
- package/lib/runners.js +46 -0
- package/lib/story-retrieval.js +77 -0
- package/lib/tuning-drafts.js +31 -3
- package/package.json +1 -1
package/lib/agent-outcomes.js
CHANGED
|
@@ -44,3 +44,58 @@ export async function agentOutcomes({ spec_id, window_days } = {}, { apiCall } =
|
|
|
44
44
|
const qs = Number.isInteger(window_days) ? `?window_days=${window_days}` : "";
|
|
45
45
|
return apiCall("GET", `${agentOutcomesPath(spec_id)}${qs}`, null);
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
49
|
+
|
|
50
|
+
export function agentOutcomesComparePath(specId) {
|
|
51
|
+
return `${agentOutcomesPath(specId)}/compare`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `agent_outcomes_compare` (loopctl US-55.2): `GET
|
|
56
|
+
* /api/v1/agent_specs/:spec_id/outcomes/compare?at=&window_days=&project_id=` — the spec's runs
|
|
57
|
+
* in the window before `at` (a landed prompt change) against its runs since, and whether each
|
|
58
|
+
* side holds the server's minimum. Keyed like `agent_outcomes`.
|
|
59
|
+
*
|
|
60
|
+
* `at` is required and must be readable by `Date.parse`, or nothing is sent. The server is
|
|
61
|
+
* stricter (ISO-8601 with an offset, not in the future) and answers 400 for the rest.
|
|
62
|
+
*/
|
|
63
|
+
export async function agentOutcomesCompare(
|
|
64
|
+
{ spec_id, at, window_days, project_id } = {},
|
|
65
|
+
{ apiCall } = {},
|
|
66
|
+
) {
|
|
67
|
+
if (typeof spec_id !== "string" || !SPEC_ID.test(spec_id)) {
|
|
68
|
+
return refuse(
|
|
69
|
+
"`spec_id` is required: an agent spec id, lower-case (e.g. `triage`, `verify`).",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof at !== "string" || at === "" || Number.isNaN(Date.parse(at))) {
|
|
74
|
+
return refuse(
|
|
75
|
+
"`at` is required: an ISO-8601 date-time with an offset, e.g. `2026-11-01T00:00:00Z` " +
|
|
76
|
+
"(a tuning draft's `landed_at`, or a deploy time).",
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (
|
|
81
|
+
window_days !== undefined &&
|
|
82
|
+
window_days !== null &&
|
|
83
|
+
!(Number.isInteger(window_days) && window_days >= 1 && window_days <= MAX_WINDOW_DAYS)
|
|
84
|
+
) {
|
|
85
|
+
return refuse(`\`window_days\` must be an integer from 1 to ${MAX_WINDOW_DAYS}.`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
project_id !== undefined &&
|
|
90
|
+
project_id !== null &&
|
|
91
|
+
!(typeof project_id === "string" && UUID.test(project_id))
|
|
92
|
+
) {
|
|
93
|
+
return refuse("`project_id` must be a project UUID.");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const params = [`at=${encodeURIComponent(at)}`];
|
|
97
|
+
if (Number.isInteger(window_days)) params.push(`window_days=${window_days}`);
|
|
98
|
+
if (typeof project_id === "string") params.push(`project_id=${encodeURIComponent(project_id)}`);
|
|
99
|
+
|
|
100
|
+
return apiCall("GET", `${agentOutcomesComparePath(spec_id)}?${params.join("&")}`, null);
|
|
101
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `agent_replay_set` (loopctl US-55.5, US-55.13): an agent spec's past runs rebuilt as replay
|
|
3
|
+
* cases — `GET /api/v1/agent_specs/:spec_id/replay_set?limit=N`, for `triage` or `verify`.
|
|
4
|
+
*
|
|
5
|
+
* triage: the newest triage runs (ended `triage_verdict` or `triage_incomplete`), each as the
|
|
6
|
+
* dispatch the live triage pass would send for its story TODAY. verify: the newest verify@1
|
|
7
|
+
* verdicts, each as the live verify dispatch at the head the runner measured. Each case says where
|
|
8
|
+
* its expected answer comes from (`human`, `downstream`, verify's `downstream_tree`, or `agent`,
|
|
9
|
+
* which is never scored). Save the response to a file and run
|
|
10
|
+
* `mix loopctl_runner.eval <spec>@<version> --replay-from FILE`.
|
|
11
|
+
*
|
|
12
|
+
* `role: :user`: every case carries customer text, as tuning drafts do, so this takes the EXACT
|
|
13
|
+
* user key (`runnerDeps()`), and a global LOOPCTL_API_KEY must not stand in. It writes nothing.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const MISSING_USER_KEY =
|
|
17
|
+
"No user-role API key configured. Set LOOPCTL_USER_KEY to a user-role key to export a " +
|
|
18
|
+
"replay set: every case carries the reporter's ticket text, so the endpoint refuses agent " +
|
|
19
|
+
"and orchestrator keys.";
|
|
20
|
+
|
|
21
|
+
export const MAX_LIMIT = 100;
|
|
22
|
+
|
|
23
|
+
const SPEC_ID = /^[a-z][a-z0-9_]{0,63}$/;
|
|
24
|
+
|
|
25
|
+
function refuse(body) {
|
|
26
|
+
return { error: true, status: 0, body };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function replaySetPath(specId) {
|
|
30
|
+
return `/api/v1/agent_specs/${encodeURIComponent(specId)}/replay_set`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `GET /api/v1/agent_specs/:spec_id/replay_set`, optionally `?limit=N` (1..100). */
|
|
34
|
+
export async function agentReplaySet({ spec_id, limit } = {}, { userKey, apiCall } = {}) {
|
|
35
|
+
if (!userKey) return refuse(MISSING_USER_KEY);
|
|
36
|
+
|
|
37
|
+
if (typeof spec_id !== "string" || !SPEC_ID.test(spec_id)) {
|
|
38
|
+
return refuse("`spec_id` is required: an agent spec id, lower-case (today `triage` or `verify`).");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (
|
|
42
|
+
limit !== undefined &&
|
|
43
|
+
limit !== null &&
|
|
44
|
+
!(Number.isInteger(limit) && limit >= 1 && limit <= MAX_LIMIT)
|
|
45
|
+
) {
|
|
46
|
+
return refuse(`\`limit\` must be an integer from 1 to ${MAX_LIMIT}.`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const qs = Number.isInteger(limit) ? `?limit=${limit}` : "";
|
|
50
|
+
return apiCall("GET", `${replaySetPath(spec_id)}${qs}`, null);
|
|
51
|
+
}
|
package/lib/chg-findings.js
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* gated like clear: a user key, used exactly.
|
|
7
7
|
*
|
|
8
8
|
* A finding is a problem the nightly sweep derived from the chg ledger (a selection miss, a
|
|
9
|
-
* flaky test, a merge with no review, a stuck change, a silent ledger sender)
|
|
10
|
-
*
|
|
9
|
+
* flaky test, a merge with no review, a stuck change, a silent ledger sender) or one the
|
|
10
|
+
* post-deploy monitor recorded (a deploy regression, loopctl US-55.14), filed as a GitHub
|
|
11
|
+
* issue that the delivery loop then fixes.
|
|
11
12
|
*
|
|
12
13
|
* The list is a READ, `role: :agent` with the hierarchy, so the handler passes
|
|
13
14
|
* LOOPCTL_AGENT_KEY as an override and `resolveKey`'s order holds. Clearing an ESCALATED
|
|
@@ -22,6 +23,7 @@ export const KINDS = [
|
|
|
22
23
|
"missing_review",
|
|
23
24
|
"stuck_change",
|
|
24
25
|
"source_silent",
|
|
26
|
+
"deploy_regression",
|
|
25
27
|
];
|
|
26
28
|
export const LIST_CAP = 200;
|
|
27
29
|
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `fly_metrics_credential`, `set_fly_metrics_credential`, `clear_fly_metrics_credential` — the
|
|
3
|
+
* tenant's own Fly metrics credential (loopctl US-55.7),
|
|
4
|
+
* `GET|PUT|DELETE /api/v1/tenants/me/fly-metrics-credential`.
|
|
5
|
+
*
|
|
6
|
+
* The post-deploy monitor reads the edge metrics of the Fly apps a tenant's intake sources name
|
|
7
|
+
* (`fly_app`) from Fly's managed Prometheus for the tenant's org, with this credential and no
|
|
8
|
+
* other: there is no operator token, so a tenant with none is simply not monitored.
|
|
9
|
+
*
|
|
10
|
+
* All three need the `:user` key: the endpoint manages a stored credential. The token is never
|
|
11
|
+
* returned — reads show `has_token`, a last-4 `token_hint` and the `org`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import os from "node:os";
|
|
15
|
+
import defaultFs from "node:fs/promises";
|
|
16
|
+
|
|
17
|
+
import { readSecretPath } from "./runners.js";
|
|
18
|
+
|
|
19
|
+
export const FLY_METRICS_CREDENTIAL_PATH = "/api/v1/tenants/me/fly-metrics-credential";
|
|
20
|
+
|
|
21
|
+
/** The longest token the server accepts (`TenantCredential.max_token_length/0`). */
|
|
22
|
+
export const MAX_FLY_TOKEN_CHARS = 4096;
|
|
23
|
+
|
|
24
|
+
const ORG_FORMAT = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
25
|
+
const TOKEN_FORMAT = /^FlyV1 [A-Za-z0-9_+/=,-]+$/;
|
|
26
|
+
|
|
27
|
+
function refuse(body) {
|
|
28
|
+
return { error: true, status: 0, body };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function flyMetricsCredential(_args = {}, { apiCall } = {}) {
|
|
32
|
+
return apiCall("GET", FLY_METRICS_CREDENTIAL_PATH, null);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The token from `token_file`: an absolute (or `~/`) path holding the output of
|
|
37
|
+
* `fly tokens create readonly <org>`, so the token never passes through the transcript. A file
|
|
38
|
+
* of several lines is read for its one line starting `FlyV1 `. Resolves to `{ token }` or
|
|
39
|
+
* `{ refusal }`; no refusal carries the file's contents.
|
|
40
|
+
*/
|
|
41
|
+
async function readTokenFile(tokenFile, { fs, homedir }) {
|
|
42
|
+
const read = await readSecretPath("token_file", tokenFile, { fs, homedir });
|
|
43
|
+
if (read.message) return { refusal: refuse(read.message) };
|
|
44
|
+
const { path, raw } = read;
|
|
45
|
+
|
|
46
|
+
const lines = raw.split(/\r?\n/).map((l) => l.trim()).filter((l) => l !== "");
|
|
47
|
+
const flyLines = lines.filter((l) => l.startsWith("FlyV1 "));
|
|
48
|
+
if (flyLines.length === 1) return { token: flyLines[0] };
|
|
49
|
+
if (lines.length === 1) return { token: lines[0] };
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
refusal: refuse(
|
|
53
|
+
`token_file '${path}' must hold one token: a single line, or exactly one line starting ` +
|
|
54
|
+
"`FlyV1 `. Nothing was sent.",
|
|
55
|
+
),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A malformed org or token is refused HERE, before any call, with the server's rule; the
|
|
61
|
+
* refusal never repeats the token, because a tool result lands in the transcript. The token
|
|
62
|
+
* comes from `token` or `token_file`, never both.
|
|
63
|
+
*/
|
|
64
|
+
export async function setFlyMetricsCredential(
|
|
65
|
+
{ org, token, token_file: tokenFile } = {},
|
|
66
|
+
{ apiCall, fs = defaultFs, homedir = os.homedir() } = {},
|
|
67
|
+
) {
|
|
68
|
+
if (typeof org !== "string" || !ORG_FORMAT.test(org)) {
|
|
69
|
+
return refuse(
|
|
70
|
+
"`org` is required: the Fly org slug (`fly orgs list`), e.g. `personal` — lowercase " +
|
|
71
|
+
"letters, digits and hyphens, at most 63 characters, starting with a letter or digit.",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (token !== undefined && tokenFile !== undefined) {
|
|
76
|
+
return refuse("Send `token` OR `token_file`, not both: they are two ways of naming the same value.");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (tokenFile !== undefined) {
|
|
80
|
+
const read = await readTokenFile(tokenFile, { fs, homedir });
|
|
81
|
+
if (read.refusal) return read.refusal;
|
|
82
|
+
token = read.token;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const trimmed = typeof token === "string" ? token.trim() : "";
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
trimmed === "" ||
|
|
89
|
+
[...trimmed].length > MAX_FLY_TOKEN_CHARS ||
|
|
90
|
+
!TOKEN_FORMAT.test(trimmed)
|
|
91
|
+
) {
|
|
92
|
+
return refuse(
|
|
93
|
+
"`token` (or `token_file`) is required: the output of `fly tokens create readonly <org>`, which starts " +
|
|
94
|
+
`\`FlyV1 \` and holds only A-Z a-z 0-9 _ + / = , - after it, at most ` +
|
|
95
|
+
`${MAX_FLY_TOKEN_CHARS} characters. A \`Bearer\` personal token is refused: it ` +
|
|
96
|
+
"reaches every org its user can.",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return apiCall("PUT", FLY_METRICS_CREDENTIAL_PATH, { org, token: trimmed });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function clearFlyMetricsCredential(_args = {}, { apiCall } = {}) {
|
|
104
|
+
return apiCall("DELETE", FLY_METRICS_CREDENTIAL_PATH, null);
|
|
105
|
+
}
|
package/lib/intake-sources.js
CHANGED
|
@@ -142,7 +142,13 @@ import os from "node:os";
|
|
|
142
142
|
import defaultFs from "node:fs/promises";
|
|
143
143
|
|
|
144
144
|
import { uuid as uuidRefusal } from "./delivery-loop.js";
|
|
145
|
-
import {
|
|
145
|
+
import {
|
|
146
|
+
expandHome,
|
|
147
|
+
readSecretPath,
|
|
148
|
+
TOKEN_DIR_MODE,
|
|
149
|
+
TOKEN_FILE_FLAGS,
|
|
150
|
+
TOKEN_FILE_MODE,
|
|
151
|
+
} from "./runners.js";
|
|
146
152
|
|
|
147
153
|
export const SOURCES_PATH = "/api/v1/intake/sources";
|
|
148
154
|
|
|
@@ -298,32 +304,9 @@ export async function chgCredentials(args, { clearable, fs = defaultFs, homedir
|
|
|
298
304
|
}
|
|
299
305
|
|
|
300
306
|
async function readSecretFile(secretFile, { fs, homedir }) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const path = expandHome(secretFile, homedir);
|
|
308
|
-
if (!nodePath.isAbsolute(path)) {
|
|
309
|
-
return {
|
|
310
|
-
refusal: refuse(
|
|
311
|
-
`change_events_secret_file must be absolute or start with ~/ (got '${secretFile}').`,
|
|
312
|
-
),
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
let raw;
|
|
317
|
-
try {
|
|
318
|
-
raw = await fs.readFile(path, "utf8");
|
|
319
|
-
} catch (err) {
|
|
320
|
-
return {
|
|
321
|
-
refusal: refuse(
|
|
322
|
-
`Could not read change_events_secret_file '${path}' (${err?.code || "error"}). ` +
|
|
323
|
-
"Nothing was sent.",
|
|
324
|
-
),
|
|
325
|
-
};
|
|
326
|
-
}
|
|
307
|
+
const read = await readSecretPath("change_events_secret_file", secretFile, { fs, homedir });
|
|
308
|
+
if (read.message) return { refusal: refuse(read.message) };
|
|
309
|
+
const { path, raw } = read;
|
|
327
310
|
|
|
328
311
|
const secret = raw.replace(/\r?\n$/, "");
|
|
329
312
|
if (secret === "") {
|
|
@@ -468,6 +451,25 @@ export function deployEnvironmentRefusal(value, { clearable }) {
|
|
|
468
451
|
);
|
|
469
452
|
}
|
|
470
453
|
|
|
454
|
+
// The rule `Loopctl.Intake.Source.fly_app_pattern/0` states and `intake_sources_fly_app_shape`
|
|
455
|
+
// holds.
|
|
456
|
+
const FLY_APP_FORMAT = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* `fly_app` (loopctl US-55.7): the Fly app the post-deploy monitor reads this source's edge
|
|
460
|
+
* metrics for. A Fly app name, or `null`, which clears it; only an update may send it.
|
|
461
|
+
*/
|
|
462
|
+
export function flyAppRefusal(value) {
|
|
463
|
+
if (value === null) return null;
|
|
464
|
+
if (typeof value === "string" && FLY_APP_FORMAT.test(value)) return null;
|
|
465
|
+
|
|
466
|
+
return refuse(
|
|
467
|
+
"`fly_app` must be a Fly app name — lowercase letters, digits and hyphens, at most 63 " +
|
|
468
|
+
"characters, starting with a letter or digit — or null to clear it (which stops the " +
|
|
469
|
+
"post-deploy monitor reading the source).",
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
471
473
|
export function sourcePath(sourceId) {
|
|
472
474
|
return `${SOURCES_PATH}/${encodeURIComponent(sourceId)}`;
|
|
473
475
|
}
|
|
@@ -498,6 +500,7 @@ export function publicSource(source) {
|
|
|
498
500
|
base_branch: source.base_branch,
|
|
499
501
|
issue_label: source.issue_label,
|
|
500
502
|
deploy_environment: source.deploy_environment,
|
|
503
|
+
fly_app: source.fly_app,
|
|
501
504
|
mode: source.mode,
|
|
502
505
|
required_checks: source.required_checks,
|
|
503
506
|
triage_requires_ticket: source.triage_requires_ticket,
|
|
@@ -893,6 +896,13 @@ export async function updateIntakeSource(
|
|
|
893
896
|
body.deploy_environment = args.deploy_environment;
|
|
894
897
|
}
|
|
895
898
|
|
|
899
|
+
// US-55.7: null is FORWARDED, the only way to stop the post-deploy monitor reading the source.
|
|
900
|
+
if (args.fly_app !== undefined) {
|
|
901
|
+
const badApp = flyAppRefusal(args.fly_app);
|
|
902
|
+
if (badApp) return badApp;
|
|
903
|
+
body.fly_app = args.fly_app;
|
|
904
|
+
}
|
|
905
|
+
|
|
896
906
|
if (args.mode !== undefined) {
|
|
897
907
|
const badMode = modeRefusal(args.mode);
|
|
898
908
|
if (badMode) return badMode;
|
|
@@ -922,7 +932,7 @@ export async function updateIntakeSource(
|
|
|
922
932
|
return refuse(
|
|
923
933
|
"Nothing to update. Name at least one of `target_epic_id` (null clears it), " +
|
|
924
934
|
"`base_branch`, `issue_label` (null clears it), `deploy_environment` (null clears " +
|
|
925
|
-
"it), `mode`, `required_checks`, " +
|
|
935
|
+
"it), `fly_app` (null clears it), `mode`, `required_checks`, " +
|
|
926
936
|
"`triage_requires_ticket`, " +
|
|
927
937
|
"`change_events_secret` (or " +
|
|
928
938
|
"`change_events_secret_file`), `chg_root_dispatch_id` or `mint_chg_root`. A body " +
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The three body reads that record a knowledge access event, and the story each one names
|
|
3
|
+
* (loopctl US-55.17): `knowledge_get` (`GET /api/v1/articles/:id`), `knowledge_context`
|
|
4
|
+
* (`GET /api/v1/knowledge/context`) and `knowledge_progressive_drill`
|
|
5
|
+
* (`GET /api/v1/knowledge/progressive/:id`).
|
|
6
|
+
*
|
|
7
|
+
* `story_id` is attribution only. When the caller passes one it is sent as given. When it
|
|
8
|
+
* passes none, `LOOPCTL_STORY_ID` is sent, read from the environment at CALL time: the loopctl
|
|
9
|
+
* runner sets it in a dispatched implement session to the story that session works, and this
|
|
10
|
+
* server inherits the session's environment. loopctl records either as `story_attribution:
|
|
11
|
+
* caller` (its word is the caller's, never the server's). An env value that is not a UUID is
|
|
12
|
+
* ignored rather than sent, since the context and drill reads refuse a malformed one with a
|
|
13
|
+
* 422 and a stray variable must not break every read. With neither, nothing is sent, and a
|
|
14
|
+
* holder key's reads are attributed server-side to its one open item.
|
|
15
|
+
*
|
|
16
|
+
* The article id arrives already checked (`articleId` in index.js, which its wiring test
|
|
17
|
+
* pins), as `id`.
|
|
18
|
+
*/
|
|
19
|
+
import { uuid } from "./delivery-loop.js";
|
|
20
|
+
|
|
21
|
+
export const STORY_ID_ENV = "LOOPCTL_STORY_ID";
|
|
22
|
+
|
|
23
|
+
/** The story id a read sends: the caller's, else a UUID-shaped `LOOPCTL_STORY_ID`, else none. */
|
|
24
|
+
export function readStoryId(story_id, env = process.env) {
|
|
25
|
+
if (story_id) return story_id;
|
|
26
|
+
const fromEnv = typeof env[STORY_ID_ENV] === "string" ? env[STORY_ID_ENV].trim() : "";
|
|
27
|
+
return fromEnv && uuid(fromEnv, STORY_ID_ENV) === null ? fromEnv : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function setWindow(params, body_max_bytes, body_offset) {
|
|
31
|
+
// 0 is meaningful on both (whole body / start at the beginning), so test for
|
|
32
|
+
// null/undefined rather than truthiness.
|
|
33
|
+
if (body_max_bytes !== undefined && body_max_bytes !== null)
|
|
34
|
+
params.set("body_max_bytes", String(body_max_bytes));
|
|
35
|
+
if (body_offset !== undefined && body_offset !== null)
|
|
36
|
+
params.set("body_offset", String(body_offset));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function withQuery(base, params) {
|
|
40
|
+
const qs = params.toString();
|
|
41
|
+
return qs ? `${base}?${qs}` : base;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `knowledge_get`: `GET /api/v1/articles/:id`. */
|
|
45
|
+
export async function knowledgeGetRead(
|
|
46
|
+
{ id, project_id, story_id, links, body_max_bytes, body_offset } = {},
|
|
47
|
+
{ apiCall, env = process.env } = {},
|
|
48
|
+
) {
|
|
49
|
+
const params = new URLSearchParams();
|
|
50
|
+
if (project_id) params.set("project_id", project_id);
|
|
51
|
+
const story = readStoryId(story_id, env);
|
|
52
|
+
if (story) params.set("story_id", story);
|
|
53
|
+
if (links) params.set("links", links);
|
|
54
|
+
setWindow(params, body_max_bytes, body_offset);
|
|
55
|
+
return apiCall("GET", withQuery(`/api/v1/articles/${id}`, params), null);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `knowledge_context`: `GET /api/v1/knowledge/context`. */
|
|
59
|
+
export async function knowledgeContextRead(
|
|
60
|
+
{ query, project_id, story_id, limit, recency_weight, memory_types, agents, conversation_id } = {},
|
|
61
|
+
{ apiCall, env = process.env } = {},
|
|
62
|
+
) {
|
|
63
|
+
const params = new URLSearchParams({ query });
|
|
64
|
+
if (project_id) params.set("project_id", project_id);
|
|
65
|
+
const story = readStoryId(story_id, env);
|
|
66
|
+
if (story) params.set("story_id", story);
|
|
67
|
+
if (limit != null) params.set("limit", String(limit));
|
|
68
|
+
if (recency_weight != null) params.set("recency_weight", String(recency_weight));
|
|
69
|
+
// Agent-memory scoping (comma-separated lists are OR'd; conversation_id is exact).
|
|
70
|
+
if (memory_types)
|
|
71
|
+
params.set("memory_types", Array.isArray(memory_types) ? memory_types.join(",") : memory_types);
|
|
72
|
+
if (agents) params.set("agents", Array.isArray(agents) ? agents.join(",") : agents);
|
|
73
|
+
if (conversation_id) params.set("conversation_id", conversation_id);
|
|
74
|
+
return apiCall("GET", `/api/v1/knowledge/context?${params}`, null);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `knowledge_progressive_drill`: `GET /api/v1/knowledge/progressive/:id`. */
|
|
78
|
+
export async function knowledgeDrillRead(
|
|
79
|
+
{ id, story_id, body_max_bytes, body_offset } = {},
|
|
80
|
+
{ apiCall, env = process.env } = {},
|
|
81
|
+
) {
|
|
82
|
+
const params = new URLSearchParams();
|
|
83
|
+
const story = readStoryId(story_id, env);
|
|
84
|
+
if (story) params.set("story_id", story);
|
|
85
|
+
setWindow(params, body_max_bytes, body_offset);
|
|
86
|
+
return apiCall("GET", withQuery(`/api/v1/knowledge/progressive/${id}`, params), null);
|
|
87
|
+
}
|
package/lib/runners.js
CHANGED
|
@@ -64,6 +64,52 @@ export function expandHome(p, homedir = os.homedir()) {
|
|
|
64
64
|
return p;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/** The largest file a `*_file` secret argument may name; every secret here is far smaller. */
|
|
68
|
+
export const MAX_SECRET_FILE_BYTES = 64 * 1024;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read the file a `*_file` secret argument names (`field` is the argument's name, for the
|
|
72
|
+
* messages). Resolves to `{ path, raw }` or `{ message }`, a refusal that never carries the
|
|
73
|
+
* file's contents. The path must be absolute or `~/`, and must be a regular file of at most
|
|
74
|
+
* `maxBytes`: it is stat'ed BEFORE it is opened, because opening a FIFO blocks until a writer
|
|
75
|
+
* comes and a device such as /dev/zero never ends, either of which would hang the tool call.
|
|
76
|
+
*/
|
|
77
|
+
export async function readSecretPath(
|
|
78
|
+
field,
|
|
79
|
+
value,
|
|
80
|
+
{ fs, homedir = os.homedir(), maxBytes = MAX_SECRET_FILE_BYTES } = {},
|
|
81
|
+
) {
|
|
82
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
83
|
+
return { message: `\`${field}\` must be a path to the file holding the secret.` };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const path = expandHome(value, homedir);
|
|
87
|
+
if (!nodePath.isAbsolute(path)) {
|
|
88
|
+
return { message: `${field} must be absolute or start with ~/ (got '${value}').` };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let stat;
|
|
92
|
+
try {
|
|
93
|
+
stat = await fs.stat(path);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return { message: `Could not read ${field} '${path}' (${err?.code || "error"}). Nothing was sent.` };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!stat.isFile()) {
|
|
99
|
+
return { message: `${field} '${path}' is not a regular file. Nothing was sent.` };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (stat.size > maxBytes) {
|
|
103
|
+
return { message: `${field} '${path}' is larger than ${maxBytes} bytes. Nothing was sent.` };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
return { path, raw: await fs.readFile(path, "utf8") };
|
|
108
|
+
} catch (err) {
|
|
109
|
+
return { message: `Could not read ${field} '${path}' (${err?.code || "error"}). Nothing was sent.` };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
67
113
|
/**
|
|
68
114
|
* `POST /api/v1/runners {name, max_sessions?}`, with the returned token written to
|
|
69
115
|
* `token_file` and never returned. Resolves to
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `story_retrieval` (loopctl US-55.3): which knowledge articles were opened while work was
|
|
3
|
+
* done, beside how the work came out.
|
|
4
|
+
*
|
|
5
|
+
* - with `story_id`: `GET /api/v1/knowledge/analytics/stories/:id/retrieval` — the story's
|
|
6
|
+
* body reads per article (title, reads, first read, reads per route) and its outcome
|
|
7
|
+
* (`first_time_pass` for a loop story; `review_rounds` and `merged` for an interactive
|
|
8
|
+
* item).
|
|
9
|
+
* - with `project_id`: `GET /api/v1/knowledge/analytics/projects/:id/retrieval-outcomes`,
|
|
10
|
+
* optionally `?window_days=N` (1..90, default 30) — the project's decided loop stories and
|
|
11
|
+
* ended interactive items in the window, grouped by whether any article was opened.
|
|
12
|
+
*
|
|
13
|
+
* Both are `role: :orchestrator` with the hierarchy (knowledge_analytics_controller.ex), so
|
|
14
|
+
* the handler sends the orchestrator key pinned as `knowledge_project_usage` does. Exactly
|
|
15
|
+
* one of `story_id` and `project_id` is required; both, neither, a malformed id or a window
|
|
16
|
+
* out of range is refused here, before any request.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { uuid } from "./delivery-loop.js";
|
|
20
|
+
|
|
21
|
+
export const MAX_WINDOW_DAYS = 90;
|
|
22
|
+
|
|
23
|
+
function refuse(body) {
|
|
24
|
+
return { error: true, status: 0, body };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ONE template literal per builder (see `test/tool-surface.js`).
|
|
28
|
+
export function storyRetrievalPath(storyId) {
|
|
29
|
+
return `/api/v1/knowledge/analytics/stories/${encodeURIComponent(storyId)}/retrieval`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function projectRetrievalOutcomesPath(projectId, query = "") {
|
|
33
|
+
return `/api/v1/knowledge/analytics/projects/${encodeURIComponent(projectId)}/retrieval-outcomes${query}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function given(value) {
|
|
37
|
+
return value !== undefined && value !== null && value !== "";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function storyRetrieval({ story_id, project_id, window_days } = {}, { apiCall } = {}) {
|
|
41
|
+
const hasStory = given(story_id);
|
|
42
|
+
const hasProject = given(project_id);
|
|
43
|
+
|
|
44
|
+
if (hasStory === hasProject) {
|
|
45
|
+
return refuse(
|
|
46
|
+
"Pass exactly one of `story_id` (one work item's opened articles and outcome) or " +
|
|
47
|
+
"`project_id` (the project's outcomes grouped by whether an article was opened).",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (hasStory) {
|
|
52
|
+
if (given(window_days)) {
|
|
53
|
+
return refuse("`window_days` applies to `project_id` only.");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const bad = uuid(story_id, "story_id");
|
|
57
|
+
if (bad) return bad;
|
|
58
|
+
|
|
59
|
+
return apiCall("GET", storyRetrievalPath(story_id), null);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const bad = uuid(project_id, "project_id");
|
|
63
|
+
if (bad) return bad;
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
given(window_days) &&
|
|
67
|
+
!(Number.isInteger(window_days) && window_days >= 1 && window_days <= MAX_WINDOW_DAYS)
|
|
68
|
+
) {
|
|
69
|
+
return refuse(
|
|
70
|
+
`\`window_days\` must be an integer from 1 to ${MAX_WINDOW_DAYS} (default 30); ` +
|
|
71
|
+
"loopctl answers 400 for any other value.",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const qs = given(window_days) ? `?window_days=${window_days}` : "";
|
|
76
|
+
return apiCall("GET", projectRetrievalOutcomesPath(project_id, qs), null);
|
|
77
|
+
}
|
package/lib/tuning-drafts.js
CHANGED
|
@@ -81,8 +81,19 @@ export async function tuningDrafts(
|
|
|
81
81
|
return apiCall("GET", `/api/v1/tuning_drafts${qs ? `?${qs}` : ""}`, null);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
/**
|
|
85
|
-
export
|
|
84
|
+
/** The most characters `replay_evidence` may carry (loopctl US-55.5). */
|
|
85
|
+
export const MAX_REPLAY_EVIDENCE_CHARS = 4000;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* `POST /api/v1/tuning_drafts/:id/landed` with `{pr_url}`, and `replay_evidence` when given
|
|
89
|
+
* (loopctl US-55.5: required by the server for a draft of a replayable target, today `triage`).
|
|
90
|
+
* The local cap counts code points; the server counts graphemes, which are never more, so
|
|
91
|
+
* nothing this sends is over the server's cap.
|
|
92
|
+
*/
|
|
93
|
+
export async function tuningDraftMarkLanded(
|
|
94
|
+
{ draft_id, pr_url, replay_evidence } = {},
|
|
95
|
+
{ userKey, apiCall } = {},
|
|
96
|
+
) {
|
|
86
97
|
if (!userKey) return refuse(MISSING_USER_KEY);
|
|
87
98
|
|
|
88
99
|
if (typeof draft_id !== "string" || !UUID.test(draft_id)) {
|
|
@@ -93,5 +104,22 @@ export async function tuningDraftMarkLanded({ draft_id, pr_url } = {}, { userKey
|
|
|
93
104
|
return refuse("`pr_url` is required: https://github.com/<owner>/<repo>/pull/<n>.");
|
|
94
105
|
}
|
|
95
106
|
|
|
96
|
-
|
|
107
|
+
const hasEvidence = replay_evidence !== undefined && replay_evidence !== null;
|
|
108
|
+
|
|
109
|
+
if (
|
|
110
|
+
hasEvidence &&
|
|
111
|
+
!(
|
|
112
|
+
typeof replay_evidence === "string" &&
|
|
113
|
+
replay_evidence.trim() !== "" &&
|
|
114
|
+
[...replay_evidence].length <= MAX_REPLAY_EVIDENCE_CHARS
|
|
115
|
+
)
|
|
116
|
+
) {
|
|
117
|
+
return refuse(
|
|
118
|
+
`\`replay_evidence\` must be text of 1 to ${MAX_REPLAY_EVIDENCE_CHARS} characters: the ` +
|
|
119
|
+
"table `mix loopctl_runner.eval --replay-from` printed.",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const body = hasEvidence ? { pr_url, replay_evidence } : { pr_url };
|
|
124
|
+
return apiCall("POST", landedPath(draft_id), body);
|
|
97
125
|
}
|