@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.16

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 (110) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/cli.mjs +314 -4
  4. package/src/doctor.mjs +53 -1
  5. package/src/exec.mjs +14 -5
  6. package/src/factory/budget/budget.mjs +259 -0
  7. package/src/factory/build/command.mjs +321 -0
  8. package/src/factory/build/factory.mjs +473 -0
  9. package/src/factory/build/findings.mjs +55 -0
  10. package/src/factory/build/host.mjs +230 -0
  11. package/src/factory/build/notes.mjs +191 -0
  12. package/src/factory/build/pull-request.mjs +74 -0
  13. package/src/factory/bundle.mjs +146 -0
  14. package/src/factory/checks/allowed-paths.mjs +50 -0
  15. package/src/factory/checks/plan-header.mjs +296 -0
  16. package/src/factory/checks/qa.mjs +85 -0
  17. package/src/factory/checks/review.mjs +73 -0
  18. package/src/factory/checks/security.mjs +50 -0
  19. package/src/factory/checks/stage-check.mjs +89 -0
  20. package/src/factory/claims/command.mjs +187 -0
  21. package/src/factory/claims/release.mjs +111 -0
  22. package/src/factory/decision/answers.mjs +103 -0
  23. package/src/factory/decision/command.mjs +64 -0
  24. package/src/factory/decision/plan.mjs +99 -0
  25. package/src/factory/decision/request.mjs +116 -0
  26. package/src/factory/doctor/files.mjs +329 -0
  27. package/src/factory/doctor/index.mjs +56 -0
  28. package/src/factory/doctor/local.mjs +497 -0
  29. package/src/factory/doctor/orchestrate.mjs +146 -0
  30. package/src/factory/engine/lease.mjs +146 -0
  31. package/src/factory/engine/local-engine.mjs +177 -0
  32. package/src/factory/engine/lockfile.mjs +64 -0
  33. package/src/factory/engine/workflow-engine.mjs +124 -0
  34. package/src/factory/gate/comment.mjs +88 -0
  35. package/src/factory/gate/config.mjs +57 -0
  36. package/src/factory/gate/gate-core.mjs +250 -0
  37. package/src/factory/gate/guarded.mjs +17 -0
  38. package/src/factory/gate/reads.mjs +105 -0
  39. package/src/factory/gate/task.mjs +193 -0
  40. package/src/factory/gate/trip-reads.mjs +82 -0
  41. package/src/factory/gate/trip.mjs +87 -0
  42. package/src/factory/init/blueprint.mjs +174 -0
  43. package/src/factory/init/index.mjs +337 -0
  44. package/src/factory/labels/evidence.mjs +162 -0
  45. package/src/factory/labels/moves.mjs +32 -0
  46. package/src/factory/labels/reconcile.mjs +267 -0
  47. package/src/factory/labels/revert.mjs +87 -0
  48. package/src/factory/labels/timeline.mjs +72 -0
  49. package/src/factory/labels/voided.mjs +59 -0
  50. package/src/factory/limits/harness.mjs +39 -0
  51. package/src/factory/limits/limits.mjs +123 -0
  52. package/src/factory/merge/command.mjs +172 -0
  53. package/src/factory/outbox/check.mjs +240 -0
  54. package/src/factory/outbox/rulings.mjs +195 -0
  55. package/src/factory/plan/command.mjs +66 -0
  56. package/src/factory/plan/comment.mjs +156 -0
  57. package/src/factory/plan/publish.mjs +149 -0
  58. package/src/factory/plan/risk.mjs +153 -0
  59. package/src/factory/qa/command.mjs +163 -0
  60. package/src/factory/records/engine-record.mjs +133 -0
  61. package/src/factory/records/main.mjs +96 -0
  62. package/src/factory/records/record.mjs +226 -0
  63. package/src/factory/records/sink.mjs +57 -0
  64. package/src/factory/report/build.mjs +92 -0
  65. package/src/factory/report/command.mjs +80 -0
  66. package/src/factory/report/facts.mjs +226 -0
  67. package/src/factory/report/gate-section.mjs +122 -0
  68. package/src/factory/report/metrics.mjs +111 -0
  69. package/src/factory/report/reads.mjs +246 -0
  70. package/src/factory/report/render.mjs +121 -0
  71. package/src/factory/report/rollout.mjs +154 -0
  72. package/src/factory/review/command.mjs +182 -0
  73. package/src/factory/run/awake.mjs +70 -0
  74. package/src/factory/run/collect.mjs +77 -0
  75. package/src/factory/run/command.mjs +490 -0
  76. package/src/factory/run/gh.mjs +46 -0
  77. package/src/factory/run/launch.mjs +47 -0
  78. package/src/factory/run/pipelines.mjs +261 -0
  79. package/src/factory/run/raw.mjs +96 -0
  80. package/src/factory/run/restart.mjs +83 -0
  81. package/src/factory/run/s3.mjs +193 -0
  82. package/src/factory/run/secrets.mjs +142 -0
  83. package/src/factory/run/teardown.mjs +242 -0
  84. package/src/factory/run/token.mjs +68 -0
  85. package/src/factory/security/command.mjs +157 -0
  86. package/src/factory/setup/app.mjs +122 -0
  87. package/src/factory/setup/command.mjs +409 -0
  88. package/src/factory/setup/labels.mjs +25 -0
  89. package/src/factory/setup/reads.mjs +129 -0
  90. package/src/factory/setup/writes.mjs +126 -0
  91. package/src/factory/snapshot/write.mjs +176 -0
  92. package/src/factory/stop/command.mjs +136 -0
  93. package/src/factory/sweep/actions-main.mjs +117 -0
  94. package/src/factory/sweep/command.mjs +65 -0
  95. package/src/factory/sweep/knobs.mjs +78 -0
  96. package/src/factory/sweep/marker-version.mjs +15 -0
  97. package/src/factory/sweep/markers.mjs +112 -0
  98. package/src/factory/sweep/overlap.mjs +24 -0
  99. package/src/factory/sweep/run.mjs +198 -0
  100. package/src/factory/sweep/shell.mjs +394 -0
  101. package/src/factory/sweep/split.mjs +104 -0
  102. package/src/factory/sweep/states.mjs +87 -0
  103. package/src/factory/sweep/sweep-core.mjs +1781 -0
  104. package/src/factory/tracker/github.mjs +674 -0
  105. package/src/factory/tracker/operations.mjs +153 -0
  106. package/src/factory/upgrade/index.mjs +83 -0
  107. package/src/init.mjs +62 -24
  108. package/src/kit-owned-paths.mjs +1 -1
  109. package/src/stage.mjs +19 -0
  110. package/src/upgrade.mjs +39 -7
