@culture.dev/agents 0.15.2 → 0.16.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 +22 -0
- package/dist/contract-schemas/api.json +73 -15
- package/dist/contract-schemas/branches.json +163 -0
- package/dist/contract-schemas/capabilities.json +133 -6
- package/dist/contract-schemas/common.json +9 -3
- package/dist/contract-schemas/control-action.json +91 -14
- package/dist/contract-schemas/error.json +189 -48
- package/dist/contract-schemas/event.json +185 -2
- package/dist/contract-schemas/run.json +111 -4
- package/dist/contract-schemas/version.json +2 -2
- package/dist/src/contract/generated/branches.d.ts +34 -0
- package/dist/src/contract/generated/branches.js +3 -0
- package/dist/src/contract/generated/branches.js.map +1 -0
- package/dist/src/contract/generated/capabilities.d.ts +39 -7
- package/dist/src/contract/generated/common.d.ts +5 -3
- package/dist/src/contract/generated/control-action.d.ts +30 -11
- package/dist/src/contract/generated/error.d.ts +31 -7
- package/dist/src/contract/generated/event.d.ts +57 -8
- package/dist/src/contract/generated/index.d.ts +37 -12
- package/dist/src/contract/generated/run.d.ts +23 -5
- package/dist/src/contract/generated/version.d.ts +2 -2
- package/dist/src/contract/ids.d.ts +6 -3
- package/dist/src/contract/ids.js +5 -2
- package/dist/src/contract/ids.js.map +1 -1
- package/dist/src/contract/index.d.ts +1 -1
- package/dist/src/contract/index.js +1 -1
- package/dist/src/contract/index.js.map +1 -1
- package/dist/src/daemon/branch-routes.d.ts +13 -0
- package/dist/src/daemon/branch-routes.js +41 -0
- package/dist/src/daemon/branch-routes.js.map +1 -0
- package/dist/src/daemon/daemon.js +10 -2
- package/dist/src/daemon/daemon.js.map +1 -1
- package/dist/src/daemon/harness-catalog.d.ts +6 -0
- package/dist/src/daemon/harness-catalog.js +20 -0
- package/dist/src/daemon/harness-catalog.js.map +1 -1
- package/dist/src/daemon/limits.d.ts +31 -0
- package/dist/src/daemon/limits.js +61 -0
- package/dist/src/daemon/limits.js.map +1 -0
- package/dist/src/daemon/main.d.ts +10 -0
- package/dist/src/daemon/main.js +161 -41
- package/dist/src/daemon/main.js.map +1 -1
- package/dist/src/daemon/ports.d.ts +21 -11
- package/dist/src/daemon/routes.d.ts +3 -0
- package/dist/src/daemon/routes.js +26 -5
- package/dist/src/daemon/routes.js.map +1 -1
- package/dist/src/daemon/secrets.d.ts +24 -0
- package/dist/src/daemon/secrets.js +52 -0
- package/dist/src/daemon/secrets.js.map +1 -0
- package/dist/src/daemon/submitter.d.ts +9 -0
- package/dist/src/daemon/submitter.js +23 -0
- package/dist/src/daemon/submitter.js.map +1 -0
- package/dist/src/harness/claude-code/index.js +1 -1
- package/dist/src/harness/claude-code/index.js.map +1 -1
- package/dist/src/harness/common/capabilities.d.ts +26 -2
- package/dist/src/harness/common/capabilities.js +31 -6
- package/dist/src/harness/common/capabilities.js.map +1 -1
- package/dist/src/harness/common/guard.d.ts +2 -1
- package/dist/src/harness/common/guard.js +6 -0
- package/dist/src/harness/common/guard.js.map +1 -1
- package/dist/src/harness/common/run-driver.d.ts +6 -1
- package/dist/src/harness/common/run-driver.js +20 -3
- package/dist/src/harness/common/run-driver.js.map +1 -1
- package/dist/src/harness/common/sandbox-workspace.d.ts +5 -1
- package/dist/src/harness/common/sandbox-workspace.js +12 -3
- package/dist/src/harness/common/sandbox-workspace.js.map +1 -1
- package/dist/src/harness/common/types.d.ts +8 -0
- package/dist/src/harness/config.d.ts +5 -0
- package/dist/src/harness/config.js +7 -3
- package/dist/src/harness/config.js.map +1 -1
- package/dist/src/harness/index.js +2 -2
- package/dist/src/harness/index.js.map +1 -1
- package/dist/src/harness/opencode/index.js +1 -1
- package/dist/src/harness/opencode/index.js.map +1 -1
- package/dist/src/harness/stub/backend.d.ts +3 -0
- package/dist/src/harness/stub/backend.js +50 -1
- package/dist/src/harness/stub/backend.js.map +1 -1
- package/dist/src/harness/stub/index.d.ts +7 -1
- package/dist/src/harness/stub/index.js +10 -2
- package/dist/src/harness/stub/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/publish/config.d.ts +25 -0
- package/dist/src/publish/config.js +47 -0
- package/dist/src/publish/config.js.map +1 -0
- package/dist/src/publish/envelope.d.ts +61 -0
- package/dist/src/publish/envelope.js +109 -0
- package/dist/src/publish/envelope.js.map +1 -0
- package/dist/src/publish/index.d.ts +22 -0
- package/dist/src/publish/index.js +35 -0
- package/dist/src/publish/index.js.map +1 -0
- package/dist/src/publish/log-tail.d.ts +31 -0
- package/dist/src/publish/log-tail.js +172 -0
- package/dist/src/publish/log-tail.js.map +1 -0
- package/dist/src/publish/publisher.d.ts +34 -0
- package/dist/src/publish/publisher.js +207 -0
- package/dist/src/publish/publisher.js.map +1 -0
- package/dist/src/publish/transport.d.ts +14 -0
- package/dist/src/publish/transport.js +69 -0
- package/dist/src/publish/transport.js.map +1 -0
- package/dist/src/runs/admission.d.ts +27 -0
- package/dist/src/runs/admission.js +97 -0
- package/dist/src/runs/admission.js.map +1 -0
- package/dist/src/runs/answer.d.ts +24 -0
- package/dist/src/runs/answer.js +99 -0
- package/dist/src/runs/answer.js.map +1 -0
- package/dist/src/runs/base-commit.d.ts +6 -0
- package/dist/src/runs/base-commit.js +23 -0
- package/dist/src/runs/base-commit.js.map +1 -0
- package/dist/src/runs/branches.d.ts +23 -0
- package/dist/src/runs/branches.js +115 -0
- package/dist/src/runs/branches.js.map +1 -0
- package/dist/src/runs/control.d.ts +12 -4
- package/dist/src/runs/control.js +14 -6
- package/dist/src/runs/control.js.map +1 -1
- package/dist/src/runs/gateway.d.ts +25 -0
- package/dist/src/runs/gateway.js +122 -0
- package/dist/src/runs/gateway.js.map +1 -0
- package/dist/src/runs/keys.d.ts +21 -0
- package/dist/src/runs/keys.js +50 -0
- package/dist/src/runs/keys.js.map +1 -0
- package/dist/src/runs/limits.d.ts +15 -0
- package/dist/src/runs/limits.js +37 -0
- package/dist/src/runs/limits.js.map +1 -0
- package/dist/src/runs/ports.d.ts +3 -0
- package/dist/src/runs/ports.js.map +1 -1
- package/dist/src/runs/reattach.d.ts +37 -0
- package/dist/src/runs/reattach.js +70 -0
- package/dist/src/runs/reattach.js.map +1 -0
- package/dist/src/runs/scan.d.ts +11 -6
- package/dist/src/runs/scan.js +12 -15
- package/dist/src/runs/scan.js.map +1 -1
- package/dist/src/runs/service.d.ts +57 -7
- package/dist/src/runs/service.js +160 -95
- package/dist/src/runs/service.js.map +1 -1
- package/dist/src/runs/submit.d.ts +34 -0
- package/dist/src/runs/submit.js +92 -0
- package/dist/src/runs/submit.js.map +1 -0
- package/dist/src/sandbox/host-command.js +5 -1
- package/dist/src/sandbox/host-command.js.map +1 -1
- package/dist/src/sandbox/run-sandbox.d.ts +8 -2
- package/dist/src/sandbox/run-sandbox.js +10 -3
- package/dist/src/sandbox/run-sandbox.js.map +1 -1
- package/dist/src/store/derive.js +3 -0
- package/dist/src/store/derive.js.map +1 -1
- package/dist/src/store/entries.d.ts +29 -0
- package/dist/src/store/entries.js +41 -0
- package/dist/src/store/entries.js.map +1 -0
- package/dist/src/store/event-log.d.ts +3 -3
- package/dist/src/store/event-log.js +2 -2
- package/dist/src/store/event-log.js.map +1 -1
- package/dist/src/store/files.d.ts +4 -0
- package/dist/src/store/files.js +4 -0
- package/dist/src/store/files.js.map +1 -1
- package/dist/src/store/idempotency.d.ts +79 -0
- package/dist/src/store/idempotency.js +281 -0
- package/dist/src/store/idempotency.js.map +1 -0
- package/dist/src/store/index.d.ts +8 -0
- package/dist/src/store/index.js +5 -0
- package/dist/src/store/index.js.map +1 -1
- package/dist/src/store/questions.d.ts +40 -0
- package/dist/src/store/questions.js +90 -0
- package/dist/src/store/questions.js.map +1 -0
- package/dist/src/store/retention.d.ts +13 -0
- package/dist/src/store/retention.js +74 -0
- package/dist/src/store/retention.js.map +1 -0
- package/dist/src/store/run-store.d.ts +4 -0
- package/dist/src/store/run-store.js +20 -4
- package/dist/src/store/run-store.js.map +1 -1
- package/dist/src/store/startup.d.ts +9 -0
- package/dist/src/store/startup.js +92 -0
- package/dist/src/store/startup.js.map +1 -0
- package/dist/src/store/summary.d.ts +7 -0
- package/dist/src/store/summary.js +126 -0
- package/dist/src/store/summary.js.map +1 -0
- package/dist/src/store/version.d.ts +10 -0
- package/dist/src/store/version.js +46 -0
- package/dist/src/store/version.js.map +1 -0
- package/dist/src/trace/config.d.ts +31 -0
- package/dist/src/trace/config.js +69 -0
- package/dist/src/trace/config.js.map +1 -0
- package/dist/src/trace/follower.d.ts +20 -0
- package/dist/src/trace/follower.js +196 -0
- package/dist/src/trace/follower.js.map +1 -0
- package/dist/src/trace/index.d.ts +21 -0
- package/dist/src/trace/index.js +42 -0
- package/dist/src/trace/index.js.map +1 -0
- package/dist/src/trace/otlp.d.ts +11 -0
- package/dist/src/trace/otlp.js +52 -0
- package/dist/src/trace/otlp.js.map +1 -0
- package/dist/src/trace/spans.d.ts +28 -0
- package/dist/src/trace/spans.js +120 -0
- package/dist/src/trace/spans.js.map +1 -0
- package/dist/src/worker/answer.d.ts +29 -0
- package/dist/src/worker/answer.js +53 -0
- package/dist/src/worker/answer.js.map +1 -0
- package/dist/src/worker/asking.d.ts +22 -0
- package/dist/src/worker/asking.js +21 -0
- package/dist/src/worker/asking.js.map +1 -0
- package/dist/src/worker/control.d.ts +7 -1
- package/dist/src/worker/control.js +22 -10
- package/dist/src/worker/control.js.map +1 -1
- package/dist/src/worker/delivery.d.ts +2 -2
- package/dist/src/worker/delivery.js +1 -1
- package/dist/src/worker/delivery.js.map +1 -1
- package/dist/src/worker/driver.d.ts +46 -1
- package/dist/src/worker/fake-driver.d.ts +35 -1
- package/dist/src/worker/fake-driver.js +45 -0
- package/dist/src/worker/fake-driver.js.map +1 -1
- package/dist/src/worker/main.js +4 -1
- package/dist/src/worker/main.js.map +1 -1
- package/dist/src/worker/questions.d.ts +67 -0
- package/dist/src/worker/questions.js +152 -0
- package/dist/src/worker/questions.js.map +1 -0
- package/dist/src/worker/run-loop.d.ts +19 -1
- package/dist/src/worker/run-loop.js +91 -25
- package/dist/src/worker/run-loop.js.map +1 -1
- package/dist/src/worker/spawn.d.ts +9 -1
- package/dist/src/worker/spawn.js +12 -3
- package/dist/src/worker/spawn.js.map +1 -1
- package/package.json +3 -2
- package/policy/profiles/culture-agents-claude-subscription.yaml +1 -1
- package/policy/profiles/culture-agents-local-gateway.yaml +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"admission.js","sourceRoot":"","sources":["../../../src/runs/admission.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAIpD,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAsB,MAAM,cAAc,CAAC;AAEjF,sFAAsF;AACtF,MAAM,UAAU,aAAa,CAAC,GAAQ;IACpC,MAAM,QAAQ,GAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC;IAC5C,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC9B,MAAM,OAAO,GAAG,GAAG,CAAC,iBAAiB,CAAC,CAAC;IACvC,OAAO,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,eAAe,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACvE,CAAC;AAED,MAAM,WAAW,GAAG,CAAC,MAAc,EAAE,aAA4B,IAAI,EAAU,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;AAE3J,oEAAoE;AACpE,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,GAAS;IACrD,IAAI,GAAY,CAAC;IACjB,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC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E,gBAAgB,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,IAAI,0BAA0B,MAAM,CAAC,MAAM,IAAI,YAAY,+CAA+C,EAAE,CAAC,CAAC;QACjL,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `runs.answer`: steps 5 to 9 of the control order for an answer (contract README, "Order of
|
|
3
|
+
* checks" and "Operator questions"). The server has already checked the version header, the run
|
|
4
|
+
* id, that the run exists and the body.
|
|
5
|
+
*
|
|
6
|
+
* 5. the run's harness reports `answer` supported, else `unsupported` naming `answer`;
|
|
7
|
+
* 6. the run is `running` (else `conflict`) and the question, read from the run's log: never asked
|
|
8
|
+
* → `not_found` with `question_id`; already answered, or past its own limit → `conflict` with
|
|
9
|
+
* `question_id`, not retryable; an answer that does not fit the question → `invalid`;
|
|
10
|
+
* 7. live control, else `unavailable` with `retry_after_ms`;
|
|
11
|
+
* 8. delivery to the worker, which checks the question again (it is the log's only writer, so two
|
|
12
|
+
* answers racing each other get one acceptance and one `conflict`), delivers it into the turn and
|
|
13
|
+
* appends `run.answer`;
|
|
14
|
+
* 9. the accepted action record.
|
|
15
|
+
*
|
|
16
|
+
* Refusals at steps 5 and 6 that the contract's `run.refused` can carry (`unsupported`, `conflict`)
|
|
17
|
+
* are also written to the log of a live run this core controls. Idempotency keys are recorded on the
|
|
18
|
+
* action but not deduplicated for answers (README: idempotency keys); a repeated answer is refused
|
|
19
|
+
* with `conflict` because the question already has its answer, so it still takes effect once.
|
|
20
|
+
*/
|
|
21
|
+
import { type AcceptedResponse, type AnswerRequest } from "../contract/index.js";
|
|
22
|
+
import type { ControlTarget } from "./control.js";
|
|
23
|
+
/** Run steps 5 to 9 for one answer and return the accepted record, or throw the typed refusal. */
|
|
24
|
+
export declare function answer(target: ControlTarget, request: AnswerRequest, runDir: string): Promise<AcceptedResponse["action"]>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `runs.answer`: steps 5 to 9 of the control order for an answer (contract README, "Order of
|
|
3
|
+
* checks" and "Operator questions"). The server has already checked the version header, the run
|
|
4
|
+
* id, that the run exists and the body.
|
|
5
|
+
*
|
|
6
|
+
* 5. the run's harness reports `answer` supported, else `unsupported` naming `answer`;
|
|
7
|
+
* 6. the run is `running` (else `conflict`) and the question, read from the run's log: never asked
|
|
8
|
+
* → `not_found` with `question_id`; already answered, or past its own limit → `conflict` with
|
|
9
|
+
* `question_id`, not retryable; an answer that does not fit the question → `invalid`;
|
|
10
|
+
* 7. live control, else `unavailable` with `retry_after_ms`;
|
|
11
|
+
* 8. delivery to the worker, which checks the question again (it is the log's only writer, so two
|
|
12
|
+
* answers racing each other get one acceptance and one `conflict`), delivers it into the turn and
|
|
13
|
+
* appends `run.answer`;
|
|
14
|
+
* 9. the accepted action record.
|
|
15
|
+
*
|
|
16
|
+
* Refusals at steps 5 and 6 that the contract's `run.refused` can carry (`unsupported`, `conflict`)
|
|
17
|
+
* are also written to the log of a live run this core controls. Idempotency keys are recorded on the
|
|
18
|
+
* action but not deduplicated for answers (README: idempotency keys); a repeated answer is refused
|
|
19
|
+
* with `conflict` because the question already has its answer, so it still takes effect once.
|
|
20
|
+
*/
|
|
21
|
+
import { buildActionId, newUlid } from "../contract/index.js";
|
|
22
|
+
import { ControlError, unavailable, unsupported } from "../daemon/errors.js";
|
|
23
|
+
import { readLog } from "../store/event-log.js";
|
|
24
|
+
import { answerFields, answerFitError, questionExpired, questionLedger } from "../store/questions.js";
|
|
25
|
+
import { ControlUnavailableError } from "../worker/control.js";
|
|
26
|
+
const refusalOf = (code, message, runId, questionId) => new ControlError(code, message, { question_id: questionId, ...(code !== "invalid" && { run_id: runId }), ...(code === "conflict" && { retryable: false }) });
|
|
27
|
+
/** Steps 5 and 6: the refusal, or null. */
|
|
28
|
+
function check(target, request, runDir) {
|
|
29
|
+
const { run } = target;
|
|
30
|
+
const capability = target.capabilities?.capabilities.answer;
|
|
31
|
+
if (!capability?.supported) {
|
|
32
|
+
const why = target.capabilities === null ? `harness ${run.harness} is not known to this core` : (capability?.reason ?? "not supported");
|
|
33
|
+
return unsupported("answer", `The ${run.harness} harness does not support answer: ${why}`.slice(0, 2000));
|
|
34
|
+
}
|
|
35
|
+
if (run.state !== "running")
|
|
36
|
+
return new ControlError("conflict", `An answer needs a running run; run ${run.id} is ${run.state}.`, { run_id: run.id, retryable: false });
|
|
37
|
+
const qid = request.question_id;
|
|
38
|
+
const ledger = questionLedger(readLog(runDir, run.id).events);
|
|
39
|
+
const question = ledger.asked.get(qid);
|
|
40
|
+
if (question === undefined)
|
|
41
|
+
return refusalOf("not_found", `Run ${run.id} never asked question ${qid}.`, run.id, qid);
|
|
42
|
+
if (ledger.withdrawn.has(qid))
|
|
43
|
+
return refusalOf("conflict", `Question ${qid} was withdrawn by the harness.`, run.id, qid);
|
|
44
|
+
if (ledger.answered.has(qid))
|
|
45
|
+
return refusalOf("conflict", `Question ${qid} already has its answer; a question takes one.`, run.id, qid);
|
|
46
|
+
if (questionExpired(question, target.now().getTime()))
|
|
47
|
+
return refusalOf("conflict", `Question ${qid} passed its time limit at ${question.answer_by}.`, run.id, qid);
|
|
48
|
+
const misfit = answerFitError(question, answerFields(request));
|
|
49
|
+
return misfit === null ? null : refusalOf("invalid", misfit, run.id, qid);
|
|
50
|
+
}
|
|
51
|
+
function replyError(target, reply) {
|
|
52
|
+
if (reply.ok)
|
|
53
|
+
return null;
|
|
54
|
+
const body = reply.error;
|
|
55
|
+
const { code, message, ...rest } = body;
|
|
56
|
+
// The worker's own refusals (the question moved on, the run ended, no ask path) keep their code.
|
|
57
|
+
if (code === "conflict" || code === "not_found" || code === "invalid" || code === "unsupported")
|
|
58
|
+
return new ControlError(code, message, rest);
|
|
59
|
+
return unavailable(`The worker of run ${target.run.id} did not accept the answer: ${message}`);
|
|
60
|
+
}
|
|
61
|
+
/** Run steps 5 to 9 for one answer and return the accepted record, or throw the typed refusal. */
|
|
62
|
+
export async function answer(target, request, runDir) {
|
|
63
|
+
const { run } = target;
|
|
64
|
+
const action = {
|
|
65
|
+
id: buildActionId(run.agent_id, newUlid(target.now().getTime())),
|
|
66
|
+
run_id: run.id,
|
|
67
|
+
kind: "answer",
|
|
68
|
+
caller: request.caller,
|
|
69
|
+
requested_at: target.now().toISOString(),
|
|
70
|
+
idempotency_key: request.idempotency_key ?? null,
|
|
71
|
+
outcome: "accepted",
|
|
72
|
+
error: null,
|
|
73
|
+
};
|
|
74
|
+
const refusal = check(target, request, runDir);
|
|
75
|
+
if (refusal !== null) {
|
|
76
|
+
const code = refusal.body.code;
|
|
77
|
+
if ((code === "unsupported" || code === "conflict") && target.worker !== null && run.live_control && !run.terminal) {
|
|
78
|
+
await target.worker.refuse({ ...action, outcome: "refused", error: refusal.body }).catch(() => undefined);
|
|
79
|
+
}
|
|
80
|
+
throw refusal;
|
|
81
|
+
}
|
|
82
|
+
if (!run.live_control || target.worker === null) {
|
|
83
|
+
throw new ControlError("unavailable", `This core holds no control channel to the worker of run ${run.id} yet; retry shortly.`, { run_id: run.id, retry_after_ms: 500 });
|
|
84
|
+
}
|
|
85
|
+
let reply;
|
|
86
|
+
try {
|
|
87
|
+
reply = await target.worker.answer(action, request);
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
if (err instanceof ControlUnavailableError)
|
|
91
|
+
throw unavailable(`The worker of run ${run.id} did not answer: ${err.message}. The answer may still take effect; check the run's events before sending it again.`);
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
const failed = replyError(target, reply);
|
|
95
|
+
if (failed !== null)
|
|
96
|
+
throw failed;
|
|
97
|
+
return { ...action, outcome: "accepted" };
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=answer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"answer.js","sourceRoot":"","sources":["../../../src/runs/answer.ts"],"names":[],"mappings":"AAAA;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ACZ,CAAC;IACD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACzC,IAAI,MAAM,KAAK,IAAI;QAAE,MAAM,MAAM,CAAC;IAClC,OAAO,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type CommandRunner } from "../sandbox/host-command.js";
|
|
2
|
+
import type { Run } from "../contract/index.js";
|
|
3
|
+
/** The ref a driver was configured to start from (`HEAD` unless its options name another). */
|
|
4
|
+
export declare function startRef(options: unknown): string;
|
|
5
|
+
/** Resolve `ref` of `repo` to a full commit, or refuse the submit with `invalid` (field `repo`). */
|
|
6
|
+
export declare function resolveBaseCommit(repo: string, ref: string, runner?: CommandRunner): Promise<NonNullable<Run["base_commit"]>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commit a run starts from, resolved once when the submit is accepted (README, order of checks
|
|
3
|
+
* for runs.submit). The worker builds the sandbox from this commit, not from whatever the host's
|
|
4
|
+
* HEAD is by the time the sandbox is ready. The repository is only read.
|
|
5
|
+
*/
|
|
6
|
+
import { invalid } from "../daemon/errors.js";
|
|
7
|
+
import { hostEnv, runCommand } from "../sandbox/host-command.js";
|
|
8
|
+
/** The ref a driver was configured to start from (`HEAD` unless its options name another). */
|
|
9
|
+
export function startRef(options) {
|
|
10
|
+
const o = options;
|
|
11
|
+
const ref = o?.sandbox?.ref ?? o?.ref;
|
|
12
|
+
return typeof ref === "string" && ref !== "" ? ref : "HEAD";
|
|
13
|
+
}
|
|
14
|
+
/** Resolve `ref` of `repo` to a full commit, or refuse the submit with `invalid` (field `repo`). */
|
|
15
|
+
export async function resolveBaseCommit(repo, ref, runner = runCommand) {
|
|
16
|
+
const res = await runner("git", ["rev-parse", "--verify", "--quiet", "--end-of-options", `${ref}^{commit}`], { cwd: repo, env: hostEnv() });
|
|
17
|
+
const sha = res.stdout.trim();
|
|
18
|
+
if (res.code !== 0 || !/^[0-9a-f]{40}([0-9a-f]{24})?$/.test(sha)) {
|
|
19
|
+
throw invalid(`Repository ${repo} has no commit to start from: ${ref} does not resolve to a commit (a repository with no commits, or a ref that does not exist); commit once, then submit again.`, "repo");
|
|
20
|
+
}
|
|
21
|
+
return sha;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=base-commit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-commit.js","sourceRoot":"","sources":["../../../src/runs/base-commit.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAsB,MAAM,4BAA4B,CAAC;AAKrF,8FAA8F;AAC9F,MAAM,UAAU,QAAQ,CAAC,OAAgB;IACvC,MAAM,CAAC,GAAG,OAAkB,CAAC;IAC7B,MAAM,GAAG,GAAG,CAAC,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,EAAE,GAAG,CAAC;IACtC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AAC9D,CAAC;AAED,oGAAoG;AACpG,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,IAAY,EAAE,GAAW,EAAE,SAAwB,UAAU;IACnG,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,kBAAkB,EAAE,GAAG,GAAG,WAAW,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5I,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC9B,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,+BAA+B,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACjE,MAAM,OAAO,CAAC,cAAc,IAAI,iCAAiC,GAAG,6HAA6H,EAAE,MAAM,CAAC,CAAC;IAC7M,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run branches of one repository: list them with their run's state, and remove the finished ones
|
|
3
|
+
* that git shows reachable from another ref (contract/branches.json).
|
|
4
|
+
*
|
|
5
|
+
* Evidence is git's, not a guess: `git merge-base --is-ancestor <tip> <other ref>` must exit 0 for
|
|
6
|
+
* some ref that is not itself a `runs/` branch. Deletion is `git branch -d`, never `-D`, so git's
|
|
7
|
+
* own merge check stands behind ours; a refusal from git is reported as a skip. A branch whose
|
|
8
|
+
* run is active, unknown, unreadable or absent is never touched.
|
|
9
|
+
*/
|
|
10
|
+
import { type Branches, type RemoveResponse, type Run } from "../contract/index.js";
|
|
11
|
+
import { type CommandRunner } from "../sandbox/host-command.js";
|
|
12
|
+
/** What the branch code needs to know of one run; a rejection means the run cannot be read. */
|
|
13
|
+
export type RunLookup = (runId: string) => Promise<Run | null>;
|
|
14
|
+
export interface BranchOptions {
|
|
15
|
+
/** Canonical repository path (what a run's `repo` holds). */
|
|
16
|
+
repo: string;
|
|
17
|
+
lookup: RunLookup;
|
|
18
|
+
runner?: CommandRunner;
|
|
19
|
+
}
|
|
20
|
+
/** Every `runs/<run id>` branch of the repository, with its run's state and the reachability evidence. */
|
|
21
|
+
export declare function listBranches(options: BranchOptions): Promise<Branches>;
|
|
22
|
+
/** Delete what may be deleted; with `dryRun` only report it. Every branch lands in `removed` or `skipped`. */
|
|
23
|
+
export declare function removeBranches(options: BranchOptions, dryRun: boolean): Promise<RemoveResponse>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run branches of one repository: list them with their run's state, and remove the finished ones
|
|
3
|
+
* that git shows reachable from another ref (contract/branches.json).
|
|
4
|
+
*
|
|
5
|
+
* Evidence is git's, not a guess: `git merge-base --is-ancestor <tip> <other ref>` must exit 0 for
|
|
6
|
+
* some ref that is not itself a `runs/` branch. Deletion is `git branch -d`, never `-D`, so git's
|
|
7
|
+
* own merge check stands behind ours; a refusal from git is reported as a skip. A branch whose
|
|
8
|
+
* run is active, unknown, unreadable or absent is never touched.
|
|
9
|
+
*/
|
|
10
|
+
import { isActive, isRunId } from "../contract/index.js";
|
|
11
|
+
import { hostEnv, runCommand } from "../sandbox/host-command.js";
|
|
12
|
+
const PREFIX = "refs/heads/runs/";
|
|
13
|
+
async function git(options, args) {
|
|
14
|
+
return (options.runner ?? runCommand)("git", ["-C", options.repo, ...args], { env: hostEnv(), timeoutMs: 60_000 });
|
|
15
|
+
}
|
|
16
|
+
async function gitOrThrow(options, args) {
|
|
17
|
+
const res = await git(options, args);
|
|
18
|
+
if (res.code !== 0)
|
|
19
|
+
throw new Error(`git ${args.join(" ")} failed (exit ${res.code}): ${res.stderr.trim()}`);
|
|
20
|
+
return res.stdout;
|
|
21
|
+
}
|
|
22
|
+
async function viewRun(options, runId) {
|
|
23
|
+
try {
|
|
24
|
+
const run = await options.lookup(runId);
|
|
25
|
+
if (run === null)
|
|
26
|
+
return { state: null, note: "no run with this id is known to the core" };
|
|
27
|
+
if (run.repo !== options.repo)
|
|
28
|
+
return { state: null, note: `the run belongs to another repository (${run.repo})` };
|
|
29
|
+
return { state: run.state, note: null };
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
return { state: null, note: `the run cannot be read: ${err instanceof Error ? err.message : String(err)}` };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Why a branch whose run reads like this must not be deleted, or null when its run is finished. */
|
|
36
|
+
function runBlocker(view) {
|
|
37
|
+
if (view.state === null)
|
|
38
|
+
return view.note ?? "the run is not known";
|
|
39
|
+
if (isActive(view.state))
|
|
40
|
+
return `its run is active (${view.state})`;
|
|
41
|
+
if (view.state === "unknown")
|
|
42
|
+
return "its run's outcome is unknown, so its commits may not have been collected";
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
/** A remote-tracking copy of a run branch, under any remote: a pushed branch is not a second place its commits live. */
|
|
46
|
+
const isRemoteRunBranch = (ref) => /^refs\/remotes\/[^/]+\/runs\//.test(ref);
|
|
47
|
+
/** Refs a run branch's commits may be reachable from: local branches that are not run branches, and remote-tracking refs that are not copies of run branches. */
|
|
48
|
+
async function otherRefs(options) {
|
|
49
|
+
const out = await gitOrThrow(options, ["for-each-ref", "--format=%(refname)", "refs/heads", "refs/remotes"]);
|
|
50
|
+
return out.split("\n").filter((r) => r !== "" && !r.startsWith(PREFIX) && !isRemoteRunBranch(r) && !r.endsWith("/HEAD"));
|
|
51
|
+
}
|
|
52
|
+
const short = (ref) => ref.replace(/^refs\/(heads|remotes)\//, "");
|
|
53
|
+
async function mergedInto(options, tip, refs) {
|
|
54
|
+
for (const ref of refs) {
|
|
55
|
+
const res = await git(options, ["merge-base", "--is-ancestor", tip, ref]);
|
|
56
|
+
if (res.code === 0)
|
|
57
|
+
return short(ref);
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/** The branch of one `for-each-ref` line, or null when the line is not a run branch this core made. */
|
|
62
|
+
async function branchOf(options, line, refs) {
|
|
63
|
+
const [ref, tip] = line.split("\0");
|
|
64
|
+
if (!ref || !tip)
|
|
65
|
+
return null;
|
|
66
|
+
const name = ref.slice("refs/heads/".length);
|
|
67
|
+
const runId = name.slice("runs/".length);
|
|
68
|
+
if (!isRunId(runId))
|
|
69
|
+
return null; // not a branch this core made
|
|
70
|
+
const view = await viewRun(options, runId);
|
|
71
|
+
return { branch: name, run_id: runId, state: view.state, state_note: view.note, tip, merged_into: await mergedInto(options, tip, refs) };
|
|
72
|
+
}
|
|
73
|
+
/** Every `runs/<run id>` branch of the repository, with its run's state and the reachability evidence. */
|
|
74
|
+
export async function listBranches(options) {
|
|
75
|
+
const out = await gitOrThrow(options, ["for-each-ref", "--format=%(refname)%00%(objectname)", PREFIX.slice(0, -1)]);
|
|
76
|
+
const refs = await otherRefs(options);
|
|
77
|
+
// One line at a time, in order: each line's lookup and git commands start once the line before is done.
|
|
78
|
+
const branches = await out.split("\n").reduce(async (before, line) => {
|
|
79
|
+
const done = await before;
|
|
80
|
+
const branch = await branchOf(options, line, refs);
|
|
81
|
+
if (branch !== null)
|
|
82
|
+
done.push(branch);
|
|
83
|
+
return done;
|
|
84
|
+
}, Promise.resolve([]));
|
|
85
|
+
return { repo: options.repo, branches };
|
|
86
|
+
}
|
|
87
|
+
/** Delete what may be deleted; with `dryRun` only report it. Every branch lands in `removed` or `skipped`. */
|
|
88
|
+
export async function removeBranches(options, dryRun) {
|
|
89
|
+
const { branches } = await listBranches(options);
|
|
90
|
+
const result = { repo: options.repo, dry_run: dryRun, removed: [], skipped: [] };
|
|
91
|
+
// One branch at a time, in order: at most one `git branch -d` runs at once.
|
|
92
|
+
await branches.reduce((before, b) => before.then(() => settleBranch(options, dryRun, result, b)), Promise.resolve());
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
/** Put one branch in `removed` or `skipped`, deleting it first unless `dryRun`. */
|
|
96
|
+
async function settleBranch(options, dryRun, result, b) {
|
|
97
|
+
const skip = (reason) => {
|
|
98
|
+
result.skipped.push({ branch: b.branch, run_id: b.run_id, state: b.state, reason });
|
|
99
|
+
};
|
|
100
|
+
const blocker = runBlocker({ state: b.state, note: b.state_note });
|
|
101
|
+
if (blocker !== null)
|
|
102
|
+
skip(blocker);
|
|
103
|
+
else if (b.merged_into === null)
|
|
104
|
+
skip("git shows no other ref containing its commits (they would be lost)");
|
|
105
|
+
else if (dryRun)
|
|
106
|
+
result.removed.push(b);
|
|
107
|
+
else {
|
|
108
|
+
const res = await git(options, ["branch", "-d", "--", b.branch]);
|
|
109
|
+
if (res.code === 0)
|
|
110
|
+
result.removed.push(b);
|
|
111
|
+
else
|
|
112
|
+
skip(`git refused to delete it: ${(res.stderr || res.stdout).trim()}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=branches.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branches.js","sourceRoot":"","sources":["../../../src/runs/branches.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAA4E,MAAM,sBAAsB,CAAC;AACnI,OAAO,EAAE,OAAO,EAAE,UAAU,EAAsB,MAAM,4BAA4B,CAAC;AAErF,MAAM,MAAM,GAAG,kBAAkB,CAAC;AAiBlC,KAAK,UAAU,GAAG,CAAC,OAAsB,EAAE,IAAc;IACvD,OAAO,CAAC,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;AACrH,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,OAAsB,EAAE,IAAc;IAC9D,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACrC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,iBAAiB,GAAG,CAAC,IAAI,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAC7G,OAAO,GAAG,CAAC,MAAM,CAAC;AACpB,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,OAAsB,EAAE,KAAa;IAC1D,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACxC,IAAI,GAAG,KAAK,IAAI;YAAE,OAAO,EAAE,KAA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IAAI,GAAG,CAAC,IAAI,KAAK,CAAC;YAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;;YACtC,IAAI,CAAC,6BAA6B,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* 5. the run's harness capability document supports the action, else `unsupported` naming it;
|
|
6
6
|
* 6. the state allows it, else `conflict` (`steer` needs `running`; `cancel` needs an active state);
|
|
7
|
-
* 7. live control (
|
|
7
|
+
* 7. live control (rebuilt by reattach after a core restart), else `unavailable` with `retry_after_ms`;
|
|
8
8
|
* 8. delivery over the worker's control channel, else `unavailable`;
|
|
9
9
|
* 9. the accepted action record (the worker appends it to the log as `run.control`).
|
|
10
10
|
*
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
* The server decides step 4 (`pause`, `resume`, `answer`) and hands the refusal to
|
|
14
14
|
* `recordDeferredRefusal`. A refused action is never forwarded to the harness.
|
|
15
15
|
*/
|
|
16
|
-
import { type AcceptedResponse, type CancelRequest, type Capabilities, type ErrorBody, type Run, type SteerRequest } from "../contract/index.js";
|
|
16
|
+
import { type AcceptedResponse, type ActionRecord, type CancelRequest, type Capabilities, type ErrorBody, type Run, type SteerRequest } from "../contract/index.js";
|
|
17
|
+
import { ControlError } from "../daemon/errors.js";
|
|
17
18
|
import type { DeferredKind, DeferredRequest } from "../daemon/ports.js";
|
|
18
19
|
import type { WorkerControl } from "../worker/spawn.js";
|
|
19
20
|
export type ControlKind = "steer" | "cancel";
|
|
@@ -27,5 +28,12 @@ export interface ControlTarget {
|
|
|
27
28
|
}
|
|
28
29
|
/** Step 4: record the server's refusal of a deferred action (`pause`, `resume`, `answer`). Never throws. */
|
|
29
30
|
export declare function recordDeferredRefusal(kind: DeferredKind, target: Pick<ControlTarget, "run" | "worker" | "now">, request: DeferredRequest, error: ErrorBody): Promise<void>;
|
|
30
|
-
/**
|
|
31
|
-
export declare
|
|
31
|
+
/** The worker answered that it did not take the action: it did not take effect (the run service releases its key). */
|
|
32
|
+
export declare class WorkerRefusedError extends ControlError {
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Run steps 5 to 9 for one action and return the accepted record, or throw the typed refusal.
|
|
36
|
+
* `beforeDeliver` runs once the action passed every check, just before it is sent to the worker
|
|
37
|
+
* (the run service writes the action's idempotency key there).
|
|
38
|
+
*/
|
|
39
|
+
export declare function control(kind: ControlKind, target: ControlTarget, request: SteerRequest | CancelRequest, beforeDeliver?: (action: ActionRecord) => void): Promise<AcceptedResponse["action"]>;
|
package/dist/src/runs/control.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* 5. the run's harness capability document supports the action, else `unsupported` naming it;
|
|
6
6
|
* 6. the state allows it, else `conflict` (`steer` needs `running`; `cancel` needs an active state);
|
|
7
|
-
* 7. live control (
|
|
7
|
+
* 7. live control (rebuilt by reattach after a core restart), else `unavailable` with `retry_after_ms`;
|
|
8
8
|
* 8. delivery over the worker's control channel, else `unavailable`;
|
|
9
9
|
* 9. the accepted action record (the worker appends it to the log as `run.control`).
|
|
10
10
|
*
|
|
@@ -64,17 +64,24 @@ async function recordRefusal(target, action, error) {
|
|
|
64
64
|
export async function recordDeferredRefusal(kind, target, request, error) {
|
|
65
65
|
await recordRefusal(target, record(target.run, kind, request, target.now), error);
|
|
66
66
|
}
|
|
67
|
+
/** The worker answered that it did not take the action: it did not take effect (the run service releases its key). */
|
|
68
|
+
export class WorkerRefusedError extends ControlError {
|
|
69
|
+
}
|
|
67
70
|
function replyError(run, reply) {
|
|
68
71
|
if (reply.ok)
|
|
69
72
|
return null;
|
|
70
73
|
const body = reply.error;
|
|
71
74
|
// The worker re-checks state; a race (the run moved on) comes back as its own `conflict`.
|
|
72
75
|
if (body.code === "conflict")
|
|
73
|
-
return new
|
|
74
|
-
return unavailable
|
|
76
|
+
return new WorkerRefusedError("conflict", body.message, { run_id: run.id, retryable: body.retryable });
|
|
77
|
+
return new WorkerRefusedError("unavailable", `The worker of run ${run.id} did not accept the action: ${body.message}`);
|
|
75
78
|
}
|
|
76
|
-
/**
|
|
77
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Run steps 5 to 9 for one action and return the accepted record, or throw the typed refusal.
|
|
81
|
+
* `beforeDeliver` runs once the action passed every check, just before it is sent to the worker
|
|
82
|
+
* (the run service writes the action's idempotency key there).
|
|
83
|
+
*/
|
|
84
|
+
export async function control(kind, target, request, beforeDeliver) {
|
|
78
85
|
const { run } = target;
|
|
79
86
|
const action = record(run, kind, request, target.now);
|
|
80
87
|
const refusal = capabilityRefusal(kind, target) ?? stateRefusal(kind, run);
|
|
@@ -83,8 +90,9 @@ export async function control(kind, target, request) {
|
|
|
83
90
|
throw refusal;
|
|
84
91
|
}
|
|
85
92
|
if (!run.live_control || target.worker === null) {
|
|
86
|
-
throw
|
|
93
|
+
throw new ControlError("unavailable", `This core holds no control channel to the worker of run ${run.id} yet; retry shortly.`, { run_id: run.id, retry_after_ms: 500 });
|
|
87
94
|
}
|
|
95
|
+
beforeDeliver?.(action);
|
|
88
96
|
let reply;
|
|
89
97
|
try {
|
|
90
98
|
reply =
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"control.js","sourceRoot":"","sources":["../../../src/runs/control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EACL,aAAa,EAEb,QAAQ,EACR,OAAO,GAQR,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,EAAE,uBAAuB,EAAqB,MAAM,sBAAsB,CAAC;AAclF,SAAS,MAAM,CAAC,GAAQ,EAAE,IAAgB,EAAE,OAAuD,EAAE,GAAe;IAClH,OAAO;Q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CAAC,YAAY,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;QAChD,MAAM,IAAI,YAAY,CAAC,aAAa,EAAE,2DAA2D,GAAG,CAAC,EAAE,sBAAsB,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,EAAE,cAAc,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1K,CAAC;IACD,aAAa,EAAE,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,KAAmB,CAAC;IACxB,IAAI,CAAC;QACH,KAAK;YACH,IAAI,KAAK,OAAO;gBACd,CAAC,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAG,OAAwB,CAAC,OAAO,CAAC;gBACtE,CAAC,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAG,OAAyB,CAAC,MAAM,CAAC,CAAC;IAC9E,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,uBAAuB;YAAE,MAAM,WAAW,CAAC,qBAAqB,GAAG,CAAC,EAAE,oBAAoB,GAAG,CAAC,OAAO,SAAS,IAAI,yEAAyE,CAAC,CAAC;QAChN,MAAM,GAAG,CAAC;IACZ,CAAC;IACD,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACtC,IAAI,MAAM,KAAK,IAAI;QAAE,MAAM,MAAM,CAAC;IAClC,OAAO,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model gateway's admission signal (lobes-cli 0.99.0). `GET /status` answers with an
|
|
3
|
+
* `admission` object: `admit` (boolean), `at` (RFC 3339), `stale_after_s` (number) and an optional
|
|
4
|
+
* `reason`. It is read with Node's fetch (injectable), under a short timeout, from the origin of
|
|
5
|
+
* the gateway URL the OpenCode harness is configured with. Anything but a fresh, well-formed
|
|
6
|
+
* sample reads unknown, never admit: a missing field, a stale or future-stamped sample, a timeout,
|
|
7
|
+
* a refused connection, a bad status, or a redirect to anywhere but loopback, or a configured host that is not loopback (no request is sent). The body is read
|
|
8
|
+
* with a 64 KiB cap, content-length included. NOT VERIFIED against
|
|
9
|
+
* a live gateway from the build sandbox (no network); the live row is in part-one-acceptance.md.
|
|
10
|
+
*/
|
|
11
|
+
import type { Signal } from "../contract/index.js";
|
|
12
|
+
export declare const GATEWAY_TIMEOUT_MS = 1000;
|
|
13
|
+
export interface GatewaySource {
|
|
14
|
+
/** Catalog ids of the harnesses that use the gateway. */
|
|
15
|
+
harnesses: readonly string[];
|
|
16
|
+
/** The gateway URL the harness is configured with; `/status` is asked at its origin. */
|
|
17
|
+
url: string;
|
|
18
|
+
fetch?: typeof fetch;
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
}
|
|
21
|
+
export declare const GATEWAY_NOT_USED = "not consulted: this harness does not use the model gateway";
|
|
22
|
+
/** Interpret one `/status` body at `now`. */
|
|
23
|
+
export declare function gatewayFromPayload(text: string, now: Date): Signal;
|
|
24
|
+
/** Read the gateway signal. Never throws and never waits longer than the timeout. Concurrent reads of one source share a request. */
|
|
25
|
+
export declare function readGateway(source: GatewaySource, now: Date): Promise<Signal>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export const GATEWAY_TIMEOUT_MS = 1000;
|
|
2
|
+
const FUTURE_SKEW_MS = 5000;
|
|
3
|
+
const MAX_BODY = 65536;
|
|
4
|
+
const MAX_HOPS = 3;
|
|
5
|
+
export const GATEWAY_NOT_USED = "not consulted: this harness does not use the model gateway";
|
|
6
|
+
const unknown = (reason, observedAt = null) => ({ state: "unknown", consulted: true, reason, observed_at: observedAt });
|
|
7
|
+
const RFC3339 = /^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(\.\d+)?([Zz]|[+-]\d{2}:\d{2})$/;
|
|
8
|
+
/** Interpret one `/status` body at `now`. */
|
|
9
|
+
export function gatewayFromPayload(text, now) {
|
|
10
|
+
let doc;
|
|
11
|
+
try {
|
|
12
|
+
doc = JSON.parse(text);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return unknown("the gateway's status is not valid JSON");
|
|
16
|
+
}
|
|
17
|
+
const root = doc !== null && typeof doc === "object" && !Array.isArray(doc) ? doc : null;
|
|
18
|
+
const a = root?.["admission"];
|
|
19
|
+
const o = a !== null && typeof a === "object" && !Array.isArray(a) ? a : null;
|
|
20
|
+
const at = o?.["at"];
|
|
21
|
+
const window = o?.["stale_after_s"];
|
|
22
|
+
if (o === null || typeof o["admit"] !== "boolean" || typeof at !== "string" || !RFC3339.test(at) || typeof window !== "number" || !Number.isFinite(window) || window <= 0) {
|
|
23
|
+
return unknown("the gateway's status lacks an admission object with a boolean admit, an RFC 3339 at and a positive stale_after_s");
|
|
24
|
+
}
|
|
25
|
+
const observed = new Date(at);
|
|
26
|
+
if (Number.isNaN(observed.getTime()))
|
|
27
|
+
return unknown("the gateway's admission has an unusable at");
|
|
28
|
+
const observedAt = observed.toISOString();
|
|
29
|
+
const ageMs = now.getTime() - observed.getTime();
|
|
30
|
+
if (ageMs > window * 1000)
|
|
31
|
+
return unknown(`the gateway reading is stale: ${Math.round(ageMs / 1000)}s old, stale after ${window}s`, observedAt);
|
|
32
|
+
if (ageMs < -FUTURE_SKEW_MS)
|
|
33
|
+
return unknown("the gateway reading is stamped in the future", observedAt);
|
|
34
|
+
if (o["admit"])
|
|
35
|
+
return { state: "admit", consulted: true, reason: null, observed_at: observedAt };
|
|
36
|
+
const why = typeof o["reason"] === "string" && o["reason"] !== "" ? o["reason"].slice(0, 200) : "no reason given";
|
|
37
|
+
return { state: "hold", consulted: true, reason: `the gateway says hold (${why})`, observed_at: observedAt };
|
|
38
|
+
}
|
|
39
|
+
const isLoopback = (u) => u.hostname === "localhost" || u.hostname === "[::1]" || /^127\.\d+\.\d+\.\d+$/.test(u.hostname);
|
|
40
|
+
/** Where the redirect `res` from `url` points; throws unless that is a loopback http or https URL. */
|
|
41
|
+
function redirectTarget(res, url) {
|
|
42
|
+
const loc = res.headers.get("location");
|
|
43
|
+
let next = null;
|
|
44
|
+
try {
|
|
45
|
+
next = loc ? new URL(loc, url) : null;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
next = null;
|
|
49
|
+
}
|
|
50
|
+
if (next === null || !isLoopback(next) || (next.protocol !== "http:" && next.protocol !== "https:"))
|
|
51
|
+
throw new Error("redirect to a non-loopback address");
|
|
52
|
+
return next;
|
|
53
|
+
}
|
|
54
|
+
/** The body of `res` as text, read from the stream and abandoned as soon as it passes MAX_BODY bytes. */
|
|
55
|
+
async function readCapped(res) {
|
|
56
|
+
const declared = Number(res.headers.get("content-length"));
|
|
57
|
+
if (Number.isFinite(declared) && declared > MAX_BODY) {
|
|
58
|
+
await res.body?.cancel().catch(() => undefined);
|
|
59
|
+
throw new Error("body too large");
|
|
60
|
+
}
|
|
61
|
+
if (res.body === null)
|
|
62
|
+
return "";
|
|
63
|
+
const chunks = [];
|
|
64
|
+
let total = 0;
|
|
65
|
+
// Leaving the loop by the throw ends the iteration, which cancels the stream: no more is read.
|
|
66
|
+
for await (const chunk of res.body) {
|
|
67
|
+
total += chunk.length;
|
|
68
|
+
if (total > MAX_BODY)
|
|
69
|
+
throw new Error("body too large");
|
|
70
|
+
chunks.push(chunk);
|
|
71
|
+
}
|
|
72
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
73
|
+
}
|
|
74
|
+
async function fetchStatus(source, signal) {
|
|
75
|
+
const doFetch = source.fetch ?? fetch;
|
|
76
|
+
let url = new URL("/status", source.url);
|
|
77
|
+
for (let hop = 0; hop <= MAX_HOPS; hop++) {
|
|
78
|
+
const res = await doFetch(url, { redirect: "manual", signal, headers: { accept: "application/json" } });
|
|
79
|
+
if (res.status >= 300 && res.status < 400) {
|
|
80
|
+
url = redirectTarget(res, url);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (!res.ok)
|
|
84
|
+
throw new Error(`status ${res.status}`);
|
|
85
|
+
return readCapped(res);
|
|
86
|
+
}
|
|
87
|
+
throw new Error("too many redirects");
|
|
88
|
+
}
|
|
89
|
+
/** The core reads the gateway's status on loopback only; any other host is not asked at all. */
|
|
90
|
+
function notOnThisHost(url) {
|
|
91
|
+
let host = null;
|
|
92
|
+
try {
|
|
93
|
+
host = new URL(url);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
host = null;
|
|
97
|
+
}
|
|
98
|
+
if (host !== null && isLoopback(host))
|
|
99
|
+
return null;
|
|
100
|
+
return unknown("the gateway is not on this host: its status is read on loopback only, so no request was sent");
|
|
101
|
+
}
|
|
102
|
+
const inflight = new WeakMap();
|
|
103
|
+
/** Read the gateway signal. Never throws and never waits longer than the timeout. Concurrent reads of one source share a request. */
|
|
104
|
+
export function readGateway(source, now) {
|
|
105
|
+
const off = notOnThisHost(source.url);
|
|
106
|
+
if (off !== null)
|
|
107
|
+
return Promise.resolve(off);
|
|
108
|
+
const running = inflight.get(source);
|
|
109
|
+
if (running)
|
|
110
|
+
return running;
|
|
111
|
+
const ms = source.timeoutMs ?? GATEWAY_TIMEOUT_MS;
|
|
112
|
+
const controller = new AbortController();
|
|
113
|
+
let timer;
|
|
114
|
+
const deadline = new Promise((resolve) => {
|
|
115
|
+
timer = setTimeout(() => { controller.abort(); resolve(unknown(`the gateway did not answer within ${ms} ms`)); }, ms);
|
|
116
|
+
});
|
|
117
|
+
const read = fetchStatus(source, controller.signal).then((text) => gatewayFromPayload(text, now), (err) => unknown(`the gateway could not be read: ${err instanceof Error && /redirect/.test(err.message) ? err.message : "unreachable, refused or an error status"}`));
|
|
118
|
+
const p = Promise.race([read, deadline]).finally(() => { clearTimeout(timer); inflight.delete(source); });
|
|
119
|
+
inflight.set(source, p);
|
|
120
|
+
return p;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +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