@@ -67,6 +67,12 @@ async fn drop_runtime_kv(state: &AppState) {
6767 db. execute ( "DROP TABLE kv" , vec ! [ ] ) . await . unwrap ( ) ;
6868}
6969
70+ async fn drop_threads_table ( state : & AppState ) {
71+ let pool = do_storage:: DbPool :: new ( std:: path:: Path :: new ( & state. cwd ) . join ( "data" ) . join ( "threadruntime" ) ) ;
72+ let db = pool. object_db ( "threadruntime" , "main" ) . await . unwrap ( ) ;
73+ db. execute ( "DROP TABLE threads" , vec ! [ ] ) . await . unwrap ( ) ;
74+ }
75+
7076/// #202: break the broker's subscriber table so every `topic_publish` fails.
7177///
7278/// The bus rides the SAME runtime isolate as `kv` (`ThreadBus::open_db(rt.db)`),
@@ -2672,7 +2678,8 @@ async fn a_model_switch_persists_and_keeps_history() {
26722678 "message" : { "text" : "first" , "messageId" : "m1" } ,
26732679 } ) ,
26742680 )
2675- . await ;
2681+ . await
2682+ . unwrap ( ) ;
26762683 // The prompt itself is written by `run_turn_with_prompt_id`, not by
26772684 // thread bootstrap, so a test that never runs a turn seeds it the way
26782685 // the runtime would. What is under test here is the SWITCH, and the
@@ -2686,7 +2693,8 @@ async fn a_model_switch_persists_and_keeps_history() {
26862693 "message" : { "text" : "second" , "messageId" : "m2" } ,
26872694 } ) ,
26882695 )
2689- . await ;
2696+ . await
2697+ . unwrap ( ) ;
26902698 seed_prompt ( & state, "t-switch" , "m2" , "second" ) . await ;
26912699
26922700 // the DURABLE thread row carries the new selection
@@ -3275,9 +3283,25 @@ async fn stop_interrupts_the_hearth_foreground_and_cancels_the_turn() {
32753283 // a real long-running foreground command in the SHARED pty — the same
32763284 // one run_bash uses, which is why a stop has to reach it
32773285 let runner = state. terminal . clone ( ) ;
3278- let running = tokio:: spawn ( async move { runner. run ( "sleep 30" , false , Some ( 25 ) , false ) . await } ) ;
3279- // let the command actually reach the shell before interrupting
3280- tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( 400 ) ) . await ;
3286+ let running = tokio:: spawn ( async move {
3287+ runner
3288+ . run ( "printf STOP_TEST_RUNNING; sleep 30" , false , Some ( 25 ) , false )
3289+ . await
3290+ } ) ;
3291+ // Prove the command reached the PTY foreground before interrupting it. A
3292+ // fixed sleep raced the shell on loaded release runs and sometimes tested
3293+ // only that Stop was accepted, not that it interrupted a live foreground.
3294+ let deadline = std:: time:: Instant :: now ( ) + std:: time:: Duration :: from_secs ( 5 ) ;
3295+ loop {
3296+ if state. terminal . read_screen ( ) . await . contains ( "STOP_TEST_RUNNING" ) {
3297+ break ;
3298+ }
3299+ assert ! (
3300+ std:: time:: Instant :: now( ) < deadline,
3301+ "foreground command never became visible before stop"
3302+ ) ;
3303+ tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( 25 ) ) . await ;
3304+ }
32813305
32823306 let started = std:: time:: Instant :: now ( ) ;
32833307 let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
@@ -4656,7 +4680,7 @@ async fn thread_meta_updates_persist_and_reach_the_next_turn() {
46564680 ensure_thread_on_shell ( & state, & json ! ( {
46574681 "threadId" : "t-meta" , "modelSelection" : { "instanceId" : "claudeAgent" , "model" : "claude-haiku-4-5-20251001" } ,
46584682 "message" : { "text" : "hi" , "messageId" : "m1" } ,
4659- } ) ) . await ;
4683+ } ) ) . await . unwrap ( ) ;
46604684
46614685 // change the mode + title OUTSIDE a turn
46624686 let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
@@ -5784,7 +5808,8 @@ async fn thread_metadata_names_the_provider_the_runtime_would_actually_run() {
57845808 "threadId" : "t-default" , "message" : { "text" : "hi" , "messageId" : "m1" } ,
57855809 } ) ,
57865810 )
5787- . await ;
5811+ . await
5812+ . unwrap ( ) ;
57885813
57895814 let t = state
57905815 . rt
@@ -5832,7 +5857,8 @@ async fn thread_metadata_names_the_provider_the_runtime_would_actually_run() {
58325857 "message" : { "text" : "hi" , "messageId" : "m1" } ,
58335858 } ) ,
58345859 )
5835- . await ;
5860+ . await
5861+ . unwrap ( ) ;
58365862 let t = state
58375863 . rt
58385864 . threads ( )
@@ -6113,10 +6139,14 @@ async fn model_switch_persists_in_thread_snapshot() {
61136139 } )
61146140 } ;
61156141 // First turn creates the thread on instance A.
6116- ensure_thread_on_shell ( & state, & cmd ( "claudeAgent" , "model-a" , "hi" ) ) . await ;
6142+ ensure_thread_on_shell ( & state, & cmd ( "claudeAgent" , "model-a" , "hi" ) )
6143+ . await
6144+ . unwrap ( ) ;
61176145 seed_prompt ( & state, tid, "m-a" , "hi" ) . await ;
61186146 // Second turn SWITCHES to instance B on the same thread.
6119- ensure_thread_on_shell ( & state, & cmd ( "codex" , "model-b" , "again" ) ) . await ;
6147+ ensure_thread_on_shell ( & state, & cmd ( "codex" , "model-b" , "again" ) )
6148+ . await
6149+ . unwrap ( ) ;
61206150 seed_prompt ( & state, tid, "m-b" , "again" ) . await ;
61216151
61226152 // The snapshot must reflect the switched selection, and keep both msgs.
@@ -6659,7 +6689,7 @@ async fn the_first_turns_project_worktree_and_mode_reach_the_thread() {
66596689 "worktreePath" : wt. to_string_lossy( ) ,
66606690 } } ,
66616691 } ) ;
6662- ensure_thread_on_shell ( & state, & command) . await ;
6692+ ensure_thread_on_shell ( & state, & command) . await . unwrap ( ) ;
66636693
66646694 // the thread the shell announced carries what the user picked
66656695 let announced = drain_until ( & mut rx, std:: time:: Duration :: from_secs ( 2 ) , |f| {
@@ -6717,6 +6747,166 @@ async fn the_first_turns_project_worktree_and_mode_reach_the_thread() {
67176747 let _ = std:: fs:: remove_dir_all ( & dir) ;
67186748}
67196749
6750+ /// #337: the real dispatch route must read first-turn lifecycle metadata from
6751+ /// `bootstrap.createThread`, not only from top-level fields or an already
6752+ /// persisted row. A draft thread has no durable row yet; rejecting this shape
6753+ /// before `ensure_thread_on_shell` runs strands the composer draft.
6754+ #[ tokio:: test]
6755+ async fn first_turn_launch_reads_runtime_metadata_from_bootstrap_create_thread ( ) {
6756+ let ( state, dir) = test_state ( ) . await ;
6757+ let wt = dir. join ( "wt-dispatch" ) ;
6758+ std:: fs:: create_dir_all ( & wt) . unwrap ( ) ;
6759+
6760+ let ( shell_tx, mut shell_rx) = mpsc:: unbounded_channel ( ) ;
6761+ request ( & state, & shell_tx, "orchestration.subscribeShell" , json ! ( { } ) ) . await ;
6762+ let _ = drain ( & mut shell_rx) ;
6763+
6764+ let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
6765+ request (
6766+ & state,
6767+ & tx,
6768+ "orchestration.dispatchCommand" ,
6769+ json ! ( { "input" : {
6770+ "type" : "thread.turn.start" ,
6771+ "commandId" : "c-bootstrap-mode" ,
6772+ "threadId" : "t-bootstrap-mode" ,
6773+ "message" : {
6774+ "messageId" : "m-bootstrap-mode" ,
6775+ "role" : "user" ,
6776+ "text" : "start with the selected mode" ,
6777+ "attachments" : [ ]
6778+ } ,
6779+ "modelSelection" : {
6780+ "instanceId" : "claudeAgent" ,
6781+ "model" : "claude-haiku-4-5-20251001"
6782+ } ,
6783+ "bootstrap" : { "createThread" : {
6784+ "projectId" : "p-bootstrap" ,
6785+ "title" : "Bootstrap mode" ,
6786+ "runtimeMode" : "approval-required" ,
6787+ "interactionMode" : "plan" ,
6788+ "branch" : "feature/bootstrap-mode" ,
6789+ "worktreePath" : wt. to_string_lossy( ) ,
6790+ } } ,
6791+ } } ) ,
6792+ )
6793+ . await ;
6794+
6795+ let exit = drain ( & mut rx)
6796+ . into_iter ( )
6797+ . find ( |f| f[ "_tag" ] == "Exit" )
6798+ . expect ( "dispatch exits" ) ;
6799+ assert_eq ! (
6800+ exit[ "exit" ] [ "_tag" ] , "Success" ,
6801+ "bootstrap runtimeMode under createThread must pass dispatch admission: {exit}"
6802+ ) ;
6803+
6804+ let saved = state
6805+ . rt
6806+ . threads ( )
6807+ . await
6808+ . into_iter ( )
6809+ . find ( |t| t[ "id" ] == "t-bootstrap-mode" )
6810+ . expect ( "dispatch persisted the draft thread" ) ;
6811+ assert_eq ! ( saved[ "runtimeMode" ] , "approval-required" ) ;
6812+ assert_eq ! ( saved[ "interactionMode" ] , "plan" ) ;
6813+ assert_eq ! ( saved[ "worktreePath" ] , wt. to_string_lossy( ) . as_ref( ) ) ;
6814+ assert_eq ! ( saved[ "branch" ] , "feature/bootstrap-mode" ) ;
6815+
6816+ let announced = drain_until ( & mut shell_rx, std:: time:: Duration :: from_secs ( 2 ) , |f| {
6817+ f. get ( "values" )
6818+ . and_then ( Value :: as_array)
6819+ . is_some_and ( |arr| arr. iter ( ) . any ( |x| x[ "kind" ] == "thread-upserted" ) )
6820+ } )
6821+ . await ;
6822+ let thread = announced
6823+ . iter ( )
6824+ . flat_map ( |f| f[ "values" ] . as_array ( ) . cloned ( ) . unwrap_or_default ( ) )
6825+ . find ( |v| v[ "kind" ] == "thread-upserted" && v[ "thread" ] [ "id" ] == "t-bootstrap-mode" )
6826+ . map ( |v| v[ "thread" ] . clone ( ) )
6827+ . unwrap_or_else ( || panic ! ( "no bootstrap thread-upserted in {announced:#?}" ) ) ;
6828+ assert_eq ! ( thread[ "runtimeMode" ] , "approval-required" ) ;
6829+ assert_eq ! ( thread[ "interactionMode" ] , "plan" ) ;
6830+ }
6831+
6832+ /// #362: a dispatch ACK means the async turn was accepted. For a draft thread
6833+ /// that acceptance is not true until the bootstrap thread row has committed;
6834+ /// otherwise a reload has no thread even though the client saw Success.
6835+ #[ tokio:: test]
6836+ async fn first_turn_launch_refuses_success_ack_when_thread_bootstrap_cannot_persist ( ) {
6837+ let ( state, dir) = test_state ( ) . await ;
6838+ let wt = dir. join ( "wt-dispatch-fail" ) ;
6839+ std:: fs:: create_dir_all ( & wt) . unwrap ( ) ;
6840+
6841+ let ( shell_tx, mut shell_rx) = mpsc:: unbounded_channel ( ) ;
6842+ request ( & state, & shell_tx, "orchestration.subscribeShell" , json ! ( { } ) ) . await ;
6843+ let _ = drain ( & mut shell_rx) ;
6844+ drop_threads_table ( & state) . await ;
6845+
6846+ let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
6847+ request (
6848+ & state,
6849+ & tx,
6850+ "orchestration.dispatchCommand" ,
6851+ json ! ( { "input" : {
6852+ "type" : "thread.turn.start" ,
6853+ "commandId" : "c-bootstrap-fail" ,
6854+ "threadId" : "t-bootstrap-fail" ,
6855+ "message" : {
6856+ "messageId" : "m-bootstrap-fail" ,
6857+ "role" : "user" ,
6858+ "text" : "do not ack this" ,
6859+ "attachments" : [ ]
6860+ } ,
6861+ "modelSelection" : {
6862+ "instanceId" : "claudeAgent" ,
6863+ "model" : "claude-haiku-4-5-20251001"
6864+ } ,
6865+ "bootstrap" : { "createThread" : {
6866+ "projectId" : "p-bootstrap" ,
6867+ "title" : "Bootstrap fail" ,
6868+ "runtimeMode" : "approval-required" ,
6869+ "interactionMode" : "plan" ,
6870+ "branch" : "feature/bootstrap-fail" ,
6871+ "worktreePath" : wt. to_string_lossy( ) ,
6872+ } } ,
6873+ } } ) ,
6874+ )
6875+ . await ;
6876+
6877+ let frames = drain ( & mut rx) ;
6878+ let exit = frames
6879+ . iter ( )
6880+ . find ( |f| f[ "_tag" ] == "Exit" )
6881+ . expect ( "dispatch exits" ) ;
6882+ assert_eq ! (
6883+ exit[ "exit" ] [ "_tag" ] , "Failure" ,
6884+ "bootstrap persistence failure must be visible before the async ACK: {frames:#?}"
6885+ ) ;
6886+ assert ! (
6887+ exit[ "exit" ] [ "cause" ]
6888+ . to_string( )
6889+ . contains( "cannot bootstrap thread" ) ,
6890+ "failure names the durable bootstrap seam: {exit}"
6891+ ) ;
6892+
6893+ let shell_frames = drain ( & mut shell_rx) ;
6894+ assert ! (
6895+ shell_frames. iter( ) . all( |f| {
6896+ f. get( "values" )
6897+ . and_then( Value :: as_array)
6898+ . map( |arr| {
6899+ arr. iter( ) . all( |x| {
6900+ x[ "kind" ] != "thread-upserted"
6901+ || x[ "thread" ] [ "id" ] != "t-bootstrap-fail"
6902+ } )
6903+ } )
6904+ . unwrap_or( true )
6905+ } ) ,
6906+ "a failed bootstrap must not announce a non-durable draft thread: {shell_frames:#?}"
6907+ ) ;
6908+ }
6909+
67206910/// #72: the path picker and open-in-editor are implemented, and their
67216911/// failures are DISTINGUISHABLE.
67226912#[ tokio:: test]
@@ -8302,6 +8492,77 @@ async fn approval_failure_projection_failure_is_not_reported_as_applied() {
83028492 let _ = std:: fs:: remove_dir_all ( & dir) ;
83038493}
83048494
8495+ /// #261: `respond_to_approval` returning `Ok(false)` means no durable pending
8496+ /// approval row was answered. The product must not publish `approval.resolved`
8497+ /// or send the applied success ACK for a stale request id.
8498+ #[ tokio:: test]
8499+ async fn an_approval_that_matched_no_pending_request_fails_the_rpc ( ) {
8500+ let ( state, dir) = test_state ( ) . await ;
8501+ state
8502+ . rt
8503+ . save_thread ( & json ! ( {
8504+ "runtimeMode" : "full-access" ,
8505+ "id" : "t-appr-stale" ,
8506+ "projectId" : "p-workspace" ,
8507+ "title" : "approval stale" ,
8508+ "createdAt" : now_iso( ) ,
8509+ "updatedAt" : now_iso( ) ,
8510+ } ) )
8511+ . await
8512+ . unwrap ( ) ;
8513+ let before = state. rt . current_sequence ( ) . await . unwrap ( ) ;
8514+
8515+ let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
8516+ request (
8517+ & state,
8518+ & tx,
8519+ "orchestration.dispatchCommand" ,
8520+ json ! ( { "input" : {
8521+ "type" : "thread.approval.respond" ,
8522+ "threadId" : "t-appr-stale" ,
8523+ "requestId" : "missing-session|7|missing-call" ,
8524+ "decision" : "accept" ,
8525+ } } ) ,
8526+ )
8527+ . await ;
8528+
8529+ let frames = drain ( & mut rx) ;
8530+ let exit = frames
8531+ . iter ( )
8532+ . find ( |f| f[ "_tag" ] == "Exit" )
8533+ . expect ( "exits" ) ;
8534+ assert_eq ! (
8535+ exit[ "exit" ] [ "_tag" ] , "Failure" ,
8536+ "a stale approval request must not be acknowledged as applied: {frames:#?}"
8537+ ) ;
8538+ assert ! (
8539+ exit[ "exit" ] [ "cause" ] . to_string( ) . contains( "pending request" ) ,
8540+ "failure names the missing durable pending approval: {exit}"
8541+ ) ;
8542+
8543+ let replayed = state
8544+ . rt
8545+ . events_after ( "t-appr-stale" , before, 100 )
8546+ . await
8547+ . unwrap ( ) ;
8548+ assert ! (
8549+ replayed. iter( ) . all( |e| {
8550+ e. pointer( "/event/payload/activity/kind" )
8551+ != Some ( & json!( "approval.resolved" ) )
8552+ } ) ,
8553+ "a stale answer must not clear the pending banner via approval.resolved: {replayed:#?}"
8554+ ) ;
8555+ assert ! (
8556+ replayed. iter( ) . any( |e| {
8557+ e. pointer( "/event/payload/activity/kind" )
8558+ == Some ( & json!( "approval.requested" ) )
8559+ && e. pointer( "/event/payload/activity/tone" ) == Some ( & json!( "error" ) )
8560+ } ) ,
8561+ "the failure mirror should keep the approval visible with an error: {replayed:#?}"
8562+ ) ;
8563+ let _ = std:: fs:: remove_dir_all ( & dir) ;
8564+ }
8565+
83058566/// PROOF (#103): a successfully delivered answer is still not "applied" until
83068567/// the resolved lifecycle activity is durably recorded for replay.
83078568#[ tokio:: test]
0 commit comments