@cassiomc1/forgeloop 1.3.0 → 1.5.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 (76) hide show
  1. package/.github/copilot-instructions.md +1 -0
  2. package/AGENTS.md +1 -0
  3. package/CLAUDE.md +1 -0
  4. package/DOCS_INDEX.md +7 -0
  5. package/EXECUTION_STATE.md +40 -0
  6. package/LOOP_ENGINEERING.md +54 -5
  7. package/LOOP_SYSTEM_DESIGN.md +22 -1
  8. package/PROTOCOL_INTEGRATION.md +41 -0
  9. package/README.md +38 -0
  10. package/TERMINOLOGY.md +15 -0
  11. package/THIRD_PARTY_NOTICES.md +15 -0
  12. package/THREAT_MODEL.md +20 -1
  13. package/docs/ARTIFACT_REFERENCE.md +43 -0
  14. package/docs/CLI_REFERENCE.md +97 -3
  15. package/docs/CROSS_HARNESS_CONTINUITY.md +23 -0
  16. package/docs/DOCUMENTATION_GUIDE.md +14 -0
  17. package/docs/GETTING_STARTED.md +1 -0
  18. package/docs/MCP.md +126 -0
  19. package/docs/RECIPES.md +82 -0
  20. package/docs/RELEASE_CHECKLIST_1_4.md +38 -0
  21. package/docs/RELEASE_CHECKLIST_1_5_MCP.md +78 -0
  22. package/docs/TROUBLESHOOTING.md +111 -1
  23. package/docs/UNIVERSAL_INTEGRATION.md +48 -0
  24. package/package.json +14 -3
  25. package/schemas/task-recovery.schema.json +61 -0
  26. package/src/cli.js +173 -347
  27. package/src/commands/audit.js +5 -0
  28. package/src/commands/inspect.js +6 -0
  29. package/src/commands/progress.js +6 -2
  30. package/src/commands/status.js +17 -0
  31. package/src/commands/task-create.js +39 -1
  32. package/src/commands/task-list.js +14 -1
  33. package/src/commands/task-lock-status.js +2 -2
  34. package/src/commands/task-recover.js +202 -0
  35. package/src/commands/task-repair-legacy-recovery.js +417 -0
  36. package/src/commands/task-resume.js +172 -0
  37. package/src/commands/task-scope.js +23 -4
  38. package/src/commands/task-show.js +18 -4
  39. package/src/commands/validate-protocol.js +19 -2
  40. package/src/core/artifact-registry.js +12 -0
  41. package/src/core/audit.js +20 -4
  42. package/src/core/bundles.js +15 -0
  43. package/src/core/cli-command-definitions.js +50 -4
  44. package/src/core/command-executors.js +387 -0
  45. package/src/core/command-input.js +107 -0
  46. package/src/core/command-runtime.js +106 -0
  47. package/src/core/completion-artifacts.js +2 -3
  48. package/src/core/completion-ownership.js +88 -0
  49. package/src/core/error-codes.js +118 -1
  50. package/src/core/events.js +130 -1
  51. package/src/core/filesystem.js +55 -6
  52. package/src/core/inspect.js +27 -0
  53. package/src/core/integration-invocation-policy.js +170 -0
  54. package/src/core/integration-limits.js +20 -0
  55. package/src/core/integration-resources.js +127 -0
  56. package/src/core/next-action-model.js +60 -0
  57. package/src/core/next-action.js +31 -0
  58. package/src/core/phase.js +2 -1
  59. package/src/core/project-root.js +21 -0
  60. package/src/core/protocol-info.js +13 -0
  61. package/src/core/reconcile-closure.js +32 -10
  62. package/src/core/recovery-history.js +116 -0
  63. package/src/core/schema-validation.js +1 -0
  64. package/src/core/task-claim-state.js +272 -0
  65. package/src/core/task-command.js +5 -1
  66. package/src/core/task-conflict-inspection.js +321 -0
  67. package/src/core/task-context.js +32 -29
  68. package/src/core/task-discovery.js +14 -1
  69. package/src/core/task-lock.js +216 -22
  70. package/src/core/task-paths.js +3 -2
  71. package/src/core/task-recovery-migration.js +192 -0
  72. package/src/core/task-recovery.js +205 -0
  73. package/src/core/task-scope.js +33 -1
  74. package/src/core/templates.js +1 -0
  75. package/src/core/transaction.js +28 -2
  76. package/src/integration.js +47 -0
