@skillcap/gdh 0.23.0 → 0.25.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 (165) hide show
  1. package/INSTALL-BUNDLE.json +1 -1
  2. package/RELEASE-SPAN-UPDATE-CONTRACTS.json +122 -0
  3. package/node_modules/@gdh/adapters/dist/authoring-hook-render.d.ts.map +1 -1
  4. package/node_modules/@gdh/adapters/dist/authoring-hook-render.js +9 -131
  5. package/node_modules/@gdh/adapters/dist/authoring-hook-render.js.map +1 -1
  6. package/node_modules/@gdh/adapters/dist/claude-statusline-render.d.ts.map +1 -1
  7. package/node_modules/@gdh/adapters/dist/claude-statusline-render.js +6 -28
  8. package/node_modules/@gdh/adapters/dist/claude-statusline-render.js.map +1 -1
  9. package/node_modules/@gdh/adapters/dist/claude-update-hook-render.d.ts.map +1 -1
  10. package/node_modules/@gdh/adapters/dist/claude-update-hook-render.js +7 -40
  11. package/node_modules/@gdh/adapters/dist/claude-update-hook-render.js.map +1 -1
  12. package/node_modules/@gdh/adapters/dist/claude-update-worker-render.d.ts.map +1 -1
  13. package/node_modules/@gdh/adapters/dist/claude-update-worker-render.js +7 -68
  14. package/node_modules/@gdh/adapters/dist/claude-update-worker-render.js.map +1 -1
  15. package/node_modules/@gdh/adapters/dist/deferred-actions-advisory.d.ts +71 -0
  16. package/node_modules/@gdh/adapters/dist/deferred-actions-advisory.d.ts.map +1 -0
  17. package/node_modules/@gdh/adapters/dist/deferred-actions-advisory.js +89 -0
  18. package/node_modules/@gdh/adapters/dist/deferred-actions-advisory.js.map +1 -0
  19. package/node_modules/@gdh/adapters/dist/durable-backup.d.ts +209 -0
  20. package/node_modules/@gdh/adapters/dist/durable-backup.d.ts.map +1 -0
  21. package/node_modules/@gdh/adapters/dist/durable-backup.js +346 -0
  22. package/node_modules/@gdh/adapters/dist/durable-backup.js.map +1 -0
  23. package/node_modules/@gdh/adapters/dist/index.d.ts +11 -3
  24. package/node_modules/@gdh/adapters/dist/index.d.ts.map +1 -1
  25. package/node_modules/@gdh/adapters/dist/index.js +79 -43
  26. package/node_modules/@gdh/adapters/dist/index.js.map +1 -1
  27. package/node_modules/@gdh/adapters/dist/inventory-sweep.d.ts +53 -0
  28. package/node_modules/@gdh/adapters/dist/inventory-sweep.d.ts.map +1 -0
  29. package/node_modules/@gdh/adapters/dist/inventory-sweep.js +98 -0
  30. package/node_modules/@gdh/adapters/dist/inventory-sweep.js.map +1 -0
  31. package/node_modules/@gdh/adapters/dist/process-orchestration.d.ts +223 -0
  32. package/node_modules/@gdh/adapters/dist/process-orchestration.d.ts.map +1 -0
  33. package/node_modules/@gdh/adapters/dist/process-orchestration.js +368 -0
  34. package/node_modules/@gdh/adapters/dist/process-orchestration.js.map +1 -0
  35. package/node_modules/@gdh/adapters/dist/self-update-mechanics.d.ts +157 -14
  36. package/node_modules/@gdh/adapters/dist/self-update-mechanics.d.ts.map +1 -1
  37. package/node_modules/@gdh/adapters/dist/self-update-mechanics.js +570 -89
  38. package/node_modules/@gdh/adapters/dist/self-update-mechanics.js.map +1 -1
  39. package/node_modules/@gdh/adapters/dist/skill-rendering.d.ts +2 -5
  40. package/node_modules/@gdh/adapters/dist/skill-rendering.d.ts.map +1 -1
  41. package/node_modules/@gdh/adapters/dist/skill-rendering.js +39 -56
  42. package/node_modules/@gdh/adapters/dist/skill-rendering.js.map +1 -1
  43. package/node_modules/@gdh/adapters/dist/template-assets.d.ts +2 -0
  44. package/node_modules/@gdh/adapters/dist/template-assets.d.ts.map +1 -0
  45. package/node_modules/@gdh/adapters/dist/template-assets.js +26 -0
  46. package/node_modules/@gdh/adapters/dist/template-assets.js.map +1 -0
  47. package/node_modules/@gdh/adapters/dist/templates/authoring-hook.js.tpl +128 -0
  48. package/node_modules/@gdh/adapters/dist/templates/claude-check-update-hook.js.tpl +37 -0
  49. package/node_modules/@gdh/adapters/dist/templates/claude-check-update-worker.js.tpl +65 -0
  50. package/node_modules/@gdh/adapters/dist/templates/claude-statusline.js.tpl +25 -0
  51. package/node_modules/@gdh/adapters/package.json +8 -8
  52. package/node_modules/@gdh/authoring/package.json +2 -2
  53. package/node_modules/@gdh/cli/dist/index.d.ts +9 -0
  54. package/node_modules/@gdh/cli/dist/index.d.ts.map +1 -1
  55. package/node_modules/@gdh/cli/dist/index.js +249 -6
  56. package/node_modules/@gdh/cli/dist/index.js.map +1 -1
  57. package/node_modules/@gdh/cli/dist/migrate.d.ts +152 -1
  58. package/node_modules/@gdh/cli/dist/migrate.d.ts.map +1 -1
  59. package/node_modules/@gdh/cli/dist/migrate.js +355 -8
  60. package/node_modules/@gdh/cli/dist/migrate.js.map +1 -1
  61. package/node_modules/@gdh/cli/dist/self-update.d.ts +14 -0
  62. package/node_modules/@gdh/cli/dist/self-update.d.ts.map +1 -1
  63. package/node_modules/@gdh/cli/dist/self-update.js +197 -15
  64. package/node_modules/@gdh/cli/dist/self-update.js.map +1 -1
  65. package/node_modules/@gdh/cli/dist/setup.d.ts +4 -0
  66. package/node_modules/@gdh/cli/dist/setup.d.ts.map +1 -1
  67. package/node_modules/@gdh/cli/dist/setup.js +8 -67
  68. package/node_modules/@gdh/cli/dist/setup.js.map +1 -1
  69. package/node_modules/@gdh/cli/package.json +10 -10
  70. package/node_modules/@gdh/core/dist/index.d.ts +99 -5
  71. package/node_modules/@gdh/core/dist/index.d.ts.map +1 -1
  72. package/node_modules/@gdh/core/dist/index.js +24 -5
  73. package/node_modules/@gdh/core/dist/index.js.map +1 -1
  74. package/node_modules/@gdh/core/dist/migrations/entries/s2c2_to_s2c3_rules_schema_v2_to_v3.d.ts +3 -0
  75. package/node_modules/@gdh/core/dist/migrations/entries/s2c2_to_s2c3_rules_schema_v2_to_v3.d.ts.map +1 -0
  76. package/node_modules/@gdh/core/dist/migrations/entries/s2c2_to_s2c3_rules_schema_v2_to_v3.js +247 -0
  77. package/node_modules/@gdh/core/dist/migrations/entries/s2c2_to_s2c3_rules_schema_v2_to_v3.js.map +1 -0
  78. package/node_modules/@gdh/core/dist/migrations/entries/s3c8_to_s3c9_register_runtime_bridge_autoload.d.ts +3 -0
  79. package/node_modules/@gdh/core/dist/migrations/entries/s3c8_to_s3c9_register_runtime_bridge_autoload.d.ts.map +1 -0
  80. package/node_modules/@gdh/core/dist/migrations/entries/s3c8_to_s3c9_register_runtime_bridge_autoload.js +152 -0
  81. package/node_modules/@gdh/core/dist/migrations/entries/s3c8_to_s3c9_register_runtime_bridge_autoload.js.map +1 -0
  82. package/node_modules/@gdh/core/dist/migrations/envelopes/envelope-output-validator.d.ts +3 -0
  83. package/node_modules/@gdh/core/dist/migrations/envelopes/envelope-output-validator.d.ts.map +1 -0
  84. package/node_modules/@gdh/core/dist/migrations/envelopes/envelope-output-validator.js +67 -0
  85. package/node_modules/@gdh/core/dist/migrations/envelopes/envelope-output-validator.js.map +1 -0
  86. package/node_modules/@gdh/core/dist/migrations/envelopes/index.d.ts +37 -0
  87. package/node_modules/@gdh/core/dist/migrations/envelopes/index.d.ts.map +1 -0
  88. package/node_modules/@gdh/core/dist/migrations/envelopes/index.js +60 -0
  89. package/node_modules/@gdh/core/dist/migrations/envelopes/index.js.map +1 -0
  90. package/node_modules/@gdh/core/dist/migrations/envelopes/types.d.ts +121 -0
  91. package/node_modules/@gdh/core/dist/migrations/envelopes/types.d.ts.map +1 -0
  92. package/node_modules/@gdh/core/dist/migrations/envelopes/types.js +2 -0
  93. package/node_modules/@gdh/core/dist/migrations/envelopes/types.js.map +1 -0
  94. package/node_modules/@gdh/core/dist/migrations/golden-harness.d.ts +40 -0
  95. package/node_modules/@gdh/core/dist/migrations/golden-harness.d.ts.map +1 -0
  96. package/node_modules/@gdh/core/dist/migrations/golden-harness.js +71 -0
  97. package/node_modules/@gdh/core/dist/migrations/golden-harness.js.map +1 -0
  98. package/node_modules/@gdh/core/dist/migrations/managed-surface-classes.d.ts +322 -0
  99. package/node_modules/@gdh/core/dist/migrations/managed-surface-classes.d.ts.map +1 -0
  100. package/node_modules/@gdh/core/dist/migrations/managed-surface-classes.js +384 -0
  101. package/node_modules/@gdh/core/dist/migrations/managed-surface-classes.js.map +1 -0
  102. package/node_modules/@gdh/core/dist/migrations/managed-target-surface-inventory.d.ts +294 -0
  103. package/node_modules/@gdh/core/dist/migrations/managed-target-surface-inventory.d.ts.map +1 -0
  104. package/node_modules/@gdh/core/dist/migrations/managed-target-surface-inventory.js +365 -0
  105. package/node_modules/@gdh/core/dist/migrations/managed-target-surface-inventory.js.map +1 -0
  106. package/node_modules/@gdh/core/dist/migrations/probes.d.ts +58 -0
  107. package/node_modules/@gdh/core/dist/migrations/probes.d.ts.map +1 -0
  108. package/node_modules/@gdh/core/dist/migrations/probes.js +112 -0
  109. package/node_modules/@gdh/core/dist/migrations/probes.js.map +1 -0
  110. package/node_modules/@gdh/core/dist/migrations/registry.d.ts +205 -0
  111. package/node_modules/@gdh/core/dist/migrations/registry.d.ts.map +1 -0
  112. package/node_modules/@gdh/core/dist/migrations/registry.js +214 -0
  113. package/node_modules/@gdh/core/dist/migrations/registry.js.map +1 -0
  114. package/node_modules/@gdh/core/dist/state/atomic-write.d.ts +19 -0
  115. package/node_modules/@gdh/core/dist/state/atomic-write.d.ts.map +1 -0
  116. package/node_modules/@gdh/core/dist/state/atomic-write.js +34 -0
  117. package/node_modules/@gdh/core/dist/state/atomic-write.js.map +1 -0
  118. package/node_modules/@gdh/core/dist/state/migration-state.d.ts +135 -0
  119. package/node_modules/@gdh/core/dist/state/migration-state.d.ts.map +1 -0
  120. package/node_modules/@gdh/core/dist/state/migration-state.js +186 -0
  121. package/node_modules/@gdh/core/dist/state/migration-state.js.map +1 -0
  122. package/node_modules/@gdh/core/dist/state/processes-snapshot.d.ts +72 -0
  123. package/node_modules/@gdh/core/dist/state/processes-snapshot.d.ts.map +1 -0
  124. package/node_modules/@gdh/core/dist/state/processes-snapshot.js +113 -0
  125. package/node_modules/@gdh/core/dist/state/processes-snapshot.js.map +1 -0
  126. package/node_modules/@gdh/core/dist/state/render-inventory.d.ts +54 -0
  127. package/node_modules/@gdh/core/dist/state/render-inventory.d.ts.map +1 -0
  128. package/node_modules/@gdh/core/dist/state/render-inventory.js +77 -0
  129. package/node_modules/@gdh/core/dist/state/render-inventory.js.map +1 -0
  130. package/node_modules/@gdh/core/package.json +1 -1
  131. package/node_modules/@gdh/docs/dist/agent-contract.d.ts +2 -1
  132. package/node_modules/@gdh/docs/dist/agent-contract.d.ts.map +1 -1
  133. package/node_modules/@gdh/docs/dist/agent-contract.js +5 -3
  134. package/node_modules/@gdh/docs/dist/agent-contract.js.map +1 -1
  135. package/node_modules/@gdh/docs/dist/guidance.d.ts +2 -0
  136. package/node_modules/@gdh/docs/dist/guidance.d.ts.map +1 -1
  137. package/node_modules/@gdh/docs/dist/guidance.js +29 -255
  138. package/node_modules/@gdh/docs/dist/guidance.js.map +1 -1
  139. package/node_modules/@gdh/docs/dist/index.d.ts +1 -1
  140. package/node_modules/@gdh/docs/dist/index.d.ts.map +1 -1
  141. package/node_modules/@gdh/docs/dist/index.js +1 -1
  142. package/node_modules/@gdh/docs/dist/index.js.map +1 -1
  143. package/node_modules/@gdh/docs/dist/query.d.ts.map +1 -1
  144. package/node_modules/@gdh/docs/dist/query.js +4 -5
  145. package/node_modules/@gdh/docs/dist/query.js.map +1 -1
  146. package/node_modules/@gdh/docs/dist/recovery-hints.js +1 -1
  147. package/node_modules/@gdh/docs/dist/recovery-hints.js.map +1 -1
  148. package/node_modules/@gdh/docs/dist/template-assets.d.ts +2 -0
  149. package/node_modules/@gdh/docs/dist/template-assets.d.ts.map +1 -0
  150. package/node_modules/@gdh/docs/dist/template-assets.js +26 -0
  151. package/node_modules/@gdh/docs/dist/template-assets.js.map +1 -0
  152. package/node_modules/@gdh/docs/dist/templates/guidance/authoring-and-validation.md.tpl +111 -0
  153. package/node_modules/@gdh/docs/dist/templates/guidance/gdh-glossary.md.tpl +34 -0
  154. package/node_modules/@gdh/docs/dist/templates/guidance/persistence-semantics.md.tpl +24 -0
  155. package/node_modules/@gdh/docs/dist/templates/guidance/project-migration.md.tpl +19 -0
  156. package/node_modules/@gdh/docs/dist/templates/guidance/project-surfaces.md.tpl +14 -0
  157. package/node_modules/@gdh/docs/package.json +2 -2
  158. package/node_modules/@gdh/mcp/package.json +8 -8
  159. package/node_modules/@gdh/observability/package.json +2 -2
  160. package/node_modules/@gdh/runtime/dist/bridge-surface.js +63 -2
  161. package/node_modules/@gdh/runtime/dist/bridge-surface.js.map +1 -1
  162. package/node_modules/@gdh/runtime/package.json +2 -2
  163. package/node_modules/@gdh/scan/package.json +3 -3
  164. package/node_modules/@gdh/verify/package.json +7 -7
  165. package/package.json +11 -11
