@nylorun/harness 0.26.0-beta → 0.27.1-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 +56 -0
- package/HOST_CONTRACT.md +13 -11
- package/README.md +11 -12
- package/dist/api/harness.d.ts +1 -1
- package/dist/api/harness.js +2 -2
- package/dist/definition/agent-definition.d.ts +2 -2
- package/dist/definition/agent-definition.js +2 -2
- package/dist/definition/registry.d.ts +3 -3
- package/dist/definition/registry.js +12 -15
- package/dist/loop/compaction/index.d.ts +4 -2
- package/dist/loop/compaction/index.js +78 -8
- package/dist/loop/dispatch.js +1 -1
- package/dist/loop/model/project.d.ts +6 -0
- package/dist/loop/model/project.js +59 -18
- package/dist/loop/run.js +2 -2
- package/dist/loop/state.js +0 -1
- package/dist/loop/step/canonicalize.d.ts +1 -3
- package/dist/loop/step/canonicalize.js +0 -15
- package/dist/loop/step/model-configuration.d.ts +4 -13
- package/dist/loop/step/model-configuration.js +12 -70
- package/dist/loop/step/resolve.js +0 -1
- package/dist/loop/step/run.js +33 -12
- package/dist/loop/step/runtime.d.ts +2 -2
- package/dist/loop/step/seal.js +0 -17
- package/dist/loop/step/slot-assembly.d.ts +5 -5
- package/dist/loop/step/slot-assembly.js +5 -5
- package/dist/loop/step/step-context.d.ts +5 -19
- package/dist/loop/step/step-context.js +9 -195
- package/dist/loop/transcript.js +7 -1
- package/dist/run/durable.d.ts +1 -3
- package/dist/run/durable.js +25 -27
- package/dist/types/execution.d.ts +0 -1
- package/package.json +2 -2
- package/dist/loop/step/compose.d.ts +0 -5
- package/dist/loop/step/compose.js +0 -109
- package/dist/loop/step/context-draft.d.ts +0 -8
- package/dist/loop/step/context-draft.js +0 -48
|
@@ -1,34 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { freezeGraph } from "@nylorun/core/define";
|
|
3
|
-
import { callsFromCanonical, candidateFromCanonical, canonicalizeOutput, identityKey, } from "./canonicalize.js";
|
|
4
|
-
import { normalizeCandidate } from "../model/normalize.js";
|
|
5
|
-
import { ContextDraft } from "./context-draft.js";
|
|
1
|
+
import { candidateFromCanonical, canonicalizeOutput } from "./canonicalize.js";
|
|
6
2
|
import { ModelConfigurationDraft } from "./model-configuration.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
/** The model-visible context of a call. Nothing adds items since step middleware was removed. */
|
|
4
|
+
const EMPTY_CONTEXT = Object.freeze({
|
|
5
|
+
items: Object.freeze([]),
|
|
6
|
+
contributors: Object.freeze([]),
|
|
7
|
+
});
|
|
8
|
+
/** Mutable Step state: the configuration the definition's parts set, and the model's answer. */
|
|
12
9
|
export class StepContext {
|
|
13
10
|
input;
|
|
14
|
-
#observe;
|
|
15
|
-
#context;
|
|
16
11
|
#configuration;
|
|
17
|
-
#denials = new Map();
|
|
18
|
-
#interactions = new Map();
|
|
19
|
-
#identities = new Map();
|
|
20
12
|
#canonical = Object.freeze([]);
|
|
21
13
|
#candidate;
|
|
22
14
|
#tripwire;
|
|
23
|
-
#response;
|
|
24
|
-
#sealed = false;
|
|
25
15
|
#configurationSnapshot;
|
|
26
|
-
|
|
27
|
-
constructor(input, observe, configuration, context) {
|
|
16
|
+
constructor(input, configuration) {
|
|
28
17
|
this.input = input;
|
|
29
|
-
this.#observe = observe;
|
|
30
18
|
this.#configuration = configuration;
|
|
31
|
-
this.#context = context;
|
|
32
19
|
}
|
|
33
20
|
get currentTripwire() {
|
|
34
21
|
return this.#tripwire;
|
|
@@ -36,14 +23,11 @@ export class StepContext {
|
|
|
36
23
|
get currentCandidate() {
|
|
37
24
|
return this.#candidate;
|
|
38
25
|
}
|
|
39
|
-
get selectedDirective() {
|
|
40
|
-
return this.configurationSnapshot().model;
|
|
41
|
-
}
|
|
42
26
|
get instructions() {
|
|
43
27
|
return Object.freeze(this.configurationSnapshot().instructions.map((item) => item.text));
|
|
44
28
|
}
|
|
45
29
|
contextSnapshot() {
|
|
46
|
-
return
|
|
30
|
+
return EMPTY_CONTEXT;
|
|
47
31
|
}
|
|
48
32
|
get offeredTools() {
|
|
49
33
|
return this.configurationSnapshot().tools;
|
|
@@ -57,15 +41,6 @@ export class StepContext {
|
|
|
57
41
|
sealConfiguration(snapshot) {
|
|
58
42
|
this.#configurationSnapshot = snapshot;
|
|
59
43
|
}
|
|
60
|
-
sealContext(snapshot) {
|
|
61
|
-
this.#contextSnapshot = snapshot;
|
|
62
|
-
}
|
|
63
|
-
denialFor(callId) {
|
|
64
|
-
return this.#denials.get(callId);
|
|
65
|
-
}
|
|
66
|
-
interactionFor(callId) {
|
|
67
|
-
return this.#interactions.get(callId);
|
|
68
|
-
}
|
|
69
44
|
canonicalCalls() {
|
|
70
45
|
return this.#canonical;
|
|
71
46
|
}
|
|
@@ -73,169 +48,8 @@ export class StepContext {
|
|
|
73
48
|
const { output, calls } = canonicalizeOutput(candidate.output);
|
|
74
49
|
this.#canonical = calls;
|
|
75
50
|
this.#candidate = candidateFromCanonical(candidate, output);
|
|
76
|
-
this.#identities.clear();
|
|
77
|
-
for (const call of calls)
|
|
78
|
-
this.#identities.set(call.id, identityKey(call.name, call.args));
|
|
79
|
-
return this.#ensureResponse();
|
|
80
51
|
}
|
|
81
52
|
tripwire(error) {
|
|
82
53
|
this.#tripwire ??= Object.freeze({ ...error });
|
|
83
|
-
return this.#ensureResponse();
|
|
84
54
|
}
|
|
85
|
-
seal() {
|
|
86
|
-
this.#sealed = true;
|
|
87
|
-
}
|
|
88
|
-
requestFacade(middlewareId, middlewareOrder) {
|
|
89
|
-
let mutatorsActive = true;
|
|
90
|
-
const mutate = (action) => {
|
|
91
|
-
if (!mutatorsActive || this.#sealed) {
|
|
92
|
-
this.#observe({
|
|
93
|
-
type: "middleware.lease-violation",
|
|
94
|
-
turnId: this.input.turnId,
|
|
95
|
-
stepId: this.input.stepId,
|
|
96
|
-
middlewareId,
|
|
97
|
-
reason: this.#sealed ? "step-sealed" : "mutators-revoked",
|
|
98
|
-
});
|
|
99
|
-
throw new HarnessError("middleware.request-mutators-revoked", "Middleware request mutators are no longer active", { details: { middlewareId } });
|
|
100
|
-
}
|
|
101
|
-
action();
|
|
102
|
-
};
|
|
103
|
-
const mutateConfiguration = (kind, action) => mutate(() => {
|
|
104
|
-
try {
|
|
105
|
-
action();
|
|
106
|
-
}
|
|
107
|
-
catch (cause) {
|
|
108
|
-
const message = cause instanceof Error ? cause.message : String(cause);
|
|
109
|
-
const code = isHarnessError(cause)
|
|
110
|
-
? cause.code
|
|
111
|
-
: kind === "tools"
|
|
112
|
-
? "tool.invalid-schema"
|
|
113
|
-
: kind === "model"
|
|
114
|
-
? "model.invalid-directive"
|
|
115
|
-
: "configuration.invalid-instructions";
|
|
116
|
-
this.#tripwire ??= Object.freeze({ code, message });
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
const mutateContext = (action) => mutate(() => {
|
|
120
|
-
try {
|
|
121
|
-
action();
|
|
122
|
-
}
|
|
123
|
-
catch (cause) {
|
|
124
|
-
const message = cause instanceof Error ? cause.message : String(cause);
|
|
125
|
-
this.#tripwire ??= Object.freeze({
|
|
126
|
-
code: isHarnessError(cause) ? cause.code : "context.invalid-item",
|
|
127
|
-
message,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
const request = {
|
|
132
|
-
executionId: this.input.executionId,
|
|
133
|
-
turnId: this.input.turnId,
|
|
134
|
-
stepId: this.input.stepId,
|
|
135
|
-
info: this.input.info,
|
|
136
|
-
turnNumber: this.input.turnNumber,
|
|
137
|
-
stepNumber: this.input.stepNumber,
|
|
138
|
-
arrivals: this.input.arrivals,
|
|
139
|
-
toolResults: this.input.toolResults,
|
|
140
|
-
transcript: this.input.transcript,
|
|
141
|
-
context: Object.freeze({
|
|
142
|
-
set: (slot, items, options) => mutateContext(() => this.#context.set(middlewareId, middlewareOrder, slot, items, options)),
|
|
143
|
-
}),
|
|
144
|
-
configuration: Object.freeze({
|
|
145
|
-
instructions: Object.freeze({
|
|
146
|
-
set: (slot, items, options) => mutateConfiguration("instructions", () => this.#configuration.setInstructions(middlewareId, middlewareOrder, slot, items, options)),
|
|
147
|
-
}),
|
|
148
|
-
tools: Object.freeze({
|
|
149
|
-
set: (slot, tools, options) => mutateConfiguration("tools", () => this.#configuration.setTools(middlewareId, middlewareOrder, slot, tools, options)),
|
|
150
|
-
}),
|
|
151
|
-
model: Object.freeze({
|
|
152
|
-
select: (directive, options) => mutateConfiguration("model", () => this.#configuration.select(middlewareId, middlewareOrder, directive, options)),
|
|
153
|
-
replace: (directive, options) => mutateConfiguration("model", () => this.#configuration.replace(middlewareId, middlewareOrder, directive, options)),
|
|
154
|
-
clear: (options) => mutateConfiguration("model", () => this.#configuration.clear(options)),
|
|
155
|
-
}),
|
|
156
|
-
}),
|
|
157
|
-
tripwire: (error) => {
|
|
158
|
-
let minted;
|
|
159
|
-
mutate(() => {
|
|
160
|
-
minted = this.tripwire(error);
|
|
161
|
-
});
|
|
162
|
-
return minted;
|
|
163
|
-
},
|
|
164
|
-
};
|
|
165
|
-
const value = Object.freeze(request);
|
|
166
|
-
return Object.freeze({
|
|
167
|
-
value,
|
|
168
|
-
revokeMutators: () => {
|
|
169
|
-
mutatorsActive = false;
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
#ensureResponse() {
|
|
174
|
-
if (this.#response)
|
|
175
|
-
return this.#response;
|
|
176
|
-
const value = Object.freeze({
|
|
177
|
-
candidate: () => this.currentCandidate,
|
|
178
|
-
toolCalls: () => callsFromCanonical(this.#canonical),
|
|
179
|
-
replace: (candidate) => this.#replace(candidate),
|
|
180
|
-
deny: (callId, reason) => {
|
|
181
|
-
if (!this.#denials.has(callId))
|
|
182
|
-
this.#denials.set(callId, reason);
|
|
183
|
-
},
|
|
184
|
-
requireInteraction: (callId, interaction) => {
|
|
185
|
-
if (!this.#interactions.has(callId))
|
|
186
|
-
this.#interactions.set(callId, freezeGraph({ ...interaction }));
|
|
187
|
-
},
|
|
188
|
-
tripwire: (error) => {
|
|
189
|
-
this.#tripwire ??= Object.freeze({ ...error });
|
|
190
|
-
return value;
|
|
191
|
-
},
|
|
192
|
-
});
|
|
193
|
-
branded.add(value);
|
|
194
|
-
this.#response = value;
|
|
195
|
-
return value;
|
|
196
|
-
}
|
|
197
|
-
#replace(candidate) {
|
|
198
|
-
let normalized;
|
|
199
|
-
try {
|
|
200
|
-
normalized = normalizeCandidate(candidate);
|
|
201
|
-
}
|
|
202
|
-
catch (error) {
|
|
203
|
-
this.#tripwire ??= Object.freeze({
|
|
204
|
-
code: isHarnessError(error) ? error.code : "response.invalid-replacement",
|
|
205
|
-
message: error instanceof Error ? error.message : String(error),
|
|
206
|
-
});
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
const { output, calls: next } = canonicalizeOutput(normalized.output);
|
|
210
|
-
for (const call of next) {
|
|
211
|
-
const original = this.#identities.get(call.id);
|
|
212
|
-
if (original === undefined) {
|
|
213
|
-
this.#tripwire ??= Object.freeze({
|
|
214
|
-
code: "response.invalid-replacement",
|
|
215
|
-
message: `replace() cannot add Tool call '${call.id}'`,
|
|
216
|
-
});
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (original !== identityKey(call.name, call.args)) {
|
|
220
|
-
this.#tripwire ??= Object.freeze({
|
|
221
|
-
code: "response.invalid-replacement",
|
|
222
|
-
message: `replace() cannot change identity of Tool call '${call.id}'`,
|
|
223
|
-
});
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
const current = this.#candidate;
|
|
228
|
-
this.#canonical = next;
|
|
229
|
-
this.#candidate = Object.freeze({
|
|
230
|
-
output,
|
|
231
|
-
...pickSidecar("finishReason", candidate, normalized, current),
|
|
232
|
-
...pickSidecar("usage", candidate, normalized, current),
|
|
233
|
-
...pickSidecar("evidence", candidate, normalized, current),
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function pickSidecar(key, replacement, normalized, current) {
|
|
238
|
-
const source = Object.hasOwn(replacement, key) ? normalized : current;
|
|
239
|
-
const value = source?.[key];
|
|
240
|
-
return value === undefined ? {} : { [key]: value };
|
|
241
55
|
}
|
package/dist/loop/transcript.js
CHANGED
|
@@ -61,7 +61,7 @@ function entryAt(value, index) {
|
|
|
61
61
|
return result;
|
|
62
62
|
}
|
|
63
63
|
case "compaction": {
|
|
64
|
-
exactKeys(entry, ["kind", "turnId", "stepId", "summary", "trigger", "tokensBefore", "tokensAfter"], `Transcript entry ${index}`);
|
|
64
|
+
exactKeys(entry, ["kind", "turnId", "stepId", "summary", "trigger", "tokensBefore", "tokensAfter", "files"], `Transcript entry ${index}`);
|
|
65
65
|
requiredString(entry.stepId, `Transcript entry ${index} stepId`);
|
|
66
66
|
if (typeof entry.summary !== "string")
|
|
67
67
|
fail(`Transcript entry ${index} summary`);
|
|
@@ -70,6 +70,9 @@ function entryAt(value, index) {
|
|
|
70
70
|
for (const key of ["tokensBefore", "tokensAfter"])
|
|
71
71
|
if (!Number.isInteger(entry[key]) || entry[key] < 0)
|
|
72
72
|
fail(`Transcript entry ${index} ${key}`);
|
|
73
|
+
if (entry.files !== undefined &&
|
|
74
|
+
(!Array.isArray(entry.files) || entry.files.some((line) => typeof line !== "string")))
|
|
75
|
+
fail(`Transcript entry ${index} files`);
|
|
73
76
|
return Object.freeze({
|
|
74
77
|
kind: "compaction",
|
|
75
78
|
turnId: entry.turnId,
|
|
@@ -78,6 +81,9 @@ function entryAt(value, index) {
|
|
|
78
81
|
trigger: entry.trigger,
|
|
79
82
|
tokensBefore: entry.tokensBefore,
|
|
80
83
|
tokensAfter: entry.tokensAfter,
|
|
84
|
+
...(entry.files === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: { files: Object.freeze([...entry.files]) }),
|
|
81
87
|
});
|
|
82
88
|
}
|
|
83
89
|
case "final":
|
package/dist/run/durable.d.ts
CHANGED
|
@@ -30,7 +30,6 @@ export interface HostEffect {
|
|
|
30
30
|
readonly kind: "model" | "tool" | "delegation" | "agent";
|
|
31
31
|
/** Set on work for an agent used as a tool; `agentId` stays the session's root agent. */
|
|
32
32
|
readonly agent?: AgentRef;
|
|
33
|
-
readonly capabilityId?: string;
|
|
34
33
|
readonly toolName?: string;
|
|
35
34
|
/** Workflow node path (flow effects). */
|
|
36
35
|
readonly path?: string;
|
|
@@ -67,7 +66,6 @@ export type DurableResult = {
|
|
|
67
66
|
export interface DurableSessionTool {
|
|
68
67
|
/** The agent used as a tool that owns this tool; absent for the root agent. */
|
|
69
68
|
readonly agentId?: string;
|
|
70
|
-
readonly capabilityId: string;
|
|
71
69
|
readonly name: string;
|
|
72
70
|
readonly description?: string;
|
|
73
71
|
readonly inputSchema: JsonObject;
|
|
@@ -77,7 +75,7 @@ export interface DurableSessionTool {
|
|
|
77
75
|
* with `tool_call`, which makes the call as this tool's own.
|
|
78
76
|
*/
|
|
79
77
|
readonly deferred?: boolean;
|
|
80
|
-
/** Text the model reads with the
|
|
78
|
+
/** Text the model reads with the Runtime's instructions while this tool is advertised. */
|
|
81
79
|
readonly instructions?: readonly string[];
|
|
82
80
|
}
|
|
83
81
|
export declare function createDurableCheckpoint(input: {
|
package/dist/run/durable.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HarnessError } from "@nylorun/core/define";
|
|
2
2
|
import { effectIdOf } from "@nylorun/core/harness-api";
|
|
3
3
|
import { AgentManifestSchema } from "@nylorun/core/contracts";
|
|
4
|
-
import { TOOL_CALL_TOOL,
|
|
4
|
+
import { TOOL_CALL_TOOL, agentFrom, withHttpTarget } from "@nylorun/core/define";
|
|
5
5
|
import { definitionFor } from "../definition/agent-definition.js";
|
|
6
6
|
import { schemaFromJSON } from "@nylorun/core/define";
|
|
7
7
|
import { hashManifest } from "@nylorun/core/define";
|
|
@@ -74,7 +74,7 @@ export async function runDurable(options) {
|
|
|
74
74
|
/** Tools of one agent. Each agent keeps its own patch order; children namespace their effects. */
|
|
75
75
|
const hostedTools = (ref) => {
|
|
76
76
|
let patchTail = Promise.resolve();
|
|
77
|
-
return (
|
|
77
|
+
return (tool) => ({
|
|
78
78
|
name: tool.name,
|
|
79
79
|
...(tool.description === undefined ? {} : { description: tool.description }),
|
|
80
80
|
inputSchema: schemaFromJSON(tool.inputSchema),
|
|
@@ -99,7 +99,7 @@ export async function runDurable(options) {
|
|
|
99
99
|
info: ctx.info,
|
|
100
100
|
state: ctx.state.entries(),
|
|
101
101
|
resume: ctx.resume,
|
|
102
|
-
}, scoped(ref, ctx.invocationId), {
|
|
102
|
+
}, scoped(ref, ctx.invocationId), { toolName: tool.name, ...(ref ? { agent: ref } : {}) });
|
|
103
103
|
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
104
104
|
await previous;
|
|
105
105
|
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
@@ -130,33 +130,31 @@ export async function runDurable(options) {
|
|
|
130
130
|
// Deferred tools stay out of the model's list; `tool_call` runs them (R2b C10).
|
|
131
131
|
const owned = mine.filter((tool) => !tool.deferred);
|
|
132
132
|
const deferred = new Map(mine.filter((tool) => tool.deferred).map((tool) => [tool.name, tool]));
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
133
|
+
const tools = {};
|
|
134
|
+
// Agents used as tools are rebuilt from their manifest body; the engine runs them. An
|
|
135
|
+
// HTTP tool keeps its target (the manifest's hash covers it) and runs as a host effect.
|
|
136
|
+
for (const tool of agent.tools ?? [])
|
|
137
|
+
if (tool.http)
|
|
138
|
+
// A pinned `approval` stays for the definition's hash; the Tool Gate holds the call.
|
|
139
|
+
tools[tool.name] = withHttpTarget(hostedTool(tool), {
|
|
140
|
+
http: tool.http,
|
|
141
|
+
...(tool.approval === undefined ? {} : { approval: tool.approval }),
|
|
142
|
+
});
|
|
143
|
+
else if (!tool.agent)
|
|
144
|
+
tools[tool.name] = hostedTool(tool);
|
|
145
|
+
// The tools the Runtime added (its `runtime` block) run as host effects too.
|
|
146
|
+
for (const tool of agent.runtime?.tools ?? [])
|
|
147
|
+
tools[tool.name] = hostedTool(tool);
|
|
148
|
+
for (const tool of owned)
|
|
149
|
+
tools[tool.name] =
|
|
150
|
+
tool.name === TOOL_CALL_TOOL
|
|
151
|
+
? deferredToolCall(tool, deferred, hostedTool)
|
|
152
|
+
: hostedTool(tool);
|
|
153
|
+
const implementations = { tools };
|
|
155
154
|
return definitionFor(agentFrom(agent, implementations, owned.length === 0
|
|
156
155
|
? undefined
|
|
157
156
|
: {
|
|
158
157
|
sessionTools: owned.map((tool) => ({
|
|
159
|
-
capabilityId: tool.capabilityId,
|
|
160
158
|
name: tool.name,
|
|
161
159
|
...(tool.instructions?.length ? { instructions: tool.instructions } : {}),
|
|
162
160
|
})),
|
|
@@ -253,7 +251,7 @@ function deferredToolCall(tool, deferred, hostedTool) {
|
|
|
253
251
|
};
|
|
254
252
|
let run = hosted.get(target.name);
|
|
255
253
|
if (!run) {
|
|
256
|
-
run = hostedTool(target
|
|
254
|
+
run = hostedTool(target);
|
|
257
255
|
hosted.set(target.name, run);
|
|
258
256
|
}
|
|
259
257
|
return run.execute(checked.value, ctx);
|
|
@@ -18,7 +18,6 @@ export type ExecutionInput = MessageInput | InputEvent | {
|
|
|
18
18
|
};
|
|
19
19
|
/** Portable reference to deployed code. Applications must preserve this value intact. */
|
|
20
20
|
export interface ToolReference {
|
|
21
|
-
readonly capabilityId: string;
|
|
22
21
|
readonly toolName: string;
|
|
23
22
|
readonly descriptor: {
|
|
24
23
|
readonly name: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.1-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.5"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@nylorun/core": "0.
|
|
80
|
+
"@nylorun/core": "0.22.0-beta"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"zod": "^4.6.5"
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { BoundMiddleware } from "@nylorun/core/define";
|
|
2
|
-
import type { StepResponse } from "@nylorun/core/define";
|
|
3
|
-
import type { ObserveEmit } from "../observe.js";
|
|
4
|
-
import { StepContext } from "./step-context.js";
|
|
5
|
-
export declare function runMiddleware(middleware: readonly BoundMiddleware[], context: StepContext, terminal: () => Promise<StepResponse>, observe: ObserveEmit): Promise<StepResponse>;
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { HostSuspension } from "../host-suspension.js";
|
|
2
|
-
import { HarnessError, isHarnessError } from "@nylorun/core/define";
|
|
3
|
-
import { isBrandedResponse, StepContext } from "./step-context.js";
|
|
4
|
-
export async function runMiddleware(middleware, context, terminal, observe) {
|
|
5
|
-
const dispatch = async (index) => {
|
|
6
|
-
if (context.currentTripwire)
|
|
7
|
-
return context.tripwire(context.currentTripwire);
|
|
8
|
-
if (index === middleware.length)
|
|
9
|
-
return terminal();
|
|
10
|
-
const item = middleware[index];
|
|
11
|
-
const lease = context.requestFacade(item.id, index);
|
|
12
|
-
let returned = false;
|
|
13
|
-
let nextCalledTwice = false;
|
|
14
|
-
let nextPromise;
|
|
15
|
-
observe({
|
|
16
|
-
type: "middleware.entered",
|
|
17
|
-
turnId: context.input.turnId,
|
|
18
|
-
stepId: context.input.stepId,
|
|
19
|
-
middlewareId: item.id,
|
|
20
|
-
});
|
|
21
|
-
try {
|
|
22
|
-
let handlerError;
|
|
23
|
-
let result;
|
|
24
|
-
try {
|
|
25
|
-
result = await item.handle(lease.value, () => {
|
|
26
|
-
if (returned)
|
|
27
|
-
throw new HarnessError("middleware.next-after-return", `Middleware '${item.id}' called next() after returning`, { details: { middlewareId: item.id } });
|
|
28
|
-
if (nextPromise) {
|
|
29
|
-
nextCalledTwice = true;
|
|
30
|
-
throw new HarnessError("middleware.next-called-twice", `Middleware '${item.id}' called next() more than once`, { details: { middlewareId: item.id } });
|
|
31
|
-
}
|
|
32
|
-
lease.revokeMutators();
|
|
33
|
-
nextPromise = dispatch(index + 1);
|
|
34
|
-
return nextPromise;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
handlerError = error;
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
returned = true;
|
|
42
|
-
lease.revokeMutators();
|
|
43
|
-
}
|
|
44
|
-
let inner;
|
|
45
|
-
if (nextPromise) {
|
|
46
|
-
try {
|
|
47
|
-
inner = await nextPromise;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
handlerError ??= error;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (handlerError instanceof HostSuspension)
|
|
54
|
-
throw handlerError;
|
|
55
|
-
if (handlerError) {
|
|
56
|
-
if (nextCalledTwice ||
|
|
57
|
-
(isHarnessError(handlerError) && handlerError.code === "middleware.next-called-twice")) {
|
|
58
|
-
return context.tripwire({
|
|
59
|
-
code: "middleware.next-called-twice",
|
|
60
|
-
message: message(handlerError),
|
|
61
|
-
scope: "execution",
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
return context.tripwire({
|
|
65
|
-
code: isHarnessError(handlerError) ? handlerError.code : "middleware.failed",
|
|
66
|
-
message: message(handlerError),
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (inner) {
|
|
70
|
-
if (result !== inner) {
|
|
71
|
-
return context.tripwire({
|
|
72
|
-
code: "middleware.invalid-response",
|
|
73
|
-
message: `Middleware '${item.id}' must return the StepResponse from next()`,
|
|
74
|
-
// Calling next() completed the step successfully; forgetting to
|
|
75
|
-
// return that response is isolated to this step. A non-response
|
|
76
|
-
// replacement remains a execution-level middleware-contract breach.
|
|
77
|
-
scope: result === undefined ? "step" : "execution",
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return inner;
|
|
81
|
-
}
|
|
82
|
-
if (!isBrandedResponse(result)) {
|
|
83
|
-
return context.tripwire({
|
|
84
|
-
code: "middleware.invalid-response",
|
|
85
|
-
message: `Middleware '${item.id}' must return a branded StepResponse`,
|
|
86
|
-
scope: "execution",
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
observe({
|
|
93
|
-
type: "middleware.completed",
|
|
94
|
-
turnId: context.input.turnId,
|
|
95
|
-
stepId: context.input.stepId,
|
|
96
|
-
middlewareId: item.id,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
try {
|
|
101
|
-
return await dispatch(0);
|
|
102
|
-
}
|
|
103
|
-
finally {
|
|
104
|
-
context.seal();
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function message(error) {
|
|
108
|
-
return error instanceof Error ? error.message : String(error);
|
|
109
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ContextMutationOptions, ContextSnapshot } from "@nylorun/core/define";
|
|
2
|
-
import type { ContextItem } from "@nylorun/core/define";
|
|
3
|
-
/** Per-step model-visible context supplied explicitly by middleware. */
|
|
4
|
-
export declare class ContextDraft {
|
|
5
|
-
#private;
|
|
6
|
-
set(middlewareId: string, middlewareOrder: number, slot: string, items: readonly ContextItem[], options?: ContextMutationOptions): void;
|
|
7
|
-
snapshot(): ContextSnapshot;
|
|
8
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
-
import { copyJson } from "@nylorun/core/define";
|
|
3
|
-
import { SlotDraft } from "./slot-assembly.js";
|
|
4
|
-
const CONTEXT_TYPE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
5
|
-
/** Per-step model-visible context supplied explicitly by middleware. */
|
|
6
|
-
export class ContextDraft {
|
|
7
|
-
#slots = new SlotDraft();
|
|
8
|
-
set(middlewareId, middlewareOrder, slot, items, options) {
|
|
9
|
-
if (!Array.isArray(items))
|
|
10
|
-
throw new HarnessError("context.invalid-item", "Runtime context items must be an array");
|
|
11
|
-
this.#slots.set({
|
|
12
|
-
middlewareId,
|
|
13
|
-
middlewareOrder,
|
|
14
|
-
slot,
|
|
15
|
-
value: Object.freeze(items.map(normalizeItem)),
|
|
16
|
-
order: options?.order,
|
|
17
|
-
reason: options?.reason,
|
|
18
|
-
invalidSlot: "context.invalid-slot",
|
|
19
|
-
invalidOrder: "context.invalid-order",
|
|
20
|
-
invalidReason: "context.invalid-reason",
|
|
21
|
-
label: "Runtime context",
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
snapshot() {
|
|
25
|
-
const slots = this.#slots.values();
|
|
26
|
-
const items = Object.freeze(slots.flatMap((slot) => slot.value));
|
|
27
|
-
const contributors = Object.freeze(slots.map((slot) => contributor(slot.owner, slot.reason)));
|
|
28
|
-
return Object.freeze({ items, contributors });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function normalizeItem(item) {
|
|
32
|
-
if (!item || typeof item !== "object")
|
|
33
|
-
throw new HarnessError("context.invalid-item", "Runtime context items must be objects");
|
|
34
|
-
if (item.type !== undefined && (typeof item.type !== "string" || !CONTEXT_TYPE.test(item.type)))
|
|
35
|
-
throw new HarnessError("context.invalid-item-type", "Runtime context item type must match [A-Za-z][A-Za-z0-9_-]{0,63}");
|
|
36
|
-
return Object.freeze({
|
|
37
|
-
...(item.type === undefined ? {} : { type: item.type }),
|
|
38
|
-
value: copyJson(item.value),
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function contributor(owner, reason) {
|
|
42
|
-
return Object.freeze({
|
|
43
|
-
middlewareId: owner.middlewareId,
|
|
44
|
-
slot: owner.slot,
|
|
45
|
-
order: owner.order,
|
|
46
|
-
...(reason === undefined ? {} : { reason }),
|
|
47
|
-
});
|
|
48
|
-
}
|