@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.
Files changed (130) hide show
  1. package/dist/{chunk-KPSK7EMY.js → chunk-5BZSJ4OO.js} +22 -157
  2. package/dist/chunk-5BZSJ4OO.js.map +1 -0
  3. package/dist/{chunk-2IICIB6D.js → chunk-E4Y7XND5.js} +40 -4
  4. package/dist/chunk-E4Y7XND5.js.map +1 -0
  5. package/dist/{chunk-JUH3LUYK.js → chunk-EXHTWQAE.js} +5 -1
  6. package/dist/chunk-EXHTWQAE.js.map +1 -0
  7. package/dist/chunk-KJVNI2UP.js +17 -0
  8. package/dist/chunk-KJVNI2UP.js.map +1 -0
  9. package/dist/{chunk-7I4OCTF3.js → chunk-OE4NHDVS.js} +15 -4
  10. package/dist/chunk-OE4NHDVS.js.map +1 -0
  11. package/dist/chunk-P2FF4AP7.js +72 -0
  12. package/dist/chunk-P2FF4AP7.js.map +1 -0
  13. package/dist/chunk-RY6Y6V5E.js +54 -0
  14. package/dist/chunk-RY6Y6V5E.js.map +1 -0
  15. package/dist/compact-process-detail-fixture.d.ts +3 -0
  16. package/dist/compact-process-detail-fixture.d.ts.map +1 -1
  17. package/dist/extension-integration-harness.d.ts +357 -0
  18. package/dist/extension-integration-harness.d.ts.map +1 -0
  19. package/dist/fakes/fake-git-ops.d.ts +12 -0
  20. package/dist/fakes/fake-git-ops.d.ts.map +1 -1
  21. package/dist/fakes/fake-llm.d.ts +11 -0
  22. package/dist/fakes/fake-llm.d.ts.map +1 -1
  23. package/dist/fakes/index.d.ts +1 -1
  24. package/dist/fakes/index.d.ts.map +1 -1
  25. package/dist/fakes/index.js +105 -4
  26. package/dist/fakes/index.js.map +1 -1
  27. package/dist/fixtures.d.ts +2 -0
  28. package/dist/fixtures.d.ts.map +1 -1
  29. package/dist/fixtures.js +157 -2
  30. package/dist/fixtures.js.map +1 -1
  31. package/dist/http-launch.d.ts +2 -1
  32. package/dist/http-launch.d.ts.map +1 -1
  33. package/dist/index.d.ts +6 -13
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +13 -119
  36. package/dist/index.js.map +1 -1
  37. package/dist/integration-harness.d.ts +36 -1
  38. package/dist/integration-harness.d.ts.map +1 -1
  39. package/dist/integration.d.ts +3 -3
  40. package/dist/integration.d.ts.map +1 -1
  41. package/dist/integration.js +693 -13
  42. package/dist/integration.js.map +1 -1
  43. package/dist/local-git.d.ts +105 -8
  44. package/dist/local-git.d.ts.map +1 -1
  45. package/dist/local-git.js +155 -24
  46. package/dist/local-git.js.map +1 -1
  47. package/dist/model-provider-fixtures.d.ts +9 -1
  48. package/dist/model-provider-fixtures.d.ts.map +1 -1
  49. package/dist/observations.d.ts +7 -0
  50. package/dist/observations.d.ts.map +1 -0
  51. package/dist/polling.d.ts +1 -0
  52. package/dist/polling.d.ts.map +1 -1
  53. package/dist/process-driver.d.ts +5 -1
  54. package/dist/process-driver.d.ts.map +1 -1
  55. package/dist/process-fixtures.d.ts +56 -0
  56. package/dist/process-fixtures.d.ts.map +1 -0
  57. package/dist/process.d.ts +440 -0
  58. package/dist/process.d.ts.map +1 -0
  59. package/dist/process.js +484 -0
  60. package/dist/process.js.map +1 -0
  61. package/dist/question-fixtures.d.ts +2 -0
  62. package/dist/question-fixtures.d.ts.map +1 -1
  63. package/dist/server-extension-test-harness.d.ts +43 -3
  64. package/dist/server-extension-test-harness.d.ts.map +1 -1
  65. package/dist/stub-worker-entry.js +1 -1
  66. package/dist/supported-question-fixtures.d.ts +96 -0
  67. package/dist/supported-question-fixtures.d.ts.map +1 -0
  68. package/dist/test-capability.d.ts +9 -0
  69. package/dist/test-capability.d.ts.map +1 -0
  70. package/dist/test-diagnostics.d.ts +14 -0
  71. package/dist/test-diagnostics.d.ts.map +1 -0
  72. package/dist/worker-testing/in-process-worker-spawn.d.ts +19 -1
  73. package/dist/worker-testing/in-process-worker-spawn.d.ts.map +1 -1
  74. package/dist/worker-testing/index.d.ts +6 -7
  75. package/dist/worker-testing/index.d.ts.map +1 -1
  76. package/dist/worker-testing/index.js +32 -22
  77. package/dist/worker-testing/index.js.map +1 -1
  78. package/dist/worker-testing/schema-driven-stub-pi.d.ts +5 -0
  79. package/dist/worker-testing/schema-driven-stub-pi.d.ts.map +1 -1
  80. package/dist/worker-testing/stub-pi-controls.d.ts +4 -0
  81. package/dist/worker-testing/stub-pi-controls.d.ts.map +1 -1
  82. package/dist/worker-testing/stub-pi-tree-handle.d.ts +78 -2
  83. package/dist/worker-testing/stub-pi-tree-handle.d.ts.map +1 -1
  84. package/dist/worker-testing/worker-runtime-harness.d.ts +37 -2
  85. package/dist/worker-testing/worker-runtime-harness.d.ts.map +1 -1
  86. package/dist/worker-testing/worker-start-payload.d.ts +17 -1
  87. package/dist/worker-testing/worker-start-payload.d.ts.map +1 -1
  88. package/package.json +14 -8
  89. package/src/compact-process-detail-fixture.ts +9 -1
  90. package/src/extension-integration-harness.ts +1059 -0
  91. package/src/fakes/fake-git-ops.ts +12 -0
  92. package/src/fakes/fake-llm.ts +11 -0
  93. package/src/fakes/index.ts +1 -1
  94. package/src/fixtures.ts +16 -0
  95. package/src/http-launch.ts +2 -1
  96. package/src/index.ts +13 -42
  97. package/src/integration-harness.ts +100 -9
  98. package/src/integration.ts +23 -9
  99. package/src/local-git.ts +196 -45
  100. package/src/model-provider-fixtures.ts +15 -2
  101. package/src/observations.ts +19 -0
  102. package/src/polling.ts +1 -0
  103. package/src/process-driver.ts +7 -1
  104. package/src/process-fixtures.ts +123 -0
  105. package/src/process.ts +768 -0
  106. package/src/question-fixtures.ts +2 -0
  107. package/src/server-extension-test-harness.ts +64 -7
  108. package/src/supported-question-fixtures.ts +168 -0
  109. package/src/test-capability.ts +9 -0
  110. package/src/test-diagnostics.ts +69 -0
  111. package/src/worker-testing/in-process-worker-spawn.ts +44 -4
  112. package/src/worker-testing/index.ts +4 -30
  113. package/src/worker-testing/schema-driven-stub-pi.ts +8 -1
  114. package/src/worker-testing/stub-pi-controls.ts +4 -0
  115. package/src/worker-testing/stub-pi-tree-handle.ts +87 -3
  116. package/src/worker-testing/worker-runtime-harness.ts +57 -6
  117. package/src/worker-testing/worker-start-payload.ts +17 -1
  118. package/dist/chunk-2IICIB6D.js.map +0 -1
  119. package/dist/chunk-3MQO3NCX.js +0 -15
  120. package/dist/chunk-3MQO3NCX.js.map +0 -1
  121. package/dist/chunk-7I4OCTF3.js.map +0 -1
  122. package/dist/chunk-7ZK5QRGP.js +0 -98
  123. package/dist/chunk-7ZK5QRGP.js.map +0 -1
  124. package/dist/chunk-JUH3LUYK.js.map +0 -1
  125. package/dist/chunk-KPSK7EMY.js.map +0 -1
  126. package/dist/chunk-MV4W6NOO.js +0 -105
  127. package/dist/chunk-MV4W6NOO.js.map +0 -1
  128. package/dist/test-app.d.ts +0 -23
  129. package/dist/test-app.d.ts.map +0 -1
  130. package/src/test-app.ts +0 -82
