@coderifts/agent-guard 8.4.0 → 9.0.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 (96) hide show
  1. package/README.md +35 -22
  2. package/dist/cjs/auto-derive.d.ts +74 -0
  3. package/dist/cjs/auto-derive.d.ts.map +1 -0
  4. package/dist/cjs/auto-derive.js +272 -0
  5. package/dist/cjs/auto-derive.js.map +1 -0
  6. package/dist/cjs/auto-recheck.d.ts +76 -0
  7. package/dist/cjs/auto-recheck.d.ts.map +1 -0
  8. package/dist/cjs/auto-recheck.js +240 -0
  9. package/dist/cjs/auto-recheck.js.map +1 -0
  10. package/dist/cjs/cas-attestation.d.ts +35 -2
  11. package/dist/cjs/cas-attestation.d.ts.map +1 -1
  12. package/dist/cjs/cas-attestation.js +105 -2
  13. package/dist/cjs/cas-attestation.js.map +1 -1
  14. package/dist/cjs/deploy-bind.d.ts +9 -7
  15. package/dist/cjs/deploy-bind.d.ts.map +1 -1
  16. package/dist/cjs/deploy-bind.js +9 -3
  17. package/dist/cjs/deploy-bind.js.map +1 -1
  18. package/dist/cjs/deploy-gate.d.ts +54 -6
  19. package/dist/cjs/deploy-gate.d.ts.map +1 -1
  20. package/dist/cjs/deploy-gate.js +73 -12
  21. package/dist/cjs/deploy-gate.js.map +1 -1
  22. package/dist/cjs/deploy-receipt-token.d.ts +59 -0
  23. package/dist/cjs/deploy-receipt-token.d.ts.map +1 -0
  24. package/dist/cjs/deploy-receipt-token.js +245 -0
  25. package/dist/cjs/deploy-receipt-token.js.map +1 -0
  26. package/dist/cjs/execution-proof.d.ts +25 -0
  27. package/dist/cjs/execution-proof.d.ts.map +1 -1
  28. package/dist/cjs/execution-proof.js +6 -0
  29. package/dist/cjs/execution-proof.js.map +1 -1
  30. package/dist/cjs/final-answer-proof.d.ts.map +1 -1
  31. package/dist/cjs/final-answer-proof.js +20 -1
  32. package/dist/cjs/final-answer-proof.js.map +1 -1
  33. package/dist/cjs/guard.d.ts.map +1 -1
  34. package/dist/cjs/guard.js +10 -1
  35. package/dist/cjs/guard.js.map +1 -1
  36. package/dist/cjs/index.d.ts +10 -4
  37. package/dist/cjs/index.d.ts.map +1 -1
  38. package/dist/cjs/index.js +23 -2
  39. package/dist/cjs/index.js.map +1 -1
  40. package/dist/cjs/tool-registry.d.ts.map +1 -1
  41. package/dist/cjs/tool-registry.js +36 -6
  42. package/dist/cjs/tool-registry.js.map +1 -1
  43. package/dist/cjs/types.d.ts +45 -1
  44. package/dist/cjs/types.d.ts.map +1 -1
  45. package/dist/cjs/with-coderifts.d.ts +16 -0
  46. package/dist/cjs/with-coderifts.d.ts.map +1 -1
  47. package/dist/cjs/with-coderifts.js +9 -0
  48. package/dist/cjs/with-coderifts.js.map +1 -1
  49. package/dist/esm/auto-derive.d.ts +74 -0
  50. package/dist/esm/auto-derive.d.ts.map +1 -0
  51. package/dist/esm/auto-derive.js +265 -0
  52. package/dist/esm/auto-derive.js.map +1 -0
  53. package/dist/esm/auto-recheck.d.ts +76 -0
  54. package/dist/esm/auto-recheck.d.ts.map +1 -0
  55. package/dist/esm/auto-recheck.js +234 -0
  56. package/dist/esm/auto-recheck.js.map +1 -0
  57. package/dist/esm/cas-attestation.d.ts +35 -2
  58. package/dist/esm/cas-attestation.d.ts.map +1 -1
  59. package/dist/esm/cas-attestation.js +103 -2
  60. package/dist/esm/cas-attestation.js.map +1 -1
  61. package/dist/esm/deploy-bind.d.ts +9 -7
  62. package/dist/esm/deploy-bind.d.ts.map +1 -1
  63. package/dist/esm/deploy-bind.js +9 -3
  64. package/dist/esm/deploy-bind.js.map +1 -1
  65. package/dist/esm/deploy-gate.d.ts +54 -6
  66. package/dist/esm/deploy-gate.d.ts.map +1 -1
  67. package/dist/esm/deploy-gate.js +70 -12
  68. package/dist/esm/deploy-gate.js.map +1 -1
  69. package/dist/esm/deploy-receipt-token.d.ts +59 -0
  70. package/dist/esm/deploy-receipt-token.d.ts.map +1 -0
  71. package/dist/esm/deploy-receipt-token.js +241 -0
  72. package/dist/esm/deploy-receipt-token.js.map +1 -0
  73. package/dist/esm/execution-proof.d.ts +25 -0
  74. package/dist/esm/execution-proof.d.ts.map +1 -1
  75. package/dist/esm/execution-proof.js +6 -0
  76. package/dist/esm/execution-proof.js.map +1 -1
  77. package/dist/esm/final-answer-proof.d.ts.map +1 -1
  78. package/dist/esm/final-answer-proof.js +20 -1
  79. package/dist/esm/final-answer-proof.js.map +1 -1
  80. package/dist/esm/guard.d.ts.map +1 -1
  81. package/dist/esm/guard.js +11 -2
  82. package/dist/esm/guard.js.map +1 -1
  83. package/dist/esm/index.d.ts +10 -4
  84. package/dist/esm/index.d.ts.map +1 -1
  85. package/dist/esm/index.js +7 -2
  86. package/dist/esm/index.js.map +1 -1
  87. package/dist/esm/tool-registry.d.ts.map +1 -1
  88. package/dist/esm/tool-registry.js +36 -6
  89. package/dist/esm/tool-registry.js.map +1 -1
  90. package/dist/esm/types.d.ts +45 -1
  91. package/dist/esm/types.d.ts.map +1 -1
  92. package/dist/esm/with-coderifts.d.ts +16 -0
  93. package/dist/esm/with-coderifts.d.ts.map +1 -1
  94. package/dist/esm/with-coderifts.js +9 -0
  95. package/dist/esm/with-coderifts.js.map +1 -1
  96. package/package.json +2 -2
