@leitwerk-dev/test-support 0.2.0 → 0.3.0
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/dist/{chunk-KPSK7EMY.js → chunk-5BZSJ4OO.js} +22 -157
- package/dist/chunk-5BZSJ4OO.js.map +1 -0
- package/dist/{chunk-2IICIB6D.js → chunk-E4Y7XND5.js} +40 -4
- package/dist/chunk-E4Y7XND5.js.map +1 -0
- package/dist/{chunk-JUH3LUYK.js → chunk-EXHTWQAE.js} +5 -1
- package/dist/chunk-EXHTWQAE.js.map +1 -0
- package/dist/chunk-KJVNI2UP.js +17 -0
- package/dist/chunk-KJVNI2UP.js.map +1 -0
- package/dist/{chunk-7I4OCTF3.js → chunk-OE4NHDVS.js} +15 -4
- package/dist/chunk-OE4NHDVS.js.map +1 -0
- package/dist/chunk-P2FF4AP7.js +72 -0
- package/dist/chunk-P2FF4AP7.js.map +1 -0
- package/dist/chunk-RY6Y6V5E.js +54 -0
- package/dist/chunk-RY6Y6V5E.js.map +1 -0
- package/dist/compact-process-detail-fixture.d.ts +3 -0
- package/dist/compact-process-detail-fixture.d.ts.map +1 -1
- package/dist/extension-integration-harness.d.ts +357 -0
- package/dist/extension-integration-harness.d.ts.map +1 -0
- package/dist/fakes/fake-git-ops.d.ts +12 -0
- package/dist/fakes/fake-git-ops.d.ts.map +1 -1
- package/dist/fakes/fake-llm.d.ts +11 -0
- package/dist/fakes/fake-llm.d.ts.map +1 -1
- package/dist/fakes/index.d.ts +1 -1
- package/dist/fakes/index.d.ts.map +1 -1
- package/dist/fakes/index.js +105 -4
- package/dist/fakes/index.js.map +1 -1
- package/dist/fixtures.d.ts +2 -0
- package/dist/fixtures.d.ts.map +1 -1
- package/dist/fixtures.js +157 -2
- package/dist/fixtures.js.map +1 -1
- package/dist/http-launch.d.ts +2 -1
- package/dist/http-launch.d.ts.map +1 -1
- package/dist/index.d.ts +6 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -119
- package/dist/index.js.map +1 -1
- package/dist/integration-harness.d.ts +36 -1
- package/dist/integration-harness.d.ts.map +1 -1
- package/dist/integration.d.ts +3 -3
- package/dist/integration.d.ts.map +1 -1
- package/dist/integration.js +693 -13
- package/dist/integration.js.map +1 -1
- package/dist/local-git.d.ts +105 -8
- package/dist/local-git.d.ts.map +1 -1
- package/dist/local-git.js +155 -24
- package/dist/local-git.js.map +1 -1
- package/dist/model-provider-fixtures.d.ts +9 -1
- package/dist/model-provider-fixtures.d.ts.map +1 -1
- package/dist/observations.d.ts +7 -0
- package/dist/observations.d.ts.map +1 -0
- package/dist/polling.d.ts +1 -0
- package/dist/polling.d.ts.map +1 -1
- package/dist/process-driver.d.ts +5 -1
- package/dist/process-driver.d.ts.map +1 -1
- package/dist/process-fixtures.d.ts +56 -0
- package/dist/process-fixtures.d.ts.map +1 -0
- package/dist/process.d.ts +440 -0
- package/dist/process.d.ts.map +1 -0
- package/dist/process.js +484 -0
- package/dist/process.js.map +1 -0
- package/dist/question-fixtures.d.ts +2 -0
- package/dist/question-fixtures.d.ts.map +1 -1
- package/dist/server-extension-test-harness.d.ts +43 -3
- package/dist/server-extension-test-harness.d.ts.map +1 -1
- package/dist/stub-worker-entry.js +1 -1
- package/dist/supported-question-fixtures.d.ts +96 -0
- package/dist/supported-question-fixtures.d.ts.map +1 -0
- package/dist/test-capability.d.ts +9 -0
- package/dist/test-capability.d.ts.map +1 -0
- package/dist/test-diagnostics.d.ts +14 -0
- package/dist/test-diagnostics.d.ts.map +1 -0
- package/dist/worker-testing/in-process-worker-spawn.d.ts +19 -1
- package/dist/worker-testing/in-process-worker-spawn.d.ts.map +1 -1
- package/dist/worker-testing/index.d.ts +6 -7
- package/dist/worker-testing/index.d.ts.map +1 -1
- package/dist/worker-testing/index.js +32 -22
- package/dist/worker-testing/index.js.map +1 -1
- package/dist/worker-testing/schema-driven-stub-pi.d.ts +5 -0
- package/dist/worker-testing/schema-driven-stub-pi.d.ts.map +1 -1
- package/dist/worker-testing/stub-pi-controls.d.ts +4 -0
- package/dist/worker-testing/stub-pi-controls.d.ts.map +1 -1
- package/dist/worker-testing/stub-pi-tree-handle.d.ts +78 -2
- package/dist/worker-testing/stub-pi-tree-handle.d.ts.map +1 -1
- package/dist/worker-testing/worker-runtime-harness.d.ts +37 -2
- package/dist/worker-testing/worker-runtime-harness.d.ts.map +1 -1
- package/dist/worker-testing/worker-start-payload.d.ts +17 -1
- package/dist/worker-testing/worker-start-payload.d.ts.map +1 -1
- package/package.json +14 -8
- package/src/compact-process-detail-fixture.ts +9 -1
- package/src/extension-integration-harness.ts +1059 -0
- package/src/fakes/fake-git-ops.ts +12 -0
- package/src/fakes/fake-llm.ts +11 -0
- package/src/fakes/index.ts +1 -1
- package/src/fixtures.ts +16 -0
- package/src/http-launch.ts +2 -1
- package/src/index.ts +13 -42
- package/src/integration-harness.ts +100 -9
- package/src/integration.ts +23 -9
- package/src/local-git.ts +196 -45
- package/src/model-provider-fixtures.ts +15 -2
- package/src/observations.ts +19 -0
- package/src/polling.ts +1 -0
- package/src/process-driver.ts +7 -1
- package/src/process-fixtures.ts +123 -0
- package/src/process.ts +768 -0
- package/src/question-fixtures.ts +2 -0
- package/src/server-extension-test-harness.ts +64 -7
- package/src/supported-question-fixtures.ts +168 -0
- package/src/test-capability.ts +9 -0
- package/src/test-diagnostics.ts +69 -0
- package/src/worker-testing/in-process-worker-spawn.ts +44 -4
- package/src/worker-testing/index.ts +4 -30
- package/src/worker-testing/schema-driven-stub-pi.ts +8 -1
- package/src/worker-testing/stub-pi-controls.ts +4 -0
- package/src/worker-testing/stub-pi-tree-handle.ts +87 -3
- package/src/worker-testing/worker-runtime-harness.ts +57 -6
- package/src/worker-testing/worker-start-payload.ts +17 -1
- package/dist/chunk-2IICIB6D.js.map +0 -1
- package/dist/chunk-3MQO3NCX.js +0 -15
- package/dist/chunk-3MQO3NCX.js.map +0 -1
- package/dist/chunk-7I4OCTF3.js.map +0 -1
- package/dist/chunk-7ZK5QRGP.js +0 -98
- package/dist/chunk-7ZK5QRGP.js.map +0 -1
- package/dist/chunk-JUH3LUYK.js.map +0 -1
- package/dist/chunk-KPSK7EMY.js.map +0 -1
- package/dist/chunk-MV4W6NOO.js +0 -105
- package/dist/chunk-MV4W6NOO.js.map +0 -1
- package/dist/test-app.d.ts +0 -23
- package/dist/test-app.d.ts.map +0 -1
- package/src/test-app.ts +0 -82
package/dist/integration.js
CHANGED
|
@@ -1,14 +1,691 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
|
|
2
|
+
postImmediateLaunch
|
|
3
|
+
} from "./chunk-P2FF4AP7.js";
|
|
4
|
+
import {
|
|
5
|
+
observe
|
|
6
|
+
} from "./chunk-KJVNI2UP.js";
|
|
7
|
+
import {
|
|
8
|
+
createProcessFixture
|
|
9
|
+
} from "./chunk-RY6Y6V5E.js";
|
|
10
|
+
import {
|
|
11
|
+
createTestServerSetupCapability
|
|
12
|
+
} from "./chunk-5BZSJ4OO.js";
|
|
13
|
+
import {
|
|
14
|
+
createInProcessWorkerSpawn
|
|
15
|
+
} from "./chunk-OE4NHDVS.js";
|
|
16
|
+
import {
|
|
17
|
+
StubPiTreeHandleFactory
|
|
18
|
+
} from "./chunk-E4Y7XND5.js";
|
|
19
|
+
|
|
20
|
+
// src/extension-integration-harness.ts
|
|
21
|
+
import { randomUUID } from "crypto";
|
|
22
|
+
import { mkdirSync, writeFileSync } from "fs";
|
|
23
|
+
import path2 from "path";
|
|
24
|
+
import { buildExtensionCatalogFromModules } from "@leitwerk-dev/extension-runtime/testing";
|
|
25
|
+
import {
|
|
26
|
+
coreHostCapabilities
|
|
27
|
+
} from "@leitwerk-dev/process-sdk";
|
|
28
|
+
import { createAcceptedLlmTurn, prepareAcceptedFixtureStart } from "@leitwerk-dev/server/testing";
|
|
29
|
+
|
|
30
|
+
// src/integration-harness.ts
|
|
31
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
32
|
+
import { tmpdir } from "os";
|
|
33
|
+
import path from "path";
|
|
34
|
+
import {
|
|
35
|
+
createAppContext,
|
|
36
|
+
getDefaultConfig
|
|
37
|
+
} from "@leitwerk-dev/server";
|
|
38
|
+
function createPersistentIntegrationFixture(prefix, configure) {
|
|
39
|
+
const root = mkdtempSync(path.join(tmpdir(), prefix));
|
|
40
|
+
let harness;
|
|
41
|
+
function createConfig() {
|
|
42
|
+
const config = getDefaultConfig();
|
|
43
|
+
config.storage.sqlite_path = path.join(root, "state.sqlite");
|
|
44
|
+
config.storage.process_workspaces_dir = path.join(root, "workspaces");
|
|
45
|
+
config.storage.tree_files_dir = path.join(root, "trees");
|
|
46
|
+
config.pi.agent_dir = path.join(root, "pi");
|
|
47
|
+
configure?.(config);
|
|
48
|
+
return config;
|
|
49
|
+
}
|
|
50
|
+
async function close() {
|
|
51
|
+
await harness?.close();
|
|
52
|
+
harness = void 0;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
/** @internal */
|
|
56
|
+
root,
|
|
57
|
+
/** @internal */
|
|
58
|
+
createConfig,
|
|
59
|
+
/** @internal */
|
|
60
|
+
context() {
|
|
61
|
+
if (!harness) throw new Error("Fixture is not open");
|
|
62
|
+
return harness.ctx;
|
|
63
|
+
},
|
|
64
|
+
/** @internal */
|
|
65
|
+
async open(options) {
|
|
66
|
+
harness = await createIntegrationHarness({
|
|
67
|
+
...options,
|
|
68
|
+
config: options.config ?? createConfig()
|
|
69
|
+
});
|
|
70
|
+
return harness;
|
|
71
|
+
},
|
|
72
|
+
/** @internal */
|
|
73
|
+
close,
|
|
74
|
+
/** @internal */
|
|
75
|
+
async dispose() {
|
|
76
|
+
try {
|
|
77
|
+
await close();
|
|
78
|
+
} finally {
|
|
79
|
+
rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function createIntegrationHarness(opts) {
|
|
85
|
+
const config = opts.config ?? getDefaultConfig();
|
|
86
|
+
if (!opts.appOverrides?.workerRunnerRuntime) {
|
|
87
|
+
config.workers.runner = "local";
|
|
88
|
+
}
|
|
89
|
+
opts.configOverride?.(config);
|
|
90
|
+
const extensionCatalog = await Promise.resolve(opts.extensionCatalog);
|
|
91
|
+
const appOpts = {
|
|
92
|
+
logger: false,
|
|
93
|
+
config,
|
|
94
|
+
extensionCatalog,
|
|
95
|
+
preProvidedCapabilities: opts.preProvidedCapabilities,
|
|
96
|
+
...opts.appOverrides
|
|
97
|
+
};
|
|
98
|
+
if (opts.inProcessWorkers !== false && !appOpts.localWorkerSpawnImpl) {
|
|
99
|
+
appOpts.localWorkerSpawnImpl = createInProcessWorkerSpawn({
|
|
100
|
+
...typeof opts.inProcessWorkers === "object" ? opts.inProcessWorkers : {},
|
|
101
|
+
extensionCatalog
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const ctx = await createAppContext(appOpts);
|
|
105
|
+
let address = "";
|
|
106
|
+
try {
|
|
107
|
+
if (opts.listen !== false) {
|
|
108
|
+
if (opts.backgroundServices !== false) {
|
|
109
|
+
({ address } = await ctx.listen({
|
|
110
|
+
host: "127.0.0.1",
|
|
111
|
+
port: 0,
|
|
112
|
+
useBoundAddressAsBaseUrl: true
|
|
113
|
+
}));
|
|
114
|
+
} else {
|
|
115
|
+
address = await ctx.app.listen({ host: "127.0.0.1", port: 0 });
|
|
116
|
+
ctx.config.server.base_url = address;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
await ctx.close().catch(() => {
|
|
121
|
+
});
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
ctx,
|
|
126
|
+
close: () => ctx.close(),
|
|
127
|
+
config,
|
|
128
|
+
get address() {
|
|
129
|
+
return address || (ctx.app.server.listening ? ctx.config.server.base_url : "");
|
|
130
|
+
},
|
|
131
|
+
resources: opts.resources ?? {}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/extension-integration-harness.ts
|
|
136
|
+
async function createExtensionIntegrationHarness(options) {
|
|
137
|
+
const catalog = await buildExtensionCatalogFromModules([
|
|
138
|
+
...options.polling === "manual" ? [
|
|
139
|
+
{
|
|
140
|
+
manifest: { id: "test-support-manual-polling", version: "1" },
|
|
141
|
+
setupServer(api) {
|
|
142
|
+
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
143
|
+
if (!deps || Array.isArray(deps)) throw new Error("Missing polling host");
|
|
144
|
+
deps.polling = {
|
|
145
|
+
create(input) {
|
|
146
|
+
return { poll: input.pollOnce };
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
] : [],
|
|
152
|
+
...options.extensions
|
|
153
|
+
]);
|
|
154
|
+
const persistent = createPersistentIntegrationFixture("leitwerk-extension-test-", (config) => {
|
|
155
|
+
config.extensions = structuredClone(options.extensionConfig ?? {});
|
|
156
|
+
if (options.hostDocker && config.local_worker) config.local_worker.allow_host_docker = true;
|
|
157
|
+
config.pi.model_profiles = (options.models ?? []).map((model) => ({
|
|
158
|
+
id: model.id,
|
|
159
|
+
provider: model.provider,
|
|
160
|
+
model_id: model.modelId,
|
|
161
|
+
thinking_level: model.thinkingLevel ?? "off"
|
|
162
|
+
}));
|
|
163
|
+
if (options.defaultModel) {
|
|
164
|
+
config.pi.process_title_generation.model_profile = options.defaultModel;
|
|
165
|
+
config.process_configs = Object.fromEntries(
|
|
166
|
+
[...catalog.processes.keys()].map((id) => [
|
|
167
|
+
id,
|
|
168
|
+
{
|
|
169
|
+
default_model_profile: options.defaultModel,
|
|
170
|
+
turn_configs: {},
|
|
171
|
+
watchers: structuredClone(options.watchers?.[id] ?? {})
|
|
172
|
+
}
|
|
173
|
+
])
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
const manual = options.execution === "manual";
|
|
178
|
+
let closed = false;
|
|
179
|
+
const pending = /* @__PURE__ */ new Map();
|
|
180
|
+
const permits = /* @__PURE__ */ new Set();
|
|
181
|
+
const executing = /* @__PURE__ */ new Set();
|
|
182
|
+
const scripts = /* @__PURE__ */ new Map();
|
|
183
|
+
const observations = /* @__PURE__ */ new Map();
|
|
184
|
+
const context = () => {
|
|
185
|
+
if (closed) throw new Error("Extension integration harness is closed");
|
|
186
|
+
return persistent.context();
|
|
187
|
+
};
|
|
188
|
+
async function open() {
|
|
189
|
+
const piFactory = new StubPiTreeHandleFactory({
|
|
190
|
+
recordSessionTrace: true,
|
|
191
|
+
toolCallScriptResolver: async (input) => {
|
|
192
|
+
const id = input.instanceId;
|
|
193
|
+
if (!id) throw new Error("Scripted prompt has no process identity");
|
|
194
|
+
const observation = observations.get(id) ?? { prompts: [], toolResults: [] };
|
|
195
|
+
observations.set(id, observation);
|
|
196
|
+
const prompt = input.identifiedPromptText ?? input.promptText;
|
|
197
|
+
observation.prompts.push(prompt);
|
|
198
|
+
const session = [...piFactory.sessions].reverse().find((session2) => session2.treeFile === input.treeFile);
|
|
199
|
+
const history = session?.getBranch().map((entry) => {
|
|
200
|
+
if (entry.type === "custom_message") return entry.content;
|
|
201
|
+
if (entry.type === "message" && entry.message) return entry.message.content;
|
|
202
|
+
return "";
|
|
203
|
+
}).filter((value) => typeof value === "string").join("\n\n") ?? "";
|
|
204
|
+
const script = manual ? scripts.get(id) : await options.script?.(
|
|
205
|
+
id,
|
|
206
|
+
prompt,
|
|
207
|
+
observe({
|
|
208
|
+
turnId: context().deps.processes.getById(id)?.selectedTurnId ?? "",
|
|
209
|
+
cwd: input.sessionCwd ?? input.workspaceRoot ?? "",
|
|
210
|
+
branchId: session?.sessionId ?? "",
|
|
211
|
+
history,
|
|
212
|
+
tools: input.tools.map((tool) => ({
|
|
213
|
+
name: tool.name,
|
|
214
|
+
parameters: tool.parameters
|
|
215
|
+
}))
|
|
216
|
+
})
|
|
217
|
+
);
|
|
218
|
+
scripts.delete(id);
|
|
219
|
+
if (!script || script.tools.length === 0 && script.markdown === void 0)
|
|
220
|
+
throw new Error(
|
|
221
|
+
`Script exhausted for process '${id}' after ${observation.prompts.length} prompt(s)`
|
|
222
|
+
);
|
|
223
|
+
return {
|
|
224
|
+
thinkingChunks: script.thinking ? [...script.thinking] : void 0,
|
|
225
|
+
textChunks: script.markdown === void 0 ? void 0 : [script.markdown],
|
|
226
|
+
calls: script.tools.map((call) => ({
|
|
227
|
+
toolName: call.name,
|
|
228
|
+
args: structuredClone(call.arguments)
|
|
229
|
+
})),
|
|
230
|
+
afterToolResult(call, result) {
|
|
231
|
+
observation.toolResults.push({ name: call.toolName, result: structuredClone(result) });
|
|
232
|
+
const next = script.afterToolResult?.(
|
|
233
|
+
{ name: call.toolName, arguments: structuredClone(call.args) },
|
|
234
|
+
structuredClone(result)
|
|
235
|
+
);
|
|
236
|
+
return next ? { toolName: next.name, args: next.arguments } : void 0;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
await persistent.open({
|
|
242
|
+
appOverrides: options.hostDocker ? { localWorkerDockerPreflightImpl: options.hostDocker.preflight } : void 0,
|
|
243
|
+
extensionCatalog: catalog,
|
|
244
|
+
preProvidedCapabilities: options.capabilities,
|
|
245
|
+
inProcessWorkers: {
|
|
246
|
+
piFactory,
|
|
247
|
+
beforeTurnBootstrap: manual ? (id, turnId, signal) => new Promise((resolve, reject) => {
|
|
248
|
+
if (signal.aborted) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (permits.delete(id)) {
|
|
252
|
+
resolve();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (pending.has(id)) {
|
|
256
|
+
reject(new Error(`Conflicting pending worker for '${id}'`));
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const abort = () => {
|
|
260
|
+
pending.delete(id);
|
|
261
|
+
};
|
|
262
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
263
|
+
pending.set(id, {
|
|
264
|
+
turnId,
|
|
265
|
+
resolve() {
|
|
266
|
+
signal.removeEventListener("abort", abort);
|
|
267
|
+
pending.delete(id);
|
|
268
|
+
resolve();
|
|
269
|
+
},
|
|
270
|
+
reject
|
|
271
|
+
});
|
|
272
|
+
}) : void 0
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
await open();
|
|
278
|
+
} catch (error) {
|
|
279
|
+
await persistent.dispose();
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
async function request(input) {
|
|
283
|
+
const response = await context().app.inject({ ...input, method: input.method ?? "GET" });
|
|
284
|
+
return {
|
|
285
|
+
/** @public */
|
|
286
|
+
statusCode: response.statusCode,
|
|
287
|
+
/** @public */
|
|
288
|
+
body: response.body,
|
|
289
|
+
/** @public */
|
|
290
|
+
json() {
|
|
291
|
+
return JSON.parse(response.body);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
async function post(url, payload = {}) {
|
|
296
|
+
const response = await request({ method: "POST", url, payload });
|
|
297
|
+
if (response.statusCode >= 400)
|
|
298
|
+
throw new Error(`POST ${url}: ${response.statusCode} ${response.body}`);
|
|
299
|
+
return response;
|
|
300
|
+
}
|
|
301
|
+
function processHandle(id) {
|
|
302
|
+
const prefix = `/api/processes/${encodeURIComponent(id)}`;
|
|
303
|
+
function snapshot() {
|
|
304
|
+
const { deps } = context();
|
|
305
|
+
const process = deps.processes.getById(id);
|
|
306
|
+
if (!process) throw new Error(`Unknown process '${id}'`);
|
|
307
|
+
const definition = catalog.processes.get(process.processId);
|
|
308
|
+
return observe({
|
|
309
|
+
workspaceRoot: path2.join(context().config.storage.process_workspaces_dir, id),
|
|
310
|
+
events: deps.events.listByInstance(id),
|
|
311
|
+
annotations: deps.turnAnnotations.listByInstance(id),
|
|
312
|
+
leafOutcomes: deps.leafOutcomeSnapshots.listByInstance(id),
|
|
313
|
+
process,
|
|
314
|
+
params: definition?.paramsCodec.parse(JSON.parse(process.paramsJson ?? "null")),
|
|
315
|
+
state: definition?.stateCodec.parse(JSON.parse(process.stateJson ?? "null")),
|
|
316
|
+
projects: deps.projects.listByInstance(id),
|
|
317
|
+
turns: deps.turnRecords.listByInstance(id),
|
|
318
|
+
inputs: deps.inputs.listByInstance(id),
|
|
319
|
+
questions: deps.questionRequests.listByInstance(id),
|
|
320
|
+
approvals: deps.toolApprovalRequests.listByInstance(id),
|
|
321
|
+
writeReceipts: deps.externalWrites.listByInstance(id)
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
async function waitFor(condition, options2 = {}) {
|
|
325
|
+
const deadline = Date.now() + (options2.timeoutMs ?? 12e3);
|
|
326
|
+
for (; ; ) {
|
|
327
|
+
const current = snapshot();
|
|
328
|
+
if (condition(current)) return current;
|
|
329
|
+
if (Date.now() >= deadline)
|
|
330
|
+
throw new Error(
|
|
331
|
+
`Timed out waiting for ${id}: ${options2.description ?? "condition"}; ${JSON.stringify({
|
|
332
|
+
position: {
|
|
333
|
+
selectedTurnId: current.process.selectedTurnId,
|
|
334
|
+
lifecycleStatus: current.process.lifecycleStatus
|
|
335
|
+
},
|
|
336
|
+
turns: current.turns.map((turn) => ({
|
|
337
|
+
id: turn.id,
|
|
338
|
+
turnId: turn.turnId,
|
|
339
|
+
status: turn.status,
|
|
340
|
+
error: turn.errorSummary
|
|
341
|
+
})),
|
|
342
|
+
inputs: current.inputs.map((input) => ({
|
|
343
|
+
id: input.id,
|
|
344
|
+
consumedAt: input.consumedAt
|
|
345
|
+
})),
|
|
346
|
+
questions: current.questions.map((question) => ({
|
|
347
|
+
id: question.id,
|
|
348
|
+
status: question.status
|
|
349
|
+
})),
|
|
350
|
+
approvals: current.approvals.map((approval) => ({
|
|
351
|
+
id: approval.id,
|
|
352
|
+
status: approval.status
|
|
353
|
+
}))
|
|
354
|
+
})}`
|
|
355
|
+
);
|
|
356
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
/** @public */
|
|
361
|
+
id,
|
|
362
|
+
/** Establish correlated accepted records under exclusive server coordination. @public */
|
|
363
|
+
async seedAcceptedTurn(fixture) {
|
|
364
|
+
const ctx = context();
|
|
365
|
+
return ctx.deps.processOperations.runExclusive(id, async () => {
|
|
366
|
+
const process = ctx.deps.processes.getById(id);
|
|
367
|
+
if (!process) throw new Error(`Unknown process '${id}'`);
|
|
368
|
+
const turn = catalog.processes.get(process.processId)?.turns.get(fixture.turnId)?.definition;
|
|
369
|
+
if (!turn || turn.kind !== "llm" && turn.kind !== "automatic")
|
|
370
|
+
throw new Error(`Undeclared worker turn '${fixture.turnId}'`);
|
|
371
|
+
if (process.currentExecution || ctx.deps.leases.getByInstance(id))
|
|
372
|
+
throw new Error("Cannot seed accepted execution while a live execution exists");
|
|
373
|
+
const running = fixture.execution.status === "running";
|
|
374
|
+
if (running && process.selectedTurnId !== fixture.turnId)
|
|
375
|
+
throw new Error("Running fixture must match the selected turn");
|
|
376
|
+
if (fixture.execution.status === "succeeded" && !Object.hasOwn(turn.outcomes ?? {}, fixture.execution.outcome) && turn.turnEnd?.outcome !== fixture.execution.outcome)
|
|
377
|
+
throw new Error(`Undeclared outcome '${fixture.execution.outcome}'`);
|
|
378
|
+
if (fixture.execution.status === "succeeded" && !fixture.execution.markdown.trim())
|
|
379
|
+
throw new Error("A successful accepted fixture requires non-empty result markdown");
|
|
380
|
+
const prepared = turn.kind === "llm" ? await prepareAcceptedFixtureStart(
|
|
381
|
+
ctx,
|
|
382
|
+
process,
|
|
383
|
+
fixture.turnId,
|
|
384
|
+
catalog.piContributions
|
|
385
|
+
) : null;
|
|
386
|
+
return ctx.deps.transaction((repos) => {
|
|
387
|
+
const turnId = `trn_${randomUUID()}`;
|
|
388
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
389
|
+
const input = {
|
|
390
|
+
id: turnId,
|
|
391
|
+
instanceId: id,
|
|
392
|
+
turnId: fixture.turnId,
|
|
393
|
+
status: fixture.execution.status,
|
|
394
|
+
attemptNumber: Math.max(
|
|
395
|
+
0,
|
|
396
|
+
...repos.turnRecords.listByInstance(id).filter((record) => record.turnId === fixture.turnId).map((record) => record.attemptNumber)
|
|
397
|
+
) + 1,
|
|
398
|
+
startedAt: timestamp,
|
|
399
|
+
endedAt: running ? null : timestamp,
|
|
400
|
+
turnResultMarkdown: fixture.execution.status === "succeeded" ? fixture.execution.markdown : null
|
|
401
|
+
};
|
|
402
|
+
if (turn.kind === "llm") {
|
|
403
|
+
if (!prepared) throw new Error("Missing fixture preparation");
|
|
404
|
+
const bundleDir = path2.join(
|
|
405
|
+
ctx.config.storage.process_workspaces_dir,
|
|
406
|
+
id,
|
|
407
|
+
".leitwerk",
|
|
408
|
+
"pi-resource-bundles"
|
|
409
|
+
);
|
|
410
|
+
mkdirSync(path2.join(bundleDir, "starts"), { recursive: true, mode: 448 });
|
|
411
|
+
writeFileSync(
|
|
412
|
+
path2.join(bundleDir, `${prepared.bundle.digest}.tar`),
|
|
413
|
+
prepared.bundle.bytes,
|
|
414
|
+
{ mode: 384 }
|
|
415
|
+
);
|
|
416
|
+
writeFileSync(
|
|
417
|
+
path2.join(bundleDir, "starts", `tsr_${turnId}.json`),
|
|
418
|
+
JSON.stringify({
|
|
419
|
+
startRecordId: `tsr_${turnId}`,
|
|
420
|
+
digest: prepared.bundle.digest,
|
|
421
|
+
persistedAt: timestamp
|
|
422
|
+
}),
|
|
423
|
+
{ mode: 384 }
|
|
424
|
+
);
|
|
425
|
+
createAcceptedLlmTurn(
|
|
426
|
+
{
|
|
427
|
+
...ctx,
|
|
428
|
+
deps: {
|
|
429
|
+
...ctx.deps,
|
|
430
|
+
processes: repos.processes,
|
|
431
|
+
turnRecords: repos.turnRecords,
|
|
432
|
+
turnStarts: repos.turnStarts,
|
|
433
|
+
leases: repos.leases
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
{ ...input, turnType: "llm", modelProfileId: prepared.start.model.profileId },
|
|
437
|
+
{ current: running, preparedStart: prepared?.start }
|
|
438
|
+
);
|
|
439
|
+
} else {
|
|
440
|
+
const lease = repos.leases.create({
|
|
441
|
+
instanceId: id,
|
|
442
|
+
workerId: `wkr_${randomUUID()}`,
|
|
443
|
+
state: running ? "busy" : "exited"
|
|
444
|
+
});
|
|
445
|
+
const start = repos.turnStarts.create({
|
|
446
|
+
instanceId: id,
|
|
447
|
+
turnId: fixture.turnId,
|
|
448
|
+
turnType: "automatic",
|
|
449
|
+
proposedTurnRecordId: turnId,
|
|
450
|
+
startKind: "selected_turn",
|
|
451
|
+
recoveryTurnRecordId: null,
|
|
452
|
+
continuation: null,
|
|
453
|
+
state: {
|
|
454
|
+
kind: "accepted",
|
|
455
|
+
start: { kind: "automatic" },
|
|
456
|
+
turnRecordId: turnId,
|
|
457
|
+
acceptedWorkerLeaseId: lease.id
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
repos.turnRecords.create({
|
|
461
|
+
...input,
|
|
462
|
+
turnType: "automatic",
|
|
463
|
+
turnStartRecordId: start.id,
|
|
464
|
+
acceptedWorkerLeaseId: lease.id
|
|
465
|
+
});
|
|
466
|
+
if (running)
|
|
467
|
+
repos.processes.update(id, {
|
|
468
|
+
currentExecution: { kind: "worker_start", id: start.id }
|
|
469
|
+
});
|
|
470
|
+
else repos.leases.update(lease.id, { exitedAt: timestamp });
|
|
471
|
+
}
|
|
472
|
+
if (fixture.execution.status === "succeeded")
|
|
473
|
+
repos.turnAnnotations.create({
|
|
474
|
+
instanceId: id,
|
|
475
|
+
annotationType: "turn_milestone",
|
|
476
|
+
annotationKey: `turn_milestone:${turnId}`,
|
|
477
|
+
references: [{ kind: "turn_record", turnRecordId: turnId, role: "subject" }],
|
|
478
|
+
payload: {
|
|
479
|
+
turnId: fixture.turnId,
|
|
480
|
+
turnType: turn.kind,
|
|
481
|
+
pathType: "primary",
|
|
482
|
+
outcome: fixture.execution.outcome
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
if (running) repos.processes.update(id, { lifecycleStatus: "active" });
|
|
486
|
+
return observe({
|
|
487
|
+
/** @public */
|
|
488
|
+
id: turnId,
|
|
489
|
+
/** @public */
|
|
490
|
+
instanceId: id,
|
|
491
|
+
/** @public */
|
|
492
|
+
turnId: fixture.turnId,
|
|
493
|
+
/** @public */
|
|
494
|
+
artifact: running ? null : { kind: "turn_result", turnRecordId: turnId }
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
},
|
|
499
|
+
/** Read detached durable business and execution observations. @public */
|
|
500
|
+
snapshot,
|
|
501
|
+
/** Wait with a bounded deadline and final observations on failure. @public */
|
|
502
|
+
waitFor,
|
|
503
|
+
/** Execute one selected worker turn; resolves after durable completion or failure. @public */
|
|
504
|
+
async runTurn(script = { tools: [] }) {
|
|
505
|
+
if (!manual) throw new Error("runTurn requires manual execution mode");
|
|
506
|
+
if (executing.has(id)) throw new Error(`A turn is already running for '${id}'`);
|
|
507
|
+
const before = snapshot();
|
|
508
|
+
const selected = before.process.selectedTurnId;
|
|
509
|
+
const turn = selected ? catalog.processes.get(before.process.processId)?.turns.get(selected)?.definition : void 0;
|
|
510
|
+
if (!selected || !turn || turn.kind !== "llm" && turn.kind !== "automatic" || before.process.lifecycleStatus !== "active")
|
|
511
|
+
throw new Error(`Process '${id}' has no active worker turn: ${JSON.stringify(before)}`);
|
|
512
|
+
executing.add(id);
|
|
513
|
+
const previous = new Set(before.turns.map((turn2) => turn2.id));
|
|
514
|
+
observations.set(id, { prompts: [], toolResults: [] });
|
|
515
|
+
scripts.set(id, script);
|
|
516
|
+
try {
|
|
517
|
+
const gate = pending.get(id);
|
|
518
|
+
if (gate) {
|
|
519
|
+
if (gate.turnId !== selected)
|
|
520
|
+
throw new Error("Pending turn does not match selected turn");
|
|
521
|
+
gate.resolve();
|
|
522
|
+
} else permits.add(id);
|
|
523
|
+
const after = await waitFor(
|
|
524
|
+
(current) => current.turns.some((turn2) => !previous.has(turn2.id) && turn2.status !== "running") || current.process.lifecycleStatus === "error",
|
|
525
|
+
{ description: `durable outcome of ${selected}` }
|
|
526
|
+
);
|
|
527
|
+
const record = after.turns.find((turn2) => !previous.has(turn2.id));
|
|
528
|
+
const milestone = record ? context().deps.turnAnnotations.findByKey(id, `turn_milestone:${record.id}`) : null;
|
|
529
|
+
return observe({
|
|
530
|
+
/** @public */
|
|
531
|
+
turn: record ?? null,
|
|
532
|
+
/** @public */
|
|
533
|
+
outcome: typeof milestone?.payload.outcome === "string" ? milestone.payload.outcome : null,
|
|
534
|
+
/** @public */
|
|
535
|
+
failure: record?.errorSummary ?? (after.process.lifecycleStatus === "error" ? "Worker start failed before acceptance" : null),
|
|
536
|
+
/** @public */
|
|
537
|
+
prompts: observations.get(id)?.prompts ?? [],
|
|
538
|
+
/** @public */
|
|
539
|
+
toolResults: observations.get(id)?.toolResults ?? [],
|
|
540
|
+
/** @public */
|
|
541
|
+
result: record?.status === "succeeded" && record.turnResultMarkdown ? { kind: "turn_result", turnRecordId: record.id } : null
|
|
542
|
+
});
|
|
543
|
+
} finally {
|
|
544
|
+
executing.delete(id);
|
|
545
|
+
scripts.delete(id);
|
|
546
|
+
permits.delete(id);
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
/** Execute an application action. @public */
|
|
550
|
+
action: (actionId, input = {}) => post(`${prefix}/actions/${encodeURIComponent(actionId)}`, { input }),
|
|
551
|
+
/** Retry the failed execution through the application command. @public */
|
|
552
|
+
retry: () => post(`${prefix}/retry`),
|
|
553
|
+
/** Queue an instruction using the application command and normal delivery. @public */
|
|
554
|
+
async supplyInput(input) {
|
|
555
|
+
const result = await context().deps.processEngine.queueInputs(id, [
|
|
556
|
+
{
|
|
557
|
+
source: "app_steer",
|
|
558
|
+
kind: "instruction",
|
|
559
|
+
bodyMarkdown: input.bodyMarkdown,
|
|
560
|
+
...input.product ? { target: { productName: input.product } } : {}
|
|
561
|
+
}
|
|
562
|
+
]);
|
|
563
|
+
if (!result.ok) throw new Error(`Input rejected: ${result.message}`);
|
|
564
|
+
return snapshot();
|
|
565
|
+
},
|
|
566
|
+
/** Answer an open question request. @public */
|
|
567
|
+
answerQuestions: (requestId, answers) => post(`${prefix}/question-requests/${encodeURIComponent(requestId)}/answers`, {
|
|
568
|
+
draft: answers
|
|
569
|
+
}),
|
|
570
|
+
/** Resolve an open tool approval. @public */
|
|
571
|
+
respondToApproval: (requestId, response) => post(`${prefix}/tool-approval-requests/${encodeURIComponent(requestId)}`, response)
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
/** Observe discovered processes. @public */
|
|
576
|
+
processes: () => observe(context().deps.processes.listAll()),
|
|
577
|
+
/** Send an HTTP-shaped request, retaining error status and body. @public */
|
|
578
|
+
request,
|
|
579
|
+
/** Get a handle that remains valid across restart. @public */
|
|
580
|
+
process: processHandle,
|
|
581
|
+
/** Admit work through a registered UI launcher. @public */
|
|
582
|
+
async launch(id, input) {
|
|
583
|
+
const response = await postImmediateLaunch(context().config.server.base_url, id, {
|
|
584
|
+
launcherInput: input
|
|
585
|
+
});
|
|
586
|
+
const body = await response.json();
|
|
587
|
+
if (!response.ok || !body.process)
|
|
588
|
+
throw new Error(`Launch '${id}' failed: ${JSON.stringify(body)}`);
|
|
589
|
+
return processHandle(body.process.id);
|
|
590
|
+
},
|
|
591
|
+
/** Create validated business data and start an active position through the engine. @public */
|
|
592
|
+
async createProcess(definition, input = {}) {
|
|
593
|
+
if (catalog.processes.get(definition.id) !== definition)
|
|
594
|
+
throw new Error(`Process '${definition.id}' must be registered by an extension`);
|
|
595
|
+
const fixture = createProcessFixture(input, definition);
|
|
596
|
+
const { deps } = context();
|
|
597
|
+
const process = deps.transaction((repos) => {
|
|
598
|
+
const process2 = repos.processes.create({
|
|
599
|
+
processId: definition.id,
|
|
600
|
+
title: fixture.title,
|
|
601
|
+
externalId: fixture.externalId,
|
|
602
|
+
externalUrl: fixture.externalUrl,
|
|
603
|
+
metadata: fixture.metadata,
|
|
604
|
+
paramsJson: fixture.paramsJson,
|
|
605
|
+
stateJson: fixture.stateJson,
|
|
606
|
+
selectedTurnId: fixture.selectedTurnId,
|
|
607
|
+
lifecycleStatus: fixture.lifecycleStatus === "active" ? "waiting" : fixture.lifecycleStatus
|
|
608
|
+
});
|
|
609
|
+
if (fixture.planRevision !== 0)
|
|
610
|
+
repos.processes.update(process2.id, { planRevision: fixture.planRevision });
|
|
611
|
+
for (const project of input.projects ?? [])
|
|
612
|
+
repos.projects.create({
|
|
613
|
+
instanceId: process2.id,
|
|
614
|
+
key: project.key,
|
|
615
|
+
repoLocator: project.repoLocator,
|
|
616
|
+
baseBranch: project.baseBranch,
|
|
617
|
+
workBranch: project.workBranch,
|
|
618
|
+
metadata: project.metadata,
|
|
619
|
+
externalId: project.externalId,
|
|
620
|
+
externalUrl: project.externalUrl,
|
|
621
|
+
pipelineStatus: project.pipelineStatus
|
|
622
|
+
});
|
|
623
|
+
return process2;
|
|
624
|
+
});
|
|
625
|
+
if (fixture.lifecycleStatus === "active" && fixture.selectedTurnId) {
|
|
626
|
+
const result = await deps.processEngine.startProcess(process.id, fixture.selectedTurnId);
|
|
627
|
+
if (!result.ok) throw new Error(`Process start failed: ${result.message}`);
|
|
628
|
+
}
|
|
629
|
+
return processHandle(process.id);
|
|
630
|
+
},
|
|
631
|
+
/** Reopen the same file-backed storage and retain existing handles. @public */
|
|
632
|
+
async restart(restartOptions = {}) {
|
|
633
|
+
if (executing.size) throw new Error("Cannot restart during runTurn");
|
|
634
|
+
const { deps } = context();
|
|
635
|
+
const paused = manual ? deps.processes.listAll().flatMap((process) => {
|
|
636
|
+
if (process.lifecycleStatus !== "active" || process.currentExecution?.kind !== "worker_start")
|
|
637
|
+
return [];
|
|
638
|
+
const start = deps.turnStarts.getById(process.currentExecution.id);
|
|
639
|
+
return start?.state.kind === "starting" ? [{ instanceId: process.id, startId: start.id }] : [];
|
|
640
|
+
}) : [];
|
|
641
|
+
await persistent.close();
|
|
642
|
+
if (restartOptions.extensionConfig)
|
|
643
|
+
options.extensionConfig = structuredClone(restartOptions.extensionConfig);
|
|
644
|
+
await restartOptions.whileStopped?.();
|
|
645
|
+
pending.clear();
|
|
646
|
+
permits.clear();
|
|
647
|
+
scripts.clear();
|
|
648
|
+
await open();
|
|
649
|
+
for (const pausedStart of paused) {
|
|
650
|
+
if (context().deps.processes.getById(pausedStart.instanceId)?.lifecycleStatus !== "error")
|
|
651
|
+
continue;
|
|
652
|
+
const result = await context().deps.processEngine.retryStartup(
|
|
653
|
+
pausedStart.instanceId,
|
|
654
|
+
pausedStart.startId
|
|
655
|
+
);
|
|
656
|
+
if (!result.ok)
|
|
657
|
+
throw new Error(`Cannot restore paused turn after restart: ${result.message}`);
|
|
658
|
+
}
|
|
659
|
+
},
|
|
660
|
+
/** Release all resources and remove harness-owned files. @public */
|
|
661
|
+
async close() {
|
|
662
|
+
if (closed) return;
|
|
663
|
+
closed = true;
|
|
664
|
+
try {
|
|
665
|
+
await persistent.dispose();
|
|
666
|
+
} finally {
|
|
667
|
+
pending.clear();
|
|
668
|
+
permits.clear();
|
|
669
|
+
scripts.clear();
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// src/polling.ts
|
|
676
|
+
async function waitForValue(read, predicate, timeoutMs = 5e3) {
|
|
677
|
+
const deadline = Date.now() + timeoutMs;
|
|
678
|
+
while (true) {
|
|
679
|
+
const value = await read();
|
|
680
|
+
if (predicate(value)) {
|
|
681
|
+
return value;
|
|
682
|
+
}
|
|
683
|
+
if (Date.now() >= deadline) {
|
|
684
|
+
throw new Error("Timed out waiting for value");
|
|
685
|
+
}
|
|
686
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
687
|
+
}
|
|
688
|
+
}
|
|
12
689
|
|
|
13
690
|
// src/process-driver.ts
|
|
14
691
|
function createProcessDriver(context) {
|
|
@@ -41,8 +718,11 @@ function createProcessDriver(context) {
|
|
|
41
718
|
}
|
|
42
719
|
}
|
|
43
720
|
return {
|
|
721
|
+
/** @public */
|
|
44
722
|
post,
|
|
723
|
+
/** @public */
|
|
45
724
|
waitForProcess,
|
|
725
|
+
/** @public */
|
|
46
726
|
wait: (id, turn, lifecycle = "waiting", timeout = 12e3) => waitForProcess(
|
|
47
727
|
id,
|
|
48
728
|
(p) => p.selectedTurnId === turn && p.lifecycleStatus === lifecycle,
|
|
@@ -50,18 +730,18 @@ function createProcessDriver(context) {
|
|
|
50
730
|
timeout,
|
|
51
731
|
lifecycle === "error"
|
|
52
732
|
),
|
|
733
|
+
/** @public */
|
|
53
734
|
action: (id, action, input = {}) => post(`/api/processes/${encodeURIComponent(id)}/actions/${encodeURIComponent(action)}`, {
|
|
54
735
|
input
|
|
55
736
|
})
|
|
56
737
|
};
|
|
57
738
|
}
|
|
58
739
|
export {
|
|
59
|
-
|
|
740
|
+
createExtensionIntegrationHarness,
|
|
60
741
|
createIntegrationHarness,
|
|
742
|
+
createPersistentIntegrationFixture,
|
|
61
743
|
createProcessDriver,
|
|
62
|
-
createTestApp,
|
|
63
744
|
createTestServerSetupCapability,
|
|
64
|
-
setupServerExtensionTest,
|
|
65
745
|
waitForValue
|
|
66
746
|
};
|
|
67
747
|
//# sourceMappingURL=integration.js.map
|