runifold-store-postgres 0.2.0

PostgreSQL conversation, semantic-memory, and distributed workflow store for Runifold
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
# Runifold

Runifold is a typed, observable, cancellable, and budget-aware runtime kernel
for models, tools, agents, and workflows in Rust.

The name combines **run** with **manifold**: models, tools, agents, and flows
are different surfaces over the same execution space.

## Quickstart

Add the facade and the providers your application needs:

```console
cargo add runifold --features openai
```

The ergonomic path automatically creates a root run with authority limited to
the Tool and child-Agent capabilities explicitly registered on the Agent:

```rust,no_run
use runifold::{ProviderModelExt, openai::OpenAiClient};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let runtime = OpenAiClient::from_api_key(std::env::var("OPENAI_API_KEY")?)?
    .runtime("gpt-5")?;
let agent = runtime
    .agent("assistant")
    .system("Answer precisely and expose uncertainty.");

let answer = agent
    .prompt_text("Why is durable execution useful?")
    .await?;
# let _ = answer;
# Ok(())
# }
```

Compatible providers have first-class modules without separate crates:

```console
cargo add runifold --features deepseek
```

```rust,no_run
use runifold::deepseek::{DeepSeekAgentExt, client};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let answer = client(std::env::var("DEEPSEEK_API_KEY")?)?
    .agent("reasoner", "deepseek-reasoner")
    .prompt_text("Why does idempotency matter?")
    .await?;
# let _ = answer;
# Ok(())
# }
```

See the [Provider support matrix](docs/PROVIDERS.md) for native versus
compatible protocols, regional endpoints, and verification levels.
Repeatable latency, throughput, reliability, and cross-framework comparison
rules, plus the standalone release-mode Rig executor, are documented in the
[benchmarking contract](docs/BENCHMARKING.md).
Production claims, mandatory fault tests, machine-readable evidence, and
explicitly unverified areas are tracked in the
[reliability matrix](docs/RELIABILITY.md).
The provider-neutral facade is compiled for `wasm32-unknown-unknown` on the
declared Rust 1.88 MSRV, with core identity, authority, cancellation, and
budget semantics executed in the mandatory edge-runtime CI gate.
OpenAI-compatible, Anthropic, Gemini and Ollama Agent paths plus native
embeddings are exercised in pinned headless Chrome through real CORS, Fetch,
SSE and NDJSON. Browser deployments must use the documented
[application-gateway credential boundary](docs/EDGE.md).

Use `agent.prompt(...)` when the canonical transcript, usage, warnings, and
provider events matter. Use `agent.run(input, &context)` when the application
must supply a tighter budget, narrower capabilities, a deadline, durable
journaling, or shared run-tree identity.

Static and dynamic grounding use the same Agent path:

```rust,ignore
let agent = client
    .agent("support", "gpt-5")
    .system("Answer only from relevant evidence.")
    .context("Returns are accepted within 30 days.")
    .dynamic_context(5, application_retriever)
    .build()?;

let answer = agent.prompt_text("Can I return an item after two weeks?").await?;
```

`dynamic_context` accepts any provider-neutral `Retriever`, including the
deterministic `InMemoryVectorIndex`. Retrieved documents are labelled as
untrusted user-level data and retain stable document IDs; they can never
become system instructions. The ergonomic prompt path grants only registered
retrievers. An explicit `RunContext` must grant each retriever capability.

Native embedding adapters reuse provider clients rather than configuration
values:

```rust,ignore
use std::sync::Arc;
use runifold::{
    Document, InMemoryVectorIndex, RetrievalContext,
    openai::{OpenAiAgentExt, OpenAiClient},
};

let client = OpenAiClient::from_api_key(std::env::var("OPENAI_API_KEY")?)?;
let embedder = Arc::new(client.embedding_model("text-embedding-3-small")?);
let built = InMemoryVectorIndex::build(
    "product-docs",
    embedder,
    vec![
        Document::new("returns", "Returns are accepted within 30 days.")?,
        Document::new("shipping", "Standard shipping takes three days.")?,
    ],
    RetrievalContext::new(),
)
.await?;

let agent = client
    .agent("support", "gpt-5")
    .dynamic_context(4, built.index)
    .build()?;
```

Gemini and Ollama expose the same `client.embedding_model(...)` path. Index
construction is tagged `RetrievalDocument`; lookup is tagged
`RetrievalQuery`, allowing providers such as Gemini to tune the two sides
correctly. Silent truncation is disabled by default.

For persistent retrieval, select a replaceable vector-store adapter:

```console
cargo add runifold --features openai,qdrant
```

```rust,ignore
use std::sync::Arc;
use runifold::{
    Document, RetrievalContext, VectorRetriever,
    openai::OpenAiClient,
    qdrant::{QdrantConfig, QdrantVectorStore},
};

let embedder = Arc::new(
    OpenAiClient::from_api_key(api_key)?
        .embedding_model("text-embedding-3-small")?
);
let store = Arc::new(QdrantVectorStore::new(
    QdrantConfig::new("http://localhost:6333")?,
    "product-docs",
)?);
let retriever = VectorRetriever::new("product-docs", embedder, store);

retriever
    .index_documents(documents, RetrievalContext::new())
    .await?;
```

Use the `pgvector` feature and `PgVectorStore` for PostgreSQL. Schema creation
and HNSW index creation are explicit setup operations; lookup and upsert never
perform hidden migrations.

Retrieval quality can be measured independently of the model and Agent:

```rust,ignore
use runifold_testkit::{RetrievalEvaluationCase, RetrievalEvaluationRunner};

let report = RetrievalEvaluationRunner::new(Arc::new(retriever))
    .run(&cases)
    .await?;

assert!(report.mean_recall_at_k >= 0.90);
assert!(report.mean_reciprocal_rank >= 0.85);
```

## Status

Runifold is pre-alpha. The implemented foundation includes:

- stable run identity and causal event envelopes;
- hierarchical cancellation and deadlines;
- explicit capability grants;
- core-enforced child authority attenuation;
- budget accounting;
- effect descriptions;
- in-memory journaling and deterministic test helpers;
- provider-neutral multimodal messages and model requests;
- explicit capability and degradation semantics;
- strict streaming content-block accumulation;
- lossless provider-event escape hatches;
- an object-safe asynchronous model invocation boundary;
- wakeable hierarchical cancellation;
- a queue-backed scripted model for deterministic invocation tests;
- Responses and Chat Completions adapters for OpenAI, Azure OpenAI, Ark, Qwen, DeepSeek,
  OpenRouter, xAI, Groq, Mistral, Together AI, Perplexity Sonar, MiniMax,
  Zhipu AI, SiliconFlow, and custom endpoints;
- a native Anthropic Messages adapter with text, images, tools, thinking, and strict SSE decoding;
- native Gemini GenerateContent SSE and Ollama chat NDJSON adapters;
- native Amazon Bedrock Converse Stream through the AWS SDK, including
  `SigV4`, temporary credentials, Tools, reasoning, usage, cancellation, and deadlines;
- offline real-HTTP Bedrock binary EventStream cassettes covering arbitrary
  frame fragmentation, truncation, deadlines, and concurrent SDK streams;
