immune-brain 5.1.1 → 5.2.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 (53) hide show
  1. package/README.md +13 -6
  2. package/README.zh-CN.md +12 -5
  3. package/package.json +1 -1
  4. package/plugins/immune-brain/.claude-plugin/plugin.json +1 -1
  5. package/plugins/immune-brain/.pi-extension/imm-canary-enroll.ts +14 -2
  6. package/plugins/immune-brain/.pi-extension/imm-canary-work.ts +93 -0
  7. package/plugins/immune-brain/.pi-extension/imm-unattended-batch.ts +81 -13
  8. package/plugins/immune-brain/agents/immune-brain-reviewer.md +20 -0
  9. package/plugins/immune-brain/dist/BASELINE.md +1 -1
  10. package/plugins/immune-brain/dist/claude/mcp-server.mjs +8573 -7449
  11. package/plugins/immune-brain/dist/docs/reference/planning-quality-gate.md +1 -1
  12. package/plugins/immune-brain/dist/imm-brainstorm.md +6 -6
  13. package/plugins/immune-brain/dist/imm-doc-prune.md +2 -1
  14. package/plugins/immune-brain/dist/imm-planner.md +63 -37
  15. package/plugins/immune-brain/dist/imm-run.md +137 -12
  16. package/plugins/immune-brain/dist/imm-test-prune.md +233 -0
  17. package/plugins/immune-brain/dist/registry.yaml +9 -0
  18. package/plugins/immune-brain/dist/role-prompts/code-review.md +20 -0
  19. package/plugins/immune-brain/dist/role-prompts/executor.md +15 -0
  20. package/plugins/immune-brain/hooks/hooks.json +31 -1
  21. package/plugins/immune-brain/runtime/assurance/active_task_notice.ts +42 -0
  22. package/plugins/immune-brain/runtime/assurance/alias_file.ts +24 -0
  23. package/plugins/immune-brain/runtime/assurance/coordinator.ts +9 -0
  24. package/plugins/immune-brain/runtime/assurance/delivery_workspace.ts +4 -1
  25. package/plugins/immune-brain/runtime/assurance/phase_entry.ts +143 -0
  26. package/plugins/immune-brain/runtime/claude/interaction.ts +10 -1
  27. package/plugins/immune-brain/runtime/claude/kernel_ports.ts +32 -6
  28. package/plugins/immune-brain/runtime/claude/lane_guard.ts +210 -0
  29. package/plugins/immune-brain/runtime/claude/mcp_server.ts +85 -7
  30. package/plugins/immune-brain/runtime/claude/review_host.ts +6 -6
  31. package/plugins/immune-brain/runtime/commands/kernel.ts +54 -1
  32. package/plugins/immune-brain/runtime/github_issue_tracker.ts +235 -7
  33. package/plugins/immune-brain/runtime/kernel/runtime_contracts.json +7 -0
  34. package/plugins/immune-brain/runtime/kernel/runtime_contracts.ts +75 -0
  35. package/plugins/immune-brain/runtime/plugin_version.ts +1 -1
  36. package/plugins/immune-brain/runtime/prompts/code-review.md +20 -0
  37. package/plugins/immune-brain/runtime/prompts/executor.md +15 -0
  38. package/plugins/immune-brain/runtime/unattended/batch_delegation.ts +160 -0
  39. package/plugins/immune-brain/runtime/unattended/batch_final_verification.ts +72 -0
  40. package/plugins/immune-brain/runtime/unattended/batch_lanes.ts +396 -23
  41. package/plugins/immune-brain/runtime/unattended/batch_leak_restore.ts +131 -0
  42. package/plugins/immune-brain/runtime/unattended/batch_preflight.ts +25 -4
  43. package/plugins/immune-brain/runtime/unattended/batch_reasons.ts +3 -2
  44. package/plugins/immune-brain/runtime/unattended/batch_runner.ts +42 -0
  45. package/plugins/immune-brain/runtime/unattended/batch_schedule.ts +40 -0
  46. package/plugins/immune-brain/runtime/unattended/batch_state.ts +155 -0
  47. package/plugins/immune-brain/runtime/unattended/lane_workspace.ts +58 -0
  48. package/plugins/immune-brain/skills/BASELINE.md +1 -1
  49. package/plugins/immune-brain/skills/imm-brainstorm/SKILL.md +3 -2
  50. package/plugins/immune-brain/skills/imm-planner/SKILL.md +8 -6
  51. package/plugins/immune-brain/skills/imm-run/SKILL.md +2 -1
  52. package/plugins/immune-brain/skills/imm-test-prune/SKILL.md +28 -0
  53. package/plugins/immune-brain/skills/registry.yaml +9 -0
@@ -8,6 +8,8 @@ const PROTOCOL_MARKER = "<!-- immune-brain-tracker:v1 -->";
8
8
  const KIND_INITIATIVE_MARKER = "<!-- immune-brain:kind=initiative -->";
9
9
  const KIND_TASK_MARKER = "<!-- immune-brain:kind=task -->";
10
10
  const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
11
+ /** Public scenario identity: an upstream Brainstorm id or a Spec-local id. */
12
+ const SCENARIO_ID_PATTERN = /^(?:BR-SCN|SCN)-[1-9][0-9]{0,8}$/;
11
13
  const MAX_GH_OUTPUT = 8 * 1024 * 1024;
12
14
  const MAX_DIAGNOSTIC = 512;
