@nylorun/harness 0.16.0-beta → 0.18.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 +30 -0
- package/HOST_CONTRACT.md +9 -5
- package/README.md +26 -6
- package/dist/compatibility.d.ts +1 -1
- package/dist/compatibility.js +1 -1
- package/dist/definition/agent-definition.d.ts +3 -0
- package/dist/definition/agent-definition.js +2 -0
- package/dist/loop/delegation.d.ts +32 -0
- package/dist/loop/delegation.js +136 -0
- package/dist/loop/dispatch.js +56 -0
- package/dist/loop/invocation.d.ts +7 -1
- package/dist/loop/invocation.js +2 -0
- package/dist/loop/run.d.ts +2 -1
- package/dist/loop/run.js +148 -8
- package/dist/loop/state.js +23 -0
- package/dist/loop/step/hooks.d.ts +56 -0
- package/dist/loop/step/hooks.js +191 -0
- package/dist/loop/step/model-configuration.d.ts +5 -0
- package/dist/loop/step/model-configuration.js +14 -1
- package/dist/loop/step/run.d.ts +9 -1
- package/dist/loop/step/run.js +47 -29
- package/dist/loop/step/runtime.d.ts +14 -2
- package/dist/run/durable.d.ts +13 -1
- package/dist/run/durable.js +124 -84
- package/dist/types/client.d.ts +1 -1
- package/dist/types/execution.d.ts +23 -2
- package/dist/utils/ids.d.ts +2 -0
- package/dist/utils/ids.js +5 -0
- package/package.json +2 -2
- package/dist/loop/step/dynamics.d.ts +0 -29
- package/dist/loop/step/dynamics.js +0 -149
package/dist/loop/step/run.js
CHANGED
|
@@ -10,11 +10,13 @@ import { resolveModelRequest } from "./resolve.js";
|
|
|
10
10
|
import { sealStep } from "./seal.js";
|
|
11
11
|
import { ModelConfigurationDraft } from "./model-configuration.js";
|
|
12
12
|
import { createId } from "../../utils/ids.js";
|
|
13
|
-
import {
|
|
13
|
+
import { applyPatch, asDecisions, callHooks, combinePatches, layerPatches, mergeDecisions, } from "./hooks.js";
|
|
14
14
|
export async function runStep(input) {
|
|
15
15
|
let requestedModelId;
|
|
16
16
|
let modelInvocationId;
|
|
17
17
|
let retryFeedback;
|
|
18
|
+
let retried = false;
|
|
19
|
+
let turnPatch;
|
|
18
20
|
const stepInput = Object.freeze({
|
|
19
21
|
executionId: input.executionId,
|
|
20
22
|
turnId: input.turnId,
|
|
@@ -42,35 +44,38 @@ export async function runStep(input) {
|
|
|
42
44
|
if (context.currentTripwire)
|
|
43
45
|
return context.tripwire(context.currentTripwire);
|
|
44
46
|
const definition = input.agent.definition;
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
step: input.stepNumber - 1,
|
|
50
|
-
arrivals: input.arrivals,
|
|
51
|
-
messages: projectModelCall(resolveModelRequest({
|
|
47
|
+
const turn = input.turn;
|
|
48
|
+
if (definition && turn) {
|
|
49
|
+
try {
|
|
50
|
+
const messages = projectModelCall(resolveModelRequest({
|
|
52
51
|
context,
|
|
53
52
|
arrivals: input.arrivals,
|
|
54
53
|
toolResults: input.toolResults,
|
|
55
54
|
output: input.output,
|
|
56
|
-
})).prompt
|
|
57
|
-
sessionState
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
55
|
+
})).prompt;
|
|
56
|
+
const sessionState = input.agent.sessionState ?? {};
|
|
57
|
+
const base = { input: turn.input, info: input.info, messages };
|
|
58
|
+
let patch = turn.patch;
|
|
59
|
+
if (turn.start) {
|
|
60
|
+
const combined = combinePatches(definition, await callHooks(definition, { at: "before", scope: "turn" }, { ...base, state: { ...sessionState } }, "before-turn"));
|
|
61
|
+
if (combined.state)
|
|
62
|
+
Object.assign(sessionState, combined.state);
|
|
63
|
+
if (combined.block !== undefined)
|
|
64
|
+
return context.tripwire({
|
|
65
|
+
code: "dynamics.blocked",
|
|
66
|
+
message: combined.block,
|
|
67
|
+
scope: "execution",
|
|
68
|
+
});
|
|
69
|
+
patch = turnPatch = combined.patch;
|
|
65
70
|
}
|
|
71
|
+
const step = combinePatches(definition, await callHooks(definition, { at: "before", scope: "step" }, { ...base, state: { ...sessionState }, step: input.stepNumber - 1 }, `before-step:${input.stepId}`));
|
|
72
|
+
if (step.state)
|
|
73
|
+
Object.assign(sessionState, step.state);
|
|
66
74
|
if (input.agent.recordAfterDynamics)
|
|
67
75
|
await input.agent.recordAfterDynamics();
|
|
68
|
-
if (
|
|
69
|
-
return context.tripwire({
|
|
70
|
-
|
|
71
|
-
message: before.blocked,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
76
|
+
if (step.block !== undefined)
|
|
77
|
+
return context.tripwire({ code: "dynamics.blocked", message: step.block });
|
|
78
|
+
applyPatch(configuration, layerPatches(patch, step.patch), 10_000);
|
|
74
79
|
}
|
|
75
80
|
catch (error) {
|
|
76
81
|
if (error instanceof HostSuspension)
|
|
@@ -148,7 +153,7 @@ export async function runStep(input) {
|
|
|
148
153
|
const candidate = context.currentCandidate;
|
|
149
154
|
if (!candidate)
|
|
150
155
|
throw new HarnessError("model.candidate-missing", "Model candidate missing after mint");
|
|
151
|
-
if (
|
|
156
|
+
if (definition && turn) {
|
|
152
157
|
const text = candidate.output
|
|
153
158
|
?.filter((block) => block.type === "text")
|
|
154
159
|
.map((block) => block.text)
|
|
@@ -159,18 +164,28 @@ export async function runStep(input) {
|
|
|
159
164
|
const call = block;
|
|
160
165
|
return { id: call.id, name: call.name, args: call.args };
|
|
161
166
|
});
|
|
162
|
-
const decision = await
|
|
167
|
+
const decision = mergeDecisions(asDecisions(await callHooks(definition, { at: "after", scope: "step" }, {
|
|
163
168
|
...(text ? { text } : {}),
|
|
164
169
|
toolCalls,
|
|
165
|
-
|
|
170
|
+
info: input.info,
|
|
171
|
+
state: { ...(input.agent.sessionState ?? {}) },
|
|
172
|
+
step: input.stepNumber - 1,
|
|
173
|
+
attempt: turn.afterStepAttempts,
|
|
174
|
+
}, `after-step:${input.stepId}`)));
|
|
166
175
|
if (input.agent.recordAfterDynamics)
|
|
167
176
|
await input.agent.recordAfterDynamics();
|
|
168
177
|
if (decision.block) {
|
|
169
178
|
return context.tripwire({ code: "dynamics.blocked", message: decision.block });
|
|
170
179
|
}
|
|
171
180
|
if (decision.retry) {
|
|
172
|
-
|
|
173
|
-
|
|
181
|
+
retried = true;
|
|
182
|
+
// Denied calls reach the model as tool results carrying the feedback.
|
|
183
|
+
if (toolCalls.length)
|
|
184
|
+
for (const call of toolCalls)
|
|
185
|
+
minted.deny(call.id, decision.retry);
|
|
186
|
+
else
|
|
187
|
+
retryFeedback = decision.retry;
|
|
188
|
+
return minted;
|
|
174
189
|
}
|
|
175
190
|
if (decision.text !== undefined) {
|
|
176
191
|
minted.replace({
|
|
@@ -198,7 +213,8 @@ export async function runStep(input) {
|
|
|
198
213
|
});
|
|
199
214
|
}
|
|
200
215
|
}, input.observe);
|
|
201
|
-
|
|
216
|
+
// A text answer sent back for a retry is not the turn's output, so skip output sealing.
|
|
217
|
+
const output = sealStep(context, retryFeedback === undefined ? input.output : undefined);
|
|
202
218
|
const candidate = output.kind === "tools" ? output.plan.candidate : context.currentCandidate;
|
|
203
219
|
return Object.freeze({
|
|
204
220
|
stepId: input.stepId,
|
|
@@ -207,6 +223,8 @@ export async function runStep(input) {
|
|
|
207
223
|
output,
|
|
208
224
|
...(modelInvocationId ? { modelInvocationId } : {}),
|
|
209
225
|
...(retryFeedback ? { retry: retryFeedback } : {}),
|
|
226
|
+
...(retried ? { retried } : {}),
|
|
227
|
+
...(turnPatch === undefined ? {} : { turnPatch }),
|
|
210
228
|
});
|
|
211
229
|
}
|
|
212
230
|
function snapshotStepStart(input) {
|
|
@@ -5,18 +5,30 @@ import type { InputEvent, TranscriptEntry } from "@nylorun/core/define";
|
|
|
5
5
|
import type { ToolResult } from "@nylorun/core/define";
|
|
6
6
|
import type { BoundMiddleware } from "@nylorun/core/define";
|
|
7
7
|
import type { JsonValue } from "@nylorun/core/define";
|
|
8
|
+
import type { CallPatch } from "./hooks.js";
|
|
8
9
|
/** Compiled definition pieces plus the per-run model adapter for one step. */
|
|
9
10
|
export interface StepRuntime {
|
|
10
11
|
readonly middleware: readonly BoundMiddleware[];
|
|
11
12
|
readonly invoke: ModelAdapter;
|
|
12
13
|
readonly registry: ToolRegistry;
|
|
13
|
-
/** Full definition for before/after
|
|
14
|
+
/** Full definition for before/after hooks (optional for middleware-only agents). */
|
|
14
15
|
readonly definition?: AgentDefinition;
|
|
15
16
|
/** Mutable session memory bag shared with tools. */
|
|
16
17
|
sessionState?: Record<string, JsonValue>;
|
|
17
|
-
/** Called after
|
|
18
|
+
/** Called after hook results are applied so the host can record. */
|
|
18
19
|
recordAfterDynamics?: () => Promise<void>;
|
|
19
20
|
}
|
|
21
|
+
/** Turn-scoped hook state carried into each step. */
|
|
22
|
+
export interface StepTurnHooks {
|
|
23
|
+
/** True for the first model call of a new turn: run before("turn"). */
|
|
24
|
+
readonly start: boolean;
|
|
25
|
+
/** Input that started the turn; hooks see it on every step, including retries. */
|
|
26
|
+
readonly input?: string;
|
|
27
|
+
/** Validated before("turn") patch applied to every step. */
|
|
28
|
+
readonly patch?: CallPatch;
|
|
29
|
+
/** Retries already requested by after("step") hooks this turn. */
|
|
30
|
+
readonly afterStepAttempts: number;
|
|
31
|
+
}
|
|
20
32
|
export interface StepInput {
|
|
21
33
|
readonly executionId: string;
|
|
22
34
|
readonly turnId: string;
|
package/dist/run/durable.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { AgentManifest } from "@nylorun/core/define";
|
|
|
2
2
|
import type { ExecutionInput, ExecutionState, RunResult } from "../types/execution.js";
|
|
3
3
|
import type { JsonObject } from "@nylorun/core/define";
|
|
4
4
|
import type { ActionOutcome } from "@nylorun/core/contracts";
|
|
5
|
+
import type { AgentRef, HookAt, HookScope } from "@nylorun/core/define";
|
|
5
6
|
import { ENGINE_VERSION } from "../compatibility.js";
|
|
6
7
|
/** Private persistence contract for hosts; this is not a session wire type. */
|
|
7
8
|
export interface DurableCheckpoint {
|
|
@@ -22,9 +23,18 @@ export interface HostEffect {
|
|
|
22
23
|
readonly turnId: string;
|
|
23
24
|
readonly agentId: string;
|
|
24
25
|
readonly manifestHash: string;
|
|
25
|
-
|
|
26
|
+
/** `delegation` journals when an agent used as a tool starts and settles; hosts record it and resolve it at once. */
|
|
27
|
+
readonly kind: "model" | "tool" | "hook" | "delegation";
|
|
28
|
+
/** Set on work for an agent used as a tool; `agentId` stays the session's root agent. */
|
|
29
|
+
readonly agent?: AgentRef;
|
|
26
30
|
readonly capabilityId?: string;
|
|
27
31
|
readonly toolName?: string;
|
|
32
|
+
/** For `hook` effects: the hook point and every capability that registered it, in manifest order. */
|
|
33
|
+
readonly hook?: {
|
|
34
|
+
readonly at: HookAt;
|
|
35
|
+
readonly scope: HookScope;
|
|
36
|
+
readonly capabilityIds: readonly string[];
|
|
37
|
+
};
|
|
28
38
|
readonly input: unknown;
|
|
29
39
|
readonly context: Record<string, unknown>;
|
|
30
40
|
}
|
|
@@ -52,6 +62,8 @@ export type DurableResult = {
|
|
|
52
62
|
};
|
|
53
63
|
/** Discovered tool advertised for this execution. It is not part of the hashed manifest. */
|
|
54
64
|
export interface DurableSessionTool {
|
|
65
|
+
/** The agent used as a tool that owns this tool; absent for the root agent. */
|
|
66
|
+
readonly agentId?: string;
|
|
55
67
|
readonly capabilityId: string;
|
|
56
68
|
readonly name: string;
|
|
57
69
|
readonly description?: string;
|
package/dist/run/durable.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
+
import { HOOK_POINTS, hasHook } from "@nylorun/core/define";
|
|
2
3
|
import { AgentManifestSchema } from "@nylorun/core/contracts";
|
|
3
4
|
import { agentFrom } from "@nylorun/core/define";
|
|
4
5
|
import { definitionFor } from "../definition/agent-definition.js";
|
|
@@ -39,11 +40,11 @@ export async function runDurable(options) {
|
|
|
39
40
|
checkpoint.state.agentId !== manifest.id ||
|
|
40
41
|
checkpoint.state.executionId !== checkpoint.sessionId))
|
|
41
42
|
throw new HarnessError("execution.incompatible", "Checkpoint definition/session mismatch");
|
|
42
|
-
let serial = 0;
|
|
43
43
|
const pending = new Map();
|
|
44
44
|
const inFlight = new Set();
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
// Effect ids are derived from stable names, never call order, so replays line up.
|
|
46
|
+
const effect = async (kind, input, context, identity, target = {}) => {
|
|
47
|
+
const effectId = `${checkpoint.turnId}:${checkpoint.segment}:${kind}:${identity}`;
|
|
47
48
|
const request = JSON.parse(JSON.stringify({
|
|
48
49
|
effectId,
|
|
49
50
|
sessionId: checkpoint.sessionId,
|
|
@@ -53,8 +54,7 @@ export async function runDurable(options) {
|
|
|
53
54
|
kind,
|
|
54
55
|
input,
|
|
55
56
|
context,
|
|
56
|
-
...
|
|
57
|
-
...(toolName ? { toolName } : {}),
|
|
57
|
+
...target,
|
|
58
58
|
}));
|
|
59
59
|
const operation = host.resolveEffect(request);
|
|
60
60
|
inFlight.add(operation);
|
|
@@ -71,95 +71,121 @@ export async function runDurable(options) {
|
|
|
71
71
|
}
|
|
72
72
|
return result.outcome;
|
|
73
73
|
};
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
release
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
ctx.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
const implementations = {};
|
|
120
|
-
const sessionTools = options.sessionTools ?? [];
|
|
121
|
-
for (const capability of manifest.capabilities) {
|
|
122
|
-
const tools = {};
|
|
123
|
-
for (const tool of capability.tools ?? [])
|
|
124
|
-
tools[tool.name] = hostedTool(capability.id, tool);
|
|
125
|
-
for (const tool of sessionTools)
|
|
126
|
-
if (tool.capabilityId === capability.id)
|
|
127
|
-
tools[tool.name] = hostedTool(capability.id, tool);
|
|
128
|
-
implementations[capability.id] = {
|
|
129
|
-
tools,
|
|
130
|
-
...(capability.beforeModelCall
|
|
131
|
-
? {
|
|
132
|
-
beforeModelCall: async (args) => (await effect("beforeModelCall", args, { info: checkpoint.info }, capability.id))
|
|
133
|
-
.value,
|
|
74
|
+
/** Tools of one agent. Each agent keeps its own patch order; children namespace their effects. */
|
|
75
|
+
const hostedTools = (ref) => {
|
|
76
|
+
let patchTail = Promise.resolve();
|
|
77
|
+
return (capabilityId, tool) => ({
|
|
78
|
+
name: tool.name,
|
|
79
|
+
...(tool.description === undefined ? {} : { description: tool.description }),
|
|
80
|
+
inputSchema: schemaFromJSON(tool.inputSchema),
|
|
81
|
+
...(tool.outputSchema ? { outputSchema: schemaFromJSON(tool.outputSchema) } : {}),
|
|
82
|
+
async execute(args, ctx) {
|
|
83
|
+
const previous = patchTail;
|
|
84
|
+
let release;
|
|
85
|
+
patchTail = new Promise((resolve) => {
|
|
86
|
+
release = resolve;
|
|
87
|
+
});
|
|
88
|
+
try {
|
|
89
|
+
const outcome = await effect("tool", args, {
|
|
90
|
+
executionId: ctx.executionId,
|
|
91
|
+
turnId: ctx.turnId,
|
|
92
|
+
stepId: ctx.stepId,
|
|
93
|
+
callId: ctx.callId,
|
|
94
|
+
invocationId: ctx.invocationId,
|
|
95
|
+
idempotencyKey: ref
|
|
96
|
+
? `${ref.delegationId}/${ctx.idempotencyKey}`
|
|
97
|
+
: ctx.idempotencyKey,
|
|
98
|
+
info: ctx.info,
|
|
99
|
+
state: ctx.state.entries(),
|
|
100
|
+
resume: ctx.resume,
|
|
101
|
+
}, scoped(ref, ctx.invocationId), { capabilityId, toolName: tool.name, ...(ref ? { agent: ref } : {}) });
|
|
102
|
+
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
103
|
+
await previous;
|
|
104
|
+
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
105
|
+
ctx.state.set(key, value);
|
|
106
|
+
const value = outcome.value;
|
|
107
|
+
return value?.kind === "interaction-required"
|
|
108
|
+
? {
|
|
109
|
+
...value,
|
|
110
|
+
interaction: {
|
|
111
|
+
...value.interaction,
|
|
112
|
+
id: `${checkpoint.turnId}:${checkpoint.segment}:interaction:${ctx.invocationId}`,
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
: value;
|
|
134
116
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
afterModelCall: async (args, ctx) => (await effect("afterModelCall", args, ctx, capability.id)).value,
|
|
117
|
+
finally {
|
|
118
|
+
await previous;
|
|
119
|
+
release();
|
|
139
120
|
}
|
|
140
|
-
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
const sessionTools = options.sessionTools ?? [];
|
|
125
|
+
/** Rebuild one agent from its manifest with every tool and hook routed through host effects. */
|
|
126
|
+
const hostedDefinition = (agent, ref) => {
|
|
127
|
+
const hostedTool = hostedTools(ref);
|
|
128
|
+
const owned = sessionTools.filter((tool) => (tool.agentId ?? manifest.id) === agent.id);
|
|
129
|
+
const implementations = {};
|
|
130
|
+
for (const capability of agent.capabilities) {
|
|
131
|
+
const tools = {};
|
|
132
|
+
// Agents used as tools are rebuilt from their manifest body; the engine runs them.
|
|
133
|
+
for (const tool of capability.tools ?? [])
|
|
134
|
+
if (!tool.agent)
|
|
135
|
+
tools[tool.name] = hostedTool(capability.id, tool);
|
|
136
|
+
for (const tool of owned)
|
|
137
|
+
if (tool.capabilityId === capability.id)
|
|
138
|
+
tools[tool.name] = hostedTool(capability.id, tool);
|
|
139
|
+
implementations[capability.id] = {
|
|
140
|
+
tools,
|
|
141
|
+
// Hooks run through runHooks below, one effect per hook point; these only satisfy binding.
|
|
142
|
+
...hookPlaceholders(capability.hooks),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const definition = definitionFor(agentFrom(agent, implementations, owned.length === 0
|
|
146
|
+
? undefined
|
|
147
|
+
: {
|
|
148
|
+
sessionTools: owned.map((tool) => ({
|
|
149
|
+
capabilityId: tool.capabilityId,
|
|
150
|
+
name: tool.name,
|
|
151
|
+
})),
|
|
152
|
+
}));
|
|
153
|
+
// All capabilities registered at a hook point share one effect, so one executor round trip.
|
|
154
|
+
const runHooks = async ({ point, capabilityIds, args, identity }) => {
|
|
155
|
+
const outcome = await effect("hook", args, { info: checkpoint.info }, scoped(ref, identity), {
|
|
156
|
+
hook: { at: point.at, scope: point.scope, capabilityIds },
|
|
157
|
+
...(ref ? { agent: ref } : {}),
|
|
158
|
+
});
|
|
159
|
+
const results = outcome.value?.results;
|
|
160
|
+
return results !== null && typeof results === "object" && !Array.isArray(results)
|
|
161
|
+
? results
|
|
162
|
+
: {};
|
|
141
163
|
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
164
|
+
return Object.freeze({ ...definition, runHooks });
|
|
165
|
+
};
|
|
166
|
+
const modelCall = (ref) => async (call, ctx) => (await effect("model", call, { request: ctx.request, invocationId: ctx.invocationId }, scoped(ref, ctx.invocationId), ref ? { agent: ref } : {})).value;
|
|
167
|
+
const delegation = {
|
|
168
|
+
child: (delegate, ref) => ({
|
|
169
|
+
definition: hostedDefinition(delegate.manifest, ref),
|
|
170
|
+
onModelCall: modelCall(ref),
|
|
171
|
+
}),
|
|
172
|
+
async announce(phase, ref, payload) {
|
|
173
|
+
await effect("delegation", payload, {}, `${ref.delegationId}:${phase}`, { agent: ref });
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
const hosted = hostedDefinition(manifest);
|
|
151
177
|
// Manifest reconstruction must retain definition identity, including schemas and instructions.
|
|
152
|
-
if (
|
|
178
|
+
if (hosted.hash !== checkpoint.manifestHash)
|
|
153
179
|
throw new HarnessError("execution.incompatible", "Reconstructed definition hash mismatch");
|
|
154
180
|
try {
|
|
155
|
-
const result = await withDeterministicIds(`${checkpoint.turnId}_${checkpoint.segment}`, () => execute(
|
|
181
|
+
const result = await withDeterministicIds(`${checkpoint.turnId}_${checkpoint.segment}`, () => execute(hosted, {
|
|
156
182
|
state: checkpoint.state ??
|
|
157
|
-
initializeExecutionState(
|
|
183
|
+
initializeExecutionState(hosted, { executionId: checkpoint.sessionId }),
|
|
158
184
|
input: checkpoint.input,
|
|
159
185
|
info: checkpoint.info,
|
|
160
186
|
signal: options.signal,
|
|
161
|
-
onModelCall:
|
|
162
|
-
}));
|
|
187
|
+
onModelCall: modelCall(),
|
|
188
|
+
}, { delegation }));
|
|
163
189
|
return { status: result.status, checkpoint: { ...checkpoint, state: result.state }, result };
|
|
164
190
|
}
|
|
165
191
|
catch (error) {
|
|
@@ -174,3 +200,17 @@ export async function runDurable(options) {
|
|
|
174
200
|
};
|
|
175
201
|
}
|
|
176
202
|
}
|
|
203
|
+
/** Effect identities of an agent used as a tool live under its delegation. */
|
|
204
|
+
function scoped(ref, identity) {
|
|
205
|
+
return ref ? `${ref.delegationId}/${identity}` : identity;
|
|
206
|
+
}
|
|
207
|
+
function hookPlaceholders(hooks) {
|
|
208
|
+
const placeholder = () => {
|
|
209
|
+
throw new HarnessError("execution.incompatible", "Hosted hooks run through the host effect");
|
|
210
|
+
};
|
|
211
|
+
const table = {};
|
|
212
|
+
for (const point of HOOK_POINTS)
|
|
213
|
+
if (hasHook(hooks, point.at, point.scope))
|
|
214
|
+
(table[point.at] ??= {})[point.scope] = placeholder;
|
|
215
|
+
return table;
|
|
216
|
+
}
|
package/dist/types/client.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { MessageInput } from "@nylorun/core/define";
|
|
|
3
3
|
import type { SavedToolCall } from "./execution.js";
|
|
4
4
|
/**
|
|
5
5
|
* Definition-parameterized client types (H11). Types only — harness does not implement Session.
|
|
6
|
-
* Runtime imports these for
|
|
6
|
+
* Runtime imports these for session and stream typing.
|
|
7
7
|
*/
|
|
8
8
|
export interface InputAccepted {
|
|
9
9
|
readonly status: "accepted" | "rejected";
|
|
@@ -2,7 +2,7 @@ import type { ModelAdapter } from "@nylorun/core/define";
|
|
|
2
2
|
import type { InputEvent, MessageInput, TranscriptEntry } from "@nylorun/core/define";
|
|
3
3
|
import type { ObserveEvent } from "@nylorun/core/define";
|
|
4
4
|
import type { JsonObject, JsonValue, Tripwire } from "@nylorun/core/define";
|
|
5
|
-
import type { RequiredInteraction, ToolExecutionResume, ToolOutcome, ToolResult } from "@nylorun/core/define";
|
|
5
|
+
import type { AgentRef, RequiredInteraction, ToolExecutionResume, ToolOutcome, ToolResult } from "@nylorun/core/define";
|
|
6
6
|
export type ExecutionInput = MessageInput | InputEvent | {
|
|
7
7
|
readonly kind: "continue";
|
|
8
8
|
} | {
|
|
@@ -58,6 +58,23 @@ export interface ExecutionPlan {
|
|
|
58
58
|
readonly immediate: readonly ToolResult[];
|
|
59
59
|
readonly calls: readonly SavedToolCall[];
|
|
60
60
|
}
|
|
61
|
+
/** Hook progress for the turn in flight. Cleared when the turn ends. */
|
|
62
|
+
export interface TurnHookState {
|
|
63
|
+
readonly turnId: string;
|
|
64
|
+
/** Text of the input that started the turn. */
|
|
65
|
+
readonly input?: string;
|
|
66
|
+
/** Validated before("turn") patch, applied to every step of the turn. */
|
|
67
|
+
readonly patch?: {
|
|
68
|
+
readonly capabilities?: Readonly<Record<string, boolean>>;
|
|
69
|
+
readonly tools?: Readonly<Record<string, boolean>>;
|
|
70
|
+
readonly instructions?: readonly string[];
|
|
71
|
+
};
|
|
72
|
+
/** Retries requested so far this turn, per hook point. */
|
|
73
|
+
readonly attempts: {
|
|
74
|
+
readonly afterStep: number;
|
|
75
|
+
readonly afterTurn: number;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
61
78
|
/** Versioned continuation data, not a live execution object. */
|
|
62
79
|
export interface ExecutionState {
|
|
63
80
|
readonly version: 1;
|
|
@@ -73,8 +90,10 @@ export interface ExecutionState {
|
|
|
73
90
|
readonly plan?: ExecutionPlan;
|
|
74
91
|
/** Abandoned actions retained for reconciliation, never automatically dispatched. */
|
|
75
92
|
readonly cancelledCalls?: readonly SavedToolCall[];
|
|
93
|
+
/** Hook progress for the current turn; present only while a turn with hooks is in flight. */
|
|
94
|
+
readonly turn?: TurnHookState;
|
|
76
95
|
/**
|
|
77
|
-
* Durable session memory (tools write via `ctx.state`;
|
|
96
|
+
* Durable session memory (tools write via `ctx.state`; hooks read it).
|
|
78
97
|
* Runtime persists this with the checkpoint.
|
|
79
98
|
*/
|
|
80
99
|
readonly state?: JsonObject;
|
|
@@ -86,6 +105,8 @@ export type ExecutionEvent = (ObserveEvent | {
|
|
|
86
105
|
readonly executionId: string;
|
|
87
106
|
readonly sequence: number;
|
|
88
107
|
readonly runId: string;
|
|
108
|
+
/** Set on events forwarded from an agent used as a tool. */
|
|
109
|
+
readonly agent?: AgentRef;
|
|
89
110
|
};
|
|
90
111
|
export interface RunOptions<Info = unknown> {
|
|
91
112
|
readonly state?: ExecutionState;
|
package/dist/utils/ids.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export declare function withDeterministicIds<T>(seed: string, fn: () => T): T;
|
|
2
2
|
export declare function createId(prefix: string): string;
|
|
3
|
+
/** Run `fn` with its own id counters, seeded under the current scope, so concurrent work stays deterministic. */
|
|
4
|
+
export declare function withNestedIds<T>(suffix: string, fn: () => T): T;
|
package/dist/utils/ids.js
CHANGED
|
@@ -11,3 +11,8 @@ export function createId(prefix) {
|
|
|
11
11
|
value.counts.set(prefix, count);
|
|
12
12
|
return `${prefix}_${value.seed}_${count}`;
|
|
13
13
|
}
|
|
14
|
+
/** Run `fn` with its own id counters, seeded under the current scope, so concurrent work stays deterministic. */
|
|
15
|
+
export function withNestedIds(suffix, fn) {
|
|
16
|
+
const value = scope.getStore();
|
|
17
|
+
return value ? scope.run({ seed: `${value.seed}_${suffix}`, counts: new Map() }, fn) : fn();
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0-beta",
|
|
4
4
|
"description": "Nylorun's TypeScript agent execution engine. See github.com/nylorun/harness.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"zod": "^4.6.5"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@nylorun/core": "0.
|
|
79
|
+
"@nylorun/core": "0.4.0-beta"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
82
|
"zod": "^4.6.5"
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { AgentDefinition } from "../../definition/agent-definition.js";
|
|
2
|
-
import type { Decision } from "@nylorun/core/define";
|
|
3
|
-
import type { JsonValue } from "@nylorun/core/define";
|
|
4
|
-
import type { PromptItem } from "@nylorun/core/define";
|
|
5
|
-
import type { InputEvent } from "@nylorun/core/define";
|
|
6
|
-
import type { ModelConfigurationDraft } from "./model-configuration.js";
|
|
7
|
-
export interface DynamicsContext {
|
|
8
|
-
readonly agent: AgentDefinition;
|
|
9
|
-
readonly info?: unknown;
|
|
10
|
-
readonly step: number;
|
|
11
|
-
readonly arrivals: readonly InputEvent[];
|
|
12
|
-
readonly messages: readonly PromptItem[];
|
|
13
|
-
sessionState: Record<string, JsonValue>;
|
|
14
|
-
}
|
|
15
|
-
/** Run all beforeModelCall handlers in capability `.use()` order. */
|
|
16
|
-
export declare function applyBeforeModelCall(ctx: DynamicsContext, configuration: ModelConfigurationDraft): Promise<{
|
|
17
|
-
readonly blocked?: string;
|
|
18
|
-
}>;
|
|
19
|
-
/** Run all afterModelCall handlers; merge with strictest-wins rules. */
|
|
20
|
-
export declare function applyAfterModelCall(ctx: DynamicsContext, args: {
|
|
21
|
-
readonly text?: string;
|
|
22
|
-
readonly toolCalls: readonly {
|
|
23
|
-
readonly id: string;
|
|
24
|
-
readonly name: string;
|
|
25
|
-
readonly args: JsonValue;
|
|
26
|
-
}[];
|
|
27
|
-
}): Promise<Decision>;
|
|
28
|
-
export declare function mergeDecisions(decisions: readonly Decision[]): Decision;
|
|
29
|
-
export declare function hasDynamics(agent: AgentDefinition): boolean;
|