- offline real-HTTP provider cassettes, including Azure API-key and Entra
  authentication, streaming fragmentation, delays, disconnects, and credential redaction;
- a shared Provider Conformance Kit covering identity, visible/reasoning
  separation, usage, raw events, error kinds, and retry safety;
- a framework-neutral Provider benchmark contract with TTFT, p50/p95/p99
  latency, throughput, reliability evidence, environment metadata, and
  baseline regression gates;
- an isolated release-mode Rig 0.40 comparison executor with equivalent-request
  validation, alternating paired rounds, bootstrap confidence intervals, and
  retained raw JSON evidence;
- concurrent real-HTTP provider stress tests with timeout, offline, and truncation classification;
- optional OpenTelemetry GenAI spans and metrics for models, agents, tools, and workflows;
- capability-safe MCP 2025-11-25 plus the 2026-07-28 stateless core, including
  Tools, Resources, Prompts, pagination, Resource Templates, subscriptions,
  Completion, Sampling, schema-driven `Mcp-Param-*` routing, bounded MRTR,
  authorization-partitioned response caching, durable Tasks backed by
  Runifold workflows, typed `notifications/tasks` state streams, and filtered
  `subscriptions/listen` over in-process, stdio, and Streamable HTTP
  transports;
- a capability-gated, object-safe tool runtime and deterministic registry;
- a bounded Model → Tool → Model agent loop;
- capability-gated Agent → Gateway → Agent delegation with child-run authority attenuation;
- composable around-middleware and asynchronous policies for Gateway governance;
- opt-in structured execution journals with cross-run causal links;
- revision-safe Agent checkpoints with explicit ambiguous-retry policy;
- capability-gated write-ahead effects with idempotent replay and conservative recovery;
- Tool and Agent delegation execution coordinated through the write-ahead effect boundary;
- optional durable SQLite stores for effects, checkpoints, and journals;
- cross-process crash recovery proving completed Tool effects are not re-executed;
- fluent Agent construction across OpenAI, Ark, Qwen, and custom compatible clients;
- provider-neutral embeddings, capability-gated Agent retrieval, and a deterministic in-memory vector index;
- native OpenAI-compatible, Gemini, and Ollama batch embedding adapters;
- typed OpenAI-compatible model discovery, bounded multipart file upload, and
  Batch create/inspect/cancel operations with browser-safe Gateway execution;
- typed OpenAI GA Realtime WebSocket sessions with bounded frames and browser
  receive queues, strict lifecycle validation, text, function-call, bounded
  PCM24/PCMU/PCMA audio and transcript deltas, redacted short-lived
  client-secret creation, cancellation/deadlines, and explicit ambiguous
  reconnect classification;
- browser-native OpenAI GA Realtime WebRTC with microphone capture, remote
  audio playback, bounded `oai-events`, direct ephemeral-secret negotiation,
  credential-free Gateway or unified server-side SDP exchange, validated
  STUN/TURN configuration, relay-only policy, observable Peer/ICE state, and
  phase-aware recovery safety verified against pinned coturn and a real relay
  network partition;
- a safety-first Realtime reconnect controller with bounded exponential
  backoff, per-invocation full jitter, cancellation/deadline enforcement,
  fresh credential/SDP negotiation on every factory invocation, redacted
  lifecycle events, fail-closed handling of ambiguous in-flight output, and a
  browser Gateway helper that rebuilds Peer/SDP/DataChannel resources while
  retrying only 408/429/5xx SDP exchange responses;
- a manual, opt-in live OpenAI Realtime canary that mints two short-lived
  client secrets, proves credential and effective-session rotation, validates
  bounded TTL, and emits only credential-free evidence;
- optional Qdrant and PostgreSQL/pgvector storage with one provider-neutral retriever;
- deterministic Recall@K, Precision@K, MRR, nDCG, latency, and usage evaluation;
- typed async Rust Tools with generated JSON Schemas and an attribute macro;
- host-only Tool state injection and explicit application-error normalization;
- backpressured Agent streaming across model, Tool, delegation, usage, and terminal events;
- Rust-type-derived structured outputs with local fail-closed decoding;
- deterministic multi-provider routing with explicit, stream-safe fallback authority;
- optional per-route circuit breakers with deterministic half-open recovery;
- bounded same-route retry with exponential backoff, jitter, `Retry-After`, and deadline truncation;
- one `Model + ProviderModel` integration contract that automatically unlocks
  canonical streaming, resilient routing, Agent construction, budgets,
  OpenTelemetry instrumentation, and durable workflow execution;
- durable sequential and conditional workflows with explicit per-step authority;
- Agent-backed workflow steps, causal child runs, and conservative checkpoint recovery;
- atomic scoped budget reservations for concurrent child runs;
- durable fail-fast parallel workflows with stable joins and per-branch recovery;
- side-effect-safe first-success races with fair start, conservative losing-budget accounting, and durable winners;
- provider-neutral distributed workflow claims with leases, heartbeats, delayed retries, and fencing tokens;
- a definition-registered worker runtime with fenced checkpoints, automatic heartbeat, lease-loss cancellation, crash resume, bounded supervision, graceful drain, and operational metrics;
- lease-free durable timers and idempotent external signals that survive process restarts and early webhook delivery;
- store-authoritative signal-or-timeout races, externally fenced cancellation, and auditable signal dead letters with safe retention.
- durable human review with inspectable interrupt state, typed approve/edit/reject decisions, idempotent delivery, and crash-safe resume;
- immutable checkpoint history with bounded state inspection, idempotent fork/replay, explicit ambiguous-effect authority, and durable lineage;
- typed multi-turn conversations with append-only transcripts, summary-buffer backpressure, bounded windows, and provenance-required cross-session semantic memory;
- tenant-scoped workflow admission with outstanding/concurrent quotas, fair claims, and fail-closed control-plane isolation;
- durable tenant token, cost, duration, turn, tool-call, and delegation budgets with atomic reservation, settlement, and crash recovery.
- cursor-paginated tenant budget audits and identity-safe OpenTelemetry metrics for admission, utilization, reservation age, and recovery forfeiture.
- restart-safe bounded OTel budget projection with named durable cursors, monotonic CAS, explicit at-least-once semantics, and compaction protection for slow consumers.
- fenced terminal Task retention with bounded PostgreSQL cleanup batches and immutable tombstone audit.
- dynamically sharded Task cleanup supervision with database-clock heartbeat, bounded concurrency, health snapshots, and low-cardinality OpenTelemetry metrics.
- governed tombstone lifecycle with legal holds, monotonic archive receipts, independent approval, fenced purge recovery, and immutable deletion evidence.
- fail-closed tenant-scoped governance authorization, authenticated audit actors, idempotent archive delivery, and low-cardinality governance telemetry.
- durable purge approval inboxes with bounded discovery, independent reviewer claims, timeout takeover fencing, and auditable approve/reject decisions.
- optional S3-compatible WORM tombstone archives with pre-signed PUT/HEAD authority, SHA-256 reconciliation, encryption, and Object Lock.
- native SDK-independent S3 SigV4 signing for AWS, MinIO, temporary credentials, and custom path-style endpoints.
- bounded S3 archive I/O with typed failure classes and automatic reconciliation when a commit succeeds but its response is lost.
- exclusively leased budget projection supervisors with database-clock expiry, heartbeat renewal, fencing-token takeover, cancellation-safe release, and low-cardinality lease-loss alerts.
- lock-free live projection health snapshots for readiness and control planes, including lease ownership, catch-up state, last acknowledged cursor, throughput, and failures.
- dynamically discovered multi-tenant budget projection with stable keyset pagination, deterministic no-coordinator sharding, bounded concurrency, and lease-safe rebalancing.