13
15
  const GH_TIMEOUT_MS = 20_000;
@@ -62,6 +64,23 @@ export interface InitiativeProjection {
62
64
  out_of_scope?: string[];
63
65
  }
64
66
 
67
+ /**
68
+ * One user scenario projected onto the Task that first makes it observable.
69
+ * Public projection only: it never widens TaskIntent scope or authority.
70
+ */
71
+ export interface InitiativeTaskScenario {
72
+ id: string;
73
+ actor: string;
74
+ given: string;
75
+ when: string;
76
+ then: string;
77
+ mode: "automated" | "manual";
78
+ /** Canonical acceptance IDs this scenario maps to. Required for automated; optional for manual. */
79
+ acceptance?: string[];
80
+ /** Why a deterministic check cannot prove this scenario. Required for manual. */
81
+ manual_reason?: string;
82
+ }
83
+
65
84
  export interface InitiativePublicationInput {
66
85
  initiative_id: string;
67
86
  goal: string;
@@ -71,6 +90,8 @@ export interface InitiativePublicationInput {
71
90
  intent: string;
72
91
  acceptance: Array<{ id: string; summary: string }>;
73
92
  projection?: TaskProjection;
93
+ /** Optional. Omitted or empty renders no User scenarios section for this Task. */
94
+ scenarios?: InitiativeTaskScenario[];
74
95
  }>;
75
96
  /**
76
97
  * Explicit amendment input: binds the caller's desired pending frontier to
@@ -179,6 +200,15 @@ export type TrackerOperation =
179
200
  goal: string;
180
201
  slices: InitiativeSlice[];
181
202
  projection?: InitiativeProjection;
203
+ /** Derived Parent listing. Absent or empty renders no User scenarios section. */
204
+ scenarios?: Array<{
205
+ slice_id: string;
206
+ id: string;
207
+ mode: "automated" | "manual";
208
+ when: string;
209
+ then: string;
210
+ manual_reason?: string;
211
+ }>;
182
212
  }
183
213
  | {
184
214
  op: "upsert-task";
@@ -191,6 +221,8 @@ export type TrackerOperation =
191
221
  projection?: TaskProjection;
192
222
  /** The canonical TaskIntent content hash this Task was prepared from. */
193
223
  intent_hash?: string;
224
+ /** Public scenarios owned by this Task. Absent renders no User scenarios section. */
225
+ scenarios?: InitiativeTaskScenario[];
194
226
  }
