@parcha/agentrun-serve 0.1.0-beta.29 → 0.1.0-beta.30
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/CHANGELOG.md +188 -0
- package/README.md +564 -97
- package/dist/adapters.d.ts +35 -6
- package/dist/adapters.js +152 -26
- package/dist/agents.d.ts +11 -5
- package/dist/agents.js +16 -10
- package/dist/app.js +80 -20
- package/dist/background.d.ts +28 -0
- package/dist/background.js +376 -0
- package/dist/box-egress.d.ts +98 -0
- package/dist/box-egress.js +259 -0
- package/dist/box-image.d.ts +1 -1
- package/dist/box-image.js +9 -7
- package/dist/box-relay-vm.d.ts +80 -0
- package/dist/box-relay-vm.js +389 -0
- package/dist/box-relay.d.ts +8 -5
- package/dist/box-relay.js +14 -9
- package/dist/boxes.d.ts +21 -3
- package/dist/boxes.js +14 -7
- package/dist/browser-cost.d.ts +41 -0
- package/dist/browser-cost.js +158 -0
- package/dist/cap.d.ts +129 -21
- package/dist/cap.js +338 -67
- package/dist/coding-tools.d.ts +7 -3
- package/dist/coding-tools.js +16 -10
- package/dist/console/THIRD-PARTY-NOTICES.txt +580 -0
- package/dist/console/assets/index-34jfpIbQ.js +481 -0
- package/dist/console/assets/index-DUHbWcIP.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/content-filter.d.ts +7 -0
- package/dist/content-filter.js +23 -0
- package/dist/decision-rows.d.ts +11 -3
- package/dist/decision-rows.js +18 -11
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +5 -2
- package/dist/files.d.ts +6 -1
- package/dist/files.js +7 -2
- package/dist/grade.d.ts +7 -10
- package/dist/grade.js +31 -51
- package/dist/graph-served.d.ts +23 -0
- package/dist/graph-served.js +75 -0
- package/dist/graph-view.d.ts +7 -5
- package/dist/graph-view.js +10 -3
- package/dist/harness-app.d.ts +4 -2
- package/dist/harness-app.js +101 -19
- package/dist/harness-opencode/adapter.d.ts +45 -0
- package/dist/harness-opencode/adapter.js +272 -0
- package/dist/harness-opencode/binary.d.ts +18 -0
- package/dist/harness-opencode/binary.js +87 -0
- package/dist/harness-opencode/config.d.ts +24 -0
- package/dist/harness-opencode/config.js +61 -0
- package/dist/harness-opencode/harness.d.ts +77 -0
- package/dist/harness-opencode/harness.js +620 -0
- package/dist/harness-opencode/host/mcp.d.ts +40 -0
- package/dist/harness-opencode/host/mcp.js +208 -0
- package/dist/harness-opencode/host/policy.d.ts +71 -0
- package/dist/harness-opencode/host/policy.js +419 -0
- package/dist/harness-opencode/host/server.d.ts +55 -0
- package/dist/harness-opencode/host/server.js +165 -0
- package/dist/harness-opencode/index.d.ts +9 -0
- package/dist/harness-opencode/index.js +6 -0
- package/dist/harness-opencode/verify.d.ts +32 -0
- package/dist/harness-opencode/verify.js +75 -0
- package/dist/harness-opencode/versions.d.ts +33 -0
- package/dist/harness-opencode/versions.js +64 -0
- package/dist/harness.d.ts +4 -1
- package/dist/harness.js +5 -2
- package/dist/host-tools.d.ts +12 -2
- package/dist/host-tools.js +26 -12
- package/dist/host.js +6 -2
- package/dist/hosted-search.js +5 -1
- package/dist/import.d.ts +2 -2
- package/dist/import.js +4 -3
- package/dist/key-check.d.ts +3 -2
- package/dist/key-check.js +7 -3
- package/dist/key-refusal.js +8 -4
- package/dist/learn/check.d.ts +10 -1
- package/dist/learn/check.js +19 -1
- package/dist/learn/grade.d.ts +3 -2
- package/dist/learn/grade.js +25 -13
- package/dist/learn/index.d.ts +2 -2
- package/dist/learn/index.js +2 -2
- package/dist/learn/notes.js +2 -1
- package/dist/learn/reflect.d.ts +2 -2
- package/dist/learn/reflect.js +51 -5
- package/dist/learn/secrets.js +28 -8
- package/dist/learn/skills.d.ts +17 -4
- package/dist/learn/skills.js +79 -6
- package/dist/learned-workflow.d.ts +59 -0
- package/dist/learned-workflow.js +191 -0
- package/dist/learning-run.d.ts +7 -5
- package/dist/learning-run.js +45 -23
- package/dist/learning-words.d.ts +6 -3
- package/dist/learning-words.js +8 -5
- package/dist/learning.d.ts +44 -3
- package/dist/learning.js +148 -3
- package/dist/mcp-servers.d.ts +7 -4
- package/dist/mcp-servers.js +5 -0
- package/dist/mcp.js +5 -5
- package/dist/model.d.ts +13 -11
- package/dist/model.js +11 -10
- package/dist/openapi/agentrun-v2.openapi.json +1 -0
- package/dist/openapi.js +358 -48
- package/dist/paid-calls.d.ts +5 -1
- package/dist/paid-calls.js +1 -1
- package/dist/pause.d.ts +2 -0
- package/dist/place.d.ts +27 -4
- package/dist/place.js +42 -10
- package/dist/propose.d.ts +2 -0
- package/dist/propose.js +38 -8
- package/dist/request-start.d.ts +12 -4
- package/dist/request-start.js +139 -15
- package/dist/run-box.d.ts +56 -4
- package/dist/run-box.js +173 -23
- package/dist/runnable.d.ts +5 -2
- package/dist/runnable.js +12 -4
- package/dist/runs.d.ts +258 -36
- package/dist/runs.js +830 -89
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +18 -0
- package/dist/server.d.ts +131 -35
- package/dist/server.js +467 -105
- package/dist/sign-ins.d.ts +99 -0
- package/dist/sign-ins.js +288 -0
- package/dist/skills.d.ts +16 -4
- package/dist/skills.js +35 -7
- package/dist/stats.d.ts +7 -2
- package/dist/stats.js +28 -7
- package/dist/tool-gate.d.ts +266 -0
- package/dist/tool-gate.js +554 -0
- package/dist/tool-keys.d.ts +46 -10
- package/dist/tool-keys.js +44 -2
- package/dist/tool-plane.d.ts +112 -27
- package/dist/tool-plane.js +205 -47
- package/dist/tools.d.ts +69 -14
- package/dist/tools.js +143 -24
- package/dist/trigger-secrets.d.ts +32 -0
- package/dist/trigger-secrets.js +143 -0
- package/dist/triggers.d.ts +439 -0
- package/dist/triggers.js +863 -0
- package/dist/turn-end.d.ts +3 -2
- package/dist/turn-end.js +3 -2
- package/dist/usage-lines.d.ts +26 -0
- package/dist/usage-lines.js +28 -0
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +11 -2
- package/dist/webhooks.d.ts +11 -4
- package/dist/webhooks.js +17 -1
- package/launcher/agentrun-serve +1 -1
- package/package.json +19 -13
- package/dist/console/assets/index-BN7oVcO4.js +0 -445
- package/dist/console/assets/index-By-jkqki.css +0 -1
package/dist/paid-calls.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type ToolExecutionApi, type ToolRegistration } from "@earendil-works/pi-durable";
|
|
2
3
|
/** Parallel's published prices (https://docs.parallel.ai/getting-started/pricing, read 2026-11-03): a `fast` search
|
|
3
4
|
* $1 per 1,000 requests for up to 10 results, each further result $1 per 1,000; an extract $1 per 1,000 URLs. */
|
|
4
5
|
export declare const PARALLEL_SEARCH_USD = 0.001;
|
|
@@ -35,6 +36,9 @@ export declare const settleIn: (record: Ledger["record"] | undefined) => void;
|
|
|
35
36
|
/** A settle of `kind` written to the run's root (settleIn), when that is set; else undefined, for the caller's own commit.
|
|
36
37
|
* monid.run's settle goes here too. */
|
|
37
38
|
export declare const settleOnRoot: (kind: string, data: Record<string, string | number | null>) => Promise<void> | undefined;
|
|
39
|
+
/** The ledger of a tool call: its id, and its commits to the run's store. Its mark (TOOL_STARTED) is the call's own
|
|
40
|
+
* commit, so a stopped turn sends nothing new; its settle goes to the run's root when that is set (settleIn). */
|
|
41
|
+
export declare const ledgerOf: (api: ToolExecutionApi, context: Context) => Ledger;
|
|
38
42
|
/** `tool` with each call's paid requests booked under it: its execute runs with the call's ledger set. */
|
|
39
43
|
export declare function booking<T extends ToolRegistration>(tool: T): T;
|
|
40
44
|
/** `work` with its paid requests booked in `ledger` (a workflow call node's); with none, booked nowhere. */
|
package/dist/paid-calls.js
CHANGED
|
@@ -61,7 +61,7 @@ export const settleIn = (record) => {
|
|
|
61
61
|
export const settleOnRoot = (kind, data) => rootRecord?.(kind, data);
|
|
62
62
|
/** The ledger of a tool call: its id, and its commits to the run's store. Its mark (TOOL_STARTED) is the call's own
|
|
63
63
|
* commit, so a stopped turn sends nothing new; its settle goes to the run's root when that is set (settleIn). */
|
|
64
|
-
const ledgerOf = (api, context) => ({ call: api.callId, record: (kind, data) => ((kind === TOOL_COST ? settleOnRoot(kind, data) : undefined) ?? api.commit(async (tx) => void (await tx.appendEntry(ROOT_CONVERSATION_ID, { kind, data })), context)) });
|
|
64
|
+
export const ledgerOf = (api, context) => ({ call: api.callId, record: (kind, data) => ((kind === TOOL_COST ? settleOnRoot(kind, data) : undefined) ?? api.commit(async (tx) => void (await tx.appendEntry(ROOT_CONVERSATION_ID, { kind, data })), context)) });
|
|
65
65
|
/** `tool` with each call's paid requests booked under it: its execute runs with the call's ledger set. */
|
|
66
66
|
export function booking(tool) {
|
|
67
67
|
return { ...tool, execute: (args, api, context) => ledgers.run(ledgerOf(api, context), () => tool.execute(args, api, context)) };
|
package/dist/pause.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export type NodeLine = {
|
|
|
31
31
|
status: "done" | "failed";
|
|
32
32
|
output?: unknown;
|
|
33
33
|
error?: string;
|
|
34
|
+
error_truncated?: boolean;
|
|
34
35
|
at: string;
|
|
35
36
|
};
|
|
36
37
|
/** Appends to the run's node log; a write that fails is lost, never the run's. */
|
|
@@ -47,6 +48,7 @@ export declare const llmAnswers: (lines: readonly NodeLine[]) => {
|
|
|
47
48
|
status: "done" | "failed";
|
|
48
49
|
output?: unknown;
|
|
49
50
|
error?: string;
|
|
51
|
+
error_truncated?: boolean;
|
|
50
52
|
at: string;
|
|
51
53
|
}[];
|
|
52
54
|
/** What the run's process last wrote of its driver's frame and its receipts. */
|
package/dist/place.d.ts
CHANGED
|
@@ -9,23 +9,46 @@ export type Place = {
|
|
|
9
9
|
mode: "sandbox";
|
|
10
10
|
box: BoxId;
|
|
11
11
|
};
|
|
12
|
-
/** The boxes runs execute in so far. */
|
|
12
|
+
/** The boxes runs execute in so far: Docker, Daytona and the VM box. */
|
|
13
13
|
export declare const RUN_BOXES: readonly BoxId[];
|
|
14
14
|
/** The boxes this server gives runs, from what its registry set up at its start (boxes.ts): a box runs execute in
|
|
15
15
|
* that the registry has, and no other. */
|
|
16
16
|
export declare function boxAvailability(registry: readonly BoxOffer[]): BoxOffer[];
|
|
17
|
+
/** The place a new agentic definition that names none is saved with: the Docker box where this server has it, ready or
|
|
18
|
+
* preparing (its first run waits for the image); else none, so the agent runs on the runner as it did before, with
|
|
19
|
+
* `reason` saying why, in docker's own words. A workflow is never given one. */
|
|
20
|
+
export declare function newAgentPlace(registry: readonly BoxOffer[]): {
|
|
21
|
+
place: Place;
|
|
22
|
+
} | {
|
|
23
|
+
reason: string;
|
|
24
|
+
};
|
|
25
|
+
/** The tools a new agent in the default box is given when its definition names none: a shell. A definition that names its
|
|
26
|
+
* tools, an empty list included, keeps exactly those. */
|
|
27
|
+
export declare const NEW_AGENT_TOOLS: readonly string[];
|
|
17
28
|
/** Why a sandbox-only tool (bash) cannot run on this server: no box runs execute in is available, each with its reason.
|
|
18
29
|
* Undefined when one is. */
|
|
19
30
|
export declare function noBoxReason(registry: readonly BoxOffer[]): string | undefined;
|
|
20
31
|
/** The start banner's words on the boxes: which are ready for a sandbox agent's tools, which are still preparing (an
|
|
21
32
|
* available box whose `state` says so, with its reason: what it is pulling), then why each box is not that
|
|
22
33
|
* AGENTRUN_BOXES lists (`listed`), or, when none is ready or preparing, each box runs execute in, with what to do. */
|
|
23
|
-
export declare function boxesLine(registry: readonly BoxOffer[], listed: ReadonlySet<BoxId
|
|
24
|
-
/** `places` in capabilities: the modes a revision may name,
|
|
25
|
-
|
|
34
|
+
export declare function boxesLine(registry: readonly BoxOffer[], listed: ReadonlySet<BoxId>, allow?: readonly string[] | "none"): string;
|
|
35
|
+
/** `places` in capabilities: the modes a revision may name, where a revision that names none runs (`default`: the runner,
|
|
36
|
+
* for a revision saved with no place), the place a new agent is saved with (`default_place`, with `default_reason` when
|
|
37
|
+
* it is the runner), each box, and the hosts a box whose `network` is `allowlist` reaches (`allow`, run-box.ts
|
|
38
|
+
* boxAllowOf; empty for `none`). */
|
|
39
|
+
export declare const placesOf: (registry: readonly BoxOffer[], allow?: readonly string[] | "none") => {
|
|
40
|
+
boxes: BoxOffer[];
|
|
41
|
+
allow: string[];
|
|
42
|
+
default_place: Place;
|
|
26
43
|
modes: ("runner" | "sandbox")[];
|
|
27
44
|
default: "runner";
|
|
45
|
+
} | {
|
|
28
46
|
boxes: BoxOffer[];
|
|
47
|
+
allow: string[];
|
|
48
|
+
default_place: Place;
|
|
49
|
+
default_reason: string;
|
|
50
|
+
modes: ("runner" | "sandbox")[];
|
|
51
|
+
default: "runner";
|
|
29
52
|
};
|
|
30
53
|
/** A run's place: its revision's, else this host's default. */
|
|
31
54
|
export declare const resolvePlace: (revision: Place | undefined) => Place;
|
package/dist/place.js
CHANGED
|
@@ -6,13 +6,18 @@
|
|
|
6
6
|
// default, and keeps the place it resolved to: a run request names none. Runs execute in Docker and in Daytona, for
|
|
7
7
|
// now: every other box says it does not run yet. A harness's own tools in the box, when they come, are an option of
|
|
8
8
|
// sandbox, never a third mode.
|
|
9
|
+
//
|
|
10
|
+
// The box is a new agent's default machine: an agentic definition that names no place is saved, at its create, with the
|
|
11
|
+
// Docker box as its place wherever this server has docker (ready, or preparing), and with bash when it names no tools
|
|
12
|
+
// (newAgentPlace). The place is written on the revision then, never resolved at run time, so a revision saved with none
|
|
13
|
+
// (before this, or where docker was not there) runs on the runner, whatever the server has now.
|
|
9
14
|
import { join } from "node:path";
|
|
10
|
-
import { BOX_IDS } from "./boxes.js";
|
|
15
|
+
import { BOX_IDS, VM_PROVIDER } from "./boxes.js";
|
|
11
16
|
import { sentence } from "./errors.js";
|
|
12
17
|
export const PLACE_MODES = ["runner", "sandbox"];
|
|
13
18
|
export const DEFAULT_PLACE_MODE = "runner";
|
|
14
|
-
/** The boxes runs execute in so far. */
|
|
15
|
-
export const RUN_BOXES = ["docker", "daytona"];
|
|
19
|
+
/** The boxes runs execute in so far: Docker, Daytona and the VM box. */
|
|
20
|
+
export const RUN_BOXES = ["docker", "daytona", VM_PROVIDER.id];
|
|
16
21
|
/** The boxes this server gives runs, from what its registry set up at its start (boxes.ts): a box runs execute in
|
|
17
22
|
* that the registry has, and no other. */
|
|
18
23
|
export function boxAvailability(registry) {
|
|
@@ -25,6 +30,18 @@ export function boxAvailability(registry) {
|
|
|
25
30
|
return { id, available: false, reason: `runs in the ${id} box are not available yet: use the ${RUN_BOXES.join(" or ")} box` };
|
|
26
31
|
});
|
|
27
32
|
}
|
|
33
|
+
/** The place a new agentic definition that names none is saved with: the Docker box where this server has it, ready or
|
|
34
|
+
* preparing (its first run waits for the image); else none, so the agent runs on the runner as it did before, with
|
|
35
|
+
* `reason` saying why, in docker's own words. A workflow is never given one. */
|
|
36
|
+
export function newAgentPlace(registry) {
|
|
37
|
+
const docker = boxAvailability(registry).find((box) => box.id === "docker");
|
|
38
|
+
if (docker?.available)
|
|
39
|
+
return { place: { mode: "sandbox", box: "docker" } };
|
|
40
|
+
return { reason: `new agents run on the runner, because the docker box is unavailable here: ${docker?.reason ?? "it is not set up"}` };
|
|
41
|
+
}
|
|
42
|
+
/** The tools a new agent in the default box is given when its definition names none: a shell. A definition that names its
|
|
43
|
+
* tools, an empty list included, keeps exactly those. */
|
|
44
|
+
export const NEW_AGENT_TOOLS = Object.freeze(["bash"]);
|
|
28
45
|
/** Why a sandbox-only tool (bash) cannot run on this server: no box runs execute in is available, each with its reason.
|
|
29
46
|
* Undefined when one is. */
|
|
30
47
|
export function noBoxReason(registry) {
|
|
@@ -36,7 +53,7 @@ export function noBoxReason(registry) {
|
|
|
36
53
|
/** The start banner's words on the boxes: which are ready for a sandbox agent's tools, which are still preparing (an
|
|
37
54
|
* available box whose `state` says so, with its reason: what it is pulling), then why each box is not that
|
|
38
55
|
* AGENTRUN_BOXES lists (`listed`), or, when none is ready or preparing, each box runs execute in, with what to do. */
|
|
39
|
-
export function boxesLine(registry, listed) {
|
|
56
|
+
export function boxesLine(registry, listed, allow = "none") {
|
|
40
57
|
const boxes = boxAvailability(registry);
|
|
41
58
|
const preparing = (box) => box.state === "preparing";
|
|
42
59
|
const ready = boxes.filter((box) => box.available && !preparing(box)).map((box) => box.id);
|
|
@@ -47,13 +64,28 @@ export function boxesLine(registry, listed) {
|
|
|
47
64
|
];
|
|
48
65
|
const none = !said.length;
|
|
49
66
|
const why = boxes.filter((box) => !box.available && (listed.has(box.id) || (none && RUN_BOXES.includes(box.id))));
|
|
50
|
-
|
|
67
|
+
// What an available box's programs reach, as capabilities say (`network`, `network_reason`, `places.allow`).
|
|
68
|
+
const hosts = new Intl.ListFormat("en", { type: "conjunction" }).format(allow === "none" ? [] : allow);
|
|
69
|
+
const reach = boxes.filter((box) => box.available && box.network).map((box) => box.network === "allowlist" ? sentence(`${box.id} boxes reach only ${hosts}`) : sentence(`${box.id} boxes have no network${box.network_reason ? `: ${box.network_reason}` : ""}`));
|
|
70
|
+
return ["Boxes:", ...(none ? ["none is ready, so sandbox agents do not run here."] : said), ...reach, ...why.map((box) => sentence(`${box.id}: ${box.reason ?? "not available"}`))].join(" ");
|
|
51
71
|
}
|
|
52
|
-
/** `places` in capabilities: the modes a revision may name,
|
|
53
|
-
|
|
72
|
+
/** `places` in capabilities: the modes a revision may name, where a revision that names none runs (`default`: the runner,
|
|
73
|
+
* for a revision saved with no place), the place a new agent is saved with (`default_place`, with `default_reason` when
|
|
74
|
+
* it is the runner), each box, and the hosts a box whose `network` is `allowlist` reaches (`allow`, run-box.ts
|
|
75
|
+
* boxAllowOf; empty for `none`). */
|
|
76
|
+
export const placesOf = (registry, allow = "none") => {
|
|
77
|
+
const fresh = newAgentPlace(registry);
|
|
78
|
+
return { modes: [...PLACE_MODES], default: DEFAULT_PLACE_MODE, ...("place" in fresh ? { default_place: fresh.place } : { default_place: { mode: "runner" }, default_reason: fresh.reason }), boxes: boxAvailability(registry).map(stated), allow: allow === "none" ? [] : [...allow] };
|
|
79
|
+
};
|
|
54
80
|
/** A box with its state: what the server set (a Docker box whose image is coming is `preparing`), else `ready` when it
|
|
55
|
-
* is available and `unavailable` when not
|
|
56
|
-
|
|
81
|
+
* is available and `unavailable` when not; and, on an available box, its network as the server set it, with
|
|
82
|
+
* `network_reason` when it is none for a cause of its own. */
|
|
83
|
+
const stated = ({ state, network, network_reason, ...offer }) => ({
|
|
84
|
+
...offer,
|
|
85
|
+
state: offer.available ? (state ?? "ready") : "unavailable",
|
|
86
|
+
...(offer.available && network ? { network } : {}),
|
|
87
|
+
...(offer.available && network === "none" && network_reason ? { network_reason } : {}),
|
|
88
|
+
});
|
|
57
89
|
/** A run's place: its revision's, else this host's default. */
|
|
58
90
|
export const resolvePlace = (revision) => revision ?? { mode: "runner" };
|
|
59
91
|
/** The place a run kept (its serve.json), or runner for a run kept before runs had one. */
|
|
@@ -66,7 +98,7 @@ export function storedPlace(value) {
|
|
|
66
98
|
export const BOX_WORK = "/run/agentrun/run/work";
|
|
67
99
|
/** The boxes that cannot bind a run's work/, being on another machine: its work/ is seeded at the box's making and
|
|
68
100
|
* pulled back (run-box.ts). */
|
|
69
|
-
export const REMOTE_BOXES = ["daytona"];
|
|
101
|
+
export const REMOTE_BOXES = ["daytona", VM_PROVIDER.id];
|
|
70
102
|
export const remotePlace = (place) => place.mode === "sandbox" && REMOTE_BOXES.includes(place.box);
|
|
71
103
|
/** The run's work/ as its agent's tools see it: on the runner, `<runDir>/work`; in any box, BOX_WORK. */
|
|
72
104
|
export const agentWorkDir = (runDir, place) => (place.mode === "runner" ? join(runDir, "work") : BOX_WORK);
|
package/dist/propose.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type Proposed = {
|
|
|
5
5
|
notes: number;
|
|
6
6
|
skills: number;
|
|
7
7
|
workflows: number;
|
|
8
|
+
left?: Problem[];
|
|
8
9
|
} | {
|
|
9
10
|
state: "dropped";
|
|
10
11
|
reason: "empty";
|
|
@@ -35,4 +36,5 @@ export declare const proposeRun: (input: {
|
|
|
35
36
|
refuse: readonly string[];
|
|
36
37
|
outcome?: string;
|
|
37
38
|
tools?: readonly string[];
|
|
39
|
+
gated?: Readonly<Record<string, string>>;
|
|
38
40
|
}) => Promise<Proposed>;
|
package/dist/propose.js
CHANGED
|
@@ -6,9 +6,16 @@
|
|
|
6
6
|
// agent's queue. Nothing is written to git here, and nothing is written at all before the checks pass: what they refuse
|
|
7
7
|
// (a note holding a secret among them) is never kept anywhere.
|
|
8
8
|
import { join } from "node:path";
|
|
9
|
-
import { checkProposal, draftFiles, GRADE_CAPS, learnedStatus, parseReflection, redactSecrets, SKILLS_DIR, WORKFLOWS_DIR } from "./learn/index.js";
|
|
9
|
+
import { checkProposal, draftFiles, unstorable, GRADE_CAPS, learnedStatus, parseReflection, redactSecrets, SKILLS_DIR, WORKFLOWS_DIR } from "./learn/index.js";
|
|
10
10
|
import { homeOf, writeDurable } from "./files.js";
|
|
11
11
|
import { activeOf, agentFolder, git, PROPOSAL, readBlobs, readSeed, withAgentRepo } from "./learning.js";
|
|
12
|
+
/** The rules that refuse a proposal whole, whatever item they name: its counts and caps, a path outside what a run may
|
|
13
|
+
* write, a deletion, the folder's index full, anything that must never be stored (a secret, a character a reader does
|
|
14
|
+
* not see), which says the run's answer is not to be trusted for any item, and text that is not UTF-8, which cannot be
|
|
15
|
+
* read for one. Every other problem is its item's alone; unstorable reads every item's text whole first. */
|
|
16
|
+
const WHOLE = new Set(["too_many_files", "too_many_notes", "too_many_skills", "too_many_workflows", "outside", "deleted", "index_full", "secret_value", "secret_shape", "invisible_unicode", "encoding"]);
|
|
17
|
+
/** The item a changed file belongs to: a draft's two files are one item (its folder), every other file is itself. */
|
|
18
|
+
const itemOf = (path) => (path.startsWith(`${WORKFLOWS_DIR}/`) ? path.split("/").slice(0, 2).join("/") : path);
|
|
12
19
|
/** `text`'s first `max` UTF-16 units, never half a character. */
|
|
13
20
|
const cut = (text, max) => (text.length <= max ? text : [...text].reduce((out, ch) => (out.length + ch.length > max ? out : out + ch), ""));
|
|
14
21
|
const utf8 = new TextDecoder("utf-8", { fatal: true });
|
|
@@ -66,17 +73,40 @@ export const proposeRun = (input) => withAgentRepo(input.data, input.agentId, as
|
|
|
66
73
|
const dsl = files.some(({ file }) => file.endsWith("/workflow.json")) ? await import("@parcha/agentrun-dsl") : undefined;
|
|
67
74
|
// A skill or a draft whose seed text has a status is a person's call (retired, or waiting for their OK): never changed.
|
|
68
75
|
const kept = [...held.texts].filter(([file, text]) => (file.endsWith("/SKILL.md") || file.endsWith("/README.md")) && learnedStatus(text) !== "active").map(([file]) => file);
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
const check = (some) => checkProposal({ base: { files: [...seed.files, ...held.files], active: activeOf(seed) }, changes: some, refuseValues: input.refuse, ...(kept.length ? { kept } : {}), ...(input.outcome === undefined ? {} : { outcome: input.outcome }), ...(input.tools ? { tools: input.tools } : {}), ...(input.gated ? { gated: input.gated } : {}), ...(dsl ? { dsl } : {}) });
|
|
77
|
+
// A verdict per item from the checks too: the items a problem names are left out (a draft's two files together),
|
|
78
|
+
// and the rest is checked again; a problem that names no item, or one that refuses the whole (WHOLE), drops it all,
|
|
79
|
+
// as does a rest the checks still refuse.
|
|
80
|
+
let checked = check(changes);
|
|
81
|
+
let left = [];
|
|
82
|
+
let passing = files;
|
|
83
|
+
if (checked.problems.length) {
|
|
84
|
+
const named = new Set(changes.map(({ path }) => itemOf(path)));
|
|
85
|
+
if (checked.problems.some((problem) => WHOLE.has(problem.rule) || problem.path === undefined || !named.has(itemOf(problem.path))))
|
|
86
|
+
return { state: "dropped", reason: "checker", problems: checked.problems };
|
|
87
|
+
// Before any item is kept from an answer the checks refuse: every name and text it gave, read whole. Something
|
|
88
|
+
// that must never be stored anywhere in it (in an item the checks refused unread, too) refuses it all.
|
|
89
|
+
const never = unstorable(changes, input.refuse);
|
|
90
|
+
if (never.length)
|
|
91
|
+
return { state: "dropped", reason: "checker", problems: [...checked.problems, ...never] };
|
|
92
|
+
const out = new Set(checked.problems.map((problem) => itemOf(problem.path)));
|
|
93
|
+
const rest = changes.filter(({ path }) => !out.has(itemOf(path)));
|
|
94
|
+
const again = rest.length ? check(rest) : undefined;
|
|
95
|
+
// A rest the checks refuse again: its own problems are what keeps it out, said with the first pass's.
|
|
96
|
+
if (!again || again.problems.length)
|
|
97
|
+
return { state: "dropped", reason: "checker", problems: [...checked.problems, ...(again?.problems ?? [])] };
|
|
98
|
+
left = checked.problems;
|
|
99
|
+
checked = again;
|
|
100
|
+
passing = files.filter(({ file }) => !out.has(itemOf(file)));
|
|
101
|
+
}
|
|
102
|
+
if (!checked.notes.length && !passing.length)
|
|
73
103
|
return { state: "dropped", reason: "empty" };
|
|
74
104
|
// Each message cut of the host's secrets whole first, and only then shortened: a secret the cut fell inside would
|
|
75
105
|
// leave a part of itself that no longer matches. The whole message is read, however long (a message is bounded by
|
|
76
106
|
// the run's own request limits), so a long one is not lost to the grader as one "[redacted]".
|
|
77
107
|
const asked = input.turn.asked.map((message) => cut(redactSecrets(message, input.refuse, Number.POSITIVE_INFINITY), GRADE_CAPS.askedMessage));
|
|
78
|
-
const proposal = { seed: input.seed, notes: checked.notes.map(({ file, text }) => ({ file, text })), ...(
|
|
108
|
+
const proposal = { seed: input.seed, notes: checked.notes.map(({ file, text }) => ({ file, text })), ...(passing.length ? { files: passing } : {}), asked };
|
|
79
109
|
await writeDurable(join(input.runDir, PROPOSAL), `${JSON.stringify(proposal)}\n`);
|
|
80
|
-
const count = (suffix) =>
|
|
81
|
-
return { state: "proposed", notes: proposal.notes.length, skills: count("/SKILL.md"), workflows: count("/workflow.json") };
|
|
110
|
+
const count = (suffix) => passing.filter(({ file }) => file.endsWith(suffix)).length;
|
|
111
|
+
return { state: "proposed", notes: proposal.notes.length, skills: count("/SKILL.md"), workflows: count("/workflow.json"), ...(left.length ? { left } : {}) };
|
|
82
112
|
});
|
package/dist/request-start.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import type { Models } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { DurableRun } from "@parcha/pi-durable-disk";
|
|
3
|
+
import { type RefusalFigures } from "./cap.js";
|
|
3
4
|
/** Whether the run in `root` (its directory) was cancelled: the mark serve's cancel writes there first. */
|
|
4
5
|
export declare const cancelledIn: (root: string) => () => boolean;
|
|
5
|
-
/** How long a
|
|
6
|
-
|
|
6
|
+
/** How long a mark that carries a hold (a lost or failed request's end, an unreadable cost's) may take to reach the store
|
|
7
|
+
* before the request fails loudly (END_NOT_WRITTEN): 15 s. A lost mark is waited for before the retry is sent, so a
|
|
8
|
+
* commit queued behind a large entry on a slow disk (seconds) past 1 s let the retry leave, and its answer reach pi,
|
|
9
|
+
* with no mark in the store: a crash then read the request at the answer's own usage. Such a mark is written only on a
|
|
10
|
+
* path already lost or failed, so the wait costs latency there and nowhere else; 15 s holds such a commit with room to
|
|
11
|
+
* spare, and still fails a store that has stopped within a turn's patience, never a turn held without bound. */
|
|
12
|
+
export declare const END_MARK_WAIT_MS = 15000;
|
|
7
13
|
/** `models` with each generation request marked before it leaves, once `open` hands over the run (its Harness). Once
|
|
8
14
|
* `cancelled` says the run was cancelled, no request is sent: each is refused here, before it is marked, or after its
|
|
9
15
|
* mark when the cancel came while the mark waited, and then its start is withdrawn (an end mark, `unsent`). */
|
|
10
|
-
export declare function markRequests<M extends Models>(models: M, cancelled?: () => boolean
|
|
16
|
+
export declare function markRequests<M extends Models>(models: M, cancelled?: () => boolean, { holdMarkWaitMs }?: {
|
|
17
|
+
holdMarkWaitMs?: number;
|
|
18
|
+
}): Promise<{
|
|
11
19
|
models: M;
|
|
12
20
|
open(run: DurableRun): void;
|
|
13
|
-
refused(): Promise<void>;
|
|
21
|
+
refused(figures?: RefusalFigures): Promise<void>;
|
|
14
22
|
}>;
|
package/dist/request-start.js
CHANGED
|
@@ -21,16 +21,25 @@ import { join } from "node:path";
|
|
|
21
21
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
22
22
|
import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
23
23
|
import { REQUEST_COST, REQUEST_ENDED, REQUEST_REFUSED, REQUEST_STARTED } from "@parcha/pi-durable-agent/timeline";
|
|
24
|
-
import { CAP_REFUSED_FIELD, CLAMP, HELD_FIELD, HELD_USD, MARK_NOT_WRITTEN, markResponseBegan, sideRequests, STOPPED_USD } from "./cap.js";
|
|
24
|
+
import { anyAttemptLost, ATTEMPTS, attemptLost, CAP_REFUSED_FIELD, END_NOT_WRITTEN, CLAMP, HELD_FIELD, HELD_USD, MARK_NOT_WRITTEN, markResponseBegan, piEnded, sideRequests, STOPPED_USD } from "./cap.js";
|
|
25
25
|
import { RUN_CANCELLED } from "./files.js";
|
|
26
|
+
import { isConnectCode, isGatewayTimeout } from "@parcha/agentrun-kernel";
|
|
26
27
|
/** Whether the run in `root` (its directory) was cancelled: the mark serve's cancel writes there first. */
|
|
27
28
|
export const cancelledIn = (root) => () => existsSync(join(root, RUN_CANCELLED));
|
|
28
|
-
/** How long a
|
|
29
|
-
|
|
29
|
+
/** How long a mark that carries a hold (a lost or failed request's end, an unreadable cost's) may take to reach the store
|
|
30
|
+
* before the request fails loudly (END_NOT_WRITTEN): 15 s. A lost mark is waited for before the retry is sent, so a
|
|
31
|
+
* commit queued behind a large entry on a slow disk (seconds) past 1 s let the retry leave, and its answer reach pi,
|
|
32
|
+
* with no mark in the store: a crash then read the request at the answer's own usage. Such a mark is written only on a
|
|
33
|
+
* path already lost or failed, so the wait costs latency there and nowhere else; 15 s holds such a commit with room to
|
|
34
|
+
* spare, and still fails a store that has stopped within a turn's patience, never a turn held without bound. */
|
|
35
|
+
export const END_MARK_WAIT_MS = 15_000;
|
|
36
|
+
/** How long a stop's mark is waited for before pi has the stopped result: a stop's mark needs no fatal wait (without it
|
|
37
|
+
* the request reads as cut, at its hold, never less), so a slow store costs a stop at most this. */
|
|
38
|
+
const STOP_MARK_WAIT_MS = 1_000;
|
|
30
39
|
/** `models` with each generation request marked before it leaves, once `open` hands over the run (its Harness). Once
|
|
31
40
|
* `cancelled` says the run was cancelled, no request is sent: each is refused here, before it is marked, or after its
|
|
32
41
|
* mark when the cancel came while the mark waited, and then its start is withdrawn (an end mark, `unsent`). */
|
|
33
|
-
export async function markRequests(models, cancelled = () => false) {
|
|
42
|
+
export async function markRequests(models, cancelled = () => false, { holdMarkWaitMs = END_MARK_WAIT_MS } = {}) {
|
|
34
43
|
const { createAssistantMessageEventStream } = await import("@earendil-works/pi-ai");
|
|
35
44
|
let open;
|
|
36
45
|
// A request a resumed turn sends before the app's open waits for it: the mark needs the run's store.
|
|
@@ -45,6 +54,12 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
45
54
|
errorMessage: why,
|
|
46
55
|
});
|
|
47
56
|
const reason = (error) => String(error?.message ?? error).slice(0, 200);
|
|
57
|
+
/** Whether `work` (a mark's write) is in the store within `holdMarkWaitMs` (END_MARK_WAIT_MS, a test's own when it
|
|
58
|
+
* gives one): undefined when it is, else why not (its refusal, or the wait run out). */
|
|
59
|
+
const landed = (work) => new Promise((resolve) => {
|
|
60
|
+
const timer = setTimeout(() => resolve(new Error(`the store had not taken it in ${holdMarkWaitMs} ms`)), holdMarkWaitMs);
|
|
61
|
+
work.then(() => (clearTimeout(timer), resolve(undefined)), (error) => (clearTimeout(timer), resolve(error ?? new Error("refused"))));
|
|
62
|
+
});
|
|
48
63
|
/** Until `work` settles, or `ms` pass. */
|
|
49
64
|
const within = (work, ms) => new Promise((resolve) => {
|
|
50
65
|
const timer = setTimeout(resolve, ms);
|
|
@@ -55,9 +70,66 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
55
70
|
const held = options?.[HELD_USD];
|
|
56
71
|
return typeof held === "number" && Number.isFinite(held) ? { [HELD_FIELD]: held } : {};
|
|
57
72
|
};
|
|
73
|
+
/** Whether a request the cap watched is lost (piEnded, from the attempts its cap recorded): an error before any event
|
|
74
|
+
* that left, or an answer after an attempt that was lost. It can have been billed, so it counts at its hold. Only a
|
|
75
|
+
* request the cap watched (a hold, and its attempts, or null on an API it cannot watch): another is read as before. */
|
|
76
|
+
const lostOf = (options, message) => {
|
|
77
|
+
const record = options;
|
|
78
|
+
if (typeof record?.[HELD_USD] !== "number" || !record || !(ATTEMPTS in record))
|
|
79
|
+
return false;
|
|
80
|
+
return piEnded(message, false, record[ATTEMPTS] ?? undefined).end === "lost";
|
|
81
|
+
};
|
|
82
|
+
/** `options` whose fetch writes the request's end mark at its hold (`lost`), once, the moment an attempt is known lost:
|
|
83
|
+
* a rejection once it could have left (any code but a connect one, and not the run's own stop) or a gateway's timeout
|
|
84
|
+
* (504, 524), as that attempt's fetch ends; an answer pi-ai then tries again (a 2xx that was not the last), before the
|
|
85
|
+
* next attempt is sent, since only the retry says so. Whatever follows (a retry's answer, a stop while pi-ai waits to
|
|
86
|
+
* retry, an error), the provider can have billed the lost attempt, and the mark is in the store before pi's entry for
|
|
87
|
+
* the request can be, so a later process reads the hold this one books, with no window between them. `options` as
|
|
88
|
+
* given on a request the cap does not watch. */
|
|
89
|
+
const watchingLost = (options, markLost) => {
|
|
90
|
+
const record = options;
|
|
91
|
+
const attempts = record && ATTEMPTS in record ? record[ATTEMPTS] : null;
|
|
92
|
+
if (!attempts || typeof record?.[HELD_USD] !== "number" || typeof record.fetch !== "function")
|
|
93
|
+
return options;
|
|
94
|
+
const inner = record.fetch;
|
|
95
|
+
let marked = false;
|
|
96
|
+
const markOnce = async () => {
|
|
97
|
+
if (marked)
|
|
98
|
+
return;
|
|
99
|
+
marked = true;
|
|
100
|
+
await markLost().catch(() => undefined);
|
|
101
|
+
};
|
|
102
|
+
/** Whether the attempt the fetch just recorded is lost as it ends, whatever comes after it. */
|
|
103
|
+
const lostNow = () => {
|
|
104
|
+
const last = attempts.at(-1);
|
|
105
|
+
return last !== undefined && ("code" in last ? !last.aborted && !isConnectCode(last.code) : isGatewayTimeout(last.status));
|
|
106
|
+
};
|
|
107
|
+
return { ...record, fetch: (async (input, init) => {
|
|
108
|
+
// An answer pi-ai tries again: lost, as the retry shows.
|
|
109
|
+
if (attempts.some((attempt) => attemptLost(attempt, false)))
|
|
110
|
+
await markOnce();
|
|
111
|
+
let response;
|
|
112
|
+
try {
|
|
113
|
+
response = await inner(input, init);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (lostNow())
|
|
117
|
+
await markOnce();
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
if (lostNow())
|
|
121
|
+
await markOnce();
|
|
122
|
+
return response;
|
|
123
|
+
}) };
|
|
124
|
+
};
|
|
58
125
|
/** What the run's cap counts a request stopped before its end at, given the answer it had streamed (its options'
|
|
59
|
-
* STOPPED_USD), as an end mark's field; its worst case when the cap gave no such figure
|
|
126
|
+
* STOPPED_USD), as an end mark's field; its worst case when the cap gave no such figure, or when an attempt before the
|
|
127
|
+
* stop was lost (anyAttemptLost: it can have been billed, and the cap counts it at its hold in this process too); none
|
|
128
|
+
* without a cap. The one figure every stop's end mark carries, a generation's and a summary's. */
|
|
60
129
|
const stoppedOf = (options, message) => {
|
|
130
|
+
const attempts = options?.[ATTEMPTS];
|
|
131
|
+
if (anyAttemptLost(attempts ?? undefined))
|
|
132
|
+
return heldOf(options);
|
|
61
133
|
const stopped = options?.[STOPPED_USD];
|
|
62
134
|
const figure = typeof stopped === "function" ? stopped(message) : undefined;
|
|
63
135
|
return typeof figure === "number" && Number.isFinite(figure) ? { [HELD_FIELD]: figure } : heldOf(options);
|
|
@@ -116,14 +188,29 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
116
188
|
// The stream's error event, passed on once the request's end is marked (below): the outer stream's result, which
|
|
117
189
|
// pi writes as the entry and the cap counts, settles on that event.
|
|
118
190
|
let failure;
|
|
191
|
+
// Its done event, held the same way: the stream's result settles on it too, so pi has an answer only once its mark
|
|
192
|
+
// that carries a hold (a lost or unpriced request's) is in the store, and its entry always comes after that mark.
|
|
193
|
+
let done;
|
|
194
|
+
// Marked lost before pi-ai tries again (watchingLost): its end mark is in the store before its answer can be.
|
|
195
|
+
let lostMarked = false;
|
|
196
|
+
// Its write refused: the request's end is not recorded, and the request fails saying so (END_NOT_WRITTEN).
|
|
197
|
+
let lostRefused;
|
|
198
|
+
const sent = watchingLost(options, async () => {
|
|
199
|
+
lostMarked = true;
|
|
200
|
+
lostRefused = await landed(mark(REQUEST_ENDED, { lost: true, ...heldOf(options) }));
|
|
201
|
+
});
|
|
119
202
|
try {
|
|
120
|
-
const inner = models.streamSimple(model, context,
|
|
203
|
+
const inner = models.streamSimple(model, context, sent);
|
|
121
204
|
for await (const event of inner) {
|
|
122
205
|
if (event.type === "error") {
|
|
123
206
|
failure = event;
|
|
124
207
|
continue;
|
|
125
208
|
}
|
|
126
209
|
began = true;
|
|
210
|
+
if (event.type === "done") {
|
|
211
|
+
done = event;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
127
214
|
outer.push(event);
|
|
128
215
|
}
|
|
129
216
|
result = await inner.result();
|
|
@@ -138,11 +225,26 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
138
225
|
// take the mark leaves the request unended, which the timeline also reads as cut, at an unknown cost: the
|
|
139
226
|
// conservative reading, so the failure is swallowed here.
|
|
140
227
|
let ending;
|
|
141
|
-
|
|
228
|
+
// Whether `ending` carries the hold a later process must read (an error's or a lost request's): it is in the store
|
|
229
|
+
// before pi has the result, however long the store takes, and a refused one fails the request loudly. A stop's
|
|
230
|
+
// mark keeps its bound: without it the request stays open, read at its hold, which is never less.
|
|
231
|
+
let holding = false;
|
|
232
|
+
// An answer whose cost cannot be read counts at its hold (the booking rule), in this process and in a later one: its
|
|
233
|
+
// end mark carries the hold (`unpriced`), in the store before pi has the answer, as a lost request's does.
|
|
234
|
+
const total = result.usage?.cost?.total;
|
|
235
|
+
const unpriced = result.stopReason !== "error" && result.stopReason !== "aborted" && !(typeof total === "number" && Number.isFinite(total)) && Object.keys(heldOf(options)).length > 0;
|
|
236
|
+
// A request marked lost before its retry was sent has its end in the store already, at its hold: nothing more.
|
|
237
|
+
if (lostMarked) {
|
|
238
|
+
if (result.stopReason === "error" && failure?.type === "error")
|
|
239
|
+
markResponseBegan(failure.error);
|
|
240
|
+
}
|
|
241
|
+
else if (result.stopReason === "aborted") {
|
|
242
|
+
// Reached only with no lost attempt: watchingLost sets `lostMarked` synchronously, before it awaits the mark, the
|
|
243
|
+
// moment an attempt is lost, and that mark lands before pi-ai's retry sleep, so a stop in the sleep finds it set.
|
|
142
244
|
const responseId = result.responseId;
|
|
143
245
|
ending = mark(REQUEST_ENDED, { ...(typeof responseId === "string" ? { responseId } : {}), ...stoppedOf(options, result) });
|
|
144
246
|
}
|
|
145
|
-
else if (result.stopReason === "error" && began) {
|
|
247
|
+
else if (result.stopReason === "error" && (began || lostOf(options, result))) {
|
|
146
248
|
// An error after its response began (an overload part way, a reset): its usage is what had streamed, not what the
|
|
147
249
|
// provider can have billed. Its message is recorded (markResponseBegan), which the cap counts at its hold in this
|
|
148
250
|
// process, and its end is marked with the hold (`error`), which the spend read back counts. One before any event
|
|
@@ -151,13 +253,31 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
151
253
|
if (failure?.type === "error")
|
|
152
254
|
markResponseBegan(failure.error);
|
|
153
255
|
ending = mark(REQUEST_ENDED, { error: true, ...heldOf(options) });
|
|
256
|
+
holding = true;
|
|
257
|
+
}
|
|
258
|
+
else if (unpriced) {
|
|
259
|
+
ending = mark(REQUEST_ENDED, { unpriced: true, ...heldOf(options) });
|
|
260
|
+
holding = true;
|
|
261
|
+
}
|
|
262
|
+
// The end mark goes in before pi has the result, so the store holds it before pi's entry; a store that has not taken
|
|
263
|
+
// it in END_MARK_WAIT_MS does not hold the turn. One that carries a hold and is not in the store by then (refused, or
|
|
264
|
+
// still being written) fails the request loudly (END_NOT_WRITTEN): pi's entry for it then leaves its start open, read
|
|
265
|
+
// at its hold (spentIn), so no store's pace stands between a crash and a request read at less than it can have cost.
|
|
266
|
+
// A stop's mark needs none of that: without it the request reads as cut, at its hold, which is never less.
|
|
267
|
+
let unrecorded = lostRefused;
|
|
268
|
+
if (ending && holding)
|
|
269
|
+
unrecorded = await landed(ending);
|
|
270
|
+
else if (ending)
|
|
271
|
+
await within(ending.catch(() => undefined), STOP_MARK_WAIT_MS);
|
|
272
|
+
if (unrecorded !== undefined && result.stopReason !== "aborted") {
|
|
273
|
+
result = failed(model, `${END_NOT_WRITTEN} (${reason(unrecorded)})`);
|
|
274
|
+
failure = { type: "error", reason: "error", error: result };
|
|
154
275
|
}
|
|
155
|
-
//
|
|
156
|
-
// has not answered in END_MARK_WAIT_MS, does not hold the turn: the result goes on, and the mark lands when it can.
|
|
157
|
-
if (ending)
|
|
158
|
-
await within(ending.catch(() => undefined), END_MARK_WAIT_MS);
|
|
276
|
+
// A request whose end could not be recorded fails loudly (failure, above) in place of its answer.
|
|
159
277
|
if (failure)
|
|
160
278
|
outer.push(failure);
|
|
279
|
+
else if (done)
|
|
280
|
+
outer.push(done);
|
|
161
281
|
outer.end(result);
|
|
162
282
|
})();
|
|
163
283
|
return outer;
|
|
@@ -205,7 +325,10 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
205
325
|
const began = result.stopReason === "error" && (usage !== undefined || result.content.length > 0);
|
|
206
326
|
if (began)
|
|
207
327
|
markResponseBegan(result);
|
|
208
|
-
|
|
328
|
+
// Failed before any response, it counts at its hold too unless it provably never left (piEnded): the same figure the
|
|
329
|
+
// cap books in this process, which a later process reads from this mark.
|
|
330
|
+
// An answer whose cost cannot be read counts at its hold too (the booking rule), which its mark carries.
|
|
331
|
+
const failure = began || lostOf(options, result) ? { failed: true, ...heldOf(options) } : result.stopReason !== "error" && !(typeof usd === "number" && Number.isFinite(usd)) && Object.keys(heldOf(options)).length > 0 ? { unpriced: true, ...heldOf(options) } : {};
|
|
209
332
|
await mark(REQUEST_COST, usage ? { ...COMPACTION, usd, usage: usage, ...failure } : { ...COMPACTION, usd, ...failure }).catch(() => undefined);
|
|
210
333
|
}
|
|
211
334
|
return result;
|
|
@@ -220,7 +343,8 @@ export async function markRequests(models, cancelled = () => false) {
|
|
|
220
343
|
return typeof value === "function" ? value.bind(target) : value;
|
|
221
344
|
},
|
|
222
345
|
});
|
|
223
|
-
// The start mark of a generation request the cap refused (capModels), which never left: pi's error entry answers it.
|
|
224
|
-
|
|
346
|
+
// The start mark of a generation request the cap refused (capModels), which never left: pi's error entry answers it. A
|
|
347
|
+
// refusal for room keeps its figures on it (what the cap counted as taken, and the least the request needed).
|
|
348
|
+
const refused = (figures) => mark(REQUEST_STARTED, { [CAP_REFUSED_FIELD]: true, ...(figures ? { spentUsd: figures.spentUsd, needsUsd: figures.needsUsd } : {}) });
|
|
225
349
|
return { models: proxy, open, refused };
|
|
226
350
|
}
|