See [RFC 0003](docs/rfcs/0003-model-invocation-boundary.md) for the model
invocation boundary and [RFC 0008](docs/rfcs/0008-agent-delegation-gateway.md)
for delegation semantics. Gateway governance is specified in
[RFC 0009](docs/rfcs/0009-gateway-middleware.md), and run-tree events in
[RFC 0010](docs/rfcs/0010-run-observability.md). Recovery semantics are in
[RFC 0011](docs/rfcs/0011-agent-checkpoints.md), effect recovery in
[RFC 0012](docs/rfcs/0012-write-ahead-effects.md), and callable integration in
[RFC 0013](docs/rfcs/0013-agent-callable-effects.md). The SQLite adapter is
specified in [RFC 0014](docs/rfcs/0014-sqlite-stores.md), and the ergonomic
Agent surface in [RFC 0015](docs/rfcs/0015-agent-builder.md). Typed functions
are specified in [RFC 0016](docs/rfcs/0016-typed-function-tools.md), with
state and error boundaries in
[RFC 0017](docs/rfcs/0017-tool-state-and-errors.md). Agent streaming is
specified in [RFC 0018](docs/rfcs/0018-agent-streaming.md), and typed
structured output in
[RFC 0019](docs/rfcs/0019-typed-structured-output.md). Safe model routing is
specified in [RFC 0020](docs/rfcs/0020-model-router.md), and circuit-breaker
semantics in [RFC 0021](docs/rfcs/0021-router-circuit-breaker.md). Retry and
backoff are specified in
[RFC 0022](docs/rfcs/0022-router-retry-backoff.md). Durable orchestration is
specified in
[RFC 0023](docs/rfcs/0023-durable-workflow-orchestration.md), with budget
reservation and parallel recovery in
[RFC 0024](docs/rfcs/0024-budget-reservations-and-parallel-workflows.md), and
safe first-success competition in
[RFC 0025](docs/rfcs/0025-safe-first-success-race.md). Native Anthropic
translation and provider conformance testing are specified in
[RFC 0026](docs/rfcs/0026-anthropic-and-provider-testkit.md).
Gemini and Ollama native protocol boundaries are specified in
[RFC 0027](docs/rfcs/0027-gemini-and-ollama-providers.md).
Provider transport reliability and concurrency contracts are specified in
[RFC 0028](docs/rfcs/0028-provider-transport-reliability.md).
The feature-gated provider crate topology and companion-crate threshold
are specified in
[RFC 0052](docs/rfcs/0052-provider-crate-topology.md).
The provider identity contract and automatic resilient runtime composition are
specified in
[RFC 0053](docs/rfcs/0053-provider-runtime-contract.md).
The native Amazon Bedrock SDK boundary is specified in
[RFC 0054](docs/rfcs/0054-amazon-bedrock-provider.md).
OpenTelemetry GenAI signal, privacy, and dependency boundaries are specified in
[RFC 0029](docs/rfcs/0029-opentelemetry-genai-observability.md).
Native MCP Tools and stdio transport semantics are specified in
[RFC 0030](docs/rfcs/0030-mcp-tools-edge.md). Streamable HTTP sessions,
authentication, SSE resumption, and network failure semantics are specified in
[RFC 0031](docs/rfcs/0031-mcp-streamable-http.md).
Capability-safe Resources and user-controlled Prompts are specified in
[RFC 0032](docs/rfcs/0032-mcp-resources-prompts.md).
Session-bound pagination, Resource Templates, subscriptions, and Completion are
specified in [RFC 0033](docs/rfcs/0033-mcp-dynamic-context.md).
Client-owned model selection, dual approval, resource limits, and bidirectional
Sampling transports are specified in
[RFC 0034](docs/rfcs/0034-mcp-sampling.md).
Versioned Agent evaluation datasets, async scorers, Run/Trace correlation, and
relative regression gates are specified in
[RFC 0035](docs/rfcs/0035-agent-quality-evaluation.md).
JSONL execution, external Candidate protocol, CI exit codes, and
JSON/JUnit/Markdown reporting are specified in
[RFC 0036](docs/rfcs/0036-evaluation-cli-ci.md).
Seeded repetitions, confidence gates, deterministic sharding, resumable
checkpoints, and evidence-validating shard merge are specified in
[RFC 0037](docs/rfcs/0037-reproducible-evaluation-experiments.md).
Case-level recovery, latency/Token/cost evidence, resource budgets, and the
reusable GitHub Actions gate are specified in
[RFC 0038](docs/rfcs/0038-evaluation-resources-case-recovery-ci.md).
Release integrity, MSRV, SemVer, supply-chain policy, SBOMs, and controlled
crates.io publication are specified in
[RFC 0039](docs/rfcs/0039-release-integrity-and-supply-chain.md); maintainers
should follow the [release runbook](docs/RELEASING.md). Provider-neutral
embedding, retrieval authority, untrusted context, and recovery semantics are
specified in [RFC 0040](docs/rfcs/0040-provider-neutral-retrieval.md). Native
embedding adapter behavior is specified in
[RFC 0041](docs/rfcs/0041-native-embedding-providers.md). Replaceable vector
stores and retrieval evaluation are specified in
[RFC 0042](docs/rfcs/0042-vector-stores-and-retrieval-evaluation.md).
Distributed workflow claims, authoritative leases, heartbeats, and fencing are
specified in
[RFC 0043](docs/rfcs/0043-distributed-workflow-leases.md). Worker execution,
fenced checkpoint CAS, and recovery supervision are specified in
[RFC 0044](docs/rfcs/0044-workflow-worker-runtime.md). Lease-free timers,
buffered signals, wake recovery, and idempotency are specified in
[RFC 0045](docs/rfcs/0045-durable-timers-and-signals.md). Deadline races,
external cancellation, and signal lifecycle governance are specified in
[RFC 0046](docs/rfcs/0046-durable-wait-governance.md). Multi-tenant workflow
admission, fairness, and control-plane isolation are specified in
[RFC 0047](docs/rfcs/0047-multi-tenant-workflow-admission.md).
Durable aggregate tenant budget reservation and settlement are specified in
[RFC 0048](docs/rfcs/0048-durable-tenant-budget-ledger.md).
Durable budget audit and low-cardinality telemetry projection are specified in
[RFC 0049](docs/rfcs/0049-tenant-budget-observability.md). Projection leases,
heartbeats, fencing, and continuous supervision are specified in
[RFC 0050](docs/rfcs/0050-budget-projection-supervision.md). Multi-tenant
discovery, deterministic sharding, and bounded projection coordination are
specified in
[RFC 0051](docs/rfcs/0051-multi-tenant-budget-projection-coordination.md).