195
227
  | {
196
228
  op: "mark-terminal";
@@ -270,7 +302,7 @@ function countLiteral(value: string, needle: string): number {
270
302
  return count;
271
303
  }
272
304
 
273
- function marker(name: "repo-id" | "initiative-id" | "task-id" | "slice-id" | "intent-hash", value: string | number): string {
305
+ function marker(name: "repo-id" | "initiative-id" | "task-id" | "slice-id" | "intent-hash" | "scenarios", value: string | number): string {
274
306
  return `<!-- immune-brain:${name}=${value} -->`;
275
307
  }
276
308
 
@@ -730,8 +762,9 @@ function stripPublishedIntentHash(body: string): string {
730
762
  return body.replace(/<!-- immune-brain:intent-hash=[A-Za-z0-9][A-Za-z0-9._:-]{0,127} -->\n?/g, "");
731
763
  }
732
764
 
733
- function ownershipMarkerValue(body: string, name: "initiative-id" | "slice-id" | "task-id"): string | null {
734
- const values = [...body.matchAll(new RegExp(`<!-- immune-brain:${name}=([A-Za-z0-9][A-Za-z0-9._-]{0,127}) -->`, "g"))];
765
+ function ownershipMarkerValue(body: string, name: "initiative-id" | "slice-id" | "task-id" | "scenarios"): string | null {
766
+ const pattern = name === "scenarios" ? "[A-Za-z0-9%_!*'()~.-]+" : "[A-Za-z0-9][A-Za-z0-9._-]{0,127}";
767
+ const values = [...body.matchAll(new RegExp(`<!-- immune-brain:${name}=(${pattern}) -->`, "g"))];
735
768
  return values.length === 1 ? values[0][1] : null;
736
769
  }
737
770
 
@@ -953,6 +986,160 @@ function listText(values: string[] | undefined, fallback: string): string {
953
986
  return values?.length ? values.map((value) => `- ${value}`).join("\n") : fallback;
954
987
  }
955
988
 
989
+ /**
990
+ * Child section after Acceptance criteria. Empty input returns "" so a Task
991
+ * without scenarios, and a publication with none at all, stays byte-identical.
992
+ */
993
+ function childScenarioSection(scenarios: InitiativeTaskScenario[] | undefined): string {
994
+ if (!scenarios?.length) return "";
995
+ const items = scenarios.map((scenario) => {
996
+ const mapped = scenario.acceptance?.length ? scenario.acceptance.map((id) => `\`${id}\``).join(", ") : "none";
997
+ const visible = `Actor: ${scenario.actor}. Given: ${scenario.given}. When: ${scenario.when}. Then: ${scenario.then}.`;
998
+ if (scenario.mode === "manual")
999
+ return `- [ ] \`${scenario.id}\` (manual, ${mapped}): ${visible} Reason: ${scenario.manual_reason}.`;
1000
+ return `- \`${scenario.id}\` (automated, ${mapped}): ${visible}`;
1001
+ });
1002
+ return `\n## User scenarios\n\n${items.join("\n")}\n`;
1003
+ }
1004
+
1005
+ /**
1006
+ * Parent listing after Testing strategy, derived from the Tasks. One plain
1007
+ * bullet per scenario; no checkboxes. Empty input returns "".
1008
+ */
1009
+ function parentScenarioSection(scenarios: Array<{ slice_id: string; id: string; mode: "automated" | "manual"; actor?: string; given?: string; when: string; then: string; manual_reason?: string }> | undefined): string {
1010
+ if (!scenarios?.length) return "";
1011
+ const items = scenarios.map((scenario) => {
1012
+ const prefix = scenario.actor && scenario.given ? `Actor: ${scenario.actor}. Given: ${scenario.given}. ` : "";
1013
+ const reason = scenario.manual_reason ? ` Reason: ${scenario.manual_reason}.` : "";
1014
+ return `- \`${scenario.id}\` (Slice \`${scenario.slice_id}\`, ${scenario.mode}): ${prefix}When: ${scenario.when}. Then: ${scenario.then}.${reason}`;
1015
+ });
1016
+ return `\n## User scenarios\n\n${items.join("\n")}\n`;
1017
+ }
1018
+
1019
+
1020
+ type HistoricalScenarioListing = InitiativeTaskScenario & { slice_id: string };
1021
+
1022
+ /**
1023
+ * Read the Parent listings already published by historical Children. Their
1024
+ * Child bytes stay untouched, but an amendment must keep those listings and
1025
+ * reserve their IDs before it rebuilds the Parent or accepts pending input.
1026
+ */
1027
+ function historicalScenarioListings(children: Iterable<{ body?: unknown }>): HistoricalScenarioListing[] | string {
1028
+ const listings: HistoricalScenarioListing[] = [];
1029
+ for (const child of children) {
1030
+ if (typeof child.body !== "string") continue;
1031
+ const sliceId = ownershipMarkerValue(child.body, "slice-id");
1032
+ if (!sliceId) continue;
1033
+ const markerStarts = [...child.body.matchAll(/<!-- immune-brain:scenarios=/g)].length;
1034
+ if (markerStarts > 1) return `historical Slice ${sliceId} has duplicate scenarios markers`;
1035
+ const encoded = ownershipMarkerValue(child.body, "scenarios");
1036
+ if (encoded === null) {
1037
+ if (markerStarts === 0) continue;
1038
+ return `historical Slice ${sliceId} has a malformed scenarios marker`;
1039
+ }
1040
+ const malformed = `historical Slice ${sliceId} has a malformed scenarios marker`;
1041
+ let decoded: unknown;
1042
+ try { decoded = JSON.parse(decodeURIComponent(encoded)); }
1043
+ catch { return malformed; }
1044
+ let scenarios: InitiativeTaskScenario[] | undefined;
1045
+ try { scenarios = normalizeTaskScenarios(decoded, `historical Slice ${sliceId}`, undefined, new Set()); }
1046
+ catch { return malformed; }
1047
+ // The marker is the only source of historical scenarios, so it must hold
1048
+ // exactly what the publish path writes for these values: the same
1049
+ // validator, re-encoded byte for byte. Anything else is an unamendable
1050
+ // Child, and the batch is rejected before any remote write.
1051
+ if (scenarios === undefined || encodeURIComponent(JSON.stringify(scenarios)) !== encoded) return malformed;
1052
+ for (const scenario of scenarios) listings.push({ ...scenario, slice_id: sliceId });
1053
+ }
1054
+ return listings;
1055
+ }
1056
+
1057
+ function retainHistoricalScenarioIds(children: Iterable<{ body?: unknown }>, seenScenarioIds = new Set<string>()): string | null {
1058
+ const listings = historicalScenarioListings(children);
1059
+ if (typeof listings === "string") return listings;
1060
+ for (const listing of listings) {
1061
+ if (seenScenarioIds.has(listing.id)) return `historical Slice ${listing.slice_id} repeats scenario id ${listing.id}`;
1062
+ seenScenarioIds.add(listing.id);
1063
+ }
1064
+ return null;
1065
+ }
1066
+
1067
+ /**
1068
+ * Reject the whole batch, before any remote write, when a scenario is malformed
1069
+ * or names an acceptance ID its TaskIntent lacks. Text uses the same public
1070
+ * projection rules as every other projected field. When `acceptanceIds` is
1071
+ * omitted the TaskIntent-bound existence check is skipped, which is what the
1072
+ * historical read-back needs: a published marker already passed it once.
1073
+ */
1074
+ function normalizeTaskScenarios(
1075
+ value: unknown,
1076
+ taskId: string,
1077
+ acceptanceIds: Set<string> | undefined,
1078
+ seenScenarioIds: Set<string>,
1079
+ ): InitiativeTaskScenario[] | undefined {
1080
+ if (value === undefined) return undefined;
1081
+ if (!Array.isArray(value)) throw new Error(`Task ${taskId} scenarios must be an array`);
1082
+ if (value.length === 0) return undefined;
1083
+ return value.map((item, index) => {
1084
+ const name = `Task ${taskId} scenarios[${index}]`;
1085
+ if (!item || typeof item !== "object" || Array.isArray(item)) throw new Error(`${name} must be an object`);
1086
+ const raw = item as Record<string, unknown>;
1087
+ const required = ["id", "actor", "given", "when", "then", "mode"] as const;
1088
+ for (const field of required) {
1089
+ if (raw[field] === undefined) throw new Error(`${name} is missing ${field}`);
1090
+ }
1091
+ const id = typeof raw.id === "string" && SCENARIO_ID_PATTERN.test(raw.id) ? raw.id : null;
1092
+ if (!id) throw new Error(`${name} id must match BR-SCN-<n> or SCN-<n>`);
1093
+ if (seenScenarioIds.has(id)) throw new Error(`Task ${taskId} repeats scenario id ${id}`);
1094
+ seenScenarioIds.add(id);
1095
+ if (raw.mode !== "automated" && raw.mode !== "manual")
1096
+ throw new Error(`${name} mode must be automated or manual`);
1097
+ const acceptance = raw.acceptance === undefined
1098
+ ? undefined
1099
+ : normalizedList(raw.acceptance, `${name}.acceptance`, 128)?.map((acceptanceId) => identifier(acceptanceId, `${name}.acceptance`));
1100
+ if (acceptance && new Set(acceptance).size !== acceptance.length)
1101
+ throw new Error(`${name} repeats an acceptance id`);
1102
+ for (const acceptanceId of acceptance ?? []) {
1103
+ if (acceptanceIds && !acceptanceIds.has(acceptanceId))
1104
+ throw new Error(`Task ${taskId} scenario ${id} names unknown acceptance id ${acceptanceId}`);
1105
+ }
1106
+ if (raw.mode === "automated" && !acceptance?.length)
1107
+ throw new Error(`Task ${taskId} scenario ${id} is automated with no acceptance id`);
1108
+ if (raw.mode === "manual" && raw.manual_reason === undefined)
1109
+ throw new Error(`Task ${taskId} scenario ${id} is manual without manual_reason`);
1110
+ const scenario: InitiativeTaskScenario = {
1111
+ id,
1112
+ actor: projectionText(raw.actor, `${name}.actor`, 500),
1113
+ given: projectionText(raw.given, `${name}.given`, 500),
1114
+ when: projectionText(raw.when, `${name}.when`, 500),
1115
+ then: projectionText(raw.then, `${name}.then`, 500),
1116
+ mode: raw.mode,
1117
+ ...(acceptance ? { acceptance } : {}),
1118
+ ...(raw.manual_reason === undefined ? {} : { manual_reason: projectionText(raw.manual_reason, `${name}.manual_reason`, 500) }),
1119
+ };
1120
+ // The historical read-back re-applies these rules to the stored values, so
1121
+ // each one must survive its own projection a second time. Escaping can grow
1122
+ // a value past its bound and a restricted-context acceptance ID passes the
1123
+ // pending path; either would make a published Child unamendable.
1124
+ const roundTrip: Array<[string, string]> = [
1125
+ ["actor", scenario.actor],
1126
+ ["given", scenario.given],
1127
+ ["when", scenario.when],
1128
+ ["then", scenario.then],
1129
+ ...(scenario.manual_reason === undefined ? [] : [["manual_reason", scenario.manual_reason] as [string, string]]),
1130
+ ];
1131
+ for (const [label, value] of roundTrip) {
1132
+ if (projectionText(value, `${name}.${label}`, 500) !== value)
1133
+ throw new Error(`${name}.${label} fails the public-projection round-trip`);
1134
+ }
1135
+ for (const acceptanceId of acceptance ?? []) {
1136
+ if (projectionText(acceptanceId, `${name}.acceptance`, 128) !== acceptanceId || identifier(acceptanceId, `${name}.acceptance`) !== acceptanceId)
1137
+ throw new Error(`${name}.acceptance fails the public-projection round-trip`);
1138
+ }
1139
+ return scenario;
1140
+ });
1141
+ }
1142
+
956
1143
  function bodyLimitFailure(operation: TrackerOperation["op"], body: string, reserve = 0): GithubTrackerResult | null {
957
1144
  return Buffer.byteLength(body, "utf8") + reserve <= GITHUB_ISSUE_BODY_LIMIT
958
1145
  ? null
@@ -973,7 +1160,7 @@ function createInitiativeBody(
973
1160
  KIND_INITIATIVE_MARKER,
974
1161
  marker("repo-id", repository.id),
975
1162
  marker("initiative-id", operation.initiative_id),
976
- ].join("\n")}\n\n${provenance}## How to use this Issue\n\n- Edit planning prose and Slice ordering directly after creation.\n- Keep each Slice marker attached to exactly one stable Slice entry.\n- The tracker never rewrites this Parent after creation; the tracker closes it as completed once every Slice Child is completed, and never closes it otherwise.\n\n## Problem\n\n${publicText(projection.problem ?? "The Initiative addresses the bounded delivery described below.", "projection.problem")}\n\n## Result\n\n${publicText(projection.result ?? operation.goal, "projection.result")}\n\n## Initiative design\n\n${publicText(projection.design ?? "Each Child preserves the shared Initiative decisions and boundaries recorded here.", "projection.design")}\n\n## Decisions\n\n${listText(projection.decisions, "- No additional Initiative decisions recorded.")}\n\n## Testing strategy\n\n${publicText(projection.testing_strategy ?? "Each Child closes from its focused acceptance verification.", "projection.testing_strategy")}\n\n## Out of scope\n\n${listText(projection.out_of_scope, "- Unrelated work outside this Initiative.")}\n\n## Slices\n\n${operation.slices.length + historicalSlices.length === 0 ? "No Slices recorded yet." : [...historicalSlices, ...operation.slices.map((slice) => `- [ ] ${marker("slice-id", slice.id)} **${slice.id}**: ${slice.result ?? slice.goal}${slice.blocked_by?.length ? ` (blocked by: ${slice.blocked_by.join(", ")})` : ""}`)].join("\n")}\n\n${ISSUE_FOOTER}\n`;
1163
+ ].join("\n")}\n\n${provenance}## How to use this Issue\n\n- Edit planning prose and Slice ordering directly after creation.\n- Keep each Slice marker attached to exactly one stable Slice entry.\n- The tracker never rewrites this Parent after creation; the tracker closes it as completed once every Slice Child is completed, and never closes it otherwise.\n\n## Problem\n\n${publicText(projection.problem ?? "The Initiative addresses the bounded delivery described below.", "projection.problem")}\n\n## Result\n\n${publicText(projection.result ?? operation.goal, "projection.result")}\n\n## Initiative design\n\n${publicText(projection.design ?? "Each Child preserves the shared Initiative decisions and boundaries recorded here.", "projection.design")}\n\n## Decisions\n\n${listText(projection.decisions, "- No additional Initiative decisions recorded.")}\n\n## Testing strategy\n\n${publicText(projection.testing_strategy ?? "Each Child closes from its focused acceptance verification.", "projection.testing_strategy")}\n${parentScenarioSection(operation.scenarios)}\n## Out of scope\n\n${listText(projection.out_of_scope, "- Unrelated work outside this Initiative.")}\n\n## Slices\n\n${operation.slices.length + historicalSlices.length === 0 ? "No Slices recorded yet." : [...historicalSlices, ...operation.slices.map((slice) => `- [ ] ${marker("slice-id", slice.id)} **${slice.id}**: ${slice.result ?? slice.goal}${slice.blocked_by?.length ? ` (blocked by: ${slice.blocked_by.join(", ")})` : ""}`)].join("\n")}\n\n${ISSUE_FOOTER}\n`;
977
1164
  }
