@llm4ts/shell 2.45.0 → 2.47.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 (67) hide show
  1. package/dist/Cli.d.ts +3 -7
  2. package/dist/Cli.d.ts.map +1 -1
  3. package/dist/Cli.js +20 -4
  4. package/dist/Cli.js.map +1 -1
  5. package/dist/FlowLaunch.d.ts +2 -16
  6. package/dist/FlowLaunch.d.ts.map +1 -1
  7. package/dist/FlowLaunch.js +20 -66
  8. package/dist/FlowLaunch.js.map +1 -1
  9. package/dist/FlowProcess.d.ts +67 -0
  10. package/dist/FlowProcess.d.ts.map +1 -0
  11. package/dist/FlowProcess.js +175 -0
  12. package/dist/FlowProcess.js.map +1 -0
  13. package/dist/Menu.d.ts +1 -1
  14. package/dist/Menu.d.ts.map +1 -1
  15. package/dist/OperatorActTools.d.ts +170 -0
  16. package/dist/OperatorActTools.d.ts.map +1 -0
  17. package/dist/OperatorActTools.js +332 -0
  18. package/dist/OperatorActTools.js.map +1 -0
  19. package/dist/OperatorConfig.d.ts +44 -0
  20. package/dist/OperatorConfig.d.ts.map +1 -0
  21. package/dist/OperatorConfig.js +88 -0
  22. package/dist/OperatorConfig.js.map +1 -0
  23. package/dist/OperatorNode.d.ts +45 -0
  24. package/dist/OperatorNode.d.ts.map +1 -0
  25. package/dist/OperatorNode.js +103 -0
  26. package/dist/OperatorNode.js.map +1 -0
  27. package/dist/OperatorRuns.d.ts +76 -0
  28. package/dist/OperatorRuns.d.ts.map +1 -0
  29. package/dist/OperatorRuns.js +146 -0
  30. package/dist/OperatorRuns.js.map +1 -0
  31. package/dist/OperatorServer.d.ts +160 -0
  32. package/dist/OperatorServer.d.ts.map +1 -0
  33. package/dist/OperatorServer.js +123 -0
  34. package/dist/OperatorServer.js.map +1 -0
  35. package/dist/OperatorTools.d.ts +558 -0
  36. package/dist/OperatorTools.d.ts.map +1 -0
  37. package/dist/OperatorTools.js +398 -0
  38. package/dist/OperatorTools.js.map +1 -0
  39. package/dist/Refine.d.ts +2 -2
  40. package/dist/Refine.d.ts.map +1 -1
  41. package/dist/Refine.js +3 -5
  42. package/dist/Refine.js.map +1 -1
  43. package/dist/RunSupervisor.d.ts +51 -0
  44. package/dist/RunSupervisor.d.ts.map +1 -0
  45. package/dist/RunSupervisor.js +282 -0
  46. package/dist/RunSupervisor.js.map +1 -0
  47. package/dist/ShellErrors.d.ts +9 -0
  48. package/dist/ShellErrors.d.ts.map +1 -0
  49. package/dist/ShellErrors.js +7 -0
  50. package/dist/ShellErrors.js.map +1 -0
  51. package/flows/epic-mvp.js +21 -0
  52. package/flows/lib/epic-mvp.js +451 -0
  53. package/flows/lib/epic-stories.js +88 -48
  54. package/flows/port-guide.js +4 -1
  55. package/package.json +13 -4
  56. package/src/Cli.ts +64 -3
  57. package/src/FlowLaunch.ts +20 -66
  58. package/src/FlowProcess.ts +239 -0
  59. package/src/OperatorActTools.ts +407 -0
  60. package/src/OperatorConfig.ts +148 -0
  61. package/src/OperatorNode.ts +147 -0
  62. package/src/OperatorRuns.ts +270 -0
  63. package/src/OperatorServer.ts +192 -0
  64. package/src/OperatorTools.ts +610 -0
  65. package/src/Refine.ts +4 -7
  66. package/src/RunSupervisor.ts +403 -0
  67. package/src/ShellErrors.ts +6 -0