@@ -0,0 +1,116 @@
1
+ import {
2
+ LEGACY_RECOVERY_MIGRATION_EVENT,
3
+ isLegacyRecoveryEventShape,
4
+ } from "./task-recovery-migration.js";
5
+
6
+ const RECOVERY_EVENT_TYPES = new Set([
7
+ "TASK_RECOVERY_RECORDED",
8
+ "OPERATOR_RECOVERY_RECORDED",
9
+ LEGACY_RECOVERY_MIGRATION_EVENT,
10
+ ]);
11
+
12
+ function recoveryError(message) {
13
+ return {
14
+ code: "E_TASK_RECOVERY_INCONSISTENT",
15
+ message,
16
+ };
17
+ }
18
+
19
+ function recoveryIdOf(event) {
20
+ return typeof event?.details?.recoveryId === "string" && event.details.recoveryId !== ""
21
+ ? event.details.recoveryId
22
+ : null;
23
+ }
24
+
25
+ export function classifyRecoveryHistory(events = []) {
26
+ const recoveries = [];
27
+ const completedRecoveries = [];
28
+ const errors = [];
29
+ const seenRecoveryIds = new Set();
30
+ let activeCycle = null;
31
+
32
+ for (const event of events) {
33
+ if (RECOVERY_EVENT_TYPES.has(event?.event)) {
34
+ // The known legacy defect signature is never an owning recovery cycle by
35
+ // itself. It remains non-owning historical evidence; ownership comes
36
+ // exclusively from its valid LEGACY_RECOVERY_MIGRATION_RECORDED event,
37
+ // which is validated (binding, uniqueness) by the ledger validator.
38
+ if (!event.details?.recoveryId && isLegacyRecoveryEventShape(event)) continue;
39
+ // A migration event only counts as a canonical recovery cycle when it
40
+ // binds an actual legacy event present earlier in this ledger.
41
+ if (event.event === LEGACY_RECOVERY_MIGRATION_EVENT
42
+ && !events.some((candidate) => candidate.seq === event.details?.legacyEventSeq
43
+ && isLegacyRecoveryEventShape(candidate))) {
44
+ errors.push(recoveryError(
45
+ `Legacy migration event at seq ${event?.seq ?? "unknown"} does not bind a legacy recovery event in this ledger`,
46
+ ));
47
+ continue;
48
+ }
49
+ const recoveryId = recoveryIdOf(event);
50
+ if (!recoveryId) {
51
+ errors.push(recoveryError(`Recovery event at seq ${event?.seq ?? "unknown"} has no recoveryId`));
52
+ continue;
53
+ }
54
+ if (seenRecoveryIds.has(recoveryId)) {
55
+ errors.push(recoveryError(`Recovery event at seq ${event.seq} reuses recovery id ${recoveryId}`));
56
+ continue;
57
+ }
58
+ if (activeCycle) {
59
+ errors.push(recoveryError(
60
+ `Recovery ${recoveryId} was recorded while recovery ${activeCycle.recoveryId} is unresolved`,
61
+ ));
62
+ continue;
63
+ }
64
+
65
+ activeCycle = {
66
+ recoveryId,
67
+ recoveryEventSeq: event.seq,
68
+ resumedEventSeq: null,
69
+ active: true,
70
+ event,
71
+ resumedEvent: null,
72
+ };
73
+ seenRecoveryIds.add(recoveryId);
74
+ recoveries.push(activeCycle);
75
+ continue;
76
+ }
77
+
78
+ if (event?.event !== "TASK_RECOVERY_RESUMED") continue;
79
+
80
+ const recoveryId = recoveryIdOf(event);
81
+ if (!recoveryId || !activeCycle || activeCycle.recoveryId !== recoveryId) {
82
+ errors.push(recoveryError(
83
+ `TASK_RECOVERY_RESUMED at seq ${event?.seq ?? "unknown"} does not reference an active recovery`,
84
+ ));
85
+ continue;
86
+ }
87
+ if (!Number.isInteger(event.seq) || event.seq <= activeCycle.recoveryEventSeq) {
88
+ errors.push(recoveryError(
89
+ `Resume sequence for recovery ${recoveryId} must be greater than recovery sequence ${activeCycle.recoveryEventSeq}`,
90
+ ));
91
+ continue;
92
+ }
93
+
94
+ activeCycle.resumedEventSeq = event.seq;
95
+ activeCycle.resumedEvent = event;
96
+ activeCycle.active = false;
97
+ completedRecoveries.push(activeCycle);
98
+ activeCycle = null;
99
+ }
100
+
101
+ const valid = errors.length === 0;
102
+ return {
103
+ recoveries,
104
+ activeRecoveryId: valid ? activeCycle?.recoveryId ?? null : null,
105
+ activeRecovery: valid && activeCycle
106
+ ? { recoveryId: activeCycle.recoveryId, event: activeCycle.event }
107
+ : null,
108
+ completedRecoveries,
109
+ valid,
110
+ errors,
111
+ };
112
+ }
113
+
114
+ export function resolveRecoveryHistory(events = []) {
115
+ return classifyRecoveryHistory(events);
116
+ }
@@ -33,6 +33,7 @@ export const SHIPPED_SCHEMA_NAMES = Object.freeze([
33
33
  "execution",
34
34
  "authority",
35
35
  "task-descriptor",
36
+ "task-recovery",
36
37
  ]);