Enable the `otel` feature to decorate model calls and durable run events:

```rust,ignore
use std::sync::Arc;
use runifold::{InMemoryJournal, Model, otel::OtelRuntime};

let telemetry = OtelRuntime::new();
let observed_model: Arc<dyn Model> = Arc::new(telemetry.model(provider));
let observed_journal = Arc::new(telemetry.journal(InMemoryJournal::new()));
```

Models and journals created from the same `OtelRuntime` share causal Run
correlation, so Turn, model, Tool, delegation, child-Agent, Router fallback,
and scoped MCP Sampling operations appear in one causal trace. The runtime
uses global OpenTelemetry providers by default and also supports explicit
tracer and meter injection. Prompt, response, Tool definition, Tool argument,
Tool result, and exception-message capture is disabled by default.
Low-cardinality operational metrics cover Agent and Turn duration, usage and
cost, errors and budget exhaustion, plus MCP Sampling requests, duration, and
failures. Run, invocation, call, and entity identities remain trace-only.
Recommended histogram buckets are enabled by default. Versioned Prometheus
recording/alert rules and a Grafana dashboard are embedded as
`otel::slo::PROMETHEUS_RULES` and `otel::slo::GRAFANA_DASHBOARD`; see the
[operations runbook](docs/operations-slo.md).

Offline quality evaluation stays separate from operational telemetry:

```rust,ignore
use runifold_testkit::{
    EvaluationCase, EvaluationDataset, EvaluationOutput, EvaluationRunner,
    JsonExactMatchScorer, RegressionPolicy,
};

let dataset = EvaluationDataset::new(
    "support-answers",
    "2026-07-26",
    vec![
        EvaluationCase::new("refund-policy", serde_json::json!("question"))?
            .with_expected(serde_json::json!("approved answer")),
    ],
)?;
let runner = EvaluationRunner::new(|case: EvaluationCase| async move {
    let output = run_candidate(case.input()).await?;
    Ok(EvaluationOutput::new(output.value).with_run_id(output.run_id))
})
.with_scorer(JsonExactMatchScorer);

let candidate = runner.run(&dataset, "prompt-v2").await?;
let comparison = candidate.compare(
    &baseline,
    &RegressionPolicy::new(0.02, 0.05, 0.0)?,
)?;
assert!(comparison.passed);
```

Reports retain Case and Run IDs for trace lookup but omit raw inputs,
references, outputs, prompts, and transcripts.
Built-in `TokenOverlapScorer` and weighted `JsonRuleScorer` cover deterministic
checks. `ModelJudgeScorer` uses any canonical Provider or Router with strict
structured output and local validation. `FileEvaluationRepository` persists
immutable, versioned datasets and reports with traversal-safe paths and
conflict detection.

Run the same gate from CI without linking an application into the CLI:

```bash
runifold-eval run \
  --dataset evals/support.jsonl \
  --dataset-name support \
  --dataset-version 2026-07-26 \
  --candidate-version prompt-v2 \
  --output artifacts/evaluation.json \
  --junit artifacts/evaluation.xml \
  --markdown artifacts/evaluation.md \
  -- ./target/release/my-eval-candidate
```

The Candidate receives only Case ID, input, and tags—never the reference
answer. Exit code `0` passes, `1` means CLI configuration or artifact failure,
and `2` means a Candidate, scorer, quality, or regression gate failed.

Measure sampling variance and resume interrupted evaluations with:

```bash
runifold-eval experiment \
  --dataset evals/support.jsonl \
  --dataset-name support \
  --dataset-version 2026-07-27 \
  --candidate-version prompt-v3 \
  --samples 10 \
  --seed 42 \
  --cache-dir .runifold/eval-cache \
  --min-confidence-lower-bound 0.85 \
  --max-flaky-case-rate 0.02 \
  --max-p95-latency-ms 3000 \
  --max-total-tokens 250000 \
  --max-total-cost-usd 10.00 \
  --output artifacts/experiment.json \
  --junit artifacts/experiment.xml \
  --markdown artifacts/experiment.md \
  -- ./target/release/my-eval-candidate
```

The Candidate receives a stable `sample_index` and per-case `seed`. It may
return `input_tokens`, `output_tokens`, and `cost_usd`; host latency is measured
independently. Every completed Case and Sample is checkpointed under a
fingerprint of the dataset content, Candidate command, scorer, seed, shard, and
process limits. Corrupt or
configuration-mismatched cache entries fail closed. Use `--shard-index` and
`--shard-count` for distributed execution, then `runifold-eval merge` to
validate and combine every shard before applying the final confidence and
flakiness gates. Resource gates fail closed when required usage is missing.

The same gate is available to same-repository GitHub Actions callers:

```yaml
jobs:
  evaluate:
    uses: ./.github/workflows/runifold-evaluation.yml
    with:
      dataset: evals/support.jsonl
      dataset-name: support
      dataset-version: 2026-07-27
      candidate-version: prompt-v4
      candidate-build-command: cargo build --locked --release -p my-eval-candidate
      candidate-command: ./target/release/my-eval-candidate
      samples: 10
      max-p95-latency-ms: "3000"
      max-total-tokens: "250000"
      max-total-cost-usd: "10.00"
    secrets:
      openai-api-key: ${{ secrets.OPENAI_API_KEY }}
```

Enable the `mcp` feature to expose authorized local Tools or import explicitly
classified remote Tools:

```rust,ignore
use std::sync::Arc;
use runifold::mcp::{
    Implementation, McpClient, McpClientConfig, McpRemoteTool, McpServer,
    RemoteToolPolicy,
};

let session = McpServer::new(
    Arc::new(local_tools),
    server_authority,
    Implementation::new("my-server", "0.1.0"),
).session();

let client = McpClient::new(
    Arc::new(session),
    McpClientConfig::new(Implementation::new("my-client", "0.1.0")),
);
client.initialize().await?;
let discovered = client.list_tools().await?;

let remote = McpRemoteTool::new(
    client,
    discovered.into_iter().next().ok_or("no tools")?,
    RemoteToolPolicy::new(
        runifold::core::EffectClass::ReadOnly,
        runifold::core::RiskLevel::Medium,
    ),
)?;
```

MCP annotations remain untrusted. The host must select effect and risk policy,
and an MCP server lists only capabilities granted by its `RunContext`.

The same client works over a production HTTP boundary:

```rust,ignore
use std::sync::Arc;
use runifold::mcp::{
    Implementation, McpClient, McpClientConfig, StreamableHttpTransport,
};

let transport = Arc::new(StreamableHttpTransport::new(
    "http://127.0.0.1:3000/mcp",
)?);
let client = McpClient::new(
    transport,
    McpClientConfig::new(Implementation::new("my-client", "0.1.0")),
);
let mode = client.connect().await?;
let tools = client.list_tools().await?;
```

`connect()` discovers the server and prefers the 2026-07-28 stateless request
data plane. Tools, Resources, Prompts, Completion, pagination, per-request
client metadata, result metadata, and the standard HTTP routing headers operate
without an initialization handshake or HTTP session. If discovery identifies a
legacy-only server, Runifold falls back to the finalized 2025-11-25
initialization flow. The returned `McpProtocolMode` makes that choice explicit.

