Repository navigation
Add a typed in-process write context and reserved client ids (refs #3435) - #3752
Conversation
|
API: the final shape for the Sync adaptation. Phase 2 only deletes the superseded // nodespace_core (also nodespace_core::db::WriteContext, nodespace_core::db::events::WriteContext)
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct WriteContext {
/// The playbook engine evaluates no event trigger against the write's events.
pub skip_playbook_triggers: bool,
/// No invariant rule runs in the write's transaction (create, update,
/// declared relationship; single and bulk); the engine repairs a
/// created/updated node after commit instead (ADR-060 §7 repair-and-log).
pub skip_invariant_rules: bool,
}
impl NodeService {
pub fn with_client(&self, client_id: impl Into<String>) -> Self; // unchanged; keeps the write context
pub fn with_write_context(&self, write_context: WriteContext) -> Self; // new; keeps the client id; in-process only
pub fn write_context(&self) -> WriteContext; // new
}
// nodespace_daemon
impl DaemonExtensions {
pub fn reserved_client_id(self, id: impl Into<String>) -> Self; // new; default: none reserved
pub fn reserved_client_ids(&self) -> &[String]; // new
}
// #[non_exhaustive] DaemonExtensionsError gains InvalidReservedClientId(String):
// a reserved id that is empty, not visible ASCII, or padded with spaces stops startup.
impl NodeServiceImpl {
pub fn with_reserved_client_ids(self, ids: Vec<String>) -> Self; // build_database_services sets it from DaemonExtensions
}The Sync daemon sets it like this: // The apply service. This replaces with_client("sync-service") on every apply-side NodeService.
let apply = node_service
.with_client(SYNC_SERVICE_CLIENT_ID) // echo label, as today
.with_write_context(WriteContext {
skip_playbook_triggers: true,
skip_invariant_rules: true,
});
// Where nodespaced-sync builds DaemonExtensions:
let extensions = DaemonExtensions::none()
/* .supported_extension(..), .behavior(..), .edge_fields(..) as today */
.reserved_client_id(SYNC_SERVICE_CLIENT_ID);
🤖 Generated with Claude Code |
|
Test results for this PR at 7482ce1:
Mutation testing on the final code: each mutation was applied on its own and the targeted tests were run. All 23 are killed, and each one by the tests expected to catch it.
An independent adversarial review is in progress. 🤖 Generated with Claude Code |
|
APPROVE Independent review of head Findings
What I checked (no issue found)
🤖 Generated with Claude Code |
|
APPROVE Re-review at The
|
|
@malibio, a summary of this PR, which is phase 1 of #3435. What changed. Core no longer needs a magic client id to recognise a write whose rules already ran elsewhere.
Core behaviour is unchanged. Nothing in core sets a write context or reserves an id. Every ordinary write runs its triggers and invariants exactly as before. Release. This is not in v0.3.5. That release was already tagged at 2bf95fe when the plan reached me, so this lands in core main for the next release. Phase 2 deletes Open question for the architect (ADR text).
🤖 Generated with Claude Code |
) WriteContext { skip_playbook_triggers, skip_invariant_rules } is set only in process through NodeService::with_write_context. Each event a write emits reaches the playbook engine beside it on a crate-private channel (EmittedEvent, subscribe_to_emitted_events); transaction and batch buffers hold the event and its context together, and batch coalescing keys on the context. The engine skips trigger evaluation for skip_playbook_triggers and runs repair-and-log for skip_invariant_rules; invariant dispatch at create, update and declared-relationship time is skipped for skip_invariant_rules. The six SqliteStore write methods that notify carry the writer's context. EventMetadata and EventEnvelope are unchanged. REPLICATED_APPLY_CLIENT_ID still reads as both flags set; its removal is a separate change. DaemonExtensions::reserved_client_id reserves client ids for a build's own in-process writers: a NodeService request (WatchNodes included) presenting one as x-ns-client-id is refused with PERMISSION_DENIED, and an id the header cannot carry stops startup (InvalidReservedClientId). Core reserves none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ithout a reader, fix comments Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
637e65c to
bbf3f6c
Compare
Refs #3435. Phase 1 of 2: additive. Phase 2 (a separate PR, held until Sync has moved to the typed context) deletes
REPLICATED_APPLY_CLIENT_IDandis_replicated_apply; only then does #3435 close.What changes
Core: a typed, in-process write context.
nodespace_core::WriteContext { skip_playbook_triggers: bool, skip_invariant_rules: bool }(alsonodespace_core::db::WriteContext). It is named by what it does. The default is an ordinary write.NodeService::with_write_context(ctx) -> NodeServiceis the only way to set it, alongsidewrite_context().with_clientkeeps the context andwith_write_contextkeeps the client id. No request field or header reaches it.subscribe_to_emitted_events) alongside the unchangedEventEnvelope. A transaction's or batch's buffer holds the event and its context together (EmittedEvent). Batch coalescing never merges writes with different contexts.skip_playbook_triggers. Repair-and-log (ADR-060 §7) runs forskip_invariant_rules.skip_invariant_rules, no rule runs at any of the three in-transaction points (create, update, declared relationship), single or bulk.REPLICATED_APPLY_CLIENT_ID("sync-service") still behaves exactly as today: it reads as both flags set. Its doc now says it is superseded and is removed in the next release. It is not#[deprecated]because CLAUDE.md bans the attribute, and it would needallow(deprecated)at the remaining uses.SqliteStorewrite methods (update_node,update_node_with_version_check,delete_node,bulk_delete,delete_subtree_atomic,delete_with_version_check) takewrite_context: WriteContextaftersource. These are library API, not an extension point. Sync main calls none of them.Daemon: reserved client ids.
DaemonExtensions::reserved_client_id(id)andreserved_client_ids(). The default is empty, so core reserves none.x-ns-client-idis a reserved id getsPERMISSION_DENIED. This covers every unary RPC, throughroute(), andWatchNodes.client_id_headeris now the daemon's single, validated source of a client id.DaemonExtensionsError::InvalidReservedClientId. That error enum isnon_exhaustive.build_database_serviceswires the reservation into every database's node API. A host usingbuild_shared_servicesneeds nothing more.Core behaviour is unchanged
source_client_id, echo suppression (set_excluded_event_origin,subscribe_to_events_excluding_origin,WatchNodesown-echo filtering) andEventMetadata/EventEnvelopeare unchanged. The public event stream carries the same envelopes in the same order, and a test pins that.EventMetadata/EventEnvelopeliterals in tests, which is why the context does not travel as a newEventMetadatafield. It uses none of the changed store methods.Tests
build_shared_services).create_node,bulk_create), the invariant skips (create, update, relationship,bulk_create,bulk_update) and repair-and-log (create, update, chain depth, the two-device ping-pong) all pass under it. Copies run the superseded id until phase 2.PERMISSION_DENIED) on writes and onWatchNodes, and writes nothing. Unreserved ids and no id are served as before. Core's daemon refuses nothing. An invalid reserved id stops startup.with_clientresetting the context, the origin filter keying on the context, store/emit/merge/batch-key propagation and the engine channel, the header refusal (on, off, refusing everything), assembly wiring, the wire setting a context, and startup validation and dedupe. The list and its results are in the PR comments.Docs (to commit to nodespace-docs main after this merges)
NodeService::with_write_context(WriteContext)(accessor;db/events.rs,services/node_service/mod.rs,playbook/engine.rs,services/node_service/invariants.rs). Default: every write runs triggers and invariant rules. Core's own tests set it in process.DaemonExtensions::reserved_client_id(hook;extensions.rs,node_service.rs). Default: none, every id is served. A core fixture reserves one and is refused.architecture/extension-points.md: the daemon API block gainsreserved_client_id/reserved_client_ids. Two new subsections:Open question for the architect
🤖 Generated with Claude Code