package/README.md CHANGED
@@ -161,6 +161,8 @@ Honesty (verbatim, and also on the machine proof as
161
161
  `limits.commit_observation_is_observed_at_t3_not_atomic: true`):
162
162
  `commit_observation is observed at T3, not atomic: another writer may act between write and observation; token-only adapters compare version token not content; host attestation is a host claim layered on the measurement`.
163
163
 
164
+ When `withCodeRifts({ executorAttestation: { registry } })` is set and a CAS outcome carries a verified `cr.exec.attest.v1` token, the T3 section upgrades to `committed — executor-attested (ATTEST_VALID, kid …)`. Invalid attestation stays host-claimed with `attest_status` visible. Observation-side only.
165
+
164
166
  > **Supply `artifacts[]` with content.** If the guard detects a contract change (e.g. from
165
167
  > `filesTouched`/`diff`) but you did **not** supply `artifacts[]` with `before`/`after`, it fails
166
168
  > closed **locally** with `outcome.verdict.cause === 'MISSING_ARTIFACT_CONTENT'` — the tool does not
@@ -568,30 +570,46 @@ honest, so the composition hands the caller the piece it owns and stops.
568
570
 
569
571
  #### `deployGate` and deploy-time `bindDeploy`
570
572
 
571
- `deployGate` is the pure decision. **`bindDeploy`** is the pure **caller** at the moment of
572
- deploying: the host supplies `{ environment, artifact_id, receipt, pipeline_enforcement }`; the
573
- package composes `deployGate` and returns a decision. No I/O, no receipt re-verify, no CD call.
573
+ `deployGate` is the pure decision. **`bindDeploy`** is the pure **caller** at the
574
+ moment of deploying. Two **explicit, non-guessable** receipt input modes (9.0.0;
575
+ P0 / audit 2026-08-24):
576
+
577
+ **(a) TOKEN mode (recommended).** Pass the signed chain_receipt token plus
578
+ `{ registry | pinnedKeyPem }` and the decision envelope. The guard verifies
579
+ **locally** (Ed25519, kid / retired window, body-hash, ID104 leeway). No
580
+ HTTP — SDK 3.4.0 `client.verifyReceipt` is I/O and is not used here.
581
+
582
+ **(b) VERIFIED-VIEW mode.** Pass `{ view_spec: 'deploy-receipt-view.v1',
583
+ verified: true, verify_status, currently_authorized, bounds… }` produced by a
584
+ verification the host attributes. Stamp with `asVerifiedDeployReceiptView`.
585
+ The marker is the same idiom as `proof_spec` / `attestation_spec`.
586
+
587
+ A bare `{ currently_authorized: true }` with **no** `view_spec` and **no**
588
+ token is DENY `unverified_receipt_view` — not `receipt_not_authorized`.
574
589
 