`StreamableHttpTransport` accepts JSON and request-scoped SSE responses. Modern
requests validate mirrored protocol, method, name, and schema-designated Tool
parameter headers against the body. The HTTP client accepts only statically
reachable primitive `x-mcp-header` declarations, excludes an invalid Tool from
discovery, and safely encodes Unicode, whitespace, control characters, and the
Base64 sentinel itself. Legacy mode retains opaque session state and resumable
server notifications. The transport never retries a request implicitly: an
expired legacy session is returned as `McpError::SessionExpired`, so a host
cannot accidentally duplicate a Tool effect. Do not annotate secrets with
`x-mcp-header`, because infrastructure may record HTTP headers.
`McpHttpServerConfig` rejects unknown browser origins by default and can require
a bearer `HttpAuthorizer`. Public deployments should terminate TLS at the
process or a trusted reverse proxy.

Modern server-to-client interaction is explicit and request-scoped.
`McpClient::listen` opens a filtered `subscriptions/listen` stream; the server
acknowledges only supported and authorized notification classes, and every
event is correlated to the listen request. Multiple stdio subscriptions are
demultiplexed independently, while HTTP uses one POST/SSE response per
subscription and allocates no protocol session.

MRTR incomplete results are handled under one total deadline and bounded round
count. Each retry receives a fresh JSON-RPC ID, only the latest keyed
`inputResponses`, and the exact opaque `requestState` returned by the server.
Hosts can install a generic `MrtrInputHandler`; an existing `SamplingService`
automatically resolves `sampling/createMessage`. On the server,
`MrtrToolGate` runs with attenuated Tool authority and must validate any echoed
state before returning `Proceed`. The canonical Tool is invoked only after the
gate proceeds.

Resources and Prompts use the same negotiated client:

```rust,ignore
use std::collections::BTreeMap;

let resources = client.list_resources().await?;
let templates = client.list_resource_templates().await?;
let content = client.read_resource(&resources[0].uri).await?;

let prompts = client.list_prompts().await?;
let rendered = client
    .get_prompt(
        &prompts[0].name,
        BTreeMap::from([("code".into(), "fn main() {}".into())]),
    )
    .await?;
```

Resource and Prompt registries repeat authorization at execution time and
create child runs containing only the selected capability. Prompt results are
returned to the host and are never inserted into a model request
automatically.

All list methods follow opaque pagination cursors automatically; matching
`*_page` methods expose one page when a host needs incremental discovery.
Resource updates require an explicit per-session subscription and are delivered
through `client.notifications()`.

Basic Sampling lets a server request a host-controlled model call without
receiving model credentials or choosing the final model:

```rust,ignore
use std::sync::Arc;
use runifold::mcp::{
    CreateMessageParams, FixedSamplingModel, McpClientConfig, ModelSamplingProvider,
    SamplingMessage, SamplingPolicy, SamplingService,
};
use runifold::model::ModelRef;

let provider = Arc::new(ModelSamplingProvider::new(
    host_model,
    Arc::new(FixedSamplingModel::new(ModelRef::new("anthropic", "claude-sonnet"))),
));
let sampling = Arc::new(SamplingService::new(
    host_approver,
    provider,
    SamplingPolicy::default(),
));
let config = McpClientConfig::new(client_implementation).with_sampling(sampling);

let result = initialized_session
    .sampling_client()
    .create_message(CreateMessageParams::new(
        vec![SamplingMessage::user_text("Summarize the approved input")],
        512,
    ))
    .await?;
```

`SamplingApprover` reviews both the request and the generated response. The
client advertises only basic Sampling: Tool-enabled Sampling and ambient
context inclusion remain fail-closed until their separate authority and loop
contracts are implemented.

Enable the first provider edge with the `openai` feature:

```rust,no_run
use runifold::{
    Budget, BudgetTracker, RunContext,
    openai::{OpenAiAgentExt, OpenAiClient},
};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let agent = OpenAiClient::from_api_key(std::env::var("OPENAI_API_KEY")?)?
    .agent("assistant", "gpt-5")
    .system("Answer precisely and expose uncertainty.")
    .max_turns(8)
    .build()?;
let answer = agent.prompt_text("Why is durable execution useful?").await?;
# let _ = answer;

// Advanced execution keeps authority and accounting explicit.
let run = RunContext::root(
    BudgetTracker::new(Budget::default()),
    agent.callable_capabilities(),
);
let response = agent.run("Why is durable execution useful?", &run).await?;
# let _ = response;
# Ok(())
# }
```

The library does not read credentials implicitly. Applications decide how
secrets enter their process.

Anthropic uses its native Messages protocol behind the `anthropic` feature:

```rust,no_run
use runifold::anthropic::{AnthropicAgentExt, AnthropicClient};

# fn example() -> Result<(), Box<dyn std::error::Error>> {
let agent = AnthropicClient::from_api_key(std::env::var("ANTHROPIC_API_KEY")?)?
    .agent("researcher", "claude-sonnet-4-5")
    .system("Separate evidence from inference.")
    .build()?;
# let _ = agent;
# Ok(())
# }
```

Runifold keeps error responsibilities at the correct boundary:

- public library APIs expose typed `thiserror` errors that callers can match,
  serialize where supported, and inspect for retry safety;
- application and example code may aggregate those errors with
  `anyhow::Result` and add operational context;
- model, Tool, Agent, Gateway, checkpoint, effect, and store failures are not
  erased into opaque strings inside the library.

See `crates/runifold/examples/error_context.rs` for the application-boundary
pattern.

Terminal model output can be constrained and decoded as a Rust type:

```rust,ignore
use runifold::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
struct ResearchAnswer {
    summary: String,
    confidence: f64,
}

let agent = client
    .agent("researcher", "your-model")
    .build_structured::<ResearchAnswer>("research_answer")?;

let typed = agent
    .run("Assess the evidence", &run)
    .await?;

println!("{} ({})", typed.output.summary, typed.output.confidence);
```

The Rust type generates the provider-facing JSON Schema, but provider
acceptance is never treated as proof. Runifold assembles only canonical text,
fails closed on refusals, and deserializes locally before returning the typed
value. The full response, transcript, counters, and usage remain available in
`typed.outcome`.

Typed Tools are ordinary async Rust functions:

```rust,ignore
use runifold::{JsonSchema, ToolContext, ToolError};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, JsonSchema)]
struct AddInput {
    left: i64,
    right: i64,
}

#[derive(JsonSchema, Serialize)]
struct AddOutput {
    sum: i64,
}

#[runifold::tool(
    description = "Add two signed integers",
    effect = "pure",
    risk = "low"
)]
async fn add(
    input: AddInput,
    _context: ToolContext,
) -> Result<AddOutput, ToolError> {
    Ok(AddOutput {
        sum: input.left + input.right,
    })
}

let agent = client
    .agent("assistant", "gpt-5")
    .tool(add_tool())
    .build()?;
```

The macro generates `add_tool()`. Input is validated before the handler runs,
output is serialized after it succeeds, and both JSON Schemas become part of
the Tool capability contract. `FunctionTool` exposes the same mechanism
without using the attribute macro.

Application services can be injected without exposing them to the model:

