create-agdf 0.4.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +9 -7
  2. package/bin/create-agdf.js +284 -152
  3. package/generated/.agdf/control/README.md +15 -3
  4. package/generated/.agdf/control/templates/RUN_STATE.md +48 -0
  5. package/generated/.github/skills/agdf-brownfield-analysis/SKILL.md +3 -3
  6. package/generated/.github/skills/agdf-delivery-path-search/SKILL.md +14 -0
  7. package/generated/.github/skills/agdf-gate-check/SKILL.md +15 -10
  8. package/generated/.github/skills/agdf-release-or/SKILL.md +1 -1
  9. package/generated/.github/skills/agdf-runtime-contract.md +24 -8
  10. package/generated/.opencode/agdf-runtime-contract.md +24 -8
  11. package/generated/.opencode/agents/agdf-brownfield-analysis.md +2 -2
  12. package/generated/.opencode/agents/agdf-delivery-path-search.md +14 -0
  13. package/generated/.opencode/agents/agdf-gate-check.md +15 -10
  14. package/generated/.opencode/agents/agdf-release-or.md +1 -1
  15. package/generated/plugins/agdf/.codex-plugin/plugin.json +1 -1
  16. package/generated/plugins/agdf/control/README.md +15 -3
  17. package/generated/plugins/agdf/control/templates/RUN_STATE.md +48 -0
  18. package/generated/plugins/agdf/hooks/session-start.sh +2 -0
  19. package/generated/plugins/agdf/meta/agdf-plugin.definition.json +1 -1
  20. package/generated/plugins/agdf/meta/agdf-runtime-contract.md +24 -8
  21. package/generated/plugins/agdf/skills/brownfield-analysis/SKILL.md +3 -3
  22. package/generated/plugins/agdf/skills/delivery-path-search/SKILL.md +14 -0
  23. package/generated/plugins/agdf/skills/gate-check/SKILL.md +15 -10
  24. package/generated/plugins/agdf/skills/release-or/SKILL.md +1 -1
  25. package/lib/control-state/aggregate.js +18 -0
  26. package/lib/control-state/index.js +6 -0
  27. package/lib/control-state/legacy-migration.js +94 -0
  28. package/lib/control-state/run-state-parser.js +196 -0
  29. package/lib/control-state/run-state-repository.js +68 -0
  30. package/lib/control-state/run-state-resolver.js +45 -0
  31. package/lib/control-state/run-state-writer.js +96 -0
  32. package/lib/delivery-path-search/candidate-policy.js +67 -2
  33. package/lib/delivery-path-search/contracts.js +127 -0
  34. package/lib/delivery-path-search/evaluators/claude.js +2 -16
  35. package/lib/delivery-path-search/evaluators/codex.js +2 -16
  36. package/lib/delivery-path-search/generators/claude.js +40 -0
  37. package/lib/delivery-path-search/generators/codex.js +38 -0
  38. package/lib/delivery-path-search/generators/protocol.js +12 -0
  39. package/lib/delivery-path-search/persistence.js +5 -0
  40. package/lib/delivery-path-search/search-engine.js +64 -2
  41. package/lib/delivery-path-search/state-adapter.js +119 -16
  42. package/lib/delivery-path-search/transports/read-only-guard.js +43 -0
  43. package/package.json +4 -2