978
1165
 
979
1166
  async function createInitiative(
@@ -1056,7 +1243,8 @@ function childBody(
1056
1243
  // idempotent replay can compare against what was actually published
1057
1244
  // instead of against whatever bytes it happens to read this time.
1058
1245
  ...(operation.intent_hash ? [marker("intent-hash", operation.intent_hash)] : []),
1059
- ].join("\n")}\n\n## Parent\n\n| Initiative | \`${operation.initiative_id}\` |\n| Parent Issue | [#${parent.number}](${parent.url}) |\n| Slice | \`${operation.slice_id}\` |\n| Risk | \`${operation.risk}\` |\n\n## Current behavior\n\n${publicText(projection.current_behavior ?? "The current behavior is defined by the repository's existing contract.", "projection.current_behavior")}\n\n## Desired behavior\n\n${publicText(projection.desired_behavior ?? projection.result ?? operation.goal, "projection.desired_behavior")}\n\n## Key interfaces\n\n${listText(projection.key_interfaces, "- Canonical TaskIntent acceptance and Kernel lifecycle remain authoritative.")}\n\n## Acceptance criteria\n\n${acceptance}\n\n## Verification\n\n${publicText(projection.verification ?? "Run the focused acceptance verification declared by the TaskIntent.", "projection.verification")}\n\n## Blocked by\n\n${projection.blocked_by?.length ? projection.blocked_by.map((id) => `- \`${identifier(id, "blocked_by task_id")}\``).join("\n") : "None"}\n\n## Out of scope\n\n${listText(projection.out_of_scope, "- Scope not declared by the validated TaskIntent.")}\n\n## Agent handoff\n\n${publicText(projection.agent_handoff ?? "Implement only the bounded TaskIntent result and run the focused checks. Do not widen scope or treat GitHub as authorization.", "projection.agent_handoff")}\n\n${ISSUE_FOOTER}\n`;
1246
+ ...(operation.scenarios?.length ? [marker("scenarios", encodeURIComponent(JSON.stringify(operation.scenarios)))] : []),
1247
+ ].join("\n")}\n\n## Parent\n\n| Initiative | \`${operation.initiative_id}\` |\n| Parent Issue | [#${parent.number}](${parent.url}) |\n| Slice | \`${operation.slice_id}\` |\n| Risk | \`${operation.risk}\` |\n\n## Current behavior\n\n${publicText(projection.current_behavior ?? "The current behavior is defined by the repository's existing contract.", "projection.current_behavior")}\n\n## Desired behavior\n\n${publicText(projection.desired_behavior ?? projection.result ?? operation.goal, "projection.desired_behavior")}\n\n## Key interfaces\n\n${listText(projection.key_interfaces, "- Canonical TaskIntent acceptance and Kernel lifecycle remain authoritative.")}\n\n## Acceptance criteria\n\n${acceptance}\n${childScenarioSection(operation.scenarios)}\n## Verification\n\n${publicText(projection.verification ?? "Run the focused acceptance verification declared by the TaskIntent.", "projection.verification")}\n\n## Blocked by\n\n${projection.blocked_by?.length ? projection.blocked_by.map((id) => `- \`${identifier(id, "blocked_by task_id")}\``).join("\n") : "None"}\n\n## Out of scope\n\n${listText(projection.out_of_scope, "- Scope not declared by the validated TaskIntent.")}\n\n## Agent handoff\n\n${publicText(projection.agent_handoff ?? "Implement only the bounded TaskIntent result and run the focused checks. Do not widen scope or treat GitHub as authorization.", "projection.agent_handoff")}\n\n${ISSUE_FOOTER}\n`;
1060
1248
  }
