newt-core 0.8.4

Newt-Agent core types, errors, and the NeMoCode-style tier router
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
use super::*;

/// The A3/W6 attach-inject inbox: enqueue is FIFO and idempotent, dequeue is
/// exactly-once and non-blocking on empty, and both are workspace-fenced —
/// the properties the interactive-attach seam rests on (D2: the web writes
/// only the inbox; the REPL alone writes turns).
#[test]
fn inbox_inject_take_is_exactly_once_fifo_idempotent_and_fenced() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("session", None).unwrap();

    // Empty inbox → non-blocking None (the REPL poll never stalls).
    assert_eq!(store.take_injected_prompt(&conv).unwrap(), None);

    // Enqueue two; dequeue FIFO, exactly-once.
    assert_eq!(
        store.inject_prompt(&conv, "first", None).unwrap(),
        InjectOutcome::Enqueued
    );
    assert_eq!(
        store.inject_prompt(&conv, "second", None).unwrap(),
        InjectOutcome::Enqueued
    );
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap().unwrap().body,
        "first"
    );
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap().unwrap().body,
        "second"
    );
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap(),
        None,
        "drained exactly once"
    );

    // Idempotency: the same idem_key is a no-op, not a second enqueue.
    assert_eq!(
        store.inject_prompt(&conv, "again", Some("k1")).unwrap(),
        InjectOutcome::Enqueued
    );
    assert_eq!(
        store.inject_prompt(&conv, "again", Some("k1")).unwrap(),
        InjectOutcome::Duplicate
    );
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap().unwrap().body,
        "again"
    );
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap(),
        None,
        "the idem duplicate did not enqueue twice"
    );

    // link_inbox_delivery records the receipt back-link without error.
    store.inject_prompt(&conv, "linked", None).unwrap();
    let taken = store.take_injected_prompt(&conv).unwrap().unwrap();
    store.link_inbox_delivery(&taken.id, "receipt-123").unwrap();

    // Workspace fence: a store on ANOTHER workspace can neither inject into
    // nor take from this conversation.
    let ws_b = tempfile::tempdir().unwrap();
    let store_b = ConversationStore::new(root.path(), ws_b.path(), 100).unwrap();
    assert!(
        store_b.inject_prompt(&conv, "cross-ws", None).is_err(),
        "cross-workspace inject is rejected"
    );
    store.inject_prompt(&conv, "mine", None).unwrap();
    assert_eq!(
        store_b.take_injected_prompt(&conv).unwrap(),
        None,
        "cross-workspace take sees nothing"
    );
    assert!(
        store.take_injected_prompt(&conv).unwrap().is_some(),
        "the owning workspace still dequeues it"
    );
}

/// Expiry drops an offer from the pending set and synthesizes NOTHING —
/// retargeted from `permission_request_expires_on_created_tick_ttl`.
#[test]
fn an_expired_offer_is_not_pending() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let mut store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("s", None).unwrap();
    store.claim_clock = || 1_000;
    let (definition, _instance) = test_offer(&store, &conv);
    let id = store
        .publish_interaction_offer(
            &conv,
            &definition,
            crate::interaction_offer::OfferDanger::High,
            &[newt_interaction::Audience::Web],
        )
        .unwrap();
    assert!(store.pending_interaction_offer(&conv).unwrap().is_some());

    // One tick inside the window is still answerable...
    store.claim_clock = || 999 + ConversationStore::PERMISSION_REQUEST_TTL_NANOS;
    assert!(store.pending_interaction_offer(&conv).unwrap().is_some());
    // ...and AT the deadline it is gone, without anything having
    // answered. The boundary is inherited deliberately from the row
    // this replaces: `published_tick > now - TTL`, so an offer expires
    // exactly at `published + TTL`.
    store.claim_clock = || 1_000 + ConversationStore::PERMISSION_REQUEST_TTL_NANOS;
    assert!(store.pending_interaction_offer(&conv).unwrap().is_none());
    assert_eq!(store.take_interaction_decision(&conv, &id).unwrap(), None);
}