@@ -0,0 +1,96 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ closeSync,
4
+ fsyncSync,
5
+ existsSync,
6
+ lstatSync,
7
+ openSync,
8
+ readFileSync,
9
+ renameSync,
10
+ unlinkSync,
11
+ writeFileSync,
12
+ } from "node:fs";
13
+ import { dirname } from "node:path";
14
+
15
+ import { parseRunState } from "./run-state-parser.js";
16
+
17
+ function fsyncDirectory(path) {
18
+ const descriptor = openSync(path, "r");
19
+ try {
20
+ fsyncSync(descriptor);
21
+ } finally {
22
+ closeSync(descriptor);
23
+ }
24
+ }
25
+
26
+ export function atomicWrite(path, content) {
27
+ if (existsSync(path)) {
28
+ const destination = lstatSync(path);
29
+ if (destination.isSymbolicLink() || !destination.isFile()) {
30
+ throw new Error("AGDF_RUN_PATH_INVALID");
31
+ }
32
+ }
33
+
34
+ const parent = lstatSync(dirname(path));
35
+ if (parent.isSymbolicLink() || !parent.isDirectory()) {
36
+ throw new Error("AGDF_RUN_PATH_INVALID");
37
+ }
38
+
39
+ const temp = `${path}.tmp-${process.pid}-${randomUUID()}`;
40
+ let descriptor;
41
+
42
+ try {
43
+ descriptor = openSync(temp, "wx");
44
+ writeFileSync(descriptor, content, "utf8");
45
+ fsyncSync(descriptor);
46
+ closeSync(descriptor);
47
+ descriptor = undefined;
48
+
49
+ renameSync(temp, path);
50
+ fsyncDirectory(dirname(path));
51
+ } catch (error) {
52
+ if (descriptor !== undefined) closeSync(descriptor);
53
+ try {
54
+ unlinkSync(temp);
55
+ } catch (cleanupError) {
56
+ if (cleanupError.code !== "ENOENT") throw cleanupError;
57
+ }
58
+ throw error;
59
+ }
60
+ }
61
+
62
+ export function writeRun(path, content, expectedRevisionId) {
63
+ const lockPath = `${path}.lock`;
64
+ let lockDescriptor;
65
+ try {
66
+ lockDescriptor = openSync(lockPath, "wx");
67
+ } catch (error) {
68
+ if (error.code === "EEXIST") throw new Error("AGDF_RUN_WRITE_LOCKED");
69
+ throw error;
70
+ }
71
+
72
+ try {
73
+ const current = parseRunState(readFileSync(path, "utf8"));
74
+ if (!current.valid) throw new Error("AGDF_RUN_STATE_INVALID");
75
+ if (current.meta.revision_id !== expectedRevisionId) {
76
+ throw new Error("AGDF_STALE_RUN_REVISION");
77
+ }
78
+
79
+ const next = content
80
+ .replace(
81
+ /^- revision:\s*.*$/m,
82
+ `- revision: ${Number(current.meta.revision) + 1}`,
83
+ )
84
+ .replace(/^- revision_id:\s*.*$/m, `- revision_id: ${randomUUID()}`);
85
+ const candidate = parseRunState(next);
86
+ if (!candidate.valid || candidate.meta.run_id !== current.meta.run_id) {
87
+ throw new Error("AGDF_RUN_STATE_INVALID");
88
+ }
89
+
90
+ atomicWrite(path, next);
91
+ return candidate;
92
+ } finally {
93
+ closeSync(lockDescriptor);
94
+ unlinkSync(lockPath);
95
+ }
96
+ }
@@ -1,12 +1,37 @@
1
1
  import { validateCandidate } from "./contracts.js";
2
2
 
3
3
  function normalized(value) {
4
- return String(value).toLowerCase().replace(/[`*_]/g, "").replace(/\s+/g, " ").trim();
4
+ return String(value).normalize("NFKC").toLowerCase().replace(/[`*_]/g, "").replace(/[^\p{L}\p{N}\s-]/gu, " ").replace(/\s+/g, " ").trim();
5
+ }
6
+
7
+ const STOP_WORDS = new Set(["a", "an", "and", "der", "die", "das", "ein", "eine", "the", "to", "und", "zu"]);
8
+
9
+ function tokens(value) {
10
+ return new Set(normalized(value).split(/\s+/).filter((item) => item && !STOP_WORDS.has(item)));
11
+ }
12
+
13
+ function jaccard(left, right) {
14
+ const union = new Set([...left, ...right]);
15
+ if (!union.size) return 1;
16
+ let intersection = 0;
17
+ for (const item of left) if (right.has(item)) intersection += 1;
18
+ return intersection / union.size;
19
+ }
20
+
21
+ function signature(candidate) {
22
+ return JSON.stringify({
23
+ gate_action: normalized(candidate.gate_action ?? candidate.action),
24
+ affected_boundaries: [...(candidate.affected_boundaries ?? [])].map(normalized).sort(),
25
+ evidence: [...(candidate.expected_evidence ?? [])].map(normalized).sort(),
26
+ tests: [...(candidate.tests ?? [])].map(normalized).sort(),
27
+ risk_strategy: normalized(candidate.risk_strategy ?? ""),
28
+ reversibility: normalized(candidate.reversibility ?? ""),
29
+ });
5
30
  }
6
31
 
7
32
  export function candidateLegality(candidateValue, input) {
8
33
  const candidate = validateCandidate(candidateValue);
9
- const action = normalized(candidate.action);
34
+ const action = normalized(candidate.gate_action ?? candidate.action);
10
35
  const forbiddenMatch = input.forbidden_actions.find((item) => {
11
36
  const forbidden = normalized(item);
12
37
  return forbidden && action === forbidden;
@@ -32,3 +57,43 @@ export function candidatesFromInput(input) {
32
57
  parent_id: null,
33
58
  }));
34
59
  }