@@ -0,0 +1,89 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { readMigrationState, runDeferredActionProbe, } from "@gdh/core";
4
+ import { BACKUP_STAGING_RELATIVE_PATH, readBackupManifest, } from "./durable-backup.js";
5
+ /**
6
+ * Read `migration.json`, run the probe per deferred action, and decide whether
7
+ * to emit the backup-cleanup advisory.
8
+ *
9
+ * Threat mitigations:
10
+ * - T-72-05-03 (TOCTOU): re-reads migration.json AND backup state freshly
11
+ * inside this function; no cached probe results are consumed across calls.
12
+ * - T-72-05-04 (malformed migration.json): `readMigrationState` returns null
13
+ * on parse/shape failure → `deferredActions` is `[]` and the advisory is
14
+ * gated through the standard truth table.
15
+ * - T-72-05-06 (stale probe → clean): the gate requires `status === "clean"`
16
+ * for every probe; `stale` and `pending` both block the advisory.
17
+ * - T-72-05-07 (interrupted migration): staging-dir check forces the advisory
18
+ * to null whenever `.gdh-state/backup.new/` exists.
19
+ *
20
+ * Defaults: `adapters.hostHarnessIntrospect = null` and `adapters.processProbe
21
+ * = null` (every probe degrades to `pending`, so the advisory does not spuriously
22
+ * fire when a host cannot evaluate the deferred conditions).
23
+ */
24
+ export async function computeDeferredActionsAdvisory(targetPath, adapters) {
25
+ const hostHarnessIntrospect = adapters?.hostHarnessIntrospect ?? null;
26
+ const processProbe = adapters?.processProbe ?? null;
27
+ const migrationState = await readMigrationState(targetPath);
28
+ const probedActions = [];
29
+ for (const action of migrationState?.deferred_actions ?? []) {
30
+ const result = await runDeferredActionProbe(action, {
31
+ targetPath,
32
+ hostHarnessIntrospect,
33
+ processProbe,
34
+ });
35
+ const entry = result.reason !== undefined
36
+ ? {
37
+ id: action.id,
38
+ description: action.description,
39
+ status: result.status,
40
+ reason: result.reason,
41
+ }
42
+ : {
43
+ id: action.id,
44
+ description: action.description,
45
+ status: result.status,
46
+ };
47
+ probedActions.push(entry);
48
+ }
49
+ // Phase 73 D-12 — envelope-pending fourth gate. The marker is the in-flight
50
+ // resume substrate (D-11); clearing the durable backup while a chain is
51
+ // paused mid-bump destroys the rollback substrate. The two advisories are
52
+ // mutually exclusive: when the envelope marker is set, the backup advisory
53
+ // MUST be null, regardless of whether the other backup-cleanup gates pass.
54
+ const pendingEnvelope = migrationState?.pending_envelope_resume ?? null;
55
+ const noEnvelopePending = pendingEnvelope === null;
56
+ // Gate evaluation (advisory truth table):
57
+ // (a) every probed action is clean (zero is trivially true)
58
+ // (b) `.gdh-state/backup/` is present (manifest readable)
59
+ // (c) `.gdh-state/backup.new/` is NOT present (no interrupted migration)
60
+ // (d) Phase 73 D-12: no envelope is pending (mutually exclusive with
61
+ // envelopeAdvisory)
62
+ const allClean = probedActions.every((a) => a.status === "clean");
63
+ const manifest = await readBackupManifest(targetPath);
64
+ const backupExists = manifest !== null;
65
+ const stagingPath = path.join(targetPath, BACKUP_STAGING_RELATIVE_PATH);
66
+ const stagingExists = await fs
67
+ .stat(stagingPath)
68
+ .then(() => true)
69
+ .catch(() => false);
70
+ const noMigrationInProgress = !stagingExists;
71
+ const backupAdvisory = allClean && backupExists && noMigrationInProgress && noEnvelopePending
72
+ ? {
73
+ command: "gdh migration clear-backups",
74
+ reason: "all_deferred_actions_clean",
75
+ from_pin: manifest.from_pin,
76
+ to_pin: manifest.to_pin,
77
+ }
78
+ : null;
79
+ const envelopeAdvisory = pendingEnvelope !== null
80
+ ? {
81
+ envelope_ref: pendingEnvelope.envelope_ref,
82
+ from_pin: pendingEnvelope.from_pin,
83
+ to_pin: pendingEnvelope.to_pin,
84
+ command: `gdh migration record-envelope-result ${pendingEnvelope.envelope_ref} --result-file <path>`,
85
+ }
86
+ : null;
87
+ return { deferredActions: probedActions, backupAdvisory, envelopeAdvisory };
88
+ }
89
+ //# sourceMappingURL=deferred-actions-advisory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deferred-actions-advisory.js","sourceRoot":"","sources":["../src/deferred-actions-advisory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EACL,kBAAkB,EAClB,sBAAsB,GAKvB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,4BAA4B,EAC5B,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAuD7B;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,8BAA8B,CAClD,UAAkB,EAClB,QAAiC;IAEjC,MAAM,qBAAqB,GACzB,QAAQ,EAAE,qBAAqB,IAAI,IAAI,CAAC;IAC1C,MAAM,YAAY,GAA2B,QAAQ,EAAE,YAAY,IAAI,IAAI,CAAC;IAE5E,MAAM,cAAc,GAAG,MAAM,kBAAkB,CAAC,UAAU,CAAC,CAAC;IAE5D,MAAM,aAAa,GAAmC,EAAE,CAAC;IACzD,KAAK,MAAM,MAAM,IAAI,cAAc,EAAE,gBAAgB,IAAI,EAAE,EAAE,CAAC;QAC5D,MAAM,MAAM,GAAG,MAAM,sBAAsB,CAAC,MAAM,EAAE;YAClD,UAAU;YACV,qBAAqB;YACrB,YAAY;SACb,CAAC,CAAC;QACH,MAAM,KAAK,GAAiC,MAAM,CAAC,MAAM,KAAK,SAAS;YACrE,CAAC,CAAC;gBACE,EAAE,EAAE,MAAM,CAAC,EAAE;gBACb,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,MAAM,E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,EAAE,QAAQ,CAAC,QAAQ;YAC3B,MAAM,EAAE,QAAQ,CAAC,MAAM;SACxB;QACH,CAAC,CAAC,IAAI,CAAC;IAEX,MAAM,gBAAgB,GACpB,eAAe,KAAK,IAAI;QACtB,CAAC,CAAC;YACE,YAAY,EAAE,eAAe,CAAC,YAAY;YAC1C,QAAQ,EAAE,eAAe,CAAC,QAAQ;YAClC,MAAM,EAAE,eAAe,CAAC,MAAM;YAC9B,OAAO,EAAE,wCAAwC,eAAe,CAAC,YAAY,uBAAuB;SACrG;QACH,CAAC,CAAC,IAAI,CAAC;IAEX,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,209 @@
1
+ import { type GdhMigrationVersionPair } from "@gdh/core";
2
+ /**
3
+ * Workspace-relative directory for the durable update backup.
4
+ *
5
+ * Single durable backup per STA-01 / D-14: this directory is overwritten
6
+ * (rm -rf semantics) on every successful backup write. Multi-backup retention
7
+ * is explicitly deferred (see 72-CONTEXT.md "Deferred Ideas").
8
+ */
9
+ export declare const BACKUP_RELATIVE_PATH = ".gdh-state/backup";
10
+ /**
11
+ * Workspace-relative staging directory used to make the backup write atomic
12
+ * at the directory level (Pitfall 6 / T-72-04-03). The staged tree is built
13
+ * here, then renamed onto BACKUP_RELATIVE_PATH so the prior backup is never
14
+ * absent from disk between rm-rf and write.
15
+ */
16
+ export declare const BACKUP_STAGING_RELATIVE_PATH = ".gdh-state/backup.new";
17
+ /** Workspace-relative path to the per-backup manifest. */
18
+ export declare const BACKUP_MANIFEST_RELATIVE_PATH = ".gdh-state/backup/manifest.json";
19
+ export type GdhBackupPreservationReason = "class_1_backup_then_overwrite";
20
+ export type GdhBackupMarkerState = "absent" | "present" | "not_applicable";
21
+ /**
22
+ * WFL-02 / D-24 preservation note attached to the backup manifest. Emitted
23
+ * when a class-1 file's GDH ownership marker is absent (user-edited drift)
24
+ * and the file is captured into the backup BEFORE re-rendering.
25
+ */
26
+ export interface GdhBackupPreservationNote {
27
+ readonly path: string;
28
+ readonly reason: GdhBackupPreservationReason;
29
+ readonly marker_state: GdhBackupMarkerState;
30
+ }
31
+ /**
32
+ * Per-path capture entry recorded in the backup manifest. `existed: false`
33
+ * matches the legacy in-memory rollbackOriginals null-content semantics: the
34
+ * file did not exist pre-update, so restore must `rm` the original path.
35
+ */
36
+ export interface GdhBackupCapturedPath {
37
+ readonly path: string;
38
+ readonly existed: boolean;
39
+ }
40
+ /**
41
+ * Backup manifest schema (D-13). Version-locked at 1; readers reject any
42
+ * other backup_version with `null` (degraded fallback) so a future schema
43
+ * bump does not silently corrupt restore.
44
+ */
45
+ export interface GdhBackupManifest {
46
+ readonly backup_version: 1;
47
+ readonly applied_at: string;
48
+ readonly from_pair: GdhMigrationVersionPair;
49
+ readonly to_pair: GdhMigrationVersionPair;
50
+ readonly from_pin: string;
51
+ readonly to_pin: string;
52
+ readonly captured_paths: readonly GdhBackupCapturedPath[];
53
+ readonly preservation_notes: readonly GdhBackupPreservationNote[];
54
+ }
55
+ export interface GdhWriteDurableBackupInput {
56
+ readonly targetPath: string;
57
+ readonly fromPair: GdhMigrationVersionPair;
58
+ readonly toPair: GdhMigrationVersionPair;
59
+ readonly fromPin: string;
60
+ readonly toPin: string;
61
+ /** Workspace-relative POSIX paths to capture (planned action paths + .gdh/project.yaml). */
62
+ readonly plannedPaths: readonly string[];
63
+ /** WFL-02 preservation notes attached to the manifest. */
64
+ readonly preservationNotes?: readonly GdhBackupPreservationNote[];
65
+ /** Optional clock for tests; defaults to () => new Date().toISOString(). */
66
+ readonly nowIso?: () => string;
67
+ }
68
+ export interface GdhWriteDurableBackupSkipped {
69
+ readonly path: string;
70
+ readonly reason: "path_traversal_blocked";
71
+ }
72
+ export type GdhWriteDurableBackupResult = {
73
+ readonly state: "written";
74
+ readonly backupPath: string;
75
+ readonly manifest: GdhBackupManifest;
76
+ readonly skipped: readonly GdhWriteDurableBackupSkipped[];
77
+ } | {
78
+ readonly state: "skipped_no_planned_paths";
79
+ };
80
+ /**
81
+ * Write a single durable backup at .gdh-state/backup/ (STA-01 / D-13 / D-14).
82
+ *
83
+ * Staged-write strategy (T-72-04-03 mitigation, Pitfall 6):
84
+ * 1. Pre-clean .gdh-state/backup.new/ from any prior interrupted run.
85
+ * 2. Capture every plannedPaths entry into .gdh-state/backup.new/, mirroring
86
+ * the workspace-relative directory structure. Missing files are recorded
87
+ * in the manifest with existed: false but no file is written.
88
+ * 3. Write manifest.json into the staging directory via writeFileAtomic.
89
+ * 4. Remove the prior .gdh-state/backup/ (if any) and rename the staging
90
+ * directory onto it.
91
+ *
92
+ * If interrupted between steps 2 and 4, the prior .gdh-state/backup/ is
93
+ * unchanged and the next call's pre-clean (step 1) removes the partial
94
+ * staging tree before it can corrupt anything.
95
+ *
96
+ * Path-traversal candidates (failing isWorkspaceRelativePath) are recorded
97
+ * in `skipped` with `path_traversal_blocked` and never written or captured.
98
+ */
99
+ export declare function writeDurableBackup(input: GdhWriteDurableBackupInput): Promise<GdhWriteDurableBackupResult>;
100
+ /**
101
+ * Read the backup manifest from .gdh-state/backup/manifest.json.
102
+ *
103
+ * Returns null on any failure (missing file, JSON parse error, schema
104
+ * mismatch). Never throws — degraded fallback so callers can branch on
105
+ * "no backup present" without try/catch ceremony, and so a corrupted
106
+ * manifest cannot trigger a partial restore (T-72-04-06).
107
+ */
108
+ export declare function readBackupManifest(targetPath: string): Promise<GdhBackupManifest | null>;
109
+ export type GdhRestoreFromDurableBackupResult = {
110
+ readonly state: "restored";
111
+ readonly restored: readonly string[];
112
+ readonly removed: readonly string[];
113
+ readonly skipped: readonly {
114
+ readonly path: string;
115
+ readonly reason: string;
116
+ }[];
117
+ /**
118
+ * WR-06: Count of captured_paths entries that failed to restore (i.e.,
119
+ * the length of `skipped`). Surfaced as a top-level field so callers
120
+ * can branch on actual restoration success without re-scanning the
121
+ * `skipped` array. When `restored.length === 0 && removed.length === 0
122
+ * && failedCount > 0`, the rollback is effectively a NO-RESTORE: every
123
+ * captured path failed (e.g., disk full, EROFS, EACCES) and the
124
+ * caller should NOT report this as a clean rollback. Pre-existing
125
+ * "best-effort" semantics are preserved (the function still returns
126
+ * \`state: "restored"\` to keep the discriminated-union contract
127
+ * stable for existing callers); the new field is additive.
128
+ */
129
+ readonly failedCount: number;
130
+ } | {
131
+ readonly state: "no_backup_present";
132
+ };
133
+ /**
134
+ * Restore byte-equal content for every captured_paths entry in the backup
135
+ * manifest. Mirrors the legacy in-memory rollbackOriginals semantics:
136
+ *
137
+ * - existed: true → atomic-write the backup content over the original path
138
+ * - existed: false → fs.rm the original path (file did not exist pre-update)
139
+ *
140
+ * The backup directory itself is read-only during restore (T-72-04 contract):
141
+ * each restore is a fresh copy from .gdh-state/backup/, so running restore
142
+ * twice produces the same final state.
143
+ *
144
+ * Path-traversal candidates in captured_paths are skipped with a structured
145
+ * warning (T-72-04-01); the rest of the restore still completes. If the
146
+ * backup content for an existed:true entry is missing on disk, that entry
147
+ * is skipped with reason "backup_content_missing" rather than aborting.
148
+ */
149
+ export declare function restoreFromDurableBackup(input: {
150
+ readonly targetPath: string;
151
+ }): Promise<GdhRestoreFromDurableBackupResult>;
152
+ export interface GdhClass1MarkerDriftInput {
153
+ readonly targetPath: string;
154
+ /**
155
+ * Workspace-relative POSIX paths of class-1 (deterministic) managed files
156
+ * that the planned action set will re-render.
157
+ */
158
+ readonly plannedClass1Paths: readonly string[];
159
+ /**
160
+ * Optional per-path expected markers. Overrides KNOWN_GDH_MARKERS for
161
+ * files whose format ships its own marker convention. Use for class-1
162
+ * file types not covered by the default list (rare).
163
+ */
164
+ readonly expectedMarkersByPath?: ReadonlyMap<string, readonly string[]>;
165
+ }
166
+ export interface GdhClass1MarkerDriftEntry {
167
+ readonly path: string;
168
+ readonly markerState: GdhBackupMarkerState;
169
+ }
170
+ export interface GdhClass1MarkerDriftResult {
171
+ readonly driftedPaths: readonly GdhClass1MarkerDriftEntry[];
172
+ readonly absentPaths: readonly string[];
173
+ readonly skipped: readonly {
174
+ readonly path: string;
175
+ readonly reason: string;
176
+ }[];
177
+ }
178
+ /**
179
+ * Detect WFL-02 / D-24 class-1 marker drift.
180
+ *
181
+ * For each `plannedClass1Paths` entry that exists on disk, check whether the
182
+ * file contains any of its expected GDH ownership markers:
183
+ * - Marker present → file is GDH-rendered; no drift; not flagged.
184
+ * - Marker absent (but file exists) → user-edited drift; flagged with
185
+ * marker_state: "absent".
186
+ * - Per-path override supplied but no marker matches → conservative
187
+ * marker_state: "not_applicable" + flagged as drifted (T-72-04-07: err on
188
+ * the side of preservation when the marker convention is uncertain).
189
+ * - File does not exist on disk → no drift; reported in absentPaths so
190
+ * the caller knows the file will be rendered fresh (no preservation).
191
+ *
192
+ * Path-traversal candidates (failing isWorkspaceRelativePath) are skipped
193
+ * with structured warnings (T-72-04-01); never read.
194
+ *
195
+ * Pair the result with `driftResultToPreservationNotes` to produce the
196
+ * preservation-notes payload for `writeDurableBackup`.
197
+ */
198
+ export declare function detectClass1MarkerDrift(input: GdhClass1MarkerDriftInput): Promise<GdhClass1MarkerDriftResult>;
199
+ /**
200
+ * Convert a drift-detection result into preservation notes ready to attach
201
+ * to a `writeDurableBackup` call.
202
+ *
203
+ * Per D-24, every drifted path is preserved with reason
204
+ * `class_1_backup_then_overwrite`; the marker_state propagates from the
205
+ * drift entry so the manifest carries an audit trail of the detection
206
+ * outcome (absent vs not_applicable).
207
+ */
208
+ export declare function driftResultToPreservationNotes(drift: GdhClass1MarkerDriftResult): readonly GdhBackupPreservationNote[];
209
+ //# sourceMappingURL=durable-backup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"durable-backup.d.ts","sourceRoot":"","sources":["../src/durable-backup.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,WAAW,CAAC;AAEnB;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBAAsB,CAAC;AACxD;;;;;GAKG;AACH,eAAO,MAAM,4BAA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ileAtomic, } from "@gdh/core";
4
+ /**
5
+ * Workspace-relative directory for the durable update backup.
6
+ *
7
+ * Single durable backup per STA-01 / D-14: this directory is overwritten
8
+ * (rm -rf semantics) on every successful backup write. Multi-backup retention
9
+ * is explicitly deferred (see 72-CONTEXT.md "Deferred Ideas").
10
+ */
11
+ export const BACKUP_RELATIVE_PATH = ".gdh-state/backup";
12
+ /**
13
+ * Workspace-relative staging directory used to make the backup write atomic
14
+ * at the directory level (Pitfall 6 / T-72-04-03). The staged tree is built
15
+ * here, then renamed onto BACKUP_RELATIVE_PATH so the prior backup is never
16
+ * absent from disk between rm-rf and write.
17
+ */
18
+ export const BACKUP_STAGING_RELATIVE_PATH = ".gdh-state/backup.new";
19
+ /** Workspace-relative path to the per-backup manifest. */
20
+ export const BACKUP_MANIFEST_RELATIVE_PATH = ".gdh-state/backup/manifest.json";
21
+ /**
22
+ * Write a single durable backup at .gdh-state/backup/ (STA-01 / D-13 / D-14).
23
+ *
24
+ * Staged-write strategy (T-72-04-03 mitigation, Pitfall 6):
25
+ * 1. Pre-clean .gdh-state/backup.new/ from any prior interrupted run.
26
+ * 2. Capture every plannedPaths entry into .gdh-state/backup.new/, mirroring
27
+ * the workspace-relative directory structure. Missing files are recorded
28
+ * in the manifest with existed: false but no file is written.
29
+ * 3. Write manifest.json into the staging directory via writeFileAtomic.
30
+ * 4. Remove the prior .gdh-state/backup/ (if any) and rename the staging
31
+ * directory onto it.
32
+ *
33
+ * If interrupted between steps 2 and 4, the prior .gdh-state/backup/ is
34
+ * unchanged and the next call's pre-clean (step 1) removes the partial
35
+ * staging tree before it can corrupt anything.
36
+ *
37
+ * Path-traversal candidates (failing isWorkspaceRelativePath) are recorded
38
+ * in `skipped` with `path_traversal_blocked` and never written or captured.
39
+ */
40
+ export async function writeDurableBackup(input) {
41
+ if (input.plannedPaths.length === 0) {
42
+ return { state: "skipped_no_planned_paths" };
43
+ }
44
+ const stagingPath = path.join(input.targetPath, BACKUP_STAGING_RELATIVE_PATH);
45
+ const finalPath = path.join(input.targetPath, BACKUP_RELATIVE_PATH);
46
+ // Step 1: pre-clean staging from any prior interrupted run, then re-create it
47
+ await fs.rm(stagingPath, { recursive: true, force: true });
48
+ await fs.mkdir(stagingPath, { recursive: true });
49
+ // Step 2: capture each planned-path file into the staging tree
50
+ const capturedPaths = [];
51
+ const skipped = [];
52
+ // Deduplicate planned paths and sort for deterministic manifest ordering
53
+ const uniquePaths = [...new Set(input.plannedPaths)].sort();
54
+ for (const relPath of uniquePaths) {
55
+ if (!isWorkspaceRelativePath(relPath)) {
56
+ skipped.push({ path: relPath, reason: "path_traversal_blocked" });
57
+ continue;
58
+ }
59
+ const sourceAbs = path.join(input.targetPath, relPath);
60
+ const destAbs = path.join(stagingPath, relPath);
61
+ let content;
62
+ try {
63
+ content = await fs.readFile(sourceAbs, "utf8");
64
+ }
65
+ catch {
66
+ content = null;
67
+ }
68
+ if (content !== null) {
69
+ await fs.mkdir(path.dirname(destAbs), { recursive: true });
70
+ await fs.writeFile(destAbs, content);
71
+ capturedPaths.push({ path: relPath, existed: true });
72
+ }
73
+ else {
74
+ capturedPaths.push({ path: relPath, existed: false });
75
+ }
76
+ }
77
+ // Step 3: write manifest into the staging directory
78
+ const nowIso = input.nowIso ?? (() => new Date().toISOString());
79
+ const manifest = {
80
+ backup_version: 1,
81
+ applied_at: nowIso(),
82
+ from_pair: input.fromPair,
83
+ to_pair: input.toPair,
84
+ from_pin: input.fromPin,
85
+ to_pin: input.toPin,
86
+ captured_paths: capturedPaths,
87
+ preservation_notes: input.preservationNotes ?? [],
88
+ };
89
+ await writeFileAtomic(path.join(stagingPath, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`);
90
+ // Step 4: remove the old backup, then rename staging into place. The rename
91
+ // is atomic on POSIX filesystems for directories on the same device, and
92
+ // .gdh-state/ is always on the same device as itself by construction.
93
+ await fs.rm(finalPath, { recursive: true, force: true });
94
+ await fs.rename(stagingPath, finalPath);
95
+ return {
96
+ state: "written",
97
+ backupPath: finalPath,
98
+ manifest,
99
+ skipped,
100
+ };
101
+ }
102
+ // ---------------------------------------------------------------------------
103
+ // Manifest read + restore (T-72-04-06 schema guard; D-10 step rollback)
104
+ // ---------------------------------------------------------------------------
105
+ function isBackupCapturedPath(value) {
106
+ if (typeof value !== "object" || value === null)
107
+ return false;
108
+ const v = value;
109
+ return typeof v["path"] === "string" && typeof v["existed"] === "boolean";
110
+ }
111
+ function isBackupPreservationNote(value) {
112
+ if (typeof value !== "object" || value === null)
113
+ return false;
114
+ const v = value;
115
+ if (typeof v["path"] !== "string")
116
+ return false;
117
+ if (v["reason"] !== "class_1_backup_then_overwrite")
118
+ return false;
119
+ const ms = v["marker_state"];
120
+ return ms === "absent" || ms === "present" || ms === "not_applicable";
121
+ }
122
+ function isMigrationVersionPair(value) {
123
+ if (typeof value !== "object" || value === null)
124
+ return false;
125
+ const v = value;
126
+ return (typeof v["schema"] === "number" && typeof v["agentContract"] === "number");
127
+ }
128
+ function isBackupManifest(value) {
129
+ if (typeof value !== "object" || value === null)
130
+ return false;
131
+ const v = value;
132
+ if (v["backup_version"] !== 1)
133
+ return false;
134
+ if (typeof v["applied_at"] !== "string")
135
+ return false;
136
+ if (typeof v["from_pin"] !== "string")
137
+ return false;
138
+ if (typeof v["to_pin"] !== "string")
139
+ return false;
140
+ if (!isMigrationVersionPair(v["from_pair"]))
141
+ return false;
142
+ if (!isMigrationVersionPair(v["to_pair"]))
143
+ return false;
144
+ const captured = v["captured_paths"];
145
+ if (!Array.isArray(captured) || !captured.every(isBackupCapturedPath)) {
146
+ return false;
147
+ }
148
+ const notes = v["preservation_notes"];
149
+ if (!Array.isArray(notes) || !notes.every(isBackupPreservationNote)) {
150
+ return false;
151
+ }
152
+ return true;
153
+ }
154
+ /**
155
+ * Read the backup manifest from .gdh-state/backup/manifest.json.
156
+ *
157
+ * Returns null on any failure (missing file, JSON parse error, schema
158
+ * mismatch). Never throws — degraded fallback so callers can branch on
159
+ * "no backup present" without try/catch ceremony, and so a corrupted
160
+ * manifest cannot trigger a partial restore (T-72-04-06).
161
+ */
162
+ export async function readBackupManifest(targetPath) {
163
+ const manifestPath = path.join(targetPath, BACKUP_MANIFEST_RELATIVE_PATH);
164
+ const raw = await fs.readFile(manifestPath, "utf8").catch(() => null);
165
+ if (raw === null)
166
+ return null;
167
+ let parsed;
168
+ try {
169
+ parsed = JSON.parse(raw);
170
+ }
171
+ catch {
172
+ return null;
173
+ }
174
+ return isBackupManifest(parsed) ? parsed : null;
175
+ }
176
+ /**
177
+ * Restore byte-equal content for every captured_paths entry in the backup
178
+ * manifest. Mirrors the legacy in-memory rollbackOriginals semantics:
179
+ *
180
+ * - existed: true → atomic-write the backup content over the original path
181
+ * - existed: false → fs.rm the original path (file did not exist pre-update)
182
+ *
183
+ * The backup directory itself is read-only during restore (T-72-04 contract):
184
+ * each restore is a fresh copy from .gdh-state/backup/, so running restore
185
+ * twice produces the same final state.
186
+ *
187
+ * Path-traversal candidates in captured_paths are skipped with a structured
188
+ * warning (T-72-04-01); the rest of the restore still completes. If the
189
+ * backup content for an existed:true entry is missing on disk, that entry
190
+ * is skipped with reason "backup_content_missing" rather than aborting.
191
+ */
192
+ export async function restoreFromDurableBackup(input) {
193
+ const manifest = await readBackupManifest(input.targetPath);
194
+ if (manifest === null) {
195
+ return { state: "no_backup_present" };
196
+ }
197
+ const backupRoot = path.join(input.targetPath, BACKUP_RELATIVE_PATH);
198
+ const restored = [];
199
+ const removed = [];
200
+ const skipped = [];
201
+ for (const entry of manifest.captured_paths) {
202
+ if (!isWorkspaceRelativePath(entry.path)) {
203
+ skipped.push({ path: entry.path, reason: "path_traversal_blocked" });
204
+ continue;
205
+ }
206
+ const originalAbs = path.join(input.targetPath, entry.path);
207
+ if (entry.existed) {
208
+ const backupAbs = path.join(backupRoot, entry.path);
209
+ const content = await fs.readFile(backupAbs, "utf8").catch(() => null);
210
+ if (content === null) {
211
+ skipped.push({ path: entry.path, reason: "backup_content_missing" });
212
+ continue;
213
+ }
214
+ try {
215
+ await writeFileAtomic(originalAbs, content);
216
+ restored.push(entry.path);
217
+ }
218
+ catch (error) {
219
+ // Best-effort restore: a destination-collision (e.g. directory at the
220
+ // expected file path, EACCES, EROFS) becomes a skipped entry rather
221
+ // than aborting the rest of the restore. Mirrors legacy
222
+ // rollbackOriginals semantics — partial recovery is preferable to
223
+ // halting mid-rollback. T-72-04-04 (single-backup contract) is
224
+ // unchanged; this only widens which failure modes degrade gracefully.
225
+ skipped.push({
226
+ path: entry.path,
227
+ reason: `restore_failed:${error instanceof Error ? error.message : String(error)}`,
228
+ });
229
+ }
230
+ }
231
+ else {
232
+ try {
233
+ await fs.rm(originalAbs, { force: true });
234
+ removed.push(entry.path);
235
+ }
236
+ catch (error) {
237
+ // Same best-effort posture: fs.rm without recursive on a directory
238
+ // throws EISDIR; recursive removal of an unexpected directory at a
239
+ // file path is destructive (could surprise an operator who created
240
+ // the directory deliberately). Skip with a structured reason.
241
+ skipped.push({
242
+ path: entry.path,
243
+ reason: `remove_failed:${error instanceof Error ? error.message : String(error)}`,
244
+ });
245
+ }
246
+ }
247
+ }
248
+ return {
249
+ state: "restored",
250
+ restored,
251
+ removed,
252
+ skipped,
253
+ // WR-06: surface aggregate restore failure as a typed scalar so callers
254
+ // can branch on actual restoration success without re-scanning `skipped`.
255
+ failedCount: skipped.length,
256
+ };
257
+ }
258
+ // ---------------------------------------------------------------------------
259
+ // WFL-02 / D-24 — class-1 marker-drift detection
260
+ // ---------------------------------------------------------------------------
261
+ /**
262
+ * Known GDH ownership markers (per 72-RESEARCH.md "Established Patterns").
263
+ *
264
+ * Each class-1 file format ships at least one of these markers in its
265
+ * GDH-rendered output. The first match in a file proves "GDH-rendered,
266
+ * untouched" — no preservation needed. Absence of every marker (when the
267
+ * file otherwise exists at a class-1 path) proves user-edited drift and
268
+ * triggers WFL-02 backup-then-overwrite.
269
+ *
270
+ * Adding a new marker convention requires updating both this list AND the
271
+ * renderer that ships it; tests in `detectClass1MarkerDrift` cover each
272
+ * marker variant explicitly.
273
+ */
274
+ const KNOWN_GDH_MARKERS = [
275
+ "<codex_skill_adapter>",
276
+ "<!-- gdh managed -->",
277
+ "# gdh:managed-hook",
278
+ "<!-- GDH-MANAGED -->",
279
+ ];
280
+ /**
281
+ * Detect WFL-02 / D-24 class-1 marker drift.
282
+ *
283
+ * For each `plannedClass1Paths` entry that exists on disk, check whether the
284
+ * file contains any of its expected GDH ownership markers:
285
+ * - Marker present → file is GDH-rendered; no drift; not flagged.
286
+ * - Marker absent (but file exists) → user-edited drift; flagged with
287
+ * marker_state: "absent".
288
+ * - Per-path override supplied but no marker matches → conservative
289
+ * marker_state: "not_applicable" + flagged as drifted (T-72-04-07: err on
290
+ * the side of preservation when the marker convention is uncertain).
291
+ * - File does not exist on disk → no drift; reported in absentPaths so
292
+ * the caller knows the file will be rendered fresh (no preservation).
293
+ *
294
+ * Path-traversal candidates (failing isWorkspaceRelativePath) are skipped
295
+ * with structured warnings (T-72-04-01); never read.
296
+ *
297
+ * Pair the result with `driftResultToPreservationNotes` to produce the
298
+ * preservation-notes payload for `writeDurableBackup`.
299
+ */
300
+ export async function detectClass1MarkerDrift(input) {
301
+ const drifted = [];
302
+ const absent = [];
303
+ const skipped = [];
304
+ for (const relPath of input.plannedClass1Paths) {
305
+ if (!isWorkspaceRelativePath(relPath)) {
306
+ skipped.push({ path: relPath, reason: "path_traversal_blocked" });
307
+ continue;
308
+ }
309
+ const absolute = path.join(input.targetPath, relPath);
310
+ const content = await fs.readFile(absolute, "utf8").catch(() => null);
311
+ if (content === null) {
312
+ absent.push(relPath);
313
+ continue;
314
+ }
315
+ const customMarkers = input.expectedMarkersByPath?.get(relPath);
316
+ const markersToCheck = customMarkers ?? KNOWN_GDH_MARKERS;
317
+ const hasMarker = markersToCheck.some((marker) => content.includes(marker));
318
+ if (hasMarker) {
319
+ // file is GDH-rendered, no drift
320
+ continue;
321
+ }
322
+ // marker absent — flag as drifted. When a custom marker list was supplied
323
+ // but none matched, the marker convention is uncertain → use the
324
+ // conservative "not_applicable" state per T-72-04-07.
325
+ const markerState = customMarkers !== undefined ? "not_applicable" : "absent";
326
+ drifted.push({ path: relPath, markerState });
327
+ }
328
+ return { driftedPaths: drifted, absentPaths: absent, skipped };
329
+ }
330
+ /**
331
+ * Convert a drift-detection result into preservation notes ready to attach
332
+ * to a `writeDurableBackup` call.
333
+ *
334
+ * Per D-24, every drifted path is preserved with reason
335
+ * `class_1_backup_then_overwrite`; the marker_state propagates from the
336
+ * drift entry so the manifest carries an audit trail of the detection
337
+ * outcome (absent vs not_applicable).
338
+ */
339
+ export function driftResultToPreservationNotes(drift) {
340
+ return drift.driftedPaths.map((entry) => ({
341
+ path: entry.path,
342
+ reason: "class_1_backup_then_overwrite",
343
+ marker_state: entry.markerState,
344
+ }));
345
+ }
346
+ //# sourceMappingURL=durable-backup.js.map