immune-brain 2.8.2

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 (101) hide show
  1. package/README.md +239 -0
  2. package/README.zh-CN.md +239 -0
  3. package/package.json +84 -0
  4. package/plugins/immune-brain/.pi-extension/imm-canary-enroll.ts +666 -0
  5. package/plugins/immune-brain/.pi-extension/imm-canary-work.ts +1622 -0
  6. package/plugins/immune-brain/.pi-extension/package.json +11 -0
  7. package/plugins/immune-brain/.pi-extension/pi-canary-assurance-progression.ts +711 -0
  8. package/plugins/immune-brain/.pi-extension/pi-canary-assurance.ts +154 -0
  9. package/plugins/immune-brain/.pi-extension/pi-canary-interaction.ts +349 -0
  10. package/plugins/immune-brain/.pi-extension/pi-canary-invocations.ts +90 -0
  11. package/plugins/immune-brain/.pi-extension/pi-canary-native-review.ts +214 -0
  12. package/plugins/immune-brain/.pi-extension/pi-canary-qa-findings.ts +50 -0
  13. package/plugins/immune-brain/.pi-extension/pi-canary-review-bundle.ts +262 -0
  14. package/plugins/immune-brain/.pi-extension/pi-canary-tool-failure.ts +28 -0
  15. package/plugins/immune-brain/.pi-extension/pi-canary-verification.ts +237 -0
  16. package/plugins/immune-brain/.pi-extension/runtime-stub.ts +414 -0
  17. package/plugins/immune-brain/.pi-extension/tsconfig.json +20 -0
  18. package/plugins/immune-brain/bin/imm-kernel +4 -0
  19. package/plugins/immune-brain/bin/imm-plan +4 -0
  20. package/plugins/immune-brain/bin/imm-pr-diag +230 -0
  21. package/plugins/immune-brain/bin/imm-retire-stale-wrapper +4 -0
  22. package/plugins/immune-brain/bin/imm-retired +4 -0
  23. package/plugins/immune-brain/bin/imm-tracker +4 -0
  24. package/plugins/immune-brain/dist/BASELINE.md +138 -0
  25. package/plugins/immune-brain/dist/docs/reference/HANDOFF-template.md +122 -0
  26. package/plugins/immune-brain/dist/docs/reference/design-contract-audit-rubric.md +149 -0
  27. package/plugins/immune-brain/dist/docs/reference/design-contract-review-checklist.md +55 -0
  28. package/plugins/immune-brain/dist/docs/reference/i18n-review-checklist.md +110 -0
  29. package/plugins/immune-brain/dist/docs/reference/immune-brain-config.md +52 -0
  30. package/plugins/immune-brain/dist/docs/reference/planning-artifact-retention.md +94 -0
  31. package/plugins/immune-brain/dist/docs/reference/planning-quality-gate.md +44 -0
  32. package/plugins/immune-brain/dist/docs/reference/subagent-dispatch-protocol.md +105 -0
  33. package/plugins/immune-brain/dist/docs/reference/ux-heuristic-checklist.md +131 -0
  34. package/plugins/immune-brain/dist/imm-brainstorm.md +140 -0
  35. package/plugins/immune-brain/dist/imm-doc-prune.md +137 -0
  36. package/plugins/immune-brain/dist/imm-loop.md +158 -0
  37. package/plugins/immune-brain/dist/imm-planner.md +387 -0
  38. package/plugins/immune-brain/dist/imm-pr-fix.md +71 -0
  39. package/plugins/immune-brain/dist/registry.yaml +49 -0
  40. package/plugins/immune-brain/dist/role-prompts/advisory-reviewer.md +16 -0
  41. package/plugins/immune-brain/dist/role-prompts/arch-explorer.md +14 -0
  42. package/plugins/immune-brain/dist/role-prompts/code-review.md +15 -0
  43. package/plugins/immune-brain/dist/role-prompts/compounder.md +20 -0
  44. package/plugins/immune-brain/dist/role-prompts/executor.md +13 -0
  45. package/plugins/immune-brain/dist/role-prompts/pr-fix.md +81 -0
  46. package/plugins/immune-brain/dist/role-prompts/qa.md +23 -0
  47. package/plugins/immune-brain/dist/role-prompts/test-fixer.md +3 -0
  48. package/plugins/immune-brain/dist/role-prompts/ui-review.md +14 -0
  49. package/plugins/immune-brain/runtime/authority_commit_receipts.ts +716 -0
  50. package/plugins/immune-brain/runtime/canonical_json.ts +19 -0
  51. package/plugins/immune-brain/runtime/commands/kernel.ts +1160 -0
  52. package/plugins/immune-brain/runtime/github_issue_tracker.ts +1009 -0
  53. package/plugins/immune-brain/runtime/kernel/application.ts +300 -0
  54. package/plugins/immune-brain/runtime/kernel/assurance_projection.ts +284 -0
  55. package/plugins/immune-brain/runtime/kernel/authority_port.ts +208 -0
  56. package/plugins/immune-brain/runtime/kernel/automatic_observations.ts +451 -0
  57. package/plugins/immune-brain/runtime/kernel/backend_claim.ts +197 -0
  58. package/plugins/immune-brain/runtime/kernel/canary_application.ts +507 -0
  59. package/plugins/immune-brain/runtime/kernel/canary_eligibility.ts +73 -0
  60. package/plugins/immune-brain/runtime/kernel/completion.ts +160 -0
  61. package/plugins/immune-brain/runtime/kernel/enrollment.ts +194 -0
  62. package/plugins/immune-brain/runtime/kernel/enrollment_authority.ts +123 -0
  63. package/plugins/immune-brain/runtime/kernel/index.ts +29 -0
  64. package/plugins/immune-brain/runtime/kernel/intent.ts +563 -0
  65. package/plugins/immune-brain/runtime/kernel/intent_token_registry.ts +80 -0
  66. package/plugins/immune-brain/runtime/kernel/legacy.ts +299 -0
  67. package/plugins/immune-brain/runtime/kernel/legacy_audit.ts +153 -0
  68. package/plugins/immune-brain/runtime/kernel/observation.ts +395 -0
  69. package/plugins/immune-brain/runtime/kernel/pi_canary_prepare.ts +169 -0
  70. package/plugins/immune-brain/runtime/kernel/readiness.ts +282 -0
  71. package/plugins/immune-brain/runtime/kernel/readiness_evidence.ts +132 -0
  72. package/plugins/immune-brain/runtime/kernel/reducer.ts +624 -0
  73. package/plugins/immune-brain/runtime/kernel/storage.ts +1780 -0
  74. package/plugins/immune-brain/runtime/kernel/storage_layout_migration.ts +791 -0
  75. package/plugins/immune-brain/runtime/kernel/storage_paths.ts +492 -0
  76. package/plugins/immune-brain/runtime/kernel/types.ts +295 -0
  77. package/plugins/immune-brain/runtime/kernel/validation.ts +963 -0
  78. package/plugins/immune-brain/runtime/loop_contract.ts +362 -0
  79. package/plugins/immune-brain/runtime/managed_task_routing_policy.ts +462 -0
  80. package/plugins/immune-brain/runtime/plan_core.ts +1053 -0
  81. package/plugins/immune-brain/runtime/prompts/advisory-reviewer.md +16 -0
  82. package/plugins/immune-brain/runtime/prompts/arch-explorer.md +14 -0
  83. package/plugins/immune-brain/runtime/prompts/code-review.md +15 -0
  84. package/plugins/immune-brain/runtime/prompts/compounder.md +20 -0
  85. package/plugins/immune-brain/runtime/prompts/executor.md +13 -0
  86. package/plugins/immune-brain/runtime/prompts/pr-fix.md +81 -0
  87. package/plugins/immune-brain/runtime/prompts/qa.md +23 -0
  88. package/plugins/immune-brain/runtime/prompts/test-fixer.md +3 -0
  89. package/plugins/immune-brain/runtime/prompts/ui-review.md +14 -0
  90. package/plugins/immune-brain/runtime/role_prompt_bridge.ts +160 -0
  91. package/plugins/immune-brain/runtime/v4_runtime.ts +295 -0
  92. package/plugins/immune-brain/runtime/verification_descriptor.ts +162 -0
  93. package/plugins/immune-brain/runtime/workspace_scope.ts +623 -0
  94. package/plugins/immune-brain/skills/.ignore +1 -0
  95. package/plugins/immune-brain/skills/BASELINE.md +138 -0
  96. package/plugins/immune-brain/skills/imm-brainstorm/SKILL.md +66 -0
  97. package/plugins/immune-brain/skills/imm-doc-prune/SKILL.md +11 -0
  98. package/plugins/immune-brain/skills/imm-loop/SKILL.md +52 -0
  99. package/plugins/immune-brain/skills/imm-planner/SKILL.md +221 -0
  100. package/plugins/immune-brain/skills/imm-pr-fix/SKILL.md +10 -0
  101. package/plugins/immune-brain/skills/registry.yaml +49 -0