60
+
61
+ export function generatedCandidatesFromResponse(response, input, existingCandidates = []) {
62
+ const accepted = [];
63
+ const rejected = [];
64
+ for (const proposal of response.proposals) {
65
+ const candidate = {
66
+ id: `generated-${proposal.proposal_id}`,
67
+ action: proposal.intent,
68
+ source: "generated",
69
+ gate_action: proposal.gate_action,
70
+ intent: proposal.intent,
71
+ expected_evidence: proposal.expected_evidence,
72
+ tests: proposal.tests,
73
+ assumptions: proposal.assumptions,
74
+ affected_boundaries: proposal.affected_boundaries,
75
+ risk_strategy: proposal.risk_strategy,
76
+ reversibility: proposal.reversibility,
77
+ generator_proposal_id: proposal.proposal_id,
78
+ depth: 0,
79
+ parent_id: null,
80
+ };
81
+ const legality = candidateLegality(candidate, input);
82
+ if (!legality.legal) {
83
+ rejected.push({ proposal_id: proposal.proposal_id, reason: legality.reason });
84
+ continue;
85
+ }
86
+ const peers = [...existingCandidates, ...accepted].filter((item) => normalized(item.gate_action ?? item.action) === normalized(candidate.gate_action));
87
+ const duplicate = peers.find((item) => {
88
+ const same = normalized(item.intent ?? item.action) === normalized(candidate.intent);
89
+ const sameDecision = signature(item) === signature(candidate);
90
+ return same || sameDecision || jaccard(tokens(item.intent ?? item.action), tokens(candidate.intent)) >= 0.8;
91
+ });
92
+ if (duplicate) {
93
+ rejected.push({ proposal_id: proposal.proposal_id, reason: "cosmetic_or_material_duplicate" });
94
+ continue;
95
+ }
96
+ accepted.push(candidate);
97
+ }
98
+ return { accepted, rejected };
99
+ }
@@ -1,4 +1,5 @@
1
1
  export const CONTRACT_VERSION = "1";
2
+ export const GENERATOR_CONTRACT_VERSION = "1";
2
3
  export const SCORING_POLICY_VERSION = "1";
