@bridge_gpt/mcp-server 0.2.71 → 0.2.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -17
- package/build/agent-launchers/claude-executor-adapter.js +22 -3
- package/build/agent-notes.js +171 -95
- package/build/agents.generated.js +7 -7
- package/build/bridge-permissions.js +31 -0
- package/build/bridge-worktree-inspection.js +237 -0
- package/build/check-evidence.js +6 -0
- package/build/ci-system-detection.js +50 -0
- package/build/ci-trigger-adapters.js +631 -0
- package/build/ci-trigger-install.js +106 -0
- package/build/claude-review-workflow.js +1 -1
- package/build/cli-help.js +6 -0
- package/build/codex-auto-recovery.js +126 -0
- package/build/codex-launcher-pin.js +33 -2
- package/build/codex-skill-adapter.js +5 -1
- package/build/command-host.js +33 -0
- package/build/commands.generated.js +7 -6
- package/build/conductor/bridge-api-client.js +1352 -50
- package/build/conductor/bring-up-facts.js +18 -5
- package/build/conductor/command-drift-production.js +42 -17
- package/build/conductor/command-drift.js +47 -0
- package/build/conductor/declare-blocked.js +554 -0
- package/build/conductor/doctor.js +187 -28
- package/build/conductor/driver-commands.js +141 -0
- package/build/conductor/driver-guard.js +277 -0
- package/build/conductor/driver-lapse.js +321 -0
- package/build/conductor/driver-register.js +176 -0
- package/build/conductor/driver-release.js +174 -0
- package/build/conductor/driver-session-state.js +315 -0
- package/build/conductor/epic-branch-ci-inspection.js +222 -29
- package/build/conductor/epic-implementer-cli.js +213 -13
- package/build/conductor/epic-server-database.js +162 -0
- package/build/conductor/epic-server-guidance.js +278 -0
- package/build/conductor/epic-server-journal.js +239 -0
- package/build/conductor/epic-server-processes.js +324 -0
- package/build/conductor/epic-server-recipe.js +397 -0
- package/build/conductor/epic-server-runtime.js +1207 -0
- package/build/conductor/epic-server-sfcc.js +107 -0
- package/build/conductor/epic-server-state.js +427 -0
- package/build/conductor/epic-server.js +594 -0
- package/build/conductor/gate-discovery.js +281 -10
- package/build/conductor/gate-settings-review.js +525 -5
- package/build/conductor/git-hooks-uninstall.js +112 -0
- package/build/conductor/git-hooks.js +81 -15
- package/build/conductor/guided-repair.js +10 -1
- package/build/conductor/install-doctor.js +2 -4
- package/build/conductor/launch-need-probes.js +6 -53
- package/build/conductor/launch-plan.js +318 -121
- package/build/conductor/local-merge.js +12 -1
- package/build/conductor/merge-execution-contract.js +1 -1
- package/build/conductor/merged-child-check.js +2 -1
- package/build/conductor/prerequisite-inputs.js +1213 -0
- package/build/conductor/readiness-assessment.js +93 -0
- package/build/conductor/readiness-cli.js +30 -9
- package/build/conductor/readiness-prerequisites.js +77 -5
- package/build/conductor/readiness-sections.js +146 -126
- package/build/conductor/readiness.js +21 -3
- package/build/conductor/recovery-cli.js +236 -15
- package/build/conductor/recovery-operations.js +34 -4
- package/build/conductor/repair-contract.js +33 -0
- package/build/conductor/tools.js +36 -4
- package/build/conductor/wait-run-attention.js +115 -0
- package/build/conductor/wait-run-cli.js +300 -21
- package/build/conductor/worker-database-doctor-inspector.js +34 -2
- package/build/conductor/worker-setup-tally.js +27 -0
- package/build/credential-store.js +96 -21
- package/build/dependency-install-declarations.js +212 -0
- package/build/docs.generated.js +4 -4
- package/build/doctor.js +91 -8
- package/build/drive-epic-help.js +23 -4
- package/build/drive-epic.js +1841 -340
- package/build/engine-paths.js +20 -0
- package/build/epic-branch-ci-summary.js +140 -0
- package/build/epic-capability-statement.js +107 -0
- package/build/epic-ci-workflow-drift.js +30 -15
- package/build/epic-ci-workflow-install.js +110 -42
- package/build/epic-ci-workflow-v2.js +274 -0
- package/build/epic-ci-workflow.js +38 -12
- package/build/epic-implementer-bin.js +2184 -52
- package/build/epic-launch-context.js +607 -0
- package/build/epic-launch-profile.js +1 -1
- package/build/epic-server-record.js +273 -0
- package/build/epic-shared/bridge-client.js +11 -3
- package/build/escalation-reachability.js +43 -0
- package/build/executor/ci-fix-test-runs.js +334 -0
- package/build/executor/cli.js +2 -0
- package/build/executor/completion-contract.js +24 -1
- package/build/executor/credentials.js +1 -1
- package/build/executor/delivery-observations.js +113 -0
- package/build/executor/delivery-outcome.js +30 -0
- package/build/executor/dependency-refresh-runner.js +132 -0
- package/build/executor/dependency-refresh.js +530 -0
- package/build/executor/deps.js +4 -0
- package/build/executor/env.js +119 -17
- package/build/executor/epic-server-context.js +141 -0
- package/build/executor/gate-qualification-job.js +127 -26
- package/build/executor/harness-coverage.js +19 -0
- package/build/executor/http-client.js +87 -6
- package/build/executor/index.js +2 -1
- package/build/executor/install-preflight.js +1 -1
- package/build/executor/job-runner.js +659 -87
- package/build/executor/likely-tests.js +94 -97
- package/build/executor/observation.js +8 -0
- package/build/executor/pre-pr-checks.js +363 -0
- package/build/executor/prompt-spec.js +127 -5
- package/build/executor/reconcile-overlap-job.js +5 -3
- package/build/executor/required-verification.js +28 -0
- package/build/executor/required-work-diagnostics.js +32 -0
- package/build/executor/required-work-ledger.js +955 -39
- package/build/executor/results.js +100 -0
- package/build/executor/service-discovery.js +42 -0
- package/build/executor/service-lifecycle.js +109 -2
- package/build/executor/service-uninstall.js +205 -0
- package/build/executor/service-unit.js +5 -3
- package/build/executor/terminal-mutation.js +8 -0
- package/build/executor/test-clock.js +3 -0
- package/build/executor/verification-hints.js +179 -0
- package/build/executor/worker-command.js +126 -5
- package/build/executor/worker-database-config.js +281 -0
- package/build/executor/worker-database-target.js +16 -12
- package/build/executor/worker-database.js +575 -116
- package/build/executor/worker-finalization.js +71 -10
- package/build/executor/worker-guard-hook.js +10 -2
- package/build/executor/worker-resources.js +25 -3
- package/build/executor/worker-verification-guidance.js +55 -5
- package/build/executor/worktree-inspection.js +42 -0
- package/build/executor/worktree.js +20 -1
- package/build/executor-launch.js +54 -2
- package/build/generated-assets-catalog.js +2 -2
- package/build/github-sign-in.js +1 -1
- package/build/index.js +1111 -381
- package/build/init.js +29 -14
- package/build/install-bridge-conductor-answers.js +465 -0
- package/build/install-bridge-conductor-apply.js +259 -0
- package/build/install-bridge-conductor-assessment.js +895 -0
- package/build/install-bridge-conductor-compact.js +161 -0
- package/build/install-bridge-conductor-contracts.js +63 -0
- package/build/install-bridge-conductor-gate.js +1383 -188
- package/build/install-bridge-conductor-setup.js +637 -92
- package/build/install-bridge-conductor.js +3163 -740
- package/build/install-bridge.js +601 -128
- package/build/install-maintenance.js +127 -0
- package/build/install-reexec.js +1 -1
- package/build/launcher-config-inspection.js +162 -1
- package/build/local-cleanup.js +59 -0
- package/build/mcp-grant-candidates.js +767 -0
- package/build/mcp-grant-doctor.js +63 -0
- package/build/mcp-grant.js +288 -0
- package/build/mcp-host-targets.js +8 -0
- package/build/mcp-identity.js +23 -0
- package/build/mcp-install-state.js +68 -2
- package/build/mcp-invoke.js +126 -13
- package/build/mcp-presets.js +107 -0
- package/build/mcp-provisioning.js +134 -2
- package/build/mcp-registration-doctor.js +31 -2
- package/build/mcp-server-invocation.js +6 -1
- package/build/merge-workflow-install.js +236 -111
- package/build/package-script-semantics.js +10 -0
- package/build/pipeline-utils.js +1 -1
- package/build/pipelines.generated.js +6 -6
- package/build/plan-discovery.js +97 -4
- package/build/plan-execution-ledger.js +248 -9
- package/build/plan-generation-io.js +262 -0
- package/build/plan-generation.js +424 -0
- package/build/plane/build-freshness.js +6 -1
- package/build/plane/cli.js +75 -9
- package/build/plane/plane-freshness.js +23 -7
- package/build/plane/preflight.js +50 -10
- package/build/plane/shutdown.js +19 -0
- package/build/plane/terminal-run-cleanup.js +23 -5
- package/build/plane/terminal-run-status.js +4 -0
- package/build/printed-remedy-commands.js +229 -15
- package/build/readiness-check.js +1 -1
- package/build/readiness-prerequisites.generated.js +311 -4
- package/build/readme.generated.js +1 -1
- package/build/repository-state-paths.js +38 -0
- package/build/repository-toolchains.js +50 -8
- package/build/run-unit-tests-launcher.js +8 -1
- package/build/setup-change-policy.js +32 -0
- package/build/setup-epic.js +1192 -198
- package/build/setup-progress.js +44 -0
- package/build/setup-question.js +180 -0
- package/build/start-tickets.js +4 -0
- package/build/test-command-detection.js +32 -88
- package/build/test-support/notes-fs.js +40 -0
- package/build/third-party-mcp-targets.js +30 -0
- package/build/ticket-delivery-enrollment.js +30 -8
- package/build/ticket-delivery-run-scope.js +128 -0
- package/build/upgrade-cli.js +85 -2
- package/build/v2-readiness-requirements.js +28 -1
- package/build/version.generated.js +3 -3
- package/build/worker-guard-hook-bin.js +2 -2
- package/build/worktrees.js +105 -0
- package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
- package/docs/EPIC-IMPLEMENTER.md +5 -0
- package/docs/epic-implementer/getting-started.md +42 -15
- package/docs/epic-implementer/self-hosting.md +14 -1
- package/package.json +2 -2
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The epic-server record contract (BAPI-1545 defined the minimal reader; BAPI-1546
|
|
3
|
+
* owns the schema and adds the one writer).
|
|
4
|
+
*
|
|
5
|
+
* The record lives at `~/.config/bridge/epic-server/<repo>.json`, outside every
|
|
6
|
+
* worktree, in its own file beside the MCP grant. Version 1:
|
|
7
|
+
*
|
|
8
|
+
* required version: 1
|
|
9
|
+
* required mode: "none" | "local" | "sfcc_shared_active"
|
|
10
|
+
* optional recipe: "present" | "absent"
|
|
11
|
+
* sfcc_shared_active only, required:
|
|
12
|
+
* sfcc: { sandbox_id, credential_ref, storefront_origin }
|
|
13
|
+
*
|
|
14
|
+
* `sandbox_id` is a non-secret instance identifier. `credential_ref` is a
|
|
15
|
+
* credential-store target that names the sandbox-only test account; the record
|
|
16
|
+
* NEVER holds a secret value. `storefront_origin` is the approved http(s) origin
|
|
17
|
+
* the browser is limited to. A `none` or `local` record that carries `sfcc` is
|
|
18
|
+
* malformed.
|
|
19
|
+
*
|
|
20
|
+
* Unknown top-level keys are tolerated, matching the grant's schema rule. This
|
|
21
|
+
* module is the ONLY reader and writer of the record: the setup item, the
|
|
22
|
+
* capability statement, the driver's helper, and the executor all import from
|
|
23
|
+
* here (coherence finding F-1).
|
|
24
|
+
*
|
|
25
|
+
* Install reads the record to build the advisory capability statement:
|
|
26
|
+
*
|
|
27
|
+
* - no file → epic_server_mode "none", recipe "unknown"
|
|
28
|
+
* - unreadable / bad → epic_server_mode "unknown", recipe "unknown"
|
|
29
|
+
* - valid → its mode, and its recipe or "unknown" when omitted
|
|
30
|
+
*
|
|
31
|
+
* The statement's `recipe` value says whether repository guidance was found at
|
|
32
|
+
* setup. It is never proof that a run recipe was proven or that a server is live.
|
|
33
|
+
* Recipe availability is never inferred from worktree files.
|
|
34
|
+
*/
|
|
35
|
+
import path from "path";
|
|
36
|
+
import { repositoryStateRecordPath } from "./repository-state-paths.js";
|
|
37
|
+
export const EPIC_SERVER_RECORD_VERSION = 1;
|
|
38
|
+
export const EPIC_SERVER_MODES = ["none", "local", "sfcc_shared_active"];
|
|
39
|
+
/** The record path for a repository, or null when the name is unusable. */
|
|
40
|
+
export function epicServerRecordPath(repoName, deps) {
|
|
41
|
+
const resolved = repositoryStateRecordPath("epic-server", repoName, deps);
|
|
42
|
+
return resolved.ok ? resolved.path : null;
|
|
43
|
+
}
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// Field rules
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
const SANDBOX_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
48
|
+
/** A credential-store target: printable, no whitespace, bounded. */
|
|
49
|
+
const CREDENTIAL_REF_RE = /^[A-Za-z0-9][A-Za-z0-9._:@/+-]{0,199}$/;
|
|
50
|
+
const MAX_ORIGIN_CHARS = 2048;
|
|
51
|
+
/** True for a usable non-secret sandbox identifier. */
|
|
52
|
+
export function isValidSfccSandboxId(value) {
|
|
53
|
+
return typeof value === "string" && SANDBOX_ID_RE.test(value);
|
|
54
|
+
}
|
|
55
|
+
/** True for a usable credential-store reference. It names a secret; it is never one. */
|
|
56
|
+
export function isValidCredentialReference(value) {
|
|
57
|
+
return typeof value === "string" && CREDENTIAL_REF_RE.test(value) && !value.includes("//");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Normalize an approved storefront origin. Only an http or https origin with no
|
|
61
|
+
* credentials, query, fragment, or path beyond `/` is accepted. Returns the
|
|
62
|
+
* normalized origin (no trailing slash), or null.
|
|
63
|
+
*/
|
|
64
|
+
export function normalizeStorefrontOrigin(value) {
|
|
65
|
+
if (typeof value !== "string" || value.length === 0 || value.length > MAX_ORIGIN_CHARS)
|
|
66
|
+
return null;
|
|
67
|
+
if (/\s/.test(value))
|
|
68
|
+
return null;
|
|
69
|
+
let url;
|
|
70
|
+
try {
|
|
71
|
+
url = new URL(value);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
77
|
+
return null;
|
|
78
|
+
if (url.username !== "" || url.password !== "")
|
|
79
|
+
return null;
|
|
80
|
+
if (url.search !== "" || url.hash !== "" || value.includes("?") || value.includes("#"))
|
|
81
|
+
return null;
|
|
82
|
+
if (url.pathname !== "/" && url.pathname !== "")
|
|
83
|
+
return null;
|
|
84
|
+
if (url.hostname === "")
|
|
85
|
+
return null;
|
|
86
|
+
// The parsed origin is the normalized form (lowercase host, no default port).
|
|
87
|
+
return url.origin;
|
|
88
|
+
}
|
|
89
|
+
function isPlainObject(value) {
|
|
90
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
91
|
+
}
|
|
92
|
+
function validateSfccTarget(raw) {
|
|
93
|
+
if (!isPlainObject(raw))
|
|
94
|
+
return null;
|
|
95
|
+
if (!isValidSfccSandboxId(raw.sandbox_id))
|
|
96
|
+
return null;
|
|
97
|
+
if (!isValidCredentialReference(raw.credential_ref))
|
|
98
|
+
return null;
|
|
99
|
+
const origin = normalizeStorefrontOrigin(raw.storefront_origin);
|
|
100
|
+
if (origin === null)
|
|
101
|
+
return null;
|
|
102
|
+
return { sandbox_id: raw.sandbox_id, credential_ref: raw.credential_ref, storefront_origin: origin };
|
|
103
|
+
}
|
|
104
|
+
/** Validate a parsed record. Unknown top-level keys are dropped. */
|
|
105
|
+
export function validateEpicServerRecord(raw) {
|
|
106
|
+
if (!isPlainObject(raw))
|
|
107
|
+
return null;
|
|
108
|
+
const doc = raw;
|
|
109
|
+
if (doc.version !== EPIC_SERVER_RECORD_VERSION)
|
|
110
|
+
return null;
|
|
111
|
+
if (typeof doc.mode !== "string" || !EPIC_SERVER_MODES.includes(doc.mode))
|
|
112
|
+
return null;
|
|
113
|
+
const record = { version: EPIC_SERVER_RECORD_VERSION, mode: doc.mode };
|
|
114
|
+
if (doc.recipe !== undefined) {
|
|
115
|
+
if (doc.recipe !== "present" && doc.recipe !== "absent")
|
|
116
|
+
return null;
|
|
117
|
+
record.recipe = doc.recipe;
|
|
118
|
+
}
|
|
119
|
+
if (record.mode === "sfcc_shared_active") {
|
|
120
|
+
const sfcc = validateSfccTarget(doc.sfcc);
|
|
121
|
+
if (sfcc === null)
|
|
122
|
+
return null;
|
|
123
|
+
record.sfcc = sfcc;
|
|
124
|
+
}
|
|
125
|
+
else if (doc.sfcc !== undefined) {
|
|
126
|
+
// SFCC-only fields on a local or none record are a malformed record.
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return record;
|
|
130
|
+
}
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
// Reader
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
/** Read the record. Only `ENOENT` is `absent`; anything else unusable is `unusable`. */
|
|
135
|
+
export async function readEpicServerRecord(repoName, deps) {
|
|
136
|
+
const resolved = repositoryStateRecordPath("epic-server", repoName, deps);
|
|
137
|
+
if (!resolved.ok)
|
|
138
|
+
return { state: "unusable" };
|
|
139
|
+
if (deps.lstat) {
|
|
140
|
+
try {
|
|
141
|
+
const st = await deps.lstat(resolved.path);
|
|
142
|
+
if (st.isSymbolicLink() || !st.isFile())
|
|
143
|
+
return { state: "unusable" };
|
|
144
|
+
if ((deps.platform ?? process.platform) !== "win32" && (st.mode & 0o077) !== 0)
|
|
145
|
+
return { state: "unusable" };
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
const code = err && typeof err === "object" ? err.code : undefined;
|
|
149
|
+
return code === "ENOENT" ? { state: "absent" } : { state: "unusable" };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let text;
|
|
153
|
+
try {
|
|
154
|
+
text = await deps.readFile(resolved.path);
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
const code = err && typeof err === "object" ? err.code : undefined;
|
|
158
|
+
return code === "ENOENT" ? { state: "absent" } : { state: "unusable" };
|
|
159
|
+
}
|
|
160
|
+
let parsed;
|
|
161
|
+
try {
|
|
162
|
+
parsed = JSON.parse(text);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return { state: "unusable" };
|
|
166
|
+
}
|
|
167
|
+
const record = validateEpicServerRecord(parsed);
|
|
168
|
+
return record === null ? { state: "unusable" } : { state: "ok", record };
|
|
169
|
+
}
|
|
170
|
+
let tempCounter = 0;
|
|
171
|
+
function defaultTempSuffix() {
|
|
172
|
+
tempCounter += 1;
|
|
173
|
+
return `${process.pid}.${tempCounter}.${Date.now().toString(36)}`;
|
|
174
|
+
}
|
|
175
|
+
/** Serialize a record deterministically: 2-space indent and a trailing newline. */
|
|
176
|
+
export function formatEpicServerRecord(record) {
|
|
177
|
+
const out = { version: record.version, mode: record.mode };
|
|
178
|
+
if (record.recipe !== undefined)
|
|
179
|
+
out.recipe = record.recipe;
|
|
180
|
+
if (record.sfcc !== undefined) {
|
|
181
|
+
out.sfcc = {
|
|
182
|
+
sandbox_id: record.sfcc.sandbox_id,
|
|
183
|
+
credential_ref: record.sfcc.credential_ref,
|
|
184
|
+
storefront_origin: record.sfcc.storefront_origin,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return `${JSON.stringify(out, null, 2)}\n`;
|
|
188
|
+
}
|
|
189
|
+
async function isSymlink(deps, filePath) {
|
|
190
|
+
if (!deps.lstat)
|
|
191
|
+
return false;
|
|
192
|
+
try {
|
|
193
|
+
return (await deps.lstat(filePath)).isSymbolicLink();
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Publish a record durably, following `writeMcpGrant()`: an exclusively created
|
|
201
|
+
* same-directory temp file with mode `0600`, write, fsync, close, chmod `0600`,
|
|
202
|
+
* atomic rename, then a best-effort directory fsync. The `epic-server` directory
|
|
203
|
+
* is created and held at mode `0700`. A symbolic link at the directory or record
|
|
204
|
+
* path is refused. A failure leaves any previous record in place and returns a
|
|
205
|
+
* fixed error that never echoes a field value.
|
|
206
|
+
*/
|
|
207
|
+
export async function writeEpicServerRecord(repoName, record, deps) {
|
|
208
|
+
const validated = validateEpicServerRecord(record);
|
|
209
|
+
if (validated === null)
|
|
210
|
+
return { ok: false, error: "the epic-server record to write is not valid" };
|
|
211
|
+
const resolved = repositoryStateRecordPath("epic-server", repoName, deps);
|
|
212
|
+
if (!resolved.ok)
|
|
213
|
+
return { ok: false, error: "the repository name is not usable as a record name" };
|
|
214
|
+
const isPosix = deps.platform !== "win32";
|
|
215
|
+
const dir = resolved.dir;
|
|
216
|
+
const tempPath = path.join(dir, `${path.basename(resolved.path)}.${(deps.tempSuffix ?? defaultTempSuffix)()}.tmp`);
|
|
217
|
+
let handle;
|
|
218
|
+
try {
|
|
219
|
+
await deps.mkdir(dir, { recursive: true });
|
|
220
|
+
if (await isSymlink(deps, dir))
|
|
221
|
+
return { ok: false, error: "the epic-server record directory is a symbolic link" };
|
|
222
|
+
if (await isSymlink(deps, resolved.path))
|
|
223
|
+
return { ok: false, error: "the epic-server record path is a symbolic link" };
|
|
224
|
+
if (isPosix)
|
|
225
|
+
await deps.chmod(dir, 0o700);
|
|
226
|
+
handle = await deps.open(tempPath, "wx", isPosix ? 0o600 : undefined);
|
|
227
|
+
await handle.writeFile(formatEpicServerRecord(validated), { encoding: "utf-8" });
|
|
228
|
+
await handle.sync();
|
|
229
|
+
await handle.close();
|
|
230
|
+
handle = undefined;
|
|
231
|
+
if (isPosix)
|
|
232
|
+
await deps.chmod(tempPath, 0o600);
|
|
233
|
+
await deps.rename(tempPath, resolved.path);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
if (handle) {
|
|
237
|
+
try {
|
|
238
|
+
await handle.close();
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
/* a close failure must not mask the write failure */
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (deps.unlink) {
|
|
245
|
+
try {
|
|
246
|
+
await deps.unlink(tempPath);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
/* best-effort temp cleanup; the previous record is untouched */
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return { ok: false, error: "the epic-server record could not be written" };
|
|
253
|
+
}
|
|
254
|
+
if (isPosix) {
|
|
255
|
+
try {
|
|
256
|
+
const dirHandle = await deps.open(dir, "r");
|
|
257
|
+
try {
|
|
258
|
+
await dirHandle.sync();
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
await dirHandle.close();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
/* best-effort: the file itself was fsynced and renamed */
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return { ok: true, path: resolved.path };
|
|
269
|
+
}
|
|
270
|
+
/** True when two records select the same configuration (the setup item's convergence test). */
|
|
271
|
+
export function sameEpicServerRecord(a, b) {
|
|
272
|
+
return formatEpicServerRecord(a) === formatEpicServerRecord(b);
|
|
273
|
+
}
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* No MCP tool is registered from this module — the `tools/list` token budget is
|
|
29
29
|
* effectively frozen.
|
|
30
30
|
*/
|
|
31
|
-
import { buildConductorJiraUrl, buildConductorVcsUrl, fetchConductorJsonPostWithTimeout, fetchConductorJsonPutWithTimeout, fetchConductorJsonWithTimeout, fetchConductorReadiness, fetchEffectiveSupervisorConfig, fetchEffectiveSupervisorSetup, fetchEpicRunState, fetchParseStatus, fetchPrReviewStatus, pollCiChecksForCommit, ConductorBridgeApiError, CONDUCTOR_FETCH_TIMEOUT_MS, } from "../conductor/bridge-api-client.js";
|
|
31
|
+
import { acquireConductorReadiness, buildConductorJiraUrl, buildConductorVcsUrl, fetchConductorJsonPostWithTimeout, fetchConductorJsonPutWithTimeout, fetchConductorJsonWithTimeout, fetchConductorReadiness, fetchEffectiveSupervisorConfig, fetchEffectiveSupervisorSetup, fetchEpicRunState, fetchParseStatus, fetchPrReviewStatus, pollCiChecksForCommit, ConductorBridgeApiError, CONDUCTOR_FETCH_TIMEOUT_MS, } from "../conductor/bridge-api-client.js";
|
|
32
32
|
/**
|
|
33
33
|
* The one message returned for any failure whose cause cannot be described
|
|
34
34
|
* without risking the credential. Stable so callers can match on it.
|
|
@@ -142,8 +142,16 @@ export async function getEffectiveSupervisorSetup(access, fetchImpl = globalThis
|
|
|
142
142
|
* missing `required_checks_empty` becomes a failure here rather than an
|
|
143
143
|
* apparently-healthy report.
|
|
144
144
|
*/
|
|
145
|
-
export async function getConductorReadiness(access, fetchImpl = globalThis.fetch) {
|
|
146
|
-
|
|
145
|
+
export async function getConductorReadiness(access, fetchImpl = globalThis.fetch, options = {}) {
|
|
146
|
+
// BAPI-1593 — the shared acquisition policy: one attempt plus at most one retry.
|
|
147
|
+
// The last sanitized failure travels through `wrap`, so the non-throwing
|
|
148
|
+
// `BridgeResult` contract and its redaction are unchanged.
|
|
149
|
+
return wrap(access, async () => {
|
|
150
|
+
const acquired = await acquireConductorReadiness(() => fetchConductorReadiness(access, fetchImpl), options);
|
|
151
|
+
if (acquired.kind === "ready")
|
|
152
|
+
return acquired.readiness;
|
|
153
|
+
throw acquired.error;
|
|
154
|
+
});
|
|
147
155
|
}
|
|
148
156
|
/**
|
|
149
157
|
* `POST /jira/resolve-ci-checks` — discover and classify the repository's CI
|
|
@@ -132,3 +132,46 @@ export function buildEscalationReachabilityCheck(report, callerChannel) {
|
|
|
132
132
|
}
|
|
133
133
|
return warn(uncertain);
|
|
134
134
|
}
|
|
135
|
+
function emailTransportState(report) {
|
|
136
|
+
const diagnostics = report === null || report.unavailableSources?.includes("server") === true
|
|
137
|
+
? null
|
|
138
|
+
: report.diagnostics ?? null;
|
|
139
|
+
if (diagnostics === null)
|
|
140
|
+
return "unknown";
|
|
141
|
+
const observed = diagnostics.filter((entry) => entry.id === EMAIL_TRANSPORT_LIVE_DIAGNOSTIC_ID);
|
|
142
|
+
if (observed.length !== 1)
|
|
143
|
+
return "unknown";
|
|
144
|
+
const [observation] = observed;
|
|
145
|
+
if (isFailedEmailTransportDiagnostic(observation))
|
|
146
|
+
return "not_live";
|
|
147
|
+
return observation.severity === "advisory" &&
|
|
148
|
+
observation.applicability === "applicable" &&
|
|
149
|
+
observation.verdict === "passed"
|
|
150
|
+
? "live"
|
|
151
|
+
: "unknown";
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Classify what the reachability output may claim (BAPI-1595). Pure.
|
|
155
|
+
*
|
|
156
|
+
* The confirmed "no reachable channel" line needs BOTH facts established. An unknown
|
|
157
|
+
* webhook declaration (an unknown channel) or an unknown email transport yields
|
|
158
|
+
* `uncertain`, which is a warning that makes no "no channel" claim. A declared
|
|
159
|
+
* webhook that did not answer stays a warning (`webhook_warning`), including when
|
|
160
|
+
* email is live. Everything else is `omitted`.
|
|
161
|
+
*/
|
|
162
|
+
export function classifyEscalationReachabilityPresentation(report, callerChannel) {
|
|
163
|
+
const channel = resolveEffectiveEscalationChannel(report, callerChannel);
|
|
164
|
+
if (channel === "webhook") {
|
|
165
|
+
const last = report?.webhook_last_delivery;
|
|
166
|
+
return last === "failed" || last === "unknown" ? "webhook_warning" : "omitted";
|
|
167
|
+
}
|
|
168
|
+
if (channel === "unknown")
|
|
169
|
+
return "uncertain";
|
|
170
|
+
// No webhook is declared (the channel is email, or none). The email transport decides.
|
|
171
|
+
const transport = emailTransportState(report);
|
|
172
|
+
if (transport === "not_live")
|
|
173
|
+
return "confirmed_no_channel";
|
|
174
|
+
if (transport === "live")
|
|
175
|
+
return channel === "email" ? "omitted" : "uncertain";
|
|
176
|
+
return "uncertain";
|
|
177
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BAPI-1582 — bounded, resume-safe `ci_fix` test-run evidence (executor side).
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `src/python/epic_implementer/ci_fix_test_runs.py`: the same logical
|
|
5
|
+
* identity, closed vocabulary, and bounds. Keep the two in step.
|
|
6
|
+
*
|
|
7
|
+
* The worker records its runs in `<worktree>/.conductor/ci-fix-test-runs.json`.
|
|
8
|
+
* Before spawn the executor seeds that file with the dispatched logical job and
|
|
9
|
+
* any valid prior runs the server restored (`ci_fix_prior_test_runs`). After
|
|
10
|
+
* exit it reads the file back and forwards a valid record as the optional
|
|
11
|
+
* `ci_fix_test_runs` result field. Everything here is telemetry and fail-open:
|
|
12
|
+
* a missing, malformed, oversized, unsafe, or other-job record means "no prior
|
|
13
|
+
* runs", never a failed job.
|
|
14
|
+
*/
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { constants } from "node:fs";
|
|
17
|
+
import { lstat, mkdir, open, readFile, rename, unlink } from "node:fs/promises";
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
export const CI_FIX_TEST_RUNS_PATH = ".conductor/ci-fix-test-runs.json";
|
|
20
|
+
export const CI_FIX_TEST_RUNS_RESULT_KEY = "ci_fix_test_runs";
|
|
21
|
+
export const CI_FIX_PRIOR_TEST_RUNS_PAYLOAD_KEY = "ci_fix_prior_test_runs";
|
|
22
|
+
export const CI_FIX_LOGICAL_JOB_PAYLOAD_KEY = "ci_fix_logical_job";
|
|
23
|
+
export const CI_FIX_TEST_RUNS_VERSION = 1;
|
|
24
|
+
export const CI_FIX_TEST_RUNS_MAX_RUNS = 128;
|
|
25
|
+
export const CI_FIX_TEST_RUNS_MAX_SUITES = 64;
|
|
26
|
+
export const CI_FIX_TEST_RUNS_SUITE_MAX_CHARS = 500;
|
|
27
|
+
export const CI_FIX_LOGICAL_JOB_MAX_CHECKS = 32;
|
|
28
|
+
export const CI_FIX_LOGICAL_JOB_CHECK_MAX_BYTES = 200;
|
|
29
|
+
/** Serialized sub-budget, well below `RESULT_JSONB_MAX_BYTES` (64 000). */
|
|
30
|
+
export const CI_FIX_TEST_RUNS_MAX_BYTES = 24_000;
|
|
31
|
+
const SHA_RE = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
32
|
+
// C0/C1 controls and U+FEFF, rejected BEFORE trimming: JavaScript's `trim` and
|
|
33
|
+
// Python's `strip` disagree on exactly these, so neither side may strip them.
|
|
34
|
+
// eslint-disable-next-line no-control-regex
|
|
35
|
+
const CONTROL_RE = /[\x00-\x1f\x7f-\x9f\ufeff]/;
|
|
36
|
+
const KINDS = new Set(["targeted", "broad"]);
|
|
37
|
+
const OUTCOMES = new Set(["passed", "failed", "timed_out"]);
|
|
38
|
+
function isRecord(value) {
|
|
39
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
function isSha(value) {
|
|
42
|
+
return typeof value === "string" && SHA_RE.test(value);
|
|
43
|
+
}
|
|
44
|
+
/** Code-point order, matching Python's `sorted` over `str`. */
|
|
45
|
+
function compareCodePoints(a, b) {
|
|
46
|
+
const left = Array.from(a);
|
|
47
|
+
const right = Array.from(b);
|
|
48
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
49
|
+
const diff = left[i].codePointAt(0) - right[i].codePointAt(0);
|
|
50
|
+
if (diff !== 0)
|
|
51
|
+
return diff;
|
|
52
|
+
}
|
|
53
|
+
return left.length - right.length;
|
|
54
|
+
}
|
|
55
|
+
/** Distinct check names, stripped and sorted (the server's `_canonical_checks` rule). */
|
|
56
|
+
export function canonicalCheckSet(checks) {
|
|
57
|
+
const names = new Set();
|
|
58
|
+
for (const check of checks) {
|
|
59
|
+
if (typeof check === "string" && check.trim())
|
|
60
|
+
names.add(check.trim());
|
|
61
|
+
}
|
|
62
|
+
return [...names].sort(compareCodePoints);
|
|
63
|
+
}
|
|
64
|
+
/** The canonical logical job, or `undefined` when it cannot be established. */
|
|
65
|
+
export function ciFixLogicalJob(failingHeadSha, checks) {
|
|
66
|
+
if (!isSha(failingHeadSha) || !Array.isArray(checks))
|
|
67
|
+
return undefined;
|
|
68
|
+
if (checks.some((check) => typeof check !== "string" || CONTROL_RE.test(check)))
|
|
69
|
+
return undefined;
|
|
70
|
+
const canonical = canonicalCheckSet(checks);
|
|
71
|
+
if (canonical.length === 0 || canonical.length > CI_FIX_LOGICAL_JOB_MAX_CHECKS)
|
|
72
|
+
return undefined;
|
|
73
|
+
for (const name of canonical) {
|
|
74
|
+
if (Buffer.byteLength(name, "utf8") > CI_FIX_LOGICAL_JOB_CHECK_MAX_BYTES)
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return { failing_head_sha: failingHeadSha, checks: canonical };
|
|
78
|
+
}
|
|
79
|
+
export function sameLogicalJob(a, b) {
|
|
80
|
+
return (a.failing_head_sha === b.failing_head_sha &&
|
|
81
|
+
a.checks.length === b.checks.length &&
|
|
82
|
+
a.checks.every((name, index) => name === b.checks[index]));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The logical job this `ci_fix` dispatch repairs. A continuation carries the
|
|
86
|
+
* server's `ci_fix_logical_job` (the original anchor and ORIGINAL check set);
|
|
87
|
+
* it is used when it names the same anchor. Otherwise the job is the anchor
|
|
88
|
+
* (`failing_head_sha`, else the bound head) plus the dispatched set.
|
|
89
|
+
*/
|
|
90
|
+
export function dispatchedLogicalJob(payload, ciFix) {
|
|
91
|
+
const anchor = ciFix.failingHeadSha ?? ciFix.headSha;
|
|
92
|
+
if (isRecord(payload) && Object.prototype.hasOwnProperty.call(payload, CI_FIX_LOGICAL_JOB_PAYLOAD_KEY)) {
|
|
93
|
+
const stamped = payload[CI_FIX_LOGICAL_JOB_PAYLOAD_KEY];
|
|
94
|
+
if (isRecord(stamped) && stamped.failing_head_sha === anchor) {
|
|
95
|
+
const job = ciFixLogicalJob(anchor, stamped.checks);
|
|
96
|
+
if (job)
|
|
97
|
+
return job;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return ciFixLogicalJob(anchor, ciFix.requiredClaimChecks);
|
|
101
|
+
}
|
|
102
|
+
function parseRun(value) {
|
|
103
|
+
if (!isRecord(value))
|
|
104
|
+
return undefined;
|
|
105
|
+
const { suite, kind, head_sha: headSha, outcome } = value;
|
|
106
|
+
if (typeof suite !== "string" ||
|
|
107
|
+
CONTROL_RE.test(suite) ||
|
|
108
|
+
!suite.trim()) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
if (typeof kind !== "string" || !KINDS.has(kind))
|
|
112
|
+
return undefined;
|
|
113
|
+
if (typeof outcome !== "string" || !OUTCOMES.has(outcome))
|
|
114
|
+
return undefined;
|
|
115
|
+
if (!isSha(headSha))
|
|
116
|
+
return undefined;
|
|
117
|
+
return { suite, kind: kind, head_sha: headSha, outcome: outcome };
|
|
118
|
+
}
|
|
119
|
+
export function serializedSize(value) {
|
|
120
|
+
return Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
121
|
+
}
|
|
122
|
+
/** Python measures `len(suite)` in code points; mirror that, not UTF-16 units. */
|
|
123
|
+
function withinSuiteBound(runs) {
|
|
124
|
+
return runs.every((run) => Array.from(run.suite).length <= CI_FIX_TEST_RUNS_SUITE_MAX_CHARS);
|
|
125
|
+
}
|
|
126
|
+
function bounded(evidence) {
|
|
127
|
+
return (evidence.runs.length <= CI_FIX_TEST_RUNS_MAX_RUNS &&
|
|
128
|
+
new Set(evidence.runs.map((run) => run.suite)).size <= CI_FIX_TEST_RUNS_MAX_SUITES &&
|
|
129
|
+
serializedSize(evidence) <= CI_FIX_TEST_RUNS_MAX_BYTES);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Normalize one version-1 record, or return `undefined`. Never throws. Only
|
|
133
|
+
* declared fields are kept; any invalid part makes the whole record absent.
|
|
134
|
+
*/
|
|
135
|
+
export function parseCiFixTestRuns(value, expected) {
|
|
136
|
+
try {
|
|
137
|
+
if (!isRecord(value))
|
|
138
|
+
return undefined;
|
|
139
|
+
if (value.version !== CI_FIX_TEST_RUNS_VERSION)
|
|
140
|
+
return undefined;
|
|
141
|
+
const logical = value.logical_job;
|
|
142
|
+
if (!isRecord(logical))
|
|
143
|
+
return undefined;
|
|
144
|
+
const job = ciFixLogicalJob(logical.failing_head_sha, logical.checks);
|
|
145
|
+
if (!job || (expected && !sameLogicalJob(job, expected)))
|
|
146
|
+
return undefined;
|
|
147
|
+
const runsValue = value.runs;
|
|
148
|
+
if (!Array.isArray(runsValue) || runsValue.length > CI_FIX_TEST_RUNS_MAX_RUNS)
|
|
149
|
+
return undefined;
|
|
150
|
+
const runs = [];
|
|
151
|
+
for (const item of runsValue) {
|
|
152
|
+
const run = parseRun(item);
|
|
153
|
+
if (!run)
|
|
154
|
+
return undefined;
|
|
155
|
+
runs.push(run);
|
|
156
|
+
}
|
|
157
|
+
if (!withinSuiteBound(runs))
|
|
158
|
+
return undefined;
|
|
159
|
+
const evidence = { version: 1, logical_job: job, runs };
|
|
160
|
+
return bounded(evidence) ? evidence : undefined;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function runKey(run) {
|
|
167
|
+
return JSON.stringify([run.suite, run.kind, run.head_sha, run.outcome]);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Merge two records of the SAME logical job conservatively. Exact duplicate
|
|
171
|
+
* observations collapse; distinct ones are kept in order (`server` first). A
|
|
172
|
+
* `passed` observation that conflicts with a failed or timed-out one for the
|
|
173
|
+
* same suite, kind, and head is dropped, so a merge never manufactures a
|
|
174
|
+
* reusable pass. When the merge exceeds the bounds, the oldest runs go first.
|
|
175
|
+
*/
|
|
176
|
+
export function mergeCiFixTestRuns(server, local) {
|
|
177
|
+
const seen = new Set();
|
|
178
|
+
const combined = [];
|
|
179
|
+
for (const run of [...server.runs, ...local.runs]) {
|
|
180
|
+
const key = runKey(run);
|
|
181
|
+
if (seen.has(key))
|
|
182
|
+
continue;
|
|
183
|
+
seen.add(key);
|
|
184
|
+
combined.push(run);
|
|
185
|
+
}
|
|
186
|
+
const notPassed = new Set(combined
|
|
187
|
+
.filter((run) => run.outcome !== "passed")
|
|
188
|
+
.map((run) => JSON.stringify([run.suite, run.kind, run.head_sha])));
|
|
189
|
+
let runs = combined.filter((run) => run.outcome !== "passed" || !notPassed.has(JSON.stringify([run.suite, run.kind, run.head_sha])));
|
|
190
|
+
let merged = { version: 1, logical_job: server.logical_job, runs };
|
|
191
|
+
while (runs.length > 0 && !bounded(merged)) {
|
|
192
|
+
runs = runs.slice(1);
|
|
193
|
+
merged = { version: 1, logical_job: server.logical_job, runs };
|
|
194
|
+
}
|
|
195
|
+
return merged;
|
|
196
|
+
}
|
|
197
|
+
export const realCiFixTestRunsFs = {
|
|
198
|
+
lstat: async (p) => {
|
|
199
|
+
try {
|
|
200
|
+
const stat = await lstat(p);
|
|
201
|
+
return { isDirectory: stat.isDirectory(), isSymbolicLink: stat.isSymbolicLink() };
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
if (errorCode(error) === "ENOENT")
|
|
205
|
+
return null;
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
mkdir: (p) => mkdir(p, { recursive: true }),
|
|
210
|
+
readFile: (p) => readFile(p, "utf8"),
|
|
211
|
+
writeExclusive: async (p, data) => {
|
|
212
|
+
const handle = await open(p, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
213
|
+
try {
|
|
214
|
+
await handle.writeFile(data, "utf8");
|
|
215
|
+
await handle.sync();
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
await handle.close();
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
rename: (from, to) => rename(from, to),
|
|
222
|
+
unlink: (p) => unlink(p),
|
|
223
|
+
};
|
|
224
|
+
/** The seams from executor deps, or `undefined` when a required one is missing. */
|
|
225
|
+
export function ciFixTestRunsFsFromDeps(deps) {
|
|
226
|
+
const { lstatPath, writeFileExclusive, rename: move, removeFile } = deps;
|
|
227
|
+
if (!lstatPath || !writeFileExclusive || !move || !removeFile)
|
|
228
|
+
return undefined;
|
|
229
|
+
return {
|
|
230
|
+
lstat: lstatPath,
|
|
231
|
+
mkdir: (p) => deps.mkdir(p, { recursive: true }),
|
|
232
|
+
readFile: (p) => deps.readFile(p),
|
|
233
|
+
writeExclusive: writeFileExclusive,
|
|
234
|
+
rename: move,
|
|
235
|
+
unlink: removeFile,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function errorCode(error) {
|
|
239
|
+
return error && typeof error === "object" ? error.code : undefined;
|
|
240
|
+
}
|
|
241
|
+
/** A safe `.conductor` directory: a real directory, never a link. */
|
|
242
|
+
async function safeDirectory(fs, directory) {
|
|
243
|
+
const meta = await fs.lstat(directory);
|
|
244
|
+
return meta !== null && meta.isDirectory && !meta.isSymbolicLink;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Seed `<worktree>/.conductor/ci-fix-test-runs.json` before a `ci_fix` spawn.
|
|
248
|
+
*
|
|
249
|
+
* The written record is the valid server record merged with a valid same-job
|
|
250
|
+
* local one, else whichever of the two is valid, else a fresh record with no
|
|
251
|
+
* runs. A malformed or other-job local file is replaced. A symlinked or
|
|
252
|
+
* non-regular `.conductor` directory or leaf is refused, and the record is
|
|
253
|
+
* published through an exclusive temporary file and a rename. Never throws:
|
|
254
|
+
* every failure is a `skipped` result with a fixed, secret-free code.
|
|
255
|
+
*/
|
|
256
|
+
export async function seedCiFixTestRunsFile(worktreePath, input, fs = realCiFixTestRunsFs) {
|
|
257
|
+
const job = input.logicalJob;
|
|
258
|
+
if (!job)
|
|
259
|
+
return { status: "skipped", code: "logical_job_unavailable" };
|
|
260
|
+
let temporary;
|
|
261
|
+
try {
|
|
262
|
+
const directory = path.join(worktreePath, ".conductor");
|
|
263
|
+
const existing = await fs.lstat(directory);
|
|
264
|
+
if (existing === null)
|
|
265
|
+
await fs.mkdir(directory);
|
|
266
|
+
if (!(await safeDirectory(fs, directory)))
|
|
267
|
+
return { status: "skipped", code: "directory_unsafe" };
|
|
268
|
+
const destination = path.join(worktreePath, CI_FIX_TEST_RUNS_PATH);
|
|
269
|
+
const leaf = await fs.lstat(destination);
|
|
270
|
+
if (leaf !== null && (leaf.isDirectory || leaf.isSymbolicLink)) {
|
|
271
|
+
return { status: "skipped", code: "destination_unsafe" };
|
|
272
|
+
}
|
|
273
|
+
let local;
|
|
274
|
+
if (leaf !== null) {
|
|
275
|
+
try {
|
|
276
|
+
const raw = await fs.readFile(destination);
|
|
277
|
+
local = Buffer.byteLength(raw, "utf8") <= CI_FIX_TEST_RUNS_MAX_BYTES * 4
|
|
278
|
+
? parseCiFixTestRuns(JSON.parse(raw), job)
|
|
279
|
+
: undefined;
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
local = undefined;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const server = parseCiFixTestRuns(input.prior, job);
|
|
286
|
+
let record;
|
|
287
|
+
let source;
|
|
288
|
+
if (server && local) {
|
|
289
|
+
record = mergeCiFixTestRuns(server, local);
|
|
290
|
+
source = "merged";
|
|
291
|
+
}
|
|
292
|
+
else if (server) {
|
|
293
|
+
record = server;
|
|
294
|
+
source = "server";
|
|
295
|
+
}
|
|
296
|
+
else if (local) {
|
|
297
|
+
record = local;
|
|
298
|
+
source = "local";
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
record = { version: 1, logical_job: job, runs: [] };
|
|
302
|
+
source = "fresh";
|
|
303
|
+
}
|
|
304
|
+
temporary = path.join(directory, `.ci-fix-test-runs-${randomUUID()}.tmp`);
|
|
305
|
+
await fs.writeExclusive(temporary, JSON.stringify(record) + "\n");
|
|
306
|
+
// Re-check right before publishing: the directory and leaf may have moved.
|
|
307
|
+
if (!(await safeDirectory(fs, directory)))
|
|
308
|
+
return { status: "skipped", code: "directory_changed" };
|
|
309
|
+
const current = await fs.lstat(destination);
|
|
310
|
+
if (current !== null && (current.isDirectory || current.isSymbolicLink)) {
|
|
311
|
+
return { status: "skipped", code: "destination_unsafe" };
|
|
312
|
+
}
|
|
313
|
+
await fs.rename(temporary, destination);
|
|
314
|
+
temporary = undefined;
|
|
315
|
+
return { status: "written", source };
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
return { status: "skipped", code: "write_failed" };
|
|
319
|
+
}
|
|
320
|
+
finally {
|
|
321
|
+
if (temporary !== undefined)
|
|
322
|
+
await fs.unlink(temporary).catch(() => undefined);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Add a valid record to a result envelope, or return the envelope unchanged
|
|
327
|
+
* when the record is absent or the augmented envelope would exceed `maxBytes`.
|
|
328
|
+
*/
|
|
329
|
+
export function withCiFixTestRuns(result, evidence, maxBytes) {
|
|
330
|
+
if (!evidence)
|
|
331
|
+
return result;
|
|
332
|
+
const augmented = { ...result, [CI_FIX_TEST_RUNS_RESULT_KEY]: evidence };
|
|
333
|
+
return serializedSize(augmented) <= maxBytes ? augmented : result;
|
|
334
|
+
}
|