1061
1249
 
1062
1250
  /** Derived approved-final content and baseline-derived historical evidence for an amendment. */
@@ -1094,6 +1282,26 @@ function approvedAmendmentContent(
1094
1282
  blocked_by: operation.projection?.blocked_by,
1095
1283
  })),
1096
1284
  }) as Extract<TrackerOperation, { op: "create-initiative" }>;
1285
+ const historicalScenarios = historicalScenarioListings([...amendment.historical.values()].map((binding) => ({ body: binding.body })));
1286
+ if (typeof historicalScenarios === "string") return historicalScenarios;
1287
+ parent.scenarios = [
1288
+ ...historicalScenarios.map((scenario) => ({
1289
+ slice_id: scenario.slice_id,
1290
+ id: scenario.id,
1291
+ mode: scenario.mode,
1292
+ when: scenario.when,
1293
+ then: scenario.then,
1294
+ ...(typeof scenario.manual_reason === "string" ? { manual_reason: scenario.manual_reason } : {}),
1295
+ })),
1296
+ ...prepared.order.flatMap((operation) =>
1297
+ (operation.scenarios ?? []).map((scenario) => ({
1298
+ slice_id: operation.slice_id,
1299
+ id: scenario.id,
1300
+ mode: scenario.mode,
1301
+ when: scenario.when,
1302
+ then: scenario.then,
1303
+ }))),
1304
+ ];
1097
1305
  const historicalSliceIds: Set<string> = new Set();
