@parcha/agentrun-kernel 0.1.0-beta.34 → 0.1.0-beta.36
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 +40 -0
- package/README.md +0 -7
- package/dist/host/box.d.ts +0 -14
- package/dist/host/index.d.ts +4 -4
- package/dist/host/index.js +2 -2
- package/dist/host/overhead.d.ts +75 -4
- package/dist/host/overhead.js +220 -10
- package/dist/host/proxy.d.ts +40 -4
- package/dist/host/proxy.js +87 -3
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -1
- package/dist/kernel/cap/index.d.ts +1 -1
- package/dist/kernel/cap/index.js +1 -1
- package/dist/kernel/cap/port.d.ts +3 -2
- package/dist/kernel/cap/port.js +2 -2
- package/dist/kernel/cap/price.js +5 -2
- package/dist/kernel/cap/run-cap.d.ts +2 -2
- package/dist/kernel/cap/run-cap.js +1 -1
- package/dist/kernel/cap/types.d.ts +7 -0
- package/dist/kernel/cap/words.d.ts +3 -0
- package/dist/kernel/cap/words.js +5 -2
- package/dist/kernel/index.d.ts +0 -2
- package/dist/kernel/index.js +0 -1
- package/dist/kernel/registry/index.d.ts +1 -1
- package/dist/kernel/registry/index.js +1 -1
- package/dist/kernel/registry/limits.d.ts +0 -1
- package/dist/kernel/registry/limits.js +0 -1
- package/dist/process/records.d.ts +0 -16
- package/dist/process/records.js +0 -41
- package/dist/process/settle.d.ts +1 -1
- package/dist/process/settle.js +2 -2
- package/dist/process/tools.d.ts +9 -0
- package/dist/process/tools.js +19 -6
- package/dist/process/zod.d.ts +0 -2
- package/dist/process/zod.js +0 -4
- package/dist/runner.d.ts +8 -0
- package/dist/runner.js +3 -3
- package/dist/testing.d.ts +17 -5
- package/dist/testing.js +72 -21
- package/dist/types.d.ts +15 -5
- package/package.json +3 -3
- package/dist/delegate.d.ts +0 -87
- package/dist/delegate.js +0 -143
- package/dist/kernel/follow-up.d.ts +0 -67
- package/dist/kernel/follow-up.js +0 -146
package/dist/kernel/follow-up.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { foldSession, openDrive } from "./drive.js";
|
|
2
|
-
import { EffectFailure, EffectInterrupted } from "./errors.js";
|
|
3
|
-
/** The effect's key in the follow-up's own session. */
|
|
4
|
-
export const FOLLOW_UP_EFFECT = "follow-up";
|
|
5
|
-
/** What a request the reserve refuses says, first. */
|
|
6
|
-
export const FOLLOW_UP_BUDGET_REACHED = "follow_up_budget_reached";
|
|
7
|
-
/** What a request asked for after the follow-up was cancelled says, first. */
|
|
8
|
-
export const FOLLOW_UP_CANCELLED = "follow_up_cancelled";
|
|
9
|
-
/** The effect that records the follow-up's own cut turn settled: no later run settles another turn. */
|
|
10
|
-
const SETTLED_EFFECT = "follow-up.settled";
|
|
11
|
-
/** The run of each follow-up key in this process: a second run waits for it, never fences it. */
|
|
12
|
-
const runs = new WeakMap();
|
|
13
|
-
/** The follow-up's own session key, beside the session it follows up. */
|
|
14
|
-
export const followUpKey = (session, name) => `${session}:follow-up:${name}`;
|
|
15
|
-
/** The follow-up's state in its records, as written: `running` is one a process that died left without an outcome. A
|
|
16
|
-
* host reads it before it resumes an open turn: a turn of a `running` or `interrupted` follow-up is settled instead. */
|
|
17
|
-
export function followUpState(records) {
|
|
18
|
-
const effect = foldSession("", records).effects.get(FOLLOW_UP_EFFECT);
|
|
19
|
-
if (!effect)
|
|
20
|
-
return "none";
|
|
21
|
-
if (effect.status === "completed")
|
|
22
|
-
return "done";
|
|
23
|
-
if (effect.status === "failed")
|
|
24
|
-
return effect.error?.interrupted ? "interrupted" : "failed";
|
|
25
|
-
return "running";
|
|
26
|
-
}
|
|
27
|
-
export function followUpOnce(options) {
|
|
28
|
-
const { store, prompt, reserveUsd } = options;
|
|
29
|
-
const { worstUsd } = options;
|
|
30
|
-
if (typeof reserveUsd !== "number" || !Number.isFinite(reserveUsd) || reserveUsd <= 0)
|
|
31
|
-
throw new Error("reserveUsd must be a number of dollars above 0");
|
|
32
|
-
if (typeof worstUsd !== "number" || !Number.isFinite(worstUsd) || worstUsd < 0)
|
|
33
|
-
throw new Error("worstUsd must be given, a number of dollars, 0 or more");
|
|
34
|
-
const key = followUpKey(options.session, options.name);
|
|
35
|
-
// The id of the prompt's submission in the session: the follow-up's own, so a later run names it to the settle and a
|
|
36
|
-
// settle never withdraws input that is not the follow-up's.
|
|
37
|
-
const submission = `follow-up:${key}`;
|
|
38
|
-
let active = false;
|
|
39
|
-
let signalNow;
|
|
40
|
-
// The session's spentUsd when the follow-up's first request asks: the reserve counts from there. Whatever the session
|
|
41
|
-
// books after that counts against the reserve too: a task request whose cost was booked late (REQUEST_COST_LATE), and
|
|
42
|
-
// a harness's own background requests during the follow-up's turn (a CLI's small-model calls). Both refuse the
|
|
43
|
-
// follow-up sooner, never let it spend more.
|
|
44
|
-
let start;
|
|
45
|
-
const refuse = (why) => ({ refuse: `${FOLLOW_UP_BUDGET_REACHED}: ${why}` });
|
|
46
|
-
return {
|
|
47
|
-
key,
|
|
48
|
-
beforeRequest({ spentUsd, inFlight }) {
|
|
49
|
-
if (!active)
|
|
50
|
-
return undefined;
|
|
51
|
-
if (signalNow?.aborted)
|
|
52
|
-
return { refuse: `${FOLLOW_UP_CANCELLED}: the follow-up was cancelled; the request was not sent` };
|
|
53
|
-
if (spentUsd === null)
|
|
54
|
-
return refuse("what the session has spent is unknown, so the follow-up's reserve cannot be kept; the request was not sent");
|
|
55
|
-
start ??= spentUsd;
|
|
56
|
-
const used = spentUsd - start;
|
|
57
|
-
if (used + worstUsd * (inFlight.length + 1) > reserveUsd)
|
|
58
|
-
return refuse(`$${used.toFixed(6)} spent of a $${reserveUsd} reserve, and the next request could cost up to $${worstUsd}; the request was not sent`);
|
|
59
|
-
return undefined;
|
|
60
|
-
},
|
|
61
|
-
run(target, signal) {
|
|
62
|
-
const byKey = runs.get(store) ?? new Map();
|
|
63
|
-
runs.set(store, byKey);
|
|
64
|
-
const prior = byKey.get(key) ?? Promise.resolve();
|
|
65
|
-
// Waiting its turn, a run still hears its own signal: cancelled, it stops waiting and leaves the run before it alone.
|
|
66
|
-
const turn = new Promise((resolve, reject) => {
|
|
67
|
-
if (signal.aborted)
|
|
68
|
-
return reject(signal.reason);
|
|
69
|
-
const onAbort = () => reject(signal.reason);
|
|
70
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
71
|
-
void prior.then(() => {
|
|
72
|
-
signal.removeEventListener("abort", onAbort);
|
|
73
|
-
resolve();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
const run = turn.then(() => runOnce(target, signal));
|
|
77
|
-
// The next run of the key waits for the one before this, whether this one ran or gave up waiting.
|
|
78
|
-
const settled = prior.then(() => run.catch(() => undefined));
|
|
79
|
-
byKey.set(key, settled);
|
|
80
|
-
void settled.then(() => byKey.get(key) === settled && byKey.delete(key));
|
|
81
|
-
return run;
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
async function runOnce(target, signal) {
|
|
85
|
-
const drive = await openDrive({ writer: await store.open(key), signal });
|
|
86
|
-
const known = drive.state().effects.get(FOLLOW_UP_EFFECT);
|
|
87
|
-
if (known?.status === "completed")
|
|
88
|
-
return { status: "done" };
|
|
89
|
-
if (known?.status === "failed") {
|
|
90
|
-
if (!known.error?.interrupted)
|
|
91
|
-
return { status: "failed", error: known.error?.message ?? "failed" };
|
|
92
|
-
// Its own turn may still be open in the session: closed unsent, never resumed, and once. An open turn after that
|
|
93
|
-
// is another's, and is left alone.
|
|
94
|
-
if (drive.state().effects.get(SETTLED_EFFECT)?.status !== "completed") {
|
|
95
|
-
if (target.state === "open_turn")
|
|
96
|
-
await target.settleOpenTurn({ opening: submission });
|
|
97
|
-
await drive.effect(SETTLED_EFFECT, { name: SETTLED_EFFECT, replay: "safe", input: null }, async () => null);
|
|
98
|
-
}
|
|
99
|
-
return { status: "interrupted" };
|
|
100
|
-
}
|
|
101
|
-
if (target.state === "open_turn")
|
|
102
|
-
throw new Error("the session has an open turn that is not this follow-up's: recover it before the follow-up");
|
|
103
|
-
try {
|
|
104
|
-
await drive.effect(FOLLOW_UP_EFFECT, { name: options.name, replay: "never", input: { prompt } }, async () => {
|
|
105
|
-
if (signal.aborted)
|
|
106
|
-
throw signal.reason;
|
|
107
|
-
active = true;
|
|
108
|
-
signalNow = signal;
|
|
109
|
-
// A cancel stops the turn and ends the run at once: no outcome is written, so the next process reads the
|
|
110
|
-
// follow-up interrupted, and a turn that ends after it never answers done.
|
|
111
|
-
let onAbort = () => { };
|
|
112
|
-
const aborted = new Promise((_, reject) => {
|
|
113
|
-
onAbort = () => {
|
|
114
|
-
void target.interrupt().catch(() => undefined);
|
|
115
|
-
reject(signal.reason);
|
|
116
|
-
};
|
|
117
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
118
|
-
});
|
|
119
|
-
aborted.catch(() => undefined);
|
|
120
|
-
try {
|
|
121
|
-
// A send the session refuses because it holds the id already cannot strand the follow-up: the effect is
|
|
122
|
-
// `never`, so a crash after the submission landed is recovered interrupted, and the prompt is never sent again.
|
|
123
|
-
await Promise.race([target.send(prompt, { id: submission }), aborted]);
|
|
124
|
-
const end = await Promise.race([target.turnEnd(), aborted]);
|
|
125
|
-
if (signal.aborted)
|
|
126
|
-
throw signal.reason;
|
|
127
|
-
if (end.status === "interrupted")
|
|
128
|
-
throw new EffectInterrupted(end.error ?? "the follow-up's turn was interrupted");
|
|
129
|
-
if (end.status !== "completed")
|
|
130
|
-
throw new Error(end.error ?? "the follow-up's turn failed");
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
finally {
|
|
134
|
-
active = false;
|
|
135
|
-
signal.removeEventListener("abort", onAbort);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
return { status: "done" };
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
if (error instanceof EffectFailure)
|
|
142
|
-
return error.error.interrupted ? { status: "interrupted" } : { status: "failed", error: error.error.message };
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|