@sema-agent/core 5.22.0 → 5.24.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 (59) hide show
  1. package/CHANGELOG.md +169 -1
  2. package/dist/agents/subagent.js +3 -2
  3. package/dist/core/checkpoint-store.d.ts +38 -3
  4. package/dist/core/checkpoint-store.js +2 -1
  5. package/dist/core/governance-codes.js +3 -0
  6. package/dist/core/hooks.d.ts +69 -2
  7. package/dist/core/hooks.js +100 -15
  8. package/dist/core/memory-engine/engine.d.ts +28 -1
  9. package/dist/core/memory-engine/engine.js +62 -3
  10. package/dist/core/memory-engine/index.d.ts +1 -1
  11. package/dist/core/memory-engine/index.js +1 -1
  12. package/dist/core/memory-engine/layout.d.ts +69 -3
  13. package/dist/core/memory-engine/layout.js +75 -6
  14. package/dist/core/permission-rule-consent.js +2 -1
  15. package/dist/core/permission-rule-org.d.ts +36 -2
  16. package/dist/core/permission-rule-org.js +23 -0
  17. package/dist/core/permission-rule-store.d.ts +25 -14
  18. package/dist/core/permission-rule-store.js +7 -2
  19. package/dist/core/permission-rule-sync.d.ts +8 -0
  20. package/dist/core/permission-rule-sync.js +35 -6
  21. package/dist/core/runner/prepare-task.d.ts +10 -2
  22. package/dist/core/runner/prepare-task.js +120 -11
  23. package/dist/core/runner/runtask.js +46 -1
  24. package/dist/core/runner/session-file-state-replay.js +3 -0
  25. package/dist/core/tool-policy.d.ts +37 -4
  26. package/dist/core/tool-policy.js +49 -19
  27. package/dist/core/tool-result-store.d.ts +17 -1
  28. package/dist/core/tool-result-store.js +79 -4
  29. package/dist/core/trace.d.ts +47 -0
  30. package/dist/core/types.d.ts +45 -5
  31. package/dist/core/wiring-manifest.d.ts +16 -1
  32. package/dist/core/wiring-manifest.js +7 -1
  33. package/dist/index.d.ts +18 -10
  34. package/dist/index.js +5 -3
  35. package/dist/orchestration/goal.d.ts +10 -0
  36. package/dist/orchestration/goal.js +6 -5
  37. package/dist/stores/file/adoption/adopt.d.ts +146 -0
  38. package/dist/stores/file/adoption/adopt.js +611 -0
  39. package/dist/stores/file/adoption/marker.d.ts +202 -0
  40. package/dist/stores/file/adoption/marker.js +205 -0
  41. package/dist/stores/file/background-agent-store.js +2 -0
  42. package/dist/stores/file/checkpoint-store.js +2 -0
  43. package/dist/stores/file/file-snapshot-store.js +2 -0
  44. package/dist/stores/file/index.d.ts +2 -0
  45. package/dist/stores/file/index.js +4 -0
  46. package/dist/stores/file/mailbox-store.js +2 -0
  47. package/dist/stores/file/memory-store.js +2 -0
  48. package/dist/stores/file/session-policy-store.d.ts +11 -1
  49. package/dist/stores/file/session-policy-store.js +9 -2
  50. package/dist/stores/file/session-store.js +2 -0
  51. package/dist/stores/file/task-list-store.js +2 -0
  52. package/dist/stores/file/tool-result-store.js +2 -0
  53. package/dist/stores/file/usage-window-store.js +2 -0
  54. package/dist/stores/file/workflow-journal-store.js +2 -0
  55. package/dist/stores/file/workflow-run-store.js +2 -0
  56. package/dist/tools/fs/bash-readonly-classifier.js +59 -10
  57. package/dist/tools/fs/fs-bash.js +7 -4
  58. package/dist/tools/monitor.js +3 -3
  59. package/package.json +3 -2