3
4
  export const SCORE_DIMENSIONS = [
4
5
  "scope_fit",
@@ -25,6 +26,39 @@ function stringArray(value, label) {
25
26
  return value.map((item) => item.trim()).filter(Boolean);
26
27
  }
27
28
 
29
+ function optionalBoundedString(value, label, maxLength = 1000) {
30
+ if (value === undefined) return undefined;
31
+ const result = requireString(value, label);
32
+ if (result.length > maxLength) throw new Error(`${label} must be at most ${maxLength} characters`);
33
+ return result;
34
+ }
35
+
36
+ function rejectUnknownKeys(value, allowed, label) {
37
+ const unknown = Object.keys(value).filter((key) => !allowed.includes(key));
38
+ if (unknown.length) throw new Error(`${label} contains unknown fields: ${unknown.join(", ")}`);
39
+ }
40
+
41
+ function generationConfig(value, wholeRunBudgets) {
42
+ if (value === undefined) return undefined;
43
+ const generation = requireObject(value, "generation");
44
+ if (typeof generation.enabled !== "boolean") throw new Error("generation.enabled must be a boolean");
45
+ const limits = {
46
+ max_calls: 1,
47
+ max_proposals: 5,
48
+ max_duration_ms: 30000,
49
+ max_cost_units: 5,
50
+ };
51
+ for (const [key, maximum] of Object.entries(limits)) {
52
+ if (!Number.isInteger(generation[key]) || generation[key] < 1 || generation[key] > maximum) {
53
+ throw new Error(`generation.${key} must be an integer from 1 to ${maximum}`);
54
+ }
55
+ }
56
+ if (generation.max_calls !== 1) throw new Error("generation.max_calls must equal 1 in this contract version");
57
+ if (generation.max_duration_ms > wholeRunBudgets.max_duration_ms) throw new Error("generation.max_duration_ms cannot exceed budgets.max_duration_ms");
58
+ if (generation.max_cost_units > wholeRunBudgets.max_cost_units) throw new Error("generation.max_cost_units cannot exceed budgets.max_cost_units");
59
+ return structuredClone(generation);
60
+ }
61
+
28
62
  export function validateSearchInput(value) {
29
63
  const input = requireObject(value, "search input");
30
64
  if (input.contract_version !== CONTRACT_VERSION) throw new Error(`unsupported search input contract_version: ${input.contract_version}`);
@@ -43,6 +77,7 @@ export function validateSearchInput(value) {
43
77
  for (const key of ["max_candidates", "max_depth", "max_evaluations", "max_duration_ms", "max_cost_units", "stability_window"]) {
44
78
  if (!Number.isInteger(budgets[key]) || budgets[key] < 1) throw new Error(`budgets.${key} must be a positive integer`);
45
79
  }
80
+ generationConfig(input.generation, budgets);
46
81
  return structuredClone(input);
47
82
  }
48
83
 
@@ -53,9 +88,101 @@ export function validateCandidate(value) {
53
88
  stringArray(candidate.expected_evidence ?? [], "candidate.expected_evidence");
54
89
  stringArray(candidate.tests ?? [], "candidate.tests");
55
90
  stringArray(candidate.assumptions ?? [], "candidate.assumptions");
91
+ if (candidate.source !== undefined && !["deterministic", "generated", "expanded"].includes(candidate.source)) {
92
+ throw new Error("candidate.source must be deterministic, generated or expanded");
93
+ }
94
+ optionalBoundedString(candidate.gate_action, "candidate.gate_action");
95
+ optionalBoundedString(candidate.intent, "candidate.intent");
96
+ stringArray(candidate.affected_boundaries ?? [], "candidate.affected_boundaries");
97
+ optionalBoundedString(candidate.risk_strategy, "candidate.risk_strategy");
98
+ optionalBoundedString(candidate.reversibility, "candidate.reversibility");
99
+ optionalBoundedString(candidate.generator_proposal_id, "candidate.generator_proposal_id", 200);
56
100
  return structuredClone(candidate);
57
101
  }
58
102
 
103
+ export function validateGeneratorRequest(value) {
104
+ const request = requireObject(value, "generator request");
105
+ rejectUnknownKeys(request, ["contract_version", "scope_key", "objective", "scope_summary", "current_gate", "allowed_actions", "forbidden_actions", "artefact_refs", "evidence", "missing_evidence", "risks", "constraints", "enforcement", "budgets"], "generator request");
106
+ if (request.contract_version !== GENERATOR_CONTRACT_VERSION) throw new Error(`unsupported generator contract_version: ${request.contract_version}`);
107
+ requireString(request.scope_key, "generator scope_key");
108
+ optionalBoundedString(request.objective, "generator objective", 2000);
109
+ optionalBoundedString(request.scope_summary, "generator scope_summary", 2000);
110
+ requireString(request.current_gate, "generator current_gate");
111
+ for (const key of ["allowed_actions", "forbidden_actions", "artefact_refs", "evidence", "missing_evidence", "risks", "constraints"]) {
112
+ const items = stringArray(request[key] ?? [], `generator ${key}`);
113
+ if (items.length > 20 || items.some((item) => item.length > 500)) throw new Error(`generator ${key} exceeds context bounds`);
114
+ }
115
+ validateEnforcement(request.enforcement);
116
+ const budgets = requireObject(request.budgets, "generator budgets");
117
+ generationConfig({ enabled: true, ...budgets }, { max_duration_ms: 120000, max_cost_units: 20 });
118
+ const serialized = JSON.stringify(request);
119
+ if (/(secret|credential|environment|raw_prompt|hidden_reasoning|source_snapshot)/i.test(serialized)) {
120
+ throw new Error("generator request contains disallowed context");
121
+ }
122
+ return structuredClone(request);
123
+ }
124
+
125
+ export function validateGeneratorResponse(value) {
126
+ const response = requireObject(value, "generator response");
127
+ rejectUnknownKeys(response, ["contract_version", "proposals", "cost_units", "metadata"], "generator response");
128
+ if (response.contract_version !== GENERATOR_CONTRACT_VERSION) throw new Error(`unsupported generator contract_version: ${response.contract_version}`);
129
+ if (!Array.isArray(response.proposals) || response.proposals.length > 5) throw new Error("generator proposals must be an array with at most 5 items");
130
+ if (!Number.isInteger(response.cost_units) || response.cost_units < 0 || response.cost_units > 5) throw new Error("generator cost_units must be an integer from 0 to 5");
131
+ if (response.metadata !== undefined) {
132
+ const metadata = requireObject(response.metadata, "generator metadata");
133
+ rejectUnknownKeys(metadata, ["name", "runtime", "model"], "generator metadata");
134
+ for (const [key, item] of Object.entries(metadata)) optionalBoundedString(item, `generator metadata.${key}`, 500);
135
+ }
136
+ const proposals = response.proposals.map((proposal, index) => {
137
+ const item = requireObject(proposal, `proposal ${index}`);
138
+ rejectUnknownKeys(item, ["proposal_id", "gate_action", "intent", "expected_evidence", "tests", "assumptions", "affected_boundaries", "risk_strategy", "reversibility"], `proposal ${index}`);
139
+ const normalized = {
140
+ proposal_id: requireString(item.proposal_id, `proposal ${index}.proposal_id`),
141
+ gate_action: requireString(item.gate_action, `proposal ${index}.gate_action`),
142
+ intent: requireString(item.intent, `proposal ${index}.intent`),
143
+ expected_evidence: stringArray(item.expected_evidence ?? [], `proposal ${index}.expected_evidence`),
144
+ tests: stringArray(item.tests ?? [], `proposal ${index}.tests`),
145
+ assumptions: stringArray(item.assumptions ?? [], `proposal ${index}.assumptions`),
146
+ affected_boundaries: stringArray(item.affected_boundaries ?? [], `proposal ${index}.affected_boundaries`),
147
+ risk_strategy: requireString(item.risk_strategy, `proposal ${index}.risk_strategy`),
148
+ reversibility: requireString(item.reversibility, `proposal ${index}.reversibility`),
149
+ };
150
+ if (JSON.stringify(normalized).length > 5000) throw new Error(`proposal ${index} exceeds size limit`);
151
+ if (/(^|\s)(rm|sudo|curl|wget|bash|sh|exec)\s/i.test(normalized.intent)) throw new Error(`proposal ${index} contains executable instructions`);
152
+ return normalized;
153
+ });
154
+ return {
155
+ contract_version: response.contract_version,
156
+ proposals,
157
+ cost_units: response.cost_units,
158
+ metadata: response.metadata ? structuredClone(response.metadata) : {},
159
+ };
160
+ }
161
+
162
+ export function generatorOutputSchema() {
163
+ return {
164
+ type: "object",
165
+ additionalProperties: false,
166
+ required: ["contract_version", "proposals", "cost_units"],
167
+ properties: {
168
+ contract_version: { type: "string", const: GENERATOR_CONTRACT_VERSION },
169
+ cost_units: { type: "integer", minimum: 0, maximum: 5 },
170
+ proposals: {
171
+ type: "array", maxItems: 5, items: {
172
+ type: "object", additionalProperties: false,
173
+ required: ["proposal_id", "gate_action", "intent", "expected_evidence", "tests", "assumptions", "affected_boundaries", "risk_strategy", "reversibility"],
174
+ properties: {
175
+ proposal_id: { type: "string", minLength: 1 }, gate_action: { type: "string", minLength: 1 }, intent: { type: "string", minLength: 1 },
176
+ expected_evidence: { type: "array", items: { type: "string" } }, tests: { type: "array", items: { type: "string" } },
177
+ assumptions: { type: "array", items: { type: "string" } }, affected_boundaries: { type: "array", items: { type: "string" } },
178
+ risk_strategy: { type: "string", minLength: 1 }, reversibility: { type: "string", minLength: 1 },
179
+ },
180
+ },
181
+ },
182
+ },
183
+ };
184
+ }
185
+
59
186
  export function validateEvaluation(value, candidateId) {
60
187
  const evaluation = requireObject(value, "evaluation");
61
188
  if (evaluation.contract_version !== CONTRACT_VERSION) throw new Error(`unsupported evaluator contract_version: ${evaluation.contract_version}`);
@@ -1,5 +1,6 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { evaluatorOutputSchema, validateEvaluation } from "../contracts.js";
3
+ import { guardedExecFileSync } from "../transports/read-only-guard.js";
3
4
 
4
5
  export function claudeEvaluator(options = {}) {
5
6
  const claudeBin = options.claudeBin ?? "claude";
@@ -8,18 +9,6 @@ export function claudeEvaluator(options = {}) {
8
9
  runtime = execFileSync(claudeBin, ["--version"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
9
10
  } catch {}
10
11
 
11
- function gitState() {
12
- try {
13
- return execFileSync("git", ["status", "--porcelain=v1", "-z"], {
14
- cwd: options.cwd,
15
- encoding: "utf8",
16
- stdio: ["ignore", "pipe", "ignore"],
17
- });
18
- } catch {
19
- return null;
20
- }
21
- }
22
-
23
12
  return {
24
13
  name: "claude",
25
14
  metadata: { name: "claude", runtime, model: options.model ?? "configured-default" },
@@ -41,15 +30,12 @@ export function claudeEvaluator(options = {}) {
41
30
  ];
42
31
  if (options.model) args.push("--model", options.model);
43
32
  args.push(prompt);
44
- const before = gitState();
45
- const raw = execFileSync(claudeBin, args, {
33
+ const raw = guardedExecFileSync(claudeBin, args, {
46
34
  cwd: options.cwd,
47
35
  encoding: "utf8",
48
36
  stdio: ["ignore", "pipe", "pipe"],
49
37
  timeout: options.timeoutMs ?? 120000,
50
38
  });
51
- const after = gitState();
52
- if (before !== null && after !== before) throw new Error("repository mutation detected during read-only evaluator run");
53
39
  const response = JSON.parse(raw);
54
40
  if (response.is_error) throw new Error(`claude evaluator returned an error: ${response.result ?? "unknown error"}`);
55
41
  return validateEvaluation(JSON.parse(response.result), candidate.id);
@@ -3,6 +3,7 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { evaluatorOutputSchema, validateEvaluation } from "../contracts.js";
6
+ import { guardedExecFileSync } from "../transports/read-only-guard.js";
6
7
 
7
8
  export function codexEvaluator(options = {}) {
8
9
  const codexBin = options.codexBin ?? "codex";
@@ -11,18 +12,6 @@ export function codexEvaluator(options = {}) {
11
12
  runtime = execFileSync(codexBin, ["--version"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
12
13
  } catch {}
13
14
 
14
- function gitState() {
15
- try {
16
- return execFileSync("git", ["status", "--porcelain=v1", "-z"], {
17
- cwd: options.cwd,
18
- encoding: "utf8",
19
- stdio: ["ignore", "pipe", "ignore"],
20
- });
21
- } catch {
22
- return null;
23
- }
24
- }
25
-
26
15
  return {
27
16
  name: "codex",
28
17
  metadata: { name: "codex", runtime, model: options.model ?? "configured-default" },
@@ -49,15 +38,12 @@ export function codexEvaluator(options = {}) {
49
38
  ];
50
39
  if (options.model) args.push("--model", options.model);
51
40
  args.push(prompt);
52
- const before = gitState();
53
- execFileSync(codexBin, args, {
41
+ guardedExecFileSync(codexBin, args, {
54
42
  cwd: options.cwd,
55
43
  encoding: "utf8",
56
44
  stdio: ["ignore", "pipe", "pipe"],
57
45
  timeout: options.timeoutMs ?? 120000,
58
46
  });
59
- const after = gitState();
60
- if (before !== null && after !== before) throw new Error("repository mutation detected during read-only evaluator run");
61
47
  return validateEvaluation(JSON.parse(readFileSync(outputPath, "utf8")), candidate.id);
62
48
  } finally {
63
49
  rmSync(temp, { recursive: true, force: true });
@@ -0,0 +1,40 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { generatorOutputSchema, validateGeneratorRequest, validateGeneratorResponse } from "../contracts.js";
3
+ import { guardedExecFileSync } from "../transports/read-only-guard.js";
4
+
5
+ export function claudeGenerator(options = {}) {
6
+ const claudeBin = options.claudeBin ?? "claude";
7
+ const execute = options.execute ?? guardedExecFileSync;
8
+ let runtime = options.runtime ?? "claude version unavailable";
9
+ if (!options.runtime) try { runtime = execFileSync(claudeBin, ["--version"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim(); } catch {}
10
+ return {
11
+ name: "claude",
12
+ metadata: { name: "claude", runtime, model: options.model ?? "configured-default" },
13
+ async generate(requestValue) {
14
+ const request = validateGeneratorRequest(requestValue);
15
+ const prompt = [
16
+ "Generate bounded advisory delivery-path proposals. Do not use tools, execute commands, or modify files.",
17
+ "Return only JSON matching the supplied schema. Do not include hidden reasoning.",
18
+ `Normalized request: ${JSON.stringify(request)}`,
19
+ ].join("\n");
20
+ const args = ["-p", "--disallowedTools", "Edit,Write,Bash", "--output-format", "json", "--json-schema", JSON.stringify(generatorOutputSchema())];
21
+ if (options.model) args.push("--model", options.model);
22
+ args.push(prompt);
23
+ let raw;
24
+ try {
25
+ raw = execute(claudeBin, args, { cwd: options.cwd, timeout: options.timeoutMs ?? request.budgets.max_duration_ms });
26
+ } catch (error) {
27
+ const providerOutput = String(error?.stdout ?? "");
28
+ if (/not logged in|unauthorized|authentication/i.test(providerOutput)) {
29
+ const authentication = new Error("claude generator authentication failed");
30
+ authentication.code = "GENERATOR_AUTHENTICATION_FAILED";
31
+ throw authentication;
32
+ }
33
+ throw error;
34
+ }
35
+ const response = JSON.parse(raw);
36
+ if (response.is_error) throw new Error(`claude generator returned an error: ${response.result ?? "unknown error"}`);
37
+ return validateGeneratorResponse(JSON.parse(response.result));
38
+ },
39
+ };
40
+ }
@@ -0,0 +1,38 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { generatorOutputSchema, validateGeneratorRequest, validateGeneratorResponse } from "../contracts.js";
6
+ import { guardedExecFileSync } from "../transports/read-only-guard.js";
7
+
8
+ export function codexGenerator(options = {}) {
9
+ const codexBin = options.codexBin ?? "codex";
10
+ const execute = options.execute ?? guardedExecFileSync;
11
+ let runtime = options.runtime ?? "codex version unavailable";
12
+ if (!options.runtime) try { runtime = execFileSync(codexBin, ["--version"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim(); } catch {}
13
+ return {
14
+ name: "codex",
15
+ metadata: { name: "codex", runtime, model: options.model ?? "configured-default" },
16
+ async generate(requestValue) {
17
+ const request = validateGeneratorRequest(requestValue);
18
+ const temp = mkdtempSync(join(tmpdir(), "agdf-dps-generator-"));
19
+ const schemaPath = join(temp, "schema.json");
20
+ const outputPath = join(temp, "output.json");
21
+ try {
22
+ writeFileSync(schemaPath, `${JSON.stringify(generatorOutputSchema(), null, 2)}\n`, "utf8");
23
+ const prompt = [
24
+ "Generate bounded advisory delivery-path proposals. Do not use tools, execute commands, or modify files.",
25
+ "Return only JSON matching the supplied schema. Do not include hidden reasoning.",
26
+ `Normalized request: ${JSON.stringify(request)}`,
27
+ ].join("\n");
28
+ const args = ["exec", "--sandbox", "read-only", "--ephemeral", "--ignore-user-config", "--output-schema", schemaPath, "--output-last-message", outputPath, "--color", "never"];
29
+ if (options.model) args.push("--model", options.model);
30
+ args.push(prompt);
31
+ execute(codexBin, args, { cwd: options.cwd, timeout: options.timeoutMs ?? request.budgets.max_duration_ms });
32
+ return validateGeneratorResponse(JSON.parse(readFileSync(outputPath, "utf8")));
33
+ } finally {
34
+ rmSync(temp, { recursive: true, force: true });
35
+ }
36
+ },
37
+ };
38
+ }
@@ -0,0 +1,12 @@
1
+ import { validateGeneratorRequest, validateGeneratorResponse } from "../contracts.js";
2
+
3
+ export function fixtureGenerator(response, metadata = { name: "fixture-generator" }) {
4
+ return {
5
+ name: metadata.name,
6
+ metadata,
7
+ async generate(request) {
8
+ validateGeneratorRequest(request);
9
+ return validateGeneratorResponse(response);
10
+ },
11
+ };
12
+ }
@@ -25,6 +25,11 @@ export function persistSearchResult(targetDir, result) {
25
25
  `- recommendation: ${recommendation}`,
26
26
  `- stopping_reason: ${safe.stopping_reason}`,
27
27
  `- evaluations: ${safe.budgets.evaluations}`,
28
+ `- generation_status: ${safe.generation?.status ?? "legacy_deterministic"}`,
29
+ `- generated_candidates: ${safe.generation ? `${safe.generation.accepted}/${safe.generation.returned}` : "0/0"}`,
30
+ `- generation_cost_units: ${safe.generation?.cost_units ?? 0}`,
31
+ `- generation_duration_ms: ${safe.generation?.duration_ms ?? 0}`,
32
+ ...(safe.generation?.failure_code ? [`- generation_failure: ${safe.generation.failure_code}`] : []),
28
33
  `- next_gate_action: ${safe.next_gate_action}`,
29
34
  "",
30
35
  "This summary contains model judgements, not measured facts. Canonical AGDF gate-check remains authoritative.",
@@ -1,11 +1,24 @@
1
1
  import { CONTRACT_VERSION, SCORING_POLICY_VERSION, validateEvaluation, validateSearchInput } from "./contracts.js";
2
- import { candidateLegality, candidatesFromInput } from "./candidate-policy.js";
2
+ import { candidateLegality, candidatesFromInput, generatedCandidatesFromResponse } from "./candidate-policy.js";
3
3
  import { scoreEvaluation } from "./scoring.js";
4
+ import { generatorRequestFromInput } from "./state-adapter.js";
5
+
6
+ function generatorFailureCode(error) {
7
+ if (error?.code === "GENERATOR_MUTATION_DETECTED") return "generator_mutation_detected";
8
+ if (error?.code === "GENERATOR_TIMEOUT") return "generator_timeout";
9
+ if (error?.code === "GENERATOR_AUTHENTICATION_FAILED") return "generator_authentication_failed";
10
+ if (error?.code === "ENOENT") return "generator_unavailable";
11
+ if (/auth/i.test(error?.message ?? "")) return "generator_authentication_failed";
12
+ if (/context|disallowed/i.test(error?.message ?? "")) return "generator_context_rejected";
13
+ if (/budget/i.test(error?.message ?? "")) return "generator_budget_exceeded";
14
+ return "generator_schema_invalid";
15
+ }
4
16
 
5
17
  export async function runDeliveryPathSearch(inputValue, evaluator, options = {}) {
6
18
  const input = validateSearchInput(inputValue);
7
19
  const startedAt = Date.now();
8
- const queue = [...(options.candidates ?? candidatesFromInput(input))];
20
+ const baseline = [...(options.candidates ?? candidatesFromInput(input))];
21
+ const queue = [...baseline];
9
22
  const rejected = [];
10
23
  const evaluated = [];
11
24
  let evaluations = 0;
@@ -13,6 +26,53 @@ export async function runDeliveryPathSearch(inputValue, evaluator, options = {})
13
26
  let lastLeaderId = null;
14
27
  let stableComparisons = 0;
15
28
  let stoppingReason = "candidate_queue_exhausted";
29
+ const generation = {
30
+ status: input.generation?.enabled ? "failed" : "disabled",
31
+ adapter: options.generator?.metadata ?? null,
32
+ budgets: input.generation ?? null,
33
+ returned: 0,
34
+ accepted: 0,
35
+ rejected: [],
36
+ cost_units: 0,
37
+ duration_ms: 0,
38
+ failure_code: null,
39
+ };
40
+
41
+ if (input.generation?.enabled) {
42
+ if (!options.generator) {
43
+ generation.failure_code = "generator_unavailable";
44
+ } else {
45
+ const generationStartedAt = Date.now();
46
+ try {
47
+ const response = await options.generator.generate(generatorRequestFromInput(input));
48
+ const elapsed = Date.now() - generationStartedAt;
49
+ generation.returned = response.proposals.length;
50
+ generation.cost_units = response.cost_units;
51
+ costUnits += response.cost_units;
52
+ if (elapsed > input.generation.max_duration_ms) {
53
+ const error = new Error("generator duration budget exceeded");
54
+ error.code = "GENERATOR_TIMEOUT";
55
+ throw error;
56
+ }
57
+ if (response.cost_units > input.generation.max_cost_units || response.proposals.length > input.generation.max_proposals) {
58
+ const error = new Error("generator response exceeds configured budget");
59
+ error.code = "GENERATOR_BUDGET_EXCEEDED";
60
+ throw error;
61
+ }
62
+ const filtered = generatedCandidatesFromResponse(response, input, baseline);
63
+ generation.accepted = filtered.accepted.length;
64
+ generation.rejected = filtered.rejected;
65
+ queue.push(...filtered.accepted.slice(0, input.generation.max_proposals));
66
+ generation.status = filtered.accepted.length === response.proposals.length ? "success" : "partial";
67
+ if (!filtered.accepted.length) generation.failure_code = "generator_no_diverse_proposals";
68
+ } catch (error) {
69
+ generation.failure_code = generatorFailureCode(error);
70
+ if (generation.failure_code === "generator_mutation_detected") throw error;
71
+ } finally {
72
+ generation.duration_ms = Date.now() - generationStartedAt;
73
+ }
74
+ }
75
+ }
16
76
 
17
77
  while (queue.length > 0) {
18
78
  if (evaluations >= input.budgets.max_evaluations) { stoppingReason = "evaluation_budget_exhausted"; break; }
@@ -46,6 +106,7 @@ export async function runDeliveryPathSearch(inputValue, evaluator, options = {})
46
106
  parent_id: candidate.id,
47
107
  depth: (candidate.depth ?? 0) + 1,
48
108
  action,
109
+ source: "expanded",
49
110
  expected_evidence: [],
50
111
  tests: [],
51
112
  assumptions: evaluation.assumptions,
@@ -85,6 +146,7 @@ export async function runDeliveryPathSearch(inputValue, evaluator, options = {})
85
146
  rejected,
86
147
  enforcement: input.enforcement,
87
148
  evaluator: evaluator.metadata ?? { name: evaluator.name ?? "unknown" },
149
+ generation,
88
150
  budgets: { configured: input.budgets, evaluations, cost_units: costUnits, stable_comparisons: stableComparisons, duration_ms: Date.now() - startedAt },
89
151
  stopping_reason: stoppingReason,
90
152
  next_gate_action: "Run canonical AGDF gate-check; search does not grant permission.",