Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Revert "Port the factory loop from the factory-control-plane branch (#1008)" (#1026)

Paul Caprioloc25b35898cbf
diff --git a/plugins/admin/public/index.html b/plugins/admin/public/index.html
index 5cf99612f..290b8f31e 100644
--- a/plugins/admin/public/index.html
+++ b/plugins/admin/public/index.html
@@ -5667,61 +5667,6 @@
><span class="status" id="st-turn-wall-clock"></span>
</div>
</section>
- <section class="card sv-customize setting-row hidden" id="card-factory-config">
- <div class="head">
- <h2>Software factory</h2>
- <p>Credentials are the service credentials factory-linear and factory-github in the keychain.</p>
- </div>
- <div class="body">
- <label for="factory-forge">forge</label>
- <select id="factory-forge">
- <option value="github">github</option>
- <option value="gitlab">gitlab</option>
- </select>
- <label for="factory-publish-project" style="margin-top: 12px">publishProject</label>
- <input type="text" id="factory-publish-project" />
- <label for="factory-target-branch" style="margin-top: 12px">targetBranch</label>
- <input type="text" id="factory-target-branch" />
- <label for="factory-repo-clone-url" style="margin-top: 12px">repoCloneUrl</label>
- <input type="text" id="factory-repo-clone-url" />
- <label for="factory-repo-setup-cmd" style="margin-top: 12px">repoSetupCmd (optional)</label>
- <textarea id="factory-repo-setup-cmd" rows="3"></textarea>
- <label for="factory-linear-team-id" style="margin-top: 12px">linearTeamId</label>
- <input type="text" id="factory-linear-team-id" />
- <label for="factory-source-app-dirs" style="margin-top: 12px">sourceAppDirs</label>
- <input type="text" id="factory-source-app-dirs" />
- <label for="factory-source-test-re" style="margin-top: 12px">sourceTestRe</label>
- <input type="text" id="factory-source-test-re" />
- <label for="factory-verify-tests-cmd" style="margin-top: 12px">verifyTestsCmd</label>
- <input type="text" id="factory-verify-tests-cmd" />
- <label for="factory-verify-test-file-cmd" style="margin-top: 12px">verifyTestFileCmd</label>
- <input type="text" id="factory-verify-test-file-cmd" />
- <label for="factory-verify-lint-cmd" style="margin-top: 12px">verifyLintCmd</label>
- <input type="text" id="factory-verify-lint-cmd" />
- <label for="factory-proof-start-cmd" style="margin-top: 12px">proofStartCmd (optional)</label>
- <input type="text" id="factory-proof-start-cmd" />
- <label for="factory-proof-base-url-cmd" style="margin-top: 12px">proofBaseUrlCmd (optional)</label>
- <input type="text" id="factory-proof-base-url-cmd" />
- <label for="factory-slack-channel" style="margin-top: 12px">slackChannel (optional)</label>
- <input type="text" id="factory-slack-channel" />
- <div style="display: flex; flex-direction: column; gap: 8px; margin-top: 12px">
- <label class="setting-toggle">
- <input type="checkbox" id="factory-bugbot-required" />
- <span class="setting-switch" aria-hidden="true"></span>
- <span class="setting-copy"><strong>bugbotRequired</strong></span>
- </label>
- <label class="setting-toggle">
- <input type="checkbox" id="factory-followups-enabled" />
- <span class="setting-switch" aria-hidden="true"></span>
- <span class="setting-copy"><strong>followupsEnabled</strong></span>
- </label>
- </div>
- </div>
- <div class="foot">
- <button class="primary" data-save="factory-config">Apply</button
- ><button id="factory-config-clear">Clear</button><span class="status" id="st-factory-config"></span>
- </div>
- </section>
<section class="card sv-customize hidden" id="card-feature-flags">
<div class="head">
<h2>Feature flags</h2>
@@ -6244,7 +6189,6 @@
$("card-people-directory"),
channelDefaults,
$("card-turn-wall-clock"),
- $("card-factory-config"),
$("card-feature-flags"),
$("card-service-credentials"),
keychains,
@@ -8218,27 +8162,6 @@
const showTurnWallClock = scope.startsWith("org:") && "turnWallClockSec" in r.data;
$("card-turn-wall-clock").classList.toggle("hidden", !showTurnWallClock);
if (showTurnWallClock) $("turn-wall-clock").value = r.data.turnWallClockSec ?? "";
- const showFactoryConfig = scope.startsWith("org:") && "factoryConfig" in r.data;
- $("card-factory-config").classList.toggle("hidden", !showFactoryConfig);
- if (showFactoryConfig) {
- const f = r.data.factoryConfig || {};
- $("factory-forge").value = f.forge || "github";
- $("factory-publish-project").value = f.publishProject || "";
- $("factory-target-branch").value = f.targetBranch || "";
- $("factory-repo-clone-url").value = f.repoCloneUrl || "";
- $("factory-repo-setup-cmd").value = f.repoSetupCmd || "";
- $("factory-linear-team-id").value = f.linearTeamId || "";
- $("factory-source-app-dirs").value = f.sourceAppDirs || "";
- $("factory-source-test-re").value = f.sourceTestRe || "";
- $("factory-verify-tests-cmd").value = f.verifyTestsCmd || "";
- $("factory-verify-test-file-cmd").value = f.verifyTestFileCmd || "";
- $("factory-verify-lint-cmd").value = f.verifyLintCmd || "";
- $("factory-proof-start-cmd").value = f.proofStartCmd || "";
- $("factory-proof-base-url-cmd").value = f.proofBaseUrlCmd || "";
- $("factory-slack-channel").value = f.slackChannel || "";
- $("factory-bugbot-required").checked = !!f.bugbotRequired;
- $("factory-followups-enabled").checked = !!f.followupsEnabled;
- }
serviceCredDirectory = r.data.directoryMembers || [];
const options = $("sc-people-options");
options.textContent = "";
@@ -10514,30 +10437,6 @@
orgName: $("branding-org-name").value.trim(),
}),
"turn-wall-clock": () => ({ sec: $("turn-wall-clock").value.trim() }),
- "factory-config": () => {
- const opt = (id, key) => {
- const v = $(id).value.trim();
- return v ? { [key]: v } : {};
- };
- return {
- forge: $("factory-forge").value,
- publishProject: $("factory-publish-project").value.trim(),
- targetBranch: $("factory-target-branch").value.trim(),
- repoCloneUrl: $("factory-repo-clone-url").value.trim(),
- linearTeamId: $("factory-linear-team-id").value.trim(),
- sourceAppDirs: $("factory-source-app-dirs").value.trim(),
- sourceTestRe: $("factory-source-test-re").value.trim(),
- verifyTestsCmd: $("factory-verify-tests-cmd").value.trim(),
- verifyTestFileCmd: $("factory-verify-test-file-cmd").value.trim(),
- verifyLintCmd: $("factory-verify-lint-cmd").value.trim(),
- ...opt("factory-repo-setup-cmd", "repoSetupCmd"),
- ...opt("factory-proof-start-cmd", "proofStartCmd"),
- ...opt("factory-proof-base-url-cmd", "proofBaseUrlCmd"),
- ...opt("factory-slack-channel", "slackChannel"),
- bugbotRequired: $("factory-bugbot-required").checked,
- followupsEnabled: $("factory-followups-enabled").checked,
- };
- },
};
const SAVE_ST = {
"security-posture": "st-security-posture",
@@ -10554,7 +10453,6 @@
"people-directory-url": "st-people-directory-url",
"ack-emoji": "st-ack-emoji",
"turn-wall-clock": "st-turn-wall-clock",
- "factory-config": "st-factory-config",
branding: "st-branding",
};
const SAVE_RELOADS = new Set([
@@ -10565,7 +10463,6 @@
"people-directory-url",
"ack-emoji",
"turn-wall-clock",
- "factory-config",
"branding",
]);
const sectionSnapshots = new Map();
@@ -10718,16 +10615,6 @@
}
};
});
- $("factory-config-clear").onclick = async () => {
- if (!confirm("Clear the factory configuration?")) return;
- const r = await api("PUT", "/api/scopes/" + encodeURIComponent(scope) + "/factory-config", { reset: true });
- if (!r.ok) {
- setStatus("st-factory-config", r.data?.message || "Clear failed.", "err");
- return;
- }
- await loadScope();
- setStatus("st-factory-config", "Cleared", "ok");
- };
$("view-governance").addEventListener("input", (e) => {
if (!(
e.target instanceof HTMLInputElement ||
diff --git a/src/api/routes/admin-resources.ts b/src/api/routes/admin-resources.ts
index 78057f341..3e542603a 100644
--- a/src/api/routes/admin-resources.ts
+++ b/src/api/routes/admin-resources.ts
@@ -21,7 +21,6 @@ import {
} from "../../model/pi-models.ts";
import { resolveRuntimeChoiceDurable } from "../../harness/harness-router.ts";
import { sanitizeBranding } from "../../resolution/branding.ts";
-import type { FactoryConfig } from "../../resolution/config-store.ts";
import {
credentialInjectionError,
isValidCredentialSlug,
@@ -156,57 +155,6 @@ function channelContainer(scope: string): string | undefined {
return ref && (kind === "channel" || kind === "group") ? ref : undefined;
}
-const REQUIRED_FACTORY_STRINGS = [
- "publishProject",
- "targetBranch",
- "repoCloneUrl",
- "linearTeamId",
- "sourceAppDirs",
- "sourceTestRe",
- "verifyTestsCmd",
- "verifyTestFileCmd",
- "verifyLintCmd",
-] as const;
-const OPTIONAL_FACTORY_STRINGS = ["repoSetupCmd", "proofStartCmd", "proofBaseUrlCmd", "slackChannel"] as const;
-const FACTORY_BOOLEANS = ["bugbotRequired", "followupsEnabled"] as const;
-
-function parseFactoryConfig(body: unknown): { value: FactoryConfig } | { error: string } {
- if (!body || typeof body !== "object" || Array.isArray(body)) {
- return { error: "factory-config: body must be an object" };
- }
- const raw = body as Record<string, unknown>;
- const allowed = new Set<string>([
- "forge",
- ...REQUIRED_FACTORY_STRINGS,
- ...OPTIONAL_FACTORY_STRINGS,
- ...FACTORY_BOOLEANS,
- ]);
- for (const key of Object.keys(raw)) {
- if (!allowed.has(key)) return { error: `factory-config: unknown key ${key}` };
- }
- if (raw.forge !== "github" && raw.forge !== "gitlab") {
- return { error: 'factory-config: forge must be "github" or "gitlab"' };
- }
- const value: Partial<FactoryConfig> = { forge: raw.forge };
- for (const key of REQUIRED_FACTORY_STRINGS) {
- const v = raw[key];
- if (typeof v !== "string" || !v.trim()) return { error: `factory-config: ${key} must be a non-empty string` };
- value[key] = v.trim();
- }
- for (const key of OPTIONAL_FACTORY_STRINGS) {
- const v = raw[key];
- if (v === undefined) continue;
- if (typeof v !== "string") return { error: `factory-config: ${key} must be a string` };
- value[key] = v;
- }
- for (const key of FACTORY_BOOLEANS) {
- const v = raw[key];
- if (typeof v !== "boolean") return { error: `factory-config: ${key} must be a boolean` };
- value[key] = v;
- }
- return { value: value as FactoryConfig };
-}
-
export const ADMIN_RESOURCES: readonly AdminResource[] = [
{
id: "security-posture",
@@ -824,29 +772,6 @@ export const ADMIN_RESOURCES: readonly AdminResource[] = [
return { ok: true };
},
},
- {
- id: "factory-config",
- kind: "custom",
- target: "org",
- clearable: true,
- label: "The software factory: which repository and Linear team it works, and how it verifies.",
- readKey: "factoryConfig",
- get: (deps) => deps.config!.getFactoryConfig(),
- apply: async (ctx, _actor, scope) => {
- const bad = orgOnly(scope, "the factory config is org-wide");
- if (bad) return bad;
- const body = ctx.body;
- const isRecord = !!body && typeof body === "object" && !Array.isArray(body);
- if (isRecord && Object.keys(body).length === 1 && (body as { reset?: unknown }).reset === true) {
- ctx.deps.config!.setFactoryConfig(null);
- return { ok: true };
- }
- const parsed = parseFactoryConfig(body);
- if ("error" in parsed) return parsed;
- ctx.deps.config!.setFactoryConfig(parsed.value);
- return { ok: true };
- },
- },
{
id: "turn-wall-clock",
kind: "string",
diff --git a/src/api/routes/loops.ts b/src/api/routes/loops.ts
index fa64701d2..5f2596b46 100644
--- a/src/api/routes/loops.ts
+++ b/src/api/routes/loops.ts
@@ -460,13 +460,9 @@ async function decideOutput(ctx: ApiCtx): Promise<void> {
if (b.decision === "return" || b.decision === "returned") {
if (!note)
return sendJson(ctx.res, 400, { error: "bad_request", message: "a return needs a note for the next attempt" });
- try {
- const returned = await deps.fire.returnOutput(loop.id, outputId, acting.actorId, note);
- if (!returned) return decisionMissing();
- return sendJson(ctx.res, 200, { output: returned });
- } catch (e) {
- return sendJson(ctx.res, 502, { error: "return_failed", message: errMessage(e) });
- }
+ const returned = await deps.fire.returnOutput(loop.id, outputId, acting.actorId, note);
+ if (!returned) return decisionMissing();
+ return sendJson(ctx.res, 200, { output: returned });
}
return sendJson(ctx.res, 400, { error: "bad_request", message: 'decision must be "shipped" or "returned"' });
}
diff --git a/src/loops/factory/contract.ts b/src/loops/factory/contract.ts
deleted file mode 100644
index 82375d15d..000000000
--- a/src/loops/factory/contract.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-import type { CapturedArtifact } from "../runner.ts";
-
-export interface ContractInput {
- stdout: string;
- sourceKey: string;
- forge: "github" | "gitlab";
- publishProject: string;
-}
-
-function prUrl(forge: ContractInput["forge"], publishProject: string, mr: string): string {
- return forge === "github"
- ? `https://github.com/${publishProject}/pull/${mr}`
- : `https://gitlab.com/${publishProject.replace(/%2F/gi, "/")}/-/merge_requests/${mr}`;
-}
-
-export function parseFactoryContract(input: ContractInput): CapturedArtifact[] {
- let branch: string | undefined;
- let mr: string | undefined;
- let alreadyFixed = false;
- let commit: string | undefined;
- let evidence: string | undefined;
-
- for (const line of input.stdout.split(/\r?\n/)) {
- if (line === "ALREADY_FIXED:true") alreadyFixed = true;
-
- const branchMatch = /^BRANCH:\s*(.+?)\s*$/.exec(line);
- const branchValue = branchMatch?.[1]?.trim();
- if (branchValue) branch = branchValue;
-
- const mrMatch = /^MR:\s*!?(\d+)\s*$/.exec(line);
- if (mrMatch?.[1] !== undefined) mr = mrMatch[1];
-
- const commitMatch = /^ALREADY_FIXED_COMMIT:\s*([0-9a-f]{7,40})\s*$/i.exec(line);
- if (commitMatch?.[1] !== undefined) commit = commitMatch[1];
-
- const evidenceMatch = /^ALREADY_FIXED_EVIDENCE:\s*(.*)$/.exec(line);
- const evidenceValue = evidenceMatch?.[1]?.trim();
- if (evidenceValue) evidence = evidenceValue;
- }
-
- if (alreadyFixed) {
- const summary = evidence ?? (commit !== undefined ? `commit ${commit}` : undefined);
- return [
- {
- shipAction: "close_already_fixed",
- title: `${input.sourceKey}: already fixed`,
- capturedBy: "classifier",
- ...(summary !== undefined ? { summary } : {}),
- ...(mr !== undefined ? { externalRef: prUrl(input.forge, input.publishProject, mr) } : {}),
- },
- ];
- }
-
- if (branch !== undefined && mr !== undefined) {
- return [
- {
- shipAction: "open_pr",
- title: `${input.sourceKey}: pull request #${mr}`,
- label: branch,
- externalRef: prUrl(input.forge, input.publishProject, mr),
- capturedBy: "classifier",
- },
- ];
- }
-
- return [];
-}
diff --git a/src/loops/factory/credentials.ts b/src/loops/factory/credentials.ts
deleted file mode 100644
index f53fdea2f..000000000
--- a/src/loops/factory/credentials.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import type { DecryptedServiceCredential, ServiceCredentialReader } from "../../credentials/keychain.ts";
-import type { ScopeId } from "../../types.ts";
-
-export const FACTORY_LINEAR_SLUG = "factory-linear";
-export const FACTORY_GITHUB_SLUG = "factory-github";
-
-export type FactoryCredentials =
- { ok: true; linearApiKey: string; githubToken: string } | { ok: false; missing: string[] };
-
-function usableSecret(rec: DecryptedServiceCredential | null): string | null {
- if (!rec || !rec.enabled) return null;
- const secret = rec.secret.trim();
- return secret === "" ? null : secret;
-}
-
-export async function readFactoryCredentials(
- reader: ServiceCredentialReader,
- orgScopeId: ScopeId,
-): Promise<FactoryCredentials> {
- const [linearRec, githubRec] = await Promise.all([
- reader.getServiceCredentialSecret(orgScopeId, FACTORY_LINEAR_SLUG),
- reader.getServiceCredentialSecret(orgScopeId, FACTORY_GITHUB_SLUG),
- ]);
- const linearApiKey = usableSecret(linearRec);
- const githubToken = usableSecret(githubRec);
- if (linearApiKey === null || githubToken === null) {
- const missing: string[] = [];
- if (linearApiKey === null) missing.push(FACTORY_LINEAR_SLUG);
- if (githubToken === null) missing.push(FACTORY_GITHUB_SLUG);
- return { ok: false, missing };
- }
- return { ok: true, linearApiKey, githubToken };
-}
diff --git a/src/loops/factory/effects.ts b/src/loops/factory/effects.ts
deleted file mode 100644
index 393fedd52..000000000
--- a/src/loops/factory/effects.ts
+++ /dev/null
@@ -1,184 +0,0 @@
-import type { Sandbox, SandboxHandle } from "../../sandbox/sandbox.ts";
-import type { FactoryConfig, ScopedConfigStore } from "../../resolution/config-store.ts";
-import type { ServiceCredentialReader } from "../../credentials/keychain.ts";
-import type { Loop, LoopItem, ScopeId } from "../../types.ts";
-import { isRunnable, type LoopStore } from "../loop-store.ts";
-import type { CapturedArtifact, LoopRunnerEffects } from "../runner.ts";
-import type { SuccessVerdict } from "../success-evaluation.ts";
-import { createSweeper } from "../../util/sweeper.ts";
-import { swallow } from "../../util/errors.ts";
-import { enumerateFactoryCandidates } from "./linear-intake.ts";
-import { readFactoryCredentials } from "./credentials.ts";
-import { preflightFactorySandbox, type PreflightResult } from "./preflight.ts";
-import { renderFactoryEnv, runFactoryProcess, type FactoryProcessResult } from "./process-work.ts";
-import { parseFactoryContract } from "./contract.ts";
-import { evaluateFactoryForge } from "./forge-evaluate.ts";
-import type { ForgeRef } from "./ship.ts";
-
-const DEFAULT_FACTORY_REPO_DIR = "/workspace/repo";
-const DEFAULT_PAUSE_POLL_MS = 30_000;
-
-export const FACTORY_LOOP_SURFACE = "factory";
-
-export const isFactoryLoop = (loop: Loop): boolean => loop.surface === FACTORY_LOOP_SURFACE;
-
-export const factoryForgeRef = (config: FactoryConfig, externalRef: string | undefined): ForgeRef | null => {
- const digits = /(\d+)$/.exec(externalRef ?? "")?.[1];
- return digits === undefined
- ? null
- : { forge: config.forge, publishProject: config.publishProject, number: Number(digits) };
-};
-
-export interface FactoryEffectsDeps {
- sandbox: Sandbox;
- config: Pick<ScopedConfigStore, "getFactoryConfig">;
- credentials: ServiceCredentialReader;
- orgScopeId: ScopeId;
- loops: Pick<LoopStore, "get">;
- repoDir?: string;
- fetch?: typeof globalThis.fetch;
- pausePollMs?: number;
-}
-
-export interface FactoryContext {
- config: FactoryConfig;
- linearApiKey: string;
- githubToken: string;
-}
-
-export type FactoryWorkEffects = Pick<LoopRunnerEffects, "enumerate" | "work" | "captureOutputs" | "evaluate">;
-
-interface FactoryRun {
- result: FactoryProcessResult;
- config: FactoryConfig;
-}
-
-const preflightDetail = (result: Extract<PreflightResult, { ok: false }>): string =>
- result.reason === "missing_tools" ? `missing_tools: ${result.missing.join(", ")}` : result.reason;
-
-const noPrVerdict = (): SuccessVerdict => ({
- outcome: "continue",
- reason: "no pull request",
- checks: [],
- judged: false,
-});
-
-const prTarget = (artifacts: CapturedArtifact[], config: FactoryConfig): { ref: ForgeRef; branch: string } | null => {
- const pr = artifacts.find((artifact) => artifact.shipAction === "open_pr");
- const ref = factoryForgeRef(config, pr?.externalRef);
- return pr?.label && ref ? { ref, branch: pr.label } : null;
-};
-
-export async function loadFactoryContext(deps: FactoryEffectsDeps): Promise<FactoryContext> {
- const config = deps.config.getFactoryConfig();
- if (!config) throw new Error("factory_config_missing");
- const credentials = await readFactoryCredentials(deps.credentials, deps.orgScopeId);
- if (!credentials.ok) throw new Error(`factory_credentials_missing: ${credentials.missing.join(", ")}`);
- return { config, linearApiKey: credentials.linearApiKey, githubToken: credentials.githubToken };
-}
-
-export function createFactoryLoopEffects(deps: FactoryEffectsDeps): FactoryWorkEffects {
- const repoDir = deps.repoDir ?? DEFAULT_FACTORY_REPO_DIR;
- const runs = new Map<string, FactoryRun>();
-
- const provisionWorkspace = (scopeId: ScopeId): Promise<SandboxHandle> =>
- deps.sandbox.provision([{ scopeId, mode: "rw", mountPath: "" }]);
-
- const teardownWarm = async (handle: SandboxHandle): Promise<void> => {
- await deps.sandbox.teardown(handle, { keepWarm: true }).catch((e: unknown) => swallow("factory teardown", e));
- };
-
- const artifactsFor = (item: LoopItem, runId: string): CapturedArtifact[] => {
- const run = runs.get(runId);
- if (!run) return [];
- return parseFactoryContract({
- stdout: run.result.stdout,
- sourceKey: item.sourceKey,
- forge: run.config.forge,
- publishProject: run.config.publishProject,
- });
- };
-
- return {
- async enumerate() {
- const { config, linearApiKey } = await loadFactoryContext(deps);
- return enumerateFactoryCandidates({ teamId: config.linearTeamId, apiKey: linearApiKey, fetch: deps.fetch });
- },
-
- async work({ loop, item, guidance }) {
- const { config, linearApiKey, githubToken } = await loadFactoryContext(deps);
-
- const preflightHandle = await provisionWorkspace(loop.ownerScopeId);
- let preflight: PreflightResult;
- try {
- preflight = await preflightFactorySandbox(deps.sandbox, preflightHandle);
- } finally {
- await teardownWarm(preflightHandle);
- }
- if (!preflight.ok) throw new Error(`factory_preflight_failed: ${preflightDetail(preflight)}`);
-
- const env = renderFactoryEnv({ config, guidance, linearApiKey, githubToken, repoDir });
-
- const controller = new AbortController();
- const pausePoll = createSweeper(
- async () => {
- const current = await deps.loops.get(loop.id);
- if (!current || !isRunnable(current)) controller.abort();
- },
- deps.pausePollMs ?? DEFAULT_PAUSE_POLL_MS,
- { label: "factory pause poll" },
- );
- pausePoll.start();
-
- let result: FactoryProcessResult;
- try {
- result = await runFactoryProcess({
- sandbox: deps.sandbox,
- scopeId: loop.ownerScopeId,
- repoDir,
- ticketId: item.sourceKey,
- env,
- signal: controller.signal,
- onChunk: () => {},
- });
- } finally {
- pausePoll.stop();
- }
- if (result.aborted) throw new Error("factory_run_aborted");
-
- const itemPrefix = `factory:${loop.id}:${item.id}:`;
- for (const key of runs.keys()) if (key.startsWith(itemPrefix)) runs.delete(key);
- const runId = `${itemPrefix}${item.attempts + 1}`;
- runs.set(runId, { result, config });
- return { runId };
- },
-
- async captureOutputs({ item, runId }) {
- return artifactsFor(item, runId);
- },
-
- async evaluate({ item, runId }) {
- try {
- const artifacts = artifactsFor(item, runId);
- if (artifacts.some((artifact) => artifact.shipAction === "close_already_fixed"))
- return { outcome: "met", reason: "already fixed", checks: [], judged: false };
- const run = runs.get(runId);
- if (!run) return noPrVerdict();
- const target = prTarget(artifacts, run.config);
- if (!target) return noPrVerdict();
- const { githubToken } = await loadFactoryContext(deps);
- return await evaluateFactoryForge({
- fetch: deps.fetch,
- forge: run.config.forge,
- publishProject: run.config.publishProject,
- bugbotRequired: run.config.bugbotRequired,
- forgeToken: githubToken,
- number: target.ref.number,
- branch: target.branch,
- });
- } finally {
- runs.delete(runId);
- }
- },
- };
-}
diff --git a/src/loops/factory/evaluate.ts b/src/loops/factory/evaluate.ts
deleted file mode 100644
index 8cd6ca24a..000000000
--- a/src/loops/factory/evaluate.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { evaluateSuccess, type SuccessCheckResult, type SuccessVerdict } from "../success-evaluation.ts";
-import { shq } from "../../util/shell.ts";
-
-const FACTORY_CHECKS = ["exact_head", "ci_green_on_head", "ledger_clean", "mergeable"] as const;
-
-const DEFAULT_SCRIPT_PATH = "tools/factory/converge-vector.sh";
-const SHELL_SAFE = /^[A-Za-z0-9._/-]+$/;
-
-export interface FactoryEvaluateInput {
- exec: (command: string) => Promise<{ code: number; stdout: string; stderr: string }>;
- attempt: number;
- mrNumber: number;
- branch: string;
- headSha?: string;
- scriptPath?: string;
-}
-
-export function parseVector(stdout: string): Map<string, string> {
- const vector = new Map<string, string>();
- for (const line of stdout.split("\n")) {
- const separator = line.indexOf("=");
- if (separator === -1) continue;
- vector.set(line.slice(0, separator), line.slice(separator + 1));
- }
- return vector;
-}
-
-const shellArg = (value: string): string => (SHELL_SAFE.test(value) ? value : shq(value));
-
-export async function evaluateFactoryItem(input: FactoryEvaluateInput): Promise<SuccessVerdict> {
- const argv = [
- "bash",
- shellArg(input.scriptPath ?? DEFAULT_SCRIPT_PATH),
- shellArg(String(input.mrNumber)),
- shellArg(input.branch),
- ];
- if (input.headSha) argv.push(shellArg(input.headSha));
-
- const { code, stdout } = await input.exec(argv.join(" "));
- const vector = parseVector(stdout);
- const degraded = code !== 0 || vector.get("vector_readable") !== "true";
-
- const resultFor = (name: string): SuccessCheckResult => {
- if (degraded)
- return { command: name, passed: false, detail: `vector_error: ${vector.get("vector_error") ?? `exit ${code}`}` };
- const detail = vector.get(`${name}_detail`);
- return { command: name, passed: vector.get(name) === "true", ...(detail === undefined ? {} : { detail }) };
- };
-
- return evaluateSuccess({
- condition: "pull request converged",
- attempt: input.attempt,
- checks: [...FACTORY_CHECKS],
- runCheck: async (name) => resultFor(name),
- judge: async () => ({ met: true, reason: "all convergence checks passed" }),
- });
-}
diff --git a/src/loops/factory/forge-evaluate.ts b/src/loops/factory/forge-evaluate.ts
deleted file mode 100644
index ce87dc967..000000000
--- a/src/loops/factory/forge-evaluate.ts
+++ /dev/null
@@ -1,195 +0,0 @@
-import type { SuccessCheckResult, SuccessVerdict } from "../success-evaluation.ts";
-
-const GITHUB_API = "https://api.github.com";
-const GITHUB_GRAPHQL = "https://api.github.com/graphql";
-const GITLAB_API = "https://gitlab.com/api/v4";
-const PAGE_SIZE = 100;
-const PAGE = `per_page=${PAGE_SIZE}`;
-const GITHUB_MERGEABLE_STATES = new Set(["clean", "unstable", "has_hooks"]);
-const GITHUB_GREEN_CONCLUSIONS = new Set(["success", "skipped", "neutral"]);
-const BUGBOT_LOGIN_PREFIX = "cursor";
-const BUGBOT_NOTE_MARKER = "BUGBOT_REVIEW";
-
-export interface ForgeEvaluateInput {
- fetch?: typeof globalThis.fetch;
- forge: "github" | "gitlab";
- publishProject: string;
- forgeToken: string;
- number: number;
- branch: string;
- bugbotRequired: boolean;
-}
-
-export const FORGE_CHECKS = ["exact_head", "ci_green_on_head", "ledger_clean", "mergeable", "bugbot_reviewed"] as const;
-
-type ForgeCheck = (typeof FORGE_CHECKS)[number];
-
-interface CheckOutcome {
- passed: boolean;
- detail?: string;
-}
-
-const isObj = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
-
-const obj = (value: unknown): Record<string, unknown> => (isObj(value) ? value : {});
-
-const arr = (value: unknown): unknown[] => (Array.isArray(value) ? value : []);
-
-const str = (value: unknown): string | undefined => (typeof value === "string" ? value : undefined);
-
-const gql = (value: string): string => JSON.stringify(value);
-
-const failed = (detail: string): Error => new Error(`forge_evaluate_failed: ${detail}`);
-
-const graphqlError = (body: unknown): string | undefined => {
- const errors = obj(body).errors;
- if (!Array.isArray(errors) || errors.length === 0) return undefined;
- return str(obj(errors[0]).message) ?? "graphql_error";
-};
-
-const forgeBase = (input: ForgeEvaluateInput): string =>
- input.forge === "github"
- ? `${GITHUB_API}/repos/${input.publishProject}`
- : `${GITLAB_API}/projects/${encodeURIComponent(input.publishProject.replace(/%2F/gi, "/"))}`;
-
-const forgeHeaders = (input: ForgeEvaluateInput): Record<string, string> =>
- input.forge === "github"
- ? { Authorization: `Bearer ${input.forgeToken}`, Accept: "application/vnd.github+json" }
- : { "PRIVATE-TOKEN": input.forgeToken };
-
-async function readJson(input: ForgeEvaluateInput, url: string, init: RequestInit): Promise<unknown> {
- const doFetch = input.fetch ?? globalThis.fetch;
- const response = await doFetch(url, init);
- if (response.status < 200 || response.status > 299) throw failed(String(response.status));
- const parsed: unknown = await response.json();
- const message = graphqlError(parsed);
- if (message !== undefined) throw failed(message);
- return parsed;
-}
-
-const forgeGet = (input: ForgeEvaluateInput, path: string): Promise<unknown> =>
- readJson(input, `${forgeBase(input)}${path}`, { method: "GET", headers: forgeHeaders(input) });
-
-const githubGraphql = (input: ForgeEvaluateInput, query: string): Promise<unknown> =>
- readJson(input, GITHUB_GRAPHQL, {
- method: "POST",
- headers: { ...forgeHeaders(input), "Content-Type": "application/json" },
- body: JSON.stringify({ query }),
- });
-
-const notesOf = (discussions: unknown[]): Record<string, unknown>[] =>
- discussions.flatMap((discussion) => arr(obj(discussion).notes).map(obj));
-
-const unresolvedDetail = (count: number): CheckOutcome =>
- count === 0 ? { passed: true } : { passed: false, detail: `${count} unresolved` };
-
-export async function evaluateFactoryForge(input: ForgeEvaluateInput): Promise<SuccessVerdict> {
- const github = input.forge === "github";
- const request = obj(await forgeGet(input, github ? `/pulls/${input.number}` : `/merge_requests/${input.number}`));
- const headSha = (github ? str(obj(request.head).sha) : str(request.sha)) ?? "";
-
- let discussions: unknown[] | undefined;
- const readDiscussions = async (): Promise<unknown[]> => {
- discussions ??= arr(await forgeGet(input, `/merge_requests/${input.number}/discussions?${PAGE}`));
- return discussions;
- };
-
- const exactHead = async (): Promise<CheckOutcome> => {
- const path = github ? `/branches/${input.branch}` : `/repository/branches/${encodeURIComponent(input.branch)}`;
- const commit = obj(obj(await forgeGet(input, path)).commit);
- const branchSha = (github ? str(commit.sha) : str(commit.id)) ?? "";
- if (headSha !== "" && headSha === branchSha) return { passed: true };
- return { passed: false, detail: `${headSha} != ${branchSha}` };
- };
-
- const ciGreenOnHead = async (): Promise<CheckOutcome> => {
- if (github) {
- const payload = obj(await forgeGet(input, `/commits/${headSha}/check-runs?${PAGE}`));
- const runs = arr(payload.check_runs);
- if (runs.length === 0) return { passed: false, detail: "no check runs" };
- // A full page may hide runs on the next one, so the check fails closed rather than trusting the visible runs.
- if (runs.length >= PAGE_SIZE) return { passed: false, detail: "check runs exceed one page" };
- const offending = runs
- .map(obj)
- .find((run) => run.status !== "completed" || !GITHUB_GREEN_CONCLUSIONS.has(str(run.conclusion) ?? ""));
- if (offending === undefined) return { passed: true };
- return { passed: false, detail: str(offending.name) ?? "unnamed check run" };
- }
- // GitLab merged-result pipelines run on a temporary merge commit, so the MR's head_pipeline is the
- // only pointer that survives that sha mismatch.
- const pipeline = obj(request.head_pipeline);
- if (Object.keys(pipeline).length === 0) return { passed: false, detail: "no pipeline" };
- const status = str(pipeline.status) ?? "unknown";
- return status === "success" ? { passed: true } : { passed: false, detail: status };
- };
-
- const ledgerClean = async (): Promise<CheckOutcome> => {
- if (github) {
- const [owner = "", name = ""] = input.publishProject.split("/");
- const data = await githubGraphql(
- input,
- `query { repository(owner: ${gql(owner)}, name: ${gql(name)}) { pullRequest(number: ${input.number}) { reviewThreads(first: ${PAGE_SIZE}) { pageInfo { hasNextPage } nodes { isResolved } } } } }`,
- );
- const connection = obj(obj(obj(obj(obj(data).data).repository).pullRequest).reviewThreads);
- if (obj(connection.pageInfo).hasNextPage === true)
- return { passed: false, detail: "review threads exceed one page" };
- const threads = arr(connection.nodes);
- return unresolvedDetail(threads.filter((thread) => obj(thread).isResolved !== true).length);
- }
- const all = await readDiscussions();
- if (all.length >= PAGE_SIZE) return { passed: false, detail: "discussions exceed one page" };
- const open = notesOf(all).filter((note) => note.resolvable === true && note.resolved === false);
- return unresolvedDetail(open.length);
- };
-
- const mergeable = async (): Promise<CheckOutcome> => {
- if (github) {
- const state = str(request.mergeable_state) ?? "unknown";
- if (request.mergeable === true && GITHUB_MERGEABLE_STATES.has(state)) return { passed: true };
- return { passed: false, detail: state };
- }
- const status = str(request.detailed_merge_status) ?? "unknown";
- return status === "mergeable" ? { passed: true } : { passed: false, detail: status };
- };
-
- const bugbotReviewed = async (): Promise<CheckOutcome> => {
- if (!input.bugbotRequired) return { passed: true, detail: "not required" };
- const reviewed = github
- ? arr(await forgeGet(input, `/pulls/${input.number}/reviews?${PAGE}`))
- .map(obj)
- .some(
- (review) =>
- (str(obj(review.user).login) ?? "").toLowerCase().startsWith(BUGBOT_LOGIN_PREFIX) &&
- headSha !== "" &&
- str(review.commit_id) === headSha,
- )
- : notesOf(await readDiscussions()).some((note) => {
- const body = str(note.body) ?? "";
- const author = obj(note.author);
- const fromBugbot = [str(author.name), str(author.username)].some((name) =>
- (name ?? "").toLowerCase().startsWith(BUGBOT_LOGIN_PREFIX),
- );
- return fromBugbot && body.includes(BUGBOT_NOTE_MARKER) && headSha !== "" && body.includes(headSha);
- });
- return reviewed ? { passed: true } : { passed: false, detail: `no bugbot review on ${headSha}` };
- };
-
- const runners: Record<ForgeCheck, () => Promise<CheckOutcome>> = {
- exact_head: exactHead,
- ci_green_on_head: ciGreenOnHead,
- ledger_clean: ledgerClean,
- mergeable,
- bugbot_reviewed: bugbotReviewed,
- };
-
- const checks: SuccessCheckResult[] = [];
- for (const name of FORGE_CHECKS) {
- const { passed, detail } = await runners[name]();
- checks.push({ command: name, passed, ...(detail === undefined ? {} : { detail }) });
- if (!passed) {
- const reason = detail === undefined ? `check failed: ${name}` : `check failed: ${name} — ${detail}`;
- return { outcome: "continue", reason, checks, judged: false };
- }
- }
- return { outcome: "met", reason: "converged", checks, judged: false };
-}
diff --git a/src/loops/factory/linear-intake.ts b/src/loops/factory/linear-intake.ts
deleted file mode 100644
index c79a5c812..000000000
--- a/src/loops/factory/linear-intake.ts
+++ /dev/null
@@ -1,113 +0,0 @@
-import type { IntakeCandidate } from "../runner.ts";
-
-export const FACTORY_INTAKE_STATE = "Auto-Triage";
-export const LINEAR_GRAPHQL_URL = "https://api.linear.app/graphql";
-
-const DEFAULT_MAX_PAGES = 5;
-const RESOLVED_BLOCKER_STATE_TYPES = new Set(["completed", "canceled"]);
-
-const FACTORY_INTAKE_QUERY = `query FactoryIntake($teamId: String!, $state: String!, $after: String) {
- team(id: $teamId) {
- issues(first: 50, after: $after, filter: { state: { name: { eq: $state } } }) {
- nodes {
- identifier
- title
- createdAt
- inverseRelations { nodes { type issue { identifier state { type } } } }
- }
- pageInfo { hasNextPage endCursor }
- }
- }
-}`;
-
-export interface LinearIntakeInput {
- teamId: string;
- apiKey: string;
- fetch?: typeof globalThis.fetch;
- stateName?: string;
- maxPages?: number;
-}
-
-interface IntakeRelation {
- type?: string;
- issue?: { identifier?: string; state?: { type?: string } | null } | null;
-}
-
-interface IntakeIssue {
- identifier: string;
- title: string;
- createdAt: string;
- inverseRelations?: { nodes?: IntakeRelation[] | null } | null;
-}
-
-interface IntakePage {
- nodes?: IntakeIssue[] | null;
- pageInfo?: { hasNextPage?: boolean; endCursor?: string | null } | null;
-}
-
-interface FactoryIntakeResponse {
- data?: { team?: { issues?: IntakePage | null } | null } | null;
- errors?: { message?: string }[] | null;
-}
-
-const intakeFailure = (detail: string): Error => new Error(`linear_intake_failed: ${detail}`);
-
-const relationBlocks = (relation: IntakeRelation): boolean => {
- const stateType = relation.issue?.state?.type;
- if (relation.type !== "blocks" || stateType === undefined) return false;
- return !RESOLVED_BLOCKER_STATE_TYPES.has(stateType);
-};
-
-const isBlocked = (issue: IntakeIssue): boolean => (issue.inverseRelations?.nodes ?? []).some(relationBlocks);
-
-const byCreatedAt = (a: IntakeIssue, b: IntakeIssue): number => {
- if (a.createdAt < b.createdAt) return -1;
- return a.createdAt > b.createdAt ? 1 : 0;
-};
-
-async function fetchIntakePage(
- doFetch: typeof globalThis.fetch,
- input: LinearIntakeInput,
- state: string,
- after: string | undefined,
-): Promise<IntakePage> {
- const res = await doFetch(LINEAR_GRAPHQL_URL, {
- method: "POST",
- headers: { Authorization: input.apiKey, "Content-Type": "application/json" },
- body: JSON.stringify({
- query: FACTORY_INTAKE_QUERY,
- variables: { teamId: input.teamId, state, after },
- }),
- });
- if (!res.ok) throw intakeFailure(String(res.status));
- let body: FactoryIntakeResponse;
- try {
- body = (await res.json()) as FactoryIntakeResponse;
- } catch {
- throw intakeFailure("unreadable response body");
- }
- const firstError = body.errors?.[0];
- if (firstError) throw intakeFailure(firstError.message ?? "linear reported an unnamed error");
- const page = body.data?.team?.issues;
- if (!page) throw intakeFailure("response carried no team issues");
- return page;
-}
-
-export async function enumerateFactoryCandidates(input: LinearIntakeInput): Promise<IntakeCandidate[]> {
- const doFetch = input.fetch ?? globalThis.fetch;
- const state = input.stateName ?? FACTORY_INTAKE_STATE;
- const maxPages = input.maxPages ?? DEFAULT_MAX_PAGES;
- const issues: IntakeIssue[] = [];
- let after: string | undefined;
- for (let request = 0; request < maxPages; request += 1) {
- const page = await fetchIntakePage(doFetch, input, state, after);
- issues.push(...(page.nodes ?? []));
- const next = page.pageInfo?.hasNextPage === true ? page.pageInfo.endCursor : undefined;
- if (!next) break;
- after = next;
- }
- return issues
- .filter((issue) => !isBlocked(issue))
- .sort(byCreatedAt)
- .map((issue) => ({ sourceKey: issue.identifier, sourceSummary: issue.title }));
-}
diff --git a/src/loops/factory/preflight.ts b/src/loops/factory/preflight.ts
deleted file mode 100644
index bdea31666..000000000
--- a/src/loops/factory/preflight.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-import { shq } from "../../util/shell.ts";
-import { supportsProcessSessions, type Sandbox, type SandboxHandle } from "../../sandbox/sandbox.ts";
-
-export const FACTORY_REQUIRED_TOOLS = ["bash", "git", "gh", "jq", "curl", "node", "npm", "claude"] as const;
-export const FACTORY_PROOF_TOOLS = ["npx"] as const;
-export const FACTORY_PREFLIGHT_TIMEOUT_MS = 30_000;
-
-const PLAYWRIGHT_PROBE_COMMAND = "npx --no -- playwright --version";
-
-export type PreflightResult =
- | { ok: true; versions: Record<string, string> }
- | { ok: false; reason: "no_process_sessions" }
- | { ok: false; reason: "missing_tools"; missing: string[]; versions: Record<string, string> };
-
-export const factoryToolProbeScript = (tools: readonly string[]): string =>
- `for t in ${tools.map(shq).join(" ")}; do if command -v "$t" >/dev/null 2>&1; then printf '%s=ok %s\\n' "$t" "$("$t" --version 2>/dev/null | head -n 1)"; else printf '%s=missing\\n' "$t"; fi; done`;
-
-export const parseFactoryToolProbe = (
- stdout: string,
- tools: readonly string[],
-): { versions: Record<string, string>; missing: string[] } => {
- const reported = new Map<string, string>();
- for (const line of stdout.split("\n")) {
- const separator = line.indexOf("=");
- if (separator < 0) continue;
- const status = line.slice(separator + 1);
- if (status !== "ok" && !status.startsWith("ok ")) continue;
- reported.set(line.slice(0, separator), status.slice("ok".length).trim());
- }
- const versions: Record<string, string> = {};
- const missing: string[] = [];
- for (const tool of tools) {
- const version = reported.get(tool);
- if (version === undefined) missing.push(tool);
- else versions[tool] = version;
- }
- return { versions, missing };
-};
-
-const firstNonEmptyLine = (stdout: string): string =>
- stdout
- .split("\n")
- .map((line) => line.trim())
- .find((line) => line.length > 0) ?? "";
-
-export async function preflightFactorySandbox(
- sandbox: Sandbox,
- handle: SandboxHandle,
- opts?: { requireProof?: boolean },
-): Promise<PreflightResult> {
- if (!supportsProcessSessions(sandbox)) return { ok: false, reason: "no_process_sessions" };
- const requireProof = opts?.requireProof === true;
- const tools: string[] = requireProof
- ? [...FACTORY_REQUIRED_TOOLS, ...FACTORY_PROOF_TOOLS]
- : [...FACTORY_REQUIRED_TOOLS];
- const probe = await sandbox.run(handle, factoryToolProbeScript(tools), {
- timeoutMs: FACTORY_PREFLIGHT_TIMEOUT_MS,
- });
- const { versions, missing } = parseFactoryToolProbe(probe.stdout, tools);
- if (requireProof) {
- const proof = await sandbox.run(handle, PLAYWRIGHT_PROBE_COMMAND, {
- timeoutMs: FACTORY_PREFLIGHT_TIMEOUT_MS,
- });
- const version = firstNonEmptyLine(proof.stdout);
- if (proof.code !== 0 || version === "") missing.push("playwright");
- else versions.playwright = version;
- }
- return missing.length > 0 ? { ok: false, reason: "missing_tools", missing, versions } : { ok: true, versions };
-}
diff --git a/src/loops/factory/process-work.ts b/src/loops/factory/process-work.ts
deleted file mode 100644
index b21a0919c..000000000
--- a/src/loops/factory/process-work.ts
+++ /dev/null
@@ -1,134 +0,0 @@
-import { CapabilityUnsupportedError, supportsProcessSessions, type Sandbox } from "../../sandbox/sandbox.ts";
-import type { FactoryConfig } from "../../resolution/config-store.ts";
-import type { ScopeId } from "../../types.ts";
-
-export const FACTORY_WRAPPER = ".claude/io-coding-agent-js.sh";
-export const FACTORY_READ_WAIT_MS = 5_000;
-export const FACTORY_STDOUT_CAP_BYTES = 4 * 1024 * 1024;
-export const FACTORY_TERM_GRACE_MS = 30_000;
-export const FACTORY_DRAIN_EMPTY_READS = 2;
-const FACTORY_READ_MAX_BYTES = 65_536;
-const TICKET_RE = /^[A-Z][A-Z0-9]*-\d+$/;
-
-export interface FactoryEnvInput {
- config: FactoryConfig;
- guidance?: string;
- linearApiKey: string;
- githubToken: string;
- repoDir: string;
-}
-
-export function renderFactoryEnv(input: FactoryEnvInput): Record<string, string> {
- const { config, guidance, linearApiKey, githubToken, repoDir } = input;
- return {
- ...(guidance !== undefined ? { IO_FEEDBACK: guidance } : {}),
- IO_LINEAR_API_KEY: linearApiKey,
- IO_GITHUB_TOKEN: githubToken,
- IO_PUBLISH_FORGE: config.forge,
- IO_PUBLISH_PROJECT: config.publishProject,
- IO_PUBLISH_TARGET: config.targetBranch,
- IO_PUBLISH_REMOTE: "origin",
- IO_SOURCE_REMOTE: "origin",
- IO_SOURCE_BASE_REF: `origin/${config.targetBranch}`,
- IO_SOURCE_APP_DIRS: config.sourceAppDirs,
- IO_SOURCE_TEST_RE: config.sourceTestRe,
- IO_VERIFY_TESTS_CMD: config.verifyTestsCmd,
- IO_VERIFY_TEST_FILE_CMD: config.verifyTestFileCmd,
- IO_VERIFY_LINT_CMD: config.verifyLintCmd,
- IO_REPO_DIR: repoDir,
- IO_FACTORY_SOURCE_DIR: repoDir,
- IO_REPO_CLONE_URL: config.repoCloneUrl,
- ...(config.repoSetupCmd !== undefined ? { IO_REPO_SETUP_CMD: config.repoSetupCmd } : {}),
- ...(config.proofStartCmd !== undefined ? { IO_PROOF_START_CMD: config.proofStartCmd } : {}),
- ...(config.proofBaseUrlCmd !== undefined ? { IO_PROOF_BASE_URL_CMD: config.proofBaseUrlCmd } : {}),
- IO_BUGBOT_REQUIRED: String(config.bugbotRequired),
- IO_FOLLOWUPS_ENABLED: String(config.followupsEnabled),
- IO_FOLLOWUP_TEAM_ID: config.linearTeamId,
- };
-}
-
-export interface FactoryProcessInput {
- sandbox: Sandbox;
- scopeId: ScopeId;
- repoDir: string;
- ticketId: string;
- env: Record<string, string>;
- onChunk?: (chunk: string) => void;
- signal?: AbortSignal;
- readWaitMs?: number;
- termGraceMs?: number;
-}
-
-export interface FactoryProcessResult {
- processId: string;
- exitCode: number;
- stdout: string;
- truncated: boolean;
- aborted: boolean;
-}
-
-export async function runFactoryProcess(input: FactoryProcessInput): Promise<FactoryProcessResult> {
- const { scopeId, repoDir, ticketId, env, onChunk, signal } = input;
- if (!TICKET_RE.test(ticketId)) throw new Error("factory_ticket_invalid");
- const sandbox = input.sandbox;
- if (!supportsProcessSessions(sandbox)) {
- throw new CapabilityUnsupportedError(sandbox.profile.backend, "process sessions");
- }
-
- const waitMs = input.readWaitMs ?? FACTORY_READ_WAIT_MS;
- const grace = input.termGraceMs ?? FACTORY_TERM_GRACE_MS;
- const handle = await sandbox.provision([{ scopeId, mode: "rw", mountPath: "" }]);
- try {
- const { processId } = await sandbox.startProcess(handle, `bash ${FACTORY_WRAPPER} ${ticketId}`, {
- cwd: repoDir,
- env,
- });
- try {
- let cursor = 0;
- let stdout = "";
- let truncated = false;
- let exited = false;
- let exitCode = 0;
- let termSent = false;
- let killSent = false;
- let killAt = 0;
- let emptyExitedReads = 0;
- for (;;) {
- const read = await sandbox.readProcess(handle, processId, {
- sinceCursor: cursor,
- maxBytes: FACTORY_READ_MAX_BYTES,
- waitMs,
- });
- cursor = read.cursor;
- if (read.chunks !== "") {
- stdout += read.chunks;
- if (stdout.length > FACTORY_STDOUT_CAP_BYTES) {
- stdout = stdout.slice(-FACTORY_STDOUT_CAP_BYTES);
- truncated = true;
- }
- onChunk?.(read.chunks);
- }
- if (read.status.state === "exited") {
- exited = true;
- exitCode = read.status.code;
- }
- emptyExitedReads = exited && read.chunks === "" ? emptyExitedReads + 1 : 0;
- if (emptyExitedReads >= FACTORY_DRAIN_EMPTY_READS) break;
- if (signal?.aborted && !termSent) {
- await sandbox.signalProcess(handle, processId, "TERM");
- termSent = true;
- killAt = Date.now() + grace;
- } else if (termSent && !killSent && read.status.state === "running" && Date.now() >= killAt) {
- await sandbox.signalProcess(handle, processId, "KILL");
- killSent = true;
- }
- }
- return { processId, exitCode, stdout, truncated, aborted: termSent };
- } catch (e) {
- await sandbox.signalProcess(handle, processId, "TERM").catch(() => {});
- throw e;
- }
- } finally {
- await sandbox.teardown(handle, { keepWarm: true }).catch(() => {});
- }
-}
diff --git a/src/loops/factory/ship.ts b/src/loops/factory/ship.ts
deleted file mode 100644
index 130aed467..000000000
--- a/src/loops/factory/ship.ts
+++ /dev/null
@@ -1,286 +0,0 @@
-const GITHUB_API = "https://api.github.com";
-const GITHUB_GRAPHQL = "https://api.github.com/graphql";
-const GITLAB_API = "https://gitlab.com/api/v4";
-const LINEAR_GRAPHQL = "https://api.linear.app/graphql";
-
-const STATE_IN_REVIEW = "In Review";
-const STATE_AUTO_TRIAGE = "Auto-Triage";
-const STATE_DONE = "Done";
-const LABEL_READY = "ready-for-review";
-const TERMINAL_STATE_TYPES = new Set(["completed", "canceled"]);
-const DRAFT_TITLE_PREFIX = /^(Draft|WIP):\s*/i;
-
-export interface ForgeRef {
- forge: "github" | "gitlab";
- publishProject: string;
- number: number;
-}
-
-export interface ShipDeps {
- fetch?: typeof globalThis.fetch;
- forgeToken: string;
- linearApiKey: string;
-}
-
-export interface ShipStepResult {
- step: string;
- changed: boolean;
- detail?: string;
-}
-
-const isObj = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
-
-const obj = (value: unknown): Record<string, unknown> => (isObj(value) ? value : {});
-
-const nodes = (value: unknown): unknown[] => {
- const list = obj(value).nodes;
- return Array.isArray(list) ? list : [];
-};
-
-const str = (value: unknown): string | undefined => (typeof value === "string" ? value : undefined);
-
-const gql = (value: string): string => JSON.stringify(value);
-
-const graphqlError = (body: unknown): string | undefined => {
- const errors = obj(body).errors;
- if (!Array.isArray(errors) || errors.length === 0) return undefined;
- return str(obj(errors[0]).message) ?? "graphql_error";
-};
-
-const forgeBase = (ref: ForgeRef): string =>
- ref.forge === "github"
- ? `${GITHUB_API}/repos/${ref.publishProject}`
- : `${GITLAB_API}/projects/${encodeURIComponent(ref.publishProject.replace(/%2F/gi, "/"))}`;
-
-export async function forgeRequest(
- ref: ForgeRef,
- deps: ShipDeps,
- slug: string,
- method: string,
- path: string,
- body?: unknown,
-): Promise<unknown> {
- const headers: Record<string, string> =
- ref.forge === "github"
- ? { Authorization: `Bearer ${deps.forgeToken}`, Accept: "application/vnd.github+json" }
- : { "PRIVATE-TOKEN": deps.forgeToken };
- if (body !== undefined) headers["Content-Type"] = "application/json";
- const doFetch = deps.fetch ?? globalThis.fetch;
- const response = await doFetch(`${forgeBase(ref)}${path}`, {
- method,
- headers,
- ...(body === undefined ? {} : { body: JSON.stringify(body) }),
- });
- if (response.status < 200 || response.status > 299) throw new Error(`forge_${slug}_failed: ${response.status}`);
- const parsed: unknown = await response.json();
- const failure = graphqlError(parsed);
- if (failure !== undefined) throw new Error(`forge_${slug}_failed: ${failure}`);
- return parsed;
-}
-
-async function githubGraphql(deps: ShipDeps, slug: string, query: string): Promise<unknown> {
- const doFetch = deps.fetch ?? globalThis.fetch;
- const response = await doFetch(GITHUB_GRAPHQL, {
- method: "POST",
- headers: {
- Authorization: `Bearer ${deps.forgeToken}`,
- Accept: "application/vnd.github+json",
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ query }),
- });
- if (response.status < 200 || response.status > 299) throw new Error(`forge_${slug}_failed: ${response.status}`);
- const parsed: unknown = await response.json();
- const failure = graphqlError(parsed);
- if (failure !== undefined) throw new Error(`forge_${slug}_failed: ${failure}`);
- return parsed;
-}
-
-async function linearGraphql(deps: ShipDeps, slug: string, query: string): Promise<Record<string, unknown>> {
- const doFetch = deps.fetch ?? globalThis.fetch;
- const response = await doFetch(LINEAR_GRAPHQL, {
- method: "POST",
- headers: { Authorization: deps.linearApiKey, "Content-Type": "application/json" },
- body: JSON.stringify({ query }),
- });
- if (response.status < 200 || response.status > 299) throw new Error(`linear_${slug}_failed: ${response.status}`);
- const parsed: unknown = await response.json();
- const failure = graphqlError(parsed);
- if (failure !== undefined) throw new Error(`linear_${slug}_failed: ${failure}`);
- return obj(obj(parsed).data);
-}
-
-interface ForgePr {
- draft: boolean;
- open: boolean;
- merged: boolean;
- nodeId?: string;
- title?: string;
-}
-
-async function readForgePr(ref: ForgeRef, deps: ShipDeps, slug: string): Promise<ForgePr> {
- if (ref.forge === "github") {
- const pr = obj(await forgeRequest(ref, deps, slug, "GET", `/pulls/${ref.number}`));
- return { draft: pr.draft === true, open: pr.state === "open", merged: pr.merged === true, nodeId: str(pr.node_id) };
- }
- const mr = obj(await forgeRequest(ref, deps, slug, "GET", `/merge_requests/${ref.number}`));
- return { draft: mr.draft === true, open: mr.state === "opened", merged: mr.state === "merged", title: str(mr.title) };
-}
-
-interface LinearWorkflowState {
- id: string;
- name: string;
- type: string;
-}
-
-interface LinearIssue {
- id: string;
- state: LinearWorkflowState;
- labels: string[];
- teamStates: LinearWorkflowState[];
-}
-
-const workflowState = (value: unknown): LinearWorkflowState => {
- const state = obj(value);
- return { id: str(state.id) ?? "", name: str(state.name) ?? "", type: str(state.type) ?? "" };
-};
-
-async function readLinearIssue(deps: ShipDeps, ticket: string): Promise<LinearIssue> {
- const data = await linearGraphql(
- deps,
- "read",
- `query { issue(id: ${gql(ticket)}) { id state { id name type } labels { nodes { id name } } team { id states { nodes { id name type } } } } }`,
- );
- const issue = obj(data.issue);
- return {
- id: str(issue.id) ?? "",
- state: workflowState(issue.state),
- labels: nodes(issue.labels).flatMap((node) => {
- const name = str(obj(node).name);
- return name === undefined ? [] : [name];
- }),
- teamStates: nodes(obj(issue.team).states).map(workflowState),
- };
-}
-
-async function moveLinearState(
- deps: ShipDeps,
- issue: LinearIssue,
- target: string,
- step: string,
-): Promise<ShipStepResult> {
- if (issue.state.name === target) return { step, changed: false };
- const state = issue.teamStates.find((candidate) => candidate.name === target);
- if (state === undefined) throw new Error(`linear_state_missing: ${target}`);
- await linearGraphql(
- deps,
- "state",
- `mutation { issueUpdate(id: ${gql(issue.id)}, input: { stateId: ${gql(state.id)} }) { success } }`,
- );
- return { step, changed: true };
-}
-
-async function postLinearComment(
- deps: ShipDeps,
- issue: LinearIssue,
- body: string,
- step: string,
-): Promise<ShipStepResult> {
- await linearGraphql(
- deps,
- "comment",
- `mutation { commentCreate(input: { issueId: ${gql(issue.id)}, body: ${gql(body)} }) { success } }`,
- );
- return { step, changed: true };
-}
-
-async function undraftForgePr(ref: ForgeRef, deps: ShipDeps): Promise<ShipStepResult> {
- const step = "undraft";
- const pr = await readForgePr(ref, deps, step);
- if (!pr.draft) return { step, changed: false };
- if (ref.forge === "github") {
- await githubGraphql(
- deps,
- step,
- `mutation { markPullRequestReadyForReview(input: { pullRequestId: ${gql(pr.nodeId ?? "")} }) { pullRequest { isDraft } } }`,
- );
- return { step, changed: true };
- }
- await forgeRequest(ref, deps, step, "PUT", `/merge_requests/${ref.number}`, {
- title: (pr.title ?? "").replace(DRAFT_TITLE_PREFIX, ""),
- });
- return { step, changed: true };
-}
-
-async function addReadyLabel(deps: ShipDeps, issue: LinearIssue): Promise<ShipStepResult> {
- const step = "linear-label";
- if (issue.labels.includes(LABEL_READY)) return { step, changed: false };
- const data = await linearGraphql(
- deps,
- "label",
- `query { issueLabels(filter: { name: { eq: ${gql(LABEL_READY)} } }) { nodes { id } } }`,
- );
- let labelId: string | undefined;
- for (const node of nodes(data.issueLabels)) {
- labelId = str(obj(node).id);
- if (labelId !== undefined) break;
- }
- if (labelId === undefined) throw new Error(`linear_label_missing: ${LABEL_READY}`);
- await linearGraphql(
- deps,
- "label",
- `mutation { issueAddLabel(id: ${gql(issue.id)}, labelId: ${gql(labelId)}) { success } }`,
- );
- return { step, changed: true };
-}
-
-async function closeForgePr(ref: ForgeRef, deps: ShipDeps): Promise<ShipStepResult> {
- const step = "close";
- const pr = await readForgePr(ref, deps, step);
- if (pr.merged) return { step, changed: false, detail: "merged" };
- if (!pr.open) return { step, changed: false };
- if (ref.forge === "github") {
- await forgeRequest(ref, deps, step, "PATCH", `/pulls/${ref.number}`, { state: "closed" });
- } else {
- await forgeRequest(ref, deps, step, "PUT", `/merge_requests/${ref.number}`, { state_event: "close" });
- }
- return { step, changed: true };
-}
-
-export async function shipFactoryPullRequest(ref: ForgeRef, ticket: string, deps: ShipDeps): Promise<ShipStepResult[]> {
- const undraft = await undraftForgePr(ref, deps);
- const issue = await readLinearIssue(deps, ticket);
- const state = await moveLinearState(deps, issue, STATE_IN_REVIEW, "linear-state");
- return [undraft, state, await addReadyLabel(deps, issue)];
-}
-
-export async function returnFactoryPullRequest(
- ref: ForgeRef | null,
- ticket: string,
- note: string,
- deps: ShipDeps,
-): Promise<ShipStepResult[]> {
- const results: ShipStepResult[] = [];
- if (ref !== null) {
- const path = ref.forge === "github" ? `/issues/${ref.number}/comments` : `/merge_requests/${ref.number}/notes`;
- await forgeRequest(ref, deps, "comment", "POST", path, { body: `Returned by the factory reviewer: ${note}` });
- results.push({ step: "forge-comment", changed: true });
- results.push(await closeForgePr(ref, deps));
- }
- const issue = await readLinearIssue(deps, ticket);
- results.push(await postLinearComment(deps, issue, `Returned to Auto-Triage: ${note}`, "linear-comment"));
- results.push(await moveLinearState(deps, issue, STATE_AUTO_TRIAGE, "linear-state"));
- return results;
-}
-
-export async function shipFactoryAlreadyFixed(
- ticket: string,
- evidence: string | undefined,
- deps: ShipDeps,
-): Promise<ShipStepResult[]> {
- const issue = await readLinearIssue(deps, ticket);
- const body = evidence === undefined || evidence === "" ? "Already fixed." : `Already fixed. ${evidence}`;
- const comment = await postLinearComment(deps, issue, body, "linear-comment");
- if (TERMINAL_STATE_TYPES.has(issue.state.type)) return [comment, { step: "linear-state", changed: false }];
- return [comment, await moveLinearState(deps, issue, STATE_DONE, "linear-state")];
-}
diff --git a/src/loops/loop-fire.ts b/src/loops/loop-fire.ts
index b4185947c..56b6caefb 100644
--- a/src/loops/loop-fire.ts
+++ b/src/loops/loop-fire.ts
@@ -24,22 +24,6 @@ import { decideShip, outputCandidate } from "./ship-gate.ts";
import { evaluateSuccess, type SuccessCheckResult, type SuccessVerdict } from "./success-evaluation.ts";
import { ledgerState } from "./ledger-view.ts";
import { adapterForItem } from "./sources/index.ts";
-import {
- createFactoryLoopEffects,
- factoryForgeRef,
- isFactoryLoop,
- loadFactoryContext,
- type FactoryContext,
- type FactoryEffectsDeps,
- type FactoryWorkEffects,
-} from "./factory/effects.ts";
-import {
- returnFactoryPullRequest,
- shipFactoryAlreadyFixed,
- shipFactoryPullRequest,
- type ShipDeps,
- type ShipStepResult,
-} from "./factory/ship.ts";
export interface LoopFireDeps {
loops: LoopStore;
@@ -47,7 +31,6 @@ export interface LoopFireDeps {
outputs: LoopOutputStore;
grants: ShipGrantStore;
trigger: TriggerDeps;
- factory?: FactoryEffectsDeps;
}
interface LoopFireResult {
@@ -386,19 +369,6 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
});
}
- async function factoryShipDeps(): Promise<{ context: FactoryContext; ship: ShipDeps }> {
- if (!deps.factory) throw new Error("factory loop has no factory deps");
- const context = await loadFactoryContext(deps.factory);
- return {
- context,
- ship: {
- forgeToken: context.githubToken,
- linearApiKey: context.linearApiKey,
- ...(deps.factory.fetch ? { fetch: deps.factory.fetch } : {}),
- },
- };
- }
-
function stageFailure(stage: string, outcome: TriggerOutcome): { error: Error; userMessage: string } | null {
if (outcome.authzFailed)
return {
@@ -465,7 +435,6 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
async function fire(loopId: string, fireKey: string): Promise<LoopFireResult> {
const loop = await deps.loops.get(loopId);
if (!loop) return { status: "failed", note: "loop not found" };
- if (isFactoryLoop(loop) && !deps.factory) return { status: "failed", note: "factory loop has no factory deps" };
if (loop.state === "enabled" && (await deps.trigger.idempotency.committed(`${fireKey}:intake`))) {
return { status: "silent", note: "duplicate fire key" };
}
@@ -474,63 +443,52 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
const grants = await deps.grants.byLoop(loopId);
const workReplies = new Map<string, string>();
- const turnEffects: FactoryWorkEffects = {
- enumerate: async () => {
- const outcome = await stageTurn(loop, `${fireKey}:intake`, threadRef, intakePrompt(loop), {
- readOnly: true,
- });
- if (!outcome.ran && !outcome.authzFailed) throw new DuplicateLoopFireError("duplicate fire key");
- const failure = stageFailure("intake", outcome);
- if (failure) throw failure.error;
- return parseIntake(outcome.reply ?? "");
- },
- work: async ({ item, guidance }) => {
- const outcome = await stageTurn(
- loop,
- `${fireKey}:work:${item.id}:${item.attempts}`,
- threadRef,
- workPrompt(loop, item, guidance),
- );
- const failure = stageFailure("work", outcome);
- if (failure) throw failure.error;
- workReplies.set(item.id, outcome.reply ?? "");
- return { runId: outcome.sessionId ?? `${threadRef}:work:${item.id}` };
- },
- captureOutputs: async ({ item }) => parseOutputs(workReplies.get(item.id) ?? ""),
- evaluate: async ({ item, attempt }) => {
- const outcome = await stageTurn(
- loop,
- `${fireKey}:judge:${item.id}:${attempt}`,
- threadRef,
- judgePrompt(loop, item),
- { readOnly: true },
- );
- const failure = stageFailure("judge", outcome);
- if (failure) throw failure.error;
- return parseVerdict(outcome.reply ?? "", loop.successChecks ?? [], loop.successCondition, attempt, maxAttempts);
- },
- };
-
- const factoryWork = isFactoryLoop(loop) && deps.factory ? createFactoryLoopEffects(deps.factory) : null;
- const factoryEffects: FactoryWorkEffects | null = factoryWork && {
- ...factoryWork,
- enumerate: async (fired) => {
- let candidates: IntakeCandidate[] = [];
- const ran = await deps.trigger.idempotency.once(`${fireKey}:intake`, async () => {
- candidates = await factoryWork.enumerate(fired);
- });
- if (!ran) throw new DuplicateLoopFireError("duplicate fire key");
- return candidates;
- },
- };
-
let summary: FireSummary;
try {
summary = await runLoopFire(
loop,
{ loops: deps.loops, items: deps.items, outputs: deps.outputs },
{
- ...(factoryEffects ?? turnEffects),
+ enumerate: async () => {
+ const outcome = await stageTurn(loop, `${fireKey}:intake`, threadRef, intakePrompt(loop), {
+ readOnly: true,
+ });
+ if (!outcome.ran && !outcome.authzFailed) throw new DuplicateLoopFireError("duplicate fire key");
+ const failure = stageFailure("intake", outcome);
+ if (failure) throw failure.error;
+ return parseIntake(outcome.reply ?? "");
+ },
+ work: async ({ item, guidance }) => {
+ const outcome = await stageTurn(
+ loop,
+ `${fireKey}:work:${item.id}:${item.attempts}`,
+ threadRef,
+ workPrompt(loop, item, guidance),
+ );
+ const failure = stageFailure("work", outcome);
+ if (failure) throw failure.error;
+ workReplies.set(item.id, outcome.reply ?? "");
+ return { runId: outcome.sessionId ?? `${threadRef}:work:${item.id}` };
+ },
+ captureOutputs: async ({ item }) => parseOutputs(workReplies.get(item.id) ?? ""),
+ evaluate: async ({ item, attempt }) => {
+ const outcome = await stageTurn(
+ loop,
+ `${fireKey}:judge:${item.id}:${attempt}`,
+ threadRef,
+ judgePrompt(loop, item),
+ { readOnly: true },
+ );
+ const failure = stageFailure("judge", outcome);
+ if (failure) throw failure.error;
+ return parseVerdict(
+ outcome.reply ?? "",
+ loop.successChecks ?? [],
+ loop.successCondition,
+ attempt,
+ maxAttempts,
+ );
+ },
authorizeAutoShip: async (output) => {
const currentLoop = await deps.loops.get(loop.id);
if (!currentLoop || !isRunnable(currentLoop)) return null;
@@ -564,17 +522,6 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
return { status: fireNeedsAttention(summary) ? "ok" : "silent", note, summary };
}
- async function runFactoryShip(output: LoopOutput, item: LoopItem): Promise<ShipStepResult[]> {
- if (output.shipAction !== "open_pr" && output.shipAction !== "close_already_fixed")
- throw new Error(`factory_ship_action_unknown: ${output.shipAction}`);
- const { context, ship } = await factoryShipDeps();
- if (output.shipAction === "close_already_fixed")
- return shipFactoryAlreadyFixed(item.sourceKey, output.summary, ship);
- const ref = factoryForgeRef(context.config, output.externalRef);
- if (!ref) throw new Error(`factory_ship_ref_missing: ${output.id}`);
- return shipFactoryPullRequest(ref, item.sourceKey, ship);
- }
-
async function shipOutput(
loopId: string,
outputId: string,
@@ -610,20 +557,6 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
const claimToken = claimed.claimToken!;
const fireKey = `loop:${loopId}:ship:${outputId}`;
if (!(await deps.outputs.beginShipAttempt(outputId, claimToken, fireKey))) return null;
- if (isFactoryLoop(loop)) {
- const steps = await runFactoryShip(claimed, item).catch(async (e: unknown) => {
- await deps.outputs.failShipping(outputId, claimToken);
- throw e;
- });
- const shipped = await deps.outputs.completeShipping(
- outputId,
- claimToken,
- { actorId, ...(note ? { note } : {}) },
- { status: "ok", note: steps.map((step) => `${step.step}=${step.changed}`).join(", ") },
- );
- if (shipped) await settleItem(loopId, shipped.itemId);
- return shipped;
- }
const outcome = await stageTurn(
loop,
fireKey,
@@ -679,17 +612,7 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
const returned = await deps.outputs.returnToLoop(outputId, { actorId, note });
if (returned) {
await deps.outputs.supersedeActiveSiblings(returned.itemId, returned.id);
- const item = await deps.items.returnToWork(returned.itemId, note);
- const loop = await deps.loops.get(loopId);
- if (item && loop && isFactoryLoop(loop)) {
- const { context, ship } = await factoryShipDeps();
- await returnFactoryPullRequest(
- returned.shipAction === "open_pr" ? factoryForgeRef(context.config, returned.externalRef) : null,
- item.sourceKey,
- note,
- ship,
- );
- }
+ await deps.items.returnToWork(returned.itemId, note);
}
return returned;
} finally {
@@ -722,7 +645,6 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {
async function followUp(loop: Loop, item: LoopItem, message: string, actorId: string): Promise<LoopItem | null> {
await deps.items.appendThread(item.id, [{ role: "human", text: message, actorId }]);
- if (isFactoryLoop(loop)) return deps.items.get(item.id);
const asked = (await deps.items.get(item.id)) ?? item;
const fireKey = `loop:${loop.id}:item:${item.id}:followup:${Date.now()}`;
const turn = await itemTurn(loop, asked, followUpPrompt(loop, asked, message), fireKey);
diff --git a/src/resolution/config-store.ts b/src/resolution/config-store.ts
index 78017de2a..c33c7ff63 100644
--- a/src/resolution/config-store.ts
+++ b/src/resolution/config-store.ts
@@ -81,27 +81,6 @@ export interface PersistedApprovedHarnesses {
scopeId: ScopeId;
ids: string[];
}
-export interface FactoryConfig {
- forge: "github" | "gitlab";
- publishProject: string;
- targetBranch: string;
- repoCloneUrl: string;
- repoSetupCmd?: string;
- linearTeamId: string;
- sourceAppDirs: string;
- sourceTestRe: string;
- verifyTestsCmd: string;
- verifyTestFileCmd: string;
- verifyLintCmd: string;
- proofStartCmd?: string;
- proofBaseUrlCmd?: string;
- slackChannel?: string;
- bugbotRequired: boolean;
- followupsEnabled: boolean;
-}
-export interface PersistedFactoryConfig extends FactoryConfig {
- scopeId: ScopeId;
-}
export interface PersistedWebuiModels {
scopeId: ScopeId;
ids: string[];
@@ -222,8 +201,6 @@ export interface ScopedConfigStore {
getApprovedHarnesses(): string[] | null;
setApprovedHarnesses(ids: string[] | null): void;
getApprovedHarnessesDurable(): Promise<string[] | null>;
- getFactoryConfig(): FactoryConfig | null;
- setFactoryConfig(config: FactoryConfig | null): void;
getInternalMemberOverrides(): string[];
setInternalMemberOverrides(members: string[]): void;
getInternalMemberOverridesDurable(): Promise<string[]>;
@@ -284,7 +261,6 @@ export function createMemoryConfigStore(
channelHeaderPin?: DurableMap<PersistedScopedFlag>;
baseModels?: DurableMap<PersistedBaseModel>;
approvedHarnesses?: DurableMap<PersistedApprovedHarnesses>;
- factoryConfigs?: DurableMap<PersistedFactoryConfig>;
internalMemberOverrides?: DurableMap<PersistedInternalMemberOverrides>;
orgAmbient?: DurableMap<PersistedScopedFlag>;
interactiveFastMode?: DurableMap<PersistedScopedFlag>;
@@ -315,7 +291,6 @@ export function createMemoryConfigStore(
const channelHeaderPin = new Map<ScopeId, boolean>();
const baseModels = new Map<ScopeId, PersistedBaseModel>();
let approvedHarnesses: string[] | null = null;
- let factoryConfig: FactoryConfig | null = null;
let internalMemberOverrides: string[] = [];
let orgAmbient = true;
let interactiveFastMode = false;
@@ -339,12 +314,6 @@ export function createMemoryConfigStore(
const channelHeaderPinStore = opts.channelHeaderPin ?? createMemoryMap<PersistedScopedFlag>();
const baseModelStore = opts.baseModels ?? createMemoryMap<PersistedBaseModel>();
const approvedHarnessStore = opts.approvedHarnesses ?? createMemoryMap<PersistedApprovedHarnesses>();
- const factoryConfigStore = opts.factoryConfigs ?? createMemoryMap<PersistedFactoryConfig>();
- const toFactoryConfig = (row: PersistedFactoryConfig | null): FactoryConfig | null => {
- if (!row) return null;
- const { scopeId: _scopeId, ...config } = row;
- return config;
- };
const internalMemberOverridesStore =
opts.internalMemberOverrides ?? createMemoryMap<PersistedInternalMemberOverrides>();
const orgAmbientStore = opts.orgAmbient ?? createMemoryMap<PersistedScopedFlag>();
@@ -502,7 +471,6 @@ export function createMemoryConfigStore(
for (const r of await channelHeaderPinStore.all()) channelHeaderPin.set(r.scopeId, r.on);
for (const r of await baseModelStore.all()) baseModels.set(r.scopeId, r);
approvedHarnesses = (await approvedHarnessStore.get(org))?.ids ?? null;
- factoryConfig = toFactoryConfig(await factoryConfigStore.get(org));
internalMemberOverrides = (await internalMemberOverridesStore.get(org))?.members ?? [];
orgAmbient = (await orgAmbientStore.get(org))?.on ?? true;
interactiveFastMode = (await interactiveFastModeStore.get(org))?.on ?? false;
@@ -874,14 +842,6 @@ export function createMemoryConfigStore(
else persist(`approvedHarnesses:${org}`, "approved harnesses", () => approvedHarnessStore.delete(org));
},
getApprovedHarnessesDurable: async () => (await approvedHarnessStore.get(org))?.ids ?? null,
- getFactoryConfig: () => (factoryConfig ? { ...factoryConfig } : null),
- setFactoryConfig(config) {
- const next = config ? { ...config } : null;
- factoryConfig = next;
- persist(`factoryConfig:${org}`, "factory config", () =>
- next ? factoryConfigStore.put(org, { scopeId: org, ...next }) : factoryConfigStore.delete(org),
- );
- },
getInternalMemberOverrides: () => [...internalMemberOverrides],
setInternalMemberOverrides(members) {
const next = [...new Set(members.map((m) => m.trim().toLowerCase()).filter(Boolean))];
@@ -1094,7 +1054,6 @@ export function createMemoryConfigStore(
externalSlack,
baseModel,
approved,
- factoryRow,
brandingRow,
orgAmbientRow,
interactiveFastModeRow,
@@ -1112,7 +1071,6 @@ export function createMemoryConfigStore(
externalSlackParticipantsStore.get(id),
baseModelStore.get(id),
id === org ? approvedHarnessStore.get(org) : null,
- id === org ? factoryConfigStore.get(org) : null,
brandingStore.get(id),
id === org ? orgAmbientStore.get(org) : null,
id === org ? interactiveFastModeStore.get(org) : null,
@@ -1156,7 +1114,6 @@ export function createMemoryConfigStore(
if (baseModel) baseModels.set(id, baseModel);
else baseModels.delete(id);
if (id === org) approvedHarnesses = approved?.ids ?? null;
- if (id === org) factoryConfig = toFactoryConfig(factoryRow);
if (id === org) internalMemberOverrides = internalOverridesRow?.members ?? [];
if (id === org) orgAmbient = orgAmbientRow?.on ?? true;
if (id === org) interactiveFastMode = interactiveFastModeRow?.on ?? false;
@@ -1188,7 +1145,6 @@ export function createMemoryConfigStore(
`interactiveFastMode:${org}`,
`individualModelAuth:${org}`,
`autoFlagger:${org}`,
- `factoryConfig:${org}`,
]
: []),
`channelHeaderPin:${id}`,
diff --git a/src/wiring.ts b/src/wiring.ts
index 02c830749..0a0608673 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -36,7 +36,6 @@ import {
type PersistedScopedFlag,
type PersistedBaseModel,
type PersistedApprovedHarnesses,
- type PersistedFactoryConfig,
type PersistedInternalMemberOverrides,
type PersistedWebuiModels,
type PersistedPeopleDirectoryUrl,
@@ -556,7 +555,6 @@ export function buildApp(
channelHeaderPin: artifactMap<PersistedScopedFlag>("channel_header_pin_flag"),
baseModels: artifactMap<PersistedBaseModel>("base_model_configs"),
approvedHarnesses: artifactMap<PersistedApprovedHarnesses>("approved_harness_configs"),
- factoryConfigs: artifactMap<PersistedFactoryConfig>("factory_configs"),
internalMemberOverrides: artifactMap<PersistedInternalMemberOverrides>("internal_member_overrides"),
orgAmbient: artifactMap<PersistedScopedFlag>("org_ambient_flag"),
interactiveFastMode: artifactMap<PersistedScopedFlag>("interactive_fast_mode_flag"),
@@ -1772,13 +1770,6 @@ export function buildApp(
currentScopeMembers,
sessions,
},
- factory: {
- sandbox,
- config: configStore,
- credentials: credentialStore,
- orgScopeId: orgScope,
- loops: loopStore,
- },
});
const loops: LoopServiceDeps = {
store: loopStore,
diff --git a/test/admin-resources.test.ts b/test/admin-resources.test.ts
index 3bcd7c523..dc7011178 100644
--- a/test/admin-resources.test.ts
+++ b/test/admin-resources.test.ts
@@ -114,14 +114,7 @@ test("GET /v1/admin/resources returns a manifest entry for every registered reso
const r = await fetch(`${srv.base}/v1/admin/resources`, { headers: ADMIN });
assert.equal(r.status, 200);
const body = (await r.json()) as {
- resources: {
- id: string;
- kind: string;
- target?: string;
- secret?: boolean;
- clearable?: boolean;
- enumValues?: unknown[];
- }[];
+ resources: { id: string; kind: string; target?: string; secret?: boolean; enumValues?: unknown[] }[];
};
const ids = body.resources.map((x) => x.id).sort();
assert.deepEqual(ids, ADMIN_RESOURCES.map((x) => x.id).sort());
@@ -133,9 +126,6 @@ test("GET /v1/admin/resources returns a manifest entry for every registered reso
assert.deepEqual(byId.get("security-posture")?.enumValues, ["dangerous", "auto", "strict"]);
assert.equal(byId.get("service-credentials")?.target, "org");
assert.equal(byId.get("service-credentials")?.secret, true);
- assert.equal(byId.get("factory-config")?.kind, "custom");
- assert.equal(byId.get("factory-config")?.target, "org");
- assert.equal(byId.get("factory-config")?.clearable, true);
assert.equal(byId.has("import"), false);
} finally {
await srv.close();
@@ -903,191 +893,3 @@ test("historical cutover policies remain visible and clearable without layer too
await srv.close();
}
});
-
-const FACTORY_BODY = {
- forge: "github",
- publishProject: "yc-software/qm",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/yc-software/qm.git",
- linearTeamId: "QM",
- sourceAppDirs: "src,plugins",
- sourceTestRe: "^test/.*\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "node --test",
- verifyLintCmd: "npm run lint",
- bugbotRequired: true,
- followupsEnabled: false,
-};
-
-const putFactory = (base: string, scope: string, body: unknown, headers: Record<string, string> = ADMIN) =>
- fetch(`${base}/v1/admin/scopes/${scope}/factory-config`, {
- method: "PUT",
- headers,
- body: JSON.stringify(body),
- });
-
-const scopeConfig = async (base: string, scope: string): Promise<Record<string, unknown>> =>
- (await (await fetch(`${base}/v1/admin/scopes/${scope}`, { headers: ADMIN })).json()) as Record<string, unknown>;
-
-const withoutKey = (key: string): Record<string, unknown> => {
- const body: Record<string, unknown> = { ...FACTORY_BODY };
- delete body[key];
- return body;
-};
-
-test("factory-config round-trips through the org scope, replaces wholesale, and is admin-gated", async () => {
- const srv = start();
- try {
- const before = await scopeConfig(srv.base, "org:default-org");
- assert.equal(before.factoryConfig, null);
-
- const put = await putFactory(srv.base, "org:default-org", FACTORY_BODY);
- assert.equal(put.status, 200);
- assert.deepEqual(await put.json(), { ok: true, scopeId: "org:default-org", resource: "factory-config" });
-
- const after = await scopeConfig(srv.base, "org:default-org");
- assert.deepEqual(after.factoryConfig, FACTORY_BODY);
- const stored = after.factoryConfig as Record<string, unknown>;
- for (const optional of ["repoSetupCmd", "proofStartCmd", "proofBaseUrlCmd", "slackChannel"]) {
- assert.equal(Object.hasOwn(stored, optional), false, `${optional} must not be materialized`);
- }
- assert.deepEqual({ ...after, factoryConfig: null }, { ...before, factoryConfig: null });
-
- const audit = (await (
- await fetch(`${srv.base}/v1/admin/audit?scope=org:default-org`, { headers: ADMIN })
- ).json()) as { events: { action: string; resource: string; principalId: string }[] };
- const updates = audit.events.filter((e) => e.action === "factory-config.update");
- assert.equal(updates.length, 1);
- assert.equal(updates[0]!.resource, "factory-config");
- assert.equal(updates[0]!.principalId, "admin-alice");
-
- const withOptionals = { ...FACTORY_BODY, repoSetupCmd: "npm ci", proofStartCmd: "npm run dev" };
- assert.equal((await putFactory(srv.base, "org:default-org", withOptionals)).status, 200);
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, withOptionals);
-
- assert.equal(
- (await putFactory(srv.base, "org:default-org", { ...FACTORY_BODY, targetBranch: " release " })).status,
- 200,
- );
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, {
- ...FACTORY_BODY,
- targetBranch: "release",
- });
-
- const denied = await putFactory(srv.base, "org:default-org", FACTORY_BODY, {
- "content-type": "application/json",
- "x-admin-actor": "nobody@default-org",
- });
- assert.equal(denied.status, 403);
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, {
- ...FACTORY_BODY,
- targetBranch: "release",
- });
- } finally {
- await srv.close();
- }
-});
-
-test("factory-config rejects every malformed body by name and leaves the stored record intact", async () => {
- const srv = start();
- try {
- assert.equal((await putFactory(srv.base, "org:default-org", FACTORY_BODY)).status, 200);
-
- const cases: [unknown, string][] = [
- [{ ...FACTORY_BODY, forge: "bitbucket" }, "forge"],
- [{}, "forge"],
- [withoutKey("verifyTestsCmd"), "verifyTestsCmd"],
- [{ ...FACTORY_BODY, targetBranch: " " }, "targetBranch"],
- [{ ...FACTORY_BODY, bugbotRequired: "true" }, "bugbotRequired"],
- [{ ...FACTORY_BODY, slackChannel: 42 }, "slackChannel"],
- [{ ...FACTORY_BODY, repoSetupCmd: null }, "repoSetupCmd"],
- [{ ...FACTORY_BODY, linearTeamID: "QM" }, "linearTeamID"],
- [[], "body must be an object"],
- [null, "body must be an object"],
- ];
- for (const [body, named] of cases) {
- const r = await putFactory(srv.base, "org:default-org", body);
- const label = JSON.stringify(body);
- assert.equal(r.status, 400, label);
- const payload = (await r.json()) as { error: string; message: string };
- assert.equal(payload.error, "bad_request", label);
- assert.ok(payload.message.startsWith("factory-config: "), `${label} → ${payload.message}`);
- assert.ok(payload.message.includes(named), `${label} → ${payload.message}`);
- }
-
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, FACTORY_BODY);
- const audit = (await (
- await fetch(`${srv.base}/v1/admin/audit?scope=org:default-org`, { headers: ADMIN })
- ).json()) as { events: { action: string }[] };
- assert.equal(audit.events.filter((e) => e.action === "factory-config.update").length, 1);
- } finally {
- await srv.close();
- }
-});
-
-test("factory-config is org-only at every scope while non-org reads echo the org record", async () => {
- const srv = start();
- try {
- assert.equal((await putFactory(srv.base, "org:default-org", FACTORY_BODY)).status, 200);
-
- for (const scope of ["channel:C1", "personal:U1"]) {
- for (const body of [FACTORY_BODY, { reset: true }]) {
- const r = await putFactory(srv.base, scope, body);
- assert.equal(r.status, 400, `${scope} ${JSON.stringify(body)}`);
- const payload = (await r.json()) as { error: string; message: string };
- assert.equal(payload.error, "bad_request");
- assert.equal(payload.message, "the factory config is org-wide; target an org scope");
- }
- }
-
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, FACTORY_BODY);
- const channel = await fetch(`${srv.base}/v1/admin/scopes/channel:C1`, { headers: ADMIN });
- assert.equal(channel.status, 200);
- assert.deepEqual(((await channel.json()) as Record<string, unknown>).factoryConfig, FACTORY_BODY);
- } finally {
- await srv.close();
- }
-});
-
-test("factory-config clears with { reset: true } only, idempotently, and leaves other org settings alone", async () => {
- const srv = start();
- try {
- assert.equal(
- (
- await fetch(`${srv.base}/v1/admin/scopes/org:default-org/approved-harnesses`, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ ids: ["pi"] }),
- })
- ).status,
- 200,
- );
- assert.equal((await putFactory(srv.base, "org:default-org", FACTORY_BODY)).status, 200);
- const seeded = await scopeConfig(srv.base, "org:default-org");
-
- const reset = await putFactory(srv.base, "org:default-org", { reset: true });
- assert.equal(reset.status, 200);
- assert.deepEqual(await reset.json(), { ok: true, scopeId: "org:default-org", resource: "factory-config" });
-
- const cleared = await scopeConfig(srv.base, "org:default-org");
- assert.equal(Object.hasOwn(cleared, "factoryConfig"), true);
- assert.equal(cleared.factoryConfig, null);
- assert.deepEqual(cleared.approvedHarnesses, seeded.approvedHarnesses);
- assert.deepEqual({ ...cleared, factoryConfig: null }, { ...seeded, factoryConfig: null });
-
- assert.equal((await putFactory(srv.base, "org:default-org", { reset: true })).status, 200);
- assert.equal((await scopeConfig(srv.base, "org:default-org")).factoryConfig, null);
-
- assert.equal((await putFactory(srv.base, "org:default-org", FACTORY_BODY)).status, 200);
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, FACTORY_BODY);
-
- for (const body of [{ reset: true, forge: "github" }, { reset: false }, { reset: "true" }, { reset: 1 }]) {
- const r = await putFactory(srv.base, "org:default-org", body);
- assert.equal(r.status, 400, JSON.stringify(body));
- assert.equal(((await r.json()) as { message: string }).message, "factory-config: unknown key reset");
- }
- assert.deepEqual((await scopeConfig(srv.base, "org:default-org")).factoryConfig, FACTORY_BODY);
- } finally {
- await srv.close();
- }
-});
diff --git a/test/config-store-org.test.ts b/test/config-store-org.test.ts
index 526cac0d7..4f5faf021 100644
--- a/test/config-store-org.test.ts
+++ b/test/config-store-org.test.ts
@@ -1,12 +1,9 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import { readFile } from "node:fs/promises";
-import { createMemoryMap, type DurableMap } from "../src/persistence/durable-map.ts";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
import {
createMemoryConfigStore,
- type FactoryConfig,
type PersistedDeploymentIdentity,
- type PersistedFactoryConfig,
type PersistedInternalMemberOverrides,
} from "../src/resolution/config-store.ts";
@@ -32,103 +29,3 @@ test("internal member overrides normalize, persist org-wide, and hydrate back",
await rehydrated.hydrate!();
assert.deepEqual(rehydrated.getInternalMemberOverrides(), ["[email protected]", "u123abc"]);
});
-
-const slowWritingMap = (): DurableMap<PersistedFactoryConfig> => {
- const inner = createMemoryMap<PersistedFactoryConfig>();
- const afterTimer = async <T>(op: () => Promise<T>): Promise<T> => {
- await new Promise((resolve) => setTimeout(resolve, 0));
- return op();
- };
- return {
- ...inner,
- put: (id, value) => afterTimer(() => inner.put(id, value)),
- delete: (id) => afterTimer(() => inner.delete(id)),
- };
-};
-
-const FACTORY_CONFIG: FactoryConfig = {
- forge: "github",
- publishProject: "yc-software/qm",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/yc-software/qm.git",
- linearTeamId: "QM",
- sourceAppDirs: "src,plugins",
- sourceTestRe: "^test/.*\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "node --test",
- verifyLintCmd: "npm run lint",
- bugbotRequired: true,
- followupsEnabled: false,
-};
-
-test("the factory config persists org-wide, hydrates back without its scope id, and clears", async () => {
- const factoryConfigs = slowWritingMap();
- const store = createMemoryConfigStore("default-org", { factoryConfigs });
- await store.hydrate!();
- assert.equal(store.getFactoryConfig(), null);
-
- store.setFactoryConfig(FACTORY_CONFIG);
- assert.deepEqual(store.getFactoryConfig(), FACTORY_CONFIG);
- await store.flushScope("org:default-org");
-
- const rebuilt = createMemoryConfigStore("default-org", { factoryConfigs });
- await rebuilt.hydrate!();
- assert.deepEqual(rebuilt.getFactoryConfig(), FACTORY_CONFIG);
- assert.equal(Object.hasOwn(rebuilt.getFactoryConfig()!, "scopeId"), false);
-
- store.setFactoryConfig(null);
- assert.equal(store.getFactoryConfig(), null);
- await store.flushScope("org:default-org");
- assert.equal(await factoryConfigs.get("org:default-org"), null);
-
- const afterClear = createMemoryConfigStore("default-org", { factoryConfigs });
- await afterClear.hydrate!();
- assert.equal(afterClear.getFactoryConfig(), null);
-});
-
-test("a second instance sees factory config writes and deletes after refreshing the org scope", async () => {
- const factoryConfigs = slowWritingMap();
- const a = createMemoryConfigStore("default-org", { factoryConfigs });
- const b = createMemoryConfigStore("default-org", { factoryConfigs });
- await a.hydrate!();
- await b.hydrate!();
-
- a.setFactoryConfig(FACTORY_CONFIG);
- await a.flushScope("org:default-org");
- assert.equal(b.getFactoryConfig(), null);
- await b.refreshScope("org:default-org");
- assert.deepEqual(b.getFactoryConfig(), FACTORY_CONFIG);
-
- await b.refreshScope("channel:C1");
- assert.deepEqual(b.getFactoryConfig(), FACTORY_CONFIG);
-
- a.setFactoryConfig(null);
- await a.flushScope("org:default-org");
- await b.refreshScope("org:default-org");
- assert.equal(b.getFactoryConfig(), null);
-});
-
-test("the factory config is copied on set and on get, so neither caller can mutate the stored record", async () => {
- const factoryConfigs = slowWritingMap();
- const store = createMemoryConfigStore("default-org", { factoryConfigs });
- await store.hydrate!();
-
- const caller = { ...FACTORY_CONFIG };
- store.setFactoryConfig(caller);
- caller.targetBranch = "hacked";
- assert.equal(store.getFactoryConfig()!.targetBranch, "main");
-
- const read = store.getFactoryConfig()!;
- read.targetBranch = "hacked-too";
- assert.equal(store.getFactoryConfig()!.targetBranch, "main");
-
- await store.flushScope("org:default-org");
- const rebuilt = createMemoryConfigStore("default-org", { factoryConfigs });
- await rebuilt.hydrate!();
- assert.deepEqual(rebuilt.getFactoryConfig(), FACTORY_CONFIG);
-});
-
-test("the factory config is bound to a durable map in wiring, not to per-instance memory", async () => {
- const wiring = await readFile(new URL("../src/wiring.ts", import.meta.url), "utf8");
- assert.match(wiring, /factoryConfigs: artifactMap<\w+>\("factory_configs"\)/);
-});
diff --git a/test/loop-factory-contract.test.ts b/test/loop-factory-contract.test.ts
deleted file mode 100644
index 14e17e697..000000000
--- a/test/loop-factory-contract.test.ts
+++ /dev/null
@@ -1,208 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import { parseFactoryContract, type ContractInput } from "../src/loops/factory/contract.ts";
-
-const github: Omit<ContractInput, "stdout"> = {
- sourceKey: "QM-12",
- forge: "github",
- publishProject: "yc-software/qm-yc",
-};
-
-const qm12OpenPr = [
- {
- shipAction: "open_pr",
- title: "QM-12: pull request #2311",
- label: "qm-12-s17868",
- externalRef: "https://github.com/yc-software/qm-yc/pull/2311",
- capturedBy: "classifier",
- },
-];
-
-test("the real QM-12 wrapper tail becomes one open_pr artifact", () => {
- const stdout = [
- '[ship-event] {"schema_version":1,"sequence":28,"decision":"finish",' +
- '"reason":"stable_final_read","ticket":"QM-12","mr_iid":2311,"branch":"qm-12-s17868"}',
- "BRANCH:qm-12-s17868",
- "MR:2311",
- ].join("\n");
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout }), qm12OpenPr);
-});
-
-test("a !-prefixed MR and padded values parse the same as the bare contract", () => {
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: "BRANCH: qm-12-s17868 \nMR: !2311 " }),
- qm12OpenPr,
- );
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:qm-12-s17868\r\nMR:2311\r\n" }), qm12OpenPr);
-});
-
-test("the later contract line wins and a malformed later line never resets it", () => {
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:old\nMR:2311\nBRANCH:new\nMR:2999" }), [
- {
- shipAction: "open_pr",
- title: "QM-12: pull request #2999",
- label: "new",
- externalRef: "https://github.com/yc-software/qm-yc/pull/2999",
- capturedBy: "classifier",
- },
- ]);
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: "BRANCH:qm-12-s17868\nMR:2311\nMR:none\nBRANCH:\nBRANCH: " }),
- qm12OpenPr,
- );
-});
-
-test("gitlab builds a merge-request URL, decoding %2F without choking on a stray %", () => {
- const gitlab = { sourceKey: "QM-16", forge: "gitlab" } as const;
- assert.deepStrictEqual(
- parseFactoryContract({ ...gitlab, stdout: "BRANCH:feature-x\nMR:!77", publishProject: "yc-software%2Fcode" }),
- [
- {
- shipAction: "open_pr",
- title: "QM-16: pull request #77",
- label: "feature-x",
- externalRef: "https://gitlab.com/yc-software/code/-/merge_requests/77",
- capturedBy: "classifier",
- },
- ],
- );
- assert.deepStrictEqual(
- parseFactoryContract({ ...gitlab, stdout: "BRANCH:feature-x\nMR:77", publishProject: "yc-software%2Fco%de" })[0]
- ?.externalRef,
- "https://gitlab.com/yc-software/co%de/-/merge_requests/77",
- );
-});
-
-test("an already-fixed kickback that retained an MR closes and never opens a PR", () => {
- const closed = [
- {
- shipAction: "close_already_fixed",
- title: "QM-12: already fixed",
- summary: "fixed on main by 9f3ac21, tests already cover it",
- externalRef: "https://github.com/yc-software/qm-yc/pull/2311",
- capturedBy: "classifier",
- },
- ];
- const lines = [
- "ALREADY_FIXED_COMMIT:9f3ac21",
- "ALREADY_FIXED_EVIDENCE:fixed on main by 9f3ac21, tests already cover it",
- "ALREADY_FIXED_DUPLICATE_OF:QM-9",
- "BRANCH:qm-12-s17868",
- "MR:2311",
- ];
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: ["ALREADY_FIXED:true", ...lines].join("\n") }),
- closed,
- );
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: [...lines, "ALREADY_FIXED:true"].join("\n") }),
- closed,
- );
-});
-
-test("a CRLF stream still closes on ALREADY_FIXED:true", () => {
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: "ALREADY_FIXED:true\r\nALREADY_FIXED_EVIDENCE:already on main\r\n" }),
- [
- {
- shipAction: "close_already_fixed",
- title: "QM-12: already fixed",
- summary: "already on main",
- capturedBy: "classifier",
- },
- ],
- );
-});
-
-test("an empty evidence tail falls through to the commit as the summary", () => {
- assert.deepStrictEqual(
- parseFactoryContract({
- ...github,
- stdout: "ALREADY_FIXED:true\nALREADY_FIXED_COMMIT:9f3ac21\nALREADY_FIXED_EVIDENCE:",
- }),
- [
- {
- shipAction: "close_already_fixed",
- title: "QM-12: already fixed",
- summary: "commit 9f3ac21",
- capturedBy: "classifier",
- },
- ],
- );
-});
-
-test("an uppercase commit with no evidence is still cited as the summary", () => {
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: "ALREADY_FIXED:true\nALREADY_FIXED_COMMIT:9F3AC21BEEF" }),
- [
- {
- shipAction: "close_already_fixed",
- title: "QM-12: already fixed",
- summary: "commit 9F3AC21BEEF",
- capturedBy: "classifier",
- },
- ],
- );
-});
-
-test("an already-fixed run with nothing to cite carries no summary", () => {
- const bare = [{ shipAction: "close_already_fixed", title: "QM-12: already fixed", capturedBy: "classifier" }];
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "ALREADY_FIXED:true" }), bare);
- assert.deepStrictEqual(
- parseFactoryContract({ ...github, stdout: "ALREADY_FIXED:true\nALREADY_FIXED_COMMIT:zz12" }),
- bare,
- );
-});
-
-test("a contract token the mirrored transcript reprints is not the contract", () => {
- assert.deepStrictEqual(
- parseFactoryContract({
- ...github,
- stdout: '[claude-out] {"text":"the wrapper prints BRANCH:foo and MR:1"}',
- }),
- [],
- );
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "[claude-out] BRANCH:qm-12-s17868\nMR:2311" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:qm-12-s17868\n[claude-out] MR:2311" }), []);
-});
-
-test("a half or malformed contract yields nothing", () => {
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:qm-12-s17868" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "MR:2311" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:qm-12\nMR:2311x" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "BRANCH:qm-12\nMR:!" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: " BRANCH:qm-12\nMR:2311" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "" }), []);
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: "\n \n\r\n" }), []);
-});
-
-test("the wrapper's observability-only sibling lines are inert", () => {
- assert.deepStrictEqual(
- parseFactoryContract({
- ...github,
- stdout: [
- "MR_ADOPTED_VIA:https://github.com/yc-software/qm-yc/pull/2311",
- "MR_OPEN_FINDINGS:one thread unresolved",
- "ALREADY_FIXED_DUPLICATE_OF:QM-9",
- "ALREADY_FIXED_COMMIT:9f3ac21",
- "BRANCH:qm-12-s17868",
- ].join("\n"),
- }),
- [],
- );
-});
-
-test("an ALREADY_FIXED near-miss does not trigger the close branch", () => {
- const openPr = [
- {
- shipAction: "open_pr",
- title: "QM-12: pull request #7",
- label: "b",
- externalRef: "https://github.com/yc-software/qm-yc/pull/7",
- capturedBy: "classifier",
- },
- ];
- for (const nearMiss of ["ALREADY_FIXED:false", "ALREADY_FIXED: true", "ALREADY_FIXED:true extra"]) {
- assert.deepStrictEqual(parseFactoryContract({ ...github, stdout: `${nearMiss}\nBRANCH:b\nMR:7` }), openPr);
- }
-});
diff --git a/test/loop-factory-credentials.test.ts b/test/loop-factory-credentials.test.ts
deleted file mode 100644
index 0e291206c..000000000
--- a/test/loop-factory-credentials.test.ts
+++ /dev/null
@@ -1,115 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import type { DecryptedServiceCredential, ServiceCredentialReader } from "../src/credentials/keychain.ts";
-import {
- FACTORY_GITHUB_SLUG,
- FACTORY_LINEAR_SLUG,
- readFactoryCredentials,
- type FactoryCredentials,
-} from "../src/loops/factory/credentials.ts";
-
-const LINEAR_SECRET = "lin_FAKE_SECRET_1";
-const GITHUB_SECRET = "ghp_FAKE_SECRET_2";
-const ORG = "org:acme";
-
-function record(slug: string, over: Partial<DecryptedServiceCredential> = {}): DecryptedServiceCredential {
- return {
- slug,
- name: slug,
- secret: slug === FACTORY_LINEAR_SLUG ? LINEAR_SECRET : GITHUB_SECRET,
- delivery: "broker",
- host: "api.example.com",
- deployments: false,
- enabled: true,
- ...over,
- };
-}
-
-function fakeReader(records: DecryptedServiceCredential[]): {
- reader: ServiceCredentialReader;
- calls: [string, string][];
-} {
- const bySlug = new Map(records.map((rec) => [rec.slug, rec]));
- const calls: [string, string][] = [];
- return {
- reader: {
- getServiceCredentialSecret: async (orgScopeId, slug) => {
- calls.push([orgScopeId, slug]);
- return bySlug.get(slug) ?? null;
- },
- },
- calls,
- };
-}
-
-test("returns each trimmed secret in its own field whatever the delivery mode", async () => {
- const { reader, calls } = fakeReader([
- record(FACTORY_LINEAR_SLUG, { secret: ` ${LINEAR_SECRET}\n`, delivery: "env", envKey: "FACTORY_LINEAR_ENV_KEY" }),
- record(FACTORY_GITHUB_SLUG),
- ]);
-
- const result = await readFactoryCredentials(reader, ORG);
-
- assert.deepEqual(result, { ok: true, linearApiKey: LINEAR_SECRET, githubToken: GITHUB_SECRET });
- assert.deepEqual(calls, [
- [ORG, "factory-linear"],
- [ORG, "factory-github"],
- ]);
-});
-
-const unusable: { label: string; over: Partial<DecryptedServiceCredential> | null }[] = [
- { label: "an absent", over: null },
- { label: "a disabled", over: { enabled: false } },
- { label: "a blank-secret", over: { secret: " \t\n" } },
- { label: "an empty-secret", over: { secret: "" } },
-];
-
-for (const { label, over } of unusable) {
- test(`${label} credential is reported missing by slug, alone or alongside the other`, async () => {
- const sides: [string, string][] = [
- [FACTORY_LINEAR_SLUG, FACTORY_GITHUB_SLUG],
- [FACTORY_GITHUB_SLUG, FACTORY_LINEAR_SLUG],
- ];
- for (const [broken, healthy] of sides) {
- const { reader } = fakeReader(over ? [record(broken, over), record(healthy)] : [record(healthy)]);
-
- const result = await readFactoryCredentials(reader, ORG);
-
- assert.deepEqual(result, { ok: false, missing: [broken] });
- assert.deepEqual(Object.keys(result), ["ok", "missing"]);
- }
-
- const { reader } = fakeReader(over ? [record(FACTORY_LINEAR_SLUG, over), record(FACTORY_GITHUB_SLUG, over)] : []);
-
- assert.deepEqual(await readFactoryCredentials(reader, ORG), {
- ok: false,
- missing: [FACTORY_LINEAR_SLUG, FACTORY_GITHUB_SLUG],
- });
- });
-}
-
-test("both credentials unusable names both slugs and carries no secret material", async () => {
- const { reader } = fakeReader([
- record(FACTORY_LINEAR_SLUG, { enabled: false }),
- record(FACTORY_GITHUB_SLUG, { enabled: false }),
- ]);
- const levels = ["log", "warn", "error", "info", "debug"] as const;
- const original = levels.map((level) => [level, console[level]] as const);
- const logged: string[] = [];
- let result: FactoryCredentials;
- try {
- for (const level of levels) {
- console[level] = (...args: unknown[]) => {
- logged.push(args.join(" "));
- };
- }
- result = await readFactoryCredentials(reader, ORG);
- } finally {
- for (const [level, fn] of original) console[level] = fn;
- }
-
- assert.deepEqual(result, { ok: false, missing: [FACTORY_LINEAR_SLUG, FACTORY_GITHUB_SLUG] });
- const serialized = JSON.stringify(result);
- assert.ok(!serialized.includes(LINEAR_SECRET) && !serialized.includes(GITHUB_SECRET), "result carries a secret");
- assert.deepEqual(logged, []);
-});
diff --git a/test/loop-factory-effects.test.ts b/test/loop-factory-effects.test.ts
deleted file mode 100644
index d2d00df06..000000000
--- a/test/loop-factory-effects.test.ts
+++ /dev/null
@@ -1,709 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import {
- createFactoryLoopEffects,
- loadFactoryContext,
- type FactoryContext,
- type FactoryEffectsDeps,
- type FactoryWorkEffects,
-} from "../src/loops/factory/effects.ts";
-import { FACTORY_REQUIRED_TOOLS } from "../src/loops/factory/preflight.ts";
-import { FACTORY_GITHUB_SLUG, FACTORY_LINEAR_SLUG } from "../src/loops/factory/credentials.ts";
-import { renderFactoryEnv } from "../src/loops/factory/process-work.ts";
-import { LINEAR_GRAPHQL_URL } from "../src/loops/factory/linear-intake.ts";
-import { FORGE_CHECKS } from "../src/loops/factory/forge-evaluate.ts";
-import type { DecryptedServiceCredential, ServiceCredentialReader } from "../src/credentials/keychain.ts";
-import type { FactoryConfig } from "../src/resolution/config-store.ts";
-import type {
- ProcessState,
- ReadProcessResult,
- Sandbox,
- SandboxHandle,
- StartProcessOptions,
- TeardownOptions,
-} from "../src/sandbox/sandbox.ts";
-import type { Loop, LoopItem, LoopState, WorkspaceLayer } from "../src/types.ts";
-
-const ORG_SCOPE = "org:acme";
-const LINEAR_KEY = "lin_FAKE_KEY";
-const GITHUB_TOKEN = "ghp_FAKE_TOKEN";
-const REPO_DIR = "/workspace/repo";
-const TICKET = "QM-12";
-
-const CONFIG: FactoryConfig = {
- forge: "github",
- publishProject: "acme/app",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/acme/app.git",
- linearTeamId: "TEAM-1",
- sourceAppDirs: "src",
- sourceTestRe: "\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "npm test --",
- verifyLintCmd: "npm run lint",
- bugbotRequired: true,
- followupsEnabled: false,
-};
-
-const LOOP: Loop = {
- id: "loop-1",
- ownerScopeId: "personal:U1",
- owner: "U1",
- createdBy: "U1",
- enabled: true,
- createdAt: 1_000,
- name: "factory",
- playbook: "ship the ticket",
- playbookVersion: 1,
- playbookHistory: [],
- policyVersion: 1,
- successCondition: "pull request converged",
- shipActions: [{ action: "open_pr", gate: "hold" }],
- state: "enabled",
- health: "healthy",
-};
-
-const ITEM: LoopItem = {
- id: "item-1",
- loopId: LOOP.id,
- sourceKey: TICKET,
- status: "in_progress",
- attempts: 0,
- runIds: [],
- outputIds: [],
- createdAt: 1_000,
- updatedAt: 1_000,
-};
-
-const WORK_STDOUT = "working\nBRANCH:fix/qm-12\nMR:42\n";
-
-const HEAD_SHA = "1111111111111111111111111111111111111111";
-
-const GH_PULL = "https://api.github.com/repos/acme/app/pulls/42";
-
-const convergedForge = (): Response[] => [
- Response.json({ head: { sha: HEAD_SHA }, mergeable: true, mergeable_state: "clean" }),
- Response.json({ commit: { sha: HEAD_SHA } }),
- Response.json({ check_runs: [{ name: "test", status: "completed", conclusion: "success" }] }),
- Response.json({ data: { repository: { pullRequest: { reviewThreads: { nodes: [] } } } } }),
- Response.json([{ user: { login: "cursor[bot]" }, commit_id: HEAD_SHA }]),
-];
-
-const OPEN_PR_ARTIFACT = {
- shipAction: "open_pr",
- title: "QM-12: pull request #42",
- label: "fix/qm-12",
- externalRef: "https://github.com/acme/app/pull/42",
- capturedBy: "classifier",
-};
-
-type Call =
- | { op: "provision"; layers: WorkspaceLayer[]; handle: SandboxHandle }
- | { op: "run"; handle: SandboxHandle; command: string }
- | { op: "startProcess"; handle: SandboxHandle; command: string; opts: StartProcessOptions | undefined }
- | { op: "readProcess"; handle: SandboxHandle; processId: string }
- | { op: "signalProcess"; handle: SandboxHandle; processId: string; signal: string }
- | { op: "teardown"; handle: SandboxHandle; opts: TeardownOptions | undefined };
-
-interface FakeSandbox {
- sandbox: Sandbox;
- calls: Call[];
-}
-
-const probeStdout = (missing: string[]): string =>
- `${FACTORY_REQUIRED_TOOLS.filter((tool) => !missing.includes(tool))
- .map((tool) => `${tool}=ok 1.0`)
- .join("\n")}\n`;
-
-function fakeSandbox(
- opts: {
- processSessions?: boolean;
- probeMissing?: string[];
- stdout?: string;
- teardownError?: Error;
- read?: (n: number, calls: Call[]) => Promise<ReadProcessResult>;
- } = {},
-): FakeSandbox {
- const calls: Call[] = [];
- const stdout = opts.stdout ?? WORK_STDOUT;
- const running: ProcessState = { state: "running" };
- const exited: ProcessState = { state: "exited", code: 0 };
- const defaultRead = async (n: number): Promise<ReadProcessResult> =>
- n === 1
- ? { chunks: stdout, cursor: stdout.length, status: running }
- : { chunks: "", cursor: stdout.length, status: exited };
- const unused = (name: string) => () => Promise.reject(new Error(`the factory effects must not call ${name}`));
- let provisioned = 0;
- const sandbox: Record<string, unknown> = {
- profile: {
- backend: "fake",
- writablePersistence: "resident_disk",
- processSessions: opts.processSessions ?? true,
- },
- provision: (layers: WorkspaceLayer[]) => {
- provisioned += 1;
- const handle: SandboxHandle = { id: `sbx-${provisioned}`, rootDir: "/workspace" };
- calls.push({ op: "provision", layers, handle });
- return Promise.resolve(handle);
- },
- run: (handle: SandboxHandle, command: string) => {
- calls.push({ op: "run", handle, command });
- return Promise.resolve({
- stdout: probeStdout(opts.probeMissing ?? []),
- stderr: "",
- code: 0,
- timedOut: false,
- });
- },
- readFile: unused("readFile"),
- writeFile: unused("writeFile"),
- writeFileBytes: unused("writeFileBytes"),
- readFileBytes: unused("readFileBytes"),
- listDir: unused("listDir"),
- removeDir: unused("removeDir"),
- startProcess: (handle: SandboxHandle, command: string, startOpts?: StartProcessOptions) => {
- calls.push({ op: "startProcess", handle, command, opts: startOpts });
- return Promise.resolve({ processId: "p1" });
- },
- readProcess: (handle: SandboxHandle, processId: string) => {
- calls.push({ op: "readProcess", handle, processId });
- const n = calls.filter((call) => call.op === "readProcess").length;
- return (opts.read ?? defaultRead)(n, calls);
- },
- writeStdin: unused("writeStdin"),
- signalProcess: (handle: SandboxHandle, processId: string, signal: string) => {
- calls.push({ op: "signalProcess", handle, processId, signal });
- return Promise.resolve();
- },
- listProcesses: unused("listProcesses"),
- teardown: (handle: SandboxHandle, teardownOpts?: TeardownOptions) => {
- calls.push({ op: "teardown", handle, opts: teardownOpts });
- return opts.teardownError ? Promise.reject(opts.teardownError) : Promise.resolve();
- },
- };
- return { sandbox: sandbox as unknown as Sandbox, calls };
-}
-
-const ops = (calls: Call[]): string[] => calls.map((call) => call.op);
-
-function fakeConfig(initial: FactoryConfig | null): {
- config: FactoryEffectsDeps["config"];
- set: (c: FactoryConfig | null) => void;
-} {
- let current = initial;
- return {
- config: { getFactoryConfig: () => current },
- set: (c) => {
- current = c;
- },
- };
-}
-
-const credentialRecord = (
- slug: string,
- over: Partial<DecryptedServiceCredential> = {},
-): DecryptedServiceCredential => ({
- slug,
- name: slug,
- secret: slug === FACTORY_LINEAR_SLUG ? LINEAR_KEY : GITHUB_TOKEN,
- delivery: "broker",
- host: "api.example.com",
- deployments: false,
- enabled: true,
- ...over,
-});
-
-function fakeCredentials(records: (DecryptedServiceCredential | null)[]): ServiceCredentialReader {
- const bySlug = new Map(records.filter((rec) => rec !== null).map((rec) => [rec.slug, rec]));
- return { getServiceCredentialSecret: async (_org, slug) => bySlug.get(slug) ?? null };
-}
-
-const healthyCredentials = (): ServiceCredentialReader =>
- fakeCredentials([credentialRecord(FACTORY_LINEAR_SLUG), credentialRecord(FACTORY_GITHUB_SLUG)]);
-
-function fakeLoops(states: (LoopState | null)[]): { loops: FactoryEffectsDeps["loops"]; ids: string[] } {
- const ids: string[] = [];
- return {
- loops: {
- get: async (id) => {
- ids.push(id);
- const state = states[Math.min(ids.length - 1, states.length - 1)];
- return state === null || state === undefined ? null : { ...LOOP, state };
- },
- },
- ids,
- };
-}
-
-interface FakeFetch {
- fetch: typeof globalThis.fetch;
- calls: { url: string; init: RequestInit | undefined }[];
-}
-
-function fakeFetch(responses: Response[]): FakeFetch {
- const calls: { url: string; init: RequestInit | undefined }[] = [];
- return {
- calls,
- fetch: (url, init) => {
- calls.push({ url: String(url), init });
- const response = responses[calls.length - 1];
- if (!response) return Promise.reject(new Error(`unscripted fetch #${calls.length}`));
- return Promise.resolve(response);
- },
- };
-}
-
-const intakePage = (identifiers: string[]): Response =>
- Response.json({
- data: {
- team: {
- issues: {
- nodes: identifiers.map((identifier, index) => ({
- identifier,
- title: `${identifier} title`,
- createdAt: `2026-01-0${index + 1}T00:00:00.000Z`,
- inverseRelations: { nodes: [] },
- })),
- pageInfo: { hasNextPage: false, endCursor: null },
- },
- },
- },
- });
-
-function deps(over: Partial<FactoryEffectsDeps> = {}): FactoryEffectsDeps {
- return {
- sandbox: fakeSandbox().sandbox,
- config: fakeConfig(CONFIG).config,
- credentials: healthyCredentials(),
- orgScopeId: ORG_SCOPE,
- loops: fakeLoops(["enabled"]).loops,
- ...over,
- };
-}
-
-async function rejection(promise: Promise<unknown>): Promise<Error> {
- try {
- await promise;
- } catch (e) {
- return e as Error;
- }
- throw new Error("expected the call to reject");
-}
-
-async function workedRunId(effects: FactoryWorkEffects, item: LoopItem = ITEM): Promise<string> {
- const { runId } = await effects.work({ loop: LOOP, item });
- return runId;
-}
-
-test("the composed object exposes exactly the four work-side effects and does no I/O to build them", async () => {
- const fake = fakeSandbox();
- const fetched = fakeFetch([intakePage(["QM-12", "QM-13"])]);
- const config = fakeConfig(CONFIG);
- const effects = createFactoryLoopEffects(
- deps({ sandbox: fake.sandbox, config: config.config, fetch: fetched.fetch }),
- );
-
- assert.deepEqual(Object.keys(effects).sort(), ["captureOutputs", "enumerate", "evaluate", "work"]);
- assert.equal("ship" in effects, false);
- assert.equal("authorizeAutoShip" in effects, false);
- assert.deepEqual(fake.calls, []);
- assert.equal(fetched.calls.length, 0);
-
- const candidates = await effects.enumerate(LOOP);
- assert.deepEqual(candidates, [
- { sourceKey: "QM-12", sourceSummary: "QM-12 title" },
- { sourceKey: "QM-13", sourceSummary: "QM-13 title" },
- ]);
- assert.deepEqual(fake.calls, []);
-
- const call = fetched.calls[0];
- assert.ok(call);
- assert.equal(call.url, LINEAR_GRAPHQL_URL);
- assert.equal(new Headers(call.init?.headers).get("Authorization"), LINEAR_KEY);
- const body = JSON.parse(String(call.init?.body)) as { variables: Record<string, unknown> };
- assert.equal(body.variables.teamId, "TEAM-1");
-});
-
-test("enumerate re-reads the factory context on every call rather than caching it", async () => {
- const fetched = fakeFetch([intakePage(["QM-12"]), intakePage(["QM-20"])]);
- const config = fakeConfig(CONFIG);
- const effects = createFactoryLoopEffects(deps({ config: config.config, fetch: fetched.fetch }));
-
- await effects.enumerate(LOOP);
- config.set({ ...CONFIG, linearTeamId: "TEAM-2" });
- await effects.enumerate(LOOP);
-
- const teamIds = fetched.calls.map(
- (call) => (JSON.parse(String(call.init?.body)) as { variables: { teamId: string } }).variables.teamId,
- );
- assert.deepEqual(teamIds, ["TEAM-1", "TEAM-2"]);
-});
-
-test("a Linear failure propagates unwrapped out of enumerate", async () => {
- const fetched = fakeFetch([new Response("nope", { status: 500 })]);
- const effects = createFactoryLoopEffects(deps({ fetch: fetched.fetch }));
- const error = await rejection(effects.enumerate(LOOP));
- assert.equal(error.message, "linear_intake_failed: 500");
-});
-
-test("loadFactoryContext resolves the org config and both credentials", async () => {
- const context: FactoryContext = await loadFactoryContext(deps());
- assert.deepEqual(context, { config: CONFIG, linearApiKey: LINEAR_KEY, githubToken: GITHUB_TOKEN });
-});
-
-test("a missing factory config fails every entry point before any sandbox or network call", async () => {
- const fake = fakeSandbox();
- const fetched = fakeFetch([]);
- const base = deps({ sandbox: fake.sandbox, config: fakeConfig(null).config, fetch: fetched.fetch });
- const effects = createFactoryLoopEffects(base);
-
- for (const promise of [loadFactoryContext(base), effects.enumerate(LOOP), workedRunId(effects)]) {
- assert.equal((await rejection(promise)).message, "factory_config_missing");
- }
- assert.deepEqual(fake.calls, []);
- assert.equal(fetched.calls.length, 0);
-});
-
-test("missing credentials name every absent slug, linear first, without leaking a secret", async () => {
- const fake = fakeSandbox();
- const base = deps({
- sandbox: fake.sandbox,
- credentials: fakeCredentials([credentialRecord(FACTORY_GITHUB_SLUG)]),
- });
- const effects = createFactoryLoopEffects(base);
- for (const promise of [loadFactoryContext(base), effects.enumerate(LOOP), workedRunId(effects)]) {
- const error = await rejection(promise);
- assert.equal(error.message, `factory_credentials_missing: ${FACTORY_LINEAR_SLUG}`);
- assert.equal(error.message.includes(GITHUB_TOKEN), false);
- }
-
- const both = createFactoryLoopEffects(
- deps({
- sandbox: fake.sandbox,
- credentials: fakeCredentials([
- credentialRecord(FACTORY_LINEAR_SLUG, { secret: " " }),
- credentialRecord(FACTORY_GITHUB_SLUG, { enabled: false }),
- ]),
- }),
- );
- const error = await rejection(both.enumerate(LOOP));
- assert.equal(error.message, `factory_credentials_missing: ${FACTORY_LINEAR_SLUG}, ${FACTORY_GITHUB_SLUG}`);
- assert.deepEqual(fake.calls, []);
-});
-
-test("work preflights on a warm-released handle, then runs the wrapper with the rendered env", async () => {
- const fake = fakeSandbox();
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox }));
-
- const { runId } = await effects.work({ loop: LOOP, item: ITEM, guidance: "smaller diff please" });
-
- assert.deepEqual(ops(fake.calls), [
- "provision",
- "run",
- "teardown",
- "provision",
- "startProcess",
- "readProcess",
- "readProcess",
- "readProcess",
- "teardown",
- ]);
- const preflightTeardown = fake.calls[2];
- assert.ok(preflightTeardown?.op === "teardown");
- assert.deepEqual(preflightTeardown.opts, { keepWarm: true });
- const provision = fake.calls[0];
- assert.ok(provision?.op === "provision");
- assert.deepEqual(provision.layers, [{ scopeId: LOOP.ownerScopeId, mode: "rw", mountPath: "" }]);
-
- const started = fake.calls[4];
- assert.ok(started?.op === "startProcess");
- assert.equal(started.command, `bash .claude/io-coding-agent-js.sh ${TICKET}`);
- assert.equal(started.opts?.cwd, REPO_DIR);
- assert.notEqual(started.handle.id, preflightTeardown.handle.id);
- assert.deepEqual(
- started.opts?.env,
- renderFactoryEnv({
- config: CONFIG,
- guidance: "smaller diff please",
- linearApiKey: LINEAR_KEY,
- githubToken: GITHUB_TOKEN,
- repoDir: REPO_DIR,
- }),
- );
- assert.equal(runId, "factory:loop-1:item-1:1");
-});
-
-test("work omits IO_FEEDBACK without guidance, honours repoDir, and numbers the run from the item's attempts", async () => {
- const fake = fakeSandbox();
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, repoDir: "/srv/code" }));
-
- const runId = await workedRunId(effects, { ...ITEM, attempts: 2 });
-
- const started = fake.calls.find((call) => call.op === "startProcess");
- assert.ok(started?.op === "startProcess");
- assert.equal(started.opts?.cwd, "/srv/code");
- assert.equal("IO_FEEDBACK" in (started.opts?.env ?? {}), false);
- assert.equal(started.opts?.env?.IO_REPO_DIR, "/srv/code");
- assert.equal(runId, "factory:loop-1:item-1:3");
-});
-
-test("a preflight that misses tools names them and never starts the wrapper", async () => {
- const fake = fakeSandbox({ probeMissing: ["jq", "gh"] });
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox }));
-
- const error = await rejection(workedRunId(effects));
-
- assert.equal(error.message, "factory_preflight_failed: missing_tools: gh, jq");
- assert.deepEqual(ops(fake.calls), ["provision", "run", "teardown"]);
-});
-
-test("a sandbox without process sessions fails preflight instead of leaking a capability error", async () => {
- const fake = fakeSandbox({ processSessions: false });
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox }));
-
- const error = await rejection(workedRunId(effects));
-
- assert.equal(error.message, "factory_preflight_failed: no_process_sessions");
- assert.equal(error.name, "Error");
- assert.deepEqual(ops(fake.calls), ["provision", "teardown"]);
-});
-
-test("captureOutputs parses the stored stdout for the returned runId and yields [] for anything else", async () => {
- const fake = fakeSandbox();
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox }));
- const runId = await workedRunId(effects);
-
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId }), [OPEN_PR_ARTIFACT]);
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId: "factory:nope:nope:1" }), []);
-});
-
-test("captureOutputs reports an already-fixed run and an empty run from the same stored stdout", async () => {
- const silent = createFactoryLoopEffects(deps({ sandbox: fakeSandbox({ stdout: "nothing here\n" }).sandbox }));
- assert.deepEqual(await silent.captureOutputs({ loop: LOOP, item: ITEM, runId: await workedRunId(silent) }), []);
-
- const fixed = createFactoryLoopEffects(
- deps({ sandbox: fakeSandbox({ stdout: "ALREADY_FIXED:true\nALREADY_FIXED_EVIDENCE:landed in main\n" }).sandbox }),
- );
- assert.deepEqual(await fixed.captureOutputs({ loop: LOOP, item: ITEM, runId: await workedRunId(fixed) }), [
- {
- shipAction: "close_already_fixed",
- title: "QM-12: already fixed",
- capturedBy: "classifier",
- summary: "landed in main",
- },
- ]);
-});
-
-test("captureOutputs parses against the forge the run executed with", async () => {
- const config = fakeConfig({ ...CONFIG, forge: "gitlab", publishProject: "acme%2Fapp" });
- const effects = createFactoryLoopEffects(deps({ config: config.config }));
- const runId = await workedRunId(effects);
-
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId }), [
- { ...OPEN_PR_ARTIFACT, externalRef: "https://gitlab.com/acme/app/-/merge_requests/42" },
- ]);
-});
-
-test("evaluate reads the pull request from the forge and provisions no sandbox", async () => {
- const fake = fakeSandbox();
- const fetched = fakeFetch(convergedForge());
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
- const before = fake.calls.length;
-
- const verdict = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId });
-
- assert.equal(verdict.outcome, "met");
- assert.equal(verdict.reason, "converged");
- assert.equal(verdict.judged, false);
- assert.deepEqual(
- verdict.checks.map((check) => check.command),
- [...FORGE_CHECKS],
- );
- assert.equal(fake.calls.length, before);
- const first = fetched.calls[0];
- assert.ok(first);
- assert.equal(first.url, GH_PULL);
- assert.equal(fetched.calls[1]?.url, "https://api.github.com/repos/acme/app/branches/fix/qm-12");
- assert.equal(new Headers(first.init?.headers).get("Authorization"), `Bearer ${GITHUB_TOKEN}`);
-});
-
-test("evaluate reports the first failing forge check without an attempt cap", async () => {
- const fetched = fakeFetch([
- Response.json({ head: { sha: HEAD_SHA }, mergeable: true, mergeable_state: "clean" }),
- Response.json({ commit: { sha: HEAD_SHA } }),
- Response.json({ check_runs: [{ name: "test", status: "completed", conclusion: "failure" }] }),
- ]);
- const effects = createFactoryLoopEffects(deps({ fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
-
- const verdict = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 9, runId });
-
- assert.deepEqual(verdict, {
- outcome: "continue",
- reason: "check failed: ci_green_on_head — test",
- judged: false,
- checks: [
- { command: "exact_head", passed: true },
- { command: "ci_green_on_head", passed: false, detail: "test" },
- ],
- });
-});
-
-test("evaluate settles an already-fixed run as met without touching the forge or the sandbox", async () => {
- const fake = fakeSandbox({ stdout: "ALREADY_FIXED:true\nALREADY_FIXED_EVIDENCE:landed in main\n" });
- const fetched = fakeFetch([]);
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
- const before = fake.calls.length;
-
- const verdict = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId });
-
- assert.deepEqual(verdict, { outcome: "met", reason: "already fixed", checks: [], judged: false });
- assert.equal(fake.calls.length, before);
- assert.equal(fetched.calls.length, 0);
-});
-
-test("evaluate uses the config the run executed with, not a config edited since", async () => {
- const config = fakeConfig(CONFIG);
- const fetched = fakeFetch(convergedForge());
- const effects = createFactoryLoopEffects(deps({ config: config.config, fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
-
- config.set({ ...CONFIG, forge: "gitlab", publishProject: "acme%2Fapp", bugbotRequired: false });
- const verdict = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId });
-
- assert.equal(verdict.outcome, "met");
- assert.equal(fetched.calls[0]?.url, GH_PULL);
- assert.equal(fetched.calls.length, 5);
-});
-
-test("a forge failure propagates unwrapped out of evaluate", async () => {
- const fetched = fakeFetch([new Response("nope", { status: 403 })]);
- const effects = createFactoryLoopEffects(deps({ fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
-
- const error = await rejection(effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId }));
-
- assert.equal(error.message, "forge_evaluate_failed: 403");
- assert.equal(error.message.includes(GITHUB_TOKEN), false);
-});
-
-test("evaluate releases the stored run whatever the outcome", async () => {
- for (const responses of [convergedForge(), [new Response("nope", { status: 403 })]]) {
- const effects = createFactoryLoopEffects(deps({ fetch: fakeFetch(responses).fetch }));
- const runId = await workedRunId(effects);
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId }), [OPEN_PR_ARTIFACT]);
-
- await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId }).catch(() => undefined);
-
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId }), []);
- assert.deepEqual(await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId }), {
- outcome: "continue",
- reason: "no pull request",
- checks: [],
- judged: false,
- });
- }
-});
-
-test("a fresh attempt for an item evicts the previous attempt's stored run and leaves other items alone", async () => {
- const perRunStdout = async (_n: number, calls: Call[]): Promise<ReadProcessResult> => {
- const since = calls.slice(calls.map((call) => call.op).lastIndexOf("startProcess"));
- return since.filter((call) => call.op === "readProcess").length === 1
- ? { chunks: WORK_STDOUT, cursor: WORK_STDOUT.length, status: { state: "running" } }
- : { chunks: "", cursor: WORK_STDOUT.length, status: { state: "exited", code: 0 } };
- };
- const effects = createFactoryLoopEffects(deps({ sandbox: fakeSandbox({ read: perRunStdout }).sandbox }));
- const other = { ...ITEM, id: "item-2" };
-
- const first = await workedRunId(effects);
- const otherRunId = await workedRunId(effects, other);
- const second = await workedRunId(effects, { ...ITEM, attempts: 1 });
-
- assert.equal(first, "factory:loop-1:item-1:1");
- assert.equal(second, "factory:loop-1:item-1:2");
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId: first }), []);
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId: second }), [OPEN_PR_ARTIFACT]);
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: other, runId: otherRunId }), [OPEN_PR_ARTIFACT]);
-});
-
-test("a teardown that rejects neither loses the run nor masks a preflight failure", async () => {
- const teardownError = new Error("warm release refused");
- const fake = fakeSandbox({ teardownError });
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox }));
-
- const runId = await workedRunId(effects);
-
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId }), [OPEN_PR_ARTIFACT]);
- assert.equal(fake.calls.filter((call) => call.op === "teardown").length, 2);
-
- const missingTool = createFactoryLoopEffects(
- deps({ sandbox: fakeSandbox({ teardownError, probeMissing: ["gh"] }).sandbox }),
- );
- assert.equal((await rejection(workedRunId(missingTool))).message, "factory_preflight_failed: missing_tools: gh");
-});
-
-test("evaluate returns the no-pull-request verdict without provisioning when there is no open_pr", async () => {
- const fake = fakeSandbox({ stdout: "nothing here\n" });
- const fetched = fakeFetch([]);
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, fetch: fetched.fetch }));
- const runId = await workedRunId(effects);
- const before = fake.calls.length;
-
- const noPr = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId });
- const unknown = await effects.evaluate({ loop: LOOP, item: ITEM, attempt: 1, runId: "factory:nope:nope:1" });
-
- const expected = { outcome: "continue", reason: "no pull request", checks: [], judged: false };
- assert.deepEqual(noPr, expected);
- assert.deepEqual(unknown, expected);
- assert.notEqual(noPr.checks, unknown.checks);
- assert.equal(fake.calls.length, before);
- assert.equal(fetched.calls.length, 0);
-});
-
-test("pausing the loop mid-run terminates the process, rejects work, and stores nothing", async () => {
- for (const state of [null, "paused", "archived", "quarantined"] as const) {
- const fake = fakeSandbox({
- read: async (_n, calls) => {
- const terminated = calls.some((call) => call.op === "signalProcess");
- if (terminated) return { chunks: "", cursor: 0, status: { state: "exited", code: 143 } };
- await new Promise((resolve) => setTimeout(resolve, 5));
- return { chunks: "", cursor: 0, status: { state: "running" } };
- },
- });
- const loops = fakeLoops(["enabled", state]);
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, loops: loops.loops, pausePollMs: 5 }));
-
- const error = await rejection(effects.work({ loop: LOOP, item: ITEM }));
-
- assert.equal(error.message, "factory_run_aborted", `state ${String(state)}`);
- assert.ok(loops.ids.length >= 2, `state ${String(state)} polled ${loops.ids.length} times`);
- assert.deepEqual(new Set(loops.ids), new Set([LOOP.id]));
- const signal = fake.calls.find((call) => call.op === "signalProcess");
- assert.ok(signal?.op === "signalProcess");
- assert.equal(signal.signal, "TERM");
- assert.equal(signal.processId, "p1");
- const started = fake.calls.find((call) => call.op === "startProcess");
- assert.ok(started?.op === "startProcess");
- assert.equal(signal.handle.id, started.handle.id);
- assert.deepEqual(await effects.captureOutputs({ loop: LOOP, item: ITEM, runId: "factory:loop-1:item-1:1" }), []);
- }
-});
-
-test("an enabled loop is never signalled and the pause poll stops when the process returns", async () => {
- const fake = fakeSandbox();
- const loops = fakeLoops(["enabled"]);
- const effects = createFactoryLoopEffects(deps({ sandbox: fake.sandbox, loops: loops.loops, pausePollMs: 1 }));
-
- const runId = await workedRunId(effects);
- const polledAtReturn = loops.ids.length;
- await new Promise((resolve) => setTimeout(resolve, 40));
-
- assert.equal(runId, "factory:loop-1:item-1:1");
- assert.equal(
- fake.calls.some((call) => call.op === "signalProcess"),
- false,
- );
- assert.equal(loops.ids.length, polledAtReturn);
-});
diff --git a/test/loop-factory-evaluate.test.ts b/test/loop-factory-evaluate.test.ts
deleted file mode 100644
index 9c86cb7ab..000000000
--- a/test/loop-factory-evaluate.test.ts
+++ /dev/null
@@ -1,216 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import { evaluateFactoryItem, parseVector } from "../src/loops/factory/evaluate.ts";
-import type { FactoryEvaluateInput } from "../src/loops/factory/evaluate.ts";
-import type { ExecResult } from "../src/sandbox/sandbox.ts";
-import type { SuccessVerdict } from "../src/loops/success-evaluation.ts";
-
-const SHA = "9bd011dab002ff48f39f0a65e5c56f8f44920880";
-const MR_NUMBER = 2311;
-const BRANCH = "qm-12-s17868";
-
-const GREEN_VECTOR: Record<string, string> = {
- head_sha: SHA,
- mr_state: "opened",
- source_branch: BRANCH,
- exact_head: "true",
- exact_head_detail: "match",
- ci_green_on_head: "true",
- ci_green_on_head_detail: "success",
- ledger_clean: "true",
- ledger_clean_detail: "no_open_comments",
- mergeable: "true",
- mergeable_detail: "can_be_merged",
- vector_readable: "true",
-};
-
-const vectorStdout = (overrides: Record<string, string | null> = {}): string => {
- const lines: string[] = [];
- for (const [key, value] of Object.entries(GREEN_VECTOR)) {
- const resolved = Object.hasOwn(overrides, key) ? overrides[key] : value;
- if (resolved !== null && resolved !== undefined) lines.push(`${key}=${resolved}`);
- }
- for (const [key, value] of Object.entries(overrides)) {
- if (Object.hasOwn(GREEN_VECTOR, key) || value === null) continue;
- lines.push(`${key}=${value}`);
- }
- return `${lines.join("\n")}\n`;
-};
-
-const recorder = (result: Partial<ExecResult> = {}) => {
- const commands: string[] = [];
- const argCounts: number[] = [];
- const exec = async (...args: unknown[]): Promise<ExecResult> => {
- commands.push(args[0] as string);
- argCounts.push(args.length);
- return {
- stdout: vectorStdout(),
- stderr: "",
- code: 0,
- timedOut: false,
- pressure: { ioFull10: 0, ioFull60: 0, load1: 0 },
- ...result,
- };
- };
- return { exec, commands, argCounts };
-};
-
-const evaluate = (
- exec: FactoryEvaluateInput["exec"],
- overrides: Partial<FactoryEvaluateInput> = {},
-): Promise<SuccessVerdict> =>
- evaluateFactoryItem({ exec, attempt: 1, mrNumber: MR_NUMBER, branch: BRANCH, ...overrides });
-
-const MET_VERDICT: SuccessVerdict = {
- outcome: "met",
- reason: "all convergence checks passed",
- checks: [
- { command: "exact_head", passed: true, detail: "match" },
- { command: "ci_green_on_head", passed: true, detail: "success" },
- { command: "ledger_clean", passed: true, detail: "no_open_comments" },
- { command: "mergeable", passed: true, detail: "can_be_merged" },
- ],
- judged: true,
-};
-
-test("a fully green vector converges, and chatter on stderr does not change that", async () => {
- const run = recorder({ stdout: vectorStdout(), stderr: "warning: refreshing token" });
-
- const verdict = await evaluate(run.exec, { headSha: SHA });
-
- assert.deepEqual(verdict, MET_VERDICT);
- assert.deepEqual(run.argCounts, [1]);
-});
-
-test("the first check that is not exactly true stops the run and carries its detail", async () => {
- const run = recorder({
- stdout: vectorStdout({ ci_green_on_head: "false", ci_green_on_head_detail: "pipeline_running" }),
- });
-
- const verdict = await evaluate(run.exec, { headSha: SHA });
-
- assert.deepEqual(verdict, {
- outcome: "continue",
- reason: "check failed: ci_green_on_head — pipeline_running",
- checks: [
- { command: "exact_head", passed: true, detail: "match" },
- { command: "ci_green_on_head", passed: false, detail: "pipeline_running" },
- ],
- judged: false,
- });
- assert.equal(run.commands.length, 1);
-});
-
-test("no value other than the exact string true is read as a passing check", async () => {
- for (const token of ["unknown", "TRUE", " true", "true\r"]) {
- const run = recorder({ stdout: vectorStdout({ mergeable: token }) });
-
- const verdict = await evaluate(run.exec);
-
- assert.equal(verdict.outcome, "continue", `token ${JSON.stringify(token)} must not pass`);
- assert.equal(verdict.judged, false);
- assert.deepEqual(verdict.checks.at(-1), { command: "mergeable", passed: false, detail: "can_be_merged" });
- }
-});
-
-test("a check the script never printed fails instead of passing by omission", async () => {
- const run = recorder({ stdout: vectorStdout({ ledger_clean: null, ledger_clean_detail: null }) });
-
- const verdict = await evaluate(run.exec);
-
- assert.deepEqual(verdict.checks.at(-1), { command: "ledger_clean", passed: false });
- assert.equal(verdict.reason, "check failed: ledger_clean");
- assert.equal(verdict.judged, false);
- assert.equal(verdict.outcome, "continue");
-});
-
-const UNREADABLE_ROWS: { name: string; result: Partial<ExecResult>; detail: string }[] = [
- {
- name: "non-zero exit with nothing on stdout",
- result: { code: 78, stdout: "", stderr: "glab: 401 Unauthorized" },
- detail: "vector_error: exit 78",
- },
- {
- name: "the script reported itself unreadable",
- result: { code: 0, stdout: vectorStdout({ vector_readable: "false", vector_error: "auth" }) },
- detail: "vector_error: auth",
- },
- {
- name: "stdout truncated before vector_readable was printed",
- result: { code: 0, stdout: vectorStdout({ vector_readable: null }) },
- detail: "vector_error: exit 0",
- },
- {
- name: "an all-green stdout behind a non-zero exit",
- result: { code: 78, stdout: vectorStdout(), stderr: "killed by signal" },
- detail: "vector_error: exit 78",
- },
-];
-
-test("an unreadable vector fails closed and never reaches the judge", async () => {
- for (const row of UNREADABLE_ROWS) {
- const run = recorder(row.result);
-
- const verdict = await evaluate(run.exec, { headSha: SHA });
-
- assert.deepEqual(
- verdict,
- {
- outcome: "continue",
- reason: `check failed: exact_head — ${row.detail}`,
- checks: [{ command: "exact_head", passed: false, detail: row.detail }],
- judged: false,
- },
- row.name,
- );
- assert.equal(run.commands.length, 1, row.name);
- }
-});
-
-test("an item is never parked, however many attempts it has taken", async () => {
- for (const attempt of [0, 999]) {
- for (const result of [{ stdout: vectorStdout({ mergeable: "false" }) }, { code: 78, stdout: "" }]) {
- const run = recorder(result);
-
- const verdict = await evaluate(run.exec, { attempt });
-
- assert.equal(verdict.outcome, "continue", `attempt ${attempt}`);
- assert.equal(verdict.reason.includes("attempt cap"), false, `attempt ${attempt}`);
- }
- }
-});
-
-test("the script is run once with every argument shell-quoted", async () => {
- const withSha = recorder();
- await evaluate(withSha.exec, { headSha: SHA });
- assert.deepEqual(withSha.commands, [`bash tools/factory/converge-vector.sh 2311 qm-12-s17868 ${SHA}`]);
-
- for (const headSha of [undefined, ""]) {
- const run = recorder();
- await evaluate(run.exec, { headSha });
- assert.deepEqual(run.commands, ["bash tools/factory/converge-vector.sh 2311 qm-12-s17868"]);
- }
-
- const spacedPath = recorder();
- await evaluate(spacedPath.exec, { scriptPath: "/tmp/my scripts/converge-vector.sh" });
- assert.deepEqual(spacedPath.commands, ["bash '/tmp/my scripts/converge-vector.sh' 2311 qm-12-s17868"]);
-
- for (const [branch, expected] of [
- ["qm-12; rm -rf /", "'qm-12; rm -rf /'"],
- ["it's-a-branch", "'it'\\''s-a-branch'"],
- ]) {
- const run = recorder();
- await evaluate(run.exec, { branch });
- assert.deepEqual(run.commands, [`bash tools/factory/converge-vector.sh 2311 ${expected}`]);
- }
-});
-
-test("parseVector splits on the first equals, keeps the last write, and normalizes nothing", () => {
- const parsed = parseVector("a=1\nb=x=y\nnoise\na=2");
- assert.equal(parsed.size, 2);
- assert.equal(parsed.get("a"), "2");
- assert.equal(parsed.get("b"), "x=y");
-
- assert.equal(parseVector("k=").get("k"), "");
- assert.equal(parseVector("a=1\r\nb=2").get("a"), "1\r");
-});
diff --git a/test/loop-factory-forge-evaluate.test.ts b/test/loop-factory-forge-evaluate.test.ts
deleted file mode 100644
index 3e89c669d..000000000
--- a/test/loop-factory-forge-evaluate.test.ts
+++ /dev/null
@@ -1,506 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import { evaluateFactoryForge, FORGE_CHECKS, type ForgeEvaluateInput } from "../src/loops/factory/forge-evaluate.ts";
-
-const TOKEN = "ght_FAKE_TOKEN";
-const BRANCH = "fix/qm-12";
-const HEAD = "1111111111111111111111111111111111111111";
-const OLD = "2222222222222222222222222222222222222222";
-
-const GH_REPO = "https://api.github.com/repos/acme/app";
-const GH = {
- pr: `GET ${GH_REPO}/pulls/42`,
- branch: `GET ${GH_REPO}/branches/${BRANCH}`,
- checkRuns: `GET ${GH_REPO}/commits/${HEAD}/check-runs?per_page=100`,
- graphql: "POST https://api.github.com/graphql",
- reviews: `GET ${GH_REPO}/pulls/42/reviews?per_page=100`,
-};
-
-const GL_PROJECT = "https://gitlab.com/api/v4/projects/acme%2Fapp";
-const GL = {
- mr: `GET ${GL_PROJECT}/merge_requests/42`,
- branch: `GET ${GL_PROJECT}/repository/branches/fix%2Fqm-12`,
- discussions: `GET ${GL_PROJECT}/merge_requests/42/discussions?per_page=100`,
-};
-
-interface Recorded {
- method: string;
- url: string;
- headers: Record<string, string>;
- body?: string;
-}
-
-interface Route {
- status?: number;
- body?: unknown;
-}
-
-const keyOf = (rec: Recorded): string => `${rec.method} ${rec.url}`;
-
-function fakeFetch(routes: Record<string, Route>): { fetchImpl: typeof fetch; calls: Recorded[] } {
- const calls: Recorded[] = [];
- const fetchImpl = (async (url: string | URL | Request, init?: RequestInit) => {
- const headers: Record<string, string> = {};
- for (const [name, value] of Object.entries((init?.headers ?? {}) as Record<string, string>)) {
- headers[name.toLowerCase()] = value;
- }
- const rec: Recorded = {
- url: String(url),
- method: init?.method ?? "GET",
- headers,
- ...(init?.body === undefined ? {} : { body: String(init.body) }),
- };
- calls.push(rec);
- const route = routes[keyOf(rec)];
- if (route === undefined) throw new Error(`unscripted route: ${keyOf(rec)}`);
- return new Response(JSON.stringify(route.body ?? {}), {
- status: route.status ?? 200,
- headers: { "content-type": "application/json" },
- });
- }) as typeof fetch;
- return { fetchImpl, calls };
-}
-
-const keysOf = (calls: Recorded[]): string[] => calls.map(keyOf);
-
-const threadsBody = (resolved: boolean[]): Route => ({
- body: {
- data: { repository: { pullRequest: { reviewThreads: { nodes: resolved.map((isResolved) => ({ isResolved })) } } } },
- },
-});
-
-const githubRoutes = (over: Record<string, Route> = {}): Record<string, Route> => ({
- [GH.pr]: { body: { head: { sha: HEAD }, mergeable: true, mergeable_state: "clean" } },
- [GH.branch]: { body: { commit: { sha: HEAD } } },
- [GH.checkRuns]: {
- body: {
- check_runs: [
- { name: "test", status: "completed", conclusion: "success" },
- { name: "lint", status: "completed", conclusion: "skipped" },
- ],
- },
- },
- [GH.graphql]: threadsBody([true, true]),
- [GH.reviews]: { body: [{ user: { login: "cursor[bot]" }, commit_id: HEAD }] },
- ...over,
-});
-
-const gitlabRoutes = (over: Record<string, Route> = {}): Record<string, Route> => ({
- [GL.mr]: {
- body: { sha: HEAD, detailed_merge_status: "mergeable", head_pipeline: { id: 9, sha: HEAD, status: "success" } },
- },
- [GL.branch]: { body: { commit: { id: HEAD } } },
- [GL.discussions]: {
- body: [
- { notes: [{ body: "nit", resolvable: true, resolved: true }] },
- {
- notes: [
- {
- body: `BUGBOT_REVIEW clean on ${HEAD}`,
- resolvable: false,
- resolved: false,
- author: { name: "cursor", username: "project_1_bot_abc" },
- },
- ],
- },
- ],
- },
- ...over,
-});
-
-const githubInput = (fetchImpl: typeof fetch, over: Partial<ForgeEvaluateInput> = {}): ForgeEvaluateInput => ({
- fetch: fetchImpl,
- forge: "github",
- publishProject: "acme/app",
- forgeToken: TOKEN,
- number: 42,
- branch: BRANCH,
- bugbotRequired: true,
- ...over,
-});
-
-const gitlabInput = (fetchImpl: typeof fetch, over: Partial<ForgeEvaluateInput> = {}): ForgeEvaluateInput =>
- githubInput(fetchImpl, { forge: "gitlab", publishProject: "acme%2Fapp", ...over });
-
-async function rejection(promise: Promise<unknown>): Promise<Error> {
- try {
- await promise;
- } catch (e) {
- return e as Error;
- }
- throw new Error("expected the call to reject");
-}
-
-test("a converged GitHub pull request meets the condition on forge reads alone", async () => {
- const fetched = fakeFetch(githubRoutes());
-
- const verdict = await evaluateFactoryForge(githubInput(fetched.fetchImpl));
-
- assert.equal(verdict.outcome, "met");
- assert.equal(verdict.reason, "converged");
- assert.equal(verdict.judged, false);
- assert.deepEqual(
- verdict.checks.map((check) => check.command),
- [...FORGE_CHECKS],
- );
- assert.deepEqual(
- verdict.checks.map((check) => check.passed),
- FORGE_CHECKS.map(() => true),
- );
- assert.equal(keysOf(fetched.calls).filter((key) => key === GH.pr).length, 1);
- const graphql = fetched.calls.find((call) => keyOf(call) === GH.graphql);
- const query = String((JSON.parse(graphql?.body ?? "{}") as { query?: string }).query);
- assert.match(query, /repository\(owner: "acme", name: "app"\)/);
- assert.match(query, /pullRequest\(number: 42\) \{ reviewThreads\(first: 100\)/);
- assert.equal(graphql?.headers["content-type"], "application/json");
- for (const call of fetched.calls) {
- assert.equal(call.headers.authorization, `Bearer ${TOKEN}`);
- assert.equal(call.headers.accept, "application/vnd.github+json");
- assert.equal("private-token" in call.headers, false);
- }
-});
-
-test("an unstable or hooked GitHub merge state still converges", async () => {
- for (const state of ["unstable", "has_hooks"]) {
- const fetched = fakeFetch(
- githubRoutes({ [GH.pr]: { body: { head: { sha: HEAD }, mergeable: true, mergeable_state: state } } }),
- );
- const verdict = await evaluateFactoryForge(githubInput(fetched.fetchImpl));
- assert.equal(verdict.outcome, "met", state);
- }
-});
-
-test("bugbot_reviewed passes as not required without reading the reviews", async () => {
- const fetched = fakeFetch(githubRoutes());
-
- const verdict = await evaluateFactoryForge(githubInput(fetched.fetchImpl, { bugbotRequired: false }));
-
- assert.equal(verdict.outcome, "met");
- assert.deepEqual(verdict.checks[4], { command: "bugbot_reviewed", passed: true, detail: "not required" });
- assert.equal(
- keysOf(fetched.calls).some((key) => key.endsWith("/reviews?per_page=100")),
- false,
- );
-});
-
-const GITHUB_FAILURES: {
- name: string;
- detail: string;
- routes: Record<string, Route>;
- reads: string[];
-}[] = [
- {
- name: "exact_head",
- detail: `${HEAD} != ${OLD}`,
- routes: { [GH.branch]: { body: { commit: { sha: OLD } } } },
- reads: [GH.pr, GH.branch],
- },
- {
- name: "exact_head",
- detail: " != ",
- routes: { [GH.pr]: { body: {} }, [GH.branch]: { body: {} } },
- reads: [GH.pr, GH.branch],
- },
- {
- name: "ci_green_on_head",
- detail: "no check runs",
- routes: { [GH.checkRuns]: { body: { check_runs: [] } } },
- reads: [GH.pr, GH.branch, GH.checkRuns],
- },
- {
- name: "ci_green_on_head",
- detail: "slow-suite",
- routes: {
- [GH.checkRuns]: {
- body: {
- check_runs: [
- { name: "test", status: "completed", conclusion: "success" },
- { name: "slow-suite", status: "in_progress", conclusion: null },
- ],
- },
- },
- },
- reads: [GH.pr, GH.branch, GH.checkRuns],
- },
- {
- name: "ci_green_on_head",
- detail: "test",
- routes: {
- [GH.checkRuns]: { body: { check_runs: [{ name: "test", status: "completed", conclusion: "failure" }] } },
- },
- reads: [GH.pr, GH.branch, GH.checkRuns],
- },
- {
- name: "ledger_clean",
- detail: "2 unresolved",
- routes: { [GH.graphql]: threadsBody([true, false, false]) },
- reads: [GH.pr, GH.branch, GH.checkRuns, GH.graphql],
- },
- {
- name: "mergeable",
- detail: "blocked",
- routes: { [GH.pr]: { body: { head: { sha: HEAD }, mergeable: true, mergeable_state: "blocked" } } },
- reads: [GH.pr, GH.branch, GH.checkRuns, GH.graphql],
- },
- {
- name: "mergeable",
- detail: "unknown",
- routes: { [GH.pr]: { body: { head: { sha: HEAD }, mergeable: null, mergeable_state: "unknown" } } },
- reads: [GH.pr, GH.branch, GH.checkRuns, GH.graphql],
- },
- {
- name: "bugbot_reviewed",
- detail: `no bugbot review on ${HEAD}`,
- routes: { [GH.reviews]: { body: [{ user: { login: "cursor[bot]" }, commit_id: OLD }] } },
- reads: [GH.pr, GH.branch, GH.checkRuns, GH.graphql, GH.reviews],
- },
- {
- name: "bugbot_reviewed",
- detail: `no bugbot review on ${HEAD}`,
- routes: { [GH.reviews]: { body: [{ user: { login: "a-human" }, commit_id: HEAD }] } },
- reads: [GH.pr, GH.branch, GH.checkRuns, GH.graphql, GH.reviews],
- },
-];
-
-test("each failing GitHub check stops the sequence at that check and names it", async () => {
- for (const scenario of GITHUB_FAILURES) {
- const label = `${scenario.name} — ${scenario.detail}`;
- const fetched = fakeFetch(githubRoutes(scenario.routes));
-
- const verdict = await evaluateFactoryForge(githubInput(fetched.fetchImpl));
-
- assert.equal(verdict.outcome, "continue", label);
- assert.equal(verdict.reason, `check failed: ${scenario.name} — ${scenario.detail}`, label);
- assert.equal(verdict.judged, false, label);
- const position = FORGE_CHECKS.indexOf(scenario.name as (typeof FORGE_CHECKS)[number]) + 1;
- assert.deepEqual(
- verdict.checks.map((check) => check.command),
- FORGE_CHECKS.slice(0, position),
- label,
- );
- assert.deepEqual(
- verdict.checks.map((check) => check.passed),
- FORGE_CHECKS.slice(0, position).map((_name, index) => index < position - 1),
- label,
- );
- assert.equal(verdict.checks[position - 1]?.detail, scenario.detail, label);
- assert.deepEqual(keysOf(fetched.calls), scenario.reads, label);
- }
-});
-
-test("a converged GitLab merge request meets the condition on its own endpoints", async () => {
- const fetched = fakeFetch(gitlabRoutes());
-
- const verdict = await evaluateFactoryForge(gitlabInput(fetched.fetchImpl));
-
- assert.equal(verdict.outcome, "met");
- assert.deepEqual(keysOf(fetched.calls), [GL.mr, GL.branch, GL.discussions]);
- for (const call of fetched.calls) {
- assert.equal(call.headers["private-token"], TOKEN);
- assert.equal("authorization" in call.headers, false);
- assert.equal(call.url.includes("%252F"), false);
- }
-});
-
-test("the merge request's head_pipeline decides ci_green_on_head, whatever sha it ran on", async () => {
- const mergedResult = fakeFetch(
- gitlabRoutes({
- [GL.mr]: {
- body: { sha: HEAD, detailed_merge_status: "mergeable", head_pipeline: { id: 9, sha: OLD, status: "success" } },
- },
- }),
- );
- assert.equal((await evaluateFactoryForge(gitlabInput(mergedResult.fetchImpl))).outcome, "met");
-
- const red = fakeFetch(
- gitlabRoutes({
- [GL.mr]: {
- body: { sha: HEAD, detailed_merge_status: "mergeable", head_pipeline: { id: 9, sha: HEAD, status: "failed" } },
- },
- }),
- );
- const verdict = await evaluateFactoryForge(gitlabInput(red.fetchImpl));
- assert.equal(verdict.reason, "check failed: ci_green_on_head — failed");
-
- const absent = fakeFetch(
- gitlabRoutes({ [GL.mr]: { body: { sha: HEAD, detailed_merge_status: "mergeable", head_pipeline: null } } }),
- );
- assert.equal(
- (await evaluateFactoryForge(gitlabInput(absent.fetchImpl))).reason,
- "check failed: ci_green_on_head — no pipeline",
- );
-});
-
-test("a truncated page fails ci_green_on_head and ledger_clean closed instead of trusting the visible part", async () => {
- const manyRuns = fakeFetch(
- githubRoutes({
- [GH.checkRuns]: {
- body: {
- check_runs: Array.from({ length: 100 }, (_, i) => ({
- name: `check-${i}`,
- status: "completed",
- conclusion: "success",
- })),
- },
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(githubInput(manyRuns.fetchImpl))).reason,
- "check failed: ci_green_on_head — check runs exceed one page",
- );
-
- const manyThreads = fakeFetch(
- githubRoutes({
- [GH.graphql]: {
- body: {
- data: {
- repository: {
- pullRequest: { reviewThreads: { pageInfo: { hasNextPage: true }, nodes: [{ isResolved: true }] } },
- },
- },
- },
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(githubInput(manyThreads.fetchImpl))).reason,
- "check failed: ledger_clean — review threads exceed one page",
- );
-
- const manyDiscussions = fakeFetch(
- gitlabRoutes({
- [GL.discussions]: {
- body: Array.from({ length: 100 }, () => ({ notes: [{ body: "ok", resolvable: true, resolved: true }] })),
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(gitlabInput(manyDiscussions.fetchImpl))).reason,
- "check failed: ledger_clean — discussions exceed one page",
- );
-});
-
-test("only a resolvable, unresolved GitLab note fails ledger_clean", async () => {
- const open = fakeFetch(
- gitlabRoutes({
- [GL.discussions]: {
- body: [{ notes: [{ body: "please fix", resolvable: true, resolved: false }] }],
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(gitlabInput(open.fetchImpl))).reason,
- "check failed: ledger_clean — 1 unresolved",
- );
-
- const chatter = fakeFetch(
- gitlabRoutes({
- [GL.discussions]: {
- body: [
- { notes: [{ body: "just a comment", resolvable: false, resolved: false }] },
- {
- notes: [
- {
- body: `BUGBOT_REVIEW clean on ${HEAD}`,
- resolvable: false,
- resolved: false,
- author: { name: "cursor", username: "project_1_bot_abc" },
- },
- ],
- },
- ],
- },
- }),
- );
- assert.equal((await evaluateFactoryForge(gitlabInput(chatter.fetchImpl))).outcome, "met");
-});
-
-test("GitLab mergeable and bugbot_reviewed read the merge status and the discussions already fetched", async () => {
- const checking = fakeFetch(
- gitlabRoutes({
- [GL.mr]: {
- body: { sha: HEAD, detailed_merge_status: "checking", head_pipeline: { id: 9, sha: HEAD, status: "success" } },
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(gitlabInput(checking.fetchImpl))).reason,
- "check failed: mergeable — checking",
- );
-
- const stale = fakeFetch(
- gitlabRoutes({
- [GL.discussions]: {
- body: [
- {
- notes: [
- {
- body: `BUGBOT_REVIEW clean on ${OLD}`,
- resolvable: false,
- author: { name: "cursor", username: "project_1_bot_abc" },
- },
- ],
- },
- ],
- },
- }),
- );
- const verdict = await evaluateFactoryForge(gitlabInput(stale.fetchImpl));
- assert.equal(verdict.reason, `check failed: bugbot_reviewed — no bugbot review on ${HEAD}`);
- assert.equal(keysOf(stale.calls).filter((key) => key === GL.discussions).length, 1);
-});
-
-test("a GitLab BUGBOT_REVIEW note from a human does not satisfy bugbot_reviewed", async () => {
- const human = fakeFetch(
- gitlabRoutes({
- [GL.discussions]: {
- body: [
- {
- notes: [
- {
- body: `BUGBOT_REVIEW clean on ${HEAD}`,
- resolvable: false,
- resolved: false,
- author: { name: "Paul", username: "pcap" },
- },
- ],
- },
- ],
- },
- }),
- );
- assert.equal(
- (await evaluateFactoryForge(gitlabInput(human.fetchImpl))).reason,
- `check failed: bugbot_reviewed — no bugbot review on ${HEAD}`,
- );
-});
-
-test("a GitLab branch with a slash is encoded once in its own segment", async () => {
- const fetched = fakeFetch(gitlabRoutes());
- await evaluateFactoryForge(gitlabInput(fetched.fetchImpl));
- assert.equal(keysOf(fetched.calls)[1], GL.branch);
-
- const gh = fakeFetch(githubRoutes());
- await evaluateFactoryForge(githubInput(gh.fetchImpl));
- assert.equal(keysOf(gh.calls)[1], `GET ${GH_REPO}/branches/fix/qm-12`);
-});
-
-test("a forge failure throws forge_evaluate_failed without the token or the project", async () => {
- const denied = fakeFetch(githubRoutes({ [GH.pr]: { status: 403, body: { message: "Forbidden" } } }));
- const error = await rejection(evaluateFactoryForge(githubInput(denied.fetchImpl)));
- assert.equal(error.message, "forge_evaluate_failed: 403");
- assert.equal(error.message.includes(TOKEN), false);
- assert.equal(error.message.includes("acme/app"), false);
-
- const graphql = fakeFetch(githubRoutes({ [GH.graphql]: { body: { errors: [{ message: "Bad credentials" }] } } }));
- assert.equal(
- (await rejection(evaluateFactoryForge(githubInput(graphql.fetchImpl)))).message,
- "forge_evaluate_failed: Bad credentials",
- );
-
- const gitlabDenied = fakeFetch(gitlabRoutes({ [GL.discussions]: { status: 403, body: {} } }));
- const gitlabError = await rejection(evaluateFactoryForge(gitlabInput(gitlabDenied.fetchImpl)));
- assert.equal(gitlabError.message, "forge_evaluate_failed: 403");
- assert.equal(gitlabError.message.includes(TOKEN), false);
-});
diff --git a/test/loop-factory-linear-intake.test.ts b/test/loop-factory-linear-intake.test.ts
deleted file mode 100644
index 41299e303..000000000
--- a/test/loop-factory-linear-intake.test.ts
+++ /dev/null
@@ -1,298 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import {
- FACTORY_INTAKE_STATE,
- LINEAR_GRAPHQL_URL,
- enumerateFactoryCandidates,
-} from "../src/loops/factory/linear-intake.ts";
-
-const API_KEY = "lin_api_SECRET";
-
-type Script = Response[] | ((index: number) => Response);
-type Call = { url: string; init: RequestInit | undefined };
-
-interface Fake {
- fetch: typeof globalThis.fetch;
- calls: Call[];
-}
-
-function fakeFetch(script: Script): Fake {
- const calls: Call[] = [];
- const fetch: typeof globalThis.fetch = (url, init) => {
- calls.push({ url: String(url), init });
- const response = typeof script === "function" ? script(calls.length - 1) : script[calls.length - 1];
- if (!response) return Promise.reject(new Error(`unscripted fetch #${calls.length}`));
- return Promise.resolve(response);
- };
- return { fetch, calls };
-}
-
-function callAt(fake: Fake, index: number): Call {
- const call = fake.calls[index];
- if (!call) throw new Error(`expected a fetch call at index ${index}, saw ${fake.calls.length}`);
- return call;
-}
-
-function requestBody(call: Call): { query: string; variables: Record<string, unknown> } {
- return JSON.parse(String(call.init?.body)) as { query: string; variables: Record<string, unknown> };
-}
-
-const headerOf = (call: Call, name: string): string | null => new Headers(call.init?.headers).get(name);
-
-const relation = (type: string, stateType: string): unknown => ({
- type,
- issue: { identifier: "QM-14", state: { type: stateType } },
-});
-
-const issue = (identifier: string, createdAt: string, relations: unknown[] = []): unknown => ({
- identifier,
- title: `${identifier} title`,
- createdAt,
- inverseRelations: { nodes: relations },
-});
-
-const page = (nodes: unknown[], hasNextPage = false, endCursor: string | null = null): Response =>
- Response.json({ data: { team: { issues: { nodes, pageInfo: { hasNextPage, endCursor } } } } });
-
-const keysOf = (candidates: { sourceKey: string }[]): string[] => candidates.map((candidate) => candidate.sourceKey);
-
-const intake = (fake: Fake, overrides: { stateName?: string; maxPages?: number; teamId?: string } = {}) =>
- enumerateFactoryCandidates({ teamId: "QM", apiKey: API_KEY, fetch: fake.fetch, ...overrides });
-
-async function rejection(promise: Promise<unknown>): Promise<Error> {
- try {
- await promise;
- } catch (e) {
- return e as Error;
- }
- throw new Error("expected the call to reject");
-}
-
-function assertNoKey(error: Error): void {
- assert.equal(error.message.includes(API_KEY), false);
- assert.equal((error.stack ?? "").includes(API_KEY), false);
- assert.equal(JSON.stringify((error as { cause?: unknown }).cause ?? null).includes(API_KEY), false);
-}
-
-test("returns Auto-Triage issues as candidates ordered by createdAt ascending", async () => {
- const fake = fakeFetch([
- page([issue("QM-20", "2026-02-02T00:00:00.000Z"), issue("QM-19", "2026-01-01T00:00:00.000Z")]),
- ]);
-
- const candidates = await intake(fake);
-
- assert.deepEqual(candidates, [
- { sourceKey: "QM-19", sourceSummary: "QM-19 title" },
- { sourceKey: "QM-20", sourceSummary: "QM-20 title" },
- ]);
- assert.equal(fake.calls.length, 1);
-});
-
-test("posts one Linear query carrying the verbatim key, the team id, and the requested state", async () => {
- const byDefault = fakeFetch([page([])]);
-
- assert.deepEqual(await intake(byDefault), []);
-
- const call = callAt(byDefault, 0);
- assert.equal(call.url, LINEAR_GRAPHQL_URL);
- assert.equal(call.init?.method, "POST");
- assert.equal(headerOf(call, "authorization"), API_KEY);
- assert.equal(headerOf(call, "content-type"), "application/json");
- assert.deepEqual(requestBody(call).variables, { teamId: "QM", state: FACTORY_INTAKE_STATE });
- assert.equal(FACTORY_INTAKE_STATE, "Auto-Triage");
-
- const overridden = fakeFetch([page([])]);
- await intake(overridden, { stateName: "Ready" });
- assert.equal(requestBody(callAt(overridden, 0)).variables.state, "Ready");
-});
-
-test("falls back to globalThis.fetch when no fetch is injected", async () => {
- const fake = fakeFetch([page([issue("QM-19", "2026-01-01T00:00:00.000Z")])]);
- const real = globalThis.fetch;
- globalThis.fetch = fake.fetch;
- try {
- const candidates = await enumerateFactoryCandidates({ teamId: "QM", apiKey: API_KEY });
- assert.deepEqual(keysOf(candidates), ["QM-19"]);
- } finally {
- globalThis.fetch = real;
- }
- assert.equal(fake.calls.length, 1);
-});
-
-test("drops an issue held by an unresolved blocks relation and keeps every other shape", async () => {
- const fake = fakeFetch([
- page([
- issue("QM-A", "2026-01-01T00:00:00.000Z", [relation("blocks", "started")]),
- issue("QM-B", "2026-01-02T00:00:00.000Z", [relation("blocks", "completed")]),
- issue("QM-C", "2026-01-03T00:00:00.000Z", [relation("blocks", "canceled")]),
- issue("QM-D", "2026-01-04T00:00:00.000Z", [relation("related", "started")]),
- issue("QM-E", "2026-01-05T00:00:00.000Z"),
- issue("QM-F", "2026-01-06T00:00:00.000Z", [relation("blocks", "completed"), relation("blocks", "unstarted")]),
- ]),
- ]);
-
- const candidates = await intake(fake);
-
- assert.deepEqual(keysOf(candidates), ["QM-B", "QM-C", "QM-D", "QM-E"]);
- assert.equal(fake.calls.length, 1);
-});
-
-test("treats absent relation, issue, and state fields as unblocked rather than throwing", async () => {
- const fake = fakeFetch([
- page([
- { identifier: "QM-I", title: "QM-I title", createdAt: "2026-01-01T00:00:00.000Z" },
- {
- identifier: "QM-J",
- title: "QM-J title",
- createdAt: "2026-01-02T00:00:00.000Z",
- inverseRelations: { nodes: null },
- },
- issue("QM-K", "2026-01-03T00:00:00.000Z", [{ type: "blocks", issue: null }]),
- issue("QM-L", "2026-01-04T00:00:00.000Z", [{ type: "blocks", issue: { identifier: "QM-14" } }]),
- ]),
- ]);
-
- const candidates = await intake(fake);
-
- assert.deepEqual(keysOf(candidates), ["QM-I", "QM-J", "QM-K", "QM-L"]);
-});
-
-test("follows the end cursor and sorts across page boundaries", async () => {
- const fake = fakeFetch([
- page([issue("QM-20", "2026-02-02T00:00:00.000Z")], true, "cur-1"),
- page([issue("QM-19", "2026-01-01T00:00:00.000Z")]),
- ]);
-
- const candidates = await intake(fake);
-
- assert.equal(fake.calls.length, 2);
- assert.equal("after" in requestBody(callAt(fake, 0)).variables, false);
- assert.equal(requestBody(callAt(fake, 1)).variables.after, "cur-1");
- assert.deepEqual(keysOf(candidates), ["QM-19", "QM-20"]);
-});
-
-test("stops at the request cap while hasNextPage is still true", async () => {
- const endless = (index: number): Response =>
- page([issue(`QM-${index}`, `2026-01-0${index + 1}T00:00:00.000Z`)], true, `cur-${index}`);
-
- const capped = fakeFetch(endless);
- assert.deepEqual(keysOf(await intake(capped, { maxPages: 2 })), ["QM-0", "QM-1"]);
- assert.equal(capped.calls.length, 2);
-
- const defaulted = fakeFetch(endless);
- await intake(defaulted);
- assert.equal(defaulted.calls.length, 5);
-});
-
-test("stops paging when the page cannot supply a next cursor", async () => {
- const nonAdvancing = [
- () => page([issue("QM-19", "2026-01-01T00:00:00.000Z")], true, null),
- () => page([issue("QM-19", "2026-01-01T00:00:00.000Z")], true, ""),
- () => Response.json({ data: { team: { issues: { nodes: [issue("QM-19", "2026-01-01T00:00:00.000Z")] } } } }),
- ];
-
- for (const respond of nonAdvancing) {
- const fake = fakeFetch(respond);
-
- assert.deepEqual(await intake(fake), [{ sourceKey: "QM-19", sourceSummary: "QM-19 title" }]);
- assert.equal(fake.calls.length, 1);
- assert.equal("after" in requestBody(callAt(fake, 0)).variables, false);
- }
-});
-
-test("returns candidates when a 200 carries an empty errors array", async () => {
- const fake = fakeFetch([
- Response.json({
- errors: [],
- data: {
- team: {
- issues: {
- nodes: [issue("QM-20", "2026-02-02T00:00:00.000Z"), issue("QM-19", "2026-01-01T00:00:00.000Z")],
- pageInfo: { hasNextPage: false, endCursor: null },
- },
- },
- },
- }),
- ]);
-
- assert.deepEqual(keysOf(await intake(fake)), ["QM-19", "QM-20"]);
- assert.equal(fake.calls.length, 1);
-});
-
-const FAILURES: { name: string; script: Script; message: RegExp }[] = [
- {
- name: "a Linear error at HTTP 200, such as an unknown team",
- script: [Response.json({ data: null, errors: [{ message: "Entity not found: Team" }] })],
- message: /^linear_intake_failed: Entity not found: Team$/,
- },
- {
- name: "a rejected key whose error page is not JSON",
- script: [new Response(`<html>denied ${API_KEY}</html>`, { status: 401 })],
- message: /^linear_intake_failed: 401$/,
- },
- {
- name: "a 200 whose body cannot be parsed",
- script: [new Response(`not json ${API_KEY}`, { status: 200 })],
- message: /^linear_intake_failed: /,
- },
- {
- name: "a 200 that carries no team issues",
- script: [Response.json({ data: { team: null } })],
- message: /^linear_intake_failed: /,
- },
- {
- name: "errors reported alongside a populated data payload",
- script: [
- Response.json({
- errors: [{ message: "rate limited" }],
- data: { team: { issues: { nodes: [issue("QM-19", "2026-01-01T00:00:00.000Z")], pageInfo: {} } } },
- }),
- ],
- message: /^linear_intake_failed: rate limited$/,
- },
-];
-
-for (const failure of FAILURES) {
- test(`fails closed on ${failure.name}, without leaking the key`, async () => {
- const fake = fakeFetch(failure.script);
-
- const error = await rejection(intake(fake));
-
- assert.match(error.message, failure.message);
- assertNoKey(error);
- });
-}
-
-test("throws rather than returning the first page when a later page fails", async () => {
- const fake = fakeFetch([
- page([issue("QM-19", "2026-01-01T00:00:00.000Z")], true, "cur-1"),
- new Response("boom", { status: 500 }),
- ]);
-
- const error = await rejection(intake(fake));
-
- assert.equal(error.message, "linear_intake_failed: 500");
- assert.equal(fake.calls.length, 2);
-});
-
-test("selects the team through team(id:), passing an id of either shape through verbatim", async () => {
- const fake = fakeFetch([page([])]);
-
- await intake(fake, { teamId: "6b661740-fd32-4189-837e-568df6fae1e8" });
-
- const { query, variables } = requestBody(callAt(fake, 0));
- assert.equal(variables.teamId, "6b661740-fd32-4189-837e-568df6fae1e8");
- for (const fragment of [
- "query FactoryIntake($teamId: String!, $state: String!, $after: String)",
- "team(id: $teamId)",
- "issues(first: 50, after: $after, filter: { state: { name: { eq: $state } } })",
- "pageInfo { hasNextPage endCursor }",
- ]) {
- assert.ok(query.includes(fragment), `query is missing ${fragment}`);
- }
- assert.match(
- query,
- /nodes \{\s+identifier\s+title\s+createdAt\s+inverseRelations \{ nodes \{ type issue \{ identifier state \{ type \} \} \} \}/,
- );
- assert.doesNotMatch(query, /team: \{ (id|key):/);
-});
diff --git a/test/loop-factory-preflight.test.ts b/test/loop-factory-preflight.test.ts
deleted file mode 100644
index 0407ea89d..000000000
--- a/test/loop-factory-preflight.test.ts
+++ /dev/null
@@ -1,211 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import { execFileSync } from "node:child_process";
-import {
- FACTORY_PREFLIGHT_TIMEOUT_MS,
- FACTORY_PROOF_TOOLS,
- FACTORY_REQUIRED_TOOLS,
- factoryToolProbeScript,
- parseFactoryToolProbe,
- preflightFactorySandbox,
-} from "../src/loops/factory/preflight.ts";
-import type { ExecOptions, ExecResult, Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";
-
-const handle: SandboxHandle = { id: "sbx-1", rootDir: "/workspace" };
-
-const PROCESS_METHODS = ["startProcess", "readProcess", "writeStdin", "signalProcess", "listProcesses"] as const;
-
-type ProcessMethod = (typeof PROCESS_METHODS)[number];
-type RunOutcome = ExecResult | Error;
-type RunCall = { command: string; opts: ExecOptions | undefined };
-
-interface Fake {
- sandbox: Sandbox;
- runCalls: RunCall[];
-}
-
-const exec = (stdout: string, code = 0): ExecResult => ({ stdout, stderr: "", code, timedOut: false });
-
-function fakeSandbox(opts: { outcomes?: RunOutcome[]; processSessions?: boolean; omit?: ProcessMethod }): Fake {
- const runCalls: RunCall[] = [];
- const outcomes = opts.outcomes ?? [];
- const unused = () => Promise.reject(new Error("preflight must not call this method"));
- const sandbox: Record<string, unknown> = {
- profile: {
- backend: "fake",
- writablePersistence: "resident_disk",
- processSessions: opts.processSessions ?? true,
- },
- provision: unused,
- run: (_handle: SandboxHandle, command: string, runOpts?: ExecOptions) => {
- runCalls.push({ command, opts: runOpts });
- const outcome = outcomes[runCalls.length - 1];
- if (outcome === undefined) return Promise.reject(new Error(`unscripted run #${runCalls.length}: ${command}`));
- return outcome instanceof Error ? Promise.reject(outcome) : Promise.resolve(outcome);
- },
- readFile: unused,
- writeFile: unused,
- writeFileBytes: unused,
- readFileBytes: unused,
- listDir: unused,
- removeDir: unused,
- teardown: unused,
- };
- for (const method of PROCESS_METHODS) sandbox[method] = unused;
- if (opts.omit) delete sandbox[opts.omit];
- return { sandbox: sandbox as unknown as Sandbox, runCalls };
-}
-
-function runCall(fake: Fake, index: number): RunCall {
- const call = fake.runCalls[index];
- if (!call) throw new Error(`expected a run call at index ${index}, saw ${fake.runCalls.length}`);
- return call;
-}
-
-const WRAPPER_TOOLCHAIN = ["bash", "git", "gh", "jq", "curl", "node", "npm", "claude"];
-
-const PROBE_VERSIONS: Record<string, string> = {
- bash: "GNU bash, version 5.2.15(1)-release",
- git: "",
- gh: "gh version 2.40.1 (2023-12-13)",
- jq: "jq-1.7",
- curl: "curl 8.5.0 (x86_64-pc-linux-gnu)",
- node: "v20.0.0 opt=1",
- npm: "10.2.3",
- claude: "1.0.60 (Claude Code)",
- npx: "10.2.3",
-};
-
-function versionOf(tool: string): string {
- const version = PROBE_VERSIONS[tool];
- if (version === undefined) throw new Error(`no scripted version for ${tool}`);
- return version;
-}
-
-const okStdout = (tools: readonly string[]): string =>
- `${tools.map((t) => (versionOf(t) === "" ? `${t}=ok` : `${t}=ok ${versionOf(t)}`)).join("\n")}\n`;
-const versionsFor = (tools: readonly string[]): Record<string, string> =>
- Object.fromEntries(tools.map((t) => [t, versionOf(t)]));
-
-const REQUIRED = [...FACTORY_REQUIRED_TOOLS];
-const PROOFED = [...FACTORY_REQUIRED_TOOLS, ...FACTORY_PROOF_TOOLS];
-
-const BOUND = { timeoutMs: FACTORY_PREFLIGHT_TIMEOUT_MS };
-
-test("a sandbox that cannot host process sessions is refused before any exec", async () => {
- const variants = [fakeSandbox({ processSessions: false }), ...PROCESS_METHODS.map((omit) => fakeSandbox({ omit }))];
- for (const variant of variants) {
- assert.deepEqual(await preflightFactorySandbox(variant.sandbox, handle), {
- ok: false,
- reason: "no_process_sessions",
- });
- assert.equal(variant.runCalls.length, 0);
- }
-});
-
-test("a fully equipped sandbox passes in one bounded round trip that probes the whole toolchain", async () => {
- for (const opts of [undefined, {}, { requireProof: false }]) {
- const fake = fakeSandbox({ outcomes: [exec(okStdout(REQUIRED))] });
- assert.deepEqual(await preflightFactorySandbox(fake.sandbox, handle, opts), {
- ok: true,
- versions: versionsFor(REQUIRED),
- });
- assert.equal(fake.runCalls.length, 1);
- const call = runCall(fake, 0);
- for (const tool of WRAPPER_TOOLCHAIN) assert.ok(call.command.includes(`'${tool}'`), `probe omits ${tool}`);
- assert.deepEqual(call.opts, BOUND);
- }
-});
-
-test("tools reported missing, unreported, or reported with a malformed status are all missing", async () => {
- const stdout = `${[
- "bash=ok GNU bash, version 5.2.15(1)-release",
- "claude=missing",
- "git=ok 2.43.0",
- "jq=/bin/sh: 1: jq: not found",
- "",
- "curl=ok curl 8.5.0",
- "hello=ok 1.0",
- "node=ok v20.0.0",
- "npm=ok 10.2.3",
- ].join("\n")}\n`;
- const fake = fakeSandbox({ outcomes: [exec(stdout)] });
- assert.deepEqual(await preflightFactorySandbox(fake.sandbox, handle), {
- ok: false,
- reason: "missing_tools",
- missing: ["gh", "jq", "claude"],
- versions: {
- bash: "GNU bash, version 5.2.15(1)-release",
- git: "2.43.0",
- curl: "curl 8.5.0",
- node: "v20.0.0",
- npm: "10.2.3",
- },
- });
-});
-
-test("a transport failure on the toolchain probe propagates unchanged", async () => {
- const boom = new Error("sprites exec probe: bad envelope");
- const fake = fakeSandbox({ outcomes: [boom] });
- await assert.rejects(preflightFactorySandbox(fake.sandbox, handle, { requireProof: true }), (err: unknown) => {
- assert.equal(err, boom);
- return true;
- });
- assert.equal(fake.runCalls.length, 1);
-});
-
-test("requireProof adds one bounded playwright probe that must already be runnable", async () => {
- const fake = fakeSandbox({ outcomes: [exec(okStdout(PROOFED)), exec("Version 1.47.2\n")] });
- assert.deepEqual(await preflightFactorySandbox(fake.sandbox, handle, { requireProof: true }), {
- ok: true,
- versions: { ...versionsFor(PROOFED), playwright: "Version 1.47.2" },
- });
- assert.equal(fake.runCalls.length, 2);
- assert.ok(runCall(fake, 0).command.includes("'npx'"));
- const proofCall = runCall(fake, 1);
- assert.match(proofCall.command, /(^|\s)--no(\s|$)/);
- assert.ok(proofCall.command.includes("playwright --version"));
- assert.deepEqual(proofCall.opts, BOUND);
-});
-
-test("a playwright probe that fails or prints nothing reports playwright missing", async () => {
- for (const proof of [exec("", 1), exec("\n \n", 0)]) {
- const fake = fakeSandbox({ outcomes: [exec(okStdout(PROOFED)), proof] });
- assert.deepEqual(await preflightFactorySandbox(fake.sandbox, handle, { requireProof: true }), {
- ok: false,
- reason: "missing_tools",
- missing: ["playwright"],
- versions: versionsFor(PROOFED),
- });
- }
-});
-
-test("a missing npx and a failing playwright probe are both reported", async () => {
- const stdout = `${okStdout(REQUIRED)}npx=missing\n`;
- const fake = fakeSandbox({ outcomes: [exec(stdout), exec("", 127)] });
- assert.deepEqual(await preflightFactorySandbox(fake.sandbox, handle, { requireProof: true }), {
- ok: false,
- reason: "missing_tools",
- missing: ["npx", "playwright"],
- versions: versionsFor(REQUIRED),
- });
-});
-
-test("a transport failure on the playwright probe propagates unchanged", async () => {
- const boom = new Error("sprites exec probe: connection reset");
- const fake = fakeSandbox({ outcomes: [exec(okStdout(PROOFED)), boom] });
- await assert.rejects(preflightFactorySandbox(fake.sandbox, handle, { requireProof: true }), (err: unknown) => {
- assert.equal(err, boom);
- return true;
- });
- assert.equal(fake.runCalls.length, 2);
-});
-
-test("the generated probe round-trips through the parser under a real /bin/sh", () => {
- const tools = ["node", "npm", "definitely-not-a-tool"];
- const stdout = execFileSync("/bin/sh", ["-c", factoryToolProbeScript(tools)], { encoding: "utf8" });
- const { versions, missing } = parseFactoryToolProbe(stdout, tools);
- assert.deepEqual(missing, ["definitely-not-a-tool"]);
- assert.ok((versions.node ?? "").length > 0);
- assert.ok((versions.npm ?? "").length > 0);
-});
diff --git a/test/loop-factory-process-work.test.ts b/test/loop-factory-process-work.test.ts
deleted file mode 100644
index c5d853288..000000000
--- a/test/loop-factory-process-work.test.ts
+++ /dev/null
@@ -1,632 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import {
- FACTORY_DRAIN_EMPTY_READS,
- FACTORY_READ_WAIT_MS,
- FACTORY_STDOUT_CAP_BYTES,
- FACTORY_TERM_GRACE_MS,
- FACTORY_WRAPPER,
- renderFactoryEnv,
- runFactoryProcess,
- type FactoryEnvInput,
- type FactoryProcessInput,
-} from "../src/loops/factory/process-work.ts";
-import { parseFactoryContract } from "../src/loops/factory/contract.ts";
-import { CapabilityUnsupportedError } from "../src/sandbox/sandbox.ts";
-import type {
- ProcessState,
- ReadProcessOptions,
- Sandbox,
- SandboxHandle,
- StartProcessOptions,
- TeardownOptions,
-} from "../src/sandbox/sandbox.ts";
-import type { FactoryConfig } from "../src/resolution/config-store.ts";
-import type { WorkspaceLayer } from "../src/types.ts";
-
-const HANDLE: SandboxHandle = { id: "sbx-1", rootDir: "/workspace" };
-const SCOPE_ID = "org:acme";
-const REPO_DIR = "/workspace/repo";
-const TICKET = "QM-12";
-
-type ProcessMethod = "startProcess" | "readProcess" | "writeStdin" | "signalProcess" | "listProcesses";
-
-const FULL_CONFIG: FactoryConfig = {
- forge: "github",
- publishProject: "acme/widgets",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/acme/widgets.git",
- repoSetupCmd: "npm ci",
- linearTeamId: "team_123",
- sourceAppDirs: "src app",
- sourceTestRe: "\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "npm test --",
- verifyLintCmd: "npm run lint",
- proofStartCmd: "npm run dev",
- proofBaseUrlCmd: "echo http://localhost:3000",
- slackChannel: "C123",
- bugbotRequired: true,
- followupsEnabled: false,
-};
-
-const MINIMAL_CONFIG: FactoryConfig = {
- forge: "gitlab",
- publishProject: "acme%2Fwidgets",
- targetBranch: "release/2",
- repoCloneUrl: "https://gitlab.com/acme/widgets.git",
- linearTeamId: "team_456",
- sourceAppDirs: "lib",
- sourceTestRe: "_spec\\.rb$",
- verifyTestsCmd: "bundle exec rspec",
- verifyTestFileCmd: "bundle exec rspec --",
- verifyLintCmd: "bundle exec rubocop",
- bugbotRequired: false,
- followupsEnabled: true,
-};
-
-const ENV_INPUT: FactoryEnvInput = {
- config: FULL_CONFIG,
- guidance: "reviewer asked for a smaller diff",
- linearApiKey: "lin_api_secret",
- githubToken: "ghp_secret",
- repoDir: REPO_DIR,
-};
-
-const OPTIONAL_KEYS = ["IO_FEEDBACK", "IO_REPO_SETUP_CMD", "IO_PROOF_START_CMD", "IO_PROOF_BASE_URL_CMD"];
-
-const FORBIDDEN_KEYS = ["SLACK_BOT_TOKEN", "SLACK_CHANNEL_ID", "SLACK_THREAD_TS", "CODING_AGENT_SESSION_URL"];
-
-type ReadStep =
- { chunks?: string; cursor: number; status?: ProcessState; delayMs?: number; onServe?: () => void } | { error: Error };
-
-interface Calls {
- provision: WorkspaceLayer[][];
- startProcess: Array<{ handle: SandboxHandle; command: string; opts: StartProcessOptions | undefined }>;
- readProcess: Array<{ handle: SandboxHandle; processId: string; opts: ReadProcessOptions | undefined }>;
- signalProcess: Array<{ handle: SandboxHandle; processId: string; signal: string }>;
- teardown: Array<{ handle: SandboxHandle; opts: TeardownOptions | undefined }>;
- order: string[];
- total: number;
-}
-
-interface Fake {
- sandbox: Sandbox;
- calls: Calls;
-}
-
-function fakeSandbox(opts: {
- reads?: ReadStep[];
- processSessions?: boolean;
- omit?: ProcessMethod;
- startError?: Error;
- signalError?: Error;
- teardownError?: Error;
-}): Fake {
- const calls: Calls = {
- provision: [],
- startProcess: [],
- readProcess: [],
- signalProcess: [],
- teardown: [],
- order: [],
- total: 0,
- };
- const reads = opts.reads ?? [];
- const unused = (name: string) => () => Promise.reject(new Error(`runFactoryProcess must not call ${name}`));
- const sandbox: Record<string, unknown> = {
- profile: {
- backend: "fake",
- writablePersistence: "resident_disk",
- processSessions: opts.processSessions ?? true,
- },
- provision: (layers: WorkspaceLayer[]) => {
- calls.total += 1;
- calls.provision.push(layers);
- calls.order.push("provision");
- return Promise.resolve(HANDLE);
- },
- run: unused("run"),
- readFile: unused("readFile"),
- writeFile: unused("writeFile"),
- writeFileBytes: unused("writeFileBytes"),
- readFileBytes: unused("readFileBytes"),
- listDir: unused("listDir"),
- removeDir: unused("removeDir"),
- startProcess: (handle: SandboxHandle, command: string, startOpts?: StartProcessOptions) => {
- calls.total += 1;
- calls.startProcess.push({ handle, command, opts: startOpts });
- calls.order.push("startProcess");
- if (opts.startError) return Promise.reject(opts.startError);
- return Promise.resolve({ processId: "p-7" });
- },
- readProcess: async (handle: SandboxHandle, processId: string, readOpts?: ReadProcessOptions) => {
- calls.total += 1;
- calls.readProcess.push({ handle, processId, opts: readOpts });
- calls.order.push("readProcess");
- const step = reads[calls.readProcess.length - 1];
- if (step === undefined) throw new Error(`unscripted readProcess #${calls.readProcess.length}`);
- if ("error" in step) throw step.error;
- if (step.delayMs !== undefined) await new Promise((resolve) => setTimeout(resolve, step.delayMs));
- step.onServe?.();
- return { chunks: step.chunks ?? "", cursor: step.cursor, status: step.status ?? { state: "running" } };
- },
- writeStdin: unused("writeStdin"),
- signalProcess: (handle: SandboxHandle, processId: string, signal: string) => {
- calls.total += 1;
- calls.signalProcess.push({ handle, processId, signal });
- calls.order.push("signalProcess");
- if (opts.signalError) return Promise.reject(opts.signalError);
- return Promise.resolve();
- },
- listProcesses: unused("listProcesses"),
- teardown: (handle: SandboxHandle, teardownOpts?: TeardownOptions) => {
- calls.total += 1;
- calls.teardown.push({ handle, opts: teardownOpts });
- calls.order.push("teardown");
- if (opts.teardownError) return Promise.reject(opts.teardownError);
- return Promise.resolve();
- },
- };
- if (opts.omit) delete sandbox[opts.omit];
- return { sandbox: sandbox as unknown as Sandbox, calls };
-}
-
-const baseInput = (fake: Fake, extra: Partial<FactoryProcessInput> = {}): FactoryProcessInput => ({
- sandbox: fake.sandbox,
- scopeId: SCOPE_ID,
- repoDir: REPO_DIR,
- ticketId: TICKET,
- env: { IO_LINEAR_API_KEY: "lin_api_secret" },
- ...extra,
-});
-
-const SUCCESS_READS: ReadStep[] = [
- { chunks: "line one\n", cursor: 9 },
- { chunks: "", cursor: 9 },
- { chunks: "BRANCH:qm-12-s99\nMR:41\n", cursor: 32, status: { state: "exited", code: 3 } },
- { chunks: "", cursor: 32, status: { state: "exited", code: 3 } },
- { chunks: "", cursor: 32, status: { state: "exited", code: 3 } },
-];
-
-const SUCCESS_STDOUT = "line one\nBRANCH:qm-12-s99\nMR:41\n";
-
-async function rejection(promise: Promise<unknown>): Promise<Error> {
- try {
- await promise;
- } catch (e) {
- return e as Error;
- }
- throw new Error("expected the promise to reject");
-}
-
-test("renderFactoryEnv renders exactly the wrapper's tabled keys and no Slack or session key", () => {
- const env = renderFactoryEnv(ENV_INPUT);
- assert.deepEqual(env, {
- IO_FEEDBACK: "reviewer asked for a smaller diff",
- IO_LINEAR_API_KEY: "lin_api_secret",
- IO_GITHUB_TOKEN: "ghp_secret",
- IO_PUBLISH_FORGE: "github",
- IO_PUBLISH_PROJECT: "acme/widgets",
- IO_PUBLISH_TARGET: "main",
- IO_PUBLISH_REMOTE: "origin",
- IO_SOURCE_REMOTE: "origin",
- IO_SOURCE_BASE_REF: "origin/main",
- IO_SOURCE_APP_DIRS: "src app",
- IO_SOURCE_TEST_RE: "\\.test\\.ts$",
- IO_VERIFY_TESTS_CMD: "npm test",
- IO_VERIFY_TEST_FILE_CMD: "npm test --",
- IO_VERIFY_LINT_CMD: "npm run lint",
- IO_REPO_DIR: REPO_DIR,
- IO_FACTORY_SOURCE_DIR: REPO_DIR,
- IO_REPO_CLONE_URL: "https://github.com/acme/widgets.git",
- IO_REPO_SETUP_CMD: "npm ci",
- IO_PROOF_START_CMD: "npm run dev",
- IO_PROOF_BASE_URL_CMD: "echo http://localhost:3000",
- IO_BUGBOT_REQUIRED: "true",
- IO_FOLLOWUPS_ENABLED: "false",
- IO_FOLLOWUP_TEAM_ID: "team_123",
- });
- assert.equal(FULL_CONFIG.slackChannel, "C123");
- for (const key of Object.keys(env)) assert.equal(key.startsWith("SLACK_"), false, `${key} is a SLACK_ key`);
- for (const key of FORBIDDEN_KEYS) assert.equal(Object.hasOwn(env, key), false, `${key} should never be set`);
- for (const [key, value] of Object.entries(env)) assert.notEqual(value, "C123", `${key} leaks the slack channel`);
- const frozen = Object.freeze({ ...ENV_INPUT, config: Object.freeze({ ...FULL_CONFIG }) });
- assert.deepEqual(renderFactoryEnv(frozen), env);
-});
-
-test("renderFactoryEnv omits every optional key whose source is absent", () => {
- const full = Object.keys(renderFactoryEnv(ENV_INPUT)).sort();
- const env = renderFactoryEnv({
- config: MINIMAL_CONFIG,
- linearApiKey: "lin_min",
- githubToken: "ghp_min",
- repoDir: "/srv/repo",
- });
- assert.deepEqual(
- Object.keys(env).sort(),
- full.filter((key) => !OPTIONAL_KEYS.includes(key)),
- );
- for (const key of OPTIONAL_KEYS) assert.equal(Object.hasOwn(env, key), false, `${key} should be absent`);
- assert.equal(env.IO_BUGBOT_REQUIRED, "false");
- assert.equal(env.IO_FOLLOWUPS_ENABLED, "true");
- assert.equal(env.IO_PUBLISH_TARGET, "release/2");
- assert.equal(env.IO_SOURCE_BASE_REF, "origin/release/2");
-});
-
-test("renderFactoryEnv keeps an empty optional source as an empty value rather than omitting it", () => {
- const env = renderFactoryEnv({
- ...ENV_INPUT,
- guidance: "",
- config: { ...FULL_CONFIG, repoSetupCmd: "", proofStartCmd: "", proofBaseUrlCmd: "" },
- });
- for (const key of OPTIONAL_KEYS) assert.equal(env[key], "", `${key} should render as an empty value`);
-});
-
-test("runFactoryProcess starts the wrapper, streams every chunk to exit and hands stdout to the parser", async () => {
- const fake = fakeSandbox({ reads: SUCCESS_READS });
- const chunks: string[] = [];
- const env = renderFactoryEnv(ENV_INPUT);
- const result = await runFactoryProcess(baseInput(fake, { env, onChunk: (c) => void chunks.push(c) }));
-
- assert.deepEqual(fake.calls.provision, [[{ scopeId: SCOPE_ID, mode: "rw", mountPath: "" }]]);
- assert.equal(fake.calls.startProcess.length, 1);
- assert.equal(fake.calls.startProcess[0]?.command, `bash ${FACTORY_WRAPPER} ${TICKET}`);
- assert.equal(fake.calls.startProcess[0]?.opts?.cwd, REPO_DIR);
- assert.equal(fake.calls.startProcess[0]?.opts?.env, env);
-
- assert.equal(fake.calls.readProcess.length, SUCCESS_READS.length);
- let expectedCursor = 0;
- fake.calls.readProcess.forEach((call, index) => {
- assert.equal(call.processId, "p-7");
- assert.equal(call.opts?.maxBytes, 65_536);
- assert.equal(call.opts?.waitMs, FACTORY_READ_WAIT_MS);
- assert.equal(call.opts?.sinceCursor, expectedCursor, `read #${index + 1} chained the wrong cursor`);
- const step = SUCCESS_READS[index];
- if (step && !("error" in step)) expectedCursor = step.cursor;
- });
-
- assert.deepEqual(chunks, ["line one\n", "BRANCH:qm-12-s99\nMR:41\n"]);
- assert.equal(result.processId, "p-7");
- assert.equal(result.exitCode, 3);
- assert.equal(result.stdout, SUCCESS_STDOUT);
- assert.equal(result.truncated, false);
- assert.equal(result.aborted, false);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
- assert.deepEqual(fake.calls.signalProcess, []);
-
- const artifacts = parseFactoryContract({
- stdout: result.stdout,
- sourceKey: TICKET,
- forge: "github",
- publishProject: "acme/widgets",
- });
- assert.equal(artifacts.length, 1);
- assert.equal(artifacts[0]?.shipAction, "open_pr");
- assert.equal(artifacts[0]?.label, "qm-12-s99");
- assert.equal(artifacts[0]?.externalRef, "https://github.com/acme/widgets/pull/41");
-});
-
-test("runFactoryProcess honours a readWaitMs override, needs no onChunk, and lets an onChunk throw escape", async () => {
- const bare = fakeSandbox({ reads: SUCCESS_READS });
- const result = await runFactoryProcess(baseInput(bare, { readWaitMs: 17 }));
- assert.equal(result.stdout, SUCCESS_STDOUT);
- for (const call of bare.calls.readProcess) assert.equal(call.opts?.waitMs, 17);
-
- const throwing = fakeSandbox({ reads: SUCCESS_READS });
- const boom = new Error("boom-chunk");
- const onChunk = () => {
- throw boom;
- };
- assert.equal(await rejection(runFactoryProcess(baseInput(throwing, { onChunk }))), boom);
- assert.deepEqual(throwing.calls.signalProcess, [{ handle: HANDLE, processId: "p-7", signal: "TERM" }]);
- assert.deepEqual(throwing.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
- assert.deepEqual(throwing.calls.order, ["provision", "startProcess", "readProcess", "signalProcess", "teardown"]);
-});
-
-test("runFactoryProcess keeps the contract-bearing tail of an over-cap stream and marks it truncated", async () => {
- const head = `HEAD-MARKER\n${"a".repeat(3 * 1024 * 1024)}`;
- const middle = "b".repeat(3 * 1024 * 1024);
- const tail = "\nBRANCH:qm-12-s99\nMR:41\n";
- const full = head + middle + tail;
- const fake = fakeSandbox({
- reads: [
- { chunks: head, cursor: head.length },
- { chunks: middle, cursor: head.length + middle.length },
- { chunks: tail, cursor: full.length, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: full.length, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: full.length, status: { state: "exited", code: 0 } },
- ],
- });
- let streamed = 0;
- const result = await runFactoryProcess(baseInput(fake, { onChunk: (c) => void (streamed += c.length) }));
-
- assert.equal(result.truncated, true);
- assert.equal(result.stdout.length, FACTORY_STDOUT_CAP_BYTES);
- assert.equal(result.stdout, full.slice(-FACTORY_STDOUT_CAP_BYTES));
- assert.equal(result.stdout.includes("HEAD-MARKER"), false);
- assert.equal(streamed, full.length);
-
- const artifacts = parseFactoryContract({
- stdout: result.stdout,
- sourceKey: TICKET,
- forge: "github",
- publishProject: "acme/widgets",
- });
- assert.equal(artifacts[0]?.label, "qm-12-s99");
-});
-
-for (const ticketId of ["QM-12; rm -rf /", "QM-12\nMR:99", "qm-12", "QM-", "-12", "", "QM-1a"]) {
- test(`runFactoryProcess rejects the ticket id ${JSON.stringify(ticketId)} before touching the sandbox`, async () => {
- const fake = fakeSandbox({ processSessions: false });
- const error = await rejection(runFactoryProcess(baseInput(fake, { ticketId })));
- assert.equal(error.message, "factory_ticket_invalid");
- assert.equal(fake.calls.total, 0);
- });
-}
-
-test("runFactoryProcess rejects a sandbox without process sessions before touching it", async () => {
- for (const opts of [{ processSessions: false }, { omit: "signalProcess" as ProcessMethod }]) {
- const fake = fakeSandbox(opts);
- const error = await rejection(runFactoryProcess(baseInput(fake)));
- assert.ok(error instanceof CapabilityUnsupportedError);
- assert.equal(error.capability, "process sessions");
- assert.equal(error.backend, "fake");
- assert.equal(fake.calls.total, 0);
- }
-});
-
-test("a sandbox failure propagates after teardown, with no partial result", async () => {
- const startBoom = new Error("boom-start");
- const started = fakeSandbox({ startError: startBoom });
- assert.equal(await rejection(runFactoryProcess(baseInput(started))), startBoom);
- assert.deepEqual(started.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
- assert.deepEqual(started.calls.readProcess, []);
- assert.deepEqual(started.calls.signalProcess, []);
-
- const readBoom = new Error("boom-read");
- const read = fakeSandbox({ reads: [{ chunks: "line one\n", cursor: 9 }, { error: readBoom }] });
- assert.equal(await rejection(runFactoryProcess(baseInput(read))), readBoom);
- assert.deepEqual(read.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("a read that throws terminates the wrapper once, before the unchanged teardown", async () => {
- const readBoom = new Error("boom-read");
- const fake = fakeSandbox({ reads: [{ chunks: "line one\n", cursor: 9 }, { error: readBoom }] });
- assert.equal(await rejection(runFactoryProcess(baseInput(fake))), readBoom);
- assert.deepEqual(fake.calls.signalProcess, [{ handle: HANDLE, processId: "p-7", signal: "TERM" }]);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
- assert.deepEqual(fake.calls.order, [
- "provision",
- "startProcess",
- "readProcess",
- "readProcess",
- "signalProcess",
- "teardown",
- ]);
-});
-
-test("a cleanup signal that itself rejects never masks the error that triggered it", async () => {
- const readBoom = new Error("boom-read");
- const fake = fakeSandbox({ reads: [{ error: readBoom }], signalError: new Error("boom-signal") });
- assert.equal(await rejection(runFactoryProcess(baseInput(fake))), readBoom);
- assert.deepEqual(fake.calls.signalProcess, [{ handle: HANDLE, processId: "p-7", signal: "TERM" }]);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("an abort TERM that rejects propagates and is still followed by the cleanup TERM", async () => {
- const controller = new AbortController();
- const signalBoom = new Error("boom-signal");
- const fake = fakeSandbox({
- reads: [{ chunks: "", cursor: 0, onServe: () => controller.abort() }],
- signalError: signalBoom,
- });
- assert.equal(await rejection(runFactoryProcess(baseInput(fake, { signal: controller.signal }))), signalBoom);
- assert.deepEqual(
- fake.calls.signalProcess.map((c) => c.signal),
- ["TERM", "TERM"],
- );
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("a failing teardown is swallowed on both the success and the failure path", async () => {
- const teardownError = new Error("boom-teardown");
- const ok = fakeSandbox({ reads: SUCCESS_READS, teardownError });
- const result = await runFactoryProcess(baseInput(ok));
- assert.equal(result.stdout, SUCCESS_STDOUT);
- assert.equal(ok.calls.teardown.length, 1);
-
- const boom = new Error("boom-read");
- const bad = fakeSandbox({ reads: [{ error: boom }], teardownError });
- assert.equal(await rejection(runFactoryProcess(baseInput(bad))), boom);
- assert.equal(bad.calls.teardown.length, 1);
-});
-
-test("an aborting signal sends one TERM, one KILL after the grace, and still drains the late output", async () => {
- const controller = new AbortController();
- const fake = fakeSandbox({
- reads: [
- { chunks: "line one\n", cursor: 9, onServe: () => controller.abort() },
- { chunks: "", cursor: 9 },
- { chunks: "BRANCH:qm-12-s99\nMR:41\n", cursor: 32, delayMs: 40 },
- { chunks: "", cursor: 32 },
- { chunks: "", cursor: 32, status: { state: "exited", code: 143 } },
- { chunks: "", cursor: 32, status: { state: "exited", code: 143 } },
- ],
- });
- const chunks: string[] = [];
- const result = await runFactoryProcess(
- baseInput(fake, { signal: controller.signal, termGraceMs: 5, onChunk: (c) => void chunks.push(c) }),
- );
-
- assert.deepEqual(
- fake.calls.signalProcess.map((c) => c.signal),
- ["TERM", "KILL"],
- );
- for (const call of fake.calls.signalProcess) assert.equal(call.processId, "p-7");
- assert.deepEqual(chunks, ["line one\n", "BRANCH:qm-12-s99\nMR:41\n"]);
- assert.equal(result.stdout, SUCCESS_STDOUT);
- assert.equal(result.aborted, true);
- assert.equal(result.exitCode, 143);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("a process that exits inside the grace window is terminated but never killed", async () => {
- const controller = new AbortController();
- const fake = fakeSandbox({
- reads: [
- { chunks: "", cursor: 0, onServe: () => controller.abort() },
- { chunks: "", cursor: 0 },
- { chunks: "", cursor: 0, status: { state: "exited", code: 143 } },
- { chunks: "", cursor: 0, status: { state: "exited", code: 143 } },
- ],
- });
- const result = await runFactoryProcess(
- baseInput(fake, { signal: controller.signal, termGraceMs: FACTORY_TERM_GRACE_MS }),
- );
- assert.deepEqual(
- fake.calls.signalProcess.map((c) => c.signal),
- ["TERM"],
- );
- assert.equal(result.aborted, true);
- assert.equal(result.exitCode, 143);
-});
-
-test("a signal aborted before the call terminates on the first pass and the default grace withholds the kill", async () => {
- const controller = new AbortController();
- controller.abort();
- const fake = fakeSandbox({
- reads: [
- { chunks: "", cursor: 0 },
- { chunks: "", cursor: 0 },
- { chunks: "BRANCH:qm-12-s99\nMR:41\n", cursor: 23 },
- { chunks: "", cursor: 23, status: { state: "exited", code: 143 } },
- { chunks: "", cursor: 23, status: { state: "exited", code: 143 } },
- ],
- });
- const result = await runFactoryProcess(baseInput(fake, { signal: controller.signal }));
- assert.deepEqual(
- fake.calls.signalProcess.map((c) => c.signal),
- ["TERM"],
- );
- assert.equal(result.aborted, true);
- assert.equal(result.exitCode, 143);
- assert.equal(result.stdout, "BRANCH:qm-12-s99\nMR:41\n");
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("no path logs to the console or puts a credential in an error", async () => {
- const env = renderFactoryEnv({ ...ENV_INPUT, linearApiKey: "LEAK-LINEAR", githubToken: "LEAK-GITHUB" });
- const failures: Array<() => Promise<unknown>> = [
- () => runFactoryProcess(baseInput(fakeSandbox({}), { env, ticketId: "QM-12; rm -rf /" })),
- () => runFactoryProcess(baseInput(fakeSandbox({ processSessions: false }), { env })),
- () => runFactoryProcess(baseInput(fakeSandbox({ startError: new Error("boom-start") }), { env })),
- () => runFactoryProcess(baseInput(fakeSandbox({ reads: [{ error: new Error("boom-read") }] }), { env })),
- () =>
- runFactoryProcess(
- baseInput(fakeSandbox({ reads: [{ error: new Error("boom-read") }], signalError: new Error("boom-signal") }), {
- env,
- }),
- ),
- ];
- const logged: unknown[][] = [];
- const original = { log: console.log, error: console.error, warn: console.warn, info: console.info };
- Object.assign(console, {
- log: (...args: unknown[]) => void logged.push(args),
- error: (...args: unknown[]) => void logged.push(args),
- warn: (...args: unknown[]) => void logged.push(args),
- info: (...args: unknown[]) => void logged.push(args),
- });
- try {
- await runFactoryProcess(baseInput(fakeSandbox({ reads: SUCCESS_READS }), { env }));
- for (const run of failures) {
- const error = await rejection(run());
- const rendered = [error.message, error.stack ?? "", JSON.stringify(Object.entries(error))].join("\n");
- for (const sentinel of ["LEAK-LINEAR", "LEAK-GITHUB"]) {
- assert.equal(rendered.includes(sentinel), false, `${error.message} leaked ${sentinel}`);
- }
- }
- } finally {
- Object.assign(console, original);
- }
- assert.deepEqual(logged, []);
-});
-
-test("output flushed between the size sample and the exit test still reaches stdout", async () => {
- const drained: ReadStep = { chunks: "", cursor: 32, status: { state: "exited", code: 3 } };
- const script: ReadStep[] = [
- { chunks: "line one\n", cursor: 9 },
- { chunks: "", cursor: 9, status: { state: "exited", code: 3 } },
- { chunks: "BRANCH:qm-12-s99\nMR:41\n", cursor: 32, status: { state: "exited", code: 3 } },
- ...Array.from({ length: FACTORY_DRAIN_EMPTY_READS }, () => drained),
- ];
- const fake = fakeSandbox({ reads: script });
- const chunks: string[] = [];
- const result = await runFactoryProcess(baseInput(fake, { onChunk: (c) => void chunks.push(c) }));
-
- assert.equal(result.stdout, SUCCESS_STDOUT);
- assert.deepEqual(chunks, ["line one\n", "BRANCH:qm-12-s99\nMR:41\n"]);
- assert.equal(fake.calls.readProcess.length, script.length);
- assert.equal(result.exitCode, 3);
- assert.equal(result.aborted, false);
- assert.deepEqual(fake.calls.signalProcess, []);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("an empty read before the exit never counts toward the drain", async () => {
- const script: ReadStep[] = [
- { chunks: "", cursor: 0 },
- { chunks: "", cursor: 0 },
- { chunks: "BRANCH:qm-12-s99\nMR:41\n", cursor: 23, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: 23, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: 23, status: { state: "exited", code: 0 } },
- ];
- const fake = fakeSandbox({ reads: script });
- const result = await runFactoryProcess(baseInput(fake));
- assert.equal(fake.calls.readProcess.length, script.length);
- assert.equal(result.stdout, "BRANCH:qm-12-s99\nMR:41\n");
-});
-
-test("a read that returns bytes resets the drain counter", async () => {
- const script: ReadStep[] = [
- { chunks: "", cursor: 0, status: { state: "exited", code: 0 } },
- { chunks: "x", cursor: 1, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: 1, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: 1, status: { state: "exited", code: 0 } },
- ];
- const fake = fakeSandbox({ reads: script });
- const result = await runFactoryProcess(baseInput(fake));
- assert.equal(fake.calls.readProcess.length, script.length);
- assert.equal(result.stdout, "x");
-});
-
-test("an abort that lands during the drain terminates once and never escalates to a kill", async () => {
- const controller = new AbortController();
- const script: ReadStep[] = [
- { chunks: "x", cursor: 1, status: { state: "exited", code: 0 } },
- { chunks: "", cursor: 1, status: { state: "exited", code: 0 }, onServe: () => controller.abort() },
- { chunks: "", cursor: 1, status: { state: "exited", code: 0 } },
- ];
- const fake = fakeSandbox({ reads: script });
- const result = await runFactoryProcess(baseInput(fake, { signal: controller.signal, termGraceMs: 0 }));
- assert.equal(fake.calls.readProcess.length, script.length);
- assert.deepEqual(fake.calls.signalProcess, [{ handle: HANDLE, processId: "p-7", signal: "TERM" }]);
- assert.equal(result.stdout, "x");
- assert.equal(result.exitCode, 0);
- assert.equal(result.aborted, true);
- assert.deepEqual(fake.calls.teardown, [{ handle: HANDLE, opts: { keepWarm: true } }]);
-});
-
-test("a latched exit keeps counting empty reads even when a later read reports running", async () => {
- const script: ReadStep[] = [
- { chunks: "", cursor: 0, status: { state: "exited", code: 5 } },
- { chunks: "", cursor: 0 },
- ];
- const fake = fakeSandbox({ reads: script });
- const result = await runFactoryProcess(baseInput(fake));
- assert.equal(FACTORY_DRAIN_EMPTY_READS, 2);
- assert.equal(fake.calls.readProcess.length, FACTORY_DRAIN_EMPTY_READS);
- assert.equal(result.exitCode, 5);
- assert.equal(result.stdout, "");
- assert.deepEqual(fake.calls.signalProcess, []);
-});
diff --git a/test/loop-factory-ship.test.ts b/test/loop-factory-ship.test.ts
deleted file mode 100644
index 3718565c2..000000000
--- a/test/loop-factory-ship.test.ts
+++ /dev/null
@@ -1,599 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import {
- forgeRequest,
- returnFactoryPullRequest,
- shipFactoryAlreadyFixed,
- shipFactoryPullRequest,
- type ForgeRef,
- type ShipDeps,
-} from "../src/loops/factory/ship.ts";
-
-const FORGE_TOKEN = "ght_FAKE";
-const LINEAR_KEY = "lin_api_FAKE";
-const TICKET = "QM-21";
-const LINEAR_URL = "https://api.linear.app/graphql";
-const GH_PULL = "https://api.github.com/repos/yc-software/qm-yc/pulls/7";
-const GH_ISSUE_COMMENTS = "https://api.github.com/repos/yc-software/qm-yc/issues/7/comments";
-const GL_BASE = "https://gitlab.com/api/v4/projects/yc-software%2Fcode";
-const GL_MR = `${GL_BASE}/merge_requests/42`;
-
-const github: ForgeRef = { forge: "github", publishProject: "yc-software/qm-yc", number: 7 };
-const gitlab: ForgeRef = { forge: "gitlab", publishProject: "yc-software/code", number: 42 };
-
-interface Recorded {
- method: string;
- url: string;
- headers: Record<string, string>;
- body?: string;
-}
-
-interface Route {
- status?: number;
- body?: unknown;
-}
-
-const parseBody = (rec: Recorded): Record<string, unknown> => JSON.parse(rec.body ?? "{}") as Record<string, unknown>;
-
-const queryOf = (rec: Recorded): string => String(parseBody(rec).query ?? "");
-
-const LINEAR_OPS = ["issueUpdate", "issueAddLabel", "issueLabels", "commentCreate", "issue("] as const;
-
-const routeKey = (rec: Recorded): string => {
- if (rec.url === LINEAR_URL) {
- const query = queryOf(rec);
- const op = LINEAR_OPS.find((candidate) => query.includes(candidate));
- return `linear:${op === "issue(" ? "issue" : (op ?? "unknown")}`;
- }
- if (rec.url === "https://api.github.com/graphql") return "github:graphql";
- return `${rec.method} ${rec.url}`;
-};
-
-function fakeFetch(routes: Record<string, Route>): { fetchImpl: typeof fetch; calls: Recorded[] } {
- const calls: Recorded[] = [];
- const fetchImpl = (async (url: string | URL | Request, init?: RequestInit) => {
- const headers: Record<string, string> = {};
- for (const [key, value] of Object.entries((init?.headers ?? {}) as Record<string, string>)) {
- headers[key.toLowerCase()] = value;
- }
- const rec: Recorded = {
- url: String(url),
- method: init?.method ?? "GET",
- headers,
- ...(init?.body ? { body: String(init.body) } : {}),
- };
- calls.push(rec);
- const route = routes[routeKey(rec)];
- if (route === undefined) throw new Error(`unscripted route: ${routeKey(rec)}`);
- return new Response(JSON.stringify(route.body ?? {}), {
- status: route.status ?? 200,
- headers: { "content-type": "application/json" },
- });
- }) as typeof fetch;
- return { fetchImpl, calls };
-}
-
-const depsFor = (fetchImpl: typeof fetch): ShipDeps => ({
- fetch: fetchImpl,
- forgeToken: FORGE_TOKEN,
- linearApiKey: LINEAR_KEY,
-});
-
-const TEAM_STATES = [
- { id: "st_triage", name: "Auto-Triage", type: "unstarted" },
- { id: "st_review", name: "In Review", type: "started" },
- { id: "st_done", name: "Done", type: "completed" },
-];
-
-const AUTO_TRIAGE = { id: "st_triage", name: "Auto-Triage", type: "unstarted" };
-const IN_REVIEW = { id: "st_review", name: "In Review", type: "started" };
-
-const issueRead = (over: {
- state?: { id: string; name: string; type: string };
- labels?: string[];
- states?: { id: string; name: string; type: string }[];
-}): Route => ({
- body: {
- data: {
- issue: {
- id: "iss_1",
- state: over.state ?? AUTO_TRIAGE,
- labels: { nodes: (over.labels ?? []).map((name, index) => ({ id: `lbl_${index}`, name })) },
- team: { id: "team_1", states: { nodes: over.states ?? TEAM_STATES } },
- },
- },
- },
-});
-
-const ok = (payload: Record<string, unknown>): Route => ({ body: { data: payload } });
-
-const LABEL_LOOKUP = ok({ issueLabels: { nodes: [{ id: "lbl_rfr" }] } });
-const STATE_OK = ok({ issueUpdate: { success: true } });
-const LABEL_OK = ok({ issueAddLabel: { success: true } });
-const COMMENT_OK = ok({ commentCreate: { success: true } });
-const READY_MUTATION = ok({ markPullRequestReadyForReview: { pullRequest: { isDraft: false } } });
-
-const keysOf = (calls: Recorded[]): string[] => calls.map(routeKey);
-
-const only = (calls: Recorded[], key: string): Recorded => {
- const matched = calls.filter((call) => routeKey(call) === key);
- assert.equal(matched.length, 1, `expected exactly one ${key}`);
- return matched[0]!;
-};
-
-const rejectionMessage = async (promise: Promise<unknown>): Promise<string> => {
- try {
- await promise;
- } catch (error) {
- return error instanceof Error ? error.message : String(error);
- }
- return assert.fail("expected a rejection");
-};
-
-const assertSecretFree = (message: string): void => {
- assert.ok(!message.includes(FORGE_TOKEN), `message leaked the forge token: ${message}`);
- assert.ok(!message.includes(LINEAR_KEY), `message leaked the linear key: ${message}`);
-};
-
-test("shipping a GitHub draft PR undrafts it, moves the ticket to In Review, and adds the label", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: true, node_id: "PR_kwDOnode7", state: "open", merged: false } },
- "github:graphql": READY_MUTATION,
- "linear:issue": issueRead({}),
- "linear:issueUpdate": STATE_OK,
- "linear:issueLabels": LABEL_LOOKUP,
- "linear:issueAddLabel": LABEL_OK,
- });
-
- const results = await shipFactoryPullRequest(github, TICKET, depsFor(fetchImpl));
-
- assert.deepStrictEqual(results, [
- { step: "undraft", changed: true },
- { step: "linear-state", changed: true },
- { step: "linear-label", changed: true },
- ]);
- assert.deepStrictEqual(keysOf(calls), [
- `GET ${GH_PULL}`,
- "github:graphql",
- "linear:issue",
- "linear:issueUpdate",
- "linear:issueLabels",
- "linear:issueAddLabel",
- ]);
- assert.equal(calls[0]!.headers.authorization, `Bearer ${FORGE_TOKEN}`);
- assert.equal(calls[0]!.headers.accept, "application/vnd.github+json");
- const ghGraphql = only(calls, "github:graphql");
- assert.equal(ghGraphql.method, "POST");
- assert.equal(ghGraphql.headers.authorization, `Bearer ${FORGE_TOKEN}`);
- assert.equal(ghGraphql.headers.accept, "application/vnd.github+json");
- assert.equal(ghGraphql.headers["content-type"], "application/json");
- assert.match(queryOf(ghGraphql), /markPullRequestReadyForReview\(input: \{ pullRequestId: "PR_kwDOnode7" \}\)/);
- assert.match(
- queryOf(only(calls, "linear:issueUpdate")),
- /issueUpdate\(id: "iss_1", input: \{ stateId: "st_review" \}\)/,
- );
- assert.match(queryOf(only(calls, "linear:issueAddLabel")), /issueAddLabel\(id: "iss_1", labelId: "lbl_rfr"\)/);
- assert.match(queryOf(only(calls, "linear:issueLabels")), /name: \{ eq: "ready-for-review" \}/);
-
- const linearCalls = calls.filter((call) => call.url === LINEAR_URL);
- assert.equal(linearCalls.length, 4);
- for (const call of linearCalls) assert.equal(call.headers.authorization, LINEAR_KEY);
- assert.equal(linearCalls.filter((call) => /issue\(id: "QM-21"\)/.test(queryOf(call))).length, 1);
-});
-
-test("re-shipping a ready GitHub PR already In Review and labelled writes nothing", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: false, node_id: "PR_kwDOnode7", state: "open", merged: false } },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
-
- const results = await shipFactoryPullRequest(github, TICKET, depsFor(fetchImpl));
-
- assert.deepStrictEqual(results, [
- { step: "undraft", changed: false },
- { step: "linear-state", changed: false },
- { step: "linear-label", changed: false },
- ]);
- assert.deepStrictEqual(keysOf(calls), [`GET ${GH_PULL}`, "linear:issue"]);
-});
-
-for (const publishProject of ["yc-software/code", "yc-software%2Fcode"]) {
- test(`shipping a GitLab draft MR strips the prefix and single-encodes ${publishProject}`, async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GL_MR}`]: { body: { draft: true, title: "Draft: Fix parser", state: "opened" } },
- [`PUT ${GL_MR}`]: { body: { title: "Fix parser" } },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
-
- const results = await shipFactoryPullRequest({ ...gitlab, publishProject }, TICKET, depsFor(fetchImpl));
-
- assert.deepStrictEqual(results, [
- { step: "undraft", changed: true },
- { step: "linear-state", changed: false },
- { step: "linear-label", changed: false },
- ]);
- const put = only(calls, `PUT ${GL_MR}`);
- assert.equal(put.url, GL_MR);
- assert.ok(!put.url.includes("%252F"));
- assert.deepStrictEqual(parseBody(put), { title: "Fix parser" });
- assert.equal(put.headers["private-token"], FORGE_TOKEN);
- assert.ok(!("authorization" in put.headers));
- assert.equal(calls.filter((call) => call.url.startsWith("https://api.github.com/")).length, 0);
- });
-}
-
-test("a GitLab WIP prefix is stripped and a ready MR is left alone", async () => {
- const wip = fakeFetch({
- [`GET ${GL_MR}`]: { body: { draft: true, title: "WIP: Fix parser", state: "opened" } },
- [`PUT ${GL_MR}`]: { body: { title: "Fix parser" } },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
- await shipFactoryPullRequest(gitlab, TICKET, depsFor(wip.fetchImpl));
- assert.deepStrictEqual(parseBody(only(wip.calls, `PUT ${GL_MR}`)), { title: "Fix parser" });
-
- const ready = fakeFetch({
- [`GET ${GL_MR}`]: { body: { draft: false, title: "Fix parser", state: "opened" } },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
- const results = await shipFactoryPullRequest(gitlab, TICKET, depsFor(ready.fetchImpl));
- assert.deepStrictEqual(results[0], { step: "undraft", changed: false });
- assert.deepStrictEqual(keysOf(ready.calls), [`GET ${GL_MR}`, "linear:issue"]);
-});
-
-test("a lowercase GitLab draft prefix is stripped", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GL_MR}`]: { body: { draft: true, title: "draft: Fix parser", state: "opened" } },
- [`PUT ${GL_MR}`]: { body: { title: "Fix parser" } },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
- const results = await shipFactoryPullRequest(gitlab, TICKET, depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "undraft", changed: true },
- { step: "linear-state", changed: false },
- { step: "linear-label", changed: false },
- ]);
- assert.deepStrictEqual(parseBody(only(calls, `PUT ${GL_MR}`)), { title: "Fix parser" });
-});
-
-test("returning a GitHub PR comments, closes, comments in Linear, and moves to Auto-Triage", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`POST ${GH_ISSUE_COMMENTS}`]: { status: 201, body: { id: 1 } },
- [`GET ${GH_PULL}`]: { body: { draft: false, state: "open", merged: false } },
- [`PATCH ${GH_PULL}`]: { body: { state: "closed" } },
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
-
- const results = await returnFactoryPullRequest(github, TICKET, "tests are red", depsFor(fetchImpl));
-
- assert.deepStrictEqual(results, [
- { step: "forge-comment", changed: true },
- { step: "close", changed: true },
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: true },
- ]);
- assert.deepStrictEqual(keysOf(calls), [
- `POST ${GH_ISSUE_COMMENTS}`,
- `GET ${GH_PULL}`,
- `PATCH ${GH_PULL}`,
- "linear:issue",
- "linear:commentCreate",
- "linear:issueUpdate",
- ]);
- assert.deepStrictEqual(parseBody(only(calls, `POST ${GH_ISSUE_COMMENTS}`)), {
- body: "Returned by the factory reviewer: tests are red",
- });
- const patch = only(calls, `PATCH ${GH_PULL}`);
- assert.deepStrictEqual(parseBody(patch), { state: "closed" });
- assert.equal(patch.headers["content-type"], "application/json");
- assert.equal(patch.headers.authorization, `Bearer ${FORGE_TOKEN}`);
- assert.equal(patch.headers.accept, "application/vnd.github+json");
- assert.ok(!("content-type" in only(calls, `GET ${GH_PULL}`).headers));
- assert.match(queryOf(only(calls, "linear:commentCreate")), /body: "Returned to Auto-Triage: tests are red"/);
- assert.match(queryOf(only(calls, "linear:issueUpdate")), /stateId: "st_triage"/);
-});
-
-test("a merged pull request is commented on but never closed", async () => {
- const merged = fakeFetch({
- [`POST ${GH_ISSUE_COMMENTS}`]: { body: { id: 1 } },
- [`GET ${GH_PULL}`]: { body: { draft: false, state: "closed", merged: true } },
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const results = await returnFactoryPullRequest(github, TICKET, "already merged", depsFor(merged.fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "forge-comment", changed: true },
- { step: "close", changed: false, detail: "merged" },
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: true },
- ]);
- assert.equal(merged.calls.filter((call) => call.method === "PATCH").length, 0);
-
- const mergedGitlab = fakeFetch({
- [`POST ${GL_MR}/notes`]: { body: { id: 1 } },
- [`GET ${GL_MR}`]: { body: { draft: false, state: "merged" } },
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const gitlabResults = await returnFactoryPullRequest(
- gitlab,
- TICKET,
- "already merged",
- depsFor(mergedGitlab.fetchImpl),
- );
- assert.deepStrictEqual(gitlabResults[1], { step: "close", changed: false, detail: "merged" });
- assert.equal(mergedGitlab.calls.filter((call) => call.method === "PUT").length, 0);
-});
-
-test("an already-closed pull request is not closed again", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`POST ${GH_ISSUE_COMMENTS}`]: { body: { id: 1 } },
- [`GET ${GH_PULL}`]: { body: { draft: false, state: "closed", merged: false } },
- "linear:issue": issueRead({ state: AUTO_TRIAGE }),
- "linear:commentCreate": COMMENT_OK,
- });
- const results = await returnFactoryPullRequest(github, TICKET, "stale", depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "forge-comment", changed: true },
- { step: "close", changed: false },
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: false },
- ]);
- assert.equal(calls.filter((call) => call.method === "PATCH").length, 0);
- assert.equal(calls.filter((call) => routeKey(call) === "linear:issueUpdate").length, 0);
-});
-
-test("returning without a ref touches no forge at all", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const results = await returnFactoryPullRequest(null, TICKET, "no output", depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: true },
- ]);
- for (const call of calls) assert.equal(new URL(call.url).hostname, "api.linear.app");
-});
-
-test("returning a GitLab merge request notes it and closes with state_event", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`POST ${GL_MR}/notes`]: { body: { id: 1 } },
- [`GET ${GL_MR}`]: { body: { draft: false, state: "opened" } },
- [`PUT ${GL_MR}`]: { body: { state: "closed" } },
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- await returnFactoryPullRequest(gitlab, TICKET, "tests are red", depsFor(fetchImpl));
- assert.deepStrictEqual(parseBody(only(calls, `POST ${GL_MR}/notes`)), {
- body: "Returned by the factory reviewer: tests are red",
- });
- assert.deepStrictEqual(parseBody(only(calls, `PUT ${GL_MR}`)), { state_event: "close" });
-});
-
-test("a reviewer note carrying a quote and a newline round-trips through the GraphQL literal", async () => {
- const note = 'he said "no"\nand walked out';
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- await returnFactoryPullRequest(null, TICKET, note, depsFor(fetchImpl));
- const query = queryOf(only(calls, "linear:commentCreate"));
- const literal = /body: ("(?:[^"\\]|\\.)*")/.exec(query)?.[1];
- assert.ok(literal !== undefined);
- assert.equal(JSON.parse(literal) as string, `Returned to Auto-Triage: ${note}`);
-});
-
-test("already-fixed comments the evidence and moves the ticket to Done", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const results = await shipFactoryAlreadyFixed(TICKET, "fixed in abc1234 on main", depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: true },
- ]);
- assert.match(queryOf(only(calls, "linear:commentCreate")), /body: "Already fixed\. fixed in abc1234 on main"/);
- assert.match(queryOf(only(calls, "linear:issueUpdate")), /stateId: "st_done"/);
- for (const call of calls) assert.equal(new URL(call.url).hostname, "api.linear.app");
-});
-
-test("already-fixed without evidence posts the bare sentence", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- await shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fetchImpl));
- assert.match(queryOf(only(calls, "linear:commentCreate")), /body: "Already fixed\."/);
- assert.ok(!queryOf(only(calls, "linear:commentCreate")).includes("undefined"));
-});
-
-for (const state of [
- { id: "st_cancel", name: "Won't Do", type: "canceled" },
- { id: "st_shipped", name: "Shipped", type: "completed" },
-]) {
- test(`already-fixed leaves a ${state.type} ticket in ${state.name}`, async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state }),
- "linear:commentCreate": COMMENT_OK,
- });
- const results = await shipFactoryAlreadyFixed(TICKET, "seen already", depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: false },
- ]);
- assert.equal(calls.filter((call) => routeKey(call) === "linear:issueUpdate").length, 0);
- });
-}
-
-test("already-fixed still moves a started state whose name merely looks terminal", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: { id: "st_dr", name: "Done Reviewing", type: "started" } }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const results = await shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fetchImpl));
- assert.deepStrictEqual(results[1], { step: "linear-state", changed: true });
- assert.match(queryOf(only(calls, "linear:issueUpdate")), /stateId: "st_done"/);
-});
-
-test("a team missing the target workflow state fails loudly and stops the sequence", async () => {
- const ship = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: false, state: "open", merged: false } },
- "linear:issue": issueRead({ states: [AUTO_TRIAGE] }),
- });
- assert.match(
- await rejectionMessage(shipFactoryPullRequest(github, TICKET, depsFor(ship.fetchImpl))),
- /^linear_state_missing: In Review$/,
- );
- assert.equal(ship.calls.filter((call) => routeKey(call) === "linear:issueLabels").length, 0);
-
- const back = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW, states: [IN_REVIEW] }),
- "linear:commentCreate": COMMENT_OK,
- });
- assert.match(
- await rejectionMessage(returnFactoryPullRequest(null, TICKET, "note", depsFor(back.fetchImpl))),
- /^linear_state_missing: Auto-Triage$/,
- );
-
- const fixed = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW, states: [AUTO_TRIAGE, IN_REVIEW] }),
- "linear:commentCreate": COMMENT_OK,
- });
- assert.match(
- await rejectionMessage(shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fixed.fetchImpl))),
- /^linear_state_missing: Done$/,
- );
-});
-
-test("an unknown ready-for-review label fails loudly and adds nothing", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: false, state: "open", merged: false } },
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:issueLabels": ok({ issueLabels: { nodes: [] } }),
- });
- assert.match(
- await rejectionMessage(shipFactoryPullRequest(github, TICKET, depsFor(fetchImpl))),
- /^linear_label_missing: ready-for-review$/,
- );
- assert.equal(calls.filter((call) => routeKey(call) === "linear:issueAddLabel").length, 0);
-});
-
-test("a forge 403 throws with the status, leaks no secret, and reaches no Linear call", async () => {
- const { fetchImpl, calls } = fakeFetch({
- [`GET ${GH_PULL}`]: { status: 403, body: { message: "Forbidden" } },
- });
- const message = await rejectionMessage(shipFactoryPullRequest(github, TICKET, depsFor(fetchImpl)));
- assert.equal(message, "forge_undraft_failed: 403");
- assertSecretFree(message);
- assert.equal(calls.filter((call) => call.url === LINEAR_URL).length, 0);
-});
-
-test("a Linear 500 throws with the status and leaks no secret", async () => {
- const { fetchImpl } = fakeFetch({ "linear:issue": { status: 500, body: {} } });
- const message = await rejectionMessage(shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fetchImpl)));
- assert.equal(message, "linear_read_failed: 500");
- assertSecretFree(message);
-});
-
-test("a GraphQL errors array on a 200 throws on either endpoint", async () => {
- const linear = fakeFetch({ "linear:issue": { body: { errors: [{ message: "boom" }] } } });
- assert.equal(
- await rejectionMessage(shipFactoryAlreadyFixed(TICKET, undefined, depsFor(linear.fetchImpl))),
- "linear_read_failed: boom",
- );
-
- const forge = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: true, node_id: "PR_kwDOnode7", state: "open", merged: false } },
- "github:graphql": { body: { errors: [{ message: "not a draft" }] } },
- });
- assert.equal(
- await rejectionMessage(shipFactoryPullRequest(github, TICKET, depsFor(forge.fetchImpl))),
- "forge_undraft_failed: not a draft",
- );
-});
-
-test("a forge path is joined to the forge base, so the forge token can never reach another host", async () => {
- const selfHosted = "https://gitlab.example.com/api/v4/projects/9/merge_requests/1";
- const { fetchImpl, calls } = fakeFetch({ [`GET ${GL_BASE}${selfHosted}`]: { body: {} } });
- await forgeRequest(gitlab, depsFor(fetchImpl), "read", "GET", selfHosted);
- assert.equal(calls.length, 1);
- assert.equal(new URL(calls[0]!.url).origin, "https://gitlab.com");
- assert.equal(calls[0]!.headers["private-token"], FORGE_TOKEN);
-});
-
-test("the GitHub GraphQL undraft accepts any 2xx and reports a non-2xx status", async () => {
- const accepted = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: true, node_id: "PR_kwDOnode7", state: "open", merged: false } },
- "github:graphql": { ...READY_MUTATION, status: 202 },
- "linear:issue": issueRead({ state: IN_REVIEW, labels: ["ready-for-review"] }),
- });
- const results = await shipFactoryPullRequest(github, TICKET, depsFor(accepted.fetchImpl));
- assert.deepStrictEqual(results[0], { step: "undraft", changed: true });
-
- const rejected = fakeFetch({
- [`GET ${GH_PULL}`]: { body: { draft: true, node_id: "PR_kwDOnode7", state: "open", merged: false } },
- "github:graphql": { status: 502, body: {} },
- });
- const message = await rejectionMessage(shipFactoryPullRequest(github, TICKET, depsFor(rejected.fetchImpl)));
- assert.equal(message, "forge_undraft_failed: 502");
- assertSecretFree(message);
-});
-
-test("an empty errors array is not a failure", async () => {
- const { fetchImpl } = fakeFetch({
- "linear:issue": { body: { errors: [], ...(issueRead({ state: IN_REVIEW }).body as Record<string, unknown>) } },
- "linear:commentCreate": { body: { errors: [], data: { commentCreate: { success: true } } } },
- "linear:issueUpdate": { body: { errors: [], data: { issueUpdate: { success: true } } } },
- });
- const results = await shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fetchImpl));
- assert.deepStrictEqual(results, [
- { step: "linear-comment", changed: true },
- { step: "linear-state", changed: true },
- ]);
-});
-
-test("a Linear personal API key is sent verbatim in Authorization, never as a Bearer token", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- await shipFactoryAlreadyFixed(TICKET, undefined, depsFor(fetchImpl));
- assert.equal(calls.length, 3);
- for (const call of calls) {
- assert.equal(call.url, LINEAR_URL);
- assert.equal(call.headers.authorization, LINEAR_KEY);
- assert.equal(call.headers["content-type"], "application/json");
- }
-});
-
-test("deps without a fetch fall back to globalThis.fetch at call time", async () => {
- const { fetchImpl, calls } = fakeFetch({
- "linear:issue": issueRead({ state: IN_REVIEW }),
- "linear:commentCreate": COMMENT_OK,
- "linear:issueUpdate": STATE_OK,
- });
- const original = globalThis.fetch;
- globalThis.fetch = fetchImpl;
- try {
- const results = await shipFactoryAlreadyFixed(TICKET, "no injected fetch", {
- forgeToken: FORGE_TOKEN,
- linearApiKey: LINEAR_KEY,
- });
- assert.equal(results.length, 2);
- assert.deepStrictEqual(keysOf(calls), ["linear:issue", "linear:commentCreate", "linear:issueUpdate"]);
- } finally {
- globalThis.fetch = original;
- }
-});
diff --git a/test/loop-factory-wiring.test.ts b/test/loop-factory-wiring.test.ts
deleted file mode 100644
index a4a788909..000000000
--- a/test/loop-factory-wiring.test.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import "./support/auto-fake-sprites.ts";
-
-import assert from "node:assert/strict";
-import { test } from "node:test";
-import { buildApp } from "../src/wiring.ts";
-import { FACTORY_LOOP_SURFACE } from "../src/loops/factory/effects.ts";
-import { scopeId } from "../src/types.ts";
-import { testConfig } from "./support/test-config.ts";
-
-test("a booted instance drives a factory loop through the factory dependencies it wired", async () => {
- const built = buildApp(testConfig());
- const { loop } = await built.loops.store.create({
- owner: "josh",
- createdBy: "josh",
- ownerScopeId: scopeId("personal", "josh"),
- name: "factory",
- surface: FACTORY_LOOP_SURFACE,
- playbook: "the factory wrapper does the work",
- successCondition: "the pull request converged",
- shipActions: [{ action: "open_pr", gate: "hold" }],
- });
-
- const unconfigured = await built.loops.fire!.fire(loop.id, "f1");
- assert.equal(unconfigured.status, "failed");
- assert.match(unconfigured.note ?? "", /factory_config_missing/);
-
- built.loops.config.setFactoryConfig({
- forge: "github",
- publishProject: "acme/app",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/acme/app.git",
- linearTeamId: "TEAM-1",
- sourceAppDirs: "src",
- sourceTestRe: "\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "npm test --",
- verifyLintCmd: "npm run lint",
- bugbotRequired: true,
- followupsEnabled: false,
- });
-
- const uncredentialed = await built.loops.fire!.fire(loop.id, "f2");
- assert.equal(uncredentialed.status, "failed");
- assert.match(uncredentialed.note ?? "", /factory_credentials_missing: factory-linear, factory-github/);
-});
diff --git a/test/loop-fire.test.ts b/test/loop-fire.test.ts
index ae5878b2b..8526d679a 100644
--- a/test/loop-fire.test.ts
+++ b/test/loop-fire.test.ts
@@ -7,12 +7,6 @@ import { createLoopOutputStore } from "../src/loops/output-store.ts";
import { createShipGrantStore } from "../src/loops/ship-grant-store.ts";
import { buildShipGrant } from "../src/loops/ship-gate.ts";
import { createIdempotencyStore } from "../src/idempotency/idempotency-store.ts";
-import { FACTORY_LOOP_SURFACE, type FactoryEffectsDeps } from "../src/loops/factory/effects.ts";
-import { FACTORY_REQUIRED_TOOLS } from "../src/loops/factory/preflight.ts";
-import { FACTORY_GITHUB_SLUG, FACTORY_LINEAR_SLUG } from "../src/loops/factory/credentials.ts";
-import type { FactoryConfig } from "../src/resolution/config-store.ts";
-import type { ServiceCredentialReader } from "../src/credentials/keychain.ts";
-import type { ReadProcessResult, Sandbox } from "../src/sandbox/sandbox.ts";
import { scopeId, type TurnRequest, type TurnResult } from "../src/types.ts";
function fakeIdentity() {
@@ -37,13 +31,7 @@ function fakeDeliveries() {
type Responder = (req: TurnRequest) => string;
-function service(
- respond: Responder,
- overrides?: {
- grants?: ReturnType<typeof createShipGrantStore>;
- factory?: (loops: ReturnType<typeof createLoopStore>) => FactoryEffectsDeps;
- },
-) {
+function service(respond: Responder, overrides?: { grants?: ReturnType<typeof createShipGrantStore> }) {
const loops = createLoopStore();
const items = createLoopItemLedger();
const outputs = createLoopOutputStore();
@@ -65,7 +53,6 @@ function service(
return { status: "ok", reply: respond(req), sessionId: `s${turns.length}` };
},
},
- ...(overrides?.factory ? { factory: overrides.factory(loops) } : {}),
});
return { loops, items, outputs, grants, fire, turns, deliveries, idempotency };
}
@@ -554,679 +541,3 @@ test("a paused loop refuses to fire", async () => {
assert.equal(result.status, "silent");
assert.equal(s.turns.length, 0);
});
-
-const FACTORY_TICKET = "QM-12";
-const FACTORY_BRANCH = "fix/qm-12";
-const FACTORY_PR_URL = "https://github.com/acme/app/pull/42";
-const LINEAR_URL = "https://api.linear.app/graphql";
-const GH_GRAPHQL = "https://api.github.com/graphql";
-const GH_REPO = "https://api.github.com/repos/acme/app";
-const HEAD_SHA = "1".repeat(40);
-const LINEAR_KEY = "lin_FAKE_KEY";
-const GITHUB_TOKEN = "ghp_FAKE_TOKEN";
-const WRAPPER_STDOUT = `working\nBRANCH:${FACTORY_BRANCH}\nMR:42\n`;
-const ALREADY_FIXED_STDOUT = "ALREADY_FIXED:true\nALREADY_FIXED_EVIDENCE:fixed by #40\n";
-
-const FACTORY_CONFIG: FactoryConfig = {
- forge: "github",
- publishProject: "acme/app",
- targetBranch: "main",
- repoCloneUrl: "https://github.com/acme/app.git",
- linearTeamId: "TEAM-1",
- sourceAppDirs: "src",
- sourceTestRe: "\\.test\\.ts$",
- verifyTestsCmd: "npm test",
- verifyTestFileCmd: "npm test --",
- verifyLintCmd: "npm run lint",
- bugbotRequired: true,
- followupsEnabled: false,
-};
-
-interface FactorySandbox {
- sandbox: Sandbox;
- ops: string[];
-}
-
-function factorySandbox(
- opts: { stdout?: string; read?: (ops: string[]) => Promise<ReadProcessResult> } = {},
-): FactorySandbox {
- const ops: string[] = [];
- const stdout = opts.stdout ?? WRAPPER_STDOUT;
- const probe = FACTORY_REQUIRED_TOOLS.map((tool) => `${tool}=ok 1.0`).join("\n");
- const unused = (name: string) => () => Promise.reject(new Error(`a factory loop must not call ${name}`));
- let provisioned = 0;
- const defaultRead = async (): Promise<ReadProcessResult> => {
- const sinceStart = ops.slice(ops.lastIndexOf("startProcess"));
- return sinceStart.filter((op) => op === "readProcess").length === 1
- ? { chunks: stdout, cursor: stdout.length, status: { state: "running" } }
- : { chunks: "", cursor: stdout.length, status: { state: "exited", code: 0 } };
- };
- const record = <T>(op: string, value: T): Promise<T> => {
- ops.push(op);
- return Promise.resolve(value);
- };
- const sandbox: Record<string, unknown> = {
- profile: { backend: "fake", writablePersistence: "resident_disk", processSessions: true },
- provision: () => {
- provisioned += 1;
- return record("provision", { id: `sbx-${provisioned}`, rootDir: "/workspace" });
- },
- run: () => record("run", { stdout: `${probe}\n`, stderr: "", code: 0, timedOut: false }),
- startProcess: () => record("startProcess", { processId: "p1" }),
- readProcess: () => {
- ops.push("readProcess");
- return (opts.read ?? defaultRead)(ops);
- },
- signalProcess: () => record("signalProcess", undefined),
- teardown: () => record("teardown", undefined),
- readFile: unused("readFile"),
- writeFile: unused("writeFile"),
- writeFileBytes: unused("writeFileBytes"),
- readFileBytes: unused("readFileBytes"),
- listDir: unused("listDir"),
- removeDir: unused("removeDir"),
- writeStdin: unused("writeStdin"),
- listProcesses: unused("listProcesses"),
- };
- return { sandbox: sandbox as unknown as Sandbox, ops };
-}
-
-interface FactoryFetchCall {
- url: string;
- method: string;
- body: string;
- query: string;
- headers: Headers;
-}
-
-interface FactoryFetch {
- fetch: typeof globalThis.fetch;
- calls: FactoryFetchCall[];
- fail: (match: string, status: number) => void;
-}
-
-function factoryFetch(
- opts: {
- intake?: string[];
- pr?: { draft?: boolean; open?: boolean; merged?: boolean };
- issue?: { name?: string; type?: string; labels?: string[] };
- } = {},
-): FactoryFetch {
- const calls: FactoryFetchCall[] = [];
- const pr = { draft: true, open: true, merged: false, ...opts.pr };
- const issue = { name: "Auto-Triage", type: "unstarted", labels: [] as string[], ...opts.issue };
- const teamStates = [
- { id: "st-triage", name: "Auto-Triage", type: "unstarted" },
- { id: "st-review", name: "In Review", type: "started" },
- { id: "st-done", name: "Done", type: "completed" },
- ];
- let failure: { match: string; status: number } | null = null;
-
- const linear = (query: string): unknown => {
- if (query.includes("FactoryIntake"))
- return {
- data: {
- team: {
- issues: {
- nodes: (opts.intake ?? [FACTORY_TICKET]).map((identifier) => ({
- identifier,
- title: `${identifier} title`,
- createdAt: "2026-01-01T00:00:00.000Z",
- inverseRelations: { nodes: [] },
- })),
- pageInfo: { hasNextPage: false, endCursor: null },
- },
- },
- },
- };
- if (query.includes("issueLabels(filter")) return { data: { issueLabels: { nodes: [{ id: "label-ready" }] } } };
- if (query.includes("issue(id:"))
- return {
- data: {
- issue: {
- id: "issue-1",
- state: { id: "st-current", name: issue.name, type: issue.type },
- labels: { nodes: issue.labels.map((name) => ({ id: name, name })) },
- team: { id: "TEAM-1", states: { nodes: teamStates } },
- },
- },
- };
- return { data: { mutation: { success: true } } };
- };
-
- const route = (url: string, method: string, query: string): unknown => {
- if (url === LINEAR_URL) return linear(query);
- if (url === GH_GRAPHQL)
- return query.includes("markPullRequestReadyForReview")
- ? { data: { markPullRequestReadyForReview: { pullRequest: { isDraft: false } } } }
- : { data: { repository: { pullRequest: { reviewThreads: { pageInfo: { hasNextPage: false }, nodes: [] } } } } };
- if (url.startsWith(`${GH_REPO}/pulls/42/reviews`)) return [{ user: { login: "cursor[bot]" }, commit_id: HEAD_SHA }];
- if (url.startsWith(`${GH_REPO}/pulls/42`))
- return method === "GET"
- ? {
- node_id: "PR_42",
- draft: pr.draft,
- state: pr.open ? "open" : "closed",
- merged: pr.merged,
- head: { sha: HEAD_SHA },
- mergeable: true,
- mergeable_state: "clean",
- }
- : {};
- if (url.startsWith(`${GH_REPO}/branches/`)) return { commit: { sha: HEAD_SHA } };
- if (url.includes("/check-runs"))
- return { check_runs: [{ name: "test", status: "completed", conclusion: "success" }] };
- if (url.startsWith(`${GH_REPO}/issues/42/comments`)) return {};
- throw new Error(`unrouted factory request: ${method} ${url}`);
- };
-
- return {
- calls,
- fail: (match, status) => {
- failure = { match, status };
- },
- fetch: (input, init) => {
- const url = String(input);
- const method = init?.method ?? "GET";
- const body = typeof init?.body === "string" ? init.body : "";
- const query = body ? String((JSON.parse(body) as { query?: unknown }).query ?? "") : "";
- calls.push({ url, method, body, query, headers: new Headers(init?.headers) });
- if (failure && url.includes(failure.match))
- return Promise.resolve(new Response("nope", { status: failure.status }));
- return Promise.resolve(Response.json(route(url, method, query) as Record<string, unknown>));
- },
- };
-}
-
-function factoryCredentials(missing: string[] = []): ServiceCredentialReader {
- return {
- getServiceCredentialSecret: async (_scope, slug) =>
- missing.includes(slug)
- ? null
- : {
- slug,
- name: slug,
- secret: slug === FACTORY_LINEAR_SLUG ? LINEAR_KEY : GITHUB_TOKEN,
- delivery: "broker",
- host: "api.example.com",
- deployments: false,
- enabled: true,
- },
- };
-}
-
-interface FactoryFake {
- deps: (loops: ReturnType<typeof createLoopStore>) => FactoryEffectsDeps;
- sandbox: FactorySandbox;
- fetch: FactoryFetch;
- instances: () => number;
-}
-
-function factoryFake(
- over: {
- config?: FactoryConfig | null;
- credentials?: ServiceCredentialReader;
- sandbox?: FactorySandbox;
- fetch?: FactoryFetch;
- } = {},
-): FactoryFake {
- const sandbox = over.sandbox ?? factorySandbox();
- const fetched = over.fetch ?? factoryFetch();
- const config = over.config === undefined ? FACTORY_CONFIG : over.config;
- let repoDirReads = 0;
- return {
- sandbox,
- fetch: fetched,
- instances: () => repoDirReads,
- deps: (loops) => ({
- sandbox: sandbox.sandbox,
- config: { getFactoryConfig: () => config },
- credentials: over.credentials ?? factoryCredentials(),
- orgScopeId: scopeId("org", "acme"),
- loops,
- fetch: fetched.fetch,
- pausePollMs: 1,
- get repoDir(): string {
- repoDirReads += 1;
- return "/workspace/repo";
- },
- }),
- };
-}
-
-const FACTORY_NO_TURNS: Responder = (req) => {
- if (stage(req) !== "other") throw new Error(`a factory loop must take no ${stage(req)} turn`);
- return "done";
-};
-
-async function makeFactoryLoop(loops: ReturnType<typeof createLoopStore>, over: Record<string, unknown> = {}) {
- return makeLoop(loops, {
- name: "factory",
- surface: FACTORY_LOOP_SURFACE,
- playbook: "the factory wrapper does the work",
- successCondition: "the pull request converged",
- shipActions: [
- { action: "open_pr", gate: "hold" },
- { action: "close_already_fixed", gate: "hold" },
- ],
- ...over,
- });
-}
-
-async function readyFactoryOutput(
- s: ReturnType<typeof service>,
- loopId: string,
- capture: { shipAction: string; title: string; externalRef?: string },
-) {
- const { item } = await s.items.enqueue({ loopId, sourceKey: FACTORY_TICKET });
- const claimed = await s.items.claim(item.id);
- const output = await s.outputs.capture({
- loopId,
- itemId: item.id,
- attemptId: "a1",
- capturedBy: "classifier",
- ...capture,
- });
- await s.items.markReady(item.id, [output.id], claimed!.claimToken!);
- await s.outputs.promoteAttempt(item.id, "a1");
- return output;
-}
-
-async function heldFactoryOutput(fake: FactoryFake, over: Record<string, unknown> = {}) {
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops, over);
- await s.fire.fire(loop.id, "f1");
- const output = (await s.outputs.awaitingReview(loop.id))[0]!;
- return { s, loop, output, before: fake.fetch.calls.length };
-}
-
-const traffic = (calls: FactoryFetchCall[]): string[] => calls.map((call) => `${call.method} ${call.url}`);
-
-test("factory surface: a fire drives the factory effects and takes no agent turn", async () => {
- const fake = factoryFake();
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
-
- const result = await s.fire.fire(loop.id, "f1");
-
- assert.equal(result.status, "ok");
- assert.equal(s.turns.length, 0);
- assert.equal(result.summary?.enqueued, 1);
- assert.equal(result.summary?.worked, 1);
- assert.equal(result.summary?.ready.length, 1);
- assert.equal(fake.instances(), 1);
- const intake = fake.fetch.calls[0];
- assert.equal(intake?.url, LINEAR_URL);
- assert.match(intake?.body ?? "", /"teamId":"TEAM-1"/);
- assert.equal(intake?.headers.get("Authorization"), LINEAR_KEY);
- assert.deepEqual(fake.sandbox.ops.slice(0, 5), ["provision", "run", "teardown", "provision", "startProcess"]);
- const items = await s.items.byLoop(loop.id);
- assert.equal(items[0]?.sourceKey, FACTORY_TICKET);
- const output = (await s.outputs.awaitingReview(loop.id))[0];
- assert.equal(output?.shipAction, "open_pr");
- assert.equal(output?.label, FACTORY_BRANCH);
- assert.equal(output?.externalRef, FACTORY_PR_URL);
- assert.equal(output?.capturedBy, "classifier");
- assert.ok(fake.fetch.calls.some((call) => call.url === `${GH_REPO}/pulls/42`));
-});
-
-test("factory surface: a repeated fire key is silent and re-runs neither intake nor the wrapper", async () => {
- const fake = factoryFake();
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
- await s.fire.fire(loop.id, "f1");
- const calls = fake.fetch.calls.length;
- const ops = fake.sandbox.ops.length;
-
- const repeat = await s.fire.fire(loop.id, "f1");
-
- assert.deepEqual(repeat, { status: "silent", note: "duplicate fire key" });
- assert.equal(fake.fetch.calls.length, calls);
- assert.equal(fake.sandbox.ops.length, ops);
- assert.equal((await s.items.byLoop(loop.id)).length, 1);
-});
-
-test("factory surface: a fire without the factory dep fails and runs nothing", async () => {
- const s = service(FACTORY_NO_TURNS);
- const loop = await makeFactoryLoop(s.loops);
-
- const result = await s.fire.fire(loop.id, "f1");
-
- assert.deepEqual(result, { status: "failed", note: "factory loop has no factory deps" });
- assert.equal(s.turns.length, 0);
- assert.deepEqual(await s.items.byLoop(loop.id), []);
- assert.equal((await s.loops.get(loop.id))?.consecutiveFailedFires, undefined);
-});
-
-test("factory surface: a missing config or credential fails the fire before the sandbox", async () => {
- const cases: [string, RegExp, Parameters<typeof factoryFake>[0]][] = [
- ["config", /factory_config_missing/, { config: null }],
- [
- "credential",
- /factory_credentials_missing: factory-github/,
- { credentials: factoryCredentials([FACTORY_GITHUB_SLUG]) },
- ],
- ];
- for (const [name, expected, over] of cases) {
- const fake = factoryFake(over);
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
-
- const result = await s.fire.fire(loop.id, "f1");
-
- assert.equal(result.status, "failed", name);
- assert.match(result.note ?? "", expected);
- assert.deepEqual(fake.sandbox.ops, []);
- }
-});
-
-test("factory surface: pausing the loop mid-run aborts the wrapper and fails the item", async () => {
- let pause: (() => Promise<void>) | null = null;
- const sandbox = factorySandbox({
- read: async (ops) => {
- if (ops.includes("signalProcess")) return { chunks: "", cursor: 0, status: { state: "exited", code: 143 } };
- if (pause) await pause();
- await new Promise((resolve) => setTimeout(resolve, 5));
- return { chunks: "", cursor: 0, status: { state: "running" } };
- },
- });
- const fake = factoryFake({ sandbox });
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
- pause = async () => {
- await s.loops.setState(loop.id, "paused");
- };
-
- const result = await s.fire.fire(loop.id, "f1");
-
- assert.equal(result.status, "failed");
- assert.deepEqual(result.summary?.failures, [`${FACTORY_TICKET}: factory_run_aborted`]);
- assert.ok(fake.sandbox.ops.includes("signalProcess"));
-});
-
-test("factory surface: shipping an open_pr output undrafts the request and advances the ticket", async () => {
- const fake = factoryFake();
- const { s, loop, output, before } = await heldFactoryOutput(fake);
-
- const shipped = await s.fire.shipOutput(loop.id, output.id, "josh");
-
- assert.equal(shipped?.state, "shipped");
- assert.equal(shipped?.decidedBy, "josh");
- assert.equal(shipped?.shipResult?.note, "undraft=true, linear-state=true, linear-label=true");
- assert.equal(shipped?.decisionNote, undefined);
- assert.equal((await s.items.get(output.itemId))?.status, "shipped");
- assert.equal(s.turns.length, 0);
- const sent = fake.fetch.calls.slice(before);
- assert.deepEqual(traffic(sent), [
- `GET ${GH_REPO}/pulls/42`,
- `POST ${GH_GRAPHQL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- ]);
- assert.equal(sent[0]?.headers.get("Authorization"), `Bearer ${GITHUB_TOKEN}`);
- assert.equal(sent[0]?.headers.get("Accept"), "application/vnd.github+json");
- assert.match(sent[1]!.query, /markPullRequestReadyForReview/);
- assert.match(sent[3]!.query, /issueUpdate\(id: "issue-1", input: \{ stateId: "st-review" \}\)/);
- assert.match(sent[5]!.query, /issueAddLabel\(id: "issue-1", labelId: "label-ready"\)/);
-});
-
-test("factory surface: re-deciding an already-shipped pull request reports every step unchanged", async () => {
- const fake = factoryFake({
- fetch: factoryFetch({
- pr: { draft: false },
- issue: { name: "In Review", type: "started", labels: ["ready-for-review"] },
- }),
- });
- const { s, loop, output, before } = await heldFactoryOutput(fake);
-
- const shipped = await s.fire.shipOutput(loop.id, output.id, "josh");
-
- assert.equal(shipped?.state, "shipped");
- assert.equal(shipped?.shipResult?.note, "undraft=false, linear-state=false, linear-label=false");
- const sent = fake.fetch.calls.slice(before);
- assert.deepEqual(traffic(sent), [`GET ${GH_REPO}/pulls/42`, `POST ${LINEAR_URL}`]);
-});
-
-test("factory surface: a forge failure while shipping releases the claim and re-throws", async () => {
- const fake = factoryFake();
- const { s, loop, output } = await heldFactoryOutput(fake);
- fake.fetch.fail(`${GH_REPO}/pulls/42`, 500);
-
- await assert.rejects(s.fire.shipOutput(loop.id, output.id, "josh"), /forge_undraft_failed: 500/);
-
- assert.equal((await s.outputs.get(output.id))?.state, "ready");
- assert.equal((await s.items.get(output.itemId))?.status, "ready");
-});
-
-test("factory surface: shipping an already-fixed output posts to Linear and touches no forge", async () => {
- const fake = factoryFake({ sandbox: factorySandbox({ stdout: ALREADY_FIXED_STDOUT }) });
- const { s, loop, output, before } = await heldFactoryOutput(fake);
-
- const shipped = await s.fire.shipOutput(loop.id, output.id, "josh");
-
- assert.equal(shipped?.state, "shipped");
- assert.equal(shipped?.shipResult?.note, "linear-comment=true, linear-state=true");
- const sent = fake.fetch.calls.slice(before);
- assert.deepEqual(traffic(sent), [`POST ${LINEAR_URL}`, `POST ${LINEAR_URL}`, `POST ${LINEAR_URL}`]);
- assert.match(sent[1]!.query, /body: "Already fixed\. fixed by #40"/);
- assert.match(sent[2]!.query, /stateId: "st-done"/);
- assert.equal(fake.fetch.calls.filter((call) => call.url.startsWith("https://api.github.com/")).length, 0);
-});
-
-test("factory surface: an already-fixed output on a terminal ticket only comments, and bare evidence stays bare", async () => {
- const terminal = factoryFake({
- sandbox: factorySandbox({ stdout: ALREADY_FIXED_STDOUT }),
- fetch: factoryFetch({ issue: { name: "Done", type: "completed" } }),
- });
- const held = await heldFactoryOutput(terminal);
- const shipped = await held.s.fire.shipOutput(held.loop.id, held.output.id, "josh");
- assert.equal(shipped?.shipResult?.note, "linear-comment=true, linear-state=false");
- assert.deepEqual(traffic(terminal.fetch.calls.slice(held.before)), [`POST ${LINEAR_URL}`, `POST ${LINEAR_URL}`]);
-
- const bare = factoryFake({ sandbox: factorySandbox({ stdout: "ALREADY_FIXED:true\n" }) });
- const noEvidence = await heldFactoryOutput(bare);
- await noEvidence.s.fire.shipOutput(noEvidence.loop.id, noEvidence.output.id, "josh");
- assert.match(bare.fetch.calls.slice(noEvidence.before)[1]!.query, /body: "Already fixed\."/);
-});
-
-test("factory surface: an unknown ship action or a ref without a number fails before any request", async () => {
- for (const broken of [
- { shipAction: "merge_pr", externalRef: FACTORY_PR_URL, expected: /factory_ship_action_unknown: merge_pr/ },
- {
- shipAction: "open_pr",
- externalRef: "https://github.com/acme/app/pull/main",
- expected: /factory_ship_ref_missing/,
- },
- ]) {
- const fake = factoryFake();
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
- const output = await readyFactoryOutput(s, loop.id, {
- shipAction: broken.shipAction,
- title: broken.shipAction,
- externalRef: broken.externalRef,
- });
-
- await assert.rejects(s.fire.shipOutput(loop.id, output.id, "josh"), broken.expected);
-
- assert.deepEqual(fake.fetch.calls, []);
- assert.equal((await s.outputs.get(output.id))?.state, "ready");
- }
-});
-
-test("factory surface: shipping without the factory dep fails instead of taking the turn path", async () => {
- const s = service(FACTORY_NO_TURNS);
- const loop = await makeFactoryLoop(s.loops);
- const output = await readyFactoryOutput(s, loop.id, {
- shipAction: "open_pr",
- title: "fix",
- externalRef: FACTORY_PR_URL,
- });
-
- await assert.rejects(s.fire.shipOutput(loop.id, output.id, "josh"), /factory loop has no factory deps/);
-
- assert.equal(s.turns.length, 0);
- assert.equal((await s.outputs.get(output.id))?.state, "ready");
- assert.equal((await s.items.get(output.itemId))?.status, "ready");
-});
-
-test("factory surface: an auto grant ships through the same forge path with no turn", async () => {
- const grants = createShipGrantStore();
- const fake = factoryFake();
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps, grants });
- const loop = await makeFactoryLoop(s.loops);
- await grants.put(
- buildShipGrant({
- loopId: loop.id,
- shipAction: "open_pr",
- actorId: "josh",
- label: FACTORY_BRANCH,
- policyVersion: loop.policyVersion,
- }),
- );
-
- const result = await s.fire.fire(loop.id, "f1");
-
- const item = (await s.items.byLoop(loop.id))[0]!;
- assert.deepEqual(result.summary?.shipped, [item.id]);
- assert.equal(s.turns.length, 0);
- assert.deepEqual(await s.outputs.awaitingReview(loop.id), []);
- assert.equal((await s.outputs.byItem(item.id))[0]?.state, "shipped");
- assert.ok(fake.fetch.calls.some((call) => call.query.includes("markPullRequestReadyForReview")));
-});
-
-test("factory surface: returning an output closes the pull request and re-triages the ticket", async () => {
- const fake = factoryFake({ fetch: factoryFetch({ issue: { name: "In Review", type: "started" } }) });
- const { s, loop, output, before } = await heldFactoryOutput(fake);
-
- const returned = await s.fire.returnOutput(loop.id, output.id, "josh", "tests are flaky");
-
- assert.equal(returned?.state, "returned");
- assert.equal((await s.items.get(output.itemId))?.status, "queued");
- assert.equal(s.turns.length, 0);
- const sent = fake.fetch.calls.slice(before);
- assert.deepEqual(traffic(sent), [
- `POST ${GH_REPO}/issues/42/comments`,
- `GET ${GH_REPO}/pulls/42`,
- `PATCH ${GH_REPO}/pulls/42`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- ]);
- assert.match(sent[0]!.body, /"Returned by the factory reviewer: tests are flaky"/);
- assert.match(sent[2]!.body, /"state":"closed"/);
- assert.match(sent[4]!.query, /body: "Returned to Auto-Triage: tests are flaky"/);
- assert.match(sent[5]!.query, /stateId: "st-triage"/);
-});
-
-test("factory surface: returning an already-fixed output leaves its evidence pull request open", async () => {
- const fake = factoryFake({
- sandbox: factorySandbox({ stdout: `${ALREADY_FIXED_STDOUT}MR:42\n` }),
- fetch: factoryFetch({ issue: { name: "In Review", type: "started" } }),
- });
- const { s, loop, output, before } = await heldFactoryOutput(fake);
- assert.equal(output.shipAction, "close_already_fixed");
- assert.equal(output.externalRef, FACTORY_PR_URL);
-
- const returned = await s.fire.returnOutput(loop.id, output.id, "josh", "still reproduces");
-
- assert.equal(returned?.state, "returned");
- assert.deepEqual(traffic(fake.fetch.calls.slice(before)), [
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- ]);
-});
-
-test("factory surface: a merged pull request is not closed on return", async () => {
- const fake = factoryFake({
- fetch: factoryFetch({
- pr: { draft: false, open: false, merged: true },
- issue: { name: "In Review", type: "started" },
- }),
- });
- const { s, loop, output, before } = await heldFactoryOutput(fake);
-
- await s.fire.returnOutput(loop.id, output.id, "josh", "shipped elsewhere");
-
- assert.deepEqual(traffic(fake.fetch.calls.slice(before)), [
- `POST ${GH_REPO}/issues/42/comments`,
- `GET ${GH_REPO}/pulls/42`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- `POST ${LINEAR_URL}`,
- ]);
-});
-
-test("factory surface: an output with no external ref runs the Linear steps only", async () => {
- const fake = factoryFake({ fetch: factoryFetch({ issue: { name: "In Review", type: "started" } }) });
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops);
- const output = await readyFactoryOutput(s, loop.id, { shipAction: "open_pr", title: "no ref" });
-
- await s.fire.returnOutput(loop.id, output.id, "josh", "start over");
-
- assert.deepEqual(traffic(fake.fetch.calls), [`POST ${LINEAR_URL}`, `POST ${LINEAR_URL}`, `POST ${LINEAR_URL}`]);
-});
-
-test("factory surface: a forge failure while returning leaves the ledger return recorded", async () => {
- const fake = factoryFake();
- const { s, loop, output } = await heldFactoryOutput(fake);
- fake.fetch.fail(`${GH_REPO}/issues/42/comments`, 500);
-
- await assert.rejects(s.fire.returnOutput(loop.id, output.id, "josh", "tests are flaky"), /forge_comment_failed: 500/);
-
- assert.equal((await s.outputs.get(output.id))?.state, "returned");
- assert.equal((await s.items.get(output.itemId))?.status, "queued");
-});
-
-test("factory surface: a follow-up records the message and takes no turn", async () => {
- const fake = factoryFake();
- const { s, loop, before } = await heldFactoryOutput(fake);
- const item = (await s.items.byLoop(loop.id))[0]!;
-
- const after = await s.fire.followUp(loop, item, "please rebase", "josh");
-
- assert.equal(s.turns.length, 0);
- assert.deepEqual(
- (after?.thread ?? []).map((message) => [message.role, message.text]),
- [["human", "please rebase"]],
- );
- assert.equal(after?.proposal, undefined);
- assert.equal(fake.fetch.calls.length, before);
-
- const action = await s.fire.itemAction(loop, item, "nudge", {});
- assert.equal(action.ok, true);
- assert.equal(s.turns.length, 1);
-});
-
-test("factory surface: an undeclared already-fixed artifact parks the item and ships nothing", async () => {
- const fake = factoryFake({ sandbox: factorySandbox({ stdout: ALREADY_FIXED_STDOUT }) });
- const s = service(FACTORY_NO_TURNS, { factory: fake.deps });
- const loop = await makeFactoryLoop(s.loops, { shipActions: [{ action: "open_pr", gate: "hold" }] });
-
- const result = await s.fire.fire(loop.id, "f1");
-
- const item = (await s.items.byLoop(loop.id))[0]!;
- assert.deepEqual(result.summary?.undeclaredShipActions, ["close_already_fixed"]);
- assert.deepEqual(result.summary?.parked, [item.id]);
- assert.deepEqual(await s.outputs.awaitingReview(loop.id), []);
-});
-
-test("factory surface: a non-factory loop with the dep wired keeps the turn pipeline", async () => {
- const fake = factoryFake();
- const s = service(HAPPY, { factory: fake.deps });
- const loop = await makeLoop(s.loops);
-
- await s.fire.fire(loop.id, "f1");
- const output = (await s.outputs.awaitingReview(loop.id))[0]!;
- await s.fire.shipOutput(loop.id, output.id, "josh");
- await s.fire.followUp(loop, (await s.items.get(output.itemId))!, "any news?", "josh");
-
- assert.deepEqual(s.turns.map(stage), ["intake", "work", "judge", "ship", "other"]);
- assert.deepEqual(fake.fetch.calls, []);
- assert.deepEqual(fake.sandbox.ops, []);
- assert.equal(fake.instances(), 0);
-});
diff --git a/test/loop-routes.test.ts b/test/loop-routes.test.ts
index 19b5d733a..de1565577 100644
--- a/test/loop-routes.test.ts
+++ b/test/loop-routes.test.ts
@@ -606,33 +606,6 @@ test("deciding an output ships or returns through the fire service", async () =>
assert.deepEqual(decisions, ["ship:o1:josh", "return:o1:josh:not yet"]);
});
-test("a failing ship or return reports the cause instead of a bare server error", async () => {
- const deps = services();
- deps.fire = {
- fire: async () => ({ status: "ok" as const }),
- shipOutput: async () => {
- throw new Error("forge_undraft_failed: 403");
- },
- returnOutput: async () => {
- throw new Error("linear_state_missing: Auto-Triage");
- },
- sweepStale: async () => {},
- followUp: async () => null,
- itemAction: async () => ({ ok: true }),
- };
- const created = await call(deps, "POST", "/v1/loops", CREATE);
- const id = (created.body as { loop: { id: string } }).loop.id;
- const shipFailed = await call(deps, "POST", `/v1/loops/${id}/outputs/o1/decide`, { decision: "ship" });
- assert.equal(shipFailed.status, 502);
- assert.deepEqual(shipFailed.body, { error: "ship_failed", message: "forge_undraft_failed: 403" });
- const returnFailed = await call(deps, "POST", `/v1/loops/${id}/outputs/o1/decide`, {
- decision: "return",
- note: "not yet",
- });
- assert.equal(returnFailed.status, 502);
- assert.deepEqual(returnFailed.body, { error: "return_failed", message: "linear_state_missing: Auto-Triage" });
-});
-
test("deciding an output reports an active item decision lease", async () => {
const deps = services();
deps.fire = {