575
- Same shape as the merge gate (`gateDecision` is pure; GitHub enforces outside us). Deploy differs:
576
- there is no PR status check to hang on, and **the environment name is a host assertion** — the
577
- receipt’s `bound_environment` is signed evidence; the target environment string is not. A host can
578
- claim `staging` while deploying to production; the package cannot tell. The result always reports
579
- `environment.provenance: 'host_asserted'` (never `verified`).
590
+ `g.verification = { mode, verify_status }` is observation (not a preimage).
591
+ The environment name remains a **host assertion**
592
+ (`environment.provenance: 'host_asserted'`).
580
593
 
581
594
  ```typescript
582
- import { bindDeploy } from '@coderifts/agent-guard';
595
+ import { bindDeploy, asVerifiedDeployReceiptView, deployGate } from '@coderifts/agent-guard';
596
+
597
+ // TOKEN (recommended)
598
+ const g = deployGate({
599
+ deployTarget: { environment: 'production', artifact_id: digest },
600
+ token: { token, decision_result, registry }, // or pinnedKeyPem
601
+ requiredContext: { operation: 'deploy', enforcement: { enforcement: 'ENFORCING', bypass_possible: false } },
602
+ });
603
+ // g.verification.mode === 'token'
583
604
 
605
+ // VERIFIED-VIEW (host already verified)
584
606
  const r = bindDeploy({
585
607
  environment: { name: 'production', provenance: 'host_asserted' },
586
608
  artifact_id: digest,
587
- receipt: receiptView, // finished view — not re-verified here
609
+ receipt: asVerifiedDeployReceiptView(computedView, 'VERIFIED_CURRENT'),
588
610
  pipeline_enforcement: { enforcement: 'ENFORCING', bypass_possible: false },
589
611
  });
590
- // r.decision_allows_deploy pure decision only
591
- // r.pipeline_action — always 'not_observed' (we did not block or run the pipeline)
592
- // r.environment.provenance — always 'host_asserted'
593
- // r.inescapable_deploy — only true under ENFORCING ∧ ¬bypass (from the gate, not a host flag)
594
- if (!r.decision_allows_deploy) process.exit(1); // host pipeline chooses to honour
612
+ if (!r.decision_allows_deploy) process.exit(1);
595
613
  ```
596
614
 
597
615
  #### `deployGate` is also the publish / register gate
@@ -604,24 +622,19 @@ verdict, enforcement residual). Prefer **`bindDeploy`** at the live step; raw `d
604
622
  for hosts that already build the full input.
605
623
 
606
624
  ```typescript
607
- import { deployGate } from '@coderifts/agent-guard';
625
+ import { deployGate, asVerifiedDeployReceiptView } from '@coderifts/agent-guard';
608
626
 
609
- // npm publish — same gate, operation label 'publish'
610
627
  const g = deployGate({
611
628
  deployTarget: {
612
629
  environment: 'npm',
613
630
  artifact_id: 'sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
614
631
  },
615
- // Receipt view: server receipt must have been issued for operation 'publish' (T7 match).
616
- // bound_environment / bound_artifact_id are NOT server-emitted signed fields — the caller
617
- // supplies them on this view alongside the token (deploy-gate's "view of the finished receipt").
618
- receipt,
632
+ receipt: asVerifiedDeployReceiptView(publishView, 'VERIFIED_CURRENT'),
619
633
  requiredContext: {
620
634
  operation: 'publish',
621
635
  enforcement: { enforcement: 'ENFORCING', bypass_possible: false },
622
636
  },
623
637
  });