1098
1306
  for (const child of amendment.historical.values()) {
1099
1307
  const sliceId = ownershipMarkerValue(child.body, "slice-id");
@@ -1120,7 +1328,7 @@ function approvedAmendmentContent(
1120
1328
  void sourceStub;
1121
1329
  // The approved final Parent body fixes the Slices checklist, so every Child
1122
1330
  // title is numbered from the Initiative order rather than from this batch.
1123
- const parentBody = createInitiativeBody(sourceStub.repository, parent, historicalSlices);
1331
+ const parentBody = createInitiativeBody(sourceStub.repository, { ...parent, scenarios: parent.scenarios }, historicalSlices);
1124
1332
  const oversizedParent = bodyLimitFailure("create-initiative", parentBody);
1125
1333
  if (oversizedParent) return `${oversizedParent.status}: ${oversizedParent.message}`;
1126
1334
  const pendingContent = new Map<string, { title: string; body: string }>();
@@ -1587,6 +1795,7 @@ function validateOperation(operation: TrackerOperation): TrackerOperation {
1587
1795
  id: identifier(item.id, `acceptance[${index}].id`),
1588
1796
  summary: projectionText(item.summary, `acceptance[${index}].summary`, 500),
1589
1797
  })),
1798
+ scenarios: operation.scenarios,
1590
1799
  };
1591
1800
  taskIssueTitle(normalized);
1592
1801
  return normalized;
@@ -1727,10 +1936,16 @@ function preflightPublication(root: string, input: InitiativePublicationInput):
1727
1936
  // projection is stamped with them, so a missing or oversized display name
1728
1937
  // fails the batch closed with zero remote writes.
1729
1938
  const initiativeShortName = initiativeDisplayNames(input.projection).shortName;
1939
+ const seenScenarioIds = new Set<string>();
1940
+ if (input.amendment) {
1941
+ const historicalBindings = (input.amendment.historical ?? []).map((child) => (child as { binding?: { body?: unknown } }).binding ?? {});
1942
+ const historicalScenarioError = retainHistoricalScenarioIds(historicalBindings, seenScenarioIds);
1943
+ if (historicalScenarioError) throw new Error(historicalScenarioError);
1944
+ }
1730
1945
  const publications = input.tasks.map((task, index) => {
1731
1946
  if (!task || typeof task !== "object" || Array.isArray(task)) throw new Error(`tasks[${index}] must be an object`);
1732
1947
  if (typeof task.intent !== "string") throw new Error(`tasks[${index}].intent must be a string`);
1733
- return taskPublication(root, input.initiative_id, task.slice_id, task.intent, task.acceptance, task.projection, index + 1, initiativeShortName);
1948
+ return taskPublication(root, input.initiative_id, task.slice_id, task.intent, task.acceptance, task.projection, task.scenarios, index + 1, initiativeShortName, seenScenarioIds);
1734
1949
  });
1735
1950
  const historicalIds = new Set<string>();
1736
1951
  if (input.amendment) {
@@ -1772,6 +1987,15 @@ function preflightPublication(root: string, input: InitiativePublicationInput):
1772
1987
  blocked_by: operation.projection?.blocked_by,
1773
1988
  })),
1774
1989
  }) as Extract<TrackerOperation, { op: "create-initiative" }>;
