@parcha/agentrun-serve 0.1.0-beta.34 → 0.1.0-beta.35
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 +135 -0
- package/README.md +84 -16
- package/dist/adapters.d.ts +3 -0
- package/dist/adapters.js +7 -0
- package/dist/agent-rows.d.ts +1 -0
- package/dist/agents-store.js +17 -2
- package/dist/archil-settings.d.ts +11 -1
- package/dist/archil-settings.js +10 -0
- package/dist/bash-read-only.d.ts +13 -0
- package/dist/bash-read-only.js +77 -0
- package/dist/browser-cost.d.ts +2 -2
- package/dist/browser-cost.js +6 -4
- package/dist/budget.d.ts +0 -5
- package/dist/budget.js +0 -5
- package/dist/cap.d.ts +6 -3
- package/dist/cap.js +7 -4
- package/dist/cli.js +32 -5
- package/dist/computer-keys.d.ts +20 -10
- package/dist/computer-keys.js +36 -57
- package/dist/computer-setting.d.ts +6 -5
- package/dist/computer-setting.js +54 -26
- package/dist/computers.d.ts +9 -4
- package/dist/computers.js +4 -2
- package/dist/console/assets/index-B7NKTU8E.css +1 -0
- package/dist/console/assets/index-CXZMgJE7.js +550 -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-cap.d.ts +7 -29
- package/dist/decision-cap.js +3 -94
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +3 -0
- package/dist/explain.js +3 -1
- package/dist/github-connection.js +25 -7
- package/dist/github-faux.d.ts +26 -0
- package/dist/github-faux.js +76 -0
- package/dist/github.d.ts +6 -2
- package/dist/github.js +5 -3
- package/dist/graph-served.d.ts +5 -3
- package/dist/graph-served.js +14 -6
- package/dist/graph-view.js +5 -2
- package/dist/harness-opencode/harness.js +0 -1
- package/dist/harness-opencode/host/policy.js +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/learn/check.d.ts +3 -1
- package/dist/learn/check.js +3 -3
- package/dist/learn/folder.d.ts +6 -3
- package/dist/learn/folder.js +7 -16
- package/dist/learn/grade.d.ts +4 -1
- package/dist/learn/grade.js +43 -5
- package/dist/learn/index.d.ts +2 -2
- package/dist/learn/index.js +2 -2
- package/dist/learn/learning.sh +1 -14
- package/dist/learn/notes.d.ts +14 -3
- package/dist/learn/notes.js +16 -6
- package/dist/learn/reflect.js +5 -3
- package/dist/learn/skills.d.ts +48 -5
- package/dist/learn/skills.js +97 -21
- package/dist/learned-nodes.d.ts +14 -0
- package/dist/learned-nodes.js +8 -0
- package/dist/learned-workflow.d.ts +9 -5
- package/dist/learned-workflow.js +53 -9
- package/dist/learning-routes.d.ts +41 -0
- package/dist/learning-routes.js +94 -26
- package/dist/learning-run.d.ts +7 -5
- package/dist/learning-run.js +9 -6
- package/dist/learning.d.ts +0 -22
- package/dist/learning.js +0 -16
- package/dist/llm.d.ts +4 -1
- package/dist/llm.js +4 -2
- package/dist/mcp-gateway.d.ts +6 -0
- package/dist/mcp-gateway.js +11 -1
- package/dist/mcp-servers-store.d.ts +12 -12
- package/dist/model.d.ts +3 -0
- package/dist/model.js +8 -0
- package/dist/openapi.js +37 -13
- package/dist/paid-calls.js +1 -1
- package/dist/pause.d.ts +2 -19
- package/dist/pause.js +5 -74
- package/dist/port-models.d.ts +10 -3
- package/dist/port-models.js +26 -6
- package/dist/propose.d.ts +2 -0
- package/dist/propose.js +1 -10
- package/dist/run-launch.d.ts +5 -3
- package/dist/run-launch.js +6 -5
- package/dist/run-learning-served.d.ts +11 -0
- package/dist/run-reads-over-store.d.ts +4 -2
- package/dist/run-reads-over-store.js +10 -8
- package/dist/run-timeline-served.js +12 -2
- package/dist/run-view.d.ts +30 -14
- package/dist/run-view.js +60 -32
- package/dist/runnable.d.ts +4 -1
- package/dist/runnable.js +14 -2
- package/dist/runner/adapter-loop.d.ts +30 -4
- package/dist/runner/adapter-loop.js +253 -29
- package/dist/runner/adapter-run-spec.d.ts +21 -0
- package/dist/runner/adapter-run-spec.js +104 -28
- package/dist/runner/adapters/pi.d.ts +29 -1
- package/dist/runner/adapters/pi.js +95 -23
- package/dist/runner/beside.d.ts +8 -0
- package/dist/runner/beside.js +18 -0
- package/dist/runner/claim-secrets.d.ts +6 -0
- package/dist/runner/claim-secrets.js +15 -1
- package/dist/runner/cli-run-spec.d.ts +3 -0
- package/dist/runner/cli-run-spec.js +123 -34
- package/dist/runner/conformance/harness.d.ts +1 -0
- package/dist/runner/conformance/harness.js +5 -5
- package/dist/runner/github-app.d.ts +7 -2
- package/dist/runner/github-app.js +46 -15
- package/dist/runner/harness-loops.d.ts +6 -1
- package/dist/runner/harness-loops.js +23 -0
- package/dist/runner/journal-kinds.d.ts +38 -2
- package/dist/runner/journal-kinds.js +23 -0
- package/dist/runner/learning-end.js +88 -16
- package/dist/runner/learning-loop.d.ts +2 -0
- package/dist/runner/learning-loop.js +13 -5
- package/dist/runner/learning-seed.d.ts +6 -0
- package/dist/runner/learning-seed.js +6 -0
- package/dist/runner/loop-contract.d.ts +4 -0
- package/dist/runner/main.d.ts +9 -4
- package/dist/runner/main.js +26 -11
- package/dist/runner/mcp-claim.d.ts +3 -0
- package/dist/runner/mcp-claim.js +6 -1
- package/dist/runner/mcp-github.d.ts +11 -2
- package/dist/runner/mcp-github.js +55 -4
- package/dist/runner/mcp-run.js +1 -0
- package/dist/runner/metrics.d.ts +14 -1
- package/dist/runner/metrics.js +72 -1
- package/dist/runner/model-route.d.ts +3 -0
- package/dist/runner/model-route.js +1 -1
- package/dist/runner/runner.js +90 -8
- package/dist/runner/spec-holds.d.ts +7 -2
- package/dist/runner/spec-holds.js +4 -3
- package/dist/runner/workflow-host.d.ts +7 -2
- package/dist/runner/workflow-host.js +3 -1
- package/dist/runner/workflow-loop.d.ts +4 -1
- package/dist/runner/workflow-loop.js +8 -2
- package/dist/runner/workflow-spec.d.ts +3 -0
- package/dist/runner/workflow-spec.js +156 -78
- package/dist/runs-over-store.d.ts +7 -1
- package/dist/runs-over-store.js +42 -11
- package/dist/seal-key.d.ts +0 -2
- package/dist/seal-key.js +0 -2
- package/dist/server-lock.js +4 -5
- package/dist/server.d.ts +21 -18
- package/dist/server.js +183 -34
- package/dist/stop-signals.d.ts +4 -2
- package/dist/stop-signals.js +5 -3
- package/dist/store/agent-body.d.ts +5 -1
- package/dist/store/agent-body.js +11 -5
- package/dist/store/deadline.d.ts +18 -0
- package/dist/store/deadline.js +45 -0
- package/dist/store/postgres/connection.d.ts +4 -0
- package/dist/store/postgres/connection.js +55 -9
- package/dist/store/sqlite/connection.js +3 -38
- package/dist/store-reads/journal.js +39 -4
- package/dist/store-reads/learning.d.ts +58 -2
- package/dist/store-reads/learning.js +172 -7
- package/dist/store-view.d.ts +15 -1
- package/dist/store-view.js +45 -12
- package/dist/templates.d.ts +9 -4
- package/dist/templates.js +14 -9
- package/dist/timeline/timeline.d.ts +8 -0
- package/dist/tool-budget.d.ts +18 -0
- package/dist/tool-budget.js +50 -0
- package/dist/tool-gate.d.ts +6 -2
- package/dist/tool-gate.js +8 -5
- package/dist/tool-plane.d.ts +9 -10
- package/dist/tools.d.ts +37 -7
- package/dist/tools.js +73 -12
- package/dist/uploads.d.ts +28 -5
- package/dist/uploads.js +82 -9
- package/dist/usage.d.ts +1 -47
- package/dist/usage.js +3 -82
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +11 -1
- package/dist/workspace/archil-claim.d.ts +2 -2
- package/dist/workspace/archil-claim.js +4 -3
- package/dist/workspace/archil-disk.d.ts +10 -1
- package/dist/workspace/archil-disk.js +4 -0
- package/dist/workspace/archil-exec.d.ts +12 -2
- package/dist/workspace/archil-exec.js +16 -0
- package/dist/workspace/archil.d.ts +2 -2
- package/dist/workspace/archil.js +25 -5
- package/dist/workspace/attach.js +2 -0
- package/dist/workspace/computer-deps.d.ts +9 -10
- package/dist/workspace/computer-deps.js +53 -40
- package/dist/workspace/computer-for-claim.d.ts +5 -3
- package/dist/workspace/computer-for-claim.js +28 -16
- package/dist/workspace/computer-ledger.d.ts +1 -8
- package/dist/workspace/computer-ledger.js +0 -3
- package/dist/workspace/computer-providers.d.ts +5 -3
- package/dist/workspace/computer-providers.js +11 -9
- package/dist/workspace/hands.d.ts +5 -2
- package/dist/workspace/hands.js +4 -3
- package/dist/workspace/index.d.ts +3 -3
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/local.d.ts +3 -0
- package/dist/workspace/local.js +9 -0
- package/dist/workspace/paths.js +3 -2
- package/dist/workspace/pi-env.d.ts +4 -0
- package/dist/workspace/pi-env.js +3 -2
- package/dist/workspace/resolve-computer.d.ts +8 -3
- package/dist/workspace/resolve-computer.js +28 -11
- package/dist/workspace/run-tools.d.ts +2 -0
- package/dist/workspace/run-tools.js +9 -3
- package/dist/workspace/workspace.d.ts +5 -0
- package/dist/workspace.d.ts +0 -3
- package/dist/workspace.js +1 -14
- package/package.json +14 -14
- package/skills/agentrun-serve/SKILL.md +1 -1
- package/templates/README.md +2 -2
- package/templates/code-review/evals/mentions-defused/case.yaml +15 -0
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +254 -0
- package/templates/code-review/evals/mentions-defused/graders/no-pings.md +16 -0
- package/templates/code-review/evals/mentions-defused/graders/posted-a-review.md +5 -0
- package/templates/code-review/evals/mentions-defused/graders/schema.md +5 -0
- package/templates/code-review/evals/mentions-defused/prompt.md +10 -0
- package/templates/kyb/evals/clean-public-company/graders/no-opensanctions.md +5 -0
- package/templates/kyb/evals/opensanctions-on/case.yaml +5 -0
- package/templates/kyb/evals/opensanctions-on/graders/licence-and-evidence.md +18 -0
- package/templates/kyb/evals/opensanctions-on/graders/schema.md +5 -0
- package/templates/kyb/evals/opensanctions-on/prompt.md +11 -0
- package/templates/release-notes/agent.md +11 -0
- package/dist/console/assets/index-RPHGlXmM.js +0 -540
- package/dist/console/assets/index-opTN5Naq.css +0 -1
package/dist/store/agent-body.js
CHANGED
|
@@ -44,18 +44,20 @@ function placeOf(place) {
|
|
|
44
44
|
}
|
|
45
45
|
/** A GPU type as a body keeps it: the vendor's own name ("T4", "A100-80GB", "RTX-PRO-6000"). */
|
|
46
46
|
const GPU_TYPE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
47
|
-
/** A computer as a body keeps it: `{kind, provider?, gpu?}`, nothing else: kind and provider ids,
|
|
48
|
-
* a GPU type name and a whole count from 1. */
|
|
47
|
+
/** A computer as a body keeps it: `{kind, provider?, image?, gpu?}`, nothing else: kind and provider ids, image a name
|
|
48
|
+
* with no spaces around it, and gpu `{type, count}`, a GPU type name and a whole count from 1. */
|
|
49
49
|
function computerOf(computer) {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const image = isObject(computer) ? computer.image : undefined;
|
|
51
|
+
const named = (value) => typeof value === "string" && value !== "" && value === value.trim();
|
|
52
|
+
if (!isObject(computer) || !hostId(computer.kind) || (computer.provider !== undefined && !hostId(computer.provider)) || (image !== undefined && !named(image)) || Object.keys(computer).some((key) => key !== "kind" && key !== "provider" && key !== "image" && key !== "gpu"))
|
|
53
|
+
invalid("the computer is not {kind, provider?, image?, gpu?}, kind and provider each an id (lowercase letters, digits and dashes), image a name");
|
|
52
54
|
const ok = computer;
|
|
53
55
|
const gpu = ok.gpu;
|
|
54
56
|
if (gpu !== undefined && (!isObject(gpu) || Object.keys(gpu).some((key) => key !== "type" && key !== "count") || typeof gpu.type !== "string" || !GPU_TYPE.test(gpu.type) || !Number.isSafeInteger(gpu.count) || gpu.count < 1)) {
|
|
55
57
|
invalid("the computer's gpu is not {type, count}: a GPU type name (letters, digits, dots, underscores and dashes) and a whole count from 1");
|
|
56
58
|
}
|
|
57
59
|
const kept = gpu;
|
|
58
|
-
return { kind: ok.kind, ...(ok.provider !== undefined ? { provider: ok.provider } : {}), ...(kept !== undefined ? { gpu: { type: kept.type, count: kept.count } } : {}) };
|
|
60
|
+
return { kind: ok.kind, ...(ok.provider !== undefined ? { provider: ok.provider } : {}), ...(ok.image !== undefined ? { image: ok.image } : {}), ...(kept !== undefined ? { gpu: { type: kept.type, count: kept.count } } : {}) };
|
|
59
61
|
}
|
|
60
62
|
/** The body of a new revision and the text of each skill it names (path to text), everything checked here, before any
|
|
61
63
|
* write. The body keeps each skill path to its text's SHA-256; `skills` is the texts, for the revision's row. */
|
|
@@ -111,6 +113,8 @@ export function bodyOf(input) {
|
|
|
111
113
|
invalid("learning is not true or false");
|
|
112
114
|
if (input.outputSchema !== undefined && !isObject(input.outputSchema))
|
|
113
115
|
invalid("the outputSchema is not a JSON object");
|
|
116
|
+
if (input.maxToolCalls !== undefined && !(Number.isSafeInteger(input.maxToolCalls) && input.maxToolCalls > 0))
|
|
117
|
+
invalid("the maxToolCalls is not a whole number above 0");
|
|
114
118
|
const body = {
|
|
115
119
|
instructions: input.instructions,
|
|
116
120
|
skills: skillShas,
|
|
@@ -122,6 +126,8 @@ export function bodyOf(input) {
|
|
|
122
126
|
...(input.learning !== undefined ? { learning: input.learning } : {}),
|
|
123
127
|
// The JSON Schema its final answer's structured part meets (definition.output_schema): a completed run's result.output.
|
|
124
128
|
...(input.outputSchema !== undefined ? { outputSchema: structuredClone(input.outputSchema) } : {}),
|
|
129
|
+
// The tool calls each conversation of its runs may make (tool-budget.ts).
|
|
130
|
+
...(input.maxToolCalls !== undefined ? { maxToolCalls: input.maxToolCalls } : {}),
|
|
125
131
|
// The agent's unattended policy (PR #2003): the runner's gate reads it from the revision body at claim (`unattended`
|
|
126
132
|
// below: "never" alone is kept).
|
|
127
133
|
...capped,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** How often an open transaction's deadline looks at the process's event loop, and how far past its look a look may
|
|
2
|
+
* come before the gap counts as the process held up (runnableDeadline). */
|
|
3
|
+
export declare const TICK_MS = 50;
|
|
4
|
+
export declare const STALL_MS = 50;
|
|
5
|
+
/** A running deadline: `clear()` stops it; `held()` is the time it gave back so far, the process held up, so a bound
|
|
6
|
+
* reckoned from the deadline's due time (a Postgres commit's) moves with it. A hold that ended since the last look is
|
|
7
|
+
* counted as `held()` reads it, never left for a look that would come after the bound was reckoned. */
|
|
8
|
+
export type RunnableDeadline = {
|
|
9
|
+
clear(): void;
|
|
10
|
+
held(): number;
|
|
11
|
+
};
|
|
12
|
+
/** Calls `expire` once `ms` of the time this process could run have passed, counted from now on the monotonic clock
|
|
13
|
+
* (performance.now: the host's clock set forward or back is never counted). A look every TICK_MS measures the event
|
|
14
|
+
* loop: a gap more than STALL_MS past its look was the process held up by other work (a harness binary hashed, a
|
|
15
|
+
* command run synchronously, the machine holding the process), wherever in the transaction it fell, before its
|
|
16
|
+
* deadline or across it, and is not counted. A wait of the transaction's own (a call awaited inside it) leaves the
|
|
17
|
+
* loop free, so it counts in full and is refused on time. */
|
|
18
|
+
export declare function runnableDeadline(ms: number, expire: () => void): RunnableDeadline;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A store transaction's deadline counted from its begin, the one rule both connections keep (sqlite/connection.ts,
|
|
2
|
+
// postgres/connection.ts): only the time this process could run counts. One counted from its call (a lease's renew, a
|
|
3
|
+
// fromCall transaction) stays each connection's wall-clock timer.
|
|
4
|
+
/** How often an open transaction's deadline looks at the process's event loop, and how far past its look a look may
|
|
5
|
+
* come before the gap counts as the process held up (runnableDeadline). */
|
|
6
|
+
export const TICK_MS = 50;
|
|
7
|
+
export const STALL_MS = 50;
|
|
8
|
+
/** Calls `expire` once `ms` of the time this process could run have passed, counted from now on the monotonic clock
|
|
9
|
+
* (performance.now: the host's clock set forward or back is never counted). A look every TICK_MS measures the event
|
|
10
|
+
* loop: a gap more than STALL_MS past its look was the process held up by other work (a harness binary hashed, a
|
|
11
|
+
* command run synchronously, the machine holding the process), wherever in the transaction it fell, before its
|
|
12
|
+
* deadline or across it, and is not counted. A wait of the transaction's own (a call awaited inside it) leaves the
|
|
13
|
+
* loop free, so it counts in full and is refused on time. */
|
|
14
|
+
export function runnableDeadline(ms, expire) {
|
|
15
|
+
const start = performance.now();
|
|
16
|
+
let last = start;
|
|
17
|
+
let held = 0;
|
|
18
|
+
const look = () => {
|
|
19
|
+
const now = performance.now();
|
|
20
|
+
const gap = now - last - TICK_MS;
|
|
21
|
+
if (gap > STALL_MS)
|
|
22
|
+
held += gap;
|
|
23
|
+
last = now;
|
|
24
|
+
return now;
|
|
25
|
+
};
|
|
26
|
+
const ticks = setInterval(look, TICK_MS);
|
|
27
|
+
let timer;
|
|
28
|
+
const check = () => {
|
|
29
|
+
const left = ms - (look() - start - held);
|
|
30
|
+
if (left <= 0)
|
|
31
|
+
return expire();
|
|
32
|
+
timer = setTimeout(check, Math.ceil(left));
|
|
33
|
+
};
|
|
34
|
+
timer = setTimeout(check, ms);
|
|
35
|
+
return {
|
|
36
|
+
clear: () => {
|
|
37
|
+
clearInterval(ticks);
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
},
|
|
40
|
+
held: () => {
|
|
41
|
+
look();
|
|
42
|
+
return held;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -58,6 +58,10 @@ export declare class Unanswered extends Error {
|
|
|
58
58
|
* (a runner's 35 s graceful stop, the server's own) instead of exiting, and a lost lock is never a 503 forever. */
|
|
59
59
|
export declare const OWNER_WAIT_MS = 120000;
|
|
60
60
|
export declare function numberParams(sql: string): string;
|
|
61
|
+
/** A Postgres error as the store's own: a refused row StoreConstraint, a lock or a conflict StoreBusy. */
|
|
62
|
+
/** Whether `error` says the database ended the session (its idle-in-transaction timeout, SQLSTATE 25P03), or that the
|
|
63
|
+
* connection is gone and takes no more statements: a transaction's work on it was rolled back by the database. */
|
|
64
|
+
export declare function sessionEnded(error: unknown): boolean;
|
|
61
65
|
export declare class PgConnection implements StoreConnection {
|
|
62
66
|
#private;
|
|
63
67
|
readonly dialect: Dialect;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// no row rolls its transaction back with the typed error, as SQLite's worker does. A server's connection also holds the
|
|
6
6
|
// store's owner lock (one server per schema): a session advisory lock on a client of its own, held until close.
|
|
7
7
|
import pg from "pg";
|
|
8
|
+
import { runnableDeadline } from "../deadline.js";
|
|
8
9
|
import { cleanText } from "../dialect.js";
|
|
9
10
|
import { Fenced, RunFinal, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreOwned } from "../store.js";
|
|
10
11
|
import { DEFAULT_TRANSACTION_DEADLINE_MS } from "../sqlite/connection.js";
|
|
@@ -217,6 +218,15 @@ async function withUnique(error, cause, columnsOf) {
|
|
|
217
218
|
return columns.length ? new StoreConstraint(error.message, { rule: "unique", unique: { table: e.table, columns } }) : error;
|
|
218
219
|
}
|
|
219
220
|
/** A Postgres error as the store's own: a refused row StoreConstraint, a lock or a conflict StoreBusy. */
|
|
221
|
+
/** Whether `error` says the database ended the session (its idle-in-transaction timeout, SQLSTATE 25P03), or that the
|
|
222
|
+
* connection is gone and takes no more statements: a transaction's work on it was rolled back by the database. */
|
|
223
|
+
export function sessionEnded(error) {
|
|
224
|
+
const e = error;
|
|
225
|
+
if (e?.code === "25P03")
|
|
226
|
+
return true;
|
|
227
|
+
const message = typeof e?.message === "string" ? e.message : "";
|
|
228
|
+
return /not queryable|Connection terminated/i.test(message);
|
|
229
|
+
}
|
|
220
230
|
function typed(error, deadlineMs) {
|
|
221
231
|
// Already the store's own (a failed statement's error, met again at the commit of a callback that caught it).
|
|
222
232
|
if (error instanceof Fenced || error instanceof RunFinal || error instanceof StoreClosed || error instanceof StoreDeadline || error instanceof StoreConstraint || error instanceof StoreBusy || error instanceof Unanswered)
|
|
@@ -518,6 +528,15 @@ export class PgConnection {
|
|
|
518
528
|
}
|
|
519
529
|
let live = true;
|
|
520
530
|
let broken;
|
|
531
|
+
// The database may end the session while the transaction waits between statements (its idle-in-transaction timeout:
|
|
532
|
+
// this process held up past the deadline and its slack): heard here, a statement or the commit after it answers
|
|
533
|
+
// StoreDeadline (home_busy, retryable), since the database rolled the transaction back and nothing of it was written.
|
|
534
|
+
let ended;
|
|
535
|
+
const onEnded = (error) => {
|
|
536
|
+
ended ??= error;
|
|
537
|
+
};
|
|
538
|
+
client.on("error", onEnded);
|
|
539
|
+
const lost = (error) => (ended !== undefined || sessionEnded(error) ? new StoreDeadline(deadlineMs) : undefined);
|
|
521
540
|
const check = () => {
|
|
522
541
|
if (!live)
|
|
523
542
|
throw new Error("the store's transaction is over: its handle takes no more statements");
|
|
@@ -541,9 +560,11 @@ export class PgConnection {
|
|
|
541
560
|
return await step(client, statement, columnsOf, deadlineMs);
|
|
542
561
|
}
|
|
543
562
|
catch (error) {
|
|
544
|
-
// A statement that failed has aborted the transaction: no later statement of it runs.
|
|
545
|
-
|
|
546
|
-
|
|
563
|
+
// A statement that failed has aborted the transaction: no later statement of it runs. On a session the database
|
|
564
|
+
// ended, it is the transaction's deadline.
|
|
565
|
+
const failure = lost(error) ?? error;
|
|
566
|
+
broken ??= failure;
|
|
567
|
+
throw failure;
|
|
547
568
|
}
|
|
548
569
|
};
|
|
549
570
|
const tx = {
|
|
@@ -560,15 +581,26 @@ export class PgConnection {
|
|
|
560
581
|
// drop, a failover) is bounded from its first statement on, not only once BEGIN has answered. Past it the client is
|
|
561
582
|
// let go with the error, which destroys its socket (Postgres rolls a dead session's transaction back), never
|
|
562
583
|
// waiting on a ROLLBACK that a silent connection would never answer.
|
|
584
|
+
// Counted from its begin, the deadline is the time this process could run (store/deadline.ts runnableDeadline, the
|
|
585
|
+
// rule SQLite's keeps): a statement answered while other work held the event loop waited behind it, and that time
|
|
586
|
+
// was no statement's, so a write that was fine is never rolled back for it. Counted from its call (a lease's), it is
|
|
587
|
+
// the wall clock's.
|
|
563
588
|
let timer;
|
|
564
|
-
const deadlineAt =
|
|
589
|
+
const deadlineAt = performance.now() + ms;
|
|
565
590
|
const deadline = new Promise((_, reject) => {
|
|
566
|
-
|
|
591
|
+
const expire = () => reject(new StoreDeadline(deadlineMs));
|
|
592
|
+
if (fromCall) {
|
|
593
|
+
const wall = setTimeout(expire, ms);
|
|
594
|
+
timer = { clear: () => clearTimeout(wall), held: () => 0 };
|
|
595
|
+
}
|
|
596
|
+
else
|
|
597
|
+
timer = runnableDeadline(ms, expire);
|
|
567
598
|
});
|
|
568
599
|
deadline.catch(() => undefined);
|
|
569
600
|
const abandon = (error) => {
|
|
570
601
|
live = false;
|
|
571
|
-
|
|
602
|
+
timer?.clear();
|
|
603
|
+
client.off("error", onEnded);
|
|
572
604
|
// Its socket destroyed here: with no query in flight (a deadline between statements) pg's own end would send a
|
|
573
605
|
// Terminate and wait for a FIN a silent connection never answers.
|
|
574
606
|
client.connection?.stream?.destroy();
|
|
@@ -596,19 +628,32 @@ export class PgConnection {
|
|
|
596
628
|
throw error;
|
|
597
629
|
}
|
|
598
630
|
live = false;
|
|
599
|
-
|
|
631
|
+
timer?.clear();
|
|
632
|
+
client.off("error", onEnded);
|
|
600
633
|
await rollBackAndRelease(client);
|
|
601
634
|
throw error;
|
|
602
635
|
}
|
|
603
636
|
live = false;
|
|
604
|
-
|
|
637
|
+
timer?.clear();
|
|
638
|
+
// Ended before its commit is sent: nothing of it was written, and the commit is never sent on a gone session.
|
|
639
|
+
const endedBefore = ended;
|
|
605
640
|
try {
|
|
641
|
+
if (endedBefore)
|
|
642
|
+
throw endedBefore;
|
|
606
643
|
// A callback that caught a failed statement's error and went on still rolls back: Postgres aborted the transaction.
|
|
607
644
|
if (broken)
|
|
608
645
|
throw broken;
|
|
609
|
-
await answered(client.query("COMMIT"), Math.max(COMMIT_FLOOR_MS, deadlineAt -
|
|
646
|
+
await answered(client.query("COMMIT"), Math.max(COMMIT_FLOOR_MS, deadlineAt + (timer?.held() ?? 0) - performance.now()), "its commit");
|
|
610
647
|
}
|
|
611
648
|
catch (error) {
|
|
649
|
+
if (endedBefore !== undefined || error?.code === "25P03") {
|
|
650
|
+
client.off("error", onEnded);
|
|
651
|
+
client.release(error);
|
|
652
|
+
if (read)
|
|
653
|
+
return value;
|
|
654
|
+
throw new StoreDeadline(deadlineMs);
|
|
655
|
+
}
|
|
656
|
+
client.off("error", onEnded);
|
|
612
657
|
if (error instanceof Unanswered) {
|
|
613
658
|
client.release(error);
|
|
614
659
|
// A read's snapshot has nothing to land: what it read stands.
|
|
@@ -619,6 +664,7 @@ export class PgConnection {
|
|
|
619
664
|
await rollBackAndRelease(client);
|
|
620
665
|
throw typed(error, deadlineMs);
|
|
621
666
|
}
|
|
667
|
+
client.off("error", onEnded);
|
|
622
668
|
client.release();
|
|
623
669
|
return value;
|
|
624
670
|
});
|
|
@@ -5,47 +5,12 @@
|
|
|
5
5
|
// rolls back. Errors come back typed: Fenced, RunFinal, StoreBusy, StoreConstraint, StoreClosed, StoreOwned.
|
|
6
6
|
import { realpathSync } from "node:fs";
|
|
7
7
|
import { Worker } from "node:worker_threads";
|
|
8
|
+
import { runnableDeadline } from "../deadline.js";
|
|
8
9
|
import { cleanText, SQLITE } from "../dialect.js";
|
|
9
10
|
import { inMemory } from "./memory.js";
|
|
10
11
|
import { Fenced, RunFinal, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreOwned, StoreWorkerLost } from "../store.js";
|
|
11
12
|
/** How long a transaction may stay open (it holds every other call of the process) before it is rolled back. */
|
|
12
13
|
export const DEFAULT_TRANSACTION_DEADLINE_MS = 10_000;
|
|
13
|
-
/** How often an open transaction's deadline looks at the process's event loop, and how far past its look a look may
|
|
14
|
-
* come before the gap counts as the process held up (runnableDeadline). */
|
|
15
|
-
const TICK_MS = 50;
|
|
16
|
-
const STALL_MS = 50;
|
|
17
|
-
/** Calls `expire` once `ms` of the time this process could run have passed, counted from now on the monotonic clock
|
|
18
|
-
* (performance.now: the host's clock set forward or back is never counted). A look every TICK_MS measures the event
|
|
19
|
-
* loop: a gap more than STALL_MS past its look was the process held up by other work (a harness binary hashed, a
|
|
20
|
-
* command run synchronously, the machine holding the process), wherever in the transaction it fell, before its
|
|
21
|
-
* deadline or across it, and is not counted. A wait of the transaction's own (a call awaited inside it) leaves the
|
|
22
|
-
* loop free, so it counts in full and is refused on time. Answers its clear. */
|
|
23
|
-
function runnableDeadline(ms, expire) {
|
|
24
|
-
const start = performance.now();
|
|
25
|
-
let last = start;
|
|
26
|
-
let held = 0;
|
|
27
|
-
const look = () => {
|
|
28
|
-
const now = performance.now();
|
|
29
|
-
const gap = now - last - TICK_MS;
|
|
30
|
-
if (gap > STALL_MS)
|
|
31
|
-
held += gap;
|
|
32
|
-
last = now;
|
|
33
|
-
return now;
|
|
34
|
-
};
|
|
35
|
-
const ticks = setInterval(look, TICK_MS);
|
|
36
|
-
let timer;
|
|
37
|
-
const check = () => {
|
|
38
|
-
const left = ms - (look() - start - held);
|
|
39
|
-
if (left <= 0)
|
|
40
|
-
return expire();
|
|
41
|
-
timer = setTimeout(check, Math.ceil(left));
|
|
42
|
-
};
|
|
43
|
-
timer = setTimeout(check, ms);
|
|
44
|
-
return () => {
|
|
45
|
-
clearInterval(ticks);
|
|
46
|
-
clearTimeout(timer);
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
14
|
/** A statement's text parameters as the store writes text (dialect.ts cleanText: no NUL), on both stores alike. */
|
|
50
15
|
const cleaned = (params) => (params ?? []).map((value) => (typeof value === "string" ? cleanText(value) : value));
|
|
51
16
|
const cleanStatement = (statement) => ({ ...statement, params: cleaned(statement.params) });
|
|
@@ -274,14 +239,14 @@ export class Connection {
|
|
|
274
239
|
const deadline = new Promise((_, reject) => {
|
|
275
240
|
const expire = () => reject(new StoreDeadline(deadlineMs));
|
|
276
241
|
// Counted from its call (a lease's), the deadline stays the wall clock's: MCP_REFRESH_LEASE_MS is reckoned from
|
|
277
|
-
// it. Counted from its begin, it is the time this process could run (runnableDeadline): while other work held
|
|
242
|
+
// it. Counted from its begin, it is the time this process could run (store/deadline.ts runnableDeadline): while other work held
|
|
278
243
|
// the event loop, a statement answered meanwhile waited behind it, and that time was no statement's.
|
|
279
244
|
if (until !== undefined) {
|
|
280
245
|
const timer = setTimeout(expire, Math.max(0, until - Date.now()));
|
|
281
246
|
clear = () => clearTimeout(timer);
|
|
282
247
|
}
|
|
283
248
|
else
|
|
284
|
-
clear = runnableDeadline(deadlineMs, expire);
|
|
249
|
+
clear = runnableDeadline(deadlineMs, expire).clear;
|
|
285
250
|
});
|
|
286
251
|
let value;
|
|
287
252
|
try {
|
|
@@ -8,6 +8,7 @@ import { RUNNER_RECORDS } from "@parcha/agentrun-kernel";
|
|
|
8
8
|
import { TOOL_SESSION } from "@parcha/agentrun-kernel/kernel/cap";
|
|
9
9
|
import { Hearing } from "../heard.js";
|
|
10
10
|
import { redactedAnswer } from "../key-refusal.js";
|
|
11
|
+
import { LEARNED_NODES, LEARNED_NODES_MAX } from "../learned-nodes.js";
|
|
11
12
|
import { labelled } from "../reflect-range.js";
|
|
12
13
|
import { reflectFrom } from "../runner/end-of-run.js";
|
|
13
14
|
import { journalEntries, journalRecords } from "../runner/journal-reader.js";
|
|
@@ -35,7 +36,8 @@ export async function runReflect(store, runId, context) {
|
|
|
35
36
|
* tool call its assistant tool-call and its result. So a claude-agent / codex / opencode run reads the same rows, words
|
|
36
37
|
* and final answer as a pi run of the same conversation. A run's own pi entries and its end-of-run turn (already model
|
|
37
38
|
* shaped) pass through unchanged, so a pi run is untouched. */
|
|
38
|
-
/** The calls the CLI record holds parked at
|
|
39
|
+
/** The calls the CLI record holds parked at an MCP door (a start marked parked, not in its tool bridge) with no sent mark
|
|
40
|
+
* and no end. */
|
|
39
41
|
function parkedUnsent(entries) {
|
|
40
42
|
const parked = new Set();
|
|
41
43
|
const past = new Set();
|
|
@@ -43,7 +45,8 @@ function parkedUnsent(entries) {
|
|
|
43
45
|
const callId = entry.data?.callId;
|
|
44
46
|
if (typeof callId !== "string")
|
|
45
47
|
continue;
|
|
46
|
-
|
|
48
|
+
const start = entry.data;
|
|
49
|
+
if (entry.kind === KINDS.callStart && start.parked === true && start.bridge !== true)
|
|
47
50
|
parked.add(callId);
|
|
48
51
|
else if (entry.kind === KINDS.callSent || entry.kind === KINDS.callEnd)
|
|
49
52
|
past.add(callId);
|
|
@@ -151,6 +154,12 @@ export function withPaidCosts(entries) {
|
|
|
151
154
|
}
|
|
152
155
|
/** Whether `entries` are a CLI harness's record (its loop's calls and messages). */
|
|
153
156
|
export const isCliRecord = (entries) => entries.some((entry) => entry.kind === KINDS.message || entry.kind === KINDS.callStart || entry.kind === KINDS.callEnd);
|
|
157
|
+
/** A path as a run's rows name it, for a harness working in `cwd`: one under `cwd` by its name there (`hello.md`), any
|
|
158
|
+
* other as given. */
|
|
159
|
+
const inWorkspace = (cwd) => (path) => {
|
|
160
|
+
const root = cwd.replace(/\/+$/, "");
|
|
161
|
+
return path.startsWith(`${root}/`) && path.length > root.length + 1 ? path.slice(root.length + 1) : path;
|
|
162
|
+
};
|
|
154
163
|
/** A run's timeline rows, the renderer (timelineOf) unchanged, over the one store's entries, with a CLI harness's record
|
|
155
164
|
* rendered as its pi-shaped entries (piShaped) and its paid tool charges read back from the paid-tools session
|
|
156
165
|
* (withPaidCosts). A run whose record carries no per-request cost (a CLI harness's, a workflow's: metrics.ts
|
|
@@ -161,9 +170,35 @@ export const isCliRecord = (entries) => entries.some((entry) => entry.kind === K
|
|
|
161
170
|
export async function runTimeline(store, runId, context, options = {}) {
|
|
162
171
|
const entries = await runEntries(store, runId, context);
|
|
163
172
|
const cli = isCliRecord(entries);
|
|
164
|
-
|
|
173
|
+
// A CLI names a file by its absolute path under its working directory (CallStartData.cwd): the rows name it by its
|
|
174
|
+
// name in the workspace, never a path on the runner's machine. Its arguments, as the harness sent them, stay.
|
|
175
|
+
const cwd = cli && !options.path ? entries.find((entry) => entry.kind === KINDS.callStart && typeof entry.data?.cwd === "string")?.data : undefined;
|
|
176
|
+
const rendered = timelineOf(withPaidCosts(cli ? piShaped(entries) : entries), cwd ? { ...options, path: inWorkspace(cwd.cwd) } : options);
|
|
165
177
|
const rows = costIsBooked(entries, cli) ? bookedUsage(rendered, entries, options) : rendered;
|
|
166
|
-
return labelled((await runReflect(store, runId, context)), await datedRows(store, runId, rows));
|
|
178
|
+
return labelled((await runReflect(store, runId, context)), withLearnedNodes(await datedRows(store, runId, rows), entries, options.redact));
|
|
179
|
+
}
|
|
180
|
+
/** `rows` with each learned_workflow call's result row carrying the nodes its workflow ran (`learned_nodes`), from the
|
|
181
|
+
* LEARNED_NODES entry the call kept beside itself (learned-workflow.ts), joined by the call's id: each label through
|
|
182
|
+
* `redact` when given (the run's cut), as every text a row shows; never a node's error text. Rows of a run that ran none are as they were. */
|
|
183
|
+
function withLearnedNodes(rows, entries, redact) {
|
|
184
|
+
const traces = new Map();
|
|
185
|
+
const said = (text) => (redact ? redact(text) : text);
|
|
186
|
+
for (const entry of entries) {
|
|
187
|
+
if (entry.kind !== LEARNED_NODES)
|
|
188
|
+
continue;
|
|
189
|
+
const data = (entry.data ?? {});
|
|
190
|
+
if (typeof data.call !== "string" || !Array.isArray(data.nodes))
|
|
191
|
+
continue;
|
|
192
|
+
traces.set(data.call, data.nodes.slice(0, LEARNED_NODES_MAX).flatMap((node) => {
|
|
193
|
+
const one = (node ?? {});
|
|
194
|
+
if (typeof one.label !== "string" || typeof one.status !== "string")
|
|
195
|
+
return [];
|
|
196
|
+
return [{ label: said(one.label), kind: String(one.kind ?? ""), status: one.status, duration_ms: typeof one.duration_ms === "number" ? one.duration_ms : 0 }];
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
if (!traces.size)
|
|
200
|
+
return rows;
|
|
201
|
+
return rows.map((row) => (row.kind === "tool_result" && row.tool_use_id && traces.has(row.tool_use_id) ? { ...row, learned_nodes: traces.get(row.tool_use_id) } : row));
|
|
167
202
|
}
|
|
168
203
|
/** A CLI or workflow run's `rows` with its usage row in its booking's words (runner/metrics.ts bookedSpend over every
|
|
169
204
|
* session of the run but its decisions'): its model requests and its paid tool calls, their sum (unknown while one's cost is, with what is known so far,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type ItemStatus, type Learning, type NoteUsed, type Shown } from "../learning.js";
|
|
2
2
|
import { type RunLaunch } from "../run-launch.js";
|
|
3
3
|
import { type Impact } from "../learning-run.js";
|
|
4
|
-
import type
|
|
4
|
+
import { type Reflect } from "../reflect-range.js";
|
|
5
5
|
import type { NoteRuns } from "../learning.js";
|
|
6
|
-
import type { TimelineEvent } from "../timeline/index.js";
|
|
6
|
+
import type { EntryLike, TimelineEvent } from "../timeline/index.js";
|
|
7
7
|
import type { AnalyticsFilter } from "./analytics.js";
|
|
8
8
|
import type { Store } from "../store/store.js";
|
|
9
9
|
/** A run's learning end: the Learning fields its end sets. */
|
|
@@ -51,6 +51,15 @@ export declare function agentLearningOverStore(store: Store, agentId: string): P
|
|
|
51
51
|
impact: Impact;
|
|
52
52
|
notes: NoteRuns;
|
|
53
53
|
}>;
|
|
54
|
+
/** What a run's claim laid of its learning seed: the learned skills and workflows it was given, by name. */
|
|
55
|
+
type Laid = {
|
|
56
|
+
readonly skills: readonly string[];
|
|
57
|
+
readonly workflows: readonly string[];
|
|
58
|
+
};
|
|
59
|
+
/** What every claim of a run laid, from its learningLaid entries, each name once: a resumed claim finds an earlier
|
|
60
|
+
* claim's skill folders there already and lays none of them again, though the run still has them. None laid when it has
|
|
61
|
+
* no entry. */
|
|
62
|
+
export declare function laidOf(entries: readonly EntryLike[]): Laid;
|
|
54
63
|
/** A run's learning end with the notes its start gave it (`given`, when it started from a seed) and those its job read
|
|
55
64
|
* (`read`, only when it read one): what a run view and GET /runs/{id}/learning show beside the end. */
|
|
56
65
|
export type RunLearning = RunLearningEnd & {
|
|
@@ -61,6 +70,41 @@ export type RunLearning = RunLearningEnd & {
|
|
|
61
70
|
* `read` calls on a file of its copy of the folder, the end-of-run turn's (`reflect`) left out, keyed on the notes it was
|
|
62
71
|
* `given` and named as given. A file it was not given (MEMORY.md, one it wrote, one not in its seed) is not one of them. */
|
|
63
72
|
export declare function notesReadOf(given: readonly NoteUsed[] | undefined, reflect: Pick<Reflect, "after" | "until"> | undefined, rows: readonly TimelineEvent[]): NoteUsed[];
|
|
73
|
+
/** What a run's job did with the learned skills and workflows its launch offered it (GET /runs/{id}/learning `used`:
|
|
74
|
+
* its `skills` and `workflows`, apart from the skills and workflows it learned): each skill, and whether the job read its SKILL.md (a read of `.agents/skills/<name>/SKILL.md`, or a
|
|
75
|
+
* harness's own skill tool on it); each learned workflow the job called, by the call's `name` (callArgsOf), how
|
|
76
|
+
* many calls and how many failed. The end-of-run turn's rows are left out, as notesRead leaves them. */
|
|
77
|
+
export type LearnedUse = {
|
|
78
|
+
readonly skills: readonly {
|
|
79
|
+
readonly name: string;
|
|
80
|
+
readonly read: boolean;
|
|
81
|
+
}[];
|
|
82
|
+
readonly workflows: readonly {
|
|
83
|
+
readonly name: string;
|
|
84
|
+
readonly calls: number;
|
|
85
|
+
readonly failed: number;
|
|
86
|
+
}[];
|
|
87
|
+
};
|
|
88
|
+
/** Each tool call of a run's journal, by its call's id: its tool and the arguments it was called with, as recorded (pi's
|
|
89
|
+
* call in its assistant entry, a CLI harness's in its record's call start). A timeline row's input preview is cut and
|
|
90
|
+
* for some tools absent, so what a call read or ran is read from these. */
|
|
91
|
+
export declare function callArgsOf(entries: readonly EntryLike[]): Map<string, {
|
|
92
|
+
tool: string;
|
|
93
|
+
args: Record<string, unknown>;
|
|
94
|
+
}>;
|
|
95
|
+
/** The skills a run's job loaded with a harness's own Skill tool (claude-agent's): a native call, which the CLI's session
|
|
96
|
+
* records as a kernel step of its own (`kernel.start` named `tool.Skill`, keyed `tool:<call id>`, then its `kernel.end`)
|
|
97
|
+
* and never as a host tool's call record, so no timeline row or callArgsOf entry has it. Each by the name the model
|
|
98
|
+
* gave (`agentrun:<name>` or the bare one), only when its step completed, and only outside every end-of-run turn (a
|
|
99
|
+
* reopened run's follow-up's loads are the job's). */
|
|
100
|
+
export declare function nativeSkillLoads(entries: readonly EntryLike[]): string[];
|
|
101
|
+
/** `laid`: what the run's claims laid (laidOf): a skill its launch offered but no claim laid (over the budget, or a name
|
|
102
|
+
* the revision's own skill holds, whose copy is the one installed) is not the run's learned skill, whatever it read.
|
|
103
|
+
* `loads`: the skills its job loaded with a harness's own Skill tool (nativeSkillLoads), each a read of its SKILL.md. */
|
|
104
|
+
export declare function learnedUseOf(started: RunLaunch["learning"], reflect: Pick<Reflect, "after" | "until"> | undefined, rows: readonly TimelineEvent[], args?: ReadonlyMap<string, {
|
|
105
|
+
tool: string;
|
|
106
|
+
args: Record<string, unknown>;
|
|
107
|
+
}>, laid?: Pick<Laid, "skills">, loads?: readonly string[]): LearnedUse;
|
|
64
108
|
/** `end` with its run's given notes (from its launch's learning start) and the notes its job read. */
|
|
65
109
|
export declare function withNotes(end: RunLearningEnd, started: RunLaunch["learning"], read: readonly NoteUsed[]): RunLearning;
|
|
66
110
|
/** GET /runs/{id}/learning from the one store, in the shape the route gives (learning-routes.ts runLearningBody): the
|
|
@@ -98,6 +142,17 @@ export declare function runLearningOverStore(store: Store, runId: string, shown:
|
|
|
98
142
|
tokens: {
|
|
99
143
|
grade: import("../learning.js").Tokens | null;
|
|
100
144
|
};
|
|
145
|
+
used: {
|
|
146
|
+
skills: {
|
|
147
|
+
name: string;
|
|
148
|
+
read: boolean;
|
|
149
|
+
}[];
|
|
150
|
+
workflows: {
|
|
151
|
+
name: string;
|
|
152
|
+
calls: number;
|
|
153
|
+
failed: number;
|
|
154
|
+
}[];
|
|
155
|
+
};
|
|
101
156
|
given?: NoteUsed[];
|
|
102
157
|
read?: NoteUsed[];
|
|
103
158
|
learned?: import("../learning.js").NoteLearned[];
|
|
@@ -109,3 +164,4 @@ export declare function runLearningOverStore(store: Store, runId: string, shown:
|
|
|
109
164
|
detail: string | null;
|
|
110
165
|
at: string;
|
|
111
166
|
} | null>;
|
|
167
|
+
export {};
|