@@ -0,0 +1,197 @@
1
+ // P2B2 backend claim ownership. NOT exported from kernel/index.ts.
2
+ // The ignored state-layout `.imm/state/active-claim.json` is the unique
3
+ // workspace-active claim and may be `active | draining` only; terminal state
4
+ // lives exclusively in the immutable tracked audit proof
5
+ // `.imm/audit/<task-id>/terminal-proof.json`. No writer is exported from this
6
+ // module: every claim change goes through the recoverable Kernel store
7
+ // transactions owned by storage.ts (enrollment, drain, terminalization).
8
+
9
+ import { lstatSync, readFileSync } from "node:fs";
10
+ import { join, resolve } from "node:path";
11
+
12
+ import { auditTerminalProofPath, stateClaimPath } from "./storage_paths";
13
+ import type { TaskLifecycle } from "./types";
14
+
15
+ const CLAIM_PATH = stateClaimPath();
16
+
17
+ export type BackendLifecycleStatus = "active" | "draining";
18
+
19
+ export interface BackendClaim {
20
+ contract: "assurance_kernel/backend_claim/v2";
21
+ backend: "kernel";
22
+ task_id: string;
23
+ intent_revision: number;
24
+ intent_content_hash: string;
25
+ enrollment_event_id: string;
26
+ lifecycle_status: BackendLifecycleStatus;
27
+ created_at: string;
28
+ updated_at: string;
29
+ }
30
+
31
+ const ALLOWED = [
32
+ "contract",
33
+ "backend",
34
+ "task_id",
35
+ "intent_revision",
36
+ "intent_content_hash",
37
+ "enrollment_event_id",
38
+ "lifecycle_status",
39
+ "created_at",
40
+ "updated_at",
41
+ ];
42
+
43
+ export const TASK_TOMBSTONE_CONTRACT = "assurance_kernel/task_tombstone/v2" as const;
44
+ const LEGACY_TASK_TOMBSTONE_CONTRACT = "assurance_kernel/task_tombstone/v1" as const;
45
+
46
+ export interface TaskTombstone {
47
+ contract: typeof TASK_TOMBSTONE_CONTRACT;
48
+ task_id: string;
49
+ lifecycle_status: "terminal";
50
+ terminal_lifecycle: Exclude<TaskLifecycle, "active">;
51
+ terminal_event_id: string;
52
+ final_record_hash: string;
53
+ terminalized_at: string;
54
+ }
55
+
56
+ const TOMBSTONE_ALLOWED = [
57
+ "contract",
58
+ "task_id",
59
+ "lifecycle_status",
60
+ "terminal_lifecycle",
61
+ "terminal_event_id",
62
+ "final_record_hash",
63
+ "terminalized_at",
64
+ ];
65
+
66
+ export class KernelBackendClaimError extends Error {
67
+ constructor(message: string) {
68
+ super(message);
69
+ this.name = "KernelBackendClaimError";
70
+ }
71
+ }
72
+
73
+ function validateTaskId(taskId: string): void {
74
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(taskId))
75
+ throw new KernelBackendClaimError("task_id is not a safe file identity");
76
+ }
77
+
78
+ function readJsonOrNull(path: string): Record<string, unknown> | null {
79
+ try {
80
+ const stat = lstatSync(path);
81
+ if (stat.isSymbolicLink())
82
+ throw new KernelBackendClaimError("owner file must not be a symlink");
83
+ if (!stat.isFile())
84
+ throw new KernelBackendClaimError("owner file is not a regular file");
85
+ } catch (error) {
86
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
87
+ throw error;
88
+ }
89
+ return JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
90
+ }
91
+
92
+ export function parseBackendClaim(raw: Record<string, unknown>): BackendClaim {
93
+ const unknown = Object.keys(raw).filter((key) => !ALLOWED.includes(key));
94
+ if (unknown.length > 0)
95
+ throw new KernelBackendClaimError(`backend claim has unknown field: ${unknown[0]}`);
96
+ if (raw.contract !== "assurance_kernel/backend_claim/v2")
97
+ throw new KernelBackendClaimError("backend claim contract is invalid");
98
+ if (raw.backend !== "kernel")
99
+ throw new KernelBackendClaimError("backend claim backend must be kernel");
100
+ if (typeof raw.task_id !== "string" || !raw.task_id.trim())
101
+ throw new KernelBackendClaimError("backend claim task_id is invalid");
102
+ validateTaskId(raw.task_id);
103
+ if (typeof raw.intent_revision !== "number" || !Number.isInteger(raw.intent_revision) || raw.intent_revision < 1)
104
+ throw new KernelBackendClaimError("backend claim intent_revision is invalid");
105
+ for (const field of ["intent_content_hash", "enrollment_event_id", "created_at", "updated_at"]) {
106
+ if (typeof raw[field] !== "string" || !String(raw[field]).trim())
107
+ throw new KernelBackendClaimError(`backend claim ${field} is invalid`);
108
+ }
109
+ if (raw.lifecycle_status !== "active" && raw.lifecycle_status !== "draining")
110
+ throw new KernelBackendClaimError(
111
+ `backend claim lifecycle_status must be active or draining; terminal state lives only in the task tombstone`,
112
+ );
113
+ return raw as unknown as BackendClaim;
114
+ }
115
+
116
+ export function readBackendClaim(root: string): BackendClaim | null {
117
+ const raw = readJsonOrNull(join(root, CLAIM_PATH));
118
+ if (!raw) return null;
119
+ return parseBackendClaim(raw);
120
+ }
121
+
122
+ /** Serialize a validated claim to the canonical file bytes. Transaction-only writer consumers. */
123
+ export function serializeBackendClaim(claim: BackendClaim): string {
124
+ parseBackendClaim(claim as unknown as Record<string, unknown>);
125
+ return `${JSON.stringify(claim, null, 2)}\n`;
126
+ }
127
+
128
+ export function parseTaskTombstone(raw: Record<string, unknown>): TaskTombstone {
129
+ const legacy = raw.contract === LEGACY_TASK_TOMBSTONE_CONTRACT;
130
+ const allowed = legacy
131
+ ? TOMBSTONE_ALLOWED.map((field) => field === "terminal_lifecycle" ? "terminal_phase" : field)
132
+ : TOMBSTONE_ALLOWED;
133
+ const unknown = Object.keys(raw).filter((key) => !allowed.includes(key));
134
+ if (unknown.length > 0)
135
+ throw new KernelBackendClaimError(`task tombstone has unknown field: ${unknown[0]}`);
136
+ if (!legacy && raw.contract !== TASK_TOMBSTONE_CONTRACT)
137
+ throw new KernelBackendClaimError("task tombstone contract is invalid");
138
+ if (typeof raw.task_id !== "string" || !raw.task_id.trim())
139
+ throw new KernelBackendClaimError("task tombstone task_id is invalid");
140
+ validateTaskId(raw.task_id);
141
+ if (raw.lifecycle_status !== "terminal")
142
+ throw new KernelBackendClaimError("task tombstone lifecycle_status must be terminal");
143
+ const terminalLifecycle = legacy ? raw.terminal_phase : raw.terminal_lifecycle;
144
+ if (terminalLifecycle !== "done" && terminalLifecycle !== "stopped")
145
+ throw new KernelBackendClaimError("task tombstone terminal_lifecycle must be done or stopped");
146
+ for (const field of ["terminal_event_id", "final_record_hash", "terminalized_at"]) {
147
+ if (typeof raw[field] !== "string" || !String(raw[field]).trim())
148
+ throw new KernelBackendClaimError(`task tombstone ${field} is invalid`);
149
+ }
150
+ if (!/^sha256:[a-f0-9]{64}$/.test(raw.final_record_hash as string))
151
+ throw new KernelBackendClaimError("task tombstone final_record_hash must be a canonical sha256 hash");
152
+ return {
153
+ contract: TASK_TOMBSTONE_CONTRACT,
154
+ task_id: raw.task_id,
155
+ lifecycle_status: "terminal",
156
+ terminal_lifecycle: terminalLifecycle,
157
+ terminal_event_id: raw.terminal_event_id as string,
158
+ final_record_hash: raw.final_record_hash as string,
159
+ terminalized_at: raw.terminalized_at as string,
160
+ };
161
+ }
162
+
163
+ /** Fail-closed task-scoped tombstone read. Malformed/unreadable/symlinked state throws; only ENOENT means absent. */
164
+ export function readTaskTombstone(root: string, taskId: string): TaskTombstone | null {
165
+ validateTaskId(taskId);
166
+ const raw = readJsonOrNull(join(resolve(root), auditTerminalProofPath(taskId)));
167
+ if (!raw) return null;
168
+ const tombstone = parseTaskTombstone(raw);
169
+ if (tombstone.task_id !== taskId)
170
+ throw new KernelBackendClaimError("task tombstone identity is inconsistent");
171
+ return tombstone;
172
+ }
173
+
174
+ /** Serialize a validated tombstone to canonical bytes. Transaction-only writer consumers. */
175
+ export function serializeTaskTombstone(tombstone: TaskTombstone): string {
176
+ parseTaskTombstone(tombstone as unknown as Record<string, unknown>);
177
+ return `${JSON.stringify(tombstone, null, 2)}\n`;
178
+ }
179
+
180
+ /**
181
+ * v3 routing guard: while a workspace-active Kernel claim exists (active or
182
+ * draining) for ANY task, canonical v3 managed mutations must fail closed.
183
+ * A terminal task leaves no workspace claim (only an immutable task tombstone),
184
+ * so v3 routing for unrelated tasks is released. Read-only commands are
185
+ * unaffected.
186
+ */
187
+ export function assertNoKernelBackendForV3(root: string, _commandTask?: string): void {
188
+ const claim = readBackendClaim(root);
189
+ if (!claim) return;
190
+ throw new KernelBackendClaimError(
191
+ `backend-owned workspace: task ${claim.task_id} is managed by the Kernel backend (${claim.lifecycle_status}); v3 managed mutation is rejected`,
192
+ );
193
+ }
194
+
195
+ export function backendClaimPath(root: string): string {
196
+ return join(resolve(root), stateClaimPath());
197
+ }