@@ -1,5 +1,6 @@
1
1
  import type { NormalizedQuestion, ProcessQuestionRequest } from "@leitwerk-dev/domain";
2
2
 
3
+ /** @internal */
3
4
  export function createTestQuestion(
4
5
  overrides: Partial<NormalizedQuestion> = {},
5
6
  ): NormalizedQuestion {
@@ -12,6 +13,7 @@ export function createTestQuestion(
12
13
  };
13
14
  }
14
15
 
16
+ /** @internal */
15
17
  export function createTestQuestionRequest(
16
18
  overrides: Partial<ProcessQuestionRequest> = {},
17
19
  ): ProcessQuestionRequest {
@@ -1,5 +1,9 @@
1
1
  import { type ExtensionCatalog, setupServerExtensions } from "@leitwerk-dev/extension-runtime";
2
2
  import { buildExtensionCatalogFromModules } from "@leitwerk-dev/extension-runtime/testing";
3
+ import {
4
+ bindExternalWrites,
5
+ type ExternalWriteLogRepoLike,
6
+ } from "@leitwerk-dev/external-writes/internal";
3
7
  import {
4
8
  type CoreServerSetupDeps,
5
9
  coreHostCapabilities,
@@ -15,17 +19,25 @@ import { flushAsyncWork } from "@leitwerk-dev/worker-protocol";
15
19
 
16
20
  export { flushAsyncWork };
17
21
 
18
- /** Register a test provider and expose its poll result without erasing its type. */
22
+ /** Register a test provider and expose its poll result without erasing its type. @public */
19
23
  export function createPollingTestExtension<T>(
20
24
  manifest: LeitwerkExtensionModule["manifest"],
21
- setup: (api: ServerExtensionAPI) => { poll(): Promise<T> } | undefined,
25
+ setup: (api: ServerExtensionAPI) =>
26
+ | {
27
+ /** @internal */
28
+ poll(): Promise<T>;
29
+ }
30
+ | undefined,
22
31
  ) {
23
32
  let provider: ReturnType<typeof setup>;
24
33
  return {
34
+ /** @internal */
25
35
  manifest,
36
+ /** @internal */
26
37
  setupServer(api: ServerExtensionAPI) {
27
38
  provider = setup(api);
28
39
  },
40
+ /** @public */
29
41
  poll() {
30
42
  if (!provider) throw new Error(`Test provider '${manifest.id}' has not been initialized`);
31
43
  return provider.poll();
@@ -33,21 +45,52 @@ export function createPollingTestExtension<T>(
33
45
  };
34
46
  }
35
47
 
36
- export function createToolCollector() {
48
+ /** @internal */
49
+ export function createToolCollector(
50
+ writes: ExternalWriteLogRepoLike = createInMemoryExternalWriteLog(),
51
+ ) {
37
52
  const tools = new Map<string, IntegrationToolDefinition>();
38
53
  const api = {
39
- tool: (tool: IntegrationToolDefinition) => tools.set(tool.name, tool),
54
+ tool: (tool: IntegrationToolDefinition) =>
55
+ tools.set(tool.name, {
56
+ ...tool,
57
+ execute: (ctx, args) =>
58
+ tool.execute(
59
+ { ...ctx, externalWrites: bindExternalWrites(writes, ctx.process.id) },
60
+ args,
61
+ ),
62
+ }),
40
63
  } as unknown as ServerExtensionAPI;
41
- return { api, tools };
64
+ return {
65
+ /** @internal */
66
+ api,
67
+ /** @internal */
68
+ tools,
69
+ };
42
70
  }
43
71
 
72
+ /** @internal */
44
73
  export interface InMemoryExternalWriteLog {
45
- records: { dedupKey: string }[];
74
+ /** @internal */
75
+ records: {
76
+ /** @internal */
77
+ dedupKey: string;
78
+ }[];
79
+ /** @internal */
46
80
  hasDedupKey(key: string): boolean;
47
- record(input: { dedupKey: string }): { dedupKey: string };
81
+ /** @internal */
82
+ record(input: {
83
+ /** @internal */
84
+ dedupKey: string;
85
+ }): {
86
+ /** @internal */
87
+ dedupKey: string;
88
+ };
89
+ /** @internal */
48
90
  getDedupKeys(): ReadonlySet<string>;
49
91
  }
50
92
 
93
+ /** @internal */
51
94
  export function createInMemoryExternalWriteLog(): InMemoryExternalWriteLog {
52
95
  const dedupKeys = new Set<string>();
53
96
  const records: { dedupKey: string }[] = [];
@@ -159,6 +202,7 @@ function createDefaultServerSetupDeps(): CoreServerSetupDeps {
159
202
  };
160
203
  }
161
204
 
205
+ /** @public */
162
206
  export function createTestServerSetupCapability(
163
207
  overrides: Partial<CoreServerSetupDeps> = {},
164
208
  ): CoreServerSetupDeps {
@@ -187,20 +231,33 @@ export function createTestServerSetupCapability(
187
231
  };
188
232
  }
189
233
 
234
+ /** @internal */
190
235
  export interface ServerExtensionTestHarness {
236
+ /** @internal */
191
237
  catalog: ExtensionCatalog;
238
+ /** @internal */
192
239
  events: ReturnType<typeof createEventBus>;
240
+ /** @internal */
193
241
  serverSetup: CoreServerSetupDeps;
242
+ /** @internal */
194
243
  startHooks: Array<() => void | Promise<void>>;
244
+ /** @internal */
195
245
  stopHooks: Array<() => void | Promise<void>>;
246
+ /** @internal */
196
247
  flushAsyncWork: typeof flushAsyncWork;
197
248
  }
198
249
 
250
+ /** @internal */
199
251
  export async function setupServerExtensionTest(input: {
252
+ /** @internal */
200
253
  modules: readonly LeitwerkExtensionModule[];
254
+ /** @internal */
201
255
  providedCapabilities?: readonly ProvidedCapability[];
256
+ /** @internal */
202
257
  serverSetup?: Partial<CoreServerSetupDeps>;
258
+ /** @internal */
203
259
  onStart?: (handler: () => void | Promise<void>) => void;
260
+ /** @internal */
204
261
  onStop?: (handler: () => void | Promise<void>) => void;
205
262
  }): Promise<ServerExtensionTestHarness> {
206
263
  const catalog = await buildExtensionCatalogFromModules(input.modules);
@@ -0,0 +1,168 @@
1
+ import { type Actor, normalizeAskQuestionsInput } from "@leitwerk-dev/domain";
2
+
3
+ /** A normalized question with stable option identities. @public */
4
+ export interface QuestionFixture {
5
+ /** @public */
6
+ id: string;
7
+ /** @public */
8
+ question: string;
9
+ /** @public */
10
+ selection: "single" | "multiple";
11
+ /** @public */
12
+ options: readonly {
13
+ /** @public */
14
+ id: string;
15
+ /** @public */
16
+ label: string;
17
+ /** @public */
18
+ details: string | null;
19
+ }[];
20
+ }
21
+
22
+ /** Correlated question request data without persistence access. @public */
23
+ export interface QuestionRequestFixture {
24
+ /** @public */
25
+ id: string;
26
+ /** @public */
27
+ instanceId: string;
28
+ /** @public */
29
+ turnRecordId: string;
30
+ /** @public */
31
+ toolCallId: string;
32
+ /** @public */
33
+ questions: readonly QuestionFixture[];
34
+ /** @public */
35
+ status: "open" | "answered" | "cancelled";
36
+ /** @public */
37
+ answers: readonly string[] | null;
38
+ /** @public */
39
+ askedAt: string;
40
+ /** @public */
41
+ answeredAt: string | null;
42
+ /** @public */
43
+ answeredBy: Actor | null;
44
+ /** @public */
45
+ cancelledAt: string | null;
46
+ }
47
+
48
+ /** Question content with generated stable option identities. @public */
49
+ export interface QuestionFixtureOptions {
50
+ /** @public */
51
+
52
+ question?: string;
53
+
54
+ /** @public */
55
+
56
+ selection?: "single" | "multiple";
57
+
58
+ /** @public */
59
+
60
+ options?: readonly {
61
+ /** @public */
62
+ label: string;
63
+
64
+ /** @public */
65
+ details?: string | null;
66
+ }[];
67
+ }
68
+
69
+ /** Normalize content using the application's question contract. @public */
70
+ export function createQuestionFixture(options: QuestionFixtureOptions = {}): QuestionFixture {
71
+ const [question] = normalizeAskQuestionsInput({
72
+ questions: [
73
+ {
74
+ question: options.question ?? "Choose a strategy",
75
+ selection: options.selection ?? "single",
76
+ options: options.options ?? [{ label: "Safe", details: "Small change" }],
77
+ },
78
+ ],
79
+ });
80
+ if (!question) throw new Error("Question normalization returned no question");
81
+ return question;
82
+ }
83
+
84
+ /** Resolution determines status and all resolution timestamps. @public */
85
+ export type QuestionFixtureResolution =
86
+ | {
87
+ /** @public */
88
+ status: "answered";
89
+
90
+ /** @public */
91
+
92
+ answers: readonly string[];
93
+
94
+ /** @public */
95
+
96
+ actor?: Actor;
97
+ }
98
+ | {
99
+ /** @public */
100
+ status: "cancelled";
101
+ };
102
+
103
+ /** Reference inputs and question content, without durable status overrides. @public */
104
+ export interface QuestionRequestFixtureOptions {
105
+ /** @public */
106
+
107
+ id?: string;
108
+
109
+ /** @public */
110
+
111
+ process?: {
112
+ /** @public */
113
+ id: string;
114
+ };
115
+
116
+ /** @public */
117
+
118
+ turn?: {
119
+ /** @public */
120
+ id: string;
121
+
122
+ /** @public */
123
+ instanceId: string;
124
+ };
125
+
126
+ /** @public */
127
+
128
+ questions?: readonly QuestionFixtureOptions[];
129
+
130
+ /** @public */
131
+
132
+ resolution?: QuestionFixtureResolution;
133
+ }
134
+
135
+ /** Create a consistent request; reject conflicting process and turn references. @public */
136
+ export function createQuestionRequestFixture(
137
+ options: QuestionRequestFixtureOptions = {},
138
+ ): QuestionRequestFixture {
139
+ const instanceId = options.process?.id ?? options.turn?.instanceId ?? "agt_test";
140
+ if (options.turn && options.turn.instanceId !== instanceId)
141
+ throw new Error("Question turn belongs to another process");
142
+ const questions = normalizeAskQuestionsInput({
143
+ questions: (options.questions ?? [{}]).map((q) => ({
144
+ question: q.question ?? "Choose a strategy",
145
+ selection: q.selection ?? "single",
146
+ options: q.options ?? [{ label: "Safe" }],
147
+ })),
148
+ });
149
+ const resolution = options.resolution;
150
+ if (resolution?.status === "answered" && resolution.answers.length !== questions.length)
151
+ throw new Error("Supply one answer per question");
152
+ return {
153
+ id: options.id ?? "qst_test",
154
+ instanceId,
155
+ turnRecordId: options.turn?.id ?? "trn_test",
156
+ toolCallId: "tool_test",
157
+ questions,
158
+ status: resolution?.status ?? "open",
159
+ askedAt: "2026-01-01T00:00:00.000Z",
160
+ answers: resolution?.status === "answered" ? [...resolution.answers] : null,
161
+ answeredAt: resolution?.status === "answered" ? "2026-01-01T00:00:01.000Z" : null,
162
+ answeredBy:
163
+ resolution?.status === "answered"
164
+ ? structuredClone(resolution.actor ?? { id: "test-user", kind: "user", provider: null })
165
+ : null,
166
+ cancelledAt: resolution?.status === "cancelled" ? "2026-01-01T00:00:01.000Z" : null,
167
+ };
168
+ }
@@ -0,0 +1,9 @@
1
+ import type { CapabilityToken } from "@leitwerk-dev/process-sdk";
2
+
3
+ /** Extension-owned adapter supplied to a harness capability token. @public */
4
+ export interface ExtensionTestCapability<T = unknown> {
5
+ /** @public */
6
+ token: CapabilityToken<T>;
7
+ /** @public */
8
+ value: T;
9
+ }
@@ -0,0 +1,69 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { threadId } from "node:worker_threads";
3
+
4
+ const active = new AsyncLocalStorage<ReturnType<typeof createTestDiagnostics>>();
5
+
6
+ /** @internal Bounded, in-memory trace. Call report from onTestFailed; successful tests stay quiet. */
7
+ export function createTestDiagnostics(label: string) {
8
+ const started = performance.now();
9
+ const events: Record<string, unknown>[] = [];
10
+ let dropped = 0;
11
+ const mark = (stage: string, details: Record<string, unknown> = {}) => {
12
+ if (events.length === 512) {
13
+ events.shift();
14
+ dropped++;
15
+ }
16
+ events.push({
17
+ ...details,
18
+ stage,
19
+ at: new Date().toISOString(),
20
+ elapsedMs: Math.round(performance.now() - started),
21
+ });
22
+ };
23
+ const diagnostics = {
24
+ /** @internal */
25
+ mark,
26
+ /** @internal */
27
+ run<T>(fn: () => T): T {
28
+ return active.run(diagnostics, fn);
29
+ },
30
+ /** @internal */
31
+ format() {
32
+ return JSON.stringify({ label, pid: process.pid, threadId, dropped, events }, null, 2);
33
+ },
34
+ /** @internal */
35
+ report() {
36
+ mark("diagnostics.report");
37
+ console.error(diagnostics.format());
38
+ },
39
+ };
40
+ mark("test.start");
41
+ return diagnostics;
42
+ }
43
+
44
+ /** @internal Logs only the operation name and exit metadata, never arguments, output or environment. */
45
+ export function traceTestSubprocess<T>(operation: string, run: () => T): T {
46
+ const trace = active.getStore();
47
+ if (!trace) return run();
48
+ const started = performance.now();
49
+ trace.mark("subprocess.start", { operation });
50
+ try {
51
+ const result = run();
52
+ trace.mark("subprocess.exit", {
53
+ operation,
54
+ status: 0,
55
+ durationMs: Math.round(performance.now() - started),
56
+ });
57
+ return result;
58
+ } catch (error) {
59
+ const details = error as { status?: unknown; signal?: unknown; code?: unknown } | null;
60
+ trace.mark("subprocess.exit", {
61
+ operation,
62
+ durationMs: Math.round(performance.now() - started),
63
+ status: typeof details?.status === "number" ? details.status : null,
64
+ signal: typeof details?.signal === "string" ? details.signal : null,
65
+ code: typeof details?.code === "string" ? details.code : null,
66
+ });
67
+ throw error;
68
+ }
69
+ }
@@ -15,18 +15,41 @@ const WebSocket =
15
15
  (require("ws") as { default?: unknown }).default ??
16
16
  require("ws");
17
17
 
18
+ /** @public */
18
19
  export interface InProcessWorkerSpawnOptions {
20
+ /** @public */
19
21
  extensionCatalog?: ExtensionCatalog | Promise<ExtensionCatalog>;
22
+ /** @public */
20
23
  piFactory?: PiTreeHandleFactory;
24
+ /** @public */
21
25
  toolCallScriptResolver?: StubToolCallScriptResolver;
22
- /** Optional delays at real connection and managed-runtime preparation boundaries. */
23
- startupDelays?: (instanceId: string) => { connectMs: number; prepareMs: number } | undefined;
26
+ /** @internal */
27
+ beforeTurnBootstrap?(instanceId: string, turnId: string, signal: AbortSignal): Promise<void>;
28
+ /** Optional delays at real connection and managed-runtime preparation boundaries. @internal */
29
+ startupDelays?: (instanceId: string) =>
30
+ | {
31
+ /** @internal */
32
+ connectMs: number;
33
+ /** @internal */
34
+ prepareMs: number;
35
+ }
36
+ | undefined;
37
+ /** @internal Sanitized IPC observations, correlated even after fixture teardown. */
38
+ onConnectionDiagnostic?: (event: {
39
+ /** @internal */
40
+ instanceId: string;
41
+ /** @internal */
42
+ workerId: string;
43
+ /** @internal */
44
+ message: string;
45
+ }) => void;
24
46
  }
25
47
 
26
48
  /**
27
49
  * Fast local-runner-shaped spawn fake. The worker receives the local runner's
28
50
  * env and connects over WebSocket just like Docker/Kubernetes workers do.
29
51
  */
52
+ /** @public */
30
53
  export function createInProcessWorkerSpawn(
31
54
  options: InProcessWorkerSpawnOptions = {},
32
55
  ): typeof spawn {
@@ -61,15 +84,26 @@ export function createInProcessWorkerSpawn(
61
84
  stderr,
62
85
  pid: Math.floor(Math.random() * 100_000) + 1_000,
63
86
  kill: (_signal?: NodeJS.Signals | number) => {
64
- finish(0);
65
- void runtime?.stop("test_kill").catch(() => {});
87
+ cancellation.abort();
88
+ void Promise.resolve(runtime?.stop("test_kill"))
89
+ .finally(() => finish(0))
90
+ .catch(() => {});
66
91
  return true;
67
92
  },
68
93
  spawnfile: command,
69
94
  spawnargs: [command, ...args],
70
95
  });
71
96
 
97
+ const beforeTurnBootstrap = options.beforeTurnBootstrap;
72
98
  runtime = createWorkerEntryRuntime({
99
+ beforeTurnBootstrap: beforeTurnBootstrap
100
+ ? (turnId) =>
101
+ beforeTurnBootstrap(
102
+ spawnOptions?.env?.LEITWERK_INSTANCE_ID ?? "",
103
+ turnId,
104
+ cancellation.signal,
105
+ )
106
+ : undefined,
73
107
  extensionCatalog: options.extensionCatalog,
74
108
  piFactory: timing
75
109
  ? {
@@ -82,6 +116,12 @@ export function createInProcessWorkerSpawn(
82
116
  }
83
117
  : piFactory,
84
118
  stderr,
119
+ onConnectionDiagnostic: (message) =>
120
+ options.onConnectionDiagnostic?.({
121
+ instanceId: spawnOptions?.env?.LEITWERK_INSTANCE_ID ?? "",
122
+ workerId: spawnOptions?.env?.LEITWERK_WORKER_ID ?? "",
123
+ message,
124
+ }),
85
125
  env: spawnOptions?.env ?? process.env,
86
126
  exit: finish,
87
127
  });
@@ -1,40 +1,14 @@
1
- export {
2
- createTestConfigSnapshot as testConfigSnapshot,
3
- createTestIpcEnvelopeBase as baseEnvelope,
4
- flushAsyncWork,
5
- } from "@leitwerk-dev/worker-protocol";
6
- export {
7
- createInProcessWorkerSpawn,
8
- type InProcessWorkerSpawnOptions,
9
- } from "../in-process-worker.js";
10
- export {
11
- createSchemaDrivenStubPiFactory,
12
- createSchemaDrivenToolCallScriptResolver,
13
- synthesizeStubArgValue,
14
- synthesizeStubToolArgs,
15
- } from "./schema-driven-stub-pi.js";
16
- export {
17
- createStubToolScriptController,
18
- type StubToolScriptController,
19
- } from "./stub-pi-controls.js";
1
+ export { flushAsyncWork } from "@leitwerk-dev/worker-protocol";
2
+ export { createInProcessWorkerSpawn } from "../in-process-worker.js";
3
+ export { createStubToolScriptController } from "./stub-pi-controls.js";
20
4
  export {
21
5
  StubPiTreeHandle,
22
6
  StubPiTreeHandleFactory,
23
- type StubPiTreeHandleFactoryOptions,
24
7
  type StubToolCallScriptCall,
25
- type StubToolCallScriptItem,
26
8
  type StubToolCallScriptResolver,
27
- type StubToolCallScriptResolverContext,
28
9
  } from "./stub-pi-tree-handle.js";
29
10
  export {
30
11
  createManualWorkerRuntimeScheduler,
31
12
  createWorkerRuntimeHarness,
32
- type ManualWorkerRuntimeScheduler,
33
- type WorkerRuntimeHarness,
34
- type WorkerRuntimeHarnessOptions,
35
- type WorkerRuntimeObservation,
36
13
  } from "./worker-runtime-harness.js";
37
- export {
38
- createTestLlmWorkerStartPayload,
39
- type TestLlmWorkerStartPayloadOptions,
40
- } from "./worker-start-payload.js";
14
+ export { createTestLlmWorkerStartPayload } from "./worker-start-payload.js";
@@ -8,6 +8,7 @@ function isObjectLike(value: unknown): value is Record<string, unknown> {
8
8
  return typeof value === "object" && value !== null;
9
9
  }
10
10
 
11
+ /** @internal */
11
12
  export function synthesizeStubArgValue(key: string, spec: unknown): unknown {
12
13
  if (key === "changedProjects") {
13
14
  return undefined;
@@ -30,6 +31,7 @@ export function synthesizeStubArgValue(key: string, spec: unknown): unknown {
30
31
  }
31
32
  }
32
33
 
34
+ /** @internal */
33
35
  export function synthesizeStubToolArgs(
34
36
  parameters: Record<string, unknown>,
35
37
  ): Record<string, unknown> {
@@ -42,6 +44,7 @@ export function synthesizeStubToolArgs(
42
44
  );
43
45
  }
44
46
 
47
+ /** @internal */
45
48
  export function createSchemaDrivenToolCallScriptResolver(): StubToolCallScriptResolver {
46
49
  return ({ tools }: StubToolCallScriptResolverContext) => {
47
50
  const markdownTool = tools.find((tool) => tool.name === "markdown_result");
@@ -73,8 +76,12 @@ export function createSchemaDrivenToolCallScriptResolver(): StubToolCallScriptRe
73
76
  };
74
77
  }
75
78
 
79
+ /** @internal */
76
80
  export function createSchemaDrivenStubPiFactory(
77
- options: { toolCallScriptResolver?: StubToolCallScriptResolver } = {},
81
+ options: {
82
+ /** @internal */
83
+ toolCallScriptResolver?: StubToolCallScriptResolver;
84
+ } = {},
78
85
  ): StubPiTreeHandleFactory {
79
86
  return new StubPiTreeHandleFactory({
80
87
  toolCallScriptResolver:
@@ -1,10 +1,14 @@
1
1
  import type { StubToolCallScriptItem, StubToolCallScriptResolver } from "./stub-pi-tree-handle.js";
2
2
 
3
+ /** @internal */
3
4
  export interface StubToolScriptController {
5
+ /** @internal */
4
6
  readonly resolver: StubToolCallScriptResolver;
7
+ /** @internal */
5
8
  set(script: StubToolCallScriptItem[]): void;
6
9
  }
7
10
 
11
+ /** @internal */
8
12
  export function createStubToolScriptController(): StubToolScriptController {
9
13
  const queue: StubToolCallScriptItem[] = [];
10
14
  return {