@godspeedai/cognate-realitytrace 0.1.1 → 0.2.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 (2) hide show
  1. package/README.md +97 -34
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,42 +1,105 @@
1
1
  # @godspeedai/cognate-realitytrace
2
2
 
3
- Optional observation adapter (spec §20): committed-outbox delivery of
4
- Framework observations to RealityTrace (SXR), plus the SXR CLI port.
3
+ Cognate's adapter for RealityTrace (SXR), an independent observation-and-evidence tool driven
4
+ through its `sxr` CLI. This package moves observations of
5
+ your application's committed history to RealityTrace without ever blocking or rewriting that
6
+ history: an outbox-based delivery port, persisted per-observation receipts, a strict
7
+ "was it recorded?" gate, and typed envelopes for execution traces and evidence packets.
5
8
 
6
- ## Normal vs strict mode
9
+ **When to use this package:** you want an external, independent ledger of what your Cognate
10
+ application actually did — recorded at-least-once from committed events, with correlation ids
11
+ that survive the trip — while keeping the adapter optional and RealityTrace unchanged.
12
+ Everything here degrades gracefully: if RealityTrace is slow or down, your app keeps running.
7
13
 
8
- **Normal mode** (default): `observationIntent` is committed in the same
9
- decision as the event; `ObservationDelivery` drains the outbox at-least-once
10
- in the background. A slow or unreachable SXR never blocks or rolls back
11
- committed history — failures retry with backoff, then poison for operator
12
- reconciliation (`unreconciled()`).
14
+ ## Installation
13
15
 