37
38
 
38
39
  export class SchemaValidationError extends Error {
@@ -0,0 +1,272 @@
1
+ import {
2
+ E_COMPLETION_OWNERSHIP_UNPROVEN,
3
+ E_TASK_CLAIM_OWNERSHIP_INCONSISTENT,
4
+ E_TASK_COMPLETE,
5
+ E_TASK_RECOVERED,
6
+ E_TASK_RECOVERY_INCONSISTENT,
7
+ } from "./error-codes.js";
8
+ import { validateCompletionOwnershipProof } from "./completion-ownership.js";
9
+ import { validateEventLedger } from "./events.js";
10
+ import { classifyRecoveryHistory } from "./recovery-history.js";
11
+ import { readTaskDescriptor } from "./task-descriptor.js";
12
+ import { normalizeWriteClaims } from "./task-scope.js";
13
+ import {
14
+ readTaskRecovery,
15
+ validateTaskRecoveryConsistency,
16
+ } from "./task-recovery.js";
17
+ import { readWorkState } from "./work-state.js";
18
+
19
+ function ownershipError(message, cause = null) {
20
+ return {
21
+ code: E_TASK_RECOVERY_INCONSISTENT,
22
+ message,
23
+ ...(cause?.code ? { causeCode: cause.code } : {}),
24
+ };
25
+ }
26
+
27
+ function appendClaims(target, claims, errors, source) {
28
+ if (claims === undefined || claims === null) return;
29
+ try {
30
+ target.push(...normalizeWriteClaims(claims));
31
+ } catch (error) {
32
+ errors.push(ownershipError(`Invalid ${source} write claims: ${error.message}`, error));
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Collects every input needed to resolve claim ownership from one immutable
38
+ * snapshot: descriptor, work state, recovery tombstone, validated ledger, and
39
+ * classified recovery history. Classification is a pure function of this
40
+ * evidence, so conflict inspection can reuse it without rereading artifacts.
41
+ */
42
+ export async function collectTaskClaimEvidence(target, {
43
+ taskId,
44
+ packageRoot,
45
+ descriptor: suppliedDescriptor = null,
46
+ state: suppliedState = null,
47
+ } = {}) {
48
+ const errors = [];
49
+ let descriptor = suppliedDescriptor;
50
+ let state = suppliedState;
51
+ let recovery = null;
52
+ let ledger = { valid: false, events: [], errors: [] };
53
+
54
+ if (!descriptor) {
55
+ try {
56
+ descriptor = (await readTaskDescriptor(target, taskId, packageRoot)).value;
57
+ } catch (error) {
58
+ errors.push(ownershipError(`Task descriptor cannot establish claim ownership: ${error.message}`, error));
59
+ }
60
+ }
61
+ if (!state) {
62
+ try {
63
+ state = await readWorkState(target, { taskId, packageRoot });
64
+ } catch (error) {
65
+ errors.push(ownershipError(`Task work state cannot establish claim ownership: ${error.message}`, error));
66
+ }
67
+ }
68
+
69
+ try {
70
+ recovery = (await readTaskRecovery(target, { taskId, packageRoot }))?.value ?? null;
71
+ } catch (error) {
72
+ errors.push(ownershipError(`Task recovery artifact cannot establish claim ownership: ${error.message}`, error));
73
+ }
74
+
75
+ try {
76
+ ledger = await validateEventLedger(target, packageRoot, { taskId });
77
+ } catch (error) {
78
+ errors.push(ownershipError(`Task event ledger cannot establish claim ownership: ${error.message}`, error));
79
+ }
80
+ if (!ledger.valid) {
81
+ for (const error of ledger.errors) {
82
+ errors.push(ownershipError(`Task event ledger is invalid: ${error.message}`, error));
83
+ }
84
+ }
85
+ if (ledger.events.some((event) => event.taskId !== taskId)) {
86
+ errors.push(ownershipError(`Task event ledger contains an event for a different task`));
87
+ }
88
+
89
+ const history = classifyRecoveryHistory(ledger.events);
90
+ const descriptorClaims = [];
91
+ appendClaims(descriptorClaims, descriptor?.writeClaims, errors, "descriptor");
92
+ const normalizedDescriptorClaims = normalizeWriteClaims(descriptorClaims);
93
+
94
+ const historicalClaims = [...normalizedDescriptorClaims];
95
+ for (const cycle of history.recoveries) {
96
+ appendClaims(historicalClaims, cycle.event?.details?.releasedClaims, errors, "recovery ledger");
97
+ }
98
+ appendClaims(historicalClaims, recovery?.releasedClaims, errors, "recovery artifact");
99
+
100
+ return {
101
+ taskId,
102
+ phase: state?.phase ?? null,
103
+ descriptor,
104
+ state,
105
+ recovery,
106
+ ledger,
107
+ history,
108
+ normalizedDescriptorClaims,
109
+ historicalWriteClaims: normalizeWriteClaims(historicalClaims),
110
+ errors,
111
+ };
112
+ }
113
+
114
+ function inconsistentResult({ taskId, phase, historicalWriteClaims, recovery, errors }) {
115
+ const reasonCodes = [
116
+ E_TASK_CLAIM_OWNERSHIP_INCONSISTENT,
117
+ ...errors.flatMap((error) => [error.code, error.causeCode]).filter(Boolean),
118
+ ];
119
+ return {
120
+ taskId,
121
+ phase,
122
+ historicalWriteClaims,
123
+ effectiveWriteClaims: historicalWriteClaims,
124
+ writeClaims: historicalWriteClaims,
125
+ claimState: "INCONSISTENT",
126
+ mutationAllowed: false,
127
+ recovery,
128
+ recoveryStatus: "INCONSISTENT",
129
+ valid: false,
130
+ ownershipValid: false,
131
+ reasonCodes: [...new Set(reasonCodes)],
132
+ errors,
133
+ ownershipErrors: errors,
134
+ };
135
+ }
136
+
137
+ /**
138
+ * Deterministic claim-ownership decision table over collected evidence.
139
+ * Claims are released only for validated canonical completion or validated
140
+ * active recovery; everything else fails closed.
141
+ */
142
+ export function classifyTaskClaimState(evidence) {
143
+ const {
144
+ taskId,
145
+ phase,
146
+ recovery,
147
+ ledger,
148
+ history,
149
+ normalizedDescriptorClaims,
150
+ historicalWriteClaims,
151
+ } = evidence;
152
+
153
+ // Classification is side-effect free: never mutate the supplied evidence.
154
+ const errors = [...(evidence.errors ?? [])];
155
+ errors.push(...validateTaskRecoveryConsistency({
156
+ taskId,
157
+ recovery,
158
+ events: ledger.events,
159
+ historicalWriteClaims: normalizedDescriptorClaims,
160
+ recoveryHistory: history,
161
+ }));
162
+
163
+ if (errors.length > 0) {
164
+ return inconsistentResult({ taskId, phase, historicalWriteClaims, recovery, errors });
165
+ }
166
+
167
+ if (phase === "COMPLETE") {
168
+ const proof = validateCompletionOwnershipProof({ taskId, state: evidence.state, ledger });
169
+ if (!proof.valid) {
170
+ return inconsistentResult({
171
+ taskId,
172
+ phase,
173
+ historicalWriteClaims,
174
+ recovery,
175
+ errors: [
176
+ ownershipError(
177
+ "COMPLETE work-state lacks canonical lifecycle/ledger completion proof; claims stay reserved",
178
+ { code: E_COMPLETION_OWNERSHIP_UNPROVEN },
179
+ ),
180
+ ...proof.errors.map((error) => ownershipError(error.message, { code: error.code })),
181
+ ],
182
+ });
183
+ }
184
+ return {
185
+ taskId,
186
+ phase,
187
+ historicalWriteClaims,
188
+ effectiveWriteClaims: [],
189
+ writeClaims: [],
190
+ claimState: "RELEASED_BY_COMPLETION",
191
+ mutationAllowed: false,
192
+ recovery: null,
193
+ recoveryStatus: "NOT_APPLICABLE",
194
+ valid: true,
195
+ ownershipValid: true,
196
+ reasonCodes: [],
197
+ errors: [],
198
+ ownershipErrors: [],
199
+ };
200
+ }
201
+
202
+ if (history.activeRecovery) {
203
+ // The consistency validator above guarantees that the active ledger cycle
204
+ // and recovery artifact identify the same recovery before this branch.
205
+ return {
206
+ taskId,
207
+ phase,
208
+ historicalWriteClaims,
209
+ effectiveWriteClaims: [],
210
+ writeClaims: [],
211
+ claimState: "RELEASED_BY_RECOVERY",
212
+ mutationAllowed: false,
213
+ recovery,
214
+ recoveryStatus: "ACTIVE",
215
+ valid: true,
216
+ ownershipValid: true,
217
+ reasonCodes: [],
218
+ errors: [],
219
+ ownershipErrors: [],
220
+ };
221
+ }
222
+
223
+ return {
224
+ taskId,
225
+ phase,
226
+ historicalWriteClaims,
227
+ effectiveWriteClaims: normalizedDescriptorClaims,
228
+ writeClaims: normalizedDescriptorClaims,
229
+ claimState: "ACTIVE",
230
+ mutationAllowed: true,
231
+ recovery: null,
232
+ recoveryStatus: history.completedRecoveries.length > 0 ? "COMPLETED" : "ABSENT",
233
+ valid: true,
234
+ ownershipValid: true,
235
+ reasonCodes: [],
236
+ errors: [],
237
+ ownershipErrors: [],
238
+ };
239
+ }
240
+
241
+ /**
242
+ * Resolve claim ownership from the task descriptor, work state, recovery
243
+ * tombstone, and the complete validated task ledger. Any disagreement retains
244
+ * every claim that can be recovered from validated inputs and fails closed.
245
+ */
246
+ export async function resolveTaskClaimState(target, options = {}) {
247
+ const evidence = await collectTaskClaimEvidence(target, options);
248
+ return classifyTaskClaimState(evidence);
249
+ }
250
+
251
+ export async function assertTaskMutationAllowed(target, options = {}) {
252
+ const result = await resolveTaskClaimState(target, options);
253
+ if (result.mutationAllowed) return result;
254
+
255
+ const inconsistent = result.claimState === "INCONSISTENT";
256
+ const error = new Error(inconsistent
257
+ ? `Task ${result.taskId} claim ownership is inconsistent; ordinary mutation is blocked`
258
+ : result.claimState === "RELEASED_BY_COMPLETION"
259
+ ? `Task ${result.taskId} is COMPLETE and cannot be mutated`
260
+ : `Task ${result.taskId} is RECOVERED and its write claims are released; run task-resume before ordinary mutation`);
261
+ error.code = inconsistent
262
+ ? E_TASK_CLAIM_OWNERSHIP_INCONSISTENT
263
+ : result.claimState === "RELEASED_BY_COMPLETION"
264
+ ? E_TASK_COMPLETE
265
+ : E_TASK_RECOVERED;
266
+ error.taskId = result.taskId;
267
+ error.claimState = result.claimState;
268
+ error.reasonCodes = result.reasonCodes;
269
+ error.errors = result.errors;
270
+ error.recovery = result.recovery;
271
+ throw error;
272
+ }
@@ -1,5 +1,6 @@
1
- import { resolveTaskContext } from "./task-context.js";
1
+ import { resolveTaskContext, TASK_SELECTION_MODES } from "./task-context.js";
2
2
  import { withTaskTransaction } from "./transaction.js";
3
+ import { assertTaskMutationAllowed } from "./task-claim-state.js";
3
4
 
4
5
  export async function withResolvedTask(
5
6
  target,
@@ -12,6 +13,7 @@ export async function withResolvedTask(
12
13
  taskId: taskOption,
13
14
  explicitRequired,
14
15
  packageRoot: options.packageRoot,
16
+ selectionMode: TASK_SELECTION_MODES.READ,
15
17
  });
16
18
 
17
19
  return callback(taskContext);
@@ -29,10 +31,12 @@ export async function withTaskMutation(
29
31
  taskId: taskOption,
30
32
  explicitRequired,
31
33
  packageRoot: options.packageRoot,
34
+ selectionMode: TASK_SELECTION_MODES.MUTATION,
32
35
  });
33
36
 
34
37
  if (taskContext) {
35
38
  return withTaskTransaction({ target, taskId: taskContext.taskId, operation, packageRoot: options.packageRoot, recordCommitEvent: true }, async (transaction) => {
39
+ await assertTaskMutationAllowed(target, { taskId: taskContext.taskId, packageRoot: options.packageRoot });
36
40
  return callback({ ...taskContext, transaction });
37
41
  });
38
42
  }