package/README.md CHANGED
@@ -41,6 +41,6 @@ npx @mnaimfaizy/agent-kit harness test custom:<name>
41
41
  - `harness test custom:<name>` runs the live probe suite on a [Custom harness](../docs/harnesses.md#custom-harness) file, `.agent-kit/harnesses/<name>.json`, as the default branch (or `--trusted-ref`) holds it, against a stub model under gVisor, with no model credential. It is the first half of a Proving run. `deep-scan` and `stage` take the same `custom:<name>`, reading the file from the same trusted commit, and `doctor` checks it.
42
42
  - `deep-scan --verify <file> --ref <ref>` is a [Verification run](../docs/security-audit.md#verification-run): it re-attempts the Reproduction of the findings in `<file>` (each a `###` section with `- **Concept id:**`, `- **Location:**` and `- **Observation:**`, as a deep report gives them) on `<ref>`, a local branch, tag or commit such as your fix branch, exported as committed, never the working tree. The recipe, the skill and the Threat pack still come from the default branch. It is confirm-only (no `full` walk, no exploration, no new findings): the agent gets the Verification-run procedure, not the full deep procedure, and the finding file goes in with its brief after the network cut. Its defaults are scan 15 min, 50 turns and the same token cap. Each flag needs the other, and `--report` is not taken with them (usage errors, exit 2). A finding file that is missing, unreadable or out of shape, or a ref that is not a local commit, is refused, exit 1, before any runtime starts. The report goes only to a new private folder; `deep-scan` prints each finding's `Reproduced: yes` (the fix does not hold) or `no` (it holds), exits 1 if the report misses one, and files or posts nothing. It is local only: refused, exit 1, in GitHub Actions (`GITHUB_ACTIONS=true`).
43
43
 
44
- The package version matches the Agent-kit Release line: `@mnaimfaizy/agent-kit@1.0.0-beta.15` is published from tag `v1.0.0-beta.15`, with npm provenance.
44
+ The package version matches the Agent-kit Release line: `@mnaimfaizy/agent-kit@1.0.0-beta.16` is published from tag `v1.0.0-beta.16`, with npm provenance.
45
45
 
46
46
  Full documentation: [Getting started](https://github.com/mnaimfaizy/agent-kit/blob/main/docs/getting-started.md) and [Adopt](https://github.com/mnaimfaizy/agent-kit/blob/main/docs/adopt.md#check-an-adoption-with-doctor).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mnaimfaizy/agent-kit",
3
- "version": "1.0.0-beta.15",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "Adopt, upgrade and check Agent-kit in a Consumer repository. Reads kit files from the pinned git tag.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/cli.mjs CHANGED
@@ -6,8 +6,16 @@
6
6
  // tearing the sandbox down), 130 or 143 one was interrupted by SIGINT or
7
7
  // SIGTERM (likewise).
8
8
 
9
+ import { readFileSync } from "node:fs";
10
+ import { homedir, hostname } from "node:os";
9
11
  import { parseArgs } from "node:util";
10
12
  import { runDeepScan } from "./deep-scan.mjs";
13
+ import { runFactoryReport } from "./factory/report/command.mjs";
14
+ import { parseDuration, runFactoryRun } from "./factory/run/command.mjs";
15
+ import { runFactorySetup } from "./factory/setup/command.mjs";
16
+ import { readLock, stateDirOf } from "./factory/engine/lockfile.mjs";
17
+ import { runFactoryStop } from "./factory/stop/command.mjs";
18
+ import { runFactorySweep } from "./factory/sweep/command.mjs";
11
19
  import { formatReport, runDoctor } from "./doctor.mjs";
12
20
  import { runHarnessTest } from "./harness-test.mjs";
13
21
  import { REPO_PATTERN, sanitizeLine } from "./checks/result.mjs";
@@ -54,6 +62,30 @@ Commands:
54
62
  checked-out commit in, hands the harness the brief it is
55
63
  given, and writes the Stage result and the raw transcript to
56
64
  private files. Writes nothing to GitHub.
65
+ factory factory setup: the GitHub side of adopting the Factory (after
66
+ init --factory): labels, the pinned control issue, the App and
67
+ its key, the ruleset. Asks before each write.
68
+ factory sweep: one sweep of this repository's factory issues,
69
+ the same sweep the factory-dispatcher workflow runs. With the
70
+ Kill switch AGENT_KIT_FACTORY_ENABLED exactly true, it claims each
71
+ queued, ready-for-agent issue (a Claim comment, read back), sets
72
+ factory:planning and starts that Stage's Caller; a queued issue
73
+ without ready-for-agent goes to factory:needs-decision. Otherwise
74
+ it changes nothing. The Kill switch is read from the environment,
75
+ else from the repository's variables.
76
+ factory report: how the Factory is doing, from the tracker alone
77
+ and your own gh login: merge rate, first-pass success, lead time,
78
+ escalation and revert rates and tokens per merged pull request,
79
+ by Risk tier; the Rollout phase your config matches and whether
80
+ the next bar is met. Reads only; posts nothing.
81
+ factory stop: stops the Factory at once, as gh run cancel would:
82
+ cancels every queued or running run of the Dispatcher and the
83
+ Stage Callers. Releases no Claim; never sets the Kill switch.
84
+ On a machine running factory run it also asks that engine to drain.
85
+ factory run: the Orchestrate engine (preview): the same sweep, run
86
+ from this machine in a loop, with Stages in Launcher sandboxes.
87
+ Needs AGENT_KIT_GITHUB_TOKEN (a fine-grained token for this one
88
+ repository, or an op:// reference) and a model key.
57
89
  harness harness test custom:<name>: run the live probe suite on your
58
90
  own Custom harness file, against a stub model, in the real
59
91
  sandbox under gVisor. The first half of a Proving run. It needs
@@ -65,6 +97,21 @@ Options for doctor:
65
97
  --strict Treat warnings as failures.
66
98
  --repo <o/r> The GitHub repository to check (default: gh's choice for
67
99
  the current directory).
100
+ On the Orchestrate engine doctor also checks the PAT's reach and reads, through
101
+ AGENT_KIT_GITHUB_TOKEN where it holds a literal (not an op:// reference).
102
+
103
+ Options for init --factory (adopt the Factory blueprint; local files only; each answer is asked
104
+ for when missing, on a terminal; --tag and --yes and --force work as below):
105
+ --factory Write the Factory blueprint instead of the flows: the
106
+ Callers, the factory config, the risk rules, CODEOWNERS
107
+ lines, a ruleset JSON and the Kit-owned bundle.
108
+ --engine <e> workflow (default) or orchestrate (no Callers, no bundle,
109
+ nothing for the App; the steps printed name the PAT and
110
+ the host recipe, docs/factory/host.md).
111
+ --codeowners <owners> Who approves changes to the Guarded paths (@login or
112
+ @org/team, comma-separated).
113
+ --ci-checks <names> Your CI's check names, which the ruleset requires
114
+ (comma-separated; never a factory/* context).
68
115
 
69
116
  Options for init (each one is asked for when missing, on a terminal):
70
117
  --tag <vX> The Agent-kit tag to adopt.
@@ -189,6 +236,11 @@ min, scan 30 min, 100 turns, 5000000 tokens):
189
236
  --transcript <file> Write the harness's raw event stream here
190
237
  (default: a new private folder). Never read.
191
238
  --error-log <file> Write the harness's error output here.
239
+ --usage <file> Write the run's usage here, as JSON with two keys:
240
+ tokens (the model proxy's count) and stopped
241
+ (turns, tokens, minutes or null). Nothing the
242
+ agent wrote. Absent on a harness the proxy cannot
243
+ front.
192
244
  --trusted-ref <ref> The commit the harness's instruction files come
193
245
  from (default: the checked-out one). A Custom
194
246
  harness's file is read from it too (without it,
@@ -254,6 +306,50 @@ Options for harness test:
254
306
  Exit codes: 0 every Guard held in every scenario, 1 refused or failed, 2
255
307
  usage error.
256
308
 
309
+ Options for factory setup (needs a terminal and an admin gh login; refuses in CI;
310
+ asks before each write; safe to run again; never sets the Kill switch):
311
+ --repo <o/r> The repository to set up (default: gh's choice for the
312
+ current directory).
313
+ --engine <e> workflow (default: labels, the pinned control issue, the
314
+ GitHub App and its key, the ruleset) or orchestrate (labels,
315
+ control issue and ruleset; no App; prints the PAT's steps:
316
+ one repository, Contents, Issues, Pull requests and Statuses
317
+ write, Variables read).
318
+
319
+ Options for factory sweep:
320
+ --repo <o/r> The repository to sweep (default: GITHUB_REPOSITORY, else
321
+ gh's choice for the current directory).
322
+ --dry-run Read the tracker and print the actions, write nothing.
323
+ Exit codes: 0 swept or drained, 1 an action failed, 2 usage error.
324
+
325
+ Options for factory run (the Orchestrate engine, preview; one engine per repository):
326
+ --repo <o/r> The repository to run (default: GITHUB_REPOSITORY, else gh's
327
+ choice for the current directory).
328
+ --once Run one sweep, wait for the Stages it started, and exit.
329
+ --drain <dur> How long a first Ctrl-C lets running Stages finish before it
330
+ cancels them (default 10m; 90s, 10m, 2h). A second Ctrl-C
331
+ stops at once.
332
+ --poll <dur> The time between sweeps (default 60s, at least 30s).
333
+ --upload-raw Also upload each run folder to the S3 raw-folder Sink, once its
334
+ bucket's lifecycle expires raw/ (never the security Stage).
335
+ Without it nothing raw leaves the machine.
336
+ Exit codes: 0 drained or swept, 1 refused (token, lease, lock) or the lease lost
337
+ during the run, 2 usage error or a missing secret, 130 stopped at once.
338
+
339
+ Options for factory stop (cancels workflow runs only; needs a gh login with
340
+ actions write):
341
+ --repo <o/r> The repository to stop (default: GITHUB_REPOSITORY, else
342
+ gh's choice for the current directory).
343
+ Exit codes: 0 stopped (or nothing running), 1 a cancel failed, 2 usage error.
344
+
345
+ Options for factory report (reads only; never posts):
346
+ --repo <o/r> The repository to report on (default: GITHUB_REPOSITORY, else
347
+ gh's choice for the current directory).
348
+ --since <Nd> The window of tasks that reached a terminal state (default 30d).
349
+ --json Print the stable factory-report/1 JSON instead of text.
350
+ --gate Add the shadow-to-on bar per tier, from the merge gate's comments.
351
+ Exit codes: 0 reported, 1 a read failed, 2 usage error.
352
+
257
353
  Options for upgrade:
258
354
  --tag <vY> The Agent-kit tag to move to (asked for when missing).
259
355
  --from <vX> The tag you are leaving, when the pins already name <vY>
@@ -268,6 +364,18 @@ Options for upgrade:
268
364
 
269
365
  const COMMAND_OPTIONS = {
270
366
  doctor: ["ci", "strict", "repo"],
367
+ factory: [
368
+ "repo",
369
+ "dry-run",
370
+ "engine",
371
+ "since",
372
+ "json",
373
+ "gate",
374
+ "once",
375
+ "drain",
376
+ "poll",
377
+ "upload-raw",
378
+ ],
271
379
  harness: ["trusted-ref", "default-branch"],
272
380
  init: [
273
381
  "tag",
@@ -280,6 +388,10 @@ const COMMAND_OPTIONS = {
280
388
  "threat-pack-dir",
281
389
  "yes",
282
390
  "force",
391
+ "factory",
392
+ "engine",
393
+ "codeowners",
394
+ "ci-checks",
283
395
  ],
284
396
  upgrade: ["tag", "from", "yes", "force"],
285
397
  "deep-scan": [
@@ -315,6 +427,7 @@ const COMMAND_OPTIONS = {
315
427
  "bundle",
316
428
  "transcript",
317
429
  "error-log",
430
+ "usage",
318
431
  "trusted-ref",
319
432
  "untracked",
320
433
  "input",
@@ -369,6 +482,18 @@ export async function main(argv, io) {
369
482
  ci: { type: "boolean" },
370
483
  strict: { type: "boolean" },
371
484
  repo: { type: "string" },
485
+ "dry-run": { type: "boolean" },
486
+ since: { type: "string" },
487
+ json: { type: "boolean" },
488
+ gate: { type: "boolean" },
489
+ once: { type: "boolean" },
490
+ drain: { type: "string" },
491
+ poll: { type: "string" },
492
+ "upload-raw": { type: "boolean" },
493
+ factory: { type: "boolean" },
494
+ engine: { type: "string" },
495
+ codeowners: { type: "string" },
496
+ "ci-checks": { type: "string" },
372
497
  tag: { type: "string" },
373
498
  from: { type: "string" },
374
499
  flows: { type: "string" },
@@ -395,6 +520,7 @@ export async function main(argv, io) {
395
520
  bundle: { type: "string" },
396
521
  transcript: { type: "string" },
397
522
  "error-log": { type: "string" },
523
+ usage: { type: "string" },
398
524
  "trusted-ref": { type: "string" },
399
525
  untracked: { type: "boolean" },
400
526
  input: { type: "string", multiple: true },
@@ -438,6 +564,27 @@ export async function main(argv, io) {
438
564
  stderr(`stage takes one Stage, got: ${rest.join(" ")}`);
439
565
  return 2;
440
566
  }
567
+ } else if (command === "factory") {
568
+ if (rest.length !== 1 || !["sweep", "setup", "report", "stop", "run"].includes(rest[0])) {
569
+ stderr(
570
+ "factory takes `sweep`, `setup`, `report`, `stop` or `run`, as in: agent-kit factory setup",
571
+ );
572
+ return 2;
573
+ }
574
+ const runOnly =
575
+ rest[0] === "run"
576
+ ? []
577
+ : ["once", "drain", "poll", "upload-raw"].filter((k) => values[k] !== undefined);
578
+ if (runOnly.length) {
579
+ stderr(`factory ${rest[0]} does not take ${runOnly.map((k) => `--${k}`).join(", ")}`);
580
+ return 2;
581
+ }
582
+ const own =
583
+ rest[0] === "report" ? [] : ["since", "json", "gate"].filter((k) => values[k] !== undefined);
584
+ if (own.length) {
585
+ stderr(`factory ${rest[0]} does not take ${own.map((k) => `--${k}`).join(", ")}`);
586
+ return 2;
587
+ }
441
588
  } else if (command === "harness") {
442
589
  if (rest.length !== 2 || rest[0] !== "test") {
443
590
  stderr("harness takes `test custom:<name>`, as in: agent-kit harness test custom:mine");
@@ -458,14 +605,46 @@ export async function main(argv, io) {
458
605
  io.runner ??
459
606
  // `stage` and `harness test` run in the Deep scan's sandbox, on the same
460
607
  // commands.
461
- runnerFor(command === "stage" || command === "harness" ? "deep-scan" : command, {
462
- ...(io.spawn ? { spawn: io.spawn } : {}),
463
- ...(io.spawnAsync ? { spawnAsync: io.spawnAsync } : {}),
464
- });
608
+ // `factory sweep` reaches only `git` and `gh`, on `doctor`'s allowlist.
609
+ runnerFor(
610
+ command === "stage" || command === "harness"
611
+ ? "deep-scan"
612
+ : command === "factory"
613
+ ? rest[0] === "run"
614
+ ? "factory-run"
615
+ : "doctor"
616
+ : command,
617
+ {
618
+ ...(io.spawn ? { spawn: io.spawn } : {}),
619
+ ...(io.spawnAsync ? { spawnAsync: io.spawnAsync } : {}),
620
+ },
621
+ );
465
622
 
466
623
  if (command === "init") {
467
624
  stdout(`agent-kit init ${VERSION}`);
625
+ const factoryOnly = ["engine", "codeowners", "ci-checks"].filter(
626
+ (k) => values[k] !== undefined,
627
+ );
628
+ if (!values.factory && factoryOnly.length) {
629
+ stderr(`${factoryOnly.map((k) => `--${k}`).join(", ")} only go with init --factory`);
630
+ return 2;
631
+ }
632
+ const flowFlags = ["flows", "actors", "verify", "preset", "harness", "threat-pack-dir"].filter(
633
+ (k) => values[k] !== undefined,
634
+ );
635
+ if (values.factory && flowFlags.length) {
636
+ stderr(`init --factory does not take ${flowFlags.map((k) => `--${k}`).join(", ")}`);
637
+ return 2;
638
+ }
468
639
  return runInit({
640
+ ...(values.factory
641
+ ? {
642
+ factory: {
643
+ engine: values.engine ?? "workflow",
644
+ flags: { codeowners: values.codeowners, ciChecks: values["ci-checks"] },
645
+ },
646
+ }
647
+ : {}),
469
648
  cwd,
470
649
  runner,
471
650
  flags: {
@@ -527,6 +706,7 @@ export async function main(argv, io) {
527
706
  bundlePath: values.bundle,
528
707
  transcriptPath: values.transcript,
529
708
  errorLogPath: values["error-log"],
709
+ usagePath: values.usage,
530
710
  trustedRef: values["trusted-ref"],
531
711
  untracked: Boolean(values.untracked),
532
712
  inputPaths: values.input,
@@ -556,6 +736,136 @@ export async function main(argv, io) {
556
736
  err: stderr,
557
737
  });
558
738
  }
739
+ if (command === "factory" && rest[0] === "run") {
740
+ const unfit = ["dry-run", "engine", "since", "json", "gate"].filter(
741
+ (k) => values[k] !== undefined,
742
+ );
743
+ if (unfit.length) {
744
+ stderr(`factory run does not take ${unfit.map((k) => `--${k}`).join(", ")}`);
745
+ return 2;
746
+ }
747
+ if (values.repo !== undefined && !REPO_PATTERN.test(values.repo)) {
748
+ stderr(`--repo must be owner/name, got: ${JSON.stringify(values.repo)}`);
749
+ return 2;
750
+ }
751
+ const drainMs = values.drain === undefined ? undefined : parseDuration(values.drain);
752
+ const pollMs = values.poll === undefined ? undefined : parseDuration(values.poll);
753
+ if (drainMs === null || pollMs === null) {
754
+ stderr(`${drainMs === null ? "--drain" : "--poll"} is a duration such as 90s, 10m or 2h`);
755
+ return 2;
756
+ }
757
+ stdout(`agent-kit factory run ${VERSION}`);
758
+ return runFactoryRun({
759
+ runner,
760
+ env: io.env ?? process.env,
761
+ repo: values.repo,
762
+ once: Boolean(values.once),
763
+ uploadRaw: Boolean(values["upload-raw"]),
764
+ cwd,
765
+ ...(drainMs === undefined ? {} : { drainMs }),
766
+ ...(pollMs === undefined ? {} : { pollMs }),
767
+ ...(io.clock ? { clock: io.clock } : {}),
768
+ ...(io.nonce ? { nonce: io.nonce } : {}),
769
+ ...(io.signals ? { signals: io.signals } : {}),
770
+ ...(io.platform ? { platform: io.platform } : {}),
771
+ ...(io.engineHost ?? {}),
772
+ out: stdout,
773
+ err: stderr,
774
+ });
775
+ }
776
+ if (command === "factory" && rest[0] === "report") {
777
+ const unfit = ["dry-run", "engine"].filter((k) => values[k] !== undefined);
778
+ if (unfit.length) {
779
+ stderr(`factory report does not take ${unfit.map((k) => `--${k}`).join(", ")}`);
780
+ return 2;
781
+ }
782
+ if (values.repo !== undefined && !REPO_PATTERN.test(values.repo)) {
783
+ stderr(`--repo must be owner/name, got: ${JSON.stringify(values.repo)}`);
784
+ return 2;
785
+ }
786
+ return runFactoryReport({
787
+ runner,
788
+ env: io.env ?? process.env,
789
+ repo: values.repo,
790
+ since: values.since,
791
+ json: Boolean(values.json),
792
+ gate: Boolean(values.gate),
793
+ ...(io.clock ? { clock: io.clock } : {}),
794
+ out: stdout,
795
+ err: stderr,
796
+ });
797
+ }
798
+ if (command === "factory" && rest[0] === "stop") {
799
+ const unfit = ["dry-run", "engine"].filter((k) => values[k] !== undefined);
800
+ if (unfit.length) {
801
+ stderr(`factory stop does not take ${unfit.map((k) => `--${k}`).join(", ")}`);
802
+ return 2;
803
+ }
804
+ if (values.repo !== undefined && !REPO_PATTERN.test(values.repo)) {
805
+ stderr(`--repo must be owner/name, got: ${JSON.stringify(values.repo)}`);
806
+ return 2;
807
+ }
808
+ stdout(`agent-kit factory stop ${VERSION}`);
809
+ return runFactoryStop({
810
+ gh: (args) => runner("gh", args),
811
+ env: io.env ?? process.env,
812
+ repo: values.repo,
813
+ local: io.localEngine ?? {
814
+ holder: (slug) =>
815
+ readLock({
816
+ dir: stateDirOf(homedir(), slug),
817
+ fs: { readFileSync },
818
+ }),
819
+ host: hostname(),
820
+ kill: (pid) => process.kill(pid, "SIGTERM"),
821
+ },
822
+ out: stdout,
823
+ err: stderr,
824
+ });
825
+ }
826
+ if (command === "factory" && rest[0] === "setup") {
827
+ stdout(`agent-kit factory setup ${VERSION}`);
828
+ if (values["dry-run"]) {
829
+ stderr("factory setup does not take --dry-run");
830
+ return 2;
831
+ }
832
+ if (values.repo !== undefined && !REPO_PATTERN.test(values.repo)) {
833
+ stderr(`--repo must be owner/name, got: ${JSON.stringify(values.repo)}`);
834
+ return 2;
835
+ }
836
+ return runFactorySetup({
837
+ runner,
838
+ env: io.env ?? process.env,
839
+ repo: values.repo,
840
+ engine: values.engine ?? "workflow",
841
+ cwd,
842
+ prompt,
843
+ ...(io.appFlow ? { appFlow: io.appFlow } : {}),
844
+ out: stdout,
845
+ err: stderr,
846
+ });
847
+ }
848
+ if (command === "factory") {
849
+ if (values.engine !== undefined) {
850
+ stderr("factory sweep does not take --engine");
851
+ return 2;
852
+ }
853
+ stdout(`agent-kit factory sweep ${VERSION}`);
854
+ if (values.repo !== undefined && !REPO_PATTERN.test(values.repo)) {
855
+ stderr(`--repo must be owner/name, got: ${JSON.stringify(values.repo)}`);
856
+ return 2;
857
+ }
858
+ return runFactorySweep({
859
+ runner,
860
+ env: io.env ?? process.env,
861
+ repo: values.repo,
862
+ dryRun: Boolean(values["dry-run"]),
863
+ ...(io.clock ? { clock: io.clock } : {}),
864
+ ...(io.nonce ? { nonce: io.nonce } : {}),
865
+ out: stdout,
866
+ err: stderr,
867
+ });
868
+ }
559
869
  if (command === "upgrade") {
560
870
  return upgrade(values, { cwd, runner, openRepo, prompt, stdout, stderr });
561
871
  }
package/src/doctor.mjs CHANGED
@@ -41,6 +41,15 @@ import {
41
41
  STAGE_CHECKS,
42
42
  } from "./checks/stages.mjs";
43
43
  import { firstLine } from "./exec.mjs";
44
+ import {
45
+ engineOf,
46
+ FACTORY_FILE_CHECKS,
47
+ FACTORY_LOCAL_CHECKS,
48
+ factoryFileResults,
49
+ factoryLocalChecks,
50
+ diskReads,
51
+ hasFactory,
52
+ } from "./factory/doctor/index.mjs";
44
53
  import { createGitHub } from "./github.mjs";
45
54
  import { openKitTag, PreManifestTagError, serverFromEnv } from "./kit-tag.mjs";
46
55
 
@@ -81,6 +90,7 @@ const skipAll = (ids, reason) => ids.map(([id, title]) => result(id, title, "ski
81
90
  * repo?: string,
82
91
  * openTag?: typeof openKitTag,
83
92
  * env?: NodeJS.ProcessEnv,
93
+ * now?: () => number,
84
94
  * }} opts
85
95
  * @returns {{ mode: "local" | "ci", modeReason: string | null, repo: string | null, tag: string | null, results: object[], refusal: string | null, exitCode: number }}
86
96
  */
@@ -92,6 +102,7 @@ export function runDoctor({
92
102
  repo: repoArg,
93
103
  openTag = openKitTag,
94
104
  env = process.env,
105
+ now = Date.now,
95
106
  }) {
96
107
  const results = [];
97
108
  let mode = ci ? "ci" : "local";
@@ -204,6 +215,10 @@ export function runDoctor({
204
215
 
205
216
  results.push(guarded("scan-recipe", () => checkScanRecipe({ root }), "Scan recipe"));
206
217
 
218
+ // A checkout with no Factory is checked exactly as before.
219
+ const factory = hasFactory(root);
220
+ if (factory) results.push(...guardedFactoryFiles({ root, tag: pins.tag }));
221
+
207
222
  if (mode === "ci") {
208
223
  results.push(
209
224
  ...skipAll(
@@ -211,9 +226,31 @@ export function runDoctor({
211
226
  `CI mode (${modeReason}): needs a local run with an admin gh login.`,
212
227
  ),
213
228
  );
229
+ if (factory) {
230
+ results.push(
231
+ ...skipAll(
232
+ FACTORY_LOCAL_CHECKS,
233
+ `CI mode (${modeReason}): needs a local run with an admin gh login.`,
234
+ ),
235
+ );
236
+ }
214
237
  } else {
215
238
  results.push(...localChecks({ runner, cwd, root, info, files, callers, snapshot }));
216
239
  results.push(...harnessChecks({ runner, cwd, root, info, callers, snapshot }));
240
+ if (factory) {
241
+ const github = createGitHub(runner, { cwd, hostname: info.host ?? null });
242
+ results.push(
243
+ ...factoryLocalChecks({
244
+ github,
245
+ runner,
246
+ cwd,
247
+ info,
248
+ engine: engineOf(diskReads(root)),
249
+ guard: guarded,
250
+ now,
251
+ }),
252
+ );
253
+ }
217
254
  }
218
255
  } finally {
219
256
  snapshot?.cleanup();
@@ -229,6 +266,19 @@ export function runDoctor({
229
266
  });
230
267
  }
231
268
 
269
+ /** The Factory's file checks; a crash becomes one failed check, never a silent pass. */
270
+ function guardedFactoryFiles({ root, tag }) {
271
+ try {
272
+ return factoryFileResults({ root, tag });
273
+ } catch (err) {
274
+ return FACTORY_FILE_CHECKS.map(([id, title]) =>
275
+ result(id, title, "fail", `Check crashed: ${firstLine(err?.message ?? err)}`, {
276
+ details: ["This is a bug in doctor; the other checks still ran."],
277
+ }),
278
+ );
279
+ }
280
+ }
281
+
232
282
  function repoInfo(github, repoArg) {
233
283
  return github.repoInfo(repoArg);
234
284
  }
@@ -478,7 +528,7 @@ export function formatResult(r) {
478
528
  }
479
529
 
480
530
  /** doctor --ci's pin and Kit-owned path checks, against an already-fetched tag. */
481
- export function fileAndPinChecks({ runner, root, snap }) {
531
+ export function fileAndPinChecks({ runner, root, snap, factory = false }) {
482
532
  let pins;
483
533
  try {
484
534
  pins = checkPins(readWorkflows(root).files);
@@ -504,6 +554,8 @@ export function fileAndPinChecks({ runner, root, snap }) {
504
554
  result("kit-owned-trees", "Kit-owned paths match the pinned tag", "fail", err.message),
505
555
  );
506
556
  }
557
+ // `upgrade` asks for the Factory's file checks too, when the checkout has a Factory.
558
+ if (factory) results.push(...guardedFactoryFiles({ root, tag: snap.tag }));
507
559
  return results;
508
560
  }
509
561
 
package/src/exec.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  // Runs commands as argument arrays, never through a shell, so no string from a
2
2
  // Consumer file or a GitHub response is ever parsed as shell syntax. Each
3
3
  // subcommand gets a runner that runs only the commands on its own allowlist:
4
- // `init`, `upgrade` and `doctor` reach only `git` and `gh`, and `deep-scan`
5
- // alone adds the accepted sandbox runtimes' CLIs and the 1Password CLI, which
6
- // reads its model credential (ADR 0019). There is no
4
+ // `init`, `upgrade` and `doctor` reach only `git` and `gh`; `deep-scan` and
5
+ // `factory-run` add the accepted sandbox runtimes' CLIs and the 1Password CLI, which
6
+ // reads the model credential (ADR 0019), and `factory-run` adds `gh` beside them and the
7
+ // host's keep-awake tools (`caffeinate`, `systemd-inhibit`). There is no
7
8
  // second runner that bypasses this. Every module takes a runner as a
8
9
  // parameter; tests inject a fake one, or a fake spawn behind the real
9
10
  // allowlist. A runner also has `start`, the same command run without
@@ -12,7 +13,7 @@
12
13
 
13
14
  import { spawn as spawnChild, spawnSync } from "node:child_process";
14
15
 
15
- /** The sandbox runtimes' command-line tools, which only `deep-scan` may run. */
16
+ /** The sandbox runtimes' command-line tools, which only `deep-scan` and `factory-run` may run. */
16
17
  export const RUNTIME_CLIS = Object.freeze([
17
18
  "sbx",
18
19
  "docker",
@@ -21,7 +22,7 @@ export const RUNTIME_CLIS = Object.freeze([
21
22
  "firecracker",
22
23
  ]);
23
24
 
24
- /** The secret managers' command-line tools, which only `deep-scan` may run. */
25
+ /** The secret managers' command-line tools, which only `deep-scan` and `factory-run` may run. */
25
26
  export const SECRET_MANAGER_CLIS = Object.freeze(["op"]);
26
27
 
27
28
  const ALLOWLISTS = Object.freeze({
@@ -29,6 +30,14 @@ const ALLOWLISTS = Object.freeze({
29
30
  upgrade: Object.freeze(["git", "gh"]),
30
31
  doctor: Object.freeze(["git", "gh"]),
31
32
  "deep-scan": Object.freeze(["git", ...RUNTIME_CLIS, ...SECRET_MANAGER_CLIS]),
33
+ "factory-run": Object.freeze([
34
+ "git",
35
+ "gh",
36
+ ...SECRET_MANAGER_CLIS,
37
+ ...RUNTIME_CLIS,
38
+ "caffeinate",
39
+ "systemd-inhibit",
40
+ ]),
32
41
  });
33
42
 
34
43
  /**