14
- **Strict mode**: callers gate a specific operation on `isRecorded(eventId)`,
15
- which is `true` only after SXR has `accepted` or already had (`duplicate`)
16
- the observation. `queued`/`retrying` never satisfies the gate.
16
+ ```sh
17
+ bun add @godspeedai/cognate-realitytrace
18
+ ```
19
+
20
+ Requires [Bun](https://bun.sh) >= 1.4.0, a
21
+ [`@godspeedai/cognate-events`](https://www.npmjs.com/package/@godspeedai/cognate-events)
22
+ store, and the external RealityTrace CLI:
23
+
24
+ - The `sxr` binary must be installed on the machine; you pass its path to the port.
25
+ - A working directory the tool can enroll as its repository (the adapter initializes it with
26
+ git on first `prepare()`).
27
+ - RealityTrace >= 0.3.0 for structured correlation readback (`readCorrelation`).
28
+
29
+ The SDK ships Bun-native TypeScript source, so add `@types/bun` and `@types/node` as dev
30
+ dependencies when typechecking your project.
31
+
32
+ ## Quick start
33
+
34
+ ```ts
35
+ import { openSqliteStore } from "@godspeedai/cognate-store-sqlite";
36
+ import {
37
+ createSxrCliPort,
38
+ ObservationDelivery,
39
+ observationIntent,
40
+ } from "@godspeedai/cognate-realitytrace";
41
+
42
+ const store = openSqliteStore({ path: "./app.sqlite" });
43
+
44
+ // One-time enrollment of the external tool (repo, question, threshold, run):
45
+ const port = createSxrCliPort({ bin: "/usr/local/bin/sxr", repoDir: "./sxr-repo" });
46
+ const workflow = await port.prepare({ title: "Runtime behaves as declared" });
47
+
48
+ const delivery = new ObservationDelivery(store, {
49
+ port,
50
+ backoff: { baseMs: 1_000, maxMs: 30_000, maxAttempts: 8 },
51
+ });
52
+
53
+ // When committing an event whose observation matters, include the observation intent
54
+ // in the SAME commit — the observation can then never refer to an event that may not exist:
55
+ await store.commit({
56
+ appends: [/* your event */],
57
+ outbox: [observationIntent(event, { classification: "durable_fact", capabilityId: "refund.approve" })],
58
+ });
59
+
60
+ // From a background loop, drain deliveries. A slow or unreachable tool never blocks
61
+ // committed history: failures retry with backoff, then park for operator attention.
62
+ await delivery.drain();
63
+
64
+ // Normal mode: check on delivery. Strict mode: gate an operation on it.
65
+ const status = await delivery.status(eventId); // queued | accepted | duplicate | retrying | failed
66
+ const stuck = await delivery.unreconciled(); // deliveries needing operator attention
67
+ if (await delivery.isRecorded(eventId)) {
68
+ // the tool has accepted (or already had) this observation
69
+ }
70
+ ```
71
+
72
+ The CLI port (`createSxrCliPort`) also offers `readCorrelation(targetRef)` — your observation's
73
+ correlation ids read back from the tool's committed record — plus `deriveEvidence` and
74
+ `readEvidence` for the tool's own evidence derivation, and `executionTraceEnvelope`,
75
+ `governedTraceFor`, and `evidencePacketEnvelope` for typed record envelopes at the boundary.
17
76
 
18
77
  ## What a receipt means — and does not mean
19
78
 
20
- A `DeliveryReceipt` (`accepted`/`duplicate`) means SXR's ledger recorded (or
21
- had already recorded) the observation bytes. It is **not** evidence and
22
- **not** settlement — those are separate, later, explicit SXR commands
23
- (`evidence create`, `settlement register`) outside this adapter's scope.
24
-
25
- ## Per-observation verifier design
26
-
27
- `sxr ingest` dedupes by `(run_id, verifier)`, not by any caller-supplied id,
28
- and mints a fresh `observation_id` on every call — even duplicates. This
29
- port uses a unique verifier per observation (`cognate.<observationId>`) so
30
- SXR's own idempotency key becomes, in effect, per-observation: retries land
31
- on the same `ledger_position` and report `already_committed`.
32
-
33
- ## Structured correlation (RealityTrace ≥ 0.3.0)
34
-
35
- `record()` passes Cognate's correlation fields to `ingest --extensions`, so
36
- the committed record carries them as CEP `extensions.cognate`.
37
- `readCorrelation(targetRef)` reads them back from that record via
38
- `ledger export`, returning `null` when no record sits at that position. A
39
- duplicate delivery keeps the first record's ids. Cognate still keeps the
40
- authoritative event → observation → receipt mapping in its `observations`
41
- store, keyed by `eventId`, and never treats a duplicate response's newly
42
- minted SXR `observation_id` as the committed id.
79
+ A delivery receipt (`accepted` or `duplicate`) means RealityTrace's ledger recorded — or had
80
+ already recorded — the observation. It is **not evidence** and **not settlement**: deriving
81
+ evidence and settling questions are separate, explicit steps of the external tool, invoked
82
+ through this adapter but decided by it. Delivery is idempotent per observation (retries land
83
+ on the same ledger position and report as `duplicate`), and the authoritative event →
84
+ observation → receipt mapping stays in your store, keyed by event id.
85
+
86
+ ## Limitations
87
+
88
+ - The port holds the enrollment workflow in memory: call `prepare()` once per process before
89
+ recording, and again after a restart if the process is long-lived.
90
+ - Delivery is at-least-once; the external tool deduplicates by run and verifier.
91
+ - Every delivery shells out to the `sxr` binary — budget for that process cost in hot paths.
92
+ - If deliveries exhaust their retries they are poisoned for operator reconciliation; nothing
93
+ deletes or rewrites committed events in the meantime.
94
+
95
+ ## Related packages
96
+
97
+ - [`@godspeedai/cognate-events`](https://www.npmjs.com/package/@godspeedai/cognate-events) —
98
+ the store and outbox machinery deliveries are driven from.
99
+ - [`@godspeedai/cognate-runtime-bun`](https://www.npmjs.com/package/@godspeedai/cognate-runtime-bun) —
100
+ the reference runtime whose events this adapter observes.
101
+ - [`@godspeedai/cognate`](https://www.npmjs.com/package/@godspeedai/cognate) — the SDK umbrella.
102
+
103
+ ## License
104
+
105
+ Apache-2.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@godspeedai/cognate-realitytrace",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Observation port, committed-outbox delivery, receipts, strict gating, and the SXR CLI adapter (spec §20). RealityTrace stays unchanged.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -22,8 +22,8 @@
22
22
  ".": "./src/index.ts"
23
23
  },
24
24
  "dependencies": {
25
- "@godspeedai/cognate-events": "^0.1.1",
26
- "@godspeedai/cognate-kernel-api": "^0.1.1"
25
+ "@godspeedai/cognate-events": "^0.2.0",
26
+ "@godspeedai/cognate-kernel-api": "^0.2.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@godspeedai/cognate-store-memory": "workspace:*",