/// A minimal published offer, minted under `store`'s own workspace
/// fence so the fence check is exercised rather than sidestepped.
fn test_offer(
    store: &ConversationStore,
    conv: &str,
) -> (
    newt_interaction::InteractionDefinition,
    newt_interaction::InteractionInstance,
) {
    let question = crate::Question::<crate::PermissionAction> {
        markdown: "\u{2298} run_command wants to run `bash`".to_string(),
        actions: vec![
            crate::Action::new(crate::PermissionAction::AllowOnce, "a", "allow once"),
            crate::Action::new(crate::PermissionAction::Deny, "d", "deny (default)"),
        ],
        note: None,
    };
    let definition = crate::interaction_adapter::question_to_definition(&question).unwrap();
    let (instance, _) = crate::interaction_gate::mint_offer(
        &definition,
        store.workspace_fence(),
        conv,
        &[newt_interaction::Audience::Web],
        // The store's OWN clock, so the offer's TTL window is the one
        // the store measures against rather than a fixed literal that
        // is already ancient by wall time.
        store.claim_tick(),
    )
    .unwrap();
    (definition, instance)
}

/// The offer transport's core contract (B0b-2, #1846), retargeted from
/// `permission_channel_publish_answer_take_race_and_fence`: publish,
/// read back, workspace fence, answer exactly once, and a local cancel
/// that arrives after an answer loses to it.
#[test]
fn interaction_offer_publish_answer_take_race_and_fence() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let other_ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("s", None).unwrap();
    let (definition, _instance) = test_offer(&store, &conv);

    let id = store
        .publish_interaction_offer(
            &conv,
            &definition,
            crate::interaction_offer::OfferDanger::Low,
            &[newt_interaction::Audience::Web],
        )
        .unwrap();
    let pending = store.pending_interaction_offer(&conv).unwrap().unwrap();
    assert_eq!(pending.instance_id, id);
    assert_eq!(pending.danger, crate::interaction_offer::OfferDanger::Low);

    // Another workspace cannot see it.
    let foreign = ConversationStore::new(root.path(), other_ws.path(), 100).unwrap();
    assert!(foreign.pending_interaction_offer(&conv).unwrap().is_none());

    // Answering wins once; a second answer loses and the first stands.
    assert_eq!(
        store
            .answer_interaction_offer(
                &conv,
                &id,
                crate::PermissionAction::AllowOnce,
                newt_interaction::Audience::Web
            )
            .unwrap(),
        AnswerOutcome::Answered
    );
    assert_eq!(
        store
            .answer_interaction_offer(
                &conv,
                &id,
                crate::PermissionAction::Deny,
                newt_interaction::Audience::Web
            )
            .unwrap(),
        AnswerOutcome::AlreadyResolved
    );
    assert_eq!(
        store.take_interaction_decision(&conv, &id).unwrap(),
        Some(crate::PermissionAction::AllowOnce)
    );
    // ...and it is no longer pending.
    assert!(store.pending_interaction_offer(&conv).unwrap().is_none());
    // A local cancel after an answer LOSES: the answer stands.
    assert!(!store.cancel_interaction_offer(&conv, &id).unwrap());
}

/// A0 freeze (#1823), retargeted (B0b-2, #1846). The old lenient read
/// consumed an unparseable VERDICT string as `Ok(None)`; the new decode
/// path is an unparseable OPTION in the stored Response. The guarantee
/// is the same and deliberately kept: an answer this build cannot read
/// is not an answer, and is never an error or an authorization.
#[test]
fn an_unknown_persisted_option_reads_as_none_not_error() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("s", None).unwrap();
    let (definition, _instance) = test_offer(&store, &conv);
    let id = store
        .publish_interaction_offer(
            &conv,
            &definition,
            crate::interaction_offer::OfferDanger::Low,
            &[newt_interaction::Audience::Web],
        )
        .unwrap();
    store
        .answer_interaction_offer(
            &conv,
            &id,
            crate::PermissionAction::AllowOnce,
            newt_interaction::Audience::Web,
        )
        .unwrap();

    // Rewrite the stored body to name an option this build does not
    // know — the shape an older or forked writer could leave behind.
    store
            .lock_conn()
            .execute(
                "UPDATE interaction_offers SET response_json = replace(response_json, 'allow_once', 'allow_twice') WHERE instance_id = ?1",
                [&id],
            )
            .unwrap();

    // LENIENT, deliberately, and unchanged from the verdict path this
    // replaces: an unreadable answer reads as "no answer", never as an
    // error and never as an authorization. Tightening it (erroring, or
    // treating it as a deny) is a change a later slice must list.
    assert_eq!(store.take_interaction_decision(&conv, &id).unwrap(), None);
    // ...and the audit fact survives regardless: the row still records
    // WHICH surface answered.
    assert_eq!(
        store.interaction_answered_by(&conv, &id).unwrap(),
        Some(newt_interaction::Audience::Web)
    );
}

