@nylorun/harness 0.36.8-beta → 0.37.0-beta
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 +17 -0
- package/dist/api/run.d.ts +23 -0
- package/dist/api/run.js +56 -1
- package/dist/loop/compaction/config.d.ts +5 -0
- package/dist/loop/compaction/index.d.ts +5 -0
- package/dist/loop/compaction/index.js +10 -0
- package/dist/loop/run.js +6 -5
- package/dist/loop/step/step-context.d.ts +6 -0
- package/dist/loop/step/step-context.js +38 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.37.0-beta
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- d688ce5: An Agent sets when its sessions compact (ADR-157): `harness.loop.compaction` (`loop.compaction` in `AGENT.md`) takes `threshold` and `keepTokens`, positive whole numbers of tokens, and `roles.<role>` over them for sessions opened with that role. Our harness compacts before a model call whose estimate passes the lower of `threshold` and the model's window minus the reply reserve, and keeps `keepTokens` word for word; unset, sessions compact as before. A value that is not a positive integer is ignored with an `engine.diagnostic` (`compaction_setting_ignored`), and the folder compiler refuses it. `turn.start` names the session's role (`role`) to our harness.
|
|
8
|
+
|
|
9
|
+
An `agent-folder/3` folder may now set `loop` for the harness's own settings, such as `compaction`; only `loop.plan`, `loop.connections` and `loop.ask`, which its plugins declare, are refused. `nylorun migrate folder --to 3` keeps those settings.
|
|
10
|
+
|
|
11
|
+
- d688ce5: A message's context (ADR-157, Host feature `message-context`): a message takes `context`, up to 16 items `{ type?, value }` and 16 KiB in all, facts for its own turn only. The Runtime keeps them on `command.message` and passes them to the turn as its input's `metadata.context`; our harness renders the turn's latest ones in the `<runtime-context>` block after the history on every model call of that turn, and later turns leave them out. A workflow session refuses them. `session.input(value, { context })` and `session.inputParts(parts, { context })` send them, and refuse before sending when the Runtime lacks the feature.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Pin core to the tested release.
|
|
16
|
+
- Updated dependencies [d688ce5]
|
|
17
|
+
- Updated dependencies [d688ce5]
|
|
18
|
+
- @nylorun/core@0.49.0-beta
|
|
19
|
+
|
|
3
20
|
## 0.36.8-beta
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/api/run.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { type HarnessChannel } from "@nylorun/core/harness-api";
|
|
|
10
10
|
import type { HarnessExecutors, HarnessRun } from "./executors.js";
|
|
11
11
|
import { type EngineConfig } from "./host.js";
|
|
12
12
|
import type { TranscriptCache } from "./transcript-cache.js";
|
|
13
|
+
import type { LoopConfig } from "../loop/config.js";
|
|
13
14
|
export interface RunContext {
|
|
14
15
|
readonly channel: HarnessChannel;
|
|
15
16
|
readonly executors: HarnessExecutors;
|
|
@@ -18,3 +19,25 @@ export interface RunContext {
|
|
|
18
19
|
readonly engine: EngineConfig;
|
|
19
20
|
}
|
|
20
21
|
export declare function runTurn(ctx: RunContext, run: HarnessRun): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* The loop settings for this run: the Host's, with the compaction settings the Agent's version
|
|
24
|
+
* sets in `harness.loop.compaction` over them, and those of `roles[<role>]` over the Agent's for a
|
|
25
|
+
* session opened with that role (ADR-157). A setting that is not a positive integer is left out,
|
|
26
|
+
* and said once with an `engine.diagnostic`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function agentLoopOf(ctx: {
|
|
29
|
+
readonly engine: {
|
|
30
|
+
readonly loop: LoopConfig;
|
|
31
|
+
};
|
|
32
|
+
readonly channel: {
|
|
33
|
+
request(kind: string, body: unknown): Promise<unknown>;
|
|
34
|
+
};
|
|
35
|
+
}, run: {
|
|
36
|
+
readonly runId: string;
|
|
37
|
+
readonly grant: {
|
|
38
|
+
readonly turnId: string;
|
|
39
|
+
};
|
|
40
|
+
}, sessionId: string, start: {
|
|
41
|
+
readonly loop?: unknown;
|
|
42
|
+
readonly role?: unknown;
|
|
43
|
+
}): Promise<LoopConfig>;
|
package/dist/api/run.js
CHANGED
|
@@ -129,7 +129,7 @@ export async function runTurn(ctx, run) {
|
|
|
129
129
|
takeInputs: true,
|
|
130
130
|
...(start.model ? { model: start.model } : {}),
|
|
131
131
|
files: filesOf(ctx, run, start, start.manifest.engine),
|
|
132
|
-
loop: ctx
|
|
132
|
+
loop: await agentLoopOf(ctx, run, sessionId, start),
|
|
133
133
|
});
|
|
134
134
|
// Our engine's outputs folder (Q5): saved before the turn ends, while the run is held, for
|
|
135
135
|
// a root agent that declares nothing or declares `engine.outputs` (ADR-100).
|
|
@@ -272,3 +272,58 @@ function compactionOf(start, after, result) {
|
|
|
272
272
|
summaryEffectIds: [...ids],
|
|
273
273
|
};
|
|
274
274
|
}
|
|
275
|
+
/** The compaction settings an Agent's version may set in `harness.loop.compaction` (ADR-157). */
|
|
276
|
+
const AGENT_COMPACTION_KEYS = ["threshold", "keepTokens"];
|
|
277
|
+
/**
|
|
278
|
+
* The loop settings for this run: the Host's, with the compaction settings the Agent's version
|
|
279
|
+
* sets in `harness.loop.compaction` over them, and those of `roles[<role>]` over the Agent's for a
|
|
280
|
+
* session opened with that role (ADR-157). A setting that is not a positive integer is left out,
|
|
281
|
+
* and said once with an `engine.diagnostic`.
|
|
282
|
+
*/
|
|
283
|
+
export async function agentLoopOf(ctx, run, sessionId, start) {
|
|
284
|
+
const base = ctx.engine.loop;
|
|
285
|
+
const settings = start.loop?.compaction;
|
|
286
|
+
if (settings === undefined)
|
|
287
|
+
return base;
|
|
288
|
+
const ignored = [];
|
|
289
|
+
const pick = (from, where) => {
|
|
290
|
+
if (from === undefined)
|
|
291
|
+
return {};
|
|
292
|
+
if (!from || typeof from !== "object" || Array.isArray(from)) {
|
|
293
|
+
ignored.push(where);
|
|
294
|
+
return {};
|
|
295
|
+
}
|
|
296
|
+
const out = {};
|
|
297
|
+
for (const key of AGENT_COMPACTION_KEYS) {
|
|
298
|
+
const value = from[key];
|
|
299
|
+
if (value === undefined)
|
|
300
|
+
continue;
|
|
301
|
+
if (typeof value === "number" && Number.isInteger(value) && value > 0)
|
|
302
|
+
out[key] = value;
|
|
303
|
+
else
|
|
304
|
+
ignored.push(`${where}.${key}`);
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
};
|
|
308
|
+
const role = typeof start.role === "string" ? start.role : undefined;
|
|
309
|
+
const roles = settings?.roles;
|
|
310
|
+
const own = pick(settings, "harness.loop.compaction");
|
|
311
|
+
const forRole = role !== undefined && roles && typeof roles === "object" && !Array.isArray(roles)
|
|
312
|
+
? pick(roles[role], `harness.loop.compaction.roles.${role}`)
|
|
313
|
+
: {};
|
|
314
|
+
if (ignored.length > 0)
|
|
315
|
+
await ctx.channel
|
|
316
|
+
.request("event", {
|
|
317
|
+
runId: run.runId,
|
|
318
|
+
sessionId,
|
|
319
|
+
turnId: run.grant.turnId,
|
|
320
|
+
type: "engine.diagnostic",
|
|
321
|
+
payload: {
|
|
322
|
+
code: "compaction_setting_ignored",
|
|
323
|
+
message: `Left out, not a positive integer: ${ignored.join(", ")}. The Host's compaction settings apply there.`,
|
|
324
|
+
},
|
|
325
|
+
})
|
|
326
|
+
.catch(() => undefined);
|
|
327
|
+
const compaction = { ...base.compaction, ...own, ...forRole };
|
|
328
|
+
return Object.freeze({ ...base, compaction: Object.freeze(compaction) });
|
|
329
|
+
}
|
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
* harness sets any part of it (`createHarness({ compaction })`); each left out is its default.
|
|
5
5
|
*/
|
|
6
6
|
export interface CompactionConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Compact before a model call whose estimate passes this many tokens, when that is lower than the
|
|
9
|
+
* model's window minus the reply reserve (ADR-157). Absent: only the window's line.
|
|
10
|
+
*/
|
|
11
|
+
readonly threshold?: number;
|
|
7
12
|
/** The kept tail is at most this many tokens... */
|
|
8
13
|
readonly keepTokens: number;
|
|
9
14
|
/** ...and at most this share of the context window. */
|
|
@@ -21,6 +21,11 @@ export interface ContextBudget {
|
|
|
21
21
|
* output limit, held to `maxReserveTokens` and `maxReserveShare` of the window.
|
|
22
22
|
*/
|
|
23
23
|
export declare function contextBudget(transcript: readonly TranscriptEntry[], model?: Pick<ModelDescriptor, "contextWindow" | "maxOutputTokens">, config?: CompactionConfig): ContextBudget | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The estimate above which a model call compacts first: the model's window minus the reply reserve,
|
|
26
|
+
* or the configured `threshold` when it is lower (ADR-157). Undefined with neither.
|
|
27
|
+
*/
|
|
28
|
+
export declare function compactionLimit(budget: ContextBudget | undefined, config?: CompactionConfig): number | undefined;
|
|
24
29
|
/**
|
|
25
30
|
* Tokens the next prompt needs: the last reported usage, which already counts the
|
|
26
31
|
* instructions, tools and everything before it, plus an estimate of what came after.
|
|
@@ -30,6 +30,16 @@ export function contextBudget(transcript, model, config = DEFAULT_COMPACTION_CON
|
|
|
30
30
|
reserve: reserveOf(model.maxOutputTokens, model.contextWindow),
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The estimate above which a model call compacts first: the model's window minus the reply reserve,
|
|
35
|
+
* or the configured `threshold` when it is lower (ADR-157). Undefined with neither.
|
|
36
|
+
*/
|
|
37
|
+
export function compactionLimit(budget, config = DEFAULT_COMPACTION_CONFIG) {
|
|
38
|
+
const line = budget ? budget.contextWindow - budget.reserve : undefined;
|
|
39
|
+
if (config.threshold === undefined)
|
|
40
|
+
return line;
|
|
41
|
+
return line === undefined ? config.threshold : Math.min(line, config.threshold);
|
|
42
|
+
}
|
|
33
43
|
/**
|
|
34
44
|
* Tokens the next prompt needs: the last reported usage, which already counts the
|
|
35
45
|
* instructions, tools and everything before it, plus an estimate of what came after.
|
package/dist/loop/run.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HostSuspension } from "./host-suspension.js";
|
|
2
2
|
import { HarnessError, isHarnessError } from "@nylorun/core/define";
|
|
3
3
|
import { runStep } from "./step/run.js";
|
|
4
|
-
import { compact, contextBudget, estimateTokens } from "./compaction/index.js";
|
|
4
|
+
import { compact, compactionLimit, contextBudget, estimateTokens } from "./compaction/index.js";
|
|
5
5
|
import { normalizeInput } from "./state.js";
|
|
6
6
|
import { createId } from "../utils/ids.js";
|
|
7
7
|
import { dispatchPlan } from "./dispatch.js";
|
|
@@ -142,11 +142,12 @@ export async function execute(agent, options, internals = {}) {
|
|
|
142
142
|
}
|
|
143
143
|
stepsRun++;
|
|
144
144
|
const stepId = createId("step");
|
|
145
|
-
// Keep the next prompt inside the model's window,
|
|
145
|
+
// Keep the next prompt inside the model's window, and under the Agent's threshold when it
|
|
146
|
+
// sets a lower one (ADR-157), before it is sent.
|
|
146
147
|
const budget = contextBudget(invocation.state.transcript, options.model, compaction);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
const limit = compactionLimit(budget, compaction);
|
|
149
|
+
if (limit !== undefined &&
|
|
150
|
+
estimateTokens(invocation.state.transcript, turnId, options.files, compaction) > limit)
|
|
150
151
|
await compactHistory("threshold", stepId);
|
|
151
152
|
const snapshot = {
|
|
152
153
|
id: invocation.state.executionId,
|
|
@@ -5,6 +5,12 @@ import type { ContextSnapshot, ModelCandidate } from "@nylorun/core/define";
|
|
|
5
5
|
import type { Tripwire } from "@nylorun/core/define";
|
|
6
6
|
import { type CanonicalCall } from "./canonicalize.js";
|
|
7
7
|
import { ModelConfigurationDraft } from "./model-configuration.js";
|
|
8
|
+
/**
|
|
9
|
+
* A message's context (ADR-157, Host feature `message-context`): the items of the turn's latest
|
|
10
|
+
* input that carries `metadata.context`, for every model call of that turn. A later turn's calls
|
|
11
|
+
* never see them: the block is rendered after the history, not kept in it.
|
|
12
|
+
*/
|
|
13
|
+
export declare function messageContextOf(input: Pick<StepInput, "turnId" | "transcript" | "arrivals">): ContextSnapshot | undefined;
|
|
8
14
|
/** Mutable Step state: the configuration the definition's parts set, and the model's answer. */
|
|
9
15
|
export declare class StepContext {
|
|
10
16
|
#private;
|
|
@@ -5,6 +5,43 @@ const EMPTY_CONTEXT = Object.freeze({
|
|
|
5
5
|
items: Object.freeze([]),
|
|
6
6
|
contributors: Object.freeze([]),
|
|
7
7
|
});
|
|
8
|
+
/**
|
|
9
|
+
* A message's context (ADR-157, Host feature `message-context`): the items of the turn's latest
|
|
10
|
+
* input that carries `metadata.context`, for every model call of that turn. A later turn's calls
|
|
11
|
+
* never see them: the block is rendered after the history, not kept in it.
|
|
12
|
+
*/
|
|
13
|
+
export function messageContextOf(input) {
|
|
14
|
+
const events = [];
|
|
15
|
+
for (const entry of input.transcript)
|
|
16
|
+
if (entry.kind === "input" && entry.turnId === input.turnId)
|
|
17
|
+
events.push(entry.event);
|
|
18
|
+
events.push(...input.arrivals);
|
|
19
|
+
for (let index = events.length - 1; index >= 0; index--) {
|
|
20
|
+
const items = contextItemsOf(events[index]);
|
|
21
|
+
if (items)
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
items,
|
|
24
|
+
contributors: Object.freeze([
|
|
25
|
+
Object.freeze({ partId: "message-context", slot: "message-context", order: 0 }),
|
|
26
|
+
]),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
function contextItemsOf(event) {
|
|
32
|
+
if (event.kind !== "user-message" && event.kind !== "interrupt")
|
|
33
|
+
return undefined;
|
|
34
|
+
const context = event.metadata?.context;
|
|
35
|
+
if (!Array.isArray(context) || context.length === 0)
|
|
36
|
+
return undefined;
|
|
37
|
+
const items = context.flatMap((item) => {
|
|
38
|
+
if (!item || typeof item !== "object" || !("value" in item))
|
|
39
|
+
return [];
|
|
40
|
+
const { type, value } = item;
|
|
41
|
+
return [Object.freeze(typeof type === "string" ? { type, value } : { value })];
|
|
42
|
+
});
|
|
43
|
+
return items.length ? Object.freeze(items) : undefined;
|
|
44
|
+
}
|
|
8
45
|
/** Mutable Step state: the configuration the definition's parts set, and the model's answer. */
|
|
9
46
|
export class StepContext {
|
|
10
47
|
input;
|
|
@@ -27,7 +64,7 @@ export class StepContext {
|
|
|
27
64
|
return Object.freeze(this.configurationSnapshot().instructions.map((item) => item.text));
|
|
28
65
|
}
|
|
29
66
|
contextSnapshot() {
|
|
30
|
-
return EMPTY_CONTEXT;
|
|
67
|
+
return messageContextOf(this.input) ?? EMPTY_CONTEXT;
|
|
31
68
|
}
|
|
32
69
|
get offeredTools() {
|
|
33
70
|
return this.configurationSnapshot().tools;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.0-beta",
|
|
4
4
|
"description": "Nylorun's TypeScript agent execution engine. See github.com/nylorun/agents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"zod": "^4.6.7"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@nylorun/core": "0.
|
|
80
|
+
"@nylorun/core": "0.49.0-beta"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"zod": "^4.6.7"
|