@@ -0,0 +1,407 @@
1
+ import * as Clock from "effect/Clock"
2
+ import * as Effect from "effect/Effect"
3
+ import * as Schema from "effect/Schema"
4
+ import * as Tool from "effect/ai/Tool"
5
+ import * as Toolkit from "effect/ai/Toolkit"
6
+ import { approveMarkdown } from "@llm4ts/flow/Approval"
7
+ import { PersistenceError } from "@llm4ts/flow/FlowError"
8
+ import {
9
+ InvalidKnob,
10
+ NotAllowed,
11
+ OperatorRun,
12
+ ReadFailed,
13
+ RunInProgress,
14
+ RunWaitResult,
15
+ UnknownEpic,
16
+ UnknownFlow,
17
+ UnknownRepo,
18
+ UnknownRun,
19
+ type OperatorKnobs,
20
+ type OperatorRunState
21
+ } from "@llm4ts/flow/OperatorSchemas"
22
+ import { retroApprovalOf, retroPaths } from "@llm4ts/flow/Retro"
23
+ import { emptyTree } from "@llm4ts/runner/AgentTree"
24
+ import {
25
+ TraceUnreadable,
26
+ foldTrace,
27
+ meaningfulEventsAfter,
28
+ runStatusOf,
29
+ type TraceFold
30
+ } from "@llm4ts/runner/RunReaders"
31
+ import { FlowLaunchError } from "./FlowProcess.ts"
32
+ import { resolveRepo, type OperatorDeps } from "./OperatorTools.ts"
33
+ import {
34
+ epicDirOf,
35
+ epicOfRun,
36
+ readEpicRunsStrict,
37
+ readFailed,
38
+ resolveRun,
39
+ tracePending,
40
+ type Repo
41
+ } from "./OperatorRuns.ts"
42
+
43
+ /**
44
+ * The operator's `run_wait` and act tools (`llm4ts mcp`, ADR 0041): waiting on
45
+ * a run without polling from the client, and starting, stopping, retro-ing and
46
+ * approving through the run supervisor. `run_wait` only reads, so it has its
47
+ * own toolkit the server registers beside the read tools; the act toolkit is
48
+ * registered only when the configuration allows runs or approvals.
49
+ */
50
+
51
+ const repoParam = Schema.optional(Schema.String)
52
+
53
+ // ---- run_wait -----------------------------------------------------------------------------
54
+
55
+ export const runWaitTool = Tool.make("run_wait", {
56
+ description:
57
+ "Wait for a run's next meaningful events after sinceSeq: returns as soon as one appears or the run is no longer running, else after timeoutSeconds (default 120, at most 300) with no events.",
58
+ parameters: Schema.Struct({
59
+ runId: Schema.String,
60
+ sinceSeq: Schema.optional(Schema.Int),
61
+ timeoutSeconds: Schema.optional(Schema.Int)
62
+ }),
63
+ success: RunWaitResult,
64
+ failure: Schema.Union([UnknownRun, TraceUnreadable, ReadFailed])
65
+ })
66
+ .annotate(Tool.Readonly, true)
67
+ .annotate(Tool.Destructive, false)
68
+
69
+ export const waitToolkit = Toolkit.make(runWaitTool)
70
+
71
+ const pollInterval = "500 millis"
72
+ const defaultWaitSeconds = 120
73
+ const maxWaitSeconds = 300
74
+
75
+ const isActive = (state: OperatorRunState): boolean => state === "starting" || state === "running"
76
+
77
+ const emptyFold = (): TraceFold => ({ lines: [], state: emptyTree(), seq: 0 })
78
+
79
+ export const waitHandlers = (
80
+ deps: OperatorDeps
81
+ ): Toolkit.HandlersFrom<Toolkit.Tools<typeof waitToolkit>> =>
82
+ waitToolkit.of({
83
+ run_wait: ({ runId, sinceSeq, timeoutSeconds }) =>
84
+ Effect.gen(function* () {
85
+ const since = sinceSeq ?? 0
86
+ const timeoutMs =
87
+ Math.min(Math.max(0, timeoutSeconds ?? defaultWaitSeconds), maxWaitSeconds) * 1_000
88
+ const run = yield* resolveRun(deps, runId)
89
+ const epic = yield* epicOfRun(deps, run)
90
+ const startedAt = yield* Clock.currentTimeMillis
91
+ for (;;) {
92
+ const fold = (yield* tracePending(deps.files, run))
93
+ ? emptyFold()
94
+ : yield* foldTrace(deps.files, run.tracePath, epic?.dir)
95
+ // A supervised run's state is the supervisor's; a run found on disk ends with its trace.
96
+ const state: OperatorRunState =
97
+ run.supervised === undefined
98
+ ? fold.lines.some((line) => line.kind === "RunEnded")
99
+ ? "exited"
100
+ : run.state
101
+ : ((yield* deps.supervisor.get(runId))?.state ?? run.state)
102
+ const events = meaningfulEventsAfter(fold.lines, since)
103
+ const exited = !isActive(state)
104
+ const elapsed = (yield* Clock.currentTimeMillis) - startedAt
105
+ if (events.length > 0 || exited || elapsed >= timeoutMs) {
106
+ const status = runStatusOf(fold, { ...run, state }, yield* deps.now)
107
+ return RunWaitResult.make({
108
+ events,
109
+ seq: Math.max(fold.seq, since),
110
+ ...(status.ended === undefined ? {} : { ended: status.ended }),
111
+ exited,
112
+ summary: status.summary
113
+ })
114
+ }
115
+ yield* Effect.sleep(pollInterval)
116
+ }
117
+ })
118
+ })
119
+
120
+ // ---- Act tools ----------------------------------------------------------------------------
121
+
122
+ export const StartedRun = Schema.Struct({
123
+ runId: Schema.String,
124
+ tracePath: Schema.optionalKey(Schema.String),
125
+ pid: Schema.optionalKey(Schema.Int)
126
+ })
127
+
128
+ const startFailures = Schema.Union([
129
+ NotAllowed,
130
+ InvalidKnob,
131
+ UnknownRepo,
132
+ UnknownEpic,
133
+ UnknownRun,
134
+ UnknownFlow,
135
+ ReadFailed,
136
+ FlowLaunchError,
137
+ RunInProgress,
138
+ PersistenceError
139
+ ])
140
+
141
+ const StartRunParams = Schema.Struct({
142
+ repo: repoParam,
143
+ flow: Schema.String,
144
+ task: Schema.optional(Schema.String),
145
+ epic: Schema.optional(Schema.String),
146
+ pack: Schema.optional(Schema.String),
147
+ executors: Schema.optional(Schema.Array(Schema.String)),
148
+ coder: Schema.optional(Schema.String),
149
+ judgeRounds: Schema.optional(Schema.Int),
150
+ mergeRevisions: Schema.optional(Schema.Int),
151
+ land: Schema.optional(Schema.String),
152
+ flags: Schema.optional(Schema.Array(Schema.String)),
153
+ requestId: Schema.optional(Schema.String)
154
+ })
155
+
156
+ export const startRunTool = Tool.make("start_run", {
157
+ description:
158
+ "Start a flow by name in the background with typed knobs; returns the run id at once. Repeating a call is safe only with the same requestId, which returns the run it already started; without one, every call starts a new run.",
159
+ parameters: StartRunParams,
160
+ success: StartedRun,
161
+ failure: startFailures
162
+ }).annotate(Tool.Destructive, false)
163
+
164
+ export const stopRunTool = Tool.make("stop_run", {
165
+ description:
166
+ "Stop a run this server started: SIGINT, then SIGTERM after a grace period; returns its final record.",
167
+ parameters: Schema.Struct({ runId: Schema.String }),
168
+ success: OperatorRun,
169
+ failure: Schema.Union([NotAllowed, UnknownRun])
170
+ }).annotate(Tool.Destructive, true)
171
+
172
+ export const retroStartTool = Tool.make("retro_start", {
173
+ description:
174
+ "Start epic-retro for an epic, optionally for one of its recorded runs; the retro report then waits for approve.",
175
+ parameters: Schema.Struct({
176
+ repo: repoParam,
177
+ epic: Schema.String,
178
+ run: Schema.optional(Schema.String)
179
+ }),
180
+ success: StartedRun,
181
+ failure: startFailures
182
+ }).annotate(Tool.Destructive, false)
183
+
184
+ export const approveTool = Tool.make("approve", {
185
+ description:
186
+ "Approve a retro report: ticks its approval marker so the next epic run applies its fixes.",
187
+ parameters: Schema.Struct({
188
+ repo: repoParam,
189
+ kind: Schema.Literals(["retro"]),
190
+ epic: Schema.String,
191
+ id: Schema.String
192
+ }),
193
+ success: Schema.Struct({ path: Schema.String, approved: Schema.Boolean }),
194
+ failure: Schema.Union([
195
+ NotAllowed,
196
+ UnknownRepo,
197
+ UnknownEpic,
198
+ UnknownRun,
199
+ ReadFailed,
200
+ PersistenceError
201
+ ])
202
+ }).annotate(Tool.Destructive, false)
203
+
204
+ export const actToolkit = Toolkit.make(startRunTool, stopRunTool, retroStartTool, approveTool)
205
+
206
+ const knobsOf = (params: typeof StartRunParams.Type): OperatorKnobs => ({
207
+ ...(params.task === undefined ? {} : { task: params.task }),
208
+ ...(params.epic === undefined ? {} : { epic: params.epic }),
209
+ ...(params.pack === undefined ? {} : { pack: params.pack }),
210
+ ...(params.executors === undefined ? {} : { executors: params.executors }),
211
+ ...(params.coder === undefined ? {} : { coder: params.coder }),
212
+ ...(params.judgeRounds === undefined ? {} : { judgeRounds: params.judgeRounds }),
213
+ ...(params.mergeRevisions === undefined ? {} : { mergeRevisions: params.mergeRevisions }),
214
+ ...(params.land === undefined ? {} : { land: params.land }),
215
+ ...(params.flags === undefined ? {} : { flags: params.flags }),
216
+ ...(params.requestId === undefined ? {} : { requestId: params.requestId })
217
+ })
218
+
219
+ const namePattern = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u
220
+ const packPattern = /^[\w.-]+(\/[\w.-]+)?$/u
221
+ const branchPattern = /^[A-Za-z0-9][A-Za-z0-9._/-]*$/u
222
+
223
+ const flagPattern = /^--[a-z][a-z0-9-]*(=[A-Za-z0-9][A-Za-z0-9._/-]*)?$/u
224
+ const flagValuePattern = /^[A-Za-z0-9][A-Za-z0-9._/-]*$/u
225
+ /**
226
+ * Flags a client may never pass through `flags`, with or without `=value`.
227
+ * `-C` already fails the shape pattern; it is named so a looser pattern
228
+ * cannot reopen it.
229
+ */
230
+ const reservedFlags: ReadonlyArray<string> = [
231
+ // Points the flow at another directory (`FlowArgs.ts`).
232
+ "--repo",
233
+ // Alias of --repo in the flow argument parser.
234
+ "-C",
235
+ // Reads the prompt from any file on the server.
236
+ "--prompt-file",
237
+ // Typed knob: epic.
238
+ "--epic",
239
+ // Typed knob: land.
240
+ "--land",
241
+ // Typed knob: judgeRounds.
242
+ "--judge-rounds",
243
+ // Typed knob: mergeRevisions.
244
+ "--merge-revisions",
245
+ // Lets soap flows send mutating calls to a real ESB.
246
+ "--allow-mutating",
247
+ // Auto-confirms prompts a human is meant to answer.
248
+ "--yes",
249
+ // Selects which environment's credentials a flow uses.
250
+ "--env",
251
+ // A filesystem path in epic-design.
252
+ "--target"
253
+ ]
254
+
255
+ const reservedFlagOf = (entry: string): string | undefined =>
256
+ reservedFlags.find((flag) => entry === flag || entry.startsWith(`${flag}=`))
257
+
258
+ /** Why a flags list is refused, or undefined when every entry has a safe shape. */
259
+ const flagsProblem = (flags: ReadonlyArray<string>): string | undefined => {
260
+ let previousWasFlag = false
261
+ for (const entry of flags) {
262
+ const reserved = reservedFlagOf(entry)
263
+ if (reserved !== undefined) return `${reserved} may not be passed through flags`
264
+ const flag = flagPattern.exec(entry)
265
+ if (flag !== null) {
266
+ previousWasFlag = flag[1] === undefined
267
+ continue
268
+ }
269
+ if (!previousWasFlag || !flagValuePattern.test(entry)) {
270
+ return "every entry must be a --flag[=value] or a plain value right after a flag"
271
+ }
272
+ previousWasFlag = false
273
+ }
274
+ return undefined
275
+ }
276
+
277
+ const invalid = (knob: string, message: string): Effect.Effect<never, InvalidKnob> =>
278
+ Effect.fail(InvalidKnob.make({ knob, message }))
279
+
280
+ /**
281
+ * Every client value that reaches the flow's argv or environment, checked
282
+ * before the supervisor sees it. Names stay names, never paths or flags:
283
+ * `pack` is a kit or kit/pack name (no `.`/`..` segment), `epic`, `coder` and
284
+ * executors are ids, `land` is a branch name. `task` is the flow's first
285
+ * positional argument, so it may not start with `-` (it would parse as a
286
+ * flag) or `@` (the flow parser reads `@<path>` as a prompt file on the
287
+ * server). `flags` is an allowlist of shapes: `--flag[=value]`, or a plain
288
+ * value right after a flag, never a reserved flag (`reservedFlags`).
289
+ */
290
+ export const validateKnobs = (knobs: OperatorKnobs): Effect.Effect<OperatorKnobs, InvalidKnob> => {
291
+ if (
292
+ knobs.pack !== undefined &&
293
+ (!packPattern.test(knobs.pack) ||
294
+ knobs.pack.split("/").some((segment) => segment === "." || segment === ".."))
295
+ ) {
296
+ return invalid("pack", "pack must be a kit or kit/pack name, not a path")
297
+ }
298
+ if (knobs.epic !== undefined && !namePattern.test(knobs.epic)) {
299
+ return invalid("epic", "epic must be an epic id")
300
+ }
301
+ if (knobs.coder !== undefined && !namePattern.test(knobs.coder)) {
302
+ return invalid("coder", "coder must be an executor or connector id")
303
+ }
304
+ if (knobs.executors?.some((id) => !namePattern.test(id)) === true) {
305
+ return invalid("executors", "every executor must be an executor id")
306
+ }
307
+ if (knobs.land !== undefined && !branchPattern.test(knobs.land)) {
308
+ return invalid("land", "land must be a branch name")
309
+ }
310
+ const nonNegative = (value: number | undefined) =>
311
+ value === undefined || (Number.isInteger(value) && value >= 0)
312
+ if (!nonNegative(knobs.judgeRounds)) {
313
+ return invalid("judgeRounds", "judgeRounds must be an integer of 0 or more")
314
+ }
315
+ if (!nonNegative(knobs.mergeRevisions)) {
316
+ return invalid("mergeRevisions", "mergeRevisions must be an integer of 0 or more")
317
+ }
318
+ if (knobs.task !== undefined && (knobs.task.startsWith("-") || knobs.task.startsWith("@"))) {
319
+ return invalid("task", "task must not start with '-' or '@'")
320
+ }
321
+ const problem = knobs.flags === undefined ? undefined : flagsProblem(knobs.flags)
322
+ if (problem !== undefined) return invalid("flags", problem)
323
+ return Effect.succeed(knobs)
324
+ }
325
+
326
+ /** A run or retro id names one file, never a path. */
327
+ const isPlainId = (id: string): boolean => /^[\w.-]+$/u.test(id) && id !== "." && id !== ".."
328
+
329
+ export const actHandlers = (
330
+ deps: OperatorDeps
331
+ ): Toolkit.HandlersFrom<Toolkit.Tools<typeof actToolkit>> => {
332
+ const requireRun = deps.config.allowRun
333
+ ? Effect.void
334
+ : Effect.fail(NotAllowed.make({ message: "this server was not started with --allow-run" }))
335
+ const requireApprove = deps.config.allowApprove
336
+ ? Effect.void
337
+ : Effect.fail(NotAllowed.make({ message: "this server was not started with --allow-approve" }))
338
+
339
+ const startFlow = (repo: Repo, name: string, knobs: OperatorKnobs) =>
340
+ Effect.gen(function* () {
341
+ const flows = yield* deps.flows(repo.path).pipe(Effect.mapError(readFailed))
342
+ const flow = flows.find((candidate) => candidate.name === name)
343
+ if (flow === undefined) {
344
+ return yield* UnknownFlow.make({
345
+ message: `unknown flow '${name}' in ${repo.id}: list_flows names the flows it can start`
346
+ })
347
+ }
348
+ const run = yield* deps.supervisor.start({
349
+ repo,
350
+ flow: { name: flow.name, path: flow.path },
351
+ knobs,
352
+ baseEnvironment: deps.environment
353
+ })
354
+ return {
355
+ runId: run.runId,
356
+ ...(run.tracePath === undefined ? {} : { tracePath: run.tracePath }),
357
+ ...(run.pid === undefined ? {} : { pid: run.pid })
358
+ }
359
+ })
360
+
361
+ return actToolkit.of({
362
+ start_run: (params) =>
363
+ Effect.gen(function* () {
364
+ yield* requireRun
365
+ const repo = yield* resolveRepo(deps.config, params.repo)
366
+ const knobs = yield* validateKnobs(knobsOf(params))
367
+ return yield* startFlow(repo, params.flow, knobs)
368
+ }),
369
+
370
+ stop_run: ({ runId }) => Effect.andThen(requireRun, deps.supervisor.stop(runId)),
371
+
372
+ retro_start: ({ repo: repoId, epic, run }) =>
373
+ Effect.gen(function* () {
374
+ yield* requireRun
375
+ const repo = yield* resolveRepo(deps.config, repoId)
376
+ const dir = yield* epicDirOf(deps, repo, epic)
377
+ if (run !== undefined) {
378
+ const recorded = yield* readEpicRunsStrict(deps.files, dir)
379
+ if (!recorded.some((entry) => entry.runId === run)) {
380
+ return yield* UnknownRun.make({
381
+ message: `epic '${epic}' has no recorded run '${run}'`
382
+ })
383
+ }
384
+ }
385
+ // The client's values, checked like start_run's; the --epic flag itself is ours.
386
+ yield* validateKnobs({ epic, ...(run === undefined ? {} : { flags: ["--run", run] }) })
387
+ return yield* startFlow(repo, "epic-retro", {
388
+ flags: ["--epic", epic, ...(run === undefined ? [] : ["--run", run])]
389
+ })
390
+ }),
391
+
392
+ approve: ({ repo: repoId, epic, id }) =>
393
+ Effect.gen(function* () {
394
+ yield* requireApprove
395
+ const repo = yield* resolveRepo(deps.config, repoId)
396
+ const dir = yield* epicDirOf(deps, repo, epic)
397
+ const unknown = UnknownRun.make({ message: `no retro '${id}' for epic '${epic}'` })
398
+ if (!isPlainId(id)) return yield* unknown
399
+ const path = retroPaths(dir, id).report
400
+ const markdown = yield* deps.files.read(path).pipe(Effect.mapError(readFailed))
401
+ if (markdown === undefined) return yield* unknown
402
+ const approved = approveMarkdown(markdown)
403
+ if (approved !== markdown) yield* deps.files.writeAtomic(path, approved)
404
+ return { path, approved: retroApprovalOf(approved).approved }
405
+ })
406
+ })
407
+ }
@@ -0,0 +1,148 @@
1
+ import { homedir } from "node:os"
2
+ import { basename, join, resolve } from "node:path"
3
+ import * as Effect from "effect/Effect"
4
+ import * as Option from "effect/Option"
5
+ import * as Schema from "effect/Schema"
6
+ import { ShellUsageError } from "./ShellErrors.ts"
7
+
8
+ export interface McpFlags {
9
+ readonly stdio: boolean
10
+ readonly http: boolean
11
+ readonly host: Option.Option<string>
12
+ readonly port: Option.Option<number>
13
+ readonly tokenFile: Option.Option<string>
14
+ readonly repo: ReadonlyArray<string>
15
+ readonly allowRun: boolean
16
+ readonly allowApprove: boolean
17
+ readonly parallelRuns: Option.Option<number>
18
+ }
19
+
20
+ export interface OperatorConfig {
21
+ readonly transport:
22
+ | { readonly _tag: "stdio" }
23
+ | {
24
+ readonly _tag: "http"
25
+ readonly host: string
26
+ readonly port: number
27
+ readonly token: string
28
+ }
29
+ readonly repos: ReadonlyArray<{ readonly id: string; readonly path: string }>
30
+ readonly allowRun: boolean
31
+ readonly allowApprove: boolean
32
+ readonly parallelRuns: number
33
+ }
34
+
35
+ export const McpConfigFile = Schema.Struct({
36
+ repos: Schema.Array(Schema.Struct({ id: Schema.String, path: Schema.String }))
37
+ })
38
+
39
+ /** The same name shape as an epic id: it names the repository's state directory. */
40
+ const repoIdPattern = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u
41
+
42
+ const usage = (message: string): ShellUsageError => new ShellUsageError({ message })
43
+
44
+ /**
45
+ * Turns the `mcp` flags into the operator configuration. The token is read from the
46
+ * environment or a file and is never echoed in an error message.
47
+ */
48
+ export const mcpOptionsFrom = (
49
+ flags: McpFlags,
50
+ environment: Readonly<Record<string, string | undefined>>,
51
+ cwd: string,
52
+ readFile: (path: string) => Effect.Effect<string | undefined>
53
+ ): Effect.Effect<OperatorConfig, ShellUsageError> =>
54
+ Effect.gen(function* () {
55
+ if (flags.stdio && flags.http) {
56
+ return yield* usage("mcp takes either --stdio or --http, not both")
57
+ }
58
+ const parallelRuns = Option.getOrElse(flags.parallelRuns, () => 1)
59
+ if (!Number.isInteger(parallelRuns) || parallelRuns < 1) {
60
+ return yield* usage("--parallel-runs must be a positive integer")
61
+ }
62
+
63
+ const transport = flags.http ? yield* httpTransport(flags, environment, cwd, readFile) : stdio
64
+
65
+ const repos = yield* reposFrom(flags, environment, cwd, readFile)
66
+ const seen = new Set<string>()
67
+ for (const repo of repos) {
68
+ if (seen.has(repo.id)) {
69
+ return yield* usage(`mcp repository id "${repo.id}" is used twice`)
70
+ }
71
+ seen.add(repo.id)
72
+ }
73
+
74
+ return {
75
+ transport,
76
+ repos,
77
+ allowRun: flags.allowRun,
78
+ allowApprove: flags.allowApprove,
79
+ parallelRuns
80
+ }
81
+ })
82
+
83
+ const stdio: OperatorConfig["transport"] = { _tag: "stdio" }
84
+
85
+ const httpTransport = (
86
+ flags: McpFlags,
87
+ environment: Readonly<Record<string, string | undefined>>,
88
+ cwd: string,
89
+ readFile: (path: string) => Effect.Effect<string | undefined>
90
+ ): Effect.Effect<OperatorConfig["transport"], ShellUsageError> =>
91
+ Effect.gen(function* () {
92
+ const fromEnvironment = environment.LLM4TS_MCP_TOKEN?.trim() ?? ""
93
+ const fromFile =
94
+ fromEnvironment.length === 0 && Option.isSome(flags.tokenFile)
95
+ ? ((yield* readFile(resolve(cwd, flags.tokenFile.value)))?.trim() ?? "")
96
+ : ""
97
+ const token = fromEnvironment.length > 0 ? fromEnvironment : fromFile
98
+ if (token.length === 0) {
99
+ return yield* usage("--http needs a token: LLM4TS_MCP_TOKEN or --token-file")
100
+ }
101
+ const port = Option.getOrElse(flags.port, () => 4321)
102
+ if (!Number.isInteger(port) || port < 0 || port > 65535) {
103
+ return yield* usage("--port must be between 0 and 65535")
104
+ }
105
+ return {
106
+ _tag: "http" as const,
107
+ host: Option.getOrElse(flags.host, () => "127.0.0.1"),
108
+ port,
109
+ token
110
+ }
111
+ })
112
+
113
+ const reposFrom = (
114
+ flags: McpFlags,
115
+ environment: Readonly<Record<string, string | undefined>>,
116
+ cwd: string,
117
+ readFile: (path: string) => Effect.Effect<string | undefined>
118
+ ): Effect.Effect<OperatorConfig["repos"], ShellUsageError> =>
119
+ Effect.gen(function* () {
120
+ if (flags.repo.length > 0) {
121
+ return flags.repo.map((path) => {
122
+ const absolute = resolve(cwd, path)
123
+ return { id: basename(absolute), path: absolute }
124
+ })
125
+ }
126
+ const configHome =
127
+ environment.XDG_CONFIG_HOME !== undefined && environment.XDG_CONFIG_HOME.length > 0
128
+ ? environment.XDG_CONFIG_HOME
129
+ : join(environment.HOME ?? homedir(), ".config")
130
+ const configPath = join(configHome, "llm4ts", "mcp.json")
131
+ const raw = yield* readFile(configPath)
132
+ if (raw === undefined) {
133
+ return yield* usage("mcp needs --repo <path> or ~/.config/llm4ts/mcp.json")
134
+ }
135
+ const file = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(McpConfigFile))(raw).pipe(
136
+ Effect.mapError(() => usage(`${configPath} is not a valid mcp config`))
137
+ )
138
+ if (file.repos.length === 0) {
139
+ return yield* usage("mcp needs --repo <path> or ~/.config/llm4ts/mcp.json")
140
+ }
141
+ const badId = file.repos.find(({ id }) => !repoIdPattern.test(id))
142
+ if (badId !== undefined) {
143
+ return yield* usage(
144
+ `${configPath}: repository id "${badId.id}" must match ${repoIdPattern.source}`
145
+ )
146
+ }
147
+ return file.repos.map(({ id, path }) => ({ id, path: resolve(cwd, path) }))
148
+ })
@@ -0,0 +1,147 @@
1
+ import { readFileSync, readdirSync } from "node:fs"
2
+ import { join } from "node:path"
3
+ import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
4
+ import type * as Cause from "effect/Cause"
5
+ import * as Clock from "effect/Clock"
6
+ import * as Effect from "effect/Effect"
7
+ import * as Fiber from "effect/Fiber"
8
+ import type { ServeError } from "effect/http/HttpServerError"
9
+ import type { PersistenceError } from "@llm4ts/flow/FlowError"
10
+ import { nodeListTraces } from "@llm4ts/runner/Costs"
11
+ import { collectDoctorReport } from "@llm4ts/runner/Doctor"
12
+ import { httpProbe, rosterReport } from "@llm4ts/runner/ExecutorRoster"
13
+ import { interruptOnSignal, type SignalSource } from "@llm4ts/runner/FlowRunner"
14
+ import { nodePlainFileStore } from "@llm4ts/runner/NodePlainFileStore"
15
+ import { nodeTranscriptFiles } from "@llm4ts/runner/Transcripts"
16
+ import { nodeListEpics } from "@llm4ts/runner/Watch"
17
+ import { discoverFlows, type FlowTierPaths } from "./FlowCatalog.ts"
18
+ import { nodeFlowProcess, type FlowProcessShape } from "./FlowProcess.ts"
19
+ import type { OperatorConfig } from "./OperatorConfig.ts"
20
+ import { runOperatorServer } from "./OperatorServer.ts"
21
+ import type { OperatorDeps } from "./OperatorTools.ts"
22
+ import { makeRunSupervisor } from "./RunSupervisor.ts"
23
+
24
+ /**
25
+ * The Node side of `llm4ts mcp` (ADR 0041): the operator's dependencies over
26
+ * the real file system and processes, and `runMcp`, which serves until the
27
+ * server fails or a SIGINT/SIGTERM interrupts it, then stops the flows it
28
+ * started before returning.
29
+ */
30
+
31
+ /** Everything `runMcp` needs besides the run supervisor, which it builds itself. */
32
+ export interface McpRuntime<E> {
33
+ readonly operator: Omit<OperatorDeps, "supervisor">
34
+ readonly flowProcess: FlowProcessShape
35
+ /** The server; `runOperatorServer` on Node, a stand-in in tests. */
36
+ readonly serve: (config: OperatorConfig, deps: OperatorDeps) => Effect.Effect<never, E>
37
+ /** How long stopping a run waits after SIGINT before SIGTERM; the supervisor's default when absent. */
38
+ readonly graceMs?: number
39
+ }
40
+
41
+ /**
42
+ * Serves the operator until the server ends or a signal arrives. The first
43
+ * SIGINT or SIGTERM interrupts the server; closing the scope then runs the
44
+ * supervisor's finalizer, which stops every flow it started. A second signal
45
+ * exits at once (130 or 143). The handlers are removed on the way out.
46
+ */
47
+ export const runMcp = <E>(
48
+ config: OperatorConfig,
49
+ runtime: McpRuntime<E>,
50
+ signals: SignalSource
51
+ ): Effect.Effect<void, E | PersistenceError> => {
52
+ const disposers: Array<() => void> = []
53
+ return Effect.scoped(
54
+ Effect.gen(function* () {
55
+ const supervisor = yield* makeRunSupervisor({
56
+ repos: config.repos,
57
+ environment: runtime.operator.environment,
58
+ files: runtime.operator.files,
59
+ process: runtime.flowProcess,
60
+ parallelRuns: config.parallelRuns,
61
+ ...(runtime.graceMs === undefined ? {} : { graceMs: runtime.graceMs })
62
+ })
63
+ const fiber = yield* Effect.forkScoped(
64
+ runtime.serve(config, { ...runtime.operator, supervisor })
65
+ )
66
+ disposers.push(interruptOnSignal(fiber, signals, "SIGINT"))
67
+ disposers.push(interruptOnSignal(fiber, signals, "SIGTERM"))
68
+ return yield* Fiber.join(fiber)
69
+ })
70
+ ).pipe(
71
+ Effect.ensuring(
72
+ Effect.sync(() => {
73
+ for (const dispose of disposers) dispose()
74
+ })
75
+ )
76
+ )
77
+ }
78
+
79
+ /** A file's text, or `undefined` when it cannot be read (the `mcp` config and token file). */
80
+ export const nodeRead = (path: string): Effect.Effect<string | undefined> =>
81
+ Effect.sync(() => {
82
+ try {
83
+ return readFileSync(path, "utf8")
84
+ } catch {
85
+ return undefined
86
+ }
87
+ })
88
+
89
+ /**
90
+ * Relative paths of every regular file under a directory, recursively; never
91
+ * directory names, and none when the directory is absent or unreadable.
92
+ */
93
+ export const nodeListFiles = (directory: string): ReadonlyArray<string> => {
94
+ const found: Array<string> = []
95
+ const walk = (relative: string): void => {
96
+ let names: ReadonlyArray<{
97
+ readonly name: string
98
+ isDirectory(): boolean
99
+ isFile(): boolean
100
+ }>
101
+ try {
102
+ names = readdirSync(join(directory, relative), { withFileTypes: true })
103
+ } catch {
104
+ return
105
+ }
106
+ for (const entry of names) {
107
+ const path = relative.length === 0 ? entry.name : join(relative, entry.name)
108
+ if (entry.isDirectory()) {
109
+ walk(path)
110
+ } else if (entry.isFile()) {
111
+ found.push(path)
112
+ }
113
+ }
114
+ }
115
+ walk("")
116
+ return found.sort()
117
+ }
118
+
119
+ /**
120
+ * The operator's dependencies on Node. `tierPaths` gives the flow tiers for a
121
+ * repository (the CLI's `shellTierPaths`, passed in to keep this module free
122
+ * of the CLI).
123
+ */
124
+ export const nodeOperatorRuntime = (
125
+ config: OperatorConfig,
126
+ tierPaths: (repoPath: string) => FlowTierPaths,
127
+ environment: Readonly<Record<string, string | undefined>> = process.env
128
+ ): McpRuntime<ServeError | Cause.IllegalArgumentError> => ({
129
+ operator: {
130
+ config,
131
+ files: nodePlainFileStore,
132
+ listTraces: nodeListTraces,
133
+ listEpics: nodeListEpics,
134
+ listFiles: nodeListFiles,
135
+ transcripts: nodeTranscriptFiles,
136
+ doctor: collectDoctorReport(),
137
+ roster: (repoPath) =>
138
+ rosterReport({ files: nodePlainFileStore, environment, workDir: repoPath }),
139
+ probe: httpProbe,
140
+ flows: (repoPath) =>
141
+ discoverFlows(tierPaths(repoPath)).pipe(Effect.provide(NodeFileSystem.layer)),
142
+ now: Clock.currentTimeMillis,
143
+ environment
144
+ },
145
+ flowProcess: nodeFlowProcess,
146
+ serve: runOperatorServer
147
+ })