fn flock_task(event: &str, body: &str) -> NewFlockTask {
    NewFlockTask {
        body: body.to_owned(),
        flock_cid: "flock-1".into(),
        event_cid: event.to_owned(),
        evidence_cid: format!("evidence-of-{event}"),
        evidence: format!("{{\"event\":\"{event}\"}}"),
    }
}

/// A flock task is deduped by (flock, event) across restarts, even once it is
/// delivered or rejected, and the web's dequeue never sees it.
#[test]
fn flock_tasks_dedupe_across_restarts_and_stay_out_of_the_web_queue() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("session", None).unwrap();
    assert_eq!(
        store
            .enqueue_flock_task(&conv, &flock_task("e1", "do e1"))
            .unwrap(),
        InjectOutcome::Enqueued
    );
    assert_eq!(
        store
            .enqueue_flock_task(&conv, &flock_task("e1", "do e1"))
            .unwrap(),
        InjectOutcome::Duplicate
    );
    store.inject_prompt(&conv, "from the web", None).unwrap();
    // each queue sees only its own rows
    assert_eq!(
        store.take_injected_prompt(&conv).unwrap().unwrap().body,
        "from the web"
    );
    assert_eq!(store.take_injected_prompt(&conv).unwrap(), None);
    let first = store.peek_flock_task(&conv).unwrap().unwrap();
    assert_eq!(first.task.body, "do e1");
    // peeking leaves it queued
    assert_eq!(store.peek_flock_task(&conv).unwrap().unwrap().id, first.id);

    // a restart: a fresh store on the same database; replaying the same
    // event is a duplicate whether it is queued, delivered or rejected
    drop(store);
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    assert_eq!(
        store
            .enqueue_flock_task(&conv, &flock_task("e1", "do e1"))
            .unwrap(),
        InjectOutcome::Duplicate
    );
    store.reject_flock_task(&first).unwrap();
    assert_eq!(
        store.peek_flock_task(&conv).unwrap(),
        None,
        "rejected is never offered"
    );
    assert_eq!(
        store
            .enqueue_flock_task(&conv, &flock_task("e1", "do e1"))
            .unwrap(),
        InjectOutcome::Duplicate
    );
    // another event of the same flock, and the same event of another flock, are new
    assert_eq!(
        store
            .enqueue_flock_task(&conv, &flock_task("e2", "do e2"))
            .unwrap(),
        InjectOutcome::Enqueued
    );
    let mut other = flock_task("e1", "other flock");
    other.flock_cid = "flock-2".into();
    assert_eq!(
        store.enqueue_flock_task(&conv, &other).unwrap(),
        InjectOutcome::Enqueued
    );
    // FIFO
    let next = store.peek_flock_task(&conv).unwrap().unwrap();
    assert_eq!(next.task.event_cid, "e2");
    assert!(next.seq > first.seq);
}

#[test]
fn a_flock_task_is_workspace_fenced() {
    let root = tempfile::tempdir().unwrap();
    let (ws_a, ws_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
    let a = ConversationStore::new(root.path(), ws_a.path(), 100).unwrap();
    let b = ConversationStore::new(root.path(), ws_b.path(), 100).unwrap();
    let conv = a.create("session", None).unwrap();
    assert!(b.enqueue_flock_task(&conv, &flock_task("e1", "x")).is_err());
    a.enqueue_flock_task(&conv, &flock_task("e1", "x")).unwrap();
    assert_eq!(b.peek_flock_task(&conv).unwrap(), None);
}

/// Accepting mints a `flock`-origin receipt (never `operator`) and marks the
/// row delivered with that receipt's id in one transaction.
#[test]
fn accepting_a_flock_task_mints_a_flock_receipt_and_marks_delivery_together() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("session", None).unwrap();
    store
        .enqueue_flock_task(&conv, &flock_task("e1", "the brief"))
        .unwrap();
    let task = store.peek_flock_task(&conv).unwrap().unwrap();

    let ctx = store
        .accept_flock_task(&conv, "session", None, &task)
        .unwrap();
    let receipt = store
        .load_prompt(ctx.submitted_prompt().id())
        .unwrap()
        .unwrap();
    assert_eq!(receipt.origin(), PromptOrigin::Flock);
    assert_eq!(receipt.raw_text(), b"the brief");
    assert_eq!(
        receipt.parent_prompt_id(),
        None,
        "it roots its own objective"
    );
    assert_eq!(receipt.root_prompt_id(), receipt.id());
    assert_eq!(
        store.latest_prompt(&conv).unwrap().unwrap().id(),
        receipt.id()
    );
    // delivered, with the back-link
    assert_eq!(store.peek_flock_task(&conv).unwrap(), None);
    let conn = store.lock_conn();
    let (delivered, link): (i64, Option<String>) = conn
        .query_row(
            "SELECT delivered, delivered_receipt_id FROM conversation_inbox WHERE id = ?1",
            [&task.id],
            |r| Ok((r.get(0)?, r.get(1)?)),
        )
        .unwrap();
    assert_eq!(delivered, 1);
    assert_eq!(link.as_deref(), Some(receipt.id().to_string().as_str()));
    drop(conn);

    // a second accept of the same row mints nothing more
    assert!(store
        .accept_flock_task(&conv, "session", None, &task)
        .is_err());
    let receipts: i64 = store
        .lock_conn()
        .query_row("SELECT COUNT(*) FROM prompt_receipts", [], |r| r.get(0))
        .unwrap();
    assert_eq!(receipts, 1, "a refused accept left a receipt behind");
}

