@cassiomc1/forgeloop 0.1.12 → 0.1.14

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.
@@ -43,6 +43,10 @@ route, required gates, and READY preflight.
43
43
  Use the project-local ForgeLoop CLI for lifecycle-owned protocol state.
44
44
  Never manually synthesize lifecycle chronology or assign ForgeLoop COMPLETE.
45
45
 
46
+ Do not install a missing verification tool merely to satisfy a check.
47
+ Use an existing equivalent, obtain explicit authority, or report the
48
+ verification dimension as NOT_VERIFIED.
49
+
46
50
  Before claiming ForgeLoop-verified completion, require
47
51
  \`forgeloop complete\` to return \`VALID\`.
48
52
 
@@ -236,7 +236,16 @@ async function loadArtifact(loader, fallback) {
236
236
  }
237
237
  }
238
238
 
239
- async function requirementsAndCoverage({ target, packageRoot, contract, route, checks, additionalEvidence = [] }) {
239
+ async function requirementsAndCoverage({
240
+ target,
241
+ packageRoot,
242
+ contract,
243
+ route,
244
+ checks,
245
+ additionalEvidence = [],
246
+ authorityContext,
247
+ runtimeContext,
248
+ }) {
240
249
  const requirements = await requiredEvidenceForTarget({
241
250
  target,
242
251
  contract,
@@ -244,14 +253,21 @@ async function requirementsAndCoverage({ target, packageRoot, contract, route, c
244
253
  packageRoot,
245
254
  additionalEvidence,
246
255
  });
247
- return { requirements, coverage: coverageForRequirements(requirements, checks) };
256
+ return {
257
+ requirements,
258
+ coverage: coverageForRequirements(requirements, checks, {
259
+ target,
260
+ taskId: contract?.value?.taskId,
261
+ options: { authorityContext, runtimeContext },
262
+ }),
263
+ };
248
264
  }
249
265
 
250
266
  export async function getNextAction(targetOrOptions = {}, packageRootOption) {
251
267
  const normalized = typeof targetOrOptions === "string"
252
268
  ? { target: targetOrOptions, packageRoot: packageRootOption }
253
269
  : targetOrOptions;
254
- const { target, packageRoot } = normalized ?? {};
270
+ const { target, packageRoot, authorityContext, runtimeContext } = normalized ?? {};
255
271
  const workState = await loadArtifact(
256
272
  () => readWorkState(target, packageRoot),
257
273
  ARTIFACT_PATHS.state,
@@ -544,6 +560,8 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
544
560
  route,
545
561
  checks: state.checks,
546
562
  additionalEvidence: preflight.policy?.requiredEvidence ?? [],
563
+ authorityContext,
564
+ runtimeContext,
547
565
  });
548
566
  const authoritative = authoritativeChecksForRequirements({
549
567
  requirements: ordinaryLeafRequirements(evidence.requirements),
@@ -587,7 +605,12 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
587
605
  );
588
606
  }
589
607
  try {
590
- await validateReceipt(receipt.value.value, packageRoot);
608
+ await validateReceipt(receipt.value.value, packageRoot, {
609
+ target,
610
+ taskId: contract?.value?.taskId,
611
+ authorityContext,
612
+ runtimeContext,
613
+ });
591
614
  } catch (error) {
592
615
  return decision(
593
616
  context,
@@ -596,7 +619,13 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
596
619
  [...requiredArtifacts, ARTIFACT_PATHS.receipt],
597
620
  );
598
621
  }
599
- const readiness = evaluateRequiredEvidence({ requirements: evidence.requirements, checks: state.checks });
622
+ const readiness = evaluateRequiredEvidence({
623
+ requirements: evidence.requirements,
624
+ checks: state.checks,
625
+ target,
626
+ taskId: contract?.value?.taskId,
627
+ options: { authorityContext, runtimeContext },
628
+ });
600
629
  const receiptRelationships = completionRelationshipErrors({
601
630
  contract,
602
631
  route,
@@ -604,8 +633,26 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
604
633
  receipt: receipt.value.value,
605
634
  requiredEvidence: evidence.requirements,
606
635
  requireRequiredChecks: false,
636
+ target,
637
+ taskId: contract?.value?.taskId,
638
+ authorityContext,
639
+ runtimeContext,
607
640
  });
608
641
  if (receiptRelationships.length > 0) {
642
+ if (receiptRelationships.some((err) => (
643
+ err.code === "E_RECEIPT_STATE_MISMATCH"
644
+ || err.code === "E_RECEIPT_CYCLE_MISMATCH"
645
+ || err.code === "E_RECEIPT_CONTRACT_MISMATCH"
646
+ || err.code === "E_ROUTE_GUIDE_MISMATCH"
647
+ || err.code === "E_EVIDENCE_COVERAGE_INVALID"
648
+ ))) {
649
+ return decision(
650
+ context,
651
+ NEXT_ACTIONS.PREPARE_COMPLETION,
652
+ artifactError("E_RECEIPT_STATE_MISMATCH", "Run forgeloop prepare-completion to refresh the execution receipt with current state", [ARTIFACT_PATHS.receipt]),
653
+ [...requiredArtifacts, ARTIFACT_PATHS.receipt],
654
+ );
655
+ }
609
656
  return result({
610
657
  ...context,
611
658
  nextAction: NEXT_ACTIONS.RESOLVE_BLOCKER,
@@ -670,8 +717,16 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
670
717
  route,
671
718
  checks: state.checks,
672
719
  additionalEvidence: preflight.policy?.requiredEvidence ?? [],
720
+ authorityContext,
721
+ runtimeContext,
722
+ });
723
+ const readiness = evaluateRequiredEvidence({
724
+ requirements: evidence.requirements,
725
+ checks: state.checks,
726
+ target,
727
+ taskId: contract?.value?.taskId,
728
+ options: { authorityContext, runtimeContext },
673
729
  });
674
- const readiness = evaluateRequiredEvidence({ requirements: evidence.requirements, checks: state.checks });
675
730
  if (!readiness.ready) {
676
731
  let recoveryAuthorized = false;
677
732
  if (state.lastCompletionAttempt?.status === "REJECTED") {
@@ -737,7 +792,12 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
737
792
  );
738
793
  }
739
794
  try {
740
- await validateReceipt(receipt.value.value, packageRoot);
795
+ await validateReceipt(receipt.value.value, packageRoot, {
796
+ target,
797
+ taskId: contract?.value?.taskId,
798
+ authorityContext,
799
+ runtimeContext,
800
+ });
741
801
  } catch (error) {
742
802
  return decision(
743
803
  context,
@@ -752,8 +812,26 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
752
812
  state,
753
813
  receipt: receipt.value.value,
754
814
  requiredEvidence: evidence.requirements,
815
+ target,
816
+ taskId: contract?.value?.taskId,
817
+ authorityContext,
818
+ runtimeContext,
755
819
  });
756
820
  if (receiptRelationships.length > 0) {
821
+ if (receiptRelationships.some((err) => (
822
+ err.code === "E_RECEIPT_STATE_MISMATCH"
823
+ || err.code === "E_RECEIPT_CYCLE_MISMATCH"
824
+ || err.code === "E_RECEIPT_CONTRACT_MISMATCH"
825
+ || err.code === "E_ROUTE_GUIDE_MISMATCH"
826
+ || err.code === "E_EVIDENCE_COVERAGE_INVALID"
827
+ ))) {
828
+ return decision(
829
+ context,
830
+ NEXT_ACTIONS.PREPARE_COMPLETION,
831
+ artifactError("E_RECEIPT_STATE_MISMATCH", "Run forgeloop prepare-completion to refresh the execution receipt with current state", [ARTIFACT_PATHS.receipt]),
832
+ [...requiredArtifacts, ARTIFACT_PATHS.receipt],
833
+ );
834
+ }
757
835
  return result({
758
836
  ...context,
759
837
  nextAction: NEXT_ACTIONS.RESOLVE_BLOCKER,
@@ -761,7 +839,7 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
761
839
  requiredArtifacts: [...requiredArtifacts, ARTIFACT_PATHS.receipt],
762
840
  });
763
841
  }
764
- const completion = await evaluateCompletion({ target, packageRoot });
842
+ const completion = await evaluateCompletion({ target, packageRoot, authorityContext, runtimeContext });
765
843
  if (completion.status !== "VALID") {
766
844
  const terminalPendingErrors = completion.errors.filter((err) => (
767
845
  err.code === "E_PUBLICATION_REQUIREMENT_PENDING" || err.code === "E_PRODUCTION_REQUIREMENT_PENDING"
@@ -793,7 +871,7 @@ export async function getNextAction(targetOrOptions = {}, packageRootOption) {
793
871
  return decision(context, NEXT_ACTIONS.RUN_COMPLETE, artifactError("COMPLETION_READY", "Completion artifacts and cross-artifact validation are valid"));
794
872
  }
795
873
  if (state.phase === "COMPLETE") {
796
- const completion = await evaluateCompletion({ target, packageRoot });
874
+ const completion = await evaluateCompletion({ target, packageRoot, authorityContext, runtimeContext });
797
875
  if (completion.status === "VALID") {
798
876
  return decision(context, NEXT_ACTIONS.NONE, artifactError("PHASE_COMPLETE", "Completion is validator-backed and terminal"));
799
877
  }
package/src/core/phase.js CHANGED
@@ -101,7 +101,7 @@ function reconcileImplementationStep(state, toPhase) {
101
101
  };
102
102
  }
103
103
 
104
- async function assertPhasePrerequisites(target, state, toPhase, packageRoot) {
104
+ async function assertPhasePrerequisites(target, state, toPhase, packageRoot, authorityContext, runtimeContext) {
105
105
  if (toPhase === "CONTRACT_READY" || toPhase === "ROUTED" || toPhase === "EXECUTING") {
106
106
  try {
107
107
  await readContract(target, packageRoot);
@@ -127,7 +127,7 @@ async function assertPhasePrerequisites(target, state, toPhase, packageRoot) {
127
127
  throw phaseError("E_PHASE_EVIDENCE_MISSING", "COMPLETE requires verification evidence");
128
128
  }
129
129
  if (toPhase === "COMPLETE") {
130
- const completion = await evaluateCompletion({ target, packageRoot, persist: false });
130
+ const completion = await evaluateCompletion({ target, packageRoot, persist: false, authorityContext, runtimeContext });
131
131
  if (completion.status !== "VALID") {
132
132
  throw phaseError("E_COMPLETION_REJECTED", "COMPLETE requires a valid completion audit", completion.errors.flatMap((error) => error.artifacts ?? []));
133
133
  }
@@ -136,11 +136,16 @@ async function assertPhasePrerequisites(target, state, toPhase, packageRoot) {
136
136
 
137
137
  export async function advanceWorkState(target, toPhase, options = {}) {
138
138
  const normalizedOptions = typeof options === "string" ? { packageRoot: options } : options;
139
- const { packageRoot, now = new Date().toISOString() } = normalizedOptions;
139
+ const {
140
+ packageRoot,
141
+ now = new Date().toISOString(),
142
+ authorityContext,
143
+ runtimeContext,
144
+ } = normalizedOptions;
140
145
  assertWorkPhase(toPhase);
141
146
  const state = await readWorkState(target, packageRoot);
142
147
  if (!state) throw phaseError("E_PHASE_PREREQUISITE_MISSING", "Cannot advance without work state", [ARTIFACT_PATHS.state]);
143
- await assertPhasePrerequisites(target, state, toPhase, packageRoot);
148
+ await assertPhasePrerequisites(target, state, toPhase, packageRoot, authorityContext, runtimeContext);
144
149
  await assertPersistedStateIdentity(target, state, toPhase, packageRoot);
145
150
  if (!isValidTransition(state.phase, toPhase)) {
146
151
  throw phaseError("E_PHASE_TRANSITION_INVALID", `Invalid work-state transition: ${state.phase} -> ${toPhase}`);
@@ -231,7 +236,12 @@ export async function advanceWorkState(target, toPhase, options = {}) {
231
236
  packageRoot,
232
237
  additionalEvidence: preflight.policy?.requiredEvidence ?? [],
233
238
  });
234
- await validateReceipt(receipt.value, packageRoot);
239
+ await validateReceipt(receipt.value, packageRoot, {
240
+ target,
241
+ taskId: contract?.value?.taskId,
242
+ authorityContext,
243
+ runtimeContext,
244
+ });
235
245
  assertCompletionRelationships({
236
246
  contract,
237
247
  route,
@@ -240,12 +250,21 @@ export async function advanceWorkState(target, toPhase, options = {}) {
240
250
  requiredEvidence,
241
251
  requireRequiredChecks: false,
242
252
  requireReceiptStateFingerprint: false,
253
+ target,
254
+ taskId: contract?.value?.taskId,
255
+ authorityContext,
256
+ runtimeContext,
243
257
  });
244
258
  nextReceipt = await createReceipt({
245
259
  ...receipt.value,
246
260
  stateFingerprint: canonicalFingerprint(next),
247
261
  verificationCycle: next.verificationCycle ?? receipt.value.verificationCycle ?? 1,
248
- }, packageRoot);
262
+ }, packageRoot, {
263
+ target,
264
+ taskId: contract?.value?.taskId,
265
+ authorityContext,
266
+ runtimeContext,
267
+ });
249
268
  assertCompletionRelationships({
250
269
  contract,
251
270
  route,
@@ -253,6 +272,10 @@ export async function advanceWorkState(target, toPhase, options = {}) {
253
272
  receipt: nextReceipt,
254
273
  requiredEvidence,
255
274
  requireRequiredChecks: false,
275
+ target,
276
+ taskId: contract?.value?.taskId,
277
+ authorityContext,
278
+ runtimeContext,
256
279
  });
257
280
  } catch (error) {
258
281
  if (error.code !== "ARTIFACT_MISSING") throw error;
@@ -81,6 +81,11 @@ export const FAILURE_CODES = Object.freeze([
81
81
  "E_TERMINAL_REQUIREMENT_NOT_TERMINAL",
82
82
  "E_TERMINAL_REQUIREMENT_TYPE_MISMATCH",
83
83
  "E_TERMINAL_STATUS_REGRESSION",
84
+ "E_INSTALLATION_AUTHORITY_REQUIRED",
85
+ "E_AUTHORITY_INVALID",
86
+ "E_AUTHORITY_SCOPE_MISMATCH",
87
+ "E_AUTHORITY_UNTRUSTED_SOURCE",
88
+ "E_VERIFICATION_TOOL_UNAVAILABLE",
84
89
  ]);
85
90
 
86
91
  export const WORK_PHASES = Object.freeze([
@@ -56,7 +56,7 @@ function assertKnownGuides(guides) {
56
56
  if (new Set(guides).size !== guides.length) throw new Error("Receipt selectedGuides must not contain duplicates");
57
57
  }
58
58
 
59
- export function assertReceiptSemantics(receipt) {
59
+ export function assertReceiptSemantics(receipt, options = {}) {
60
60
  const evidence = receipt.evidence ?? [];
61
61
  if (receipt.verificationCycle !== undefined
62
62
  && (!Number.isInteger(receipt.verificationCycle) || receipt.verificationCycle < 1)) {
@@ -66,7 +66,7 @@ export function assertReceiptSemantics(receipt) {
66
66
 
67
67
  for (const [index, check] of receipt.checks.entries()) {
68
68
  if (check?.schemaVersion !== undefined || check?.id !== undefined || check?.evidenceKind !== undefined) {
69
- assertCheck(check, `receipt.checks[${index}]`);
69
+ assertCheck(check, `receipt.checks[${index}]`, options);
70
70
  continue;
71
71
  }
72
72
  if (check?.status === "passed") {
@@ -132,15 +132,15 @@ export function assertReceiptSemantics(receipt) {
132
132
  return receipt;
133
133
  }
134
134
 
135
- export async function validateReceipt(receipt, packageRoot) {
135
+ export async function validateReceipt(receipt, packageRoot, options = {}) {
136
136
  assertSecretFree(receipt);
137
137
  const schema = await readSchema("execution-receipt", packageRoot);
138
138
  assertSchema(receipt, schema, "execution receipt");
139
139
  assertKnownGuides(receipt.selectedGuides);
140
- return assertReceiptSemantics(receipt);
140
+ return assertReceiptSemantics(receipt, options);
141
141
  }
142
142
 
143
- export async function createReceipt(input, packageRoot) {
143
+ export async function createReceipt(input, packageRoot, options = {}) {
144
144
  assertSecretFree(input);
145
145
  const receipt = {
146
146
  schemaVersion: RECEIPT_SCHEMA_VERSION,
@@ -169,5 +169,5 @@ export async function createReceipt(input, packageRoot) {
169
169
  deployed: false,
170
170
  },
171
171
  };
172
- return validateReceipt(receipt, packageRoot);
172
+ return validateReceipt(receipt, packageRoot, options);
173
173
  }
@@ -0,0 +1,80 @@
1
+ export const AUTHORITY_TRUST_MODES = Object.freeze(["NONE", "HOST_ATTESTED"]);
2
+
3
+ const AUTHORITY_CONTEXT_FIELDS = Object.freeze([
4
+ "trustedAuthorityFile",
5
+ "trustedAuthorityDir",
6
+ "authorities",
7
+ "authority",
8
+ ]);
9
+
10
+ function configuredValue(value) {
11
+ return typeof value === "string" && value.trim() !== "" ? value.trim() : undefined;
12
+ }
13
+
14
+ function providerValue(provider) {
15
+ if (typeof provider === "function") return provider();
16
+ return provider && typeof provider === "object" && !Array.isArray(provider) ? provider : {};
17
+ }
18
+
19
+ function contextInput(input = {}) {
20
+ const source = input && typeof input === "object" && !Array.isArray(input) ? input : {};
21
+ const provider = providerValue(source.trustedAuthorityProvider);
22
+ const explicit = source.authorityContext && typeof source.authorityContext === "object"
23
+ ? source.authorityContext
24
+ : {};
25
+ const { authorityContext: _authorityContext, trustedAuthorityProvider: _trustedAuthorityProvider, ...direct } = source;
26
+ return { ...direct, ...provider, ...explicit };
27
+ }
28
+
29
+ export function hasAuthorityContext(options = {}) {
30
+ if (!options || typeof options !== "object") return false;
31
+ return Boolean(
32
+ options.authorityContext
33
+ || options.runtimeContext?.authorityContext
34
+ || (options.runtimeContext && typeof options.runtimeContext === "object" && options.runtimeContext.trustMode),
35
+ );
36
+ }
37
+
38
+ export function createAuthorityContext(input = {}) {
39
+ const values = contextInput(input);
40
+ const trustMode = values.trustMode ?? values.authorityTrustMode ?? "NONE";
41
+ if (!AUTHORITY_TRUST_MODES.includes(trustMode)) {
42
+ const error = new Error(`Unsupported authority trust mode: ${trustMode}`);
43
+ error.code = "E_AUTHORITY_INVALID";
44
+ throw error;
45
+ }
46
+
47
+ const context = { trustMode };
48
+ for (const field of AUTHORITY_CONTEXT_FIELDS) {
49
+ if (field === "trustedAuthorityFile" || field === "trustedAuthorityDir") {
50
+ const value = configuredValue(values[field]);
51
+ if (value !== undefined) context[field] = value;
52
+ } else if (values[field] !== undefined) {
53
+ context[field] = values[field];
54
+ }
55
+ }
56
+ return Object.freeze(context);
57
+ }
58
+
59
+ export function resolveAuthorityContext(options = {}) {
60
+ const source = options && typeof options === "object" && !Array.isArray(options) ? options : {};
61
+ if (source.authorityContext !== undefined) {
62
+ return createAuthorityContext(source.authorityContext);
63
+ }
64
+ if (source.runtimeContext?.authorityContext !== undefined) {
65
+ return createAuthorityContext(source.runtimeContext.authorityContext);
66
+ }
67
+ if (source.runtimeContext && typeof source.runtimeContext === "object" && source.runtimeContext.trustMode) {
68
+ return createAuthorityContext(source.runtimeContext);
69
+ }
70
+
71
+ const hasDirectAuthority = AUTHORITY_CONTEXT_FIELDS.some((field) => source[field] !== undefined);
72
+ // Direct resolver options are legacy source selectors, not a host attestation
73
+ // channel. They remain NONE unless wrapped in an explicit runtime context.
74
+ return hasDirectAuthority ? createAuthorityContext({ ...source, trustMode: "NONE" }) : createAuthorityContext();
75
+ }
76
+
77
+ export function createForgeLoopContext(options = {}) {
78
+ const authorityContext = createAuthorityContext(options);
79
+ return Object.freeze({ authorityContext });
80
+ }