624
- // g.deploy_allowed — decision flag (name is historical; see caveat)
625
638
  ```
626
639
 
627
640
  MCP **register** is the same shape: set `operation: 'register'` and put the **manifest digest** in
@@ -0,0 +1,74 @@
1
+ /**
2
+ * S1 auto-derive — wrap-layer before/after pair from the tool call + current-state readers.
3
+ *
4
+ * Default OFF. Frozen guardToolCall is unchanged. Host args.artifacts always win.
5
+ * BEFORE comes from a reader of the SAME target (fs path / api / db / registry).
6
+ * AFTER is the call's intended write content. Missing target → before:null
7
+ * (derivation_note before_unavailable); empty file → before:"".
8
+ *
9
+ * Reader throw/timeout → today's fragment path (binder lift); never block on derivation.
10
+ * Observation-only: derivation is not a fingerprint preimage field.
11
+ */
12
+ import type { ArtifactType, ResolveConfig } from './artifact-resolver.js';
13
+ import type { GuardConfig, ToolCallDescriptor } from './types.js';
14
+ export declare const AUTO_DERIVE_SOURCE: "guard_auto_derived";
15
+ export declare const AUTO_DERIVE_READ_TIMEOUT_MS = 2000;
16
+ export type AutoDeriveMode = 'host_supplied' | 'auto_derived' | 'fragment_only';
17
+ export type AutoDeriveTargetKind = 'fs' | 'api' | 'db' | 'registry';
18
+ export type AutoDeriveTarget = {
19
+ kind: AutoDeriveTargetKind;
20
+ key: string;
21
+ };
22
+ export type AutoDeriveNote = {
23
+ target: string;
24
+ note: 'before_unavailable' | 'reader_threw' | 'reader_timeout' | 'not_contract_path';
25
+ };
26
+ export type AutoDeriveObservation = {
27
+ derivation?: {
28
+ mode: AutoDeriveMode;
29
+ targets: ReadonlyArray<AutoDeriveTarget>;
30
+ notes?: ReadonlyArray<AutoDeriveNote>;
31
+ };
32
+ };
33
+ export type AutoDeriveReader = (key: string) => string | null | Promise<string | null>;
34
+ /** Current-state readers. Return null = absent (not empty). Throw/timeout → fragment fallback. */
35
+ export type AutoDeriveReaders = {
36
+ fs?: AutoDeriveReader;
37
+ api?: AutoDeriveReader;
38
+ db?: AutoDeriveReader;
39
+ registry?: AutoDeriveReader;
40
+ };
41
+ export type AutoDeriveConfig = {
42
+ readers?: AutoDeriveReaders;
43
+ /** CAP-202 resolve() config (pathTypeHints, ssotPrefer). Optional; not required for fs reads. */
44
+ resolveConfig?: ResolveConfig;
45
+ };
46
+ export type DerivedArtifact = {
47
+ id: string;
48
+ type: ArtifactType;
49
+ before: string | null;
50
+ after: string;
51
+ source: typeof AUTO_DERIVE_SOURCE;
52
+ };
53
+ /** Default fs reader: utf8 contents, ENOENT → null (absent ≠ empty). Other errors throw. */
54
+ export declare function defaultFsReader(filePath: string): Promise<string | null>;
55
+ export declare function normalizeAutoDerive(raw: unknown): AutoDeriveConfig | null;
56
+ export type AutoDeriveResult = {
57
+ call: ToolCallDescriptor;
58
+ derivation: NonNullable<AutoDeriveObservation['derivation']>;
59
+ failed?: {
60
+ cause: string;
61
+ };
62
+ };
63
+ /**
64
+ * Derive a before/after pair onto the call descriptor when the host did not supply artifacts.
65
+ * Never throws out — reader failures return the bound call with fragment_only + failed.cause.
66
+ */
67
+ export declare function runAutoDerive(args: {
68
+ bound: ToolCallDescriptor;
69
+ rawArgs: unknown;
70
+ cfg: AutoDeriveConfig;
71
+ config: GuardConfig;
72
+ }): Promise<AutoDeriveResult>;
73
+ export declare function attachDerivation<T>(outcome: T, derivation: NonNullable<AutoDeriveObservation['derivation']> | null | undefined): T;
74
+ //# sourceMappingURL=auto-derive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-derive.d.ts","sourceRoot":"","sources":["../../src/auto-derive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,KAAK,EAAE,WAAW,EAAc,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AAM,eAAe,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,OAAO,kBAAkB,CAAC;CACnC,CAAC;AAYF,4FAA4F;AAC5F,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAU9E;AAkBD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,GAAG,IAAI,CAgBzE;AA0FD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,kBAAkB,CAAC;IACzB,UAAU,EAAE,WAAW,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC,CAAC;IAC7D,MAAM,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CAC5B,CAAC;AAEF;;;GAGG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE;IACxC,KAAK,EAAE,kBAAkB,CAAC;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,gBAAgB,CAAC;IACtB,MAAM,EAAE,WAAW,CAAC;CACrB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAmG5B;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,OAAO,EAAE,CAAC,EACV,UAAU,EAAE,WAAW,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,GAC9E,CAAC,CAGH"}
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ /**
3
+ * S1 auto-derive — wrap-layer before/after pair from the tool call + current-state readers.
4
+ *
5
+ * Default OFF. Frozen guardToolCall is unchanged. Host args.artifacts always win.
6
+ * BEFORE comes from a reader of the SAME target (fs path / api / db / registry).
7
+ * AFTER is the call's intended write content. Missing target → before:null
8
+ * (derivation_note before_unavailable); empty file → before:"".
9
+ *
10
+ * Reader throw/timeout → today's fragment path (binder lift); never block on derivation.
11
+ * Observation-only: derivation is not a fingerprint preimage field.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.AUTO_DERIVE_READ_TIMEOUT_MS = exports.AUTO_DERIVE_SOURCE = void 0;
15
+ exports.defaultFsReader = defaultFsReader;
16
+ exports.normalizeAutoDerive = normalizeAutoDerive;
17
+ exports.runAutoDerive = runAutoDerive;
18
+ exports.attachDerivation = attachDerivation;
19
+ const node_fs_1 = require("node:fs");
20
+ const artifact_resolver_js_1 = require("./artifact-resolver.js");
21
+ exports.AUTO_DERIVE_SOURCE = 'guard_auto_derived';
22
+ exports.AUTO_DERIVE_READ_TIMEOUT_MS = 2000;
23
+ function iso() {
24
+ return new Date().toISOString();
25
+ }
26
+ function emit(config, e) {
27
+ if (config.onEvent) {
28
+ try {
29
+ config.onEvent(e);
30
+ }
31
+ catch { /* onEvent never throws out */ }
32
+ }
33
+ }
34
+ /** Default fs reader: utf8 contents, ENOENT → null (absent ≠ empty). Other errors throw. */
35
+ async function defaultFsReader(filePath) {
36
+ try {
37
+ return await node_fs_1.promises.readFile(filePath, 'utf8');
38
+ }
39
+ catch (err) {
40
+ const code = err && typeof err === 'object' && 'code' in err
41
+ ? String(err.code)
42
+ : '';
43
+ if (code === 'ENOENT')
44
+ return null;
45
+ throw err;
46
+ }
47
+ }
48
+ function looksLikeResolveConfig(o) {
49
+ return 'ssotPrefer' in o
50
+ || 'generatedGlobs' in o
51
+ || 'pathTypeHints' in o
52
+ || 'openApiAssembly' in o
53
+ || 'maxRefDepth' in o;
54
+ }
55
+ function looksLikeReaders(o) {
56
+ const r = o;
57
+ return typeof r.fs === 'function'
58
+ || typeof r.api === 'function'
59
+ || typeof r.db === 'function'
60
+ || typeof r.registry === 'function';
61
+ }
62
+ function normalizeAutoDerive(raw) {
63
+ if (raw === true)
64
+ return { readers: { fs: defaultFsReader } };
65
+ if (!raw || raw === false || typeof raw !== 'object')
66
+ return null;
67
+ const o = raw;
68
+ const cfg = {};
69
+ if (o.resolveConfig && typeof o.resolveConfig === 'object')
70
+ cfg.resolveConfig = o.resolveConfig;
71
+ if (o.readers && typeof o.readers === 'object') {
72
+ if (looksLikeReaders(o.readers)) {
73
+ cfg.readers = { ...o.readers };
74
+ }
75
+ else if (looksLikeResolveConfig(o.readers)) {
76
+ cfg.resolveConfig = o.readers;
77
+ }
78
+ }
79
+ if (!cfg.readers)
80
+ cfg.readers = {};
81
+ if (typeof cfg.readers.fs !== 'function')
82
+ cfg.readers.fs = defaultFsReader;
83
+ return cfg;
84
+ }
85
+ function rawRecord(args) {
86
+ if (!args || typeof args !== 'object' || Array.isArray(args))
87
+ return null;
88
+ return args;
89
+ }
90
+ function hostSuppliedArtifacts(args) {
91
+ const a = rawRecord(args);
92
+ return !!(a && Array.isArray(a.artifacts));
93
+ }
94
+ function targetFromArgs(a) {
95
+ if (typeof a.path === 'string' && a.path.length > 0)
96
+ return { kind: 'fs', key: a.path };
97
+ if (typeof a.url === 'string' && a.url.length > 0)
98
+ return { kind: 'api', key: a.url };
99
+ if (typeof a.endpoint === 'string' && a.endpoint.length > 0)
100
+ return { kind: 'api', key: a.endpoint };
101
+ if (typeof a.table === 'string' && a.table.length > 0) {
102
+ const id = a.id != null ? String(a.id) : '';
103
+ return { kind: 'db', key: id ? `${a.table}/${id}` : a.table };
104
+ }
105
+ if (typeof a.key === 'string' && a.key.length > 0)
106
+ return { kind: 'registry', key: a.key };
107
+ return null;
108
+ }
109
+ function intendedAfter(a, before) {
110
+ for (const k of ['contents', 'content', 'new_content', 'new_contents']) {
111
+ if (typeof a[k] === 'string')
112
+ return a[k];
113
+ }
114
+ const oldS = typeof a.old_string === 'string' ? a.old_string : null;
115
+ const newS = typeof a.new_string === 'string' ? a.new_string : null;
116
+ if (newS == null)
117
+ return null;
118
+ if (typeof before === 'string' && oldS && oldS.length > 0 && before.includes(oldS)) {
119
+ return before.replace(oldS, newS);
120
+ }
121
+ if (newS.length > 0)
122
+ return newS;
123
+ return null;
124
+ }
125
+ function typeForTarget(target, a, resolveConfig) {
126
+ if (typeof a.type === 'string' && a.type) {
127
+ const t = a.type;
128
+ if (t === 'openapi' || t === 'graphql' || t === 'grpc' || t === 'asyncapi' || t === 'mcp_manifest') {
129
+ return t;
130
+ }
131
+ }
132
+ if (target.kind === 'fs') {
133
+ const hinted = resolveConfig && resolveConfig.pathTypeHints
134
+ ? resolveConfig.pathTypeHints[target.key]
135
+ : undefined;
136
+ if (hinted)
137
+ return hinted;
138
+ return (0, artifact_resolver_js_1.classifyByName)(target.key);
139
+ }
140
+ return null;
141
+ }
142
+ async function withTimeout(p, ms) {
143
+ let timer;
144
+ try {
145
+ return await Promise.race([
146
+ p,
147
+ new Promise((_, reject) => {
148
+ timer = setTimeout(() => {
149
+ const e = new Error(`autoDerive reader timed out after ${ms}ms`);
150
+ e.name = 'TimeoutError';
151
+ reject(e);
152
+ }, Math.max(1, ms));
153
+ }),
154
+ ]);
155
+ }
156
+ finally {
157
+ if (timer)
158
+ clearTimeout(timer);
159
+ }
160
+ }
161
+ async function readCurrent(readers, target) {
162
+ const fn = readers[target.kind];
163
+ if (typeof fn !== 'function') {
164
+ const err = new Error(`autoDerive: no ${target.kind} reader`);
165
+ err.code = 'NO_READER';
166
+ throw err;
167
+ }
168
+ return withTimeout(Promise.resolve(fn(target.key)), exports.AUTO_DERIVE_READ_TIMEOUT_MS);
169
+ }
170
+ /**
171
+ * Derive a before/after pair onto the call descriptor when the host did not supply artifacts.
172
+ * Never throws out — reader failures return the bound call with fragment_only + failed.cause.
173
+ */
174
+ async function runAutoDerive(args) {
175
+ const bound = args.bound;
176
+ if (hostSuppliedArtifacts(args.rawArgs)) {
177
+ return {
178
+ call: bound,
179
+ derivation: { mode: 'host_supplied', targets: [] },
180
+ };
181
+ }
182
+ const a = rawRecord(args.rawArgs) || rawRecord(bound.arguments);
183
+ if (!a) {
184
+ return { call: bound, derivation: { mode: 'fragment_only', targets: [] } };
185
+ }
186
+ const target = targetFromArgs(a);
187
+ if (!target) {
188
+ return { call: bound, derivation: { mode: 'fragment_only', targets: [] } };
189
+ }
190
+ const type = typeForTarget(target, a, args.cfg.resolveConfig);
191
+ if (!type) {
192
+ return {
193
+ call: bound,
194
+ derivation: {
195
+ mode: 'fragment_only',
196
+ targets: [target],
197
+ notes: [{ target: target.key, note: 'not_contract_path' }],
198
+ },
199
+ };
200
+ }
201
+ const readers = args.cfg.readers || { fs: defaultFsReader };
202
+ let before;
203
+ try {
204
+ before = await readCurrent(readers, target);
205
+ }
206
+ catch (err) {
207
+ const name = err && typeof err === 'object' && 'name' in err
208
+ ? String(err.name)
209
+ : '';
210
+ const cause = name === 'TimeoutError' ? 'reader_timeout' : 'reader_threw';
211
+ emit(args.config, { type: 'derive_failed', at: iso(), cause });
212
+ return {
213
+ call: bound,
214
+ derivation: {
215
+ mode: 'fragment_only',
216
+ targets: [target],
217
+ notes: [{
218
+ target: target.key,
219
+ note: cause === 'reader_timeout' ? 'reader_timeout' : 'reader_threw',
220
+ }],
221
+ },
222
+ failed: { cause },
223
+ };
224
+ }
225
+ const after = intendedAfter(a, before);
226
+ if (after == null) {
227
+ return {
228
+ call: bound,
229
+ derivation: { mode: 'fragment_only', targets: [target] },
230
+ };
231
+ }
232
+ const notes = [];
233
+ if (before === null) {
234
+ notes.push({ target: target.key, note: 'before_unavailable' });
235
+ // Absence must not erase a binder-lifted fragment pair (old_string/new_string).
236
+ if (Array.isArray(bound.artifacts) && bound.artifacts.length > 0) {
237
+ return {
238
+ call: bound,
239
+ derivation: {
240
+ mode: 'fragment_only',
241
+ targets: [target],
242
+ notes,
243
+ },
244
+ };
245
+ }
246
+ }
247
+ const derived = {
248
+ id: `${type}:${target.key}`,
249
+ type,
250
+ before,
251
+ after,
252
+ source: exports.AUTO_DERIVE_SOURCE,
253
+ };
254
+ const call = {
255
+ ...bound,
256
+ artifacts: [derived],
257
+ };
258
+ return {
259
+ call,
260
+ derivation: {
261
+ mode: 'auto_derived',
262
+ targets: [target],
263
+ ...(notes.length ? { notes } : {}),
264
+ },
265
+ };
266
+ }
267
+ function attachDerivation(outcome, derivation) {
268
+ if (!derivation)
269
+ return outcome;
270
+ return { ...outcome, derivation };
271
+ }
272
+ //# sourceMappingURL=auto-derive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-derive.js","sourceRoot":"","sources":["../../src/auto-derive.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;AAqEH,0CAUC;AAkBD,kDAgBC;AAoGD,sCAwGC;AAED,4CAMC;AAnUD,qCAA0C;AAC1C,iEAAwD;AAI3C,QAAA,kBAAkB,GAAG,oBAA6B,CAAC;AACnD,QAAA,2BAA2B,GAAG,IAAI,CAAC;AAkDhD,SAAS,GAAG;IACV,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,IAAI,CAAC,MAAmB,EAAE,CAAa;IAC9C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,IAAI,CAAC;YAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,8BAA8B,CAAC,CAAC;IACrE,CAAC;AACH,CAAC;AAED,4FAA4F;AACrF,KAAK,UAAU,eAAe,CAAC,QAAgB;IACpD,IAAI,CAAC;QACH,OAAO,MAAM,kBAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC9C,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,MAAM,IAAI,GAAG,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,IAAI,GAAG;YAC1D,CAAC,CAAC,MAAM,CAAE,GAAyB,CAAC,IAAI,CAAC;YACzC,CAAC,CAAC,EAAE,CAAC;QACP,IAAI,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACnC,MAAM,GAAG,CAAC;I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,eAAe;oBACrB,OAAO,EAAE,CAAC,MAAM,CAAC;oBACjB,KAAK;iBACN;aACF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAoB;QAC/B,EAAE,EAAE,GAAG,IAAI,IAAI,MAAM,CAAC,GAAG,EAAE;QAC3B,IAAI;QACJ,MAAM;QACN,KAAK;QACL,MAAM,EAAE,0BAAkB;KAC3B,CAAC;IAEF,MAAM,IAAI,GAAuB;QAC/B,GAAG,KAAK;QACR,SAAS,EAAE,CAAC,OAA8B,CAAC;KAC5C,CAAC;IACF,OAAO;QACL,IAAI;QACJ,UAAU,EAAE;YACV,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,CAAC,MAAM,CAAC;YACjB,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACnC;KACF,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAC9B,OAAU,EACV,UAA+E;IAE/E,IAAI,CAAC,UAAU;QAAE,OAAO,OAAO,CAAC;IAChC,OAAO,EAAE,GAAI,OAAkB,EAAE,UAAU,EAAO,CAAC;AACrD,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * S6 auto-recheck loop — host-side orchestration AFTER a BLOCK with a remediation_transaction.
3
+ *
4
+ * Default OFF. The frozen guardToolCall is unchanged; this wraps it. The guard never writes
5
+ * artifacts: the host's applyFix applies the fix (honesty line). After applyFix true, artifacts
6
+ * are re-resolved FRESH (resolve() from artifact-resolver when resolveInput is supplied, else
7
+ * the host-updated call / rebind).
8
+ *
9
+ * recheck_scope is an OUTPUT on the BLOCK envelope (app remediation-transaction.js). The
10
+ * preflight request does not accept a scope hint (measured SDK / authorizeChangeSet) — so
11
+ * each attempt is a FULL re-preflight. Noted, not faked.
12
+ *
13
+ * Nothing here enters a fingerprint preimage (host orchestration only).
14
+ */
15
+ import type { GuardConfig, GuardOutcome, ToolCallDescriptor, ExecuteFactory, GuardToolCallContext } from './types.js';
16
+ import type { RemediationTransactionView } from './remediation-loop-attestation.js';
17
+ import type { ResolveInput, ResolveConfig } from './artifact-resolver.js';
18
+ /** Hard cap on RE-preflights per original call. */
19
+ export declare const AUTO_RECHECK_MAX_CAP = 3;
20
+ export type RecheckTrailEntry = {
21
+ attempt: number;
22
+ decision_id: string | null;
23
+ fingerprint: string | null;
24
+ execution_action: string | null;
25
+ };
26
+ export type RecheckStopReason = 'no_progress' | 'apply_fix_declined' | 'apply_fix_threw' | 'exhausted';
27
+ export type RecheckObservation = {
28
+ recheck_trail?: ReadonlyArray<RecheckTrailEntry>;
29
+ /** 849 metric feed. Present only when the loop ran. value true iff a recheck landed allow-class. */
30
+ fixed_after_block?: {
31
+ value: true | null;
32
+ attempt_count: number | null;
33
+ };
34
+ recheck_stop_reason?: RecheckStopReason;
35
+ };
36
+ export type AutoRecheckApplyFixContext = {
37
+ call: ToolCallDescriptor;
38
+ attempt: number;
39
+ outcome: GuardOutcome<unknown>;
40
+ };
41
+ export type AutoRecheckConfig = {
42
+ /**
43
+ * Bound on RE-preflights per original call. Allowed 1–3; values above 3 clamp to 3.
44
+ * Values below 1 disable the loop.
45
+ */
46
+ maxAttempts: number;
47
+ /**
48
+ * HOST applies the fix. The guard never writes artifacts.
49
+ * false / throw → loop ends, original BLOCK stands.
50
+ */
51
+ applyFix: (remediation: RemediationTransactionView, context: AutoRecheckApplyFixContext) => boolean | Promise<boolean>;
52
+ /**
53
+ * Optional. After applyFix true, produce a fresh git snapshot for resolve() (CAP-202).
54
+ * When absent, the loop re-runs `rebind` / uses artifacts the host attached on context.call.
55
+ */
56
+ resolveInput?: () => ResolveInput | Promise<ResolveInput>;
57
+ resolveConfig?: ResolveConfig;
58
+ };
59
+ export declare function clampMaxAttempts(n: unknown): number;
60
+ export declare function normalizeAutoRecheck(raw: unknown): AutoRecheckConfig | null;
61
+ export type AutoRecheckLoopArgs<T> = {
62
+ call: ToolCallDescriptor;
63
+ factory: ExecuteFactory<T>;
64
+ config: GuardConfig;
65
+ callContext?: GuardToolCallContext;
66
+ rebind: () => ToolCallDescriptor | Promise<ToolCallDescriptor>;
67
+ /** Re-collect freshness VALUES from the FRESH call (full re-preflight). */
68
+ refreshContext?: (call: ToolCallDescriptor) => Promise<GuardToolCallContext | undefined>;
69
+ };
70
+ /**
71
+ * First call is always a normal guardToolCall. The loop runs only when autoRecheck is
72
+ * normalized (applyFix present, maxAttempts in 1–3) AND the outcome is BLOCK with a
73
+ * remediation_transaction.
74
+ */
75
+ export declare function runAutoRecheckLoop<T>(args: AutoRecheckLoopArgs<T>): Promise<GuardOutcome<T>>;
76
+ //# sourceMappingURL=auto-recheck.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-recheck.d.ts","sourceRoot":"","sources":["../../src/auto-recheck.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EAEZ,kBAAkB,EAClB,cAAc,EAEd,oBAAoB,EAGrB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE1E,mDAAmD;AACnD,eAAO,MAAM,oBAAoB,IAAI,CAAC;AAEtC,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,iBAAiB,GACzB,aAAa,GACb,oBAAoB,GACpB,iBAAiB,GACjB,WAAW,CAAC;AAEhB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,aAAa,CAAC,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAA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