```rust,ignore
use std::sync::Arc;
use runifold::{IntoToolError, State, ToolContext, ToolError};

#[runifold::tool(
    description = "Search the application index",
    effect = "read_only",
    risk = "low"
)]
async fn search(
    state: State<SearchService>,
    input: SearchInput,
    context: ToolContext,
) -> Result<SearchOutput, SearchError> {
    state.search(input, context).await
}

impl IntoToolError for SearchError {
    fn into_tool_error(self) -> ToolError {
        // Select an intentionally safe category, message, and retry policy.
        ToolError::local(
            runifold::ToolErrorKind::Execution,
            "search is temporarily unavailable",
        )
    }
}

let tool = search_tool(Arc::new(SearchService::new()));
```

`State<T>` never enters the model schema, transcript, or Effect input.
Application errors are not converted through `Display` automatically because
their text may contain credentials, queries, or internal implementation data.

The same Agent loop can be consumed as a backpressured event stream:

```rust,ignore
use futures_util::StreamExt;
use runifold::{AgentStreamEvent, model::ModelStreamEvent};

let mut events = agent.stream("Explain the recovery design", &run);
while let Some(event) = events.next().await {
    match event? {
        AgentStreamEvent::Model {
            event: ModelStreamEvent::TextDelta { text, .. },
            ..
        } => print!("{text}"),
        AgentStreamEvent::CallableStarted { kind, call, .. } => {
            eprintln!("{kind:?} {} started", call.name);
        }
        AgentStreamEvent::UsageUpdated { usage } => {
            eprintln!("tokens: {}", usage.tokens);
        }
        AgentStreamEvent::Completed { outcome } => {
            eprintln!("completed in {} turns", outcome.turns);
        }
        _ => {}
    }
}
```

Every visible event introduces a poll boundary. A slow consumer therefore
slows Agent execution instead of growing an unbounded event buffer.

Provider identity and wire protocol are configured independently:

```rust,no_run
use runifold::openai::{OpenAiConfig, OpenAiWireProtocol};

# fn example() -> Result<(), Box<dyn std::error::Error>> {
let ark = OpenAiConfig::ark(std::env::var("ARK_API_KEY")?)?;

let qwen = OpenAiConfig::qwen(
    std::env::var("DASHSCOPE_API_KEY")?,
    "https://dashscope.aliyuncs.com/compatible-mode/v1",
    OpenAiWireProtocol::ChatCompletions,
)?;

let custom = OpenAiConfig::custom(
    "my-gateway",
    "https://llm.example.com/v1",
    OpenAiWireProtocol::ChatCompletions,
)?;
# let _ = (ark, qwen, custom);
# Ok(())
# }
```

Multiple physical models can sit behind one logical model identity:

```rust,ignore
use std::{sync::Arc, time::Duration};
use runifold::{
    Agent, CircuitBreakerConfig, ModelFallbackPolicy, ModelRef, ModelRetryPolicy,
    ModelRouter, RetryJitter,
    model::ModelErrorKind,
};

let logical = ModelRef::new("router", "assistant");
let router = ModelRouter::builder(logical.clone())
    .route(
        "openai-primary",
        Arc::new(openai_client),
        ModelRef::new("openai", "your-primary-model"),
    )
    .route(
        "ark-backup",
        Arc::new(ark_client),
        ModelRef::new("ark", "your-backup-model"),
    )
    .fallback_policy(
        ModelFallbackPolicy::safe_only()
            .allow_unknown(ModelErrorKind::Transport)
            .allow_unknown(ModelErrorKind::Provider),
    )
    .circuit_breaker(CircuitBreakerConfig::new(
        3,
        Duration::from_secs(30),
    )?)
    .retry_policy(
        ModelRetryPolicy::exponential(
            3,
            Duration::from_millis(100),
            Duration::from_secs(2),
            2,
        )?
        .jitter(RetryJitter::Full)
        .allow_unknown(ModelErrorKind::Transport),
    )
    .build()?;

let agent = Agent::builder("assistant", Arc::new(router), logical)
    .build()?;
```

The default policy only falls back for errors explicitly marked retry-safe.
Allowing an error kind with unknown safety is deliberate authority to risk a
second provider charge. Cancellation never falls back, and after the first
canonical stream event Runifold locks the selected route to prevent duplicate
visible output. The selected route and safe summaries of earlier failures are
retained as canonical provider events.

Circuit breakers are opt-in and independent per physical route. After the
configured number of consecutive counted failures, a route is skipped until
its cooldown expires. Exactly one request becomes the half-open recovery
probe; all concurrent requests continue to other routes. Terminal success
closes the circuit, while a failed or abandoned probe reopens it.
`router.route_health()` returns immutable health snapshots suitable for
metrics and readiness diagnostics.

Retry is also opt-in. `max_attempts` includes the initial call, and every retry
gets a distinct invocation identity. The effective wait is the greater of
local backoff and provider `Retry-After`. Runifold stops before sleeping when
the delay would cross the invocation deadline, observes cancellation during
the wait, and never retries after the first canonical stream event.

Child agents are exposed through an explicit gateway route. The route itself
is an `Agent` capability, while the child receives only the configured subset
of the parent's capabilities:

```rust,no_run
use std::sync::Arc;

use runifold::{
    Agent, AgentDescriptor, AgentGateway, AgentRoute, Model, ModelRef,
};

# fn example(
#     parent_model: Arc<dyn Model>,
#     child_model: Arc<dyn Model>,
# ) -> Result<(), Box<dyn std::error::Error>> {
let child = Arc::new(Agent::new(
    "researcher",
    child_model,
    ModelRef::new("qwen", "your-child-model"),
));
let descriptor = AgentDescriptor::new(
    "ask_researcher",
    "Delegate focused research to the researcher agent",
);
let mut gateway = AgentGateway::new();
gateway.register(AgentRoute::new(descriptor.clone(), child))?;

// Grant `descriptor.capability()` to the parent RunContext before execution.
let parent = Agent::new(
    "coordinator",
    parent_model,
    ModelRef::new("ark", "your-parent-model"),
)
.agents(gateway);
# let _ = (parent, descriptor);
# Ok(())
# }
```

Gateway middleware uses an around-call boundary. It may inspect or transform
input, deny execution, observe results, or explicitly retry:

```rust,ignore
impl GatewayMiddleware for AuditLayer {
    fn handle<'a>(
        &'a self,
        request: DelegationRequest,
        next: GatewayNext<'a>,
    ) -> GatewayFuture<'a, Result<AgentOutcome, GatewayError>> {
        Box::pin(async move {
            self.record_started(&request);
            let result = next.run(request).await;
            self.record_finished(&result);
            result
        })
    }
}
```

Route identity and parent authority cannot be replaced by middleware. Every
call to `next` re-enters the protected lifecycle, capability, authority, depth,
and budget boundary.

Attach a journal to the root Run to observe the complete execution tree:

```rust,no_run
use std::sync::Arc;

use runifold::{
    Budget, BudgetTracker, CapabilitySet, InMemoryJournal, RunContext,
};

let journal = InMemoryJournal::new();
let run = RunContext::root(
    BudgetTracker::new(Budget::default()),
    CapabilitySet::new(),
)
.with_journal(Arc::new(journal.clone()));

// Run an Agent with `run`, then inspect or export `journal.events()`.
# let _ = run;
```

