mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-11-30 00:07:37 +00:00
7.7 KiB
7.7 KiB
Simplify RunEvent While Keeping Wire JSON Stable
Summary
- Refactor the event model so
RunEventstores only envelope metadata plus a typedEventBody; the JSON wire format stays{ ..., "event": "...", "properties": { ... } }. - Treat this as an internal Rust API break now: remove public
RunEvent.eventandRunEvent.properties, update repo call sites in one pass, and align the code withdocs-internal/events-strategy.md's "canonical envelope built once" rule. - Preserve forward-compatibility for unknown stored events explicitly instead of relying on duplicate cached fields.
- Phase the refactor into three commits so the direct
Event -> RunEventmapping can land and be verified before the cached-field removal.
Implementation Changes
- Sequencing
- Commit 1: add
EventBody::event_name() -> &str, removeevent_name_from_body()andproperties_from_body(), and replace both with explicit implementations that keep cached fields working through commits 1 and 2. - Commit 2: rework
fabro-workflowto constructRunEventdirectly fromEvent; this is the main structural refactor and the primary regression risk. - Commit 3: remove cached
RunEvent.event/RunEvent.properties, update callers and tests, and replaceEventBody::Unknownwith a raw-preserving variant.
- Commit 1: add
lib/crates/fabro-types/src/run_event/mod.rs- Redefine
RunEventto containid,ts,run_id, optional envelope metadata, andbody: EventBodyonly. - Keep
RunEvent::from_value,from_json_str, andto_value, but make them thin wire-boundary helpers around a private raw wire struct for{ event, properties }. - Replace
EventBody::Unknownwith a raw-preserving variant such asUnknown { name: String, properties: Value }. - Add
EventBody::event_name() -> &strimplemented as an exhaustivematchreturning the serde rename string for each known variant andname.as_str()forUnknown. - In commit 1, replace
properties_from_body()with an explicit property-serialization helper that derives the inner properties payload without the current serialize-and-pluck helper pattern; it may still serialize as an interim step, but it should exist only to support cached fields and wire serialization during the transition. - Keep JSON property extraction as a serialization helper, not a hot-path public API. Use it only in
RunEvent::to_value/Serializeand in wire-shape tests that need JSON-level assertions. - Remove
refresh_cache,event_name_from_body, andproperties_from_body. - Call out unknown-event fallback explicitly:
Unknown { name, properties }cannot rely on#[serde(other)], soRunEvent::from_valuemust use a custom fallback path that preserves raweventandpropertieswhen typedEventBodydeserialization fails.
- Redefine
lib/crates/fabro-workflow/src/event.rs- Split the current conversion into two explicit pieces: envelope metadata extraction and
Event -> EventBodyconstruction. - Rework
to_run_event_at()to buildRunEventdirectly, not viajson!plusRunEvent::from_value. - Keep all existing canonicalization rules, but express them as Rust matches:
run_idstripping, node/session extraction, node-label defaults, failure/error normalization, and agent/sandbox nested event flattening. - Treat
Event::AgentandEvent::Sandboxas the bulk of the work:Event::Agentmust expand eachAgentEventsub-variant into the correspondingEventBodyvariant while also liftingstage -> node_id, preservingsession_id/parent_session_idin the envelope, and mergingvisitinto the inner props where required.Event::Sandboxmust unwrap eachSandboxEventsub-variant into the correspondingEventBodyvariant while preserving the current flattened wire shape.Event::WorkflowRunFailedmust continue convertingFabroErrorinto the stored string form used byRunFailedProps.- stage/parallel/prompt/watchdog variants must continue moving
node_id/stage/branch/nodeinto the envelope with the same currentnode_labeldefaults.
- Make the lossy cross-crate conversions explicit in the implementation and guard them with wire-shape characterization tests:
fabro_agent::AgentError -> Stringfabro_llm::error::SdkError -> string fields in retry propsfabro_llmusage types ->fabro_typesusage structsfabro_workflow::error::FabroError -> String
- Delete
tagged_variant_fields*once all variant mapping is direct and covered by tests. - Keep redaction/persistence logic driven by serialized
RunEventwire value; no wire-shape change and no redaction contract change. - Explicitly keep the
build_redacted_event_payloadpipeline out of scope for this pass: no changes toto_value() -> normalize -> to_string -> redact -> from_str.
- Split the current conversion into two explicit pieces: envelope metadata extraction and
lib/crates/fabro-store/src/types.rs,lib/crates/fabro-store/src/run_state.rs, and repo consumers- Update call sites to stop reading
RunEvent.eventandRunEvent.propertiesdirectly. - Default rule: production consumers match on
body; only serialization/wire tests should rely on JSON property extraction. - Route store decoding through one helper path (
TryFrom<&EventPayload>orRunEvent::from_value) and keep clone-based payload parsing for now; zero-copy parsing is out of scope for this pass. - Update strategy/docs terminology only, not code naming: leave
RunEventas the code type in this pass and aligndocs-internal/events-strategy.mdif needed.
- Update call sites to stop reading
Test Plan
lib/crates/fabro-types/src/run_event/mod.rs- known event round-trip preserves the wire JSON shape
- unknown event round-trip preserves raw
eventandproperties - known event name with invalid properties still fails deserialization
- absent optional envelope fields serialize as omitted fields, not
null
lib/crates/fabro-workflow/src/event.rs- characterization tests for representative variants: stage event, agent event, sandbox event, and run failure
- assert direct construction produces the same wire JSON and envelope fields as today
- assert
build_redacted_event_payloadstill returns a validEventPayload - add focused coverage for agent flattening and sandbox flattening, since those wrappers are the highest-risk conversion paths
lib/crates/fabro-store/src/run_state.rsor adjacent store tests- replay persisted payloads into
RunProjectionstill reconstructs run, status, checkpoint, retro, and pull-request state correctly
- replay persisted payloads into
- Test migration rules
- behavior tests should prefer matching on typed
bodyinstead of reintroducing JSON-shaped assertions - wire-contract tests should assert on
to_value()/ serialized JSON when the exactpropertiesshape matters - do not replace all former
stored.properties["foo"]assertions with a general-purpose allocating helper in production code fabro-cli/src/commands/run/run_progress/event.rs::from_run_event()is already aligned with the target design because it matches onEventBody; only any remaining CLI tests asserting throughstored.propertiesneed migration in commit 3
- behavior tests should prefer matching on typed
- Verification
- run
cargo nextest run -p fabro-types - run
cargo nextest run -p fabro-workflow - run
cargo nextest run -p fabro-store - run
cargo fmt --check --allandcargo clippy --workspace -- -D warnings
- run
Assumptions
- The JSON wire protocol stays compatible; only the internal Rust representation and helper APIs change.
- Internal Rust API break is acceptable now; tests and internal consumers will be updated in the same pass.
- Unknown stored events are a supported forward-compatibility case and must survive parse/serialize unchanged.
- This pass optimizes for simplicity and maintainability first; deeper read-side performance work such as borrowed parsing, eliminating
Valueclones in projection, or optimizing the redaction pipeline can follow separately.