oh-my-copilot 5.18.2 → 5.19.2
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +206 -104
- package/bridge/__pycache__/gyoshu_bridge.cpython-314.pyc +0 -0
- package/bridge/claude-md-coordinator.cjs +2 -2
- package/bridge/cli.cjs +7155 -4471
- package/bridge/mcp-server.cjs +2470 -1027
- package/bridge/runtime-cli.cjs +102 -12
- package/bridge/team-mcp.cjs +100 -11
- package/bridge/team.js +100 -11
- package/dist/budget/ledger.d.ts +5 -0
- package/dist/budget/ledger.d.ts.map +1 -1
- package/dist/budget/ledger.js +2 -1
- package/dist/budget/ledger.js.map +1 -1
- package/dist/cli/commands/__tests__/factory-forge-cli.test.js +3 -3
- package/dist/cli/commands/__tests__/factory-forge-cli.test.js.map +1 -1
- package/dist/cli/commands/__tests__/factory-init.test.js +239 -17
- package/dist/cli/commands/__tests__/factory-init.test.js.map +1 -1
- package/dist/cli/commands/__tests__/factory-run-cli.test.js +103 -2
- package/dist/cli/commands/__tests__/factory-run-cli.test.js.map +1 -1
- package/dist/cli/commands/factory-board.d.ts +9 -0
- package/dist/cli/commands/factory-board.d.ts.map +1 -0
- package/dist/cli/commands/factory-board.js +34 -0
- package/dist/cli/commands/factory-board.js.map +1 -0
- package/dist/cli/commands/factory-check.d.ts +1 -0
- package/dist/cli/commands/factory-check.d.ts.map +1 -1
- package/dist/cli/commands/factory-check.js +2 -1
- package/dist/cli/commands/factory-check.js.map +1 -1
- package/dist/cli/commands/factory-poll.d.ts +9 -0
- package/dist/cli/commands/factory-poll.d.ts.map +1 -0
- package/dist/cli/commands/factory-poll.js +34 -0
- package/dist/cli/commands/factory-poll.js.map +1 -0
- package/dist/cli/commands/factory-setup.d.ts +2 -0
- package/dist/cli/commands/factory-setup.d.ts.map +1 -1
- package/dist/cli/commands/factory-setup.js +9 -3
- package/dist/cli/commands/factory-setup.js.map +1 -1
- package/dist/cli/commands/factory.d.ts +61 -18
- package/dist/cli/commands/factory.d.ts.map +1 -1
- package/dist/cli/commands/factory.js +325 -53
- package/dist/cli/commands/factory.js.map +1 -1
- package/dist/factory/__tests__/check-run.test.js +37 -96
- package/dist/factory/__tests__/check-run.test.js.map +1 -1
- package/dist/factory/__tests__/enqueue-ado.test.d.ts +2 -0
- package/dist/factory/__tests__/enqueue-ado.test.d.ts.map +1 -0
- package/dist/factory/__tests__/enqueue-ado.test.js +375 -0
- package/dist/factory/__tests__/enqueue-ado.test.js.map +1 -0
- package/dist/factory/__tests__/enqueue.test.d.ts +2 -0
- package/dist/factory/__tests__/enqueue.test.d.ts.map +1 -0
- package/dist/factory/__tests__/enqueue.test.js +470 -0
- package/dist/factory/__tests__/enqueue.test.js.map +1 -0
- package/dist/factory/__tests__/heal-ado.test.d.ts +2 -0
- package/dist/factory/__tests__/heal-ado.test.d.ts.map +1 -0
- package/dist/factory/__tests__/heal-ado.test.js +237 -0
- package/dist/factory/__tests__/heal-ado.test.js.map +1 -0
- package/dist/factory/__tests__/listener.test.js +27 -9
- package/dist/factory/__tests__/listener.test.js.map +1 -1
- package/dist/factory/__tests__/map-frontier.test.js +68 -0
- package/dist/factory/__tests__/map-frontier.test.js.map +1 -1
- package/dist/factory/__tests__/map-ingest.test.js +4 -3
- package/dist/factory/__tests__/map-ingest.test.js.map +1 -1
- package/dist/factory/__tests__/poll-ado.test.d.ts +2 -0
- package/dist/factory/__tests__/poll-ado.test.d.ts.map +1 -0
- package/dist/factory/__tests__/poll-ado.test.js +263 -0
- package/dist/factory/__tests__/poll-ado.test.js.map +1 -0
- package/dist/factory/__tests__/poll.test.d.ts +2 -0
- package/dist/factory/__tests__/poll.test.d.ts.map +1 -0
- package/dist/factory/__tests__/poll.test.js +748 -0
- package/dist/factory/__tests__/poll.test.js.map +1 -0
- package/dist/factory/board/__tests__/board-ado.test.d.ts +2 -0
- package/dist/factory/board/__tests__/board-ado.test.d.ts.map +1 -0
- package/dist/factory/board/__tests__/board-ado.test.js +194 -0
- package/dist/factory/board/__tests__/board-ado.test.js.map +1 -0
- package/dist/factory/board/__tests__/board.test.d.ts +2 -0
- package/dist/factory/board/__tests__/board.test.d.ts.map +1 -0
- package/dist/factory/board/__tests__/board.test.js +287 -0
- package/dist/factory/board/__tests__/board.test.js.map +1 -0
- package/dist/factory/board/collect.d.ts +145 -0
- package/dist/factory/board/collect.d.ts.map +1 -0
- package/dist/factory/board/collect.js +330 -0
- package/dist/factory/board/collect.js.map +1 -0
- package/dist/factory/board/render.d.ts +12 -0
- package/dist/factory/board/render.d.ts.map +1 -0
- package/dist/factory/board/render.js +93 -0
- package/dist/factory/board/render.js.map +1 -0
- package/dist/factory/check/__tests__/ado.test.js +14 -1
- package/dist/factory/check/__tests__/ado.test.js.map +1 -1
- package/dist/factory/check/__tests__/cli.test.js +38 -0
- package/dist/factory/check/__tests__/cli.test.js.map +1 -1
- package/dist/factory/check/__tests__/rules.test.js +72 -2
- package/dist/factory/check/__tests__/rules.test.js.map +1 -1
- package/dist/factory/check/cli.d.ts +34 -6
- package/dist/factory/check/cli.d.ts.map +1 -1
- package/dist/factory/check/cli.js +56 -34
- package/dist/factory/check/cli.js.map +1 -1
- package/dist/factory/check/rules.d.ts +11 -3
- package/dist/factory/check/rules.d.ts.map +1 -1
- package/dist/factory/check/rules.js +18 -5
- package/dist/factory/check/rules.js.map +1 -1
- package/dist/factory/check/score.d.ts +22 -2
- package/dist/factory/check/score.d.ts.map +1 -1
- package/dist/factory/check/score.js +55 -1
- package/dist/factory/check/score.js.map +1 -1
- package/dist/factory/enqueue.d.ts +195 -0
- package/dist/factory/enqueue.d.ts.map +1 -0
- package/dist/factory/enqueue.js +788 -0
- package/dist/factory/enqueue.js.map +1 -0
- package/dist/factory/heal/cli.d.ts +34 -2
- package/dist/factory/heal/cli.d.ts.map +1 -1
- package/dist/factory/heal/cli.js +88 -19
- package/dist/factory/heal/cli.js.map +1 -1
- package/dist/factory/heal/history.d.ts +12 -6
- package/dist/factory/heal/history.d.ts.map +1 -1
- package/dist/factory/heal/history.js +16 -6
- package/dist/factory/heal/history.js.map +1 -1
- package/dist/factory/heal/implement.d.ts +3 -1
- package/dist/factory/heal/implement.d.ts.map +1 -1
- package/dist/factory/heal/implement.js +1 -0
- package/dist/factory/heal/implement.js.map +1 -1
- package/dist/factory/heal/run.d.ts +3 -0
- package/dist/factory/heal/run.d.ts.map +1 -1
- package/dist/factory/heal/run.js +1 -1
- package/dist/factory/heal/run.js.map +1 -1
- package/dist/factory/listener.d.ts +13 -17
- package/dist/factory/listener.d.ts.map +1 -1
- package/dist/factory/listener.js +37 -63
- package/dist/factory/listener.js.map +1 -1
- package/dist/factory/map-frontier.d.ts +8 -1
- package/dist/factory/map-frontier.d.ts.map +1 -1
- package/dist/factory/map-frontier.js +65 -5
- package/dist/factory/map-frontier.js.map +1 -1
- package/dist/factory/map-ingest.d.ts.map +1 -1
- package/dist/factory/map-ingest.js +12 -1
- package/dist/factory/map-ingest.js.map +1 -1
- package/dist/factory/poll.d.ts +308 -0
- package/dist/factory/poll.d.ts.map +1 -0
- package/dist/factory/poll.js +677 -0
- package/dist/factory/poll.js.map +1 -0
- package/dist/factory/run/__tests__/engine.ado.test.js +157 -8
- package/dist/factory/run/__tests__/engine.ado.test.js.map +1 -1
- package/dist/factory/run/__tests__/engine.outcome.test.d.ts +2 -0
- package/dist/factory/run/__tests__/engine.outcome.test.d.ts.map +1 -0
- package/dist/factory/run/__tests__/engine.outcome.test.js +223 -0
- package/dist/factory/run/__tests__/engine.outcome.test.js.map +1 -0
- package/dist/factory/run/__tests__/engine.test.js +91 -0
- package/dist/factory/run/__tests__/engine.test.js.map +1 -1
- package/dist/factory/run/__tests__/units.test.js +299 -2
- package/dist/factory/run/__tests__/units.test.js.map +1 -1
- package/dist/factory/run/engine.d.ts +11 -3
- package/dist/factory/run/engine.d.ts.map +1 -1
- package/dist/factory/run/engine.js +145 -22
- package/dist/factory/run/engine.js.map +1 -1
- package/dist/factory/run/exec.d.ts +32 -4
- package/dist/factory/run/exec.d.ts.map +1 -1
- package/dist/factory/run/exec.js +120 -15
- package/dist/factory/run/exec.js.map +1 -1
- package/dist/factory/run/outcome.d.ts +31 -0
- package/dist/factory/run/outcome.d.ts.map +1 -0
- package/dist/factory/run/outcome.js +70 -0
- package/dist/factory/run/outcome.js.map +1 -0
- package/dist/factory/run/review-output.d.ts +6 -1
- package/dist/factory/run/review-output.d.ts.map +1 -1
- package/dist/factory/run/review-output.js +13 -3
- package/dist/factory/run/review-output.js.map +1 -1
- package/dist/factory/run/state.d.ts +2 -0
- package/dist/factory/run/state.d.ts.map +1 -1
- package/dist/factory/run/state.js.map +1 -1
- package/dist/factory/setup/__tests__/pipeline-pick.test.d.ts +2 -0
- package/dist/factory/setup/__tests__/pipeline-pick.test.d.ts.map +1 -0
- package/dist/factory/setup/__tests__/pipeline-pick.test.js +84 -0
- package/dist/factory/setup/__tests__/pipeline-pick.test.js.map +1 -0
- package/dist/factory/setup/__tests__/setup.test.js +1 -0
- package/dist/factory/setup/__tests__/setup.test.js.map +1 -1
- package/dist/factory/setup/detect.js +1 -1
- package/dist/factory/setup/detect.js.map +1 -1
- package/dist/factory/setup/pipeline-pick.d.ts +25 -0
- package/dist/factory/setup/pipeline-pick.d.ts.map +1 -0
- package/dist/factory/setup/pipeline-pick.js +55 -0
- package/dist/factory/setup/pipeline-pick.js.map +1 -0
- package/dist/factory/setup/write.d.ts +6 -0
- package/dist/factory/setup/write.d.ts.map +1 -1
- package/dist/factory/setup/write.js +3 -1
- package/dist/factory/setup/write.js.map +1 -1
- package/dist/factory/watchdog.d.ts +3 -2
- package/dist/factory/watchdog.d.ts.map +1 -1
- package/dist/factory/watchdog.js +5 -4
- package/dist/factory/watchdog.js.map +1 -1
- package/dist/factory/watchdog.test.js +19 -4
- package/dist/factory/watchdog.test.js.map +1 -1
- package/dist/forge/__tests__/config.test.js +51 -0
- package/dist/forge/__tests__/config.test.js.map +1 -1
- package/dist/forge/__tests__/gh-argv-baseline.chain.test.js +2 -2
- package/dist/forge/__tests__/gh-argv-baseline.chain.test.js.map +1 -1
- package/dist/forge/__tests__/gh-argv-baseline.factory.test.js +6 -4
- package/dist/forge/__tests__/gh-argv-baseline.factory.test.js.map +1 -1
- package/dist/forge/__tests__/gh-argv-baseline.tracker-writes.test.d.ts +2 -0
- package/dist/forge/__tests__/gh-argv-baseline.tracker-writes.test.d.ts.map +1 -0
- package/dist/forge/__tests__/gh-argv-baseline.tracker-writes.test.js +100 -0
- package/dist/forge/__tests__/gh-argv-baseline.tracker-writes.test.js.map +1 -0
- package/dist/forge/ado/__tests__/acceptance-scripts.test.js +360 -30
- package/dist/forge/ado/__tests__/acceptance-scripts.test.js.map +1 -1
- package/dist/forge/ado/__tests__/adapter.test.js +18 -3
- package/dist/forge/ado/__tests__/adapter.test.js.map +1 -1
- package/dist/forge/ado/__tests__/ci.test.d.ts +2 -0
- package/dist/forge/ado/__tests__/ci.test.d.ts.map +1 -0
- package/dist/forge/ado/__tests__/ci.test.js +271 -0
- package/dist/forge/ado/__tests__/ci.test.js.map +1 -0
- package/dist/forge/ado/__tests__/routes.test.js +24 -5
- package/dist/forge/ado/__tests__/routes.test.js.map +1 -1
- package/dist/forge/ado/__tests__/tracker-writes.test.d.ts +8 -0
- package/dist/forge/ado/__tests__/tracker-writes.test.d.ts.map +1 -0
- package/dist/forge/ado/__tests__/tracker-writes.test.js +377 -0
- package/dist/forge/ado/__tests__/tracker-writes.test.js.map +1 -0
- package/dist/forge/ado/__tests__/world.d.ts +4 -0
- package/dist/forge/ado/__tests__/world.d.ts.map +1 -1
- package/dist/forge/ado/__tests__/world.js +3 -2
- package/dist/forge/ado/__tests__/world.js.map +1 -1
- package/dist/forge/ado/checks.d.ts +25 -2
- package/dist/forge/ado/checks.d.ts.map +1 -1
- package/dist/forge/ado/checks.js +47 -23
- package/dist/forge/ado/checks.js.map +1 -1
- package/dist/forge/ado/ci.d.ts +13 -0
- package/dist/forge/ado/ci.d.ts.map +1 -0
- package/dist/forge/ado/ci.js +134 -0
- package/dist/forge/ado/ci.js.map +1 -0
- package/dist/forge/ado/guard.d.ts +19 -6
- package/dist/forge/ado/guard.d.ts.map +1 -1
- package/dist/forge/ado/guard.js +52 -11
- package/dist/forge/ado/guard.js.map +1 -1
- package/dist/forge/ado/index.d.ts +7 -1
- package/dist/forge/ado/index.d.ts.map +1 -1
- package/dist/forge/ado/index.js +16 -3
- package/dist/forge/ado/index.js.map +1 -1
- package/dist/forge/ado/reviews.d.ts +9 -1
- package/dist/forge/ado/reviews.d.ts.map +1 -1
- package/dist/forge/ado/reviews.js +45 -1
- package/dist/forge/ado/reviews.js.map +1 -1
- package/dist/forge/ado/routes.d.ts +35 -5
- package/dist/forge/ado/routes.d.ts.map +1 -1
- package/dist/forge/ado/routes.js +181 -9
- package/dist/forge/ado/routes.js.map +1 -1
- package/dist/forge/ado/tracker.d.ts +13 -2
- package/dist/forge/ado/tracker.d.ts.map +1 -1
- package/dist/forge/ado/tracker.js +139 -3
- package/dist/forge/ado/tracker.js.map +1 -1
- package/dist/forge/ado/wiql.d.ts +2 -0
- package/dist/forge/ado/wiql.d.ts.map +1 -1
- package/dist/forge/ado/wiql.js +6 -0
- package/dist/forge/ado/wiql.js.map +1 -1
- package/dist/forge/body-file.d.ts +17 -0
- package/dist/forge/body-file.d.ts.map +1 -0
- package/dist/forge/body-file.js +59 -0
- package/dist/forge/body-file.js.map +1 -0
- package/dist/forge/config.d.ts +3 -1
- package/dist/forge/config.d.ts.map +1 -1
- package/dist/forge/config.js +37 -2
- package/dist/forge/config.js.map +1 -1
- package/dist/forge/github/__tests__/argv-parity.test.js +17 -1
- package/dist/forge/github/__tests__/argv-parity.test.js.map +1 -1
- package/dist/forge/github/gh.d.ts +6 -0
- package/dist/forge/github/gh.d.ts.map +1 -1
- package/dist/forge/github/gh.js +2 -0
- package/dist/forge/github/gh.js.map +1 -1
- package/dist/forge/github/reviews.d.ts +2 -0
- package/dist/forge/github/reviews.d.ts.map +1 -1
- package/dist/forge/github/reviews.js +6 -0
- package/dist/forge/github/reviews.js.map +1 -1
- package/dist/forge/github/tracker.d.ts +20 -4
- package/dist/forge/github/tracker.d.ts.map +1 -1
- package/dist/forge/github/tracker.js +59 -5
- package/dist/forge/github/tracker.js.map +1 -1
- package/dist/forge/types.d.ts +108 -2
- package/dist/forge/types.d.ts.map +1 -1
- package/dist/forge/types.js +8 -0
- package/dist/forge/types.js.map +1 -1
- package/dist/graph/schema.d.ts +10 -10
- package/dist/hooks/session-end/__tests__/chain-enqueuer.test.js +156 -35
- package/dist/hooks/session-end/__tests__/chain-enqueuer.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/check-evidence.test.js +4 -4
- package/dist/hooks/session-end/__tests__/check-evidence.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/copilot-liveness.test.js +3 -3
- package/dist/hooks/session-end/__tests__/copilot-liveness.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/foreground-bootstrap.test.js +3 -3
- package/dist/hooks/session-end/__tests__/foreground-bootstrap.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/routing.test.js +36 -1
- package/dist/hooks/session-end/__tests__/routing.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/spawn-next.test.js +96 -5
- package/dist/hooks/session-end/__tests__/spawn-next.test.js.map +1 -1
- package/dist/hooks/session-end/__tests__/worker.test.js +38 -2
- package/dist/hooks/session-end/__tests__/worker.test.js.map +1 -1
- package/dist/hooks/session-end/chain-enqueuer.d.ts.map +1 -1
- package/dist/hooks/session-end/chain-enqueuer.js +22 -2
- package/dist/hooks/session-end/chain-enqueuer.js.map +1 -1
- package/dist/hooks/session-end/routing.d.ts +14 -0
- package/dist/hooks/session-end/routing.d.ts.map +1 -1
- package/dist/hooks/session-end/routing.js +25 -0
- package/dist/hooks/session-end/routing.js.map +1 -1
- package/dist/hooks/session-end/spawn-next.d.ts +20 -2
- package/dist/hooks/session-end/spawn-next.d.ts.map +1 -1
- package/dist/hooks/session-end/spawn-next.js +72 -5
- package/dist/hooks/session-end/spawn-next.js.map +1 -1
- package/dist/smoke/__tests__/copilot-chain-scenario.test.js +2 -1
- package/dist/smoke/__tests__/copilot-chain-scenario.test.js.map +1 -1
- package/dist/smoke/copilot-chain-scenario.d.ts.map +1 -1
- package/dist/smoke/copilot-chain-scenario.js +5 -1
- package/dist/smoke/copilot-chain-scenario.js.map +1 -1
- package/dist/team/__tests__/worker-launch-ack.test.js +134 -5
- package/dist/team/__tests__/worker-launch-ack.test.js.map +1 -1
- package/dist/team/worker-launch-ack.d.ts.map +1 -1
- package/dist/team/worker-launch-ack.js +19 -4
- package/dist/team/worker-launch-ack.js.map +1 -1
- package/docs/CLAUDE.md +1 -1
- package/docs/MIGRATION.md +26 -0
- package/docs/REFERENCE.md +187 -20
- package/docs/design/FACTORY-FINAL-DESIGN.md +1 -1
- package/docs/shipyard.md +17 -1
- package/package.json +2 -2
- package/plugin.json +1 -1
- package/scripts/ado-acceptance.mjs +309 -70
- package/scripts/lib/backlog-to-workitems.mjs +24 -21
- package/skills/ask-navigator/SKILL.md +5 -2
- package/skills/drydock/SKILL.md +2 -1
- package/skills/harbor/SKILL.md +17 -2
- package/skills/intent/SKILL.md +4 -0
- package/skills/launch/SKILL.md +35 -2
- package/skills/map/SKILL.md +2 -1
- package/skills/refit/SKILL.md +1 -0
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `omg factory poll` on GitHub (a scripted gh + git): tick order, the intake
|
|
3
|
+
* diff and its seen file, the PR resume hint, the board diff, the heal
|
|
4
|
+
* capability gate, the owner lock, the rate-limit backoff, the stop signal,
|
|
5
|
+
* the idle seam, and the read guard: a tick never spawns and never writes to
|
|
6
|
+
* the tracker or forge.
|
|
7
|
+
*/
|
|
8
|
+
import { execFileSync } from 'node:child_process';
|
|
9
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { hostname, tmpdir } from 'node:os';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
13
|
+
import { factoryRunJson } from '../../cli/commands/factory.js';
|
|
14
|
+
import { factoryPollCommand } from '../../cli/commands/factory-poll.js';
|
|
15
|
+
import { createForge } from '../../forge/index.js';
|
|
16
|
+
import { FactorySafetyError } from '../run/exec.js';
|
|
17
|
+
import { HealRunRefused } from '../heal/run.js';
|
|
18
|
+
import { HEAL_PIPELINE_MISSING } from '../heal/cli.js';
|
|
19
|
+
import { adoConfig } from '../../forge/ado/__tests__/world.js';
|
|
20
|
+
import { DEFAULT_HEAL_CONFIG } from '../heal/config.js';
|
|
21
|
+
import { DEFAULT_FACTORY_RUN_LIMITS } from '../run/budget.js';
|
|
22
|
+
import { healStateDir } from '../heal/state.js';
|
|
23
|
+
import { computeFactoryIntegrity, createFactoryState, factoryRepoOwnerPath, factoryStateDir, factoryStatePath, moveTo, runKeyOf, setGate } from '../run/state.js';
|
|
24
|
+
import { acquirePoller, defaultPollHealScan, nextInterval, parseInterval, PollBusyError, pollAuditPath, pollStatePath, POLL_JSON_SCHEMA_VERSION, POLL_MAX_INTERVAL_MS, autoStartTimeoutMs, defaultPollAutoStart, defaultPollBudget, parseRunNextOutcome, runFactoryPoll, } from '../poll.js';
|
|
25
|
+
const SHA = 'a'.repeat(40);
|
|
26
|
+
const HEAD = 'b'.repeat(40);
|
|
27
|
+
const T0 = '2026-10-31T10:00:00.000Z';
|
|
28
|
+
const NOW = Date.parse('2026-10-31T12:00:00.000Z');
|
|
29
|
+
const AC = '## Acceptance criteria\n- `omg x` exits 0\n';
|
|
30
|
+
const REPO = 'https://github.com/o/r';
|
|
31
|
+
const ok = (stdout = '') => ({ status: 0, stdout, stderr: '' });
|
|
32
|
+
const MIN = 60_000;
|
|
33
|
+
let root;
|
|
34
|
+
let out;
|
|
35
|
+
let err;
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
root = mkdtempSync(join(tmpdir(), 'omg-poll-'));
|
|
38
|
+
// A git checkout, so the poll and run state live under it (outside a repository the state root climbs to the home directory).
|
|
39
|
+
execFileSync('git', ['init', '-q', root]);
|
|
40
|
+
if (!pollStatePath(root).startsWith(root) || !factoryStateDir(root).startsWith(root))
|
|
41
|
+
throw new Error('state dir is outside the test root');
|
|
42
|
+
out = [];
|
|
43
|
+
err = [];
|
|
44
|
+
});
|
|
45
|
+
afterEach(() => rmSync(root, { recursive: true, force: true }));
|
|
46
|
+
/** gh + git: issues 1 and 2 in the label source; PR 9 (run #4) with `prState`. `onCall` sees each command first. */
|
|
47
|
+
function scripted(o = {}) {
|
|
48
|
+
const calls = [];
|
|
49
|
+
const exec = (cmd, args) => {
|
|
50
|
+
const a = args.join(' ');
|
|
51
|
+
calls.push(`${cmd} ${a}`);
|
|
52
|
+
const hooked = o.onCall?.(cmd, args);
|
|
53
|
+
if (hooked)
|
|
54
|
+
return hooked;
|
|
55
|
+
if (cmd === 'git') {
|
|
56
|
+
if (a === 'rev-parse --show-toplevel')
|
|
57
|
+
return ok(`${root}\n`);
|
|
58
|
+
if (a === 'remote get-url origin')
|
|
59
|
+
return ok('https://github.com/o/r.git\n');
|
|
60
|
+
if (a === 'fetch origin dev')
|
|
61
|
+
return ok();
|
|
62
|
+
if (a === 'rev-parse --verify refs/remotes/origin/dev')
|
|
63
|
+
return ok(`${SHA}\n`);
|
|
64
|
+
if (a === `cat-file -e ${SHA}^{commit}`)
|
|
65
|
+
return ok();
|
|
66
|
+
if (args[0] === 'ls-remote')
|
|
67
|
+
return ok('');
|
|
68
|
+
if (args[0] === 'show')
|
|
69
|
+
return { status: 128, stdout: '', stderr: `fatal: path does not exist in '${SHA}'` };
|
|
70
|
+
}
|
|
71
|
+
if (cmd === 'gh') {
|
|
72
|
+
if (args[0] === 'issue' && args[1] === 'list')
|
|
73
|
+
return ok(JSON.stringify((o.listed ?? [1, 2]).map((number) => ({ number }))));
|
|
74
|
+
if (args[0] === 'issue' && args[1] === 'view') {
|
|
75
|
+
const n = Number(args[2]);
|
|
76
|
+
return ok(JSON.stringify({ title: `issue ${n}`, body: n === 1 ? AC : 'no plan', state: 'OPEN', url: `${REPO}/issues/${n}`, author: { login: 'o' }, comments: [], labels: [] }));
|
|
77
|
+
}
|
|
78
|
+
if (args[0] === 'pr' && args[1] === 'list')
|
|
79
|
+
return ok('[]');
|
|
80
|
+
if (args[0] === 'pr' && args[1] === 'view' && args[2] === '9') {
|
|
81
|
+
return ok(JSON.stringify({ number: 9, state: o.prState ?? 'OPEN', isDraft: false, baseRefName: 'dev', baseRefOid: SHA, headRefName: 'factory/issue-4', headRefOid: HEAD, url: `${REPO}/pull/9` }));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`unscripted ${cmd} ${a}`);
|
|
85
|
+
};
|
|
86
|
+
return { exec, calls };
|
|
87
|
+
}
|
|
88
|
+
const scan = (calls = []) => (o) => {
|
|
89
|
+
calls.push(`${o.kind} ${o.base}`);
|
|
90
|
+
return { base: o.base, dryRun: false, entries: [{ runId: '77', event: 'push', headBranch: 'dev', sig8: '0123abcd', class: 'test-failure', reason: 'x', recorded: true }], queued: 0 };
|
|
91
|
+
};
|
|
92
|
+
function deps(exec, over = {}) {
|
|
93
|
+
return {
|
|
94
|
+
exec,
|
|
95
|
+
base: () => 'dev',
|
|
96
|
+
trustedUser: () => undefined,
|
|
97
|
+
projectConfig: () => undefined,
|
|
98
|
+
createForge,
|
|
99
|
+
now: () => NOW,
|
|
100
|
+
healScan: scan(),
|
|
101
|
+
owner: { pid: 4242, hostname: hostname(), isAlive: () => false },
|
|
102
|
+
onStop: () => () => { },
|
|
103
|
+
sleep: async () => { throw new Error('a --once poll never sleeps'); },
|
|
104
|
+
out: (l) => out.push(l),
|
|
105
|
+
err: (l) => err.push(l),
|
|
106
|
+
...over,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const poll = (exec, o = {}, over = {}) => runFactoryPoll(o, deps(exec, over));
|
|
110
|
+
const ticks = () => out.map((l) => JSON.parse(l));
|
|
111
|
+
const LABEL = { factory: { intake: { source: { type: 'label', label: 'factory' } } } };
|
|
112
|
+
function save(s) {
|
|
113
|
+
const path = factoryStatePath(root, runKeyOf(s));
|
|
114
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
115
|
+
writeFileSync(path, JSON.stringify({ ...s, integrity: computeFactoryIntegrity(s) }));
|
|
116
|
+
}
|
|
117
|
+
const issueRun = (issue) => ({ ...createFactoryState({ issue, repoUrl: REPO, base: 'dev' }, T0), forgeKind: 'github', issueTitle: `issue ${issue}` });
|
|
118
|
+
/** #4 parked at the merge gate with PR 9. */
|
|
119
|
+
const seedParked = () => save({ ...setGate(moveTo(issueRun(4), 'gates', 'gates', T0), 'merge', 'gates green; a human merges', T0), pr: 9, head: HEAD });
|
|
120
|
+
describe('omg factory poll on GitHub', () => {
|
|
121
|
+
it('--once --json: heal, intake, PRs and board in one tick; the seen file and the audit', async () => {
|
|
122
|
+
seedParked();
|
|
123
|
+
const healCalls = [];
|
|
124
|
+
const w = scripted({ prState: 'MERGED' });
|
|
125
|
+
expect(await poll(w.exec, { once: true, json: true }, { projectConfig: () => LABEL, healScan: scan(healCalls) })).toBe(0);
|
|
126
|
+
const [t] = ticks();
|
|
127
|
+
expect(out).toHaveLength(1);
|
|
128
|
+
expect(t).toMatchObject({
|
|
129
|
+
schemaVersion: POLL_JSON_SCHEMA_VERSION, tick: 1, forge: 'github', repo: REPO,
|
|
130
|
+
heal: { ran: true, entries: 1, recorded: 1, queued: 0 },
|
|
131
|
+
intake: { ran: true, items: 2, new: [{ id: 1, bucket: 'runnable', reasons: [] }, { id: 2, bucket: 'needs-plan' }] },
|
|
132
|
+
prs: [{ id: 4, pr: 9, state: 'MERGED', hint: 'omg factory run 4' }],
|
|
133
|
+
idle: true, rateLimited: false, errors: [],
|
|
134
|
+
});
|
|
135
|
+
expect(t.nextIntervalMs).toBeUndefined();
|
|
136
|
+
expect(healCalls).toEqual(['github dev']);
|
|
137
|
+
// Every row on the first tick: the two backlog items and the parked run (not in the source).
|
|
138
|
+
expect(t.board.map((b) => [b.stage, b.row.id])).toEqual([['backlog', 1], ['backlog', 2], ['outside', 4]]);
|
|
139
|
+
// Tick order: the intake query, then the new items' reads, then the PR read.
|
|
140
|
+
const order = w.calls.filter((c) => /^gh (issue list|issue view|pr view)/.test(c)).map((c) => c.split(' ').slice(1, 3).join(' '));
|
|
141
|
+
expect(order.indexOf('issue list')).toBeLessThan(order.indexOf('issue view'));
|
|
142
|
+
expect(order.indexOf('issue view')).toBeLessThan(order.indexOf('pr view'));
|
|
143
|
+
expect(JSON.parse(readFileSync(pollStatePath(root), 'utf8'))).toEqual({ seen: [1, 2], at: '2026-10-31T12:00:00.000Z' });
|
|
144
|
+
const audit = readFileSync(pollAuditPath(root), 'utf8').trim().split('\n').map((l) => JSON.parse(l));
|
|
145
|
+
expect(audit.map((a) => [a.kind, a.id])).toEqual([['intake-new', 1], ['intake-new', 2], ['pr-done', 4]]);
|
|
146
|
+
expect(audit[2]).toMatchObject({ pr: 9, state: 'MERGED', hint: 'omg factory run 4' });
|
|
147
|
+
});
|
|
148
|
+
it('a second tick reports only items new since the seen file', async () => {
|
|
149
|
+
const w = scripted({ listed: [1, 2] });
|
|
150
|
+
expect(await poll(w.exec, { once: true, json: true }, { projectConfig: () => LABEL })).toBe(0);
|
|
151
|
+
out.length = 0;
|
|
152
|
+
const w2 = scripted({ listed: [1, 2, 3] });
|
|
153
|
+
expect(await poll(w2.exec, { once: true, json: true }, { projectConfig: () => LABEL })).toBe(0);
|
|
154
|
+
expect(ticks()[0].intake).toMatchObject({ ran: true, items: 3, new: [{ id: 3 }] });
|
|
155
|
+
// check reads #3 alone; then the board reads each item (GitHub has no batched peek).
|
|
156
|
+
expect(w2.calls.filter((c) => c.startsWith('gh issue view')).map((c) => c.split(' ')[3])).toEqual(['3', '1', '2', '3']);
|
|
157
|
+
expect(JSON.parse(readFileSync(pollStatePath(root), 'utf8')).seen).toEqual([1, 2, 3]);
|
|
158
|
+
});
|
|
159
|
+
it('an open PR prints no hint; text output names a merged one', async () => {
|
|
160
|
+
seedParked();
|
|
161
|
+
expect(await poll(scripted().exec, { once: true, json: true })).toBe(0);
|
|
162
|
+
expect(ticks()[0].prs).toEqual([{ id: 4, pr: 9, state: 'OPEN' }]);
|
|
163
|
+
out.length = 0;
|
|
164
|
+
expect(await poll(scripted({ prState: 'CLOSED' }).exec, { once: true })).toBe(0);
|
|
165
|
+
expect(out.join('\n')).toContain('PR #9 of #4 is closed by a human: run `omg factory run 4` to record it');
|
|
166
|
+
});
|
|
167
|
+
it('never spawns and never writes: every command is a read, and the guard refuses writes before gh', async () => {
|
|
168
|
+
seedParked();
|
|
169
|
+
let guarded;
|
|
170
|
+
const spy = (exec, cwd, config, options) => {
|
|
171
|
+
guarded = exec;
|
|
172
|
+
return createForge(exec, cwd, config, options);
|
|
173
|
+
};
|
|
174
|
+
const w = scripted({ prState: 'MERGED' });
|
|
175
|
+
expect(await poll(w.exec, { once: true, json: true }, { projectConfig: () => LABEL, createForge: spy })).toBe(0);
|
|
176
|
+
for (const c of w.calls)
|
|
177
|
+
expect(c, c).toMatch(/^(?:git (?:rev-parse|remote get-url|fetch origin dev|cat-file -e|ls-remote|show)|gh (?:issue (?:list|view)|pr (?:list|view)))\b/);
|
|
178
|
+
const before = w.calls.length;
|
|
179
|
+
const writes = [
|
|
180
|
+
['issue', 'edit', '1', '--add-label', 'factory:building'],
|
|
181
|
+
['issue', 'comment', '1', '--body-file', join(root, 'b.md')],
|
|
182
|
+
['pr', 'ready', '9'],
|
|
183
|
+
['pr', 'merge', '9', '--squash'],
|
|
184
|
+
['pr', 'comment', '9', '--body-file', join(root, 'b.md')],
|
|
185
|
+
['run', 'rerun', '77', '--failed'],
|
|
186
|
+
];
|
|
187
|
+
for (const args of writes)
|
|
188
|
+
expect(() => guarded('gh', args, { cwd: root }), args.join(' ')).toThrow(FactorySafetyError);
|
|
189
|
+
for (const cmd of ['copilot', 'node', 'omg'])
|
|
190
|
+
expect(() => guarded(cmd, ['-p', 'x'], { cwd: root })).toThrow(FactorySafetyError);
|
|
191
|
+
expect(w.calls.length).toBe(before);
|
|
192
|
+
});
|
|
193
|
+
it('a guard refusal inside a tick propagates instead of being reported as a read failure', async () => {
|
|
194
|
+
const refusing = (exec, cwd, config, options) => {
|
|
195
|
+
const forge = createForge(exec, cwd, config, options);
|
|
196
|
+
return { ...forge, tracker: { ...forge.tracker, queryItems: () => { exec('gh', ['issue', 'edit', '1', '--add-label', 'x']); return []; } } };
|
|
197
|
+
};
|
|
198
|
+
await expect(poll(scripted().exec, { once: true, json: true }, { projectConfig: () => LABEL, createForge: refusing })).rejects.toThrow(FactorySafetyError);
|
|
199
|
+
});
|
|
200
|
+
it('the heal tick runs only with the heal capability, and a refused scan only skips it', async () => {
|
|
201
|
+
const noHeal = (exec, cwd, config, options) => {
|
|
202
|
+
const forge = createForge(exec, cwd, config, options);
|
|
203
|
+
return { ...forge, capabilities: { ...forge.capabilities, heal: false } };
|
|
204
|
+
};
|
|
205
|
+
const calls = [];
|
|
206
|
+
expect(await poll(scripted().exec, { once: true, json: true }, { createForge: noHeal, healScan: scan(calls) })).toBe(0);
|
|
207
|
+
expect(calls).toEqual([]);
|
|
208
|
+
expect(ticks()[0].heal).toEqual({ ran: false, reason: expect.stringContaining('no heal capability') });
|
|
209
|
+
out.length = 0;
|
|
210
|
+
expect(await poll(scripted().exec, { once: true, json: true, heal: false }, { healScan: scan(calls) })).toBe(0);
|
|
211
|
+
expect(calls).toEqual([]);
|
|
212
|
+
expect(ticks()[0].heal).toEqual({ ran: false, reason: '--no-heal' });
|
|
213
|
+
out.length = 0;
|
|
214
|
+
const refused = () => { throw new HealRunRefused('not here'); };
|
|
215
|
+
expect(await poll(scripted().exec, { once: true, json: true }, { healScan: refused })).toBe(0);
|
|
216
|
+
expect(ticks()[0]).toMatchObject({ heal: { ran: false, reason: 'not here' }, errors: [] });
|
|
217
|
+
});
|
|
218
|
+
it('the default heal scan on Azure DevOps without factory.heal.pipeline refuses with heal scan\'s message before any command', () => {
|
|
219
|
+
const exec = () => { throw new Error('no command runs'); };
|
|
220
|
+
const heal = defaultPollHealScan(exec, () => { throw new Error('no config read'); }, join(root, 'ledger'), () => { throw new Error('no az entry'); });
|
|
221
|
+
expect(() => heal({ root, base: 'dev', kind: 'azure-devops', forge: adoConfig(), now: new Date(NOW), warn: () => { } })).toThrow(new HealRunRefused(HEAL_PIPELINE_MISSING));
|
|
222
|
+
});
|
|
223
|
+
it('owner lock: a second poller is refused; a dead owner is taken over and released', async () => {
|
|
224
|
+
const live = acquirePoller(root, T0, { pid: 1, hostname: hostname(), isAlive: () => true });
|
|
225
|
+
await expect(poll(scripted().exec, { once: true, json: true }, { owner: { pid: 2, hostname: hostname(), isAlive: () => true } })).rejects.toThrow(PollBusyError);
|
|
226
|
+
const cli = factoryPollCommand(deps(scripted().exec, { owner: { pid: 2, hostname: hostname(), isAlive: () => true } }));
|
|
227
|
+
await cli.parseAsync(['--once', '--json'], { from: 'user' });
|
|
228
|
+
expect(process.exitCode).toBe(1);
|
|
229
|
+
expect(JSON.parse(out.at(-1))).toEqual({ ok: false, error: expect.stringContaining('omg factory poll already runs in this repository (pid 1') });
|
|
230
|
+
process.exitCode = 0;
|
|
231
|
+
live.release();
|
|
232
|
+
out.length = 0;
|
|
233
|
+
// A recorded owner whose pid is dead on this host is taken over; the run releases it again.
|
|
234
|
+
acquirePoller(root, T0, { pid: 1, hostname: hostname(), isAlive: () => true });
|
|
235
|
+
expect(await poll(scripted().exec, { once: true, json: true }, { owner: { pid: 2, hostname: hostname(), isAlive: () => false } })).toBe(0);
|
|
236
|
+
expect(JSON.parse(readFileSync(pollStatePath(root), 'utf8')).pid).toBeUndefined();
|
|
237
|
+
});
|
|
238
|
+
it('the interval: default 5 min, 2 min floor, 60 min ceiling', async () => {
|
|
239
|
+
expect(parseInterval('5m')).toBe(5 * MIN);
|
|
240
|
+
expect(parseInterval('3')).toBe(3 * MIN);
|
|
241
|
+
expect(parseInterval('120s')).toBe(2 * MIN);
|
|
242
|
+
expect(parseInterval('1h')).toBe(60 * MIN);
|
|
243
|
+
for (const bad of ['1m', '90s', '2h', '0', '-5m', '5 min', ''])
|
|
244
|
+
expect(() => parseInterval(bad), bad).toThrow();
|
|
245
|
+
const cli = factoryPollCommand(deps(scripted().exec));
|
|
246
|
+
await cli.parseAsync(['--interval', '1m', '--once'], { from: 'user' });
|
|
247
|
+
expect(process.exitCode).toBe(2);
|
|
248
|
+
expect(err.join('\n')).toMatch(/below the 2 min floor/);
|
|
249
|
+
process.exitCode = 0;
|
|
250
|
+
});
|
|
251
|
+
it('backoff: a rate-limited tick doubles the wait up to 60 min; a clean tick resets it', async () => {
|
|
252
|
+
expect(nextInterval(5 * MIN, 5 * MIN, true)).toBe(10 * MIN);
|
|
253
|
+
expect(nextInterval(40 * MIN, 5 * MIN, true)).toBe(POLL_MAX_INTERVAL_MS);
|
|
254
|
+
expect(nextInterval(40 * MIN, 5 * MIN, false)).toBe(5 * MIN);
|
|
255
|
+
let n = 0;
|
|
256
|
+
const limited = new Set([1, 2]);
|
|
257
|
+
const w = scripted({
|
|
258
|
+
onCall: (cmd, args) => (cmd === 'gh' && args[0] === 'issue' && args[1] === 'list' && limited.has(++n)
|
|
259
|
+
? { status: 1, stdout: '', stderr: 'HTTP 429: Too Many Requests' } : undefined),
|
|
260
|
+
});
|
|
261
|
+
const waits = [];
|
|
262
|
+
let stop = () => { };
|
|
263
|
+
const code = await poll(w.exec, { json: true }, {
|
|
264
|
+
projectConfig: () => LABEL,
|
|
265
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
266
|
+
sleep: async (ms) => { waits.push(ms); if (waits.length === 3)
|
|
267
|
+
stop(); },
|
|
268
|
+
});
|
|
269
|
+
expect(code).toBe(0);
|
|
270
|
+
expect(waits).toEqual([10 * MIN, 20 * MIN, 5 * MIN]);
|
|
271
|
+
expect(ticks().map((t) => [t.rateLimited, t.nextIntervalMs])).toEqual([[true, 10 * MIN], [true, 20 * MIN], [false, 5 * MIN]]);
|
|
272
|
+
expect(ticks()[0].errors[0]).toMatch(/^intake: .*429/);
|
|
273
|
+
});
|
|
274
|
+
it('warns once when the budget ledger an earlier tick saw has disappeared', async () => {
|
|
275
|
+
const file = join(root, '.omg', 'state', 'budget-ledger.json');
|
|
276
|
+
mkdirSync(join(root, '.omg', 'state'), { recursive: true });
|
|
277
|
+
writeFileSync(file, '{}');
|
|
278
|
+
let stop = () => { };
|
|
279
|
+
let n = 0;
|
|
280
|
+
const code = await poll(scripted().exec, { json: true }, {
|
|
281
|
+
projectConfig: () => LABEL,
|
|
282
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
283
|
+
sleep: async () => { n += 1; if (n === 1)
|
|
284
|
+
rmSync(file); if (n === 3)
|
|
285
|
+
stop(); },
|
|
286
|
+
});
|
|
287
|
+
expect(code).toBe(0);
|
|
288
|
+
expect(ticks().map((t) => t.warnings.filter((w) => /disappeared/.test(w)).length)).toEqual([0, 1, 0]);
|
|
289
|
+
expect(ticks()[1].warnings.join('\n')).toMatch(/budget ledger .*budget-ledger\.json disappeared; spend restarts at 0/);
|
|
290
|
+
});
|
|
291
|
+
it('a stop signal during a tick ends the loop after that tick; the board then prints only changed rows', async () => {
|
|
292
|
+
let stop = () => { };
|
|
293
|
+
let ticked = 0;
|
|
294
|
+
const w = scripted({ onCall: (cmd, args) => { if (cmd === 'gh' && args[1] === 'list' && args[0] === 'issue' && ++ticked === 2)
|
|
295
|
+
stop(); return undefined; } });
|
|
296
|
+
const sleeps = [];
|
|
297
|
+
const code = await poll(w.exec, { json: true }, {
|
|
298
|
+
projectConfig: () => LABEL,
|
|
299
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
300
|
+
sleep: async (ms) => { sleeps.push(ms); },
|
|
301
|
+
});
|
|
302
|
+
expect(code).toBe(0);
|
|
303
|
+
expect(sleeps).toEqual([5 * MIN]);
|
|
304
|
+
expect(ticks().map((t) => t.tick)).toEqual([1, 2]);
|
|
305
|
+
// The second tick finished (output printed) and nothing on the board changed.
|
|
306
|
+
expect(ticks()[1].board).toEqual([]);
|
|
307
|
+
expect(ticks()[0].board.map((b) => b.row.id)).toEqual([1, 2]);
|
|
308
|
+
expect(err.join('\n')).toMatch(/stopping after the current tick/);
|
|
309
|
+
});
|
|
310
|
+
it('idle: a live engine owner or a run in flight is not idle; a parked run is', async () => {
|
|
311
|
+
seedParked();
|
|
312
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
313
|
+
expect(ticks()[0].idle).toBe(true);
|
|
314
|
+
out.length = 0;
|
|
315
|
+
save(moveTo(issueRun(5), 'implement', 'plan ok', T0));
|
|
316
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
317
|
+
expect(ticks()[0].idle).toBe(false);
|
|
318
|
+
rmSync(factoryStatePath(root, 5));
|
|
319
|
+
out.length = 0;
|
|
320
|
+
writeFileSync(factoryRepoOwnerPath(root), JSON.stringify({ pid: 99, hostname: hostname(), startedAt: T0, run: '4' }));
|
|
321
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false }, { owner: { pid: 2, hostname: hostname(), isAlive: (pid) => pid === 99 } })).toBe(0);
|
|
322
|
+
expect(ticks()[0].idle).toBe(false);
|
|
323
|
+
expect(existsSync(factoryRepoOwnerPath(root))).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
it('no intake source: the intake tick is skipped and the board shows runs only', async () => {
|
|
326
|
+
seedParked();
|
|
327
|
+
const w = scripted();
|
|
328
|
+
expect(await poll(w.exec, { once: true, json: true })).toBe(0);
|
|
329
|
+
expect(ticks()[0].intake).toEqual({ ran: false, reason: 'no intake source (factory.intake.source)' });
|
|
330
|
+
expect(ticks()[0].board.map((b) => [b.stage, b.row.id])).toEqual([['outside', 4]]);
|
|
331
|
+
expect(w.calls.some((c) => c.startsWith('gh issue'))).toBe(false);
|
|
332
|
+
});
|
|
333
|
+
it('idle: an unreadable or integrity-failed run state is never idle', async () => {
|
|
334
|
+
seedParked();
|
|
335
|
+
const path = factoryStatePath(root, 5);
|
|
336
|
+
// Parked at a gate (would be idle) but its seal fails: readFactoryState still reads it.
|
|
337
|
+
const parked = setGate(moveTo(issueRun(5), 'gates', 'gates', T0), 'merge', 'gates green; a human merges', T0);
|
|
338
|
+
writeFileSync(path, JSON.stringify({ ...parked, integrity: 'f'.repeat(64) }));
|
|
339
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
340
|
+
expect(ticks()[0].idle).toBe(false);
|
|
341
|
+
out.length = 0;
|
|
342
|
+
save(parked);
|
|
343
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
344
|
+
expect(ticks()[0].idle).toBe(true);
|
|
345
|
+
out.length = 0;
|
|
346
|
+
writeFileSync(path, '{ not json');
|
|
347
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
348
|
+
expect(ticks()[0].idle).toBe(false);
|
|
349
|
+
out.length = 0;
|
|
350
|
+
rmSync(path);
|
|
351
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false })).toBe(0);
|
|
352
|
+
expect(ticks()[0].idle).toBe(true);
|
|
353
|
+
});
|
|
354
|
+
it('owner: a pid without a hostname is no owner; a stale heartbeat is taken over; the refusal names the file', async () => {
|
|
355
|
+
const path = pollStatePath(root);
|
|
356
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
357
|
+
const me = { pid: 2, hostname: hostname(), isAlive: () => true };
|
|
358
|
+
writeFileSync(path, JSON.stringify({ pid: 1, startedAt: T0, seen: [7] }));
|
|
359
|
+
acquirePoller(root, T0, me).release();
|
|
360
|
+
expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual({ seen: [7] });
|
|
361
|
+
// Another host, last beat 2 h 1 min ago: stale.
|
|
362
|
+
const beatAt = new Date(NOW - 121 * MIN).toISOString();
|
|
363
|
+
writeFileSync(path, JSON.stringify({ pid: 1, hostname: 'elsewhere', startedAt: T0, beatAt, seen: [7] }));
|
|
364
|
+
const now = new Date(NOW).toISOString();
|
|
365
|
+
acquirePoller(root, now, me);
|
|
366
|
+
expect(JSON.parse(readFileSync(path, 'utf8'))).toMatchObject({ pid: 2, hostname: hostname(), startedAt: now, beatAt: now, seen: [7] });
|
|
367
|
+
// A live owner within the window is refused, with the file to delete.
|
|
368
|
+
writeFileSync(path, JSON.stringify({ pid: 1, hostname: 'elsewhere', startedAt: T0, beatAt: new Date(NOW - 119 * MIN).toISOString(), seen: [] }));
|
|
369
|
+
expect(() => acquirePoller(root, now, me)).toThrow(PollBusyError);
|
|
370
|
+
expect(() => acquirePoller(root, now, me)).toThrow(`If no poller runs, delete ${path}`);
|
|
371
|
+
});
|
|
372
|
+
it('owner: each tick writes the heartbeat under the lock', async () => {
|
|
373
|
+
let clock = NOW;
|
|
374
|
+
let stop = () => { };
|
|
375
|
+
const beats = [];
|
|
376
|
+
const code = await poll(scripted().exec, { json: true }, {
|
|
377
|
+
now: () => clock,
|
|
378
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
379
|
+
sleep: async () => {
|
|
380
|
+
beats.push(JSON.parse(readFileSync(pollStatePath(root), 'utf8')).beatAt);
|
|
381
|
+
clock += 5 * MIN;
|
|
382
|
+
if (beats.length === 2)
|
|
383
|
+
stop();
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
expect(code).toBe(0);
|
|
387
|
+
expect(beats).toEqual([new Date(NOW).toISOString(), new Date(NOW + 5 * MIN).toISOString()]);
|
|
388
|
+
// Released: no owner and no heartbeat left.
|
|
389
|
+
expect(JSON.parse(readFileSync(pollStatePath(root), 'utf8')).beatAt).toBeUndefined();
|
|
390
|
+
});
|
|
391
|
+
it('a failed intake check or seen-file write is a tick error, never the end of the loop', async () => {
|
|
392
|
+
// On the first tick item 2 cannot be shaped into a result: checkItems throws.
|
|
393
|
+
const throwing = (exec, cwd, config, options) => {
|
|
394
|
+
const forge = createForge(exec, cwd, config, options);
|
|
395
|
+
return {
|
|
396
|
+
...forge,
|
|
397
|
+
tracker: {
|
|
398
|
+
...forge.tracker,
|
|
399
|
+
viewItem: (id) => {
|
|
400
|
+
const item = forge.tracker.viewItem(id);
|
|
401
|
+
return id === 2 && ticks().length === 0 ? Object.defineProperty({ ...item }, 'title', { get: () => { throw new Error('bad title'); } }) : item;
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
};
|
|
406
|
+
let stop = () => { };
|
|
407
|
+
let lockHeld = false;
|
|
408
|
+
const lock = `${pollStatePath(root)}.lock`;
|
|
409
|
+
const code = await poll(scripted({
|
|
410
|
+
// The second tick's seen-file write meets a held lock (a live pid, fresh).
|
|
411
|
+
onCall: (cmd, args) => {
|
|
412
|
+
if (cmd === 'gh' && args[0] === 'issue' && args[1] === 'list' && ticks().length === 1) {
|
|
413
|
+
writeFileSync(lock, JSON.stringify({ pid: process.pid, timestamp: Date.now() }));
|
|
414
|
+
lockHeld = true;
|
|
415
|
+
}
|
|
416
|
+
return undefined;
|
|
417
|
+
},
|
|
418
|
+
}).exec, { json: true }, {
|
|
419
|
+
projectConfig: () => LABEL,
|
|
420
|
+
createForge: throwing,
|
|
421
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
422
|
+
sleep: async () => {
|
|
423
|
+
if (lockHeld) {
|
|
424
|
+
rmSync(lock);
|
|
425
|
+
lockHeld = false;
|
|
426
|
+
}
|
|
427
|
+
if (ticks().length === 3)
|
|
428
|
+
stop();
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
expect(code).toBe(0);
|
|
432
|
+
const [t1, t2] = ticks();
|
|
433
|
+
expect(t1.intake).toEqual({ ran: false, reason: 'the intake check failed' });
|
|
434
|
+
expect(t1.errors).toEqual([expect.stringMatching(/^intake check: bad title/)]);
|
|
435
|
+
expect(t2.errors).toEqual(expect.arrayContaining([expect.stringMatching(/^intake seen: /)]));
|
|
436
|
+
expect(ticks()).toHaveLength(3);
|
|
437
|
+
// Tick 1 marked nothing seen and tick 2 could not write: tick 3 checks both ids again and records them.
|
|
438
|
+
expect(ticks()[2].intake).toMatchObject({ ran: true, new: [{ id: 1 }, { id: 2 }] });
|
|
439
|
+
expect(JSON.parse(readFileSync(pollStatePath(root), 'utf8')).seen).toEqual([1, 2]);
|
|
440
|
+
}, 30_000);
|
|
441
|
+
it('the audit never appends through a symbolic link', async () => {
|
|
442
|
+
const target = join(root, 'elsewhere.jsonl');
|
|
443
|
+
writeFileSync(target, '');
|
|
444
|
+
mkdirSync(dirname(pollAuditPath(root)), { recursive: true });
|
|
445
|
+
let linked = true;
|
|
446
|
+
try {
|
|
447
|
+
symlinkSync(target, pollAuditPath(root), 'file');
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
linked = false;
|
|
451
|
+
}
|
|
452
|
+
// Without the privilege to make a file link (Windows), a junction is the link.
|
|
453
|
+
if (!linked) {
|
|
454
|
+
mkdirSync(join(root, 'dir-target'));
|
|
455
|
+
symlinkSync(join(root, 'dir-target'), pollAuditPath(root), 'junction');
|
|
456
|
+
}
|
|
457
|
+
seedParked();
|
|
458
|
+
expect(await poll(scripted({ prState: 'MERGED' }).exec, { once: true, json: true }, { projectConfig: () => LABEL })).toBe(0);
|
|
459
|
+
expect(ticks()[0].intake).toMatchObject({ ran: true, items: 2 });
|
|
460
|
+
expect(readFileSync(target, 'utf8')).toBe('');
|
|
461
|
+
expect(lstatSync(pollAuditPath(root)).isSymbolicLink()).toBe(true);
|
|
462
|
+
});
|
|
463
|
+
it('the default heal scan on GitHub: read-only argv, heal state under the repository', () => {
|
|
464
|
+
const calls = [];
|
|
465
|
+
const exec = (cmd, args) => {
|
|
466
|
+
calls.push(`${cmd} ${args.join(' ')}`);
|
|
467
|
+
if (cmd === 'gh' && args[0] === 'run' && args[1] === 'list')
|
|
468
|
+
return ok('[]');
|
|
469
|
+
if (cmd === 'git' && args[0] === 'show')
|
|
470
|
+
return { status: 128, stdout: '', stderr: 'fatal: path does not exist' };
|
|
471
|
+
throw new Error(`unscripted ${cmd} ${args.join(' ')}`);
|
|
472
|
+
};
|
|
473
|
+
const warnings = [];
|
|
474
|
+
const ledger = mkdtempSync(join(tmpdir(), 'omg-poll-ledger-'));
|
|
475
|
+
try {
|
|
476
|
+
const heal = defaultPollHealScan(exec, () => ({ config: { ...DEFAULT_HEAL_CONFIG }, warnings: [] }), ledger);
|
|
477
|
+
expect(heal({ root, base: 'dev', kind: 'github', now: new Date(NOW), warn: (m) => warnings.push(m) })).toMatchObject({ entries: [], queued: 0 });
|
|
478
|
+
}
|
|
479
|
+
finally {
|
|
480
|
+
rmSync(ledger, { recursive: true, force: true });
|
|
481
|
+
}
|
|
482
|
+
expect(calls.length).toBeGreaterThan(0);
|
|
483
|
+
for (const c of calls)
|
|
484
|
+
expect(c, c).toMatch(/^(?:gh run list |git show refs\/remotes\/origin\/dev:)/);
|
|
485
|
+
expect(healStateDir(root).startsWith(root)).toBe(true);
|
|
486
|
+
expect(warnings.join('\n')).toMatch(/known-flaky/);
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
describe('omg factory poll: factory.intake.autoStart', () => {
|
|
490
|
+
const ON = { factory: { intake: { autoStart: true } } };
|
|
491
|
+
/** A fake engine: records each start, optionally leaves a run behind. */
|
|
492
|
+
function engine(r = {}) {
|
|
493
|
+
const starts = [];
|
|
494
|
+
const autoStart = async (o) => {
|
|
495
|
+
starts.push(o.root);
|
|
496
|
+
r.after?.();
|
|
497
|
+
return { exitCode: r.exitCode ?? 0, parsed: r.parsed ?? true, next: r.next === undefined ? 1 : r.next, message: 'parked at awaiting-human: merge' };
|
|
498
|
+
};
|
|
499
|
+
return { starts, autoStart };
|
|
500
|
+
}
|
|
501
|
+
const on = (e, over = {}) => ({ trustedUser: () => ON, projectConfig: () => LABEL, autoStart: e.autoStart, budget: () => null, ...over });
|
|
502
|
+
it('is off by default: an idle tick starts nothing', async () => {
|
|
503
|
+
const e = engine();
|
|
504
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { trustedUser: () => undefined }))).toBe(0);
|
|
505
|
+
expect(ticks()[0]).toMatchObject({ idle: true, autoStart: { started: false, reason: 'factory.intake.autoStart is off' } });
|
|
506
|
+
expect(e.starts).toEqual([]);
|
|
507
|
+
});
|
|
508
|
+
it('a working-tree key is ignored with a warning', async () => {
|
|
509
|
+
const e = engine();
|
|
510
|
+
const project = { factory: { intake: { ...LABEL.factory.intake, autoStart: true } } };
|
|
511
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { trustedUser: () => undefined, projectConfig: () => project }))).toBe(0);
|
|
512
|
+
expect(ticks()[0].autoStart).toEqual({ started: false, reason: 'factory.intake.autoStart is off' });
|
|
513
|
+
expect(err.join('\n')).toContain('ignoring factory.intake.autoStart from the working-tree project config');
|
|
514
|
+
expect(e.starts).toEqual([]);
|
|
515
|
+
});
|
|
516
|
+
it('on an idle tick it starts one run --next and reports the item and the exit code (JSON and text)', async () => {
|
|
517
|
+
const e = engine({ exitCode: 3, next: 1 });
|
|
518
|
+
const budget = [];
|
|
519
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { budget: (r) => { budget.push(r); return null; } }))).toBe(0);
|
|
520
|
+
expect(ticks()[0]).toMatchObject({ idle: true, autoStart: { started: true, next: 1, exitCode: 3, message: 'parked at awaiting-human: merge' } });
|
|
521
|
+
expect([e.starts, budget]).toEqual([[root], [root]]);
|
|
522
|
+
const audit = readFileSync(pollAuditPath(root), 'utf8').trim().split('\n').map((l) => JSON.parse(l));
|
|
523
|
+
expect(audit.filter((a) => a.kind.startsWith('auto-start')).map((a) => [a.kind, a.next, a.exitCode])).toEqual([['auto-start', undefined, undefined], ['auto-start-done', 1, 3]]);
|
|
524
|
+
out.length = 0;
|
|
525
|
+
expect(await poll(scripted().exec, { once: true }, on(engine({ next: null })))).toBe(0);
|
|
526
|
+
expect(out.join('\n')).toContain('auto-start: omg factory run --next found nothing runnable, exit 0');
|
|
527
|
+
out.length = 0;
|
|
528
|
+
expect(await poll(scripted().exec, { once: true }, on(e))).toBe(0);
|
|
529
|
+
expect(out.join('\n')).toContain('auto-start: omg factory run --next took #1, exit 3: parked at awaiting-human: merge');
|
|
530
|
+
});
|
|
531
|
+
it('starts only on an idle tick with a successful, unthrottled intake read', async () => {
|
|
532
|
+
const e = engine();
|
|
533
|
+
save(moveTo(issueRun(5), 'implement', 'plan ok', T0));
|
|
534
|
+
await poll(scripted().exec, { once: true, json: true, prs: false }, on(e));
|
|
535
|
+
expect(ticks().at(-1)).toMatchObject({ idle: false, autoStart: { started: false, reason: 'a run is in flight' } });
|
|
536
|
+
rmSync(factoryStatePath(root, 5));
|
|
537
|
+
await poll(scripted().exec, { once: true, json: true, intake: false }, on(e));
|
|
538
|
+
expect(ticks().at(-1).autoStart).toEqual({ started: false, reason: 'no intake read this tick (--no-intake)' });
|
|
539
|
+
await poll(scripted().exec, { once: true, json: true }, on(e, { projectConfig: () => undefined }));
|
|
540
|
+
expect(ticks().at(-1).autoStart).toEqual({ started: false, reason: 'no intake read this tick (no intake source (factory.intake.source))' });
|
|
541
|
+
// A parked run keeps the tick idle; its PR read is throttled.
|
|
542
|
+
seedParked();
|
|
543
|
+
const limited = scripted({ onCall: (cmd, args) => (cmd === 'gh' && args[0] === 'pr' && args[1] === 'view' ? { status: 1, stdout: '', stderr: 'HTTP 429: Too Many Requests' } : undefined) });
|
|
544
|
+
await poll(limited.exec, { once: true, json: true }, on(e));
|
|
545
|
+
expect(ticks().at(-1)).toMatchObject({ idle: true, intake: { ran: true }, rateLimited: true, autoStart: { started: false, reason: 'the tick was rate-limited' } });
|
|
546
|
+
expect(e.starts).toEqual([]);
|
|
547
|
+
});
|
|
548
|
+
it('not started while the run lock is held or without budget headroom', async () => {
|
|
549
|
+
const e = engine();
|
|
550
|
+
const owner = () => writeFileSync(factoryRepoOwnerPath(root), JSON.stringify({ pid: 99, hostname: hostname(), startedAt: T0, run: '4' }));
|
|
551
|
+
const busy = { pid: 2, hostname: hostname(), isAlive: (pid) => pid === 99 };
|
|
552
|
+
mkdirSync(factoryStateDir(root), { recursive: true });
|
|
553
|
+
owner();
|
|
554
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { owner: busy }))).toBe(0);
|
|
555
|
+
expect(ticks().at(-1).autoStart).toEqual({ started: false, reason: 'the run lock is held: an engine runs in this repository' });
|
|
556
|
+
rmSync(factoryRepoOwnerPath(root));
|
|
557
|
+
// idle is advisory: an engine that claims the lock during the board step (after the idle check; the items are
|
|
558
|
+
// seen, so the board reads first) makes poll's own claim refuse at start time; the tick is skipped, not failed.
|
|
559
|
+
let views = 0;
|
|
560
|
+
const late = scripted({ onCall: (cmd, args) => { if (cmd === 'gh' && args[0] === 'issue' && args[1] === 'view' && ++views === 1)
|
|
561
|
+
owner(); return undefined; } });
|
|
562
|
+
expect(await poll(late.exec, { once: true, json: true, prs: false }, on(e, { owner: busy }))).toBe(0);
|
|
563
|
+
expect(ticks().at(-1)).toMatchObject({ idle: true, errors: [], autoStart: { started: false, reason: expect.stringMatching(/^the run lock is held: another omg factory engine runs in this repository \(4, pid 99/) } });
|
|
564
|
+
expect(JSON.parse(readFileSync(factoryRepoOwnerPath(root), 'utf8'))).toMatchObject({ pid: 99, run: '4' });
|
|
565
|
+
rmSync(factoryRepoOwnerPath(root));
|
|
566
|
+
// A successful claim is released before the start, so the run's own claim (the binding one) can take it.
|
|
567
|
+
let held = 'unread';
|
|
568
|
+
const check = async () => { held = existsSync(factoryRepoOwnerPath(root)); return { exitCode: 0, parsed: true, next: null, message: 'nothing runnable' }; };
|
|
569
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on({ autoStart: check }))).toBe(0);
|
|
570
|
+
expect([ticks().at(-1).autoStart.started, held]).toEqual([true, false]);
|
|
571
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { budget: () => 'the AI credits used (9.00) reached budget.softLimit 5' }))).toBe(0);
|
|
572
|
+
expect(ticks().at(-1).autoStart).toEqual({ started: false, reason: 'the AI credits used (9.00) reached budget.softLimit 5' });
|
|
573
|
+
expect(e.starts).toEqual([]);
|
|
574
|
+
});
|
|
575
|
+
it('a broken run state (failing its integrity check) is never idle and starts nothing', async () => {
|
|
576
|
+
const e = engine();
|
|
577
|
+
const s = moveTo(issueRun(6), 'implement', 'plan ok', T0);
|
|
578
|
+
mkdirSync(factoryStateDir(root), { recursive: true });
|
|
579
|
+
writeFileSync(factoryStatePath(root, 6), JSON.stringify({ ...s, phase: 'merged', integrity: computeFactoryIntegrity(s) }));
|
|
580
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false }, on(e))).toBe(0);
|
|
581
|
+
expect(ticks().at(-1)).toMatchObject({ idle: false, autoStart: { started: false, reason: 'a run state is unreadable or fails its integrity check' } });
|
|
582
|
+
writeFileSync(factoryStatePath(root, 6), '{ not json');
|
|
583
|
+
expect(await poll(scripted().exec, { once: true, json: true, prs: false }, on(e))).toBe(0);
|
|
584
|
+
expect(ticks().at(-1)).toMatchObject({ idle: false, autoStart: { started: false, reason: 'a run state is unreadable or fails its integrity check' } });
|
|
585
|
+
expect(e.starts).toEqual([]);
|
|
586
|
+
});
|
|
587
|
+
it('at most one run per tick: the started run in flight keeps the next tick from starting another', async () => {
|
|
588
|
+
const e = engine({ after: () => save(moveTo(issueRun(1), 'implement', 'lane lost', T0)) });
|
|
589
|
+
let stop = () => { };
|
|
590
|
+
let sleeps = 0;
|
|
591
|
+
const code = await poll(scripted().exec, { json: true, prs: false }, on(e, {
|
|
592
|
+
onStop: (h) => { stop = h; return () => { }; },
|
|
593
|
+
sleep: async () => { if (++sleeps === 2)
|
|
594
|
+
stop(); },
|
|
595
|
+
}));
|
|
596
|
+
expect(code).toBe(0);
|
|
597
|
+
expect(e.starts).toHaveLength(1);
|
|
598
|
+
expect(ticks().map((t) => t.autoStart.started)).toEqual([true, false]);
|
|
599
|
+
expect(ticks()[1].autoStart).toEqual({ started: false, reason: 'a run is in flight' });
|
|
600
|
+
});
|
|
601
|
+
it('an unreadable outcome renders "outcome unreadable", never "found nothing runnable", and the audit logs no next', async () => {
|
|
602
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(engine({ parsed: false, next: null, exitCode: 1 })))).toBe(0);
|
|
603
|
+
expect(ticks()[0].autoStart).toMatchObject({ started: true, parsed: false, next: null, exitCode: 1 });
|
|
604
|
+
const done = readFileSync(pollAuditPath(root), 'utf8').trim().split('\n').map((l) => JSON.parse(l)).filter((a) => a.kind === 'auto-start-done');
|
|
605
|
+
expect(done).toHaveLength(1);
|
|
606
|
+
expect(done[0]).toMatchObject({ outcome: 'unreadable', exitCode: 1 });
|
|
607
|
+
expect('next' in done[0]).toBe(false);
|
|
608
|
+
out.length = 0;
|
|
609
|
+
expect(await poll(scripted().exec, { once: true }, on(engine({ parsed: false, next: null, exitCode: 1 })))).toBe(0);
|
|
610
|
+
expect(out.join('\n')).toContain('auto-start: omg factory run --next outcome unreadable, exit 1');
|
|
611
|
+
expect(out.join('\n')).not.toContain('found nothing runnable');
|
|
612
|
+
});
|
|
613
|
+
describe('defaultPollBudget (fails closed)', () => {
|
|
614
|
+
const ledger = (credits) => {
|
|
615
|
+
mkdirSync(join(root, '.omg', 'state'), { recursive: true });
|
|
616
|
+
writeFileSync(join(root, '.omg', 'state', 'budget-ledger.json'), JSON.stringify({
|
|
617
|
+
version: 1, since: T0, sessions: { s1: { credits, premiumRequests: 0, source: 'measured', detail: 'x', updatedAt: T0 } }, teams: {}, notified: {},
|
|
618
|
+
}));
|
|
619
|
+
};
|
|
620
|
+
const limit = (softLimit) => ({ budget: { softLimit } });
|
|
621
|
+
const gate = (l, env = {}) => defaultPollBudget(() => ({ trustedUser: l.trustedUser, base: l.base, project: l.project }), env)(root);
|
|
622
|
+
it('reads budget.softLimit from the user config, then the base branch', () => {
|
|
623
|
+
ledger(4.5);
|
|
624
|
+
expect(gate({ trustedUser: limit(5) })).toBeNull();
|
|
625
|
+
expect(gate({ base: limit(5) })).toBeNull();
|
|
626
|
+
ledger(5);
|
|
627
|
+
expect(gate({ trustedUser: limit(5) })).toMatch(/AI credits used \(5\.00\) reached budget\.softLimit 5/);
|
|
628
|
+
expect(gate({ base: limit(5) })).toMatch(/reached budget\.softLimit 5/);
|
|
629
|
+
// The user config wins over the base branch.
|
|
630
|
+
expect(gate({ trustedUser: limit(10), base: limit(5) })).toBeNull();
|
|
631
|
+
});
|
|
632
|
+
it('a project layer raising the limit does not raise it; lowering it lowers it', () => {
|
|
633
|
+
ledger(5);
|
|
634
|
+
expect(gate({ trustedUser: limit(5), project: limit(100) })).toMatch(/reached budget\.softLimit 5 /);
|
|
635
|
+
expect(gate({ base: limit(5), project: limit(100) })).toMatch(/reached budget\.softLimit 5 /);
|
|
636
|
+
ledger(3);
|
|
637
|
+
expect(gate({ trustedUser: limit(5) })).toBeNull();
|
|
638
|
+
expect(gate({ trustedUser: limit(5), project: limit(2) })).toMatch(/AI credits used \(3\.00\) reached budget\.softLimit 2 /);
|
|
639
|
+
});
|
|
640
|
+
it('no trusted softLimit blocks the start, even with a project value', () => {
|
|
641
|
+
ledger(0);
|
|
642
|
+
expect(gate({})).toBe('autoStart needs budget.softLimit (user config or base branch)');
|
|
643
|
+
expect(gate({ project: limit(5) })).toBe('autoStart needs budget.softLimit (user config or base branch)');
|
|
644
|
+
expect(gate({ trustedUser: limit(-1) })).toMatch(/user config: budget\.softLimit must be a positive number/);
|
|
645
|
+
});
|
|
646
|
+
it('the env kill switch blocks the start instead of opening the gate', () => {
|
|
647
|
+
ledger(0);
|
|
648
|
+
expect(gate({ trustedUser: limit(5) }, { OMC_BUDGET_GOVERNOR: '0' })).toBe('the budget governor is off (OMC_BUDGET_GOVERNOR=0); autoStart needs it on');
|
|
649
|
+
expect(gate({}, { OMC_BUDGET_GOVERNOR: '0' })).toMatch(/OMC_BUDGET_GOVERNOR=0/);
|
|
650
|
+
});
|
|
651
|
+
it('an invalid or corrupt ledger blocks the start; a missing one is empty', () => {
|
|
652
|
+
expect(gate({ trustedUser: limit(5) })).toBeNull();
|
|
653
|
+
mkdirSync(join(root, '.omg', 'state'), { recursive: true });
|
|
654
|
+
writeFileSync(join(root, '.omg', 'state', 'budget-ledger.json'), '{ corrupt');
|
|
655
|
+
expect(gate({ trustedUser: limit(5) })).toMatch(/^the budget ledger is unreadable .*budget-ledger\.json/);
|
|
656
|
+
writeFileSync(join(root, '.omg', 'state', 'budget-ledger.json'), JSON.stringify({ version: 2, since: T0 }));
|
|
657
|
+
expect(gate({ trustedUser: limit(5) })).toMatch(/^the budget ledger is unreadable/);
|
|
658
|
+
});
|
|
659
|
+
it('the factory.run limits must resolve', () => {
|
|
660
|
+
ledger(0);
|
|
661
|
+
expect(gate({ trustedUser: { ...limit(5), factory: { run: { laneMaxCredits: 0 } } } })).toMatch(/laneMaxCredits 0 is below the Copilot CLI minimum/);
|
|
662
|
+
});
|
|
663
|
+
it('runFactoryPoll wires it by default: the working tree alone cannot satisfy it', async () => {
|
|
664
|
+
const e = engine();
|
|
665
|
+
expect(await poll(scripted().exec, { once: true, json: true }, on(e, { budget: undefined, projectConfig: () => ({ ...LABEL, ...limit(5) }) }))).toBe(0);
|
|
666
|
+
expect(ticks()[0].autoStart).toEqual({ started: false, reason: 'autoStart needs budget.softLimit (user config or base branch)' });
|
|
667
|
+
expect(e.starts).toEqual([]);
|
|
668
|
+
});
|
|
669
|
+
});
|
|
670
|
+
it('contract: the real `omg factory run --next --json` documents (next, message, error) parse', () => {
|
|
671
|
+
const state = issueRun(7);
|
|
672
|
+
const outcome = { exitCode: 3, phase: state.phase, state, message: 'parked at awaiting-human: merge', mergeCommand: 'gh pr merge 9' };
|
|
673
|
+
expect(parseRunNextOutcome(factoryRunJson.outcome(outcome, 7))).toEqual({ parsed: true, next: 7, message: 'parked at awaiting-human: merge' });
|
|
674
|
+
expect(parseRunNextOutcome(factoryRunJson.nothingRunnable({ runnable: 0, 'needs-plan': 1, blocked: 2 }))).toEqual({ parsed: true, next: null, message: 'nothing runnable' });
|
|
675
|
+
expect(parseRunNextOutcome(factoryRunJson.error('--next needs the forge config (omg factory setup)'))).toEqual({ parsed: true, next: null, message: '--next needs the forge config (omg factory setup)' });
|
|
676
|
+
// Truncated or foreign stdout is not an outcome.
|
|
677
|
+
expect(parseRunNextOutcome(factoryRunJson.outcome(outcome, 7).slice(0, 40)).parsed).toBe(false);
|
|
678
|
+
expect(parseRunNextOutcome('')).toEqual({ parsed: false, next: null, message: 'no outcome on stdout' });
|
|
679
|
+
expect(parseRunNextOutcome('[1]').parsed).toBe(false);
|
|
680
|
+
});
|
|
681
|
+
it('autoStartTimeoutMs: the run wall time plus one lane and one gates phase, plus 30 min', () => {
|
|
682
|
+
expect(autoStartTimeoutMs({ ...DEFAULT_FACTORY_RUN_LIMITS })).toBe((240 + 30 + 90 + 30) * MIN);
|
|
683
|
+
expect(autoStartTimeoutMs({ ...DEFAULT_FACTORY_RUN_LIMITS, maxRunWallTimeMs: 60 * MIN, laneTimeoutMs: 10 * MIN, gatesTimeoutMs: 20 * MIN })).toBe(120 * MIN);
|
|
684
|
+
});
|
|
685
|
+
describe('defaultPollAutoStart', () => {
|
|
686
|
+
const script = () => join(root, 'fake-omg.cjs');
|
|
687
|
+
it('a child `<entry> factory run --next --json` in the root; the outcome is read from its stdout', async () => {
|
|
688
|
+
writeFileSync(script(), 'process.stdout.write(JSON.stringify({ ok: false, next: 7, message: process.argv.slice(2).join(" ") + " @ " + process.cwd() }, null, 2)); process.exit(3);\n');
|
|
689
|
+
const r = await defaultPollAutoStart(script())({ root });
|
|
690
|
+
expect(r).toMatchObject({ exitCode: 3, parsed: true, next: 7 });
|
|
691
|
+
expect(r.message).toMatch(/^factory run --next --json @ /);
|
|
692
|
+
writeFileSync(script(), 'process.stdout.write("garbage"); process.exit(1);\n');
|
|
693
|
+
expect(await defaultPollAutoStart(script())({ root })).toEqual({ exitCode: 1, parsed: false, next: null, message: 'no outcome on stdout' });
|
|
694
|
+
expect(await defaultPollAutoStart('')({ root })).toMatchObject({ exitCode: 1, parsed: false, next: null });
|
|
695
|
+
});
|
|
696
|
+
it('a truncated stdout is unreadable', async () => {
|
|
697
|
+
writeFileSync(script(), 'const big = "x".repeat(5 * 1024 * 1024); process.stdout.write(JSON.stringify({ ok: true, next: 3, message: big }), () => process.exit(0));\n');
|
|
698
|
+
expect(await defaultPollAutoStart(script())({ root })).toMatchObject({ exitCode: 0, parsed: false, next: null, message: expect.stringMatching(/exceeded/) });
|
|
699
|
+
});
|
|
700
|
+
it('a spawn error resolves (never rejects)', async () => {
|
|
701
|
+
writeFileSync(script(), 'process.exit(0);\n');
|
|
702
|
+
expect(await defaultPollAutoStart(script())({ root: join(root, 'missing') })).toMatchObject({ exitCode: 1, parsed: false, next: null, message: expect.stringMatching(/^omg factory run --next did not start: /) });
|
|
703
|
+
});
|
|
704
|
+
it('past its limit the child gets SIGTERM, then SIGKILL after the grace', async () => {
|
|
705
|
+
// It ignores SIGTERM where it can (POSIX), so the grace's SIGKILL ends it; on Windows SIGTERM already ends it.
|
|
706
|
+
writeFileSync(script(), 'process.on("SIGTERM", () => {}); setInterval(() => {}, 1000);\n');
|
|
707
|
+
const r = await defaultPollAutoStart(script(), { timeoutMs: () => 300, graceMs: 300 })({ root });
|
|
708
|
+
expect(r).toMatchObject({ exitCode: 1, parsed: false, next: null, message: expect.stringMatching(/ran past its 0 min limit and was stopped \(ended by SIG(?:TERM|KILL)\)$/) });
|
|
709
|
+
});
|
|
710
|
+
it.skipIf(process.platform === 'win32')('past its limit the SIGKILL reaches the child\'s own children (the process group)', async () => {
|
|
711
|
+
// The child ignores SIGTERM and leaves a grandchild that does too; only the group's SIGKILL ends the grandchild.
|
|
712
|
+
const pidFile = join(root, 'grandchild.pid');
|
|
713
|
+
writeFileSync(script(), `process.on("SIGTERM", () => {});
|
|
714
|
+
const { spawn } = require("child_process");
|
|
715
|
+
const g = spawn(process.execPath, ["-e", "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000)"], { stdio: "ignore" });
|
|
716
|
+
require("fs").writeFileSync(${JSON.stringify(pidFile)}, String(g.pid));
|
|
717
|
+
setInterval(() => {}, 1000);
|
|
718
|
+
`);
|
|
719
|
+
const r = await defaultPollAutoStart(script(), { timeoutMs: () => 1500, graceMs: 300 })({ root });
|
|
720
|
+
expect(r.message).toMatch(/was stopped \(ended by SIGKILL\)$/);
|
|
721
|
+
const pid = Number(readFileSync(pidFile, 'utf8'));
|
|
722
|
+
let alive = true;
|
|
723
|
+
for (let i = 0; i < 20 && alive; i++) {
|
|
724
|
+
try {
|
|
725
|
+
process.kill(pid, 0);
|
|
726
|
+
await new Promise((res) => setTimeout(res, 100));
|
|
727
|
+
}
|
|
728
|
+
catch {
|
|
729
|
+
alive = false;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (alive)
|
|
733
|
+
process.kill(pid, 'SIGKILL');
|
|
734
|
+
expect(alive).toBe(false);
|
|
735
|
+
});
|
|
736
|
+
it('forwards poll\'s stop signal to the child and reports a child ended by a signal', async () => {
|
|
737
|
+
writeFileSync(script(), 'setInterval(() => {}, 1000); process.stdout.write("");\n');
|
|
738
|
+
let forward;
|
|
739
|
+
let unsubscribed = false;
|
|
740
|
+
const run = defaultPollAutoStart(script(), { signals: (f) => { forward = f; return () => { unsubscribed = true; }; } })({ root });
|
|
741
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
742
|
+
forward('SIGTERM');
|
|
743
|
+
expect(await run).toEqual({ exitCode: 1, parsed: false, next: null, message: 'no outcome on stdout (ended by SIGTERM)' });
|
|
744
|
+
expect(unsubscribed).toBe(true);
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
});
|
|
748
|
+
//# sourceMappingURL=poll.test.js.map
|