@@ -0,0 +1,146 @@
1
+ /**
2
+ * design/183 (F-011) — `adoptLocalDataRoot`: the ROOT-level adoption state machine for the file
3
+ * backend. It generalizes the design/182 rule-bucket arc (exclusive lock + durable intent marker +
4
+ * monotone phase CAS + permanent terminal record + quarantine-not-delete) to the WHOLE data root:
5
+ *
6
+ * ① take the data-root locks (the engine's own boot lock + a dedicated ADOPTION-LOCK) — a live
7
+ * writer refuses the adoption loudly instead of interleaving with it
8
+ * ② land the durable root intent marker `adoption.json` → phase 2
9
+ * ③ identity-axis rebind legs (the ONLY stores whose data carries identity):
10
+ * - permission rules: the design/182 machine, NESTED as-is — never re-implemented. Its own
11
+ * sub-arc (including its first sync round) runs here; legs are independent, the root phase
12
+ * only orders "all legs of a stage complete before the stage advances"
13
+ * - session-policy rows: per-row ATOMIC REWRITE (the filename is a one-way hash of the
14
+ * composite key, so a rename cannot rebind; the `__principal` metadata must change too)
15
+ * → phase 3
16
+ * ④ resolution switch: publishing phase 4 is itself the switch (the marker is the truth host
17
+ * configuration follows, never the reverse), and the affected-deployment CONFIG ACCOUNT is
18
+ * landed — configs the machine cannot reach are honest `migrated: false` entries that stay
19
+ * outstanding until a consumer WITNESSES them → phase 4
20
+ * ⑤ carriage legs (data to the cloud): pluggable, idempotent, re-run whole on resume; rows the
21
+ * far side refuses land in the per-store QUARANTINE area — preserved and disclosed, never
22
+ * deleted, and never blocking the arc (a quarantined row is a COMPLETED disposition)
23
+ * → phase 5
24
+ * ⑥ the marker is atomically rewritten into its PERMANENT terminal record carrying the immutable
25
+ * report. Never deleted; a re-run short-circuits to it.
26
+ *
27
+ * FREEZE (design/183 §8 load-bearing premise): from the moment ② lands, the whole root is read-only
28
+ * for everyone but this machine — live writers are excluded by the locks, and across crashes every
29
+ * file store constructor refuses the in-flight marker (invariant I6, `assertAdoptionBootGate`).
30
+ * Idempotent resume ("re-run completes the unfinished legs") is only sound because no per-store row
31
+ * moves underneath it; the freeze is the premise of idempotence, not an implementation detail.
32
+ *
33
+ * What this half deliberately does NOT do: bulk data carriage for the zero-identity stores (session,
34
+ * checkpoint, memory, …) — ownership of those rows rides with the carriage legs (server bulk-import
35
+ * endpoints / existing sync faces), which plug into ⑤ through {@link AdoptionCarriageLeg}. Core's
36
+ * file half ships the freeze face, the acceptance face and the rebind legs.
37
+ */
38
+ import type { PermissionRuleSyncTransport } from "../../../core/permission-rule-sync.js";
39
+ import { type AdoptionReceipt, type AdoptionSource, type AffectedDeploymentConfig } from "./marker.js";
40
+ /** The per-leg context handed to a carriage leg: rows the far side REFUSES go here — preserved and
41
+ * disclosed, never deleted (design/183 §7.2). Quarantining never fails the leg. */
42
+ export interface AdoptionCarriageLegContext {
43
+ quarantine: (name: string, bytes: string) => void;
44
+ }
45
+ /**
46
+ * One pluggable carriage leg for stage ⑤ (design/183 §5). MUST be idempotent: a resume re-runs every
47
+ * carriage leg in full (a sync round is a join; an import is row-keyed) — only legs WITHOUT a
48
+ * self-attesting completion predicate record completion bits, and carriage legs all have one (the
49
+ * far side's row set). `run` resolves with the carried row count; a throw stalls the arc at the
50
+ * ④→⑤ boundary and the marker keeps the root frozen until a re-run completes the leg.
51
+ */
52
+ export interface AdoptionCarriageLeg {
53
+ /** Store family name as it should appear in the report legs (e.g. "session", "memory"). */
54
+ store: string;
55
+ run: (ctx: AdoptionCarriageLegContext) => Promise<{
56
+ rows?: number;
57
+ } | void>;
58
+ }
59
+ export interface AdoptLocalDataRootOptions {
60
+ /** The data root (the directory `FileStorageBackend` and the independent file stores build over). */
61
+ root: string;
62
+ from: AdoptionSource;
63
+ toPrincipal: string;
64
+ /** The claim REFERENCE for the new identity's credentials (minted by the server; design/183 §4.3).
65
+ * A reference, never the credential bytes — the marker file is a plain-text sidecar. */
66
+ credentials: {
67
+ issuedBy: "server";
68
+ ref: string;
69
+ };
70
+ /** The permission-rule bucket leg (design/182, nested unchanged). Absent ⇒ the deployment has no
71
+ * file rule bucket and the leg is skipped. */
72
+ rules?: {
73
+ dir: string;
74
+ transport: PermissionRuleSyncTransport;
75
+ };
76
+ /** Stage-⑤ carriage legs (server bulk-import / existing sync faces). All must complete for the arc
77
+ * to reach its terminal record. */
78
+ carriage?: AdoptionCarriageLeg[];
79
+ /** Extra affected-deployment configs beyond the built-in minimum face (design/183 §6: the table may
80
+ * GROW, never shrink — an entry with the same (deployment, key) overrides the built-in values). */
81
+ additionalConfigs?: AffectedDeploymentConfig[];
82
+ now?: () => number;
83
+ onError?: (message: string) => void;
84
+ }
85
+ export type AdoptLocalDataRootResult = AdoptionReceipt
86
+ /** A leg failed. The marker holds the last COMPLETED phase and keeps the root frozen (I6); call
87
+ * again to resume from there. No bytes were lost — that is the state machine's whole contract. */
88
+ | {
89
+ status: "stalled";
90
+ phase: 2 | 3 | 4 | 5;
91
+ error: string;
92
+ };
93
+ /** A consumer's typed read-back receipt: proof it OBSERVED the required value at runtime, bound to
94
+ * this adoption. Only this clears an account entry — an operator ack is recorded, never clearing. */
95
+ export interface AdoptionConfigWitnessReceipt {
96
+ adoptionId: string;
97
+ deployment: string;
98
+ key: string;
99
+ observedValue: string;
100
+ atMs: number;
101
+ }
102
+ /** Record an operator acknowledgement on one account entry. Recorded, NEVER clearing: the account's
103
+ * discriminating power comes from consumer witness, not from a hand-written boolean (design/183 §3.3). */
104
+ export declare function ackAdoptionConfig(root: string, deployment: string, key: string, now?: () => number): void;
105
+ /**
106
+ * Clear one config-account entry with a consumer's bound runtime read-back receipt. The receipt must
107
+ * name THIS adoption and carry the entry's exact required value — anything else is refused (typed):
108
+ * a mis-bound witness would clear the account while the deployment is still locked out.
109
+ */
110
+ export declare function witnessAdoptionConfig(root: string, receipt: AdoptionConfigWitnessReceipt): void;
111
+ /** Enumerate the quarantine area (introspection face — a quarantined row is DISCLOSED, not buried). */
112
+ export declare function listAdoptionQuarantine(root: string): Array<{
113
+ store: string;
114
+ name: string;
115
+ path: string;
116
+ }>;
117
+ /** One root's adoption posture, readable WITHOUT the adoption parameters (the standing
118
+ * introspection face design/183 §3.3 requires: while the config account is uncleared, the
119
+ * outstanding list stays loudly readable — a standing answer, not a one-shot log line). */
120
+ export type AdoptionStatus = {
121
+ state: "none";
122
+ } | {
123
+ state: "in-flight";
124
+ from: AdoptionSource;
125
+ toPrincipal: string;
126
+ phase: 2 | 3 | 4 | 5;
127
+ } | {
128
+ state: "adopted";
129
+ receipt: AdoptionReceipt;
130
+ };
131
+ /** Read the root's adoption posture: never throws on absence, throws (typed) on a corrupt marker. */
132
+ export declare function readAdoptionStatus(root: string): AdoptionStatus;
133
+ /**
134
+ * Run (or resume) the root-level adoption state machine over the data root (design/183 §3).
135
+ *
136
+ * Contract notes for the calling deployment:
137
+ * - stop the engine first: this machine takes the SAME boot lock (`root/LOCK`) as
138
+ * `FileStorageBackend`, so a live instance refuses the adoption loudly rather than interleaving —
139
+ * plus the dedicated `ADOPTION-LOCK` against a concurrent adoption of independently-constructed
140
+ * stores. Across crashes the in-flight marker + the I6 boot gate carry the same exclusion;
141
+ * - idempotent: a re-run with the same (from → toPrincipal) answers the recorded receipt without
142
+ * re-running any leg; a re-run toward a DIFFERENT principal is refused (typed);
143
+ * - quarantined rows never block the arc, and post-terminal repair rides each store's NORMAL
144
+ * channel (sync round / import generation — design/183 §7.2), never a re-opened adoption.
145
+ */
146
+ export declare function adoptLocalDataRoot(opts: AdoptLocalDataRootOptions): Promise<AdoptLocalDataRootResult>;