Agent events contain identities, state transitions, normalized error kinds,
usage, and counters. Prompt text, tool arguments, and output bodies are not
recorded by default.

Checkpointed execution persists the canonical transcript and local counters:

```rust,no_run
use std::sync::Arc;

use runifold::{
    AgentCheckpoint, InMemoryCheckpointStore, ResumePolicy,
};

# async fn example(
#     agent: &runifold::Agent,
#     run: &runifold::RunContext,
# ) -> Result<(), runifold::AgentError> {
let checkpoint = AgentCheckpoint::new(Arc::new(
    InMemoryCheckpointStore::new(),
));

let outcome = agent
    .run_checkpointed("perform the task", run, &checkpoint)
    .await?;

// Reopening a completed checkpoint is idempotent and performs no model call.
let same_outcome = agent
    .resume(&checkpoint, run, ResumePolicy::RejectAmbiguous)
    .await?;
# let _ = (outcome, same_outcome);
# Ok(())
# }
```

If interruption occurs during a model/tool/delegation turn, normal resume
returns `AmbiguousCheckpoint`. Retrying that turn requires the explicit
`RetryInterruptedTurn` policy because it may repeat model cost. Completed Tool
and delegation effects are replayed without handler execution when the Agent
uses the same effect store.

External effects can use a finer-grained write-ahead protocol:

```rust,ignore
let executor = EffectExecutor::new(Arc::new(
    InMemoryEffectStore::new(),
));

let outcome = executor
    .execute(
        effect_request,
        &run,
        &handler,
        EffectRecoveryPolicy::RejectAmbiguous,
    )
    .await?;
```

Completed effects are replayed from durable state. A `Started` effect is
retried only with explicit `RetrySafe` policy and only when it is Pure,
ReadOnly, or an IdempotentWrite with an idempotency key.

Agents create their own in-memory executor by default. Inject a shared,
durable executor with `Agent::effect_executor` when recovery must survive
process restarts. Callable keys are derived from execution identity, Agent
name, turn, and call position; a different request at the same position is
rejected instead of replaying the wrong result.

For durable local execution, enable `sqlite` (system SQLite) or
`sqlite-bundled`, then share one store across the three persistence roles:

```rust,ignore
use std::sync::Arc;

use runifold::{
    AgentCheckpoint, EffectExecutor,
    sqlite::SqliteStore,
};

let store = Arc::new(SqliteStore::open("runifold.db")?);
let checkpoint = AgentCheckpoint::new(store.clone());
let executor = EffectExecutor::new(store.clone());
let run = run.with_journal(store);
let agent = agent.effect_executor(executor);
```

SQLite is an optional local adapter, not part of the runtime kernel. A service
can implement the same traits with PostgreSQL or another transactional store.

Distributed Workflow workers use the `workflow-postgres` feature:

```rust,ignore
use std::{sync::Arc, time::Duration};
use runifold::{
    Budget, CancellationToken, CapabilitySet, LeaseDuration, WorkerId,
    WorkflowDefinition, WorkflowRegistry, WorkflowResumePolicy,
    WorkflowSupervisor, WorkflowSupervisorConfig, WorkflowWorker,
    postgres::PostgresWorkflowStore,
};

let store = Arc::new(
    PostgresWorkflowStore::connect(&database_url, "runifold_workflows").await?
);
store.ensure_schema().await?;

let mut registry = WorkflowRegistry::new();
registry.register(
    WorkflowDefinition::new(
        Arc::new(workflow),
        Budget::default(),
        CapabilitySet::new(),
    )
    .with_resume_policy(WorkflowResumePolicy::RetryInterruptedStep),
)?;

let worker = WorkflowWorker::new(
    store,
    registry,
    WorkerId::parse("worker-01")?,
    LeaseDuration::new(Duration::from_secs(30))?,
    Duration::from_secs(10),
)?;

let shutdown = CancellationToken::new();
let supervisor = WorkflowSupervisor::new(
    Arc::new(worker),
    WorkflowSupervisorConfig::new(16)?
        .with_backoff(Duration::from_millis(25), Duration::from_secs(5))?,
);
let report = supervisor.run(&shutdown).await;
```

`run_once` remains available for embedded hosts and claims at most one task.
`WorkflowSupervisor` adds continuous polling, bounded concurrency, exponential
idle/error backoff, a low-cardinality metric snapshot, and graceful shutdown
that stops admission before draining started cycles. A failed heartbeat
cancels and joins the in-flight Workflow before returning `LeaseLost`. Every
distributed checkpoint write also validates the worker fencing token
independently.

Durable waits are definition nodes, not sleeping worker futures:

```rust,ignore
let tenant = WorkflowTenantId::parse("acme")?;
store
    .set_tenant_policy(
        tenant.clone(),
        WorkflowTenantPolicy::new(10_000, 100)?,
    )
    .await?;

let workflow = Workflow::builder("approval")
    .timer("cooldown", Duration::from_secs(30))
    .wait_for_signal_or_timeout(
        "approval",
        "approved",
        Duration::from_secs(24 * 60 * 60),
    )
    .agent("fulfill", fulfillment_agent, capabilities)
    .build()?;

let signal = WorkflowSignal::new(
    workflow_checkpoint_id,
    WorkflowSignalName::parse("approved")?,
    serde_json::json!({"approved_by": "operator-7"}),
)?;
let outcome = store.publish_signal(tenant.clone(), signal).await?;
```

Timers use store-authoritative time and hold no lease while waiting. Signals
target a workflow checkpoint and carry a stable publication identity:
duplicates with identical content are accepted idempotently, conflicting reuse
is rejected, and signals received before the wait are buffered. A
signal-or-timeout node emits a typed `WorkflowWaitOutcome`, with the store
choosing exactly one winner. `WorkflowStore::cancel` fences leased work;
`inspect_signal` exposes lifecycle metadata without payloads; and
`compact_signals` deletes only expired consumed or dead-letter identities.
Every external control-plane operation also requires a `WorkflowTenantId`.
Claims rotate across eligible tenants before applying task priority, while
each tenant's outstanding-task and unexpired-lease limits are enforced
independently.

Human review uses the same durable wake and fencing machinery:

```rust,ignore
let workflow = Workflow::builder("transfer")
    .interrupt("review", "Review the proposed transfer")
    .agent("continue", transfer_agent, capabilities)
    .build()?;

let snapshot = store.inspect(tenant.clone(), workflow_checkpoint_id).await?;
let request = snapshot.interrupt.expect("workflow is awaiting review");
let command = WorkflowInterruptCommand::new(
    workflow_checkpoint_id,
    request.interrupt_id,
    WorkflowInterruptDecision::edit(serde_json::json!({"amount": 40}))?,
)?;
let outcome = store.decide_interrupt(tenant, command).await?;
```

The prompt, proposal, and stable interrupt identity are checkpointed before
the worker lease is released. A decision ID is independently stable, so an
operator can safely retry the same command after a timeout. The downstream
node receives a typed `WorkflowInterruptOutcome`, preserving the distinction
between approval, edit, and rejection.