/// The dequeue -> receipt crash window: when the receipt cannot be minted the
/// row is still queued (nothing was marked), and when the row cannot be
/// marked the receipt is rolled back. Neither half survives alone.
#[test]
fn a_failed_accept_leaves_no_receipt_and_the_task_still_queued() {
    let root = tempfile::tempdir().unwrap();
    let (ws_a, ws_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
    let store = ConversationStore::new(root.path(), ws_a.path(), 100).unwrap();
    let other = ConversationStore::new(root.path(), ws_b.path(), 100).unwrap();
    let conv = store.create("session", None).unwrap();
    store
        .enqueue_flock_task(&conv, &flock_task("e1", "the brief"))
        .unwrap();
    let task = store.peek_flock_task(&conv).unwrap().unwrap();

    // receipt side fails (another workspace's fence): the row stays queued
    assert!(other
        .accept_flock_task(&conv, "session", None, &task)
        .is_err());
    assert_eq!(store.peek_flock_task(&conv).unwrap().unwrap().id, task.id);
    assert_eq!(store.latest_prompt(&conv).unwrap(), None);

    // mark side fails (the row was rejected meanwhile): the receipt rolls back
    store.reject_flock_task(&task).unwrap();
    assert!(store
        .accept_flock_task(&conv, "session", None, &task)
        .is_err());
    assert_eq!(
        store.latest_prompt(&conv).unwrap(),
        None,
        "a receipt survived the failed delivery mark"
    );

    // and a task that was peeked but never accepted (the process died) is
    // offered again to the next store
    store
        .enqueue_flock_task(&conv, &flock_task("e2", "second"))
        .unwrap();
    let peeked = store.peek_flock_task(&conv).unwrap().unwrap();
    drop(store);
    let again = ConversationStore::new(root.path(), ws_a.path(), 100).unwrap();
    assert_eq!(again.peek_flock_task(&conv).unwrap().unwrap().id, peeked.id);
}

/// A database made before the `flock` origin existed (its CHECK refuses it)
/// is rebuilt on open with every receipt intact, and then takes flock tasks.
#[test]
fn an_older_database_is_migrated_to_allow_the_flock_origin() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let (conv, old_id) = {
        let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
        let conv = store.create("session", None).unwrap();
        let ctx = store
            .begin_prompt(&conv, "session", None, NewPrompt::operator("old", "old"))
            .unwrap();
        (conv, ctx.submitted_prompt().id())
    };
    // put the table back as an older binary made it: the narrow CHECK
    {
        let db = rusqlite::Connection::open(root.path().join(DB_FILE)).unwrap();
        let sql: String = db
            .query_row(
                "SELECT sql FROM sqlite_master WHERE name = 'prompt_receipts'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert!(sql.contains("'flock'"), "the fresh schema allows flock");
        db.execute_batch("PRAGMA foreign_keys = OFF; PRAGMA writable_schema = ON;")
            .unwrap();
        db.execute(
            "UPDATE sqlite_master SET sql = replace(sql, ', ''flock''', '') WHERE name = 'prompt_receipts'",
            [],
        )
        .unwrap();
        db.execute_batch("PRAGMA writable_schema = OFF;").unwrap();
    }
    {
        let db = rusqlite::Connection::open(root.path().join(DB_FILE)).unwrap();
        let sql: String = db
            .query_row(
                "SELECT sql FROM sqlite_master WHERE name = 'prompt_receipts'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert!(
            !sql.contains("'flock'"),
            "the fixture is the older table: {sql}"
        );
    }
    // reopening migrates it
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let old = store
        .load_prompt(old_id)
        .unwrap()
        .expect("the old receipt survived");
    assert_eq!(old.raw_text(), b"old");
    assert_eq!(old.origin(), PromptOrigin::Operator);
    store
        .enqueue_flock_task(&conv, &flock_task("e1", "new"))
        .unwrap();
    let task = store.peek_flock_task(&conv).unwrap().unwrap();
    let ctx = store
        .accept_flock_task(&conv, "session", None, &task)
        .unwrap();
    let new = store
        .load_prompt(ctx.submitted_prompt().id())
        .unwrap()
        .unwrap();
    assert_eq!(new.origin(), PromptOrigin::Flock);
    // chronology is intact across the rebuild
    assert_eq!(
        new.previous_prompt_id(),
        Some(old_id),
        "the rebuilt table kept receipt order"
    );
    let violations: i64 = store
        .lock_conn()
        .query_row("SELECT COUNT(*) FROM pragma_foreign_key_check", [], |r| {
            r.get(0)
        })
        .unwrap();
    assert_eq!(violations, 0);
}

/// Review 2976 r1 finding 1: a self-rooted flock receipt must be readable by
/// every lineage reader: context rebuild (resume), an operator continuation,
/// a harness retry (durable and ephemeral) and an artifact write.
#[test]
fn a_flock_rooted_objective_can_be_resumed_continued_retried_and_annotated() {
    let root = tempfile::tempdir().unwrap();
    let ws = tempfile::tempdir().unwrap();
    let store = ConversationStore::new(root.path(), ws.path(), 100).unwrap();
    let conv = store.create("session", None).unwrap();
    store
        .enqueue_flock_task(&conv, &flock_task("e1", "the brief"))
        .unwrap();
    let task = store.peek_flock_task(&conv).unwrap().unwrap();
    let ctx = store
        .accept_flock_task(&conv, "session", None, &task)
        .unwrap();
    let id = ctx.submitted_prompt().id();

    // resume: the context rebuilds, and the flock receipt is its own authority
    let again = store.turn_prompt_context(&conv, id).unwrap().unwrap();
    assert_eq!(again.submitted_prompt().id(), id);
    assert_eq!(again.active().id(), id);
    assert_eq!(again.active().receipt().origin(), PromptOrigin::Flock);

    // an operator continuation and a harness retry attach to it
    let cont = store
        .begin_prompt(
            &conv,
            "session",
            None,
            NewPrompt::operator_continuation("go on", "go on", id),
        )
        .unwrap();
    assert_eq!(cont.submitted_prompt().receipt().root_prompt_id(), id);
    let retry = store
        .begin_prompt(
            &conv,
            "session",
            None,
            NewPrompt::harness_retry("retry", "retry", id),
        )
        .unwrap();
    assert_eq!(retry.active().id(), id, "authority stays the task");

    // an ephemeral retry from the rebuilt context
    let eph =
        crate::TurnPromptContext::ephemeral_harness_retry(conv.clone(), "r", "r", &again).unwrap();
    assert_eq!(eph.active().id(), id);

    // an artifact can be written against it
    store
        .append_prompt_artifact(
            &conv,
            id,
            crate::NewPromptArtifact::new(
                crate::ArtifactKind::Decision,
                crate::ArtifactRelation::DerivedFrom,
            ),
        )
        .unwrap();
}

/// Review 2976 CI: concurrent openers of one fresh database race the additive
/// column migration (`duplicate column name: ...`). Every opener must
/// succeed, over many rounds, each released together by a barrier.
#[test]
fn concurrent_openers_of_a_fresh_database_all_succeed() {
    for _ in 0..40 {
        let root = tempfile::tempdir().unwrap();
        let ws = tempfile::tempdir().unwrap();
        let gate = std::sync::Arc::new(std::sync::Barrier::new(8));
        let handles: Vec<_> = (0..8)
            .map(|_| {
                let (root, ws, gate) = (
                    root.path().to_path_buf(),
                    ws.path().to_path_buf(),
                    std::sync::Arc::clone(&gate),
                );
                std::thread::spawn(move || {
                    gate.wait();
                    ConversationStore::new(&root, &ws, 100).map(drop)
                })
            })
            .collect();
        for h in handles {
            h.join().unwrap().unwrap();
        }
    }
}