lex_vcs/operation.rs
1//! The `Operation` enum + `OperationRecord` (operation plus its
2//! causal parents and resulting `OpId`).
3//!
4//! See `lib.rs` for the design context and #129 for the issue.
5
6use indexmap::IndexSet;
7use serde::{Deserialize, Serialize};
8use std::collections::{BTreeMap, BTreeSet};
9
10use crate::canonical;
11
12/// Signature identity of a function or type — the part that stays
13/// stable across body edits. Wraps the same string identity
14/// `lex-store` uses; we keep it as `String` here so this crate has
15/// no dependency on `lex-store`'s internals.
16pub type SigId = String;
17
18/// Content hash of a single stage (function body, type def, ...).
19/// Same string identity as the file under `<root>/stages/<SigId>/
20/// implementations/<StageId>.ast.json`.
21pub type StageId = String;
22
23/// Identity of an operation. `(kind, payload, parents)` SHA-256 in
24/// lowercase hex (64 chars). Two operations with identical payloads
25/// and parent sets produce identical `OpId`s; the store dedupes on
26/// this.
27pub type OpId = String;
28
29/// Sorted set of effect-kind strings (e.g. `["fs_write", "io"]`).
30/// `BTreeSet` so the canonical form is order-independent for
31/// hashing.
32pub type EffectSet = BTreeSet<String>;
33
34/// Reference to an imported module — either a stdlib name
35/// (`std.io`) or a local path (`./helpers`). Kept as a string so
36/// this crate doesn't pull in `lex-syntax`'s parser.
37pub type ModuleRef = String;
38
39/// The alias a module binds to when the import writes no explicit
40/// `as` — the module reference's last path segment, splitting on
41/// either `.` (stdlib, `std.sql` → `sql`) or `/` (local/package,
42/// `./error` → `error`, `lex-web/lib` → `lib`). Lex actually requires
43/// an explicit alias on every import, so this is not a language
44/// default; it is the convention `AddImport` uses to decide when an
45/// alias can be omitted from the op (keeping the `OpId` stable) and
46/// `export-git` uses to reconstruct it. The two MUST agree, so both
47/// call this one function.
48pub fn default_import_alias(module: &str) -> String {
49 module
50 .rsplit(['.', '/'])
51 .find(|seg| !seg.is_empty())
52 .unwrap_or(module)
53 .to_string()
54}
55
56/// Version tag for the operation canonical form (#244).
57///
58/// The pre-image bytes hashed to derive an `OpId` are not stable
59/// across schema evolutions: adding a field to `OperationKind` or
60/// changing its serde representation rotates every existing `OpId`.
61/// This enum tags the encoding used so a long-lived store can detect
62/// mismatches and migrate explicitly via [`crate::migrate`].
63///
64/// **Today only [`Self::V1`] is in production.** Adding a future
65/// variant requires:
66///
67/// 1. A new arm in [`Operation::canonical_bytes_in`].
68/// 2. An update to the canonical-form spec in [`crate::canonical`].
69/// 3. A `CHANGELOG.md` entry under `### Internal` calling out the
70/// `OpId` rotation.
71/// 4. A migration recipe via [`crate::migrate::plan_migration`] —
72/// the mechanism is encoder-agnostic, but each new variant needs
73/// its own `canonical_bytes_in` arm.
74#[derive(
75 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
76)]
77#[serde(rename_all = "lowercase")]
78pub enum OperationFormat {
79 #[default]
80 V1,
81}
82
83impl OperationFormat {
84 /// The format every newly-emitted op uses today.
85 pub const CURRENT: OperationFormat = OperationFormat::V1;
86
87 /// `true` for the implicit format (V1). Used by the
88 /// `skip_serializing_if` hook on [`OperationRecord::format_version`]
89 /// so existing V1 stores keep byte-identical on-disk JSON —
90 /// adding the version field doesn't itself rotate any `OpId`.
91 pub fn is_implicit(&self) -> bool {
92 matches!(self, OperationFormat::V1)
93 }
94}
95
96/// Effect of applying an operation on a stage's content-addressed
97/// identity. Used as the `produces` field of an [`OperationRecord`]
98/// so consumers can answer "after this op, what's the head stage
99/// for this SigId?" without rerunning the apply step.
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
101#[serde(tag = "kind", rename_all = "snake_case")]
102pub enum StageTransition {
103 /// New SigId; produces a stage that didn't exist before.
104 Create { sig_id: SigId, stage_id: StageId },
105 /// Existing SigId; replaces its head stage.
106 Replace { sig_id: SigId, from: StageId, to: StageId },
107 /// SigId removed; no head stage afterwards.
108 Remove { sig_id: SigId, last: StageId },
109 /// SigId renamed; same body hash, different signature identity.
110 Rename { from: SigId, to: SigId, body_stage_id: StageId },
111 /// Import-only change; doesn't touch any stage.
112 ImportOnly,
113 /// Merge op result. `entries` lists only the sigs whose head
114 /// changed relative to the merge op's first parent (`dst_head`):
115 /// `Some(stage_id)` sets the head; `None` removes the sig.
116 /// Sigs unaffected by the merge are not listed.
117 ///
118 /// **Canonical-form contract:** `BTreeMap` is load-bearing —
119 /// iteration is sorted by `SigId`, so on-disk JSON for two
120 /// callers that resolved the same conflicts in different
121 /// orders produces byte-identical output. Switching to
122 /// `HashMap` here would break canonical stability of the
123 /// `OperationRecord` JSON file and is rejected by the
124 /// canonical-form spec in `crate::canonical`.
125 Merge {
126 entries: BTreeMap<SigId, Option<StageId>>,
127 },
128}
129
130impl StageTransition {
131 /// Every stage id this transition references — the content-addressed
132 /// blobs a peer needs alongside the op record to render or replay it.
133 /// Used by `op push`/`pull` to sync stage objects, not just op records.
134 pub fn stage_ids(&self) -> Vec<StageId> {
135 match self {
136 StageTransition::Create { stage_id, .. } => vec![stage_id.clone()],
137 StageTransition::Replace { from, to, .. } => vec![from.clone(), to.clone()],
138 StageTransition::Remove { last, .. } => vec![last.clone()],
139 StageTransition::Rename { body_stage_id, .. } => vec![body_stage_id.clone()],
140 StageTransition::ImportOnly => Vec::new(),
141 StageTransition::Merge { entries } => entries.values().flatten().cloned().collect(),
142 }
143 }
144}
145
146/// The kinds of operations that produce stage transitions. Mirrors
147/// the initial set in #129; new kinds (`MoveBetweenFiles`,
148/// `SplitFunction`, `ExtractType`) can be added later as long as
149/// they're appended at the end of this enum or use explicit
150/// `#[serde(rename = "...")]` tags so existing `OpId`s stay stable.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152#[serde(tag = "op", rename_all = "snake_case")]
153pub enum OperationKind {
154 /// New function published. `effects` is the effect set declared
155 /// in the signature; tracked here (not just inside the stage)
156 /// so #130's write-time gate has a cheap path to check effect
157 /// changes without rehydrating the AST.
158 ///
159 /// `budget_cost` (#247) records the function's declared
160 /// `[budget(N)]` cost. Optional with `skip_serializing_if`, so
161 /// pre-#247 ops without a declared budget continue to hash to
162 /// their original `OpId` (additive serialization, same trick
163 /// `intent_id` uses). `None` means the function declared no
164 /// budget effect; `Some(n)` is the literal `n` from
165 /// `[budget(n)]`.
166 AddFunction {
167 sig_id: SigId,
168 stage_id: StageId,
169 effects: EffectSet,
170 #[serde(default, skip_serializing_if = "Option::is_none")]
171 budget_cost: Option<u64>,
172 },
173 /// Function removed; `last_stage_id` is the head before the
174 /// remove (so blame can walk the predecessor without scanning).
175 RemoveFunction {
176 sig_id: SigId,
177 last_stage_id: StageId,
178 },
179 /// Function body changed; signature unchanged.
180 ///
181 /// `from_budget` / `to_budget` (#247) record the declared
182 /// `[budget(N)]` on each side. Same `Option` + `skip` discipline
183 /// as `AddFunction.budget_cost` — pre-#247 ops keep their
184 /// `OpId`s. The pair is what `lex op log --budget-drift` reads
185 /// to surface "budget grew/shrank" diffs without rehydrating
186 /// stages.
187 ModifyBody {
188 sig_id: SigId,
189 from_stage_id: StageId,
190 to_stage_id: StageId,
191 #[serde(default, skip_serializing_if = "Option::is_none")]
192 from_budget: Option<u64>,
193 #[serde(default, skip_serializing_if = "Option::is_none")]
194 to_budget: Option<u64>,
195 },
196 /// Symbol renamed. The body hash is preserved (`body_stage_id`)
197 /// so two renames of the same body collapse to the same OpId
198 /// and `lex blame` walks the rename as a single causal event
199 /// rather than `delete + add`.
200 RenameSymbol {
201 from: SigId,
202 to: SigId,
203 body_stage_id: StageId,
204 },
205 /// Effect signature changed. Captures both old and new effect
206 /// sets so the write-time gate (#130) can verify importers
207 /// haven't silently broken.
208 ///
209 /// `from_budget` / `to_budget` (#247) capture the declared
210 /// `[budget(N)]` on each side. ChangeEffectSig usually fires
211 /// because the effect *list* changed; #247 makes budget drift
212 /// visible without forcing a full effect-set diff.
213 ChangeEffectSig {
214 sig_id: SigId,
215 from_stage_id: StageId,
216 to_stage_id: StageId,
217 from_effects: EffectSet,
218 to_effects: EffectSet,
219 #[serde(default, skip_serializing_if = "Option::is_none")]
220 from_budget: Option<u64>,
221 #[serde(default, skip_serializing_if = "Option::is_none")]
222 to_budget: Option<u64>,
223 },
224 /// Import added to a file. `in_file` is the canonical path
225 /// (relative to the repo root, forward-slashes) so two
226 /// machines hashing the same edit get the same OpId.
227 AddImport {
228 in_file: String,
229 module: ModuleRef,
230 /// The binding the module is imported under (`import "std.sql"
231 /// as sql` → `"sql"`). Lex requires an alias on every import,
232 /// but this is `None` whenever it equals the module's default
233 /// alias (the last path segment) — the common case — so those
234 /// `AddImport`s serialize exactly as before and keep their
235 /// original `OpId` (additive serialization, same trick as
236 /// `budget_cost`). Only a non-default alias (`import "./error"
237 /// as e`) is carried explicitly. Without it, `export-git`
238 /// cannot reconstruct a compilable module — the reference alone
239 /// doesn't say what name the body binds.
240 #[serde(default, skip_serializing_if = "Option::is_none")]
241 alias: Option<String>,
242 },
243 RemoveImport {
244 in_file: String,
245 module: ModuleRef,
246 },
247 AddType {
248 sig_id: SigId,
249 stage_id: StageId,
250 },
251 RemoveType {
252 sig_id: SigId,
253 last_stage_id: StageId,
254 },
255 ModifyType {
256 sig_id: SigId,
257 from_stage_id: StageId,
258 to_stage_id: StageId,
259 },
260 /// Merge of two branch heads. Carries only an informational count
261 /// of resolved sigs so two structurally identical merges of
262 /// different sizes don't collide on op_id; the per-sig deltas live
263 /// in `OperationRecord::produces` (`StageTransition::Merge`).
264 Merge {
265 resolved: usize,
266 },
267 /// Typed transform: inlined a `let x := v; body` by
268 /// substituting `v` for every unshadowed `x` in `body`, then
269 /// replacing the entire `Let` node with the substituted body
270 /// (#280). The op records the let-binding's position and the
271 /// inlined name; the actual substituted value lives in the
272 /// content-addressed `to_stage_id` so the op_id stays compact.
273 InlineLet {
274 sig_id: SigId,
275 from_stage_id: StageId,
276 to_stage_id: StageId,
277 let_node: String,
278 binding_name: String,
279 #[serde(default, skip_serializing_if = "Option::is_none")]
280 from_budget: Option<u64>,
281 #[serde(default, skip_serializing_if = "Option::is_none")]
282 to_budget: Option<u64>,
283 },
284 /// Typed transform: renamed a `let`-bound local within a fn
285 /// body (#280). Records the old/new identifiers and the position
286 /// of the let-binding in the AST. Body-shape-stable: the renamed
287 /// stage typically hashes near the original.
288 RenameLocal {
289 sig_id: SigId,
290 from_stage_id: StageId,
291 to_stage_id: StageId,
292 /// Path-style NodeId of the `Let` expression at the time of
293 /// the transform.
294 let_node: String,
295 old_name: String,
296 new_name: String,
297 #[serde(default, skip_serializing_if = "Option::is_none")]
298 from_budget: Option<u64>,
299 #[serde(default, skip_serializing_if = "Option::is_none")]
300 to_budget: Option<u64>,
301 },
302 /// Typed transform: replaced one arm's body in a `Match`
303 /// expression (#280). Semantically a `ModifyBody`, but the op
304 /// records *which* arm changed and *where* in the AST — so the
305 /// op log reads as a semantic edit history rather than as
306 /// opaque hash-to-hash bytes.
307 ///
308 /// `match_node` is the [`lex_ast::ids::NodeId`] of the Match
309 /// expression at the time of the transform. NodeIds aren't
310 /// stable across structural edits — they're audit-trail metadata,
311 /// not re-derivation keys. The authoritative record of the new
312 /// stage is `to_stage_id` (content-addressed).
313 ///
314 /// `from_budget`/`to_budget` follow the same `skip_if_none`
315 /// discipline as [`Self::ModifyBody`]: pre-#280 ops continue
316 /// hashing to their original `OpId`s.
317 ReplaceMatchArm {
318 sig_id: SigId,
319 from_stage_id: StageId,
320 to_stage_id: StageId,
321 /// Path-style NodeId of the Match expression that was
322 /// modified, captured at transform time. See
323 /// [`lex_ast::ids::NodeId`] for the format.
324 match_node: String,
325 arm_index: usize,
326 #[serde(default, skip_serializing_if = "Option::is_none")]
327 from_budget: Option<u64>,
328 #[serde(default, skip_serializing_if = "Option::is_none")]
329 to_budget: Option<u64>,
330 },
331 /// Multi-agent coordination: a stage proposed for a sig
332 /// without advancing the branch (#294). Multiple agents can
333 /// land `Candidate` ops on the same sig concurrently without
334 /// contention — they all chain off the current head and don't
335 /// move it. Used together with [`Self::Promote`] to model
336 /// bake-offs: several agents propose, one is promoted.
337 ///
338 /// The `Operation`'s `intent_id` is expected to be set so
339 /// downstream consumers can distinguish proposals by author.
340 /// (The schema doesn't enforce this; the gate does.)
341 Candidate {
342 sig_id: SigId,
343 stage_id: StageId,
344 },
345 /// Multi-agent coordination: promotes a previously-landed
346 /// [`Self::Candidate`] op as the new head for its sig (#294).
347 /// Carries the list of *other* candidates this Promote
348 /// supersedes so the op log explicitly records the bake-off
349 /// shape.
350 ///
351 /// Acts as a `ModifyBody` (or `AddFunction` when the sig has
352 /// no head) for branch-head purposes — `transition_for_kind`
353 /// returns the appropriate `StageTransition`.
354 Promote {
355 sig_id: SigId,
356 /// Op id of the [`Self::Candidate`] being promoted.
357 winner_candidate: OpId,
358 /// Stage id of the winner (duplicates the candidate's
359 /// `stage_id` for fast lookup; saves a log round-trip
360 /// for `lex op show`).
361 winner_stage_id: StageId,
362 /// Every other live `Candidate` for `sig_id` at the time
363 /// of promotion. Sorted by op_id for canonical-form
364 /// stability. After this `Promote` lands, none of these
365 /// op_ids appear in [`Store::list_candidates`].
366 supersedes: Vec<OpId>,
367 /// Current branch head stage for `sig_id`, or `None` if
368 /// the sig had no head (the Promote is creating it).
369 /// `None` is serialized as missing for canonical stability
370 /// across "first promote on a sig" vs "later promote".
371 #[serde(default, skip_serializing_if = "Option::is_none")]
372 from_stage_id: Option<StageId>,
373 #[serde(default, skip_serializing_if = "Option::is_none")]
374 from_budget: Option<u64>,
375 #[serde(default, skip_serializing_if = "Option::is_none")]
376 to_budget: Option<u64>,
377 },
378}
379
380impl OperationKind {
381 /// The `(SigId, Option<StageId>)` an op kind targets, as used by
382 /// `StageTransition::Merge::entries`. Used by the merge-commit
383 /// path (#134) to translate a `Resolution::Custom { op }` into
384 /// the head-map delta the merge op records:
385 ///
386 /// * Adds → `(sig, Some(stage_id))`
387 /// * Modifies → `(sig, Some(to_stage_id))`
388 /// * Removes → `(sig, None)`
389 /// * Renames → `(to_sig, Some(body_stage_id))`
390 /// * `AddImport` / `RemoveImport` / nested `Merge` → `None`
391 /// (no single sig→stage delta)
392 pub fn merge_target(&self) -> Option<(SigId, Option<StageId>)> {
393 use OperationKind::*;
394 match self {
395 AddFunction { sig_id, stage_id, .. }
396 | AddType { sig_id, stage_id }
397 => Some((sig_id.clone(), Some(stage_id.clone()))),
398 ModifyBody { sig_id, to_stage_id, .. }
399 | ChangeEffectSig { sig_id, to_stage_id, .. }
400 | ModifyType { sig_id, to_stage_id, .. }
401 | ReplaceMatchArm { sig_id, to_stage_id, .. }
402 | RenameLocal { sig_id, to_stage_id, .. }
403 | InlineLet { sig_id, to_stage_id, .. }
404 => Some((sig_id.clone(), Some(to_stage_id.clone()))),
405 Promote { sig_id, winner_stage_id, .. }
406 => Some((sig_id.clone(), Some(winner_stage_id.clone()))),
407 RemoveFunction { sig_id, .. }
408 | RemoveType { sig_id, .. }
409 => Some((sig_id.clone(), None)),
410 RenameSymbol { to, body_stage_id, .. }
411 => Some((to.clone(), Some(body_stage_id.clone()))),
412 AddImport { .. } | RemoveImport { .. } | Merge { .. } => None,
413 // Candidate ops don't advance the branch head; they
414 // don't fit the (sig, Option<stage_id>) head-delta
415 // shape that `merge_target` describes.
416 Candidate { .. } => None,
417 }
418 }
419
420 /// `(from_budget, to_budget)` for ops that carry a budget delta
421 /// (#247). `(None, None)` for ops where the budget isn't part
422 /// of the canonical payload — `RemoveFunction`, `RenameSymbol`,
423 /// imports, and merges. `AddFunction` reports `(None,
424 /// Some(cost))` for "this is the initial cost." Used by `lex op
425 /// show`, `lex op log --budget-drift`, and `lex audit --budget`.
426 pub fn budget_delta(&self) -> (Option<u64>, Option<u64>) {
427 use OperationKind::*;
428 match self {
429 AddFunction { budget_cost, .. } => (None, *budget_cost),
430 ModifyBody { from_budget, to_budget, .. }
431 | ChangeEffectSig { from_budget, to_budget, .. }
432 | ReplaceMatchArm { from_budget, to_budget, .. }
433 | RenameLocal { from_budget, to_budget, .. }
434 | InlineLet { from_budget, to_budget, .. }
435 | Promote { from_budget, to_budget, .. } => (*from_budget, *to_budget),
436 _ => (None, None),
437 }
438 }
439
440 /// The `SigId` an op touches if it carries a budget — used for
441 /// per-sig audit rollups in `lex audit --budget`. Returns `None`
442 /// for ops without a relevant budget (the same set as the
443 /// `_ => (None, None)` arm of [`Self::budget_delta`]).
444 pub fn budget_sig(&self) -> Option<&SigId> {
445 use OperationKind::*;
446 match self {
447 AddFunction { sig_id, .. }
448 | ModifyBody { sig_id, .. }
449 | ChangeEffectSig { sig_id, .. }
450 | ReplaceMatchArm { sig_id, .. }
451 | RenameLocal { sig_id, .. }
452 | InlineLet { sig_id, .. }
453 | Promote { sig_id, .. } => Some(sig_id),
454 _ => None,
455 }
456 }
457}
458
459/// Extract the declared `[budget(N)]` integer from an [`EffectSet`],
460/// if any (#247).
461///
462/// Effect labels in [`EffectSet`] are produced by
463/// [`crate::compute_diff::effect_label`]: a `[budget(50)]`
464/// declaration becomes the literal string `"budget(50)"`. This
465/// helper parses that literal back to the integer; bare `"budget"`
466/// (no arg) returns `None` because the magnitude is unknown. A
467/// stage with multiple budget declarations — which the type-
468/// checker should reject anyway — picks the smallest, conservative
469/// answer for `lex audit --budget`.
470pub fn budget_from_effects(effects: &EffectSet) -> Option<u64> {
471 let mut min_cost: Option<u64> = None;
472 for label in effects {
473 let Some(rest) = label.strip_prefix("budget(") else { continue };
474 let Some(inner) = rest.strip_suffix(')') else { continue };
475 let Ok(n) = inner.parse::<u64>() else { continue };
476 min_cost = Some(min_cost.map(|c| c.min(n)).unwrap_or(n));
477 }
478 min_cost
479}
480
481/// The operation as a whole — its kind and the causal predecessors
482/// it assumes. The `OpId` is computed from this plus a sorted view
483/// of `parents`.
484///
485/// Operations without parents are valid and represent "applies to
486/// the empty repository" or "applies to the synthetic genesis
487/// state." `lex store migrate v1→v2` will produce parentless ops
488/// for stages it can't trace back to a clear predecessor.
489#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
490pub struct Operation {
491 #[serde(flatten)]
492 pub kind: OperationKind,
493 /// Operations whose `produces` this op assumes. Sorted before
494 /// hashing for canonical form. Empty for ops against the empty
495 /// repo.
496 #[serde(default, skip_serializing_if = "Vec::is_empty")]
497 pub parents: Vec<OpId>,
498 /// The intent that caused this op, if known. Optional because
499 /// operations produced outside an agent harness (e.g. a human
500 /// running `lex publish` directly) don't have one.
501 ///
502 /// Including the intent in the canonical hash means the same
503 /// logical change made under different intents produces
504 /// different `OpId`s — causally distinct events should hash
505 /// distinctly. Ops with `intent_id: None` keep their existing
506 /// hashes (the field is omitted from the canonical JSON via
507 /// `skip_serializing_if`), so this is backwards-compatible
508 /// for stores written before #131.
509 #[serde(default, skip_serializing_if = "Option::is_none")]
510 pub intent_id: Option<crate::intent::IntentId>,
511}
512
513impl Operation {
514 /// Construct an operation against zero or more parents. Caller
515 /// supplies parents in any order; canonicalization sorts them
516 /// before hashing.
517 pub fn new(kind: OperationKind, parents: impl IntoIterator<Item = OpId>) -> Self {
518 let mut parents: Vec<OpId> = parents.into_iter().collect();
519 parents.sort();
520 parents.dedup();
521 Self { kind, parents, intent_id: None }
522 }
523
524 /// Tag this operation with the intent that produced it. The
525 /// builder shape keeps existing call sites untouched; agent
526 /// harnesses that record intent call this once before
527 /// applying the op.
528 pub fn with_intent(mut self, intent_id: impl Into<crate::intent::IntentId>) -> Self {
529 self.intent_id = Some(intent_id.into());
530 self
531 }
532
533 /// Compute this operation's content-addressed identity under the
534 /// current production canonical form ([`OperationFormat::CURRENT`]).
535 ///
536 /// Stable across runs and machines: same `(kind, payload,
537 /// sorted parents, intent_id)` produces the same `OpId`. The
538 /// invariant #129's automatic-dedup behavior relies on.
539 pub fn op_id(&self) -> OpId {
540 self.op_id_in(OperationFormat::CURRENT)
541 }
542
543 /// Compute the `OpId` under a specific canonical-form version.
544 ///
545 /// Used by [`crate::migrate`] to derive new `OpId`s when porting
546 /// a store across format versions. Production code should call
547 /// [`Self::op_id`].
548 pub fn op_id_in(&self, format: OperationFormat) -> OpId {
549 canonical::hash_bytes(&self.canonical_bytes_in(format))
550 }
551
552 /// The byte sequence that gets hashed to produce [`Self::op_id`]
553 /// under the current canonical form. Equivalent to
554 /// `self.canonical_bytes_in(OperationFormat::CURRENT)`.
555 ///
556 /// Exposed (not just consumed by `op_id`) so golden tests can pin
557 /// the exact pre-image. **Not** equal to `serde_json::to_vec(&op)`
558 /// in general — the on-disk JSON skips empty `parents` and
559 /// `None` `intent_id`, while the canonical form always emits a
560 /// (sorted, deduped) `parents` array. See `canonical.rs` for the
561 /// full V1 canonical-form spec.
562 pub fn canonical_bytes(&self) -> Vec<u8> {
563 self.canonical_bytes_in(OperationFormat::CURRENT)
564 }
565
566 /// The pre-image hashed under a specific canonical-form version.
567 ///
568 /// Today every `OperationFormat` variant routes to V1's encoder
569 /// (only V1 exists in production). When V2 lands, this match
570 /// gains an arm and the migration tool's encoder closure routes
571 /// here.
572 pub fn canonical_bytes_in(&self, format: OperationFormat) -> Vec<u8> {
573 match format {
574 OperationFormat::V1 => self.canonical_bytes_v1(),
575 }
576 }
577
578 fn canonical_bytes_v1(&self) -> Vec<u8> {
579 // Build a transient hashable view rather than hashing
580 // `self` directly so the parent ordering is canonical
581 // even if a caller hand-constructs an `Operation` with
582 // unsorted parents.
583 let canonical = CanonicalView {
584 kind: &self.kind,
585 parents: self.parents.iter().collect::<IndexSet<_>>().into_iter().collect::<BTreeSet<_>>(),
586 intent_id: self.intent_id.as_deref(),
587 };
588 serde_json::to_vec(&canonical).expect("canonical serialization")
589 }
590}
591
592/// Hashable shadow of [`Operation`] with parents in a `BTreeSet` so
593/// the serialization is order-independent regardless of how the
594/// caller constructed the live operation. Never persisted; lives
595/// only as a transient for hashing.
596#[derive(Serialize)]
597struct CanonicalView<'a> {
598 #[serde(flatten)]
599 kind: &'a OperationKind,
600 parents: BTreeSet<&'a OpId>,
601 /// `skip_serializing_if = "Option::is_none"` keeps existing
602 /// `OpId`s stable for ops without an intent — the field is
603 /// omitted from the canonical JSON entirely.
604 #[serde(skip_serializing_if = "Option::is_none")]
605 intent_id: Option<&'a str>,
606}
607
608/// An operation paired with its computed `OpId` and the resulting
609/// stage transition. This is what gets persisted under
610/// `<root>/ops/<OpId>.json`.
611///
612/// `format_version` records the canonical form the `op_id` was
613/// computed under. Pre-#244 stores didn't emit this field; reading
614/// such records deserializes to [`OperationFormat::V1`] (the
615/// implicit pre-versioning format), and writing V1 records continues
616/// to omit it (`skip_serializing_if = is_implicit`) so adding the
617/// field doesn't rotate any existing `OpId` or change any on-disk
618/// byte. Records written under a future format will explicitly
619/// carry their version tag.
620#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
621pub struct OperationRecord {
622 pub op_id: OpId,
623 #[serde(default, skip_serializing_if = "OperationFormat::is_implicit")]
624 pub format_version: OperationFormat,
625 #[serde(flatten)]
626 pub op: Operation,
627 pub produces: StageTransition,
628}
629
630impl OperationRecord {
631 pub fn new(op: Operation, produces: StageTransition) -> Self {
632 let op_id = op.op_id();
633 Self { op_id, format_version: OperationFormat::CURRENT, op, produces }
634 }
635}
636
637#[cfg(test)]
638mod tests {
639 use super::*;
640
641 fn add_factorial() -> OperationKind {
642 OperationKind::AddFunction {
643 sig_id: "fac::Int->Int".into(),
644 stage_id: "abc123".into(),
645 effects: BTreeSet::new(),
646 budget_cost: None,
647 }
648 }
649
650 #[test]
651 fn identical_operations_have_identical_op_ids() {
652 let a = Operation::new(add_factorial(), []);
653 let b = Operation::new(add_factorial(), []);
654 assert_eq!(a.op_id(), b.op_id());
655 }
656
657 #[test]
658 fn different_operations_have_different_op_ids() {
659 let a = Operation::new(add_factorial(), []);
660 let b = Operation::new(
661 OperationKind::AddFunction {
662 sig_id: "double::Int->Int".into(),
663 stage_id: "abc123".into(),
664 effects: BTreeSet::new(),
665 budget_cost: None,
666 },
667 [],
668 );
669 assert_ne!(a.op_id(), b.op_id());
670 }
671
672 #[test]
673 fn parent_set_changes_op_id() {
674 let no_parent = Operation::new(add_factorial(), []);
675 let with_parent = Operation::new(add_factorial(), ["op-parent-1".into()]);
676 assert_ne!(no_parent.op_id(), with_parent.op_id());
677 }
678
679 #[test]
680 fn parent_order_does_not_affect_op_id() {
681 let a = Operation::new(add_factorial(), ["b".into(), "a".into(), "c".into()]);
682 let b = Operation::new(add_factorial(), ["c".into(), "a".into(), "b".into()]);
683 assert_eq!(a.op_id(), b.op_id());
684 // and the stored form is sorted.
685 assert_eq!(a.parents, vec!["a".to_string(), "b".to_string(), "c".to_string()]);
686 }
687
688 #[test]
689 fn duplicate_parents_are_deduped() {
690 let with_dups = Operation::new(
691 add_factorial(),
692 ["a".into(), "a".into(), "b".into()],
693 );
694 let no_dups = Operation::new(
695 add_factorial(),
696 ["a".into(), "b".into()],
697 );
698 assert_eq!(with_dups.op_id(), no_dups.op_id());
699 assert_eq!(with_dups.parents, vec!["a".to_string(), "b".to_string()]);
700 }
701
702 #[test]
703 fn rename_with_same_body_hashes_equal_across_runs() {
704 // Two independent runs producing the same rename against the
705 // same parent should produce the same OpId — this is the
706 // automatic-dedup property #129 relies on for distributed
707 // agents.
708 let kind = OperationKind::RenameSymbol {
709 from: "parse::Str->Int".into(),
710 to: "parse_int::Str->Int".into(),
711 body_stage_id: "abc123".into(),
712 };
713 let a = Operation::new(kind.clone(), ["op-parent".into()]);
714 let b = Operation::new(kind, ["op-parent".into()]);
715 assert_eq!(a.op_id(), b.op_id());
716 }
717
718 #[test]
719 fn rename_does_not_collide_with_delete_plus_add() {
720 // The whole point of `RenameSymbol` is that it's a different
721 // OpId from the (semantically-equivalent) `RemoveFunction +
722 // AddFunction` pair. Causal history sees one event, not two.
723 let rename = Operation::new(
724 OperationKind::RenameSymbol {
725 from: "parse::Str->Int".into(),
726 to: "parse_int::Str->Int".into(),
727 body_stage_id: "abc123".into(),
728 },
729 ["op-parent".into()],
730 );
731 let remove = Operation::new(
732 OperationKind::RemoveFunction {
733 sig_id: "parse::Str->Int".into(),
734 last_stage_id: "abc123".into(),
735 },
736 ["op-parent".into()],
737 );
738 let add = Operation::new(
739 OperationKind::AddFunction {
740 sig_id: "parse_int::Str->Int".into(),
741 stage_id: "abc123".into(),
742 effects: BTreeSet::new(),
743 budget_cost: None,
744 },
745 ["op-parent".into()],
746 );
747 assert_ne!(rename.op_id(), remove.op_id());
748 assert_ne!(rename.op_id(), add.op_id());
749 }
750
751 #[test]
752 fn effect_set_order_does_not_affect_op_id() {
753 // Effects are a BTreeSet so iteration is sorted. Build two
754 // ops via different insertion orders and confirm the
755 // canonical form is identical.
756 let a_effects: EffectSet = ["io".into(), "fs_write".into()].into_iter().collect();
757 let b_effects: EffectSet = ["fs_write".into(), "io".into()].into_iter().collect();
758 let a = Operation::new(
759 OperationKind::AddFunction {
760 sig_id: "x".into(), stage_id: "s".into(), effects: a_effects,
761 budget_cost: None,
762 },
763 [],
764 );
765 let b = Operation::new(
766 OperationKind::AddFunction {
767 sig_id: "x".into(), stage_id: "s".into(), effects: b_effects,
768 budget_cost: None,
769 },
770 [],
771 );
772 assert_eq!(a.op_id(), b.op_id());
773 }
774
775 #[test]
776 fn op_id_is_64_char_lowercase_hex() {
777 let id = Operation::new(add_factorial(), []).op_id();
778 assert_eq!(id.len(), 64);
779 assert!(id.chars().all(|c| c.is_ascii_digit() || ('a'..='f').contains(&c)));
780 }
781
782 #[test]
783 fn round_trip_through_serde_json() {
784 let op = Operation::new(
785 OperationKind::ChangeEffectSig {
786 sig_id: "f".into(),
787 from_stage_id: "old".into(),
788 to_stage_id: "new".into(),
789 from_effects: BTreeSet::new(),
790 to_effects: ["io".into()].into_iter().collect(),
791 from_budget: None,
792 to_budget: None,
793 },
794 ["op-parent".into()],
795 );
796 let json = serde_json::to_string(&op).expect("serialize");
797 let back: Operation = serde_json::from_str(&json).expect("deserialize");
798 assert_eq!(op, back);
799 assert_eq!(op.op_id(), back.op_id());
800 }
801
802 #[test]
803 fn operation_record_carries_op_id() {
804 let op = Operation::new(add_factorial(), []);
805 let expected = op.op_id();
806 let rec = OperationRecord::new(
807 op,
808 StageTransition::Create {
809 sig_id: "fac::Int->Int".into(),
810 stage_id: "abc123".into(),
811 },
812 );
813 assert_eq!(rec.op_id, expected);
814 }
815
816 #[test]
817 fn intent_id_is_part_of_op_id_canonical_hash() {
818 // The dedup property: same `(kind, parents, intent_id)`
819 // produces the same OpId. Different intent_ids on
820 // otherwise-identical ops produce different OpIds, so
821 // causally distinct events (different prompts) hash
822 // distinctly.
823 let no_intent = Operation::new(add_factorial(), []);
824 let with_intent_a = Operation::new(add_factorial(), [])
825 .with_intent("intent-a");
826 let with_intent_b = Operation::new(add_factorial(), [])
827 .with_intent("intent-b");
828 let with_intent_a_again = Operation::new(add_factorial(), [])
829 .with_intent("intent-a");
830
831 // No-intent op is distinct from any intent-tagged variant.
832 assert_ne!(no_intent.op_id(), with_intent_a.op_id());
833 // Different intents → different OpIds.
834 assert_ne!(with_intent_a.op_id(), with_intent_b.op_id());
835 // Same intent → same OpId (the load-bearing dedup invariant).
836 assert_eq!(with_intent_a.op_id(), with_intent_a_again.op_id());
837 }
838
839 #[test]
840 fn op_without_intent_keeps_pre_intent_op_id() {
841 // Backwards-compat invariant: an op constructed without an
842 // intent must hash to the same value as it would have
843 // before #131 added the field. The golden test below pins
844 // the exact hash; this one asserts that adding then
845 // resetting to None doesn't drift.
846 let mut op = Operation::new(add_factorial(), []);
847 let baseline = op.op_id();
848 op.intent_id = Some("transient".into());
849 let with_intent = op.op_id();
850 assert_ne!(baseline, with_intent);
851 op.intent_id = None;
852 let back = op.op_id();
853 assert_eq!(baseline, back);
854 }
855
856 /// Golden hash. If this changes, the canonical form has shifted
857 /// and *every* op_id in every existing store has changed too —
858 /// that's a major-version event for the data model and should
859 /// be a deliberate decision, not an accident from reordering
860 /// fields. Update with care.
861 #[test]
862 fn canonical_form_is_stable_for_a_known_input() {
863 let op = Operation::new(
864 OperationKind::AddFunction {
865 sig_id: "fac::Int->Int".into(),
866 stage_id: "abc123".into(),
867 effects: BTreeSet::new(),
868 budget_cost: None,
869 },
870 [],
871 );
872 assert_eq!(
873 op.op_id(),
874 "f112990d31ef2a63f3e5ca5680637ed36a54bc7e8230510ae0c0e93fcb39d104"
875 );
876 }
877
878 #[test]
879 fn merge_kind_round_trips() {
880 let op = Operation::new(
881 OperationKind::Merge { resolved: 3 },
882 ["op-a".into(), "op-b".into()],
883 );
884 let json = serde_json::to_string(&op).expect("ser");
885 let back: Operation = serde_json::from_str(&json).expect("de");
886 assert_eq!(op, back);
887 assert_eq!(op.op_id(), back.op_id());
888 }
889
890 #[test]
891 fn merge_stage_transition_round_trips() {
892 let mut entries = BTreeMap::new();
893 entries.insert("sig-a".to_string(), Some("stage-a".to_string()));
894 entries.insert("sig-b".to_string(), None); // removed by merge
895 let t = StageTransition::Merge { entries };
896 let json = serde_json::to_string(&t).expect("ser");
897 let back: StageTransition = serde_json::from_str(&json).expect("de");
898 assert_eq!(t, back);
899 }
900
901 #[test]
902 fn merge_resolved_count_changes_op_id() {
903 // Two merges with the same parents but different resolved counts
904 // must hash differently — keeps structurally distinct merges from
905 // colliding on op_id.
906 let parents: Vec<OpId> = vec!["op-a".into(), "op-b".into()];
907 let one = Operation::new(OperationKind::Merge { resolved: 1 }, parents.clone());
908 let two = Operation::new(OperationKind::Merge { resolved: 2 }, parents);
909 assert_ne!(one.op_id(), two.op_id());
910 }
911
912 #[test]
913 fn existing_add_function_op_id_is_unchanged_after_merge_added() {
914 // Constructing the new Merge variant in the same enum must not
915 // perturb the canonical bytes of existing variants. The golden
916 // hash test below checks the literal value; this one verifies
917 // the property holds even after a Merge op has been built.
918 let _merge = Operation::new(
919 OperationKind::Merge { resolved: 0 },
920 ["op-x".into(), "op-y".into()],
921 );
922 let op = Operation::new(add_factorial(), []);
923 assert_eq!(
924 op.op_id(),
925 "f112990d31ef2a63f3e5ca5680637ed36a54bc7e8230510ae0c0e93fcb39d104"
926 );
927 }
928}