@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,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BAPI-1621: the input collection and publication around the pure builder in
|
|
3
|
+
* `plan-generation.ts`.
|
|
4
|
+
*
|
|
5
|
+
* `drive-epic` calls into this module ONLY after discovery returned
|
|
6
|
+
* `plan_not_found` with no `--plan-file`, and only when no non-staging entry was
|
|
7
|
+
* skipped. A found, supplied, or ambiguous plan never reaches it, so a launch with
|
|
8
|
+
* a plan present makes none of these reads.
|
|
9
|
+
*
|
|
10
|
+
* Collection reads through the GENERAL Bridge resolver — the same path
|
|
11
|
+
* `prerequisite-inputs` uses since BAPI-1617 — because it runs before any local
|
|
12
|
+
* plane is up. It reads, in order: the epic's live run (only a successful "no
|
|
13
|
+
* run" answer continues), the epic's complete child list, the epic and child
|
|
14
|
+
* descriptions, and the tracked-file inventory (`git ls-files -z`, once, as a
|
|
15
|
+
* list-form subprocess with no shell and no ticket-derived argument). Every
|
|
16
|
+
* failure maps to one fixed reason from `PLAN_GENERATION_FAILURE_REASONS`; no raw
|
|
17
|
+
* error text, ticket text, or credential leaves this module.
|
|
18
|
+
*
|
|
19
|
+
* Publication (real launches only — a preview never calls it) stages the DAG and
|
|
20
|
+
* then the metadata in a random `.staging-…` folder beside the destination,
|
|
21
|
+
* validates the plan with the server's read-only validate lane, and publishes by
|
|
22
|
+
* ONE plain `rename` of the folder to `<epic-key-lowercase>-generated/`. The rename
|
|
23
|
+
* refuses an existing non-empty folder, which is how two concurrent launches end
|
|
24
|
+
* with one plan: the loser removes only its own staging folder and runs discovery
|
|
25
|
+
* again. A failure before the rename removes the staging folder; nothing after the
|
|
26
|
+
* rename is undone.
|
|
27
|
+
*/
|
|
28
|
+
import { execFile } from "node:child_process";
|
|
29
|
+
import { randomBytes } from "node:crypto";
|
|
30
|
+
import { mkdir, readFile, rename, rm, unlink, writeFile, realpath } from "node:fs/promises";
|
|
31
|
+
import path from "node:path";
|
|
32
|
+
import { writeJsonAtomically } from "./conductor-bundle-artifacts.js";
|
|
33
|
+
import { GENERATED_PLAN_VERSION, PLAN_GENERATED_BY, PLAN_GENERATOR_VERSION, buildGeneratedEpicPlan, compareTicketKeys, } from "./plan-generation.js";
|
|
34
|
+
/**
|
|
35
|
+
* Collect the inputs, build the plan, and validate it with the server. Writes
|
|
36
|
+
* nothing. Only a successful "no live run" answer reaches the child list.
|
|
37
|
+
*/
|
|
38
|
+
export async function attemptPlanGeneration(epicKey, deps) {
|
|
39
|
+
const failed = (reason) => ({ kind: "failed", reason });
|
|
40
|
+
let access;
|
|
41
|
+
try {
|
|
42
|
+
const resolved = await deps.resolveAccess();
|
|
43
|
+
if (!resolved.ok)
|
|
44
|
+
return failed("access_unavailable");
|
|
45
|
+
access = resolved.access;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return failed("access_unavailable");
|
|
49
|
+
}
|
|
50
|
+
let live;
|
|
51
|
+
try {
|
|
52
|
+
live = await deps.readLiveRun(access, epicKey);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
live = "unknown";
|
|
56
|
+
}
|
|
57
|
+
if (live === "live" || live === "conflict")
|
|
58
|
+
return { kind: "not_attempted", liveRun: live };
|
|
59
|
+
if (live !== "none")
|
|
60
|
+
return failed("live_run_unknown");
|
|
61
|
+
let enumeration;
|
|
62
|
+
try {
|
|
63
|
+
enumeration = await deps.fetchChildren(access, epicKey);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return failed("children_unreadable");
|
|
67
|
+
}
|
|
68
|
+
if (!enumeration.enumeration_complete)
|
|
69
|
+
return failed("enumeration_incomplete");
|
|
70
|
+
let epicDescription;
|
|
71
|
+
try {
|
|
72
|
+
epicDescription = (await deps.fetchTicket(access, epicKey)).description ?? "";
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return failed("description_unreadable");
|
|
76
|
+
}
|
|
77
|
+
// Descriptions are read one at a time, in key order, so the read count and
|
|
78
|
+
// order are deterministic. A failed read is never an empty description, but a
|
|
79
|
+
// blank ticket is. Only children the builder may plan are read: a done child
|
|
80
|
+
// or a spike contributes no text, so its body can never stop generation.
|
|
81
|
+
const childDescriptions = new Map();
|
|
82
|
+
const byKey = [...enumeration.children].sort((a, b) => compareTicketKeys(a.ticket_key, b.ticket_key));
|
|
83
|
+
for (const child of byKey) {
|
|
84
|
+
if (!mayBePlanned(child))
|
|
85
|
+
continue;
|
|
86
|
+
try {
|
|
87
|
+
childDescriptions.set(child.ticket_key, (await deps.fetchTicket(access, child.ticket_key)).description ?? "");
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return failed("description_unreadable");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let trackedFiles;
|
|
94
|
+
try {
|
|
95
|
+
trackedFiles = await deps.listTrackedFiles();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
trackedFiles = null;
|
|
99
|
+
}
|
|
100
|
+
if (trackedFiles === null)
|
|
101
|
+
return failed("inventory_unavailable");
|
|
102
|
+
const built = buildGeneratedEpicPlan({
|
|
103
|
+
epicKey,
|
|
104
|
+
enumerationComplete: enumeration.enumeration_complete,
|
|
105
|
+
children: enumeration.children,
|
|
106
|
+
epicDescription,
|
|
107
|
+
childDescriptions,
|
|
108
|
+
trackedFiles,
|
|
109
|
+
});
|
|
110
|
+
if (!built.ok)
|
|
111
|
+
return failed(built.reason);
|
|
112
|
+
const verdict = await validateGeneratedPlan(access, epicKey, built.dag, deps.validatePlan);
|
|
113
|
+
if (verdict !== "valid")
|
|
114
|
+
return failed(verdict);
|
|
115
|
+
return { kind: "generated", dag: built.dag, summary: built.summary, access };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Whether the builder could make this child a `planned` node. Mirrors the
|
|
119
|
+
* builder's classification (done first, then spikes) so only those descriptions
|
|
120
|
+
* are read. Malformed metadata still counts as "may be planned": the builder
|
|
121
|
+
* rejects it with `invalid_child_metadata` either way.
|
|
122
|
+
*/
|
|
123
|
+
function mayBePlanned(child) {
|
|
124
|
+
if (child.status_category === "done")
|
|
125
|
+
return false;
|
|
126
|
+
return (child.issue_type ?? "").trim().toLowerCase() !== "spike";
|
|
127
|
+
}
|
|
128
|
+
/** The server's read-only validate lane, reduced to a fixed verdict. */
|
|
129
|
+
export async function validateGeneratedPlan(access, epicKey, dag, validatePlan) {
|
|
130
|
+
try {
|
|
131
|
+
const result = await validatePlan(access, {
|
|
132
|
+
planVersion: GENERATED_PLAN_VERSION,
|
|
133
|
+
planBlob: dag,
|
|
134
|
+
epicKey,
|
|
135
|
+
});
|
|
136
|
+
return result.valid ? "valid" : "validation_rejected";
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Unreachable, an older server (404), or a malformed answer: never "valid".
|
|
140
|
+
return "validation_unavailable";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// Tracked-file inventory
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
const GIT_TIMEOUT_MS = 15_000;
|
|
147
|
+
const GIT_MAX_BUFFER = 64 * 1024 * 1024;
|
|
148
|
+
function runGit(args, cwd) {
|
|
149
|
+
return new Promise((resolve) => {
|
|
150
|
+
// List form, no shell: the arguments are fixed literals, never ticket text.
|
|
151
|
+
execFile("git", [...args], { cwd, timeout: GIT_TIMEOUT_MS, maxBuffer: GIT_MAX_BUFFER, encoding: "utf8" }, (err, stdout) => {
|
|
152
|
+
resolve(err ? null : stdout);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/** `git ls-files -z` at the repository root that holds `cwd`; null on any failure. */
|
|
157
|
+
export async function listTrackedFilesWithGit(cwd) {
|
|
158
|
+
const top = await runGit(["rev-parse", "--show-toplevel"], cwd);
|
|
159
|
+
const root = top?.trim();
|
|
160
|
+
if (!root || !path.isAbsolute(root))
|
|
161
|
+
return null;
|
|
162
|
+
const listed = await runGit(["ls-files", "-z"], root);
|
|
163
|
+
if (listed === null)
|
|
164
|
+
return null;
|
|
165
|
+
return listed.split("\0").filter((entry) => entry !== "");
|
|
166
|
+
}
|
|
167
|
+
export function createDefaultPlanPublicationFs() {
|
|
168
|
+
return {
|
|
169
|
+
readFile: (filePath) => readFile(filePath, "utf8"),
|
|
170
|
+
writeFile: (filePath, data) => writeFile(filePath, data, "utf8"),
|
|
171
|
+
rename: (from, to) => rename(from, to),
|
|
172
|
+
unlink: (filePath) => unlink(filePath),
|
|
173
|
+
realpath: (filePath) => realpath(filePath),
|
|
174
|
+
mkdir: (dirPath, options) => mkdir(dirPath, options),
|
|
175
|
+
removeTree: (dirPath) => rm(dirPath, { recursive: true, force: true }),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** The one folder a generated plan for `epicKey` is published to. */
|
|
179
|
+
export function generatedPlanFolder(docsDir, epicKey) {
|
|
180
|
+
return path.join(path.resolve(docsDir, "epic-plans"), `${epicKey.toLowerCase()}-generated`);
|
|
181
|
+
}
|
|
182
|
+
/** The metadata a generated plan carries beside its DAG. */
|
|
183
|
+
export function generatedPlanMetadata(epicKey, summary) {
|
|
184
|
+
return {
|
|
185
|
+
epic_key: epicKey,
|
|
186
|
+
generated_by: PLAN_GENERATED_BY,
|
|
187
|
+
generator_version: PLAN_GENERATOR_VERSION,
|
|
188
|
+
summary,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
const COLLISION_CODES = new Set(["EEXIST", "ENOTEMPTY", "EPERM"]);
|
|
192
|
+
function errorCode(err) {
|
|
193
|
+
const code = err?.code;
|
|
194
|
+
return typeof code === "string" ? code : undefined;
|
|
195
|
+
}
|
|
196
|
+
/** Stage, validate, and publish one generated plan by a single folder rename. */
|
|
197
|
+
export async function publishGeneratedPlan(input) {
|
|
198
|
+
const { fs } = input;
|
|
199
|
+
const plansDir = path.resolve(input.docsDir, "epic-plans");
|
|
200
|
+
const finalDir = generatedPlanFolder(input.docsDir, input.epicKey);
|
|
201
|
+
const suffix = input.randomSuffix?.() ?? randomBytes(8).toString("hex");
|
|
202
|
+
const stagingDir = path.join(plansDir, `.staging-${suffix}`);
|
|
203
|
+
const cleanup = async () => {
|
|
204
|
+
try {
|
|
205
|
+
await fs.removeTree(stagingDir);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Best effort: discovery never counts a dot-folder, so a leftover is inert.
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
try {
|
|
212
|
+
await fs.mkdir(plansDir, { recursive: true });
|
|
213
|
+
// Not recursive: the staging folder must be new and ours alone.
|
|
214
|
+
await fs.mkdir(stagingDir, { recursive: false });
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return { kind: "failed", reason: "publish_failed" };
|
|
218
|
+
}
|
|
219
|
+
// DAG first, metadata last: a folder with no metadata is never a candidate.
|
|
220
|
+
const dagWrite = await writeJsonAtomically(path.join(stagingDir, "epic-plan.dag.json"), input.dag, fs);
|
|
221
|
+
const metaWrite = dagWrite.ok
|
|
222
|
+
? await writeJsonAtomically(path.join(stagingDir, "epic-plan.meta.json"), generatedPlanMetadata(input.epicKey, input.summary), fs)
|
|
223
|
+
: dagWrite;
|
|
224
|
+
if (!metaWrite.ok) {
|
|
225
|
+
await cleanup();
|
|
226
|
+
return { kind: "failed", reason: "publish_failed" };
|
|
227
|
+
}
|
|
228
|
+
let verdict;
|
|
229
|
+
try {
|
|
230
|
+
verdict = await input.validate();
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
verdict = "validation_unavailable";
|
|
234
|
+
}
|
|
235
|
+
if (verdict !== "valid") {
|
|
236
|
+
await cleanup();
|
|
237
|
+
return { kind: "failed", reason: verdict };
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
// No destination precheck: the rename itself is the exclusive claim.
|
|
241
|
+
await fs.rename(stagingDir, finalDir);
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
await cleanup();
|
|
245
|
+
if (!COLLISION_CODES.has(errorCode(err) ?? ""))
|
|
246
|
+
return { kind: "failed", reason: "publish_failed" };
|
|
247
|
+
let discovery;
|
|
248
|
+
try {
|
|
249
|
+
discovery = await input.rediscover();
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
return { kind: "failed", reason: "collision_unresolved" };
|
|
253
|
+
}
|
|
254
|
+
// A winner's plan is found (use it) or ambiguous (today's stop). "Not found"
|
|
255
|
+
// after a collision means the destination holds something that is not a plan:
|
|
256
|
+
// stop rather than try a second name.
|
|
257
|
+
if (discovery.status === "plan_not_found")
|
|
258
|
+
return { kind: "failed", reason: "collision_unresolved" };
|
|
259
|
+
return { kind: "collision", discovery };
|
|
260
|
+
}
|
|
261
|
+
return { kind: "published", dagPath: path.join(finalDir, "epic-plan.dag.json") };
|
|
262
|
+
}
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BAPI-1621: build a missing epic plan DAG from stored ticket data.
|
|
3
|
+
*
|
|
4
|
+
* `drive-epic` needs `epic-plan.dag.json` for every new run. When discovery finds
|
|
5
|
+
* none, setup can build one here instead of stopping. This module is the ONE owner
|
|
6
|
+
* of that policy, and it is pure: every input is injected, and nothing here reads
|
|
7
|
+
* a file, runs a process, opens a socket, reads the environment, or calls a model.
|
|
8
|
+
* `plan-generation-io.ts` collects the inputs and publishes the result.
|
|
9
|
+
*
|
|
10
|
+
* The rules, in the order they apply:
|
|
11
|
+
*
|
|
12
|
+
* - **Nodes come only from the complete child list.** Ticket text never adds a
|
|
13
|
+
* node, changes a status, or names a child. An incomplete list, a repeated or
|
|
14
|
+
* malformed key, or a child with no usable status category or type fails the
|
|
15
|
+
* whole build with a fixed reason.
|
|
16
|
+
* - **Done first, then spikes.** A child whose backend category is `done` is a
|
|
17
|
+
* `done` node whatever its type. An unfinished Spike is left out with the fixed
|
|
18
|
+
* reason `unfinished_spike`. No planned child left means `no_runnable_children`.
|
|
19
|
+
* - **Order is all-or-nothing.** The epic's "Ordered child manifest" section gives
|
|
20
|
+
* the order and edges only when every part of it is usable. Otherwise the
|
|
21
|
+
* children run as one chain in ascending key order.
|
|
22
|
+
* - **Uncertain files never allow parallel work.** File citations in each child's
|
|
23
|
+
* description are kept only when they are safe, repository-relative, and match
|
|
24
|
+
* the tracked-file inventory. If any planned child has no kept file, every
|
|
25
|
+
* planned child runs one at a time, in the selected order, and no node carries
|
|
26
|
+
* `touched_files` (an omitted field is the server's opt-out; `[]` would claim
|
|
27
|
+
* "touches nothing", which is not known).
|
|
28
|
+
*/
|
|
29
|
+
import { EPIC_KEY_PATTERN } from "./drive-epic-key.js";
|
|
30
|
+
/** The generation policy's version. Bump it when any rule here changes. */
|
|
31
|
+
export const PLAN_GENERATOR_VERSION = 1;
|
|
32
|
+
/** The `generated_by` value a generated plan's metadata carries. */
|
|
33
|
+
export const PLAN_GENERATED_BY = "drive-epic";
|
|
34
|
+
/** The DAG `plan_version` every generated plan uses. */
|
|
35
|
+
export const GENERATED_PLAN_VERSION = 1;
|
|
36
|
+
/** Every reason a generation attempt can stop. Fixed vocabulary, never raw text. */
|
|
37
|
+
export const PLAN_GENERATION_FAILURE_REASONS = [
|
|
38
|
+
"enumeration_incomplete",
|
|
39
|
+
"invalid_child_metadata",
|
|
40
|
+
"description_unreadable",
|
|
41
|
+
"no_runnable_children",
|
|
42
|
+
"children_unreadable",
|
|
43
|
+
"inventory_unavailable",
|
|
44
|
+
"access_unavailable",
|
|
45
|
+
"live_run_unknown",
|
|
46
|
+
"discovery_blocked",
|
|
47
|
+
"validation_rejected",
|
|
48
|
+
"validation_unavailable",
|
|
49
|
+
"publish_failed",
|
|
50
|
+
"collision_unresolved",
|
|
51
|
+
];
|
|
52
|
+
/** One fixed, plain sentence per failure reason, for stop text and previews. */
|
|
53
|
+
export const PLAN_GENERATION_FAILURE_TEXT = {
|
|
54
|
+
enumeration_incomplete: "the epic's child list could not be read completely",
|
|
55
|
+
invalid_child_metadata: "a child has a malformed key, or no usable status category or type",
|
|
56
|
+
description_unreadable: "a ticket description could not be read",
|
|
57
|
+
no_runnable_children: "the epic has no unfinished child the Epic Implementer can run",
|
|
58
|
+
children_unreadable: "the epic's child list could not be read",
|
|
59
|
+
inventory_unavailable: "the repository's tracked-file list could not be read",
|
|
60
|
+
access_unavailable: "Bridge API access could not be resolved for the ticket reads",
|
|
61
|
+
live_run_unknown: "whether the epic already has a live run could not be confirmed",
|
|
62
|
+
discovery_blocked: "plan discovery skipped a folder that might hold this epic's plan",
|
|
63
|
+
validation_rejected: "the server's plan validation rejected the generated plan",
|
|
64
|
+
validation_unavailable: "the server's plan validation could not be reached",
|
|
65
|
+
publish_failed: "the generated plan could not be written",
|
|
66
|
+
collision_unresolved: "the generated plan's folder is already taken by something that is not a plan",
|
|
67
|
+
};
|
|
68
|
+
/** Why a planned child was left out of the plan. */
|
|
69
|
+
export const PLAN_GENERATION_EXCLUSION_REASONS = ["unfinished_spike"];
|
|
70
|
+
/** Where the order came from. */
|
|
71
|
+
export const PLAN_GENERATION_ORDER_SOURCES = ["epic_manifest", "key_order"];
|
|
72
|
+
/** Why the plan runs one child at a time, when it does. */
|
|
73
|
+
export const PLAN_GENERATION_SERIALIZED_REASONS = ["unknown_touched_files", "key_order_fallback"];
|
|
74
|
+
/** The backend status categories the builder recognizes. */
|
|
75
|
+
export const RECOGNIZED_STATUS_CATEGORIES = ["new", "indeterminate", "done"];
|
|
76
|
+
/** Ticket keys in stored text. */
|
|
77
|
+
const TICKET_KEY_IN_TEXT = /\b[A-Z][A-Z0-9_]{0,19}-[0-9]+\b/g;
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Key ordering
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
/**
|
|
82
|
+
* Ascending, locale-independent key order: project prefix by code unit, then the
|
|
83
|
+
* number numerically, so `BAPI-9` sorts before `BAPI-10`.
|
|
84
|
+
*/
|
|
85
|
+
export function compareTicketKeys(a, b) {
|
|
86
|
+
const split = (key) => {
|
|
87
|
+
const dash = key.lastIndexOf("-");
|
|
88
|
+
return [key.slice(0, dash), Number(key.slice(dash + 1))];
|
|
89
|
+
};
|
|
90
|
+
const [pa, na] = split(a);
|
|
91
|
+
const [pb, nb] = split(b);
|
|
92
|
+
if (pa !== pb)
|
|
93
|
+
return pa < pb ? -1 : 1;
|
|
94
|
+
return na - nb;
|
|
95
|
+
}
|
|
96
|
+
const MD_MANIFEST_HEADING = /^\s{0,3}##\s+ordered child manifest\b/i;
|
|
97
|
+
const JIRA_MANIFEST_HEADING = /^\s*h2\.\s+ordered child manifest\b/i;
|
|
98
|
+
const MD_SECTION_END = /^\s{0,3}#{1,2}\s/;
|
|
99
|
+
const JIRA_SECTION_END = /^\s*h[12]\.\s/;
|
|
100
|
+
const MD_ITEM = /^\s{0,3}(?:[-*+]|\d{1,3}[.)])\s+(.*)$/;
|
|
101
|
+
const JIRA_ITEM = /^(?:#|\*|-)\s+(.*)$/;
|
|
102
|
+
const DEPENDS_ON = /[`*_]*depends_on[`*_]*\s*:\s*/gi;
|
|
103
|
+
const DEPENDS_ON_VALUE = /^[`*_]*\s*(none|[A-Z][A-Z0-9_]{0,19}-[0-9]+(?:\s*,\s*[A-Z][A-Z0-9_]{0,19}-[0-9]+)*)\s*[`*_]*\s*[.;]?\s*[`*_]*\s*$/i;
|
|
104
|
+
/**
|
|
105
|
+
* Parse the epic's "Ordered child manifest" section, or return null when there is
|
|
106
|
+
* no single, well-formed section. Membership and edge checks happen later, against
|
|
107
|
+
* the child list. Ticket text is data: nothing here is executed or followed.
|
|
108
|
+
*/
|
|
109
|
+
export function parseOrderedChildManifest(description) {
|
|
110
|
+
const lines = description.split(/\r?\n/);
|
|
111
|
+
const headings = [];
|
|
112
|
+
lines.forEach((line, index) => {
|
|
113
|
+
if (MD_MANIFEST_HEADING.test(line))
|
|
114
|
+
headings.push({ index, jira: false });
|
|
115
|
+
else if (JIRA_MANIFEST_HEADING.test(line))
|
|
116
|
+
headings.push({ index, jira: true });
|
|
117
|
+
});
|
|
118
|
+
if (headings.length !== 1)
|
|
119
|
+
return null;
|
|
120
|
+
const { index: start, jira } = headings[0];
|
|
121
|
+
const sectionEnd = jira ? JIRA_SECTION_END : MD_SECTION_END;
|
|
122
|
+
const itemPattern = jira ? JIRA_ITEM : MD_ITEM;
|
|
123
|
+
const rawItems = [];
|
|
124
|
+
let current = null;
|
|
125
|
+
const flush = () => {
|
|
126
|
+
if (current !== null)
|
|
127
|
+
rawItems.push(current.join(" "));
|
|
128
|
+
current = null;
|
|
129
|
+
};
|
|
130
|
+
for (let i = start + 1; i < lines.length; i += 1) {
|
|
131
|
+
const line = lines[i];
|
|
132
|
+
if (sectionEnd.test(line))
|
|
133
|
+
break;
|
|
134
|
+
const item = itemPattern.exec(line);
|
|
135
|
+
if (item !== null) {
|
|
136
|
+
flush();
|
|
137
|
+
current = [item[1]];
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (line.trim() === "") {
|
|
141
|
+
flush();
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
// An indented line continues the current item; an unindented paragraph is
|
|
145
|
+
// section prose and is ignored.
|
|
146
|
+
if (current !== null && /^\s+\S/.test(line))
|
|
147
|
+
current.push(line.trim());
|
|
148
|
+
else
|
|
149
|
+
flush();
|
|
150
|
+
}
|
|
151
|
+
flush();
|
|
152
|
+
if (rawItems.length === 0)
|
|
153
|
+
return null;
|
|
154
|
+
const items = [];
|
|
155
|
+
for (const text of rawItems) {
|
|
156
|
+
const markers = [...text.matchAll(DEPENDS_ON)];
|
|
157
|
+
if (markers.length !== 1)
|
|
158
|
+
return null;
|
|
159
|
+
const marker = markers[0];
|
|
160
|
+
const identityText = text.slice(0, marker.index);
|
|
161
|
+
const valueText = text.slice((marker.index ?? 0) + marker[0].length);
|
|
162
|
+
const identities = new Set(identityText.match(TICKET_KEY_IN_TEXT) ?? []);
|
|
163
|
+
if (identities.size !== 1)
|
|
164
|
+
return null;
|
|
165
|
+
const value = DEPENDS_ON_VALUE.exec(valueText);
|
|
166
|
+
if (value === null)
|
|
167
|
+
return null;
|
|
168
|
+
const dependsOn = value[1].toLowerCase() === "none" ? [] : value[1].split(",").map((part) => part.trim().toUpperCase());
|
|
169
|
+
items.push({ key: [...identities][0], dependsOn });
|
|
170
|
+
}
|
|
171
|
+
return items;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Check a parsed manifest against the classified children. Returns the order and
|
|
175
|
+
* edges over retained nodes, or null when ANY part is unusable — never a partly
|
|
176
|
+
* repaired list.
|
|
177
|
+
*/
|
|
178
|
+
function applyManifest(items, allChildKeys, retained, excluded) {
|
|
179
|
+
const seenItems = new Set();
|
|
180
|
+
const listedRetained = new Set();
|
|
181
|
+
const order = [];
|
|
182
|
+
const dependsOn = new Map();
|
|
183
|
+
for (const item of items) {
|
|
184
|
+
if (!allChildKeys.has(item.key) || seenItems.has(item.key))
|
|
185
|
+
return null;
|
|
186
|
+
seenItems.add(item.key);
|
|
187
|
+
const deps = new Set();
|
|
188
|
+
for (const dep of item.dependsOn) {
|
|
189
|
+
// A dependency must be a retained item listed EARLIER: that one rule
|
|
190
|
+
// rejects unknown keys, excluded spikes, self-edges, cycles, and
|
|
191
|
+
// out-of-order declarations.
|
|
192
|
+
if (dep === item.key || deps.has(dep) || !listedRetained.has(dep))
|
|
193
|
+
return null;
|
|
194
|
+
deps.add(dep);
|
|
195
|
+
}
|
|
196
|
+
if (excluded.has(item.key)) {
|
|
197
|
+
// A listed unfinished spike is dropped only when nothing depends on it; a
|
|
198
|
+
// later dependency on it fails the "listed earlier and retained" check.
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
listedRetained.add(item.key);
|
|
202
|
+
order.push(item.key);
|
|
203
|
+
dependsOn.set(item.key, [...deps]);
|
|
204
|
+
}
|
|
205
|
+
for (const key of retained) {
|
|
206
|
+
if (!listedRetained.has(key))
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
return { order, dependsOn };
|
|
210
|
+
}
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
// Touched-file evidence
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
const BRACE_SPAN = /\{\{([^{}\r\n]{1,300})\}\}/g;
|
|
215
|
+
const BACKTICK_SPAN = /`([^`\r\n]{1,300})`/g;
|
|
216
|
+
const LINE_SUFFIX = /:\d+(?:[-–]\d+)?$/;
|
|
217
|
+
const UNSAFE_PATH_CHARS = /[\s*?[\]{}$;&|<>()'"!`\\#:,=%^~@]|[\u0000-\u001f\u007f]/;
|
|
218
|
+
export function buildTrackedInventory(trackedFiles) {
|
|
219
|
+
const files = new Set();
|
|
220
|
+
const directories = new Set();
|
|
221
|
+
for (const file of trackedFiles) {
|
|
222
|
+
if (file === "")
|
|
223
|
+
continue;
|
|
224
|
+
files.add(file);
|
|
225
|
+
const parts = file.split("/");
|
|
226
|
+
for (let i = 1; i < parts.length; i += 1)
|
|
227
|
+
directories.add(parts.slice(0, i).join("/"));
|
|
228
|
+
}
|
|
229
|
+
return { files, directories };
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Return the repository-relative path a cited span names, or null when the span is
|
|
233
|
+
* not a safe, plausible repository file. Lexical only: no filesystem access.
|
|
234
|
+
*/
|
|
235
|
+
export function acceptCitedPath(span, inventory) {
|
|
236
|
+
let candidate = span.trim().replace(LINE_SUFFIX, "");
|
|
237
|
+
if (candidate === "" || candidate.length > 300)
|
|
238
|
+
return null;
|
|
239
|
+
if (candidate.startsWith("/") || /^[A-Za-z]:/.test(candidate) || candidate.includes("\\"))
|
|
240
|
+
return null;
|
|
241
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(candidate) || candidate.startsWith("www."))
|
|
242
|
+
return null;
|
|
243
|
+
if (candidate.endsWith("/"))
|
|
244
|
+
return null;
|
|
245
|
+
// Traversal is checked on the raw segments, before any normalization.
|
|
246
|
+
const rawSegments = candidate.split("/");
|
|
247
|
+
if (rawSegments.some((segment) => segment === ".."))
|
|
248
|
+
return null;
|
|
249
|
+
if (UNSAFE_PATH_CHARS.test(candidate))
|
|
250
|
+
return null;
|
|
251
|
+
if (candidate.startsWith("./"))
|
|
252
|
+
candidate = candidate.slice(2);
|
|
253
|
+
const segments = candidate.split("/");
|
|
254
|
+
if (segments.some((segment) => segment === "" || segment === "."))
|
|
255
|
+
return null;
|
|
256
|
+
if (inventory.files.has(candidate))
|
|
257
|
+
return candidate;
|
|
258
|
+
if (inventory.directories.has(candidate))
|
|
259
|
+
return null;
|
|
260
|
+
// A file the change will create: its parent must already hold tracked files.
|
|
261
|
+
// The root never qualifies, so a bare word in backticks is not a new file.
|
|
262
|
+
if (segments.length < 2)
|
|
263
|
+
return null;
|
|
264
|
+
const parent = segments.slice(0, -1).join("/");
|
|
265
|
+
return inventory.directories.has(parent) ? candidate : null;
|
|
266
|
+
}
|
|
267
|
+
/** The safe, deduplicated, sorted paths a description cites. */
|
|
268
|
+
export function extractTouchedFiles(description, inventory) {
|
|
269
|
+
const kept = new Set();
|
|
270
|
+
for (const pattern of [BRACE_SPAN, BACKTICK_SPAN]) {
|
|
271
|
+
for (const match of description.matchAll(pattern)) {
|
|
272
|
+
const accepted = acceptCitedPath(match[1], inventory);
|
|
273
|
+
if (accepted !== null)
|
|
274
|
+
kept.add(accepted);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return [...kept].sort();
|
|
278
|
+
}
|
|
279
|
+
// ---------------------------------------------------------------------------
|
|
280
|
+
// Summary validation (shared with discovery)
|
|
281
|
+
// ---------------------------------------------------------------------------
|
|
282
|
+
const CHILD_KEY_PATTERN = /^[A-Z][A-Z0-9_]{0,19}-[0-9]+$/;
|
|
283
|
+
function isOneOf(values, value) {
|
|
284
|
+
return typeof value === "string" && values.includes(value);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Validate a stored summary against the same fixed vocabularies the builder uses.
|
|
288
|
+
* Returns null for anything else, so free-form metadata never reaches a launch
|
|
289
|
+
* screen.
|
|
290
|
+
*/
|
|
291
|
+
export function parseGeneratedPlanSummary(value) {
|
|
292
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
293
|
+
return null;
|
|
294
|
+
const record = value;
|
|
295
|
+
if (!isOneOf(PLAN_GENERATION_ORDER_SOURCES, record.order_source))
|
|
296
|
+
return null;
|
|
297
|
+
const serialized = record.serialized_reason;
|
|
298
|
+
if (serialized !== null && !isOneOf(PLAN_GENERATION_SERIALIZED_REASONS, serialized))
|
|
299
|
+
return null;
|
|
300
|
+
if (!Array.isArray(record.excluded) || !Array.isArray(record.unknown_files))
|
|
301
|
+
return null;
|
|
302
|
+
const excluded = [];
|
|
303
|
+
for (const entry of record.excluded) {
|
|
304
|
+
if (entry === null || typeof entry !== "object")
|
|
305
|
+
return null;
|
|
306
|
+
const { ticket_key: key, reason } = entry;
|
|
307
|
+
if (typeof key !== "string" || !CHILD_KEY_PATTERN.test(key))
|
|
308
|
+
return null;
|
|
309
|
+
if (!isOneOf(PLAN_GENERATION_EXCLUSION_REASONS, reason))
|
|
310
|
+
return null;
|
|
311
|
+
excluded.push({ ticket_key: key, reason });
|
|
312
|
+
}
|
|
313
|
+
const unknownFiles = [];
|
|
314
|
+
for (const key of record.unknown_files) {
|
|
315
|
+
if (typeof key !== "string" || !CHILD_KEY_PATTERN.test(key))
|
|
316
|
+
return null;
|
|
317
|
+
unknownFiles.push(key);
|
|
318
|
+
}
|
|
319
|
+
return {
|
|
320
|
+
order_source: record.order_source,
|
|
321
|
+
serialized_reason: serialized,
|
|
322
|
+
excluded,
|
|
323
|
+
unknown_files: unknownFiles,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
// ---------------------------------------------------------------------------
|
|
327
|
+
// The builder
|
|
328
|
+
// ---------------------------------------------------------------------------
|
|
329
|
+
/** Build the plan, or return the one fixed reason it cannot be built. */
|
|
330
|
+
export function buildGeneratedEpicPlan(input) {
|
|
331
|
+
const fail = (reason) => ({ ok: false, reason });
|
|
332
|
+
if (!EPIC_KEY_PATTERN.test(input.epicKey))
|
|
333
|
+
return fail("invalid_child_metadata");
|
|
334
|
+
if (input.enumerationComplete !== true)
|
|
335
|
+
return fail("enumeration_incomplete");
|
|
336
|
+
// --- Boundary validation of the authoritative child list. -----------------
|
|
337
|
+
const allKeys = new Set();
|
|
338
|
+
for (const child of input.children) {
|
|
339
|
+
const key = child.ticket_key;
|
|
340
|
+
if (!CHILD_KEY_PATTERN.test(key) || key === input.epicKey || allKeys.has(key)) {
|
|
341
|
+
return fail("invalid_child_metadata");
|
|
342
|
+
}
|
|
343
|
+
if (!isOneOf(RECOGNIZED_STATUS_CATEGORIES, child.status_category))
|
|
344
|
+
return fail("invalid_child_metadata");
|
|
345
|
+
if (typeof child.issue_type !== "string" || child.issue_type.trim() === "") {
|
|
346
|
+
return fail("invalid_child_metadata");
|
|
347
|
+
}
|
|
348
|
+
allKeys.add(key);
|
|
349
|
+
}
|
|
350
|
+
// --- Classify: done first, then unfinished spikes. -------------------------
|
|
351
|
+
const done = new Set();
|
|
352
|
+
const planned = new Set();
|
|
353
|
+
const excludedKeys = new Set();
|
|
354
|
+
for (const child of input.children) {
|
|
355
|
+
if (child.status_category === "done")
|
|
356
|
+
done.add(child.ticket_key);
|
|
357
|
+
else if (child.issue_type.trim().toLowerCase() === "spike")
|
|
358
|
+
excludedKeys.add(child.ticket_key);
|
|
359
|
+
else
|
|
360
|
+
planned.add(child.ticket_key);
|
|
361
|
+
}
|
|
362
|
+
if (planned.size === 0)
|
|
363
|
+
return fail("no_runnable_children");
|
|
364
|
+
for (const key of planned) {
|
|
365
|
+
if (!input.childDescriptions.has(key))
|
|
366
|
+
return fail("description_unreadable");
|
|
367
|
+
}
|
|
368
|
+
const retained = new Set([...done, ...planned]);
|
|
369
|
+
// --- Order: the whole manifest, or ascending key order. --------------------
|
|
370
|
+
const items = parseOrderedChildManifest(input.epicDescription);
|
|
371
|
+
const manifest = items === null ? null : applyManifest(items, allKeys, retained, excludedKeys);
|
|
372
|
+
const orderSource = manifest === null ? "key_order" : "epic_manifest";
|
|
373
|
+
const order = manifest?.order ?? [...retained].sort(compareTicketKeys);
|
|
374
|
+
// --- Files. -----------------------------------------------------------------
|
|
375
|
+
const inventory = buildTrackedInventory(input.trackedFiles);
|
|
376
|
+
const filesByKey = new Map();
|
|
377
|
+
const unknownFiles = [];
|
|
378
|
+
for (const key of order) {
|
|
379
|
+
if (!planned.has(key))
|
|
380
|
+
continue;
|
|
381
|
+
const files = extractTouchedFiles(input.childDescriptions.get(key) ?? "", inventory);
|
|
382
|
+
if (files.length === 0)
|
|
383
|
+
unknownFiles.push(key);
|
|
384
|
+
else
|
|
385
|
+
filesByKey.set(key, files);
|
|
386
|
+
}
|
|
387
|
+
// --- Edges. -----------------------------------------------------------------
|
|
388
|
+
// A chain links PLANNED children only: a done node is already satisfied, so a
|
|
389
|
+
// chain through it would let the next planned child start beside the previous.
|
|
390
|
+
const serializedReason = unknownFiles.length > 0 ? "unknown_touched_files" : manifest === null ? "key_order_fallback" : null;
|
|
391
|
+
const chain = serializedReason !== null;
|
|
392
|
+
const dependsOn = new Map();
|
|
393
|
+
let previousPlanned = null;
|
|
394
|
+
for (const key of order) {
|
|
395
|
+
if (!planned.has(key)) {
|
|
396
|
+
dependsOn.set(key, chain ? [] : [...(manifest?.dependsOn.get(key) ?? [])]);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
dependsOn.set(key, chain ? (previousPlanned === null ? [] : [previousPlanned]) : [...(manifest?.dependsOn.get(key) ?? [])]);
|
|
400
|
+
previousPlanned = key;
|
|
401
|
+
}
|
|
402
|
+
const nodes = order.map((key) => {
|
|
403
|
+
const node = {
|
|
404
|
+
ticket_key: key,
|
|
405
|
+
status: done.has(key) ? "done" : "planned",
|
|
406
|
+
depends_on: dependsOn.get(key) ?? [],
|
|
407
|
+
};
|
|
408
|
+
// Omitted, never `[]`, whenever files are not known for the whole plan.
|
|
409
|
+
if (unknownFiles.length === 0 && planned.has(key))
|
|
410
|
+
node.touched_files = filesByKey.get(key) ?? [];
|
|
411
|
+
return node;
|
|
412
|
+
});
|
|
413
|
+
const edges = nodes.flatMap((node) => node.depends_on.map((from) => ({ from, to: node.ticket_key })));
|
|
414
|
+
return {
|
|
415
|
+
ok: true,
|
|
416
|
+
dag: { plan_version: GENERATED_PLAN_VERSION, nodes, edges },
|
|
417
|
+
summary: {
|
|
418
|
+
order_source: orderSource,
|
|
419
|
+
serialized_reason: serializedReason,
|
|
420
|
+
excluded: [...excludedKeys].sort(compareTicketKeys).map((key) => ({ ticket_key: key, reason: "unfinished_spike" })),
|
|
421
|
+
unknown_files: unknownFiles,
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|