Checkpoint time travel creates a new execution instead of mutating history:

```rust,ignore
let revisions = store
    .list_checkpoint_history(
        tenant.clone(),
        workflow_checkpoint_id,
        None,
        WorkflowCheckpointHistoryLimit::new(64)?,
    )
    .await?;

let selected = &revisions[3];
let command = WorkflowForkCommand::new(
    workflow_checkpoint_id,
    selected.revision,
    WorkflowForkPolicy::RejectAmbiguous,
);
let fork = store.fork_workflow(tenant, command).await?;
```

Every revision is immutable. The fork receives a new checkpoint and Run
identity, keeps the source workflow version, accumulated usage, and capability
ceiling, and records `WorkflowLineage` back to the exact parent revision.
Completed steps are not replayed. A serial `StepInFlight` revision is rejected
unless the caller explicitly selects `RetryInterruptedStep`; in-flight
parallel and race revisions remain fail-closed. Forked timers and timeouts
restart from branch creation, while signal and human-review waits remain
durably suspended under the new task identity.

Multi-turn Agent context uses a separate `ConversationStore` boundary:

```rust,ignore
let store = InMemoryConversationStore::new();
let conversation_id = ConversationId::new();
let namespace = MemoryNamespace::parse("tenant.user-42")?;
let policy = ConversationContextPolicy::new(
    ConversationWindow::new(16)?,
)
.with_semantic_memory(4)?;

let turn = agent
    .run_conversation(
        "Continue our Rust design discussion",
        &run,
        &store,
        conversation_id,
        namespace,
        policy,
    )
    .await?;
```

The transcript is immutable model-visible conversation data. `Journal`
continues to contain execution facts and is never stored as conversation
history. A `ConversationSummary` is a lossy, monotonically advancing view over
a transcript prefix and never deletes that prefix. `summary_buffer` contains
older unsummarized entries outside the bounded live window; Agents fail closed
with `SummaryRequired` instead of silently dropping them. `SemanticMemory`
requires explicit upsert and immutable transcript provenance, is searchable
across conversations only inside its `MemoryNamespace`, and is injected as
untrusted transient context rather than masquerading as prior dialogue.

Production deployments can persist the same contract in `PostgreSQL`:

```rust,ignore
let store = PostgresConversationStore::connect(
    &database_url,
    "runifold_conversations",
).await?;
store.ensure_schema().await?; // explicit deployment step, never hidden in a turn

let automatic_summary = AutomaticConversationSummary::new(
    ConversationContextPolicy::new(ConversationWindow::new(16)?)
        .with_summary_batch(ConversationSummaryBatch::new(32)?),
    &summary_agent,
)
.with_pass_limit(ConversationSummaryPassLimit::new(8)?);
let turn = agent
    .run_conversation_with_summary(
        "Continue our Rust design discussion",
        &run,
        &store,
        conversation_id,
        namespace,
        automatic_summary,
    )
    .await?;
```

The PostgreSQL adapter uses atomic compare-and-swap transcript commits,
monotonic summary commits, namespace-isolated semantic memory, and explicit
schema setup. `summary_agent` implements `ConversationSummarizer`; because it
runs through the canonical Agent engine with the same `RunContext`, summary
generation remains subject to cancellation, deadlines, budgets, authority,
and journaling. Transcript content is marked as untrusted data in the summary
prompt, and a concurrent transcript append causes an explicit summary CAS
conflict rather than committing a stale summary.

Both the live window and each summary batch are bounded independently.
`summary_backlog` reports how many older entries remain without loading them,
and automatic compaction stops with `SummaryPassLimitExceeded` before the main
model runs when the configured pass limit is insufficient.

PostgreSQL semantic memory can opt into native pgvector search:

```rust,ignore
let store = PostgresConversationStore::connect(
    &database_url,
    "runifold_conversations",
)
.await?
.with_semantic_memory_embedder(Arc::new(embedding_model));
store.ensure_schema().await?;
store
    .ensure_semantic_memory_vector_schema(NonZeroU32::new(1536)?)
    .await?;

let stored = store
    .upsert_memory_scoped(command, RetrievalContext::for_run(&run))
    .await?;
```

Scoped memory writes and searches use `RetrievalDocument` and
`RetrievalQuery` embedding tasks respectively, persist the memory and vector
in one PostgreSQL statement, and return attributable embedding/database
`Usage`. Conversational Agent lookup uses the scoped path automatically, so
embedding tokens, cost, duration, cancellation, and deadlines participate in
the caller's run. Without an embedder the same API retains deterministic
lexical search.

## Design principles

1. Every execution is a `Run`.
2. Every fact is an `Event`.
3. Every external action is an `Effect`.
4. Every permission is an explicit `Capability`.
5. No silent degradation or information loss.
6. Parent and child runs use structured concurrency.
7. Policies are separate from mechanisms.
8. External protocols are adapters, not core types.
9. Testability is a product feature.
10. Stable kernel, replaceable edges.

See [the project charter](docs/CHARTER.md) and
[RFC 0001](docs/rfcs/0001-runtime-kernel.md). Persistence and fault-injection
requirements are documented in the [testing guide](docs/TESTING.md).

## Workspace

| Crate | Purpose |
|---|---|
| `runifold` | Ergonomic public facade |
| `runifold-agent` | Bounded model-tool loop, capability-gated delegation, middleware governance, and canonical transcript |
| `runifold-core` | Run, event, effect, capability, budget, and cancellation primitives |
| `runifold-effect` | Write-ahead effects, idempotency, recovery policy, and durable result replay |
| `runifold-eval-cli` | JSONL evaluation runner, external Candidate protocol, and CI quality gates |
| `runifold-model` | Provider-neutral model requests, content, capabilities, and stream accumulation |
| `runifold-macros` | Attribute macros for typed async Rust Tools |
| `runifold-mcp` | Capability-safe MCP Tools, Resources, Templates, Prompts, Completion, Sampling, stdio, and Streamable HTTP |
| `runifold-observability-otel` | Optional OpenTelemetry GenAI spans and metrics |
| `runifold-providers` | Feature-gated HTTP and SDK-backed model provider adapters |
| `runifold-provider-testkit` | Offline real-HTTP cassettes, protocol assertions, delays, and disconnect injection |
| `runifold-retrieval` | Provider-neutral embeddings, capability-safe retrieval, and a deterministic reference vector index |
| `runifold-retrieval-pgvector` | Explicit PostgreSQL/pgvector persistence and cosine/HNSW retrieval |
| `runifold-retrieval-qdrant` | Qdrant REST upsert and query adapter with stable document identity |
| `runifold-store-postgres` | PostgreSQL conversations, semantic memory, workflow claims, fenced checkpoints, leases, heartbeats, and fencing tokens |
| `runifold-store-sqlite` | Optional durable effects, checkpoints, and journals in one SQLite database |
| `runifold-testkit` | Deterministic runtime helpers, quality datasets, async scorers, and regression gates |
| `runifold-tool` | Tool descriptors, capability gating, registry, and execution |

Planned edge crates include A2A transports and additional persistence backends.

## License

Licensed under either Apache-2.0 or MIT, at your option.