1990
+ initiative.scenarios = operations.flatMap((operation) =>
1991
+ (operation.scenarios ?? []).map((scenario) => ({
1992
+ slice_id: operation.slice_id,
1993
+ id: scenario.id,
1994
+ mode: scenario.mode,
1995
+ when: scenario.when,
1996
+ then: scenario.then,
1997
+ })),
1998
+ );
1775
1999
  const intentBindings = new Map(publications.map((publication) => [publication.operation.task_id, {
1776
2000
  intent_path: publication.intent_path,
1777
2001
  intent_content_hash: publication.intent_content_hash,
@@ -2874,8 +3098,10 @@ function taskPublication(
2874
3098
  intentPath: string,
2875
3099
  acceptance: unknown,
2876
3100
  projection: TaskProjection | undefined,
3101
+ scenarios: unknown,
2877
3102
  ordinal: number,
2878
3103
  initiativeShortName: string,
3104
+ seenScenarioIds: Set<string>,
2879
3105
  ): PreparedPublicationTask {
2880
3106
  const absoluteRoot = resolve(root);
2881
3107
  const absolutePath = resolve(absoluteRoot, intentPath);
@@ -2901,6 +3127,7 @@ function taskPublication(
2901
3127
  });
2902
3128
  const missingIds = [...expectedIds].filter((id) => !publicById.has(id));
2903
3129
  if (missingIds.length) throw new Error(`Task ${taskId} is missing public acceptance ids: ${missingIds.join(", ")}`);
3130
+ const normalizedScenarios = normalizeTaskScenarios(scenarios, taskId, expectedIds, seenScenarioIds);
2904
3131
  // Display names are stamped from the Initiative so every Child title carries
2905
3132
  // the same short name and its declared Slice position; an explicitly
2906
3133
  // supplied value must agree, never silently override.
@@ -2924,6 +3151,7 @@ function taskPublication(
2924
3151
  acceptance: intent.acceptance.map((item) => publicById.get(item.id)!),
2925
3152
  projection: stampedProjection,
2926
3153
  intent_hash: read.content_hash,
3154
+ scenarios: normalizedScenarios,
2927
3155
  }) as Extract<TrackerOperation, { op: "upsert-task" }>,
2928
3156
  intent_path: read.intent_ref.path,
2929
3157
  intent_content_hash: read.content_hash,
@@ -0,0 +1,7 @@
1
+ {
2
+ "contract": "immune_brain/runtime_contracts/v1",
3
+ "task_record": "assurance_kernel/task_record/v4",
4
+ "task_tombstone": "assurance_kernel/task_tombstone/v2",
5
+ "assurance_projection": "assurance_kernel/assurance_projection/v1",
6
+ "kernel_store_schema": 1
7
+ }
@@ -0,0 +1,75 @@
1
+ // The contract identifiers one runtime reads and writes in a worktree's
2
+ // Authority Store. A lane-mode batch reads every Lane's Kernel state with the
3
+ // coordinator's own runtime, so the coordinator and the runtime a Lane
4
+ // Executor loads must agree on these identifiers; a plugin version number
5
+ // proves nothing (two different builds can carry the same one).
6
+ //
7
+ // `runtime_contracts.json` is the same set as data, shipped next to this
8
+ // module, so a coordinator can read another runtime's identifiers without
9
+ // executing its code. A test keeps the two in step.
10
+ import { existsSync, readFileSync } from "node:fs";
11
+ import { dirname, join, resolve } from "node:path";
12
+ import { fileURLToPath } from "node:url";
13
+ import { TASK_TOMBSTONE_CONTRACT } from "./backend_claim";
14
+ import { KERNEL_STORE_SCHEMA_VERSION } from "./storage_paths";
15
+ import { TASK_RECORD_CONTRACT_V4 } from "./types";
16
+
17
+ export const RUNTIME_CONTRACTS_MANIFEST = "runtime_contracts.json";
18
+
19
+ export interface RuntimeContracts {
20
+ contract: "immune_brain/runtime_contracts/v1";
21
+ task_record: string;
22
+ task_tombstone: string;
23
+ assurance_projection: string;
24
+ kernel_store_schema: number;
25
+ }
26
+
27
+ /** The identifiers this running runtime supports. */
28
+ export const RUNTIME_CONTRACTS: RuntimeContracts = {
29
+ contract: "immune_brain/runtime_contracts/v1",
30
+ task_record: TASK_RECORD_CONTRACT_V4,
31
+ task_tombstone: TASK_TOMBSTONE_CONTRACT,
32
+ assurance_projection: "assurance_kernel/assurance_projection/v1",
33
+ kernel_store_schema: KERNEL_STORE_SCHEMA_VERSION,
34
+ };
35
+
36
+ const KEYS = ["task_record", "task_tombstone", "assurance_projection", "kernel_store_schema"] as const;
37
+
38
+ /** Where this running runtime's code was loaded from, for a refusal that names both sides. */
39
+ export function runningRuntimeSource(): string {
40
+ try {
41
+ return dirname(fileURLToPath(import.meta.url));
42
+ } catch {
43
+ return "unknown";
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Read another runtime's identifiers from its manifest. `runtimeDir` is either
49
+ * the `runtime/kernel` directory or a plugin root that contains one. Null when
50
+ * no manifest exists there; a malformed manifest throws.
51
+ */
52
+ export function readRuntimeContracts(runtimeDir: string): { contracts: RuntimeContracts; path: string } | null {
53
+ const candidates = [
54
+ join(runtimeDir, RUNTIME_CONTRACTS_MANIFEST),
55
+ join(runtimeDir, "kernel", RUNTIME_CONTRACTS_MANIFEST),
56
+ join(runtimeDir, "runtime", "kernel", RUNTIME_CONTRACTS_MANIFEST),
57
+ join(runtimeDir, "plugins", "immune-brain", "runtime", "kernel", RUNTIME_CONTRACTS_MANIFEST),
58
+ ];
59
+ const path = candidates.find((candidate) => existsSync(candidate));
60
+ if (!path) return null;
61
+ const raw = JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
62
+ if (raw.contract !== "immune_brain/runtime_contracts/v1")
63
+ throw new Error(`${path} is not an immune_brain/runtime_contracts/v1 manifest`);
64
+ for (const key of KEYS) {
65
+ const value = raw[key];
66
+ if (key === "kernel_store_schema" ? !Number.isSafeInteger(value) : typeof value !== "string" || !value)
67
+ throw new Error(`${path} has an invalid ${key}`);
68
+ }
69
+ return { contracts: raw as unknown as RuntimeContracts, path: resolve(path) };
70
+ }
71
+
72
+ /** The identifiers on which two runtimes disagree; empty when they can read each other's state. */
73
+ export function runtimeContractDifferences(a: RuntimeContracts, b: RuntimeContracts): string[] {
74
+ return KEYS.filter((key) => a[key] !== b[key]).map((key) => `${key}: ${String(a[key])} != ${String(b[key])}`);
75
+ }
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/plugin_versioning.ts from the root package.json.
2
- export const PLUGIN_VERSION = "5.1.1" as const;
2
+ export const PLUGIN_VERSION = "5.2.0" as const;
@@ -26,6 +26,11 @@ material task-local maintenance risks. Pure naming, length, complexity
26
26
  thresholds, formatting, and design preference are not findings and must not
27
27
  cause style-only rework.
28
28
 
29
+ Test-suite growth is advisory, never blocking: note a delivered test that
30
+ duplicates an existing assertion, asserts only private implementation, or
31
+ leaves a test protecting behavior the change retired. Weakened or missing
32
+ tests for an acceptance remain blocking under the rules above.
33
+
29
34
  When a finding repeats an acceptance id or anchor that already produced rework in
30
35
  this task, do not file it as a fresh single-trigger defect. Name the invariant
31
36
  class it belongs to and state in `evidence.trigger` why the previously accepted
@@ -34,6 +39,13 @@ about the fix's scope, not a new bug. A trigger the accepted contract explicitly
34
39
  excludes is reported as an advisory note against that boundary, never as blocking
35
40
  rework.
36
41
 
42
+ A defect that lies inside the TaskIntent goal and `scope_hint` is a blocking
43
+ finding even when no acceptance assertion names its exact case word for word:
44
+ cite the acceptance whose principle it violates as `acceptance_id` and
45
+ `violated.ref`. Ask for the fix and a test that pins the case, never for the case
46
+ to be written into the acceptance text; a missing literal clause is not itself a
47
+ finding and does not call for an Intent revision.
48
+
37
49
  ## Invariant and Evidence Coverage
38
50
 
39
51
  Judge whether the change actually closes the invariant its acceptance names,
@@ -53,6 +65,14 @@ seam, or a seam silently replaced by a different one, is a finding carrying
53
65
  named, its absence is not a finding: candidates written before this rule carries
54
66
  no defect for lacking one.
55
67
 
68
+ When the Spec referenced by an acceptance maps an `automated` user scenario to
69
+ it, check that the delivered tests at that acceptance's agreed seam assert the
70
+ scenario's `Then`: a scenario whose observable result is asserted nowhere is a
71
+ finding carrying `violated.kind` of `acceptance` with `ref` the acceptance id.
72
+ A `manual` scenario, and an acceptance with no mapped scenario, produce no such
73
+ finding. This adds no finding kind and no verdict branch: it is the same
74
+ `acceptance` finding the seam rule above already defines.
75
+
56
76
  Before pass, enumerate the immutable change set and read each complete per-file
57
77
  diff, including deletions. Page bounded reads until every hunk is visible; a
58
78
  truncated aggregate output or summary is incomplete coverage. Trace changed
@@ -54,6 +54,13 @@ Record missing evidence as blocked and repair it before handoff. The Loop owns
54
54
  verdict submission, finding disposition and the second-rework generalize-or-refute
55
55
  rule.
56
56
 
57
+ A finding whose defect lies inside the TaskIntent goal and `scope_hint` is
58
+ ordinary rework even when no acceptance names its exact case: fix the code and
59
+ add the test that pins the case. Do not propose an Intent revision for it, and
60
+ never append a single input case to an acceptance assertion to close a finding;
61
+ acceptance states principles, tests enumerate cases. Route to `imm-planner` only
62
+ when the goal, the scope, or a user-visible commitment must change.
63
+
57
64
  ## Code Quality Guard
58
65
 
59
66
  Before handoff, check the implementation for real implementation rather than
@@ -61,6 +68,14 @@ mock or hard-coded success, swallowed unexpected errors, missing validation at
61
68
  external trust boundaries, invented dependencies or APIs, unauthorized
62
69
  observable behavior changes, and production paths without a current caller.
63
70
  Do not weaken tests or hide an incomplete result to make Verification pass.
71
+
72
+ Keep the test suite lean. Each added test pins one acceptance invariant or
73
+ finding case at its public seam; do not mirror private calls, mock order or
74
+ internal return values that a public assertion already covers. Before adding a
75
+ test, search existing tests for the same behavior and extend or parameterize
76
+ them instead of duplicating setup. When the change retires or replaces a
77
+ behavior, update or delete the tests that protected it within scope and name
78
+ the retired behavior or continuing owner in Delivery Evidence.
64
79
  Treat naming, function length, parameter count, nesting, and abstraction taste
65
80
  as contextual signals, never as automatic failure thresholds.
66
81