@@ -15,12 +15,14 @@ import {
1515 ApprovalRequestId ,
1616 CommandId ,
1717 DEFAULT_PROVIDER_INTERACTION_MODE ,
18+ EnvironmentId ,
1819 EventId ,
1920 MessageId ,
2021 ProjectId ,
2122 ThreadId ,
2223 TurnId ,
2324} from "@t3tools/contracts" ;
25+ import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations" ;
2426import * as Effect from "effect/Effect" ;
2527import * as Deferred from "effect/Deferred" ;
2628import * as Exit from "effect/Exit" ;
@@ -69,6 +71,19 @@ const asApprovalRequestId = (value: string): ApprovalRequestId => ApprovalReques
6971const asMessageId = ( value : string ) : MessageId => MessageId . make ( value ) ;
7072const asTurnId = ( value : string ) : TurnId => TurnId . make ( value ) ;
7173
74+ const assistantQuoteText = "Retain the reconnect backoff." ;
75+ const assistantCitation = {
76+ version : 1 as const ,
77+ environmentId : EnvironmentId . make ( "source-environment" ) ,
78+ threadId : ThreadId . make ( "source-thread" ) ,
79+ messageId : asMessageId ( "source-message" ) ,
80+ text : assistantQuoteText ,
81+ start : 0 ,
82+ end : assistantQuoteText . length ,
83+ prefix : "" ,
84+ suffix : "" ,
85+ } ;
86+
7287const deriveServerPathsSync = ( baseDir : string , devUrl : URL | undefined ) =>
7388 Effect . runSync ( deriveServerPaths ( baseDir , devUrl ) . pipe ( Effect . provide ( NodeServices . layer ) ) ) ;
7489
@@ -824,7 +839,13 @@ describe("ProviderCommandReactor", () => {
824839 it ( "pins the first user message when regeneration context is truncated" , async ( ) => {
825840 const harness = await createHarness ( ) ;
826841 const now = "2026-01-01T00:00:00.000Z" ;
827- const firstUserMessage = `Review subagent monitoring risks. ${ "Opening context. " . repeat ( 200 ) } ` ;
842+ const quoteText = "界" . repeat ( 1_000 ) ;
843+ const citation = serializeAssistantCitation ( {
844+ ...assistantCitation ,
845+ text : quoteText ,
846+ end : quoteText . length ,
847+ } ) ;
848+ const firstUserMessage = `Review subagent monitoring risks. ${ citation } ${ "Opening context. " . repeat ( 200 ) } ` ;
828849 const recentUserMessage = `LATEST FINDING: ${ "implementation detail " . repeat ( 320 ) } ` ;
829850 harness . generateThreadTitle . mockReturnValue (
830851 Effect . succeed ( { title : "Review subagent monitoring risks" } ) ,
@@ -927,7 +948,8 @@ describe("ProviderCommandReactor", () => {
927948 throw new Error ( "Expected a title generation input" ) ;
928949 }
929950 const message = input . message ;
930- expect ( message . startsWith ( "USER:\nReview subagent monitoring risks." ) ) . toBe ( true ) ;
951+ expect ( message . startsWith ( `USER:\nReview subagent monitoring risks. ${ quoteText } ` ) ) . toBe ( true ) ;
952+ expect ( message ) . not . toContain ( "t3-citation://" ) ;
931953 expect ( message ) . toContain ( "[First user message truncated]" ) ;
932954 expect ( message ) . toContain ( "[Earlier content truncated]" ) ;
933955 expect ( message ) . toContain ( "image.png" ) ;
@@ -936,6 +958,14 @@ describe("ProviderCommandReactor", () => {
936958 "opening-context-image" ,
937959 "recent-context-image" ,
938960 ] ) ;
961+ const readModel = await harness . readModel ( ) ;
962+ expect (
963+ readModel . threads
964+ . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) )
965+ ?. messages . find (
966+ ( entry ) => entry . id === asMessageId ( "user-message-before-long-title-regeneration" ) ,
967+ ) ?. text ,
968+ ) . toBe ( firstUserMessage ) ;
939969 } ) ;
940970
941971 it ( "clears title regeneration state left pending across reactor startup" , async ( ) => {
@@ -1439,13 +1469,14 @@ describe("ProviderCommandReactor", () => {
14391469 const harness = await createHarness ( ) ;
14401470 const now = "2026-01-01T00:00:00.000Z" ;
14411471 const seededTitle = "Fix reconnect spinner on resume" ;
1472+ const prompt = `[effort:high]\\n\\nFix reconnect spinner on resume ${ serializeAssistantCitation ( assistantCitation ) } ` ;
14421473 harness . generateThreadTitle . mockReturnValue (
14431474 Effect . succeed ( {
14441475 title : "Reconnect spinner resume bug" ,
14451476 } ) ,
14461477 ) ;
14471478
1448- await Effect . runPromise (
1479+ await harness . runEffect (
14491480 harness . engine . dispatch ( {
14501481 type : "thread.meta.update" ,
14511482 commandId : CommandId . make ( "cmd-thread-title-formatted-seed" ) ,
@@ -1454,15 +1485,27 @@ describe("ProviderCommandReactor", () => {
14541485 } ) ,
14551486 ) ;
14561487
1457- await Effect . runPromise (
1488+ const titleUpdated = await harness . runEffect (
1489+ harness . engine . streamDomainEvents . pipe (
1490+ Stream . filter (
1491+ ( event ) =>
1492+ event . type === "thread.meta-updated" &&
1493+ event . payload . title === "Reconnect spinner resume bug" ,
1494+ ) ,
1495+ Stream . take ( 1 ) ,
1496+ Stream . toPull ,
1497+ Scope . provide ( scope ! ) ,
1498+ ) ,
1499+ ) ;
1500+ await harness . runEffect (
14581501 harness . engine . dispatch ( {
14591502 type : "thread.turn.start" ,
14601503 commandId : CommandId . make ( "cmd-turn-start-title-formatted" ) ,
14611504 threadId : ThreadId . make ( "thread-1" ) ,
14621505 message : {
14631506 messageId : asMessageId ( "user-message-title-formatted" ) ,
14641507 role : "user" ,
1465- text : "[effort:high]\\n\\nFix reconnect spinner on resume" ,
1508+ text : prompt ,
14661509 attachments : [ ] ,
14671510 } ,
14681511 titleSeed : seededTitle ,
@@ -1472,25 +1515,34 @@ describe("ProviderCommandReactor", () => {
14721515 } ) ,
14731516 ) ;
14741517
1475- await waitFor ( ( ) => harness . generateThreadTitle . mock . calls . length === 1 ) ;
1476- await waitFor ( async ( ) => {
1477- const readModel = await harness . readModel ( ) ;
1478- return (
1479- readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ?. title ===
1480- "Reconnect spinner resume bug"
1481- ) ;
1482- } ) ;
1518+ await harness . runEffect ( titleUpdated ) ;
1519+ await harness . drain ( ) ;
14831520
1521+ expect ( harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] . message ) . toBe (
1522+ `[effort:high]\\n\\nFix reconnect spinner on resume ${ assistantQuoteText } ` ,
1523+ ) ;
1524+ expect ( harness . generateThreadTitle . mock . calls [ 0 ] ?. [ 0 ] . message ) . not . toContain ( "t3-citation://" ) ;
14841525 const readModel = await harness . readModel ( ) ;
14851526 const thread = readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ;
14861527 expect ( thread ?. title ) . toBe ( "Reconnect spinner resume bug" ) ;
1528+ expect (
1529+ thread ?. messages . find ( ( entry ) => entry . id === asMessageId ( "user-message-title-formatted" ) )
1530+ ?. text ,
1531+ ) . toBe ( prompt ) ;
1532+ expect ( harness . sendTurn . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchObject ( { input : prompt } ) ;
14871533 } ) ;
14881534
14891535 it ( "generates a worktree branch name for the first turn" , async ( ) => {
14901536 const harness = await createHarness ( ) ;
14911537 const now = "2026-01-01T00:00:00.000Z" ;
1538+ const prompt = `Add a safer reconnect backoff. ${ serializeAssistantCitation ( assistantCitation ) } ` ;
1539+ const statusRefreshed = await harness . runEffect ( Deferred . make < void > ( ) ) ;
1540+ const refreshStatus = harness . refreshStatus . getMockImplementation ( ) ! ;
1541+ harness . refreshStatus . mockImplementation ( ( cwd ) =>
1542+ refreshStatus ( cwd ) . pipe ( Effect . tap ( ( ) => Deferred . succeed ( statusRefreshed , undefined ) ) ) ,
1543+ ) ;
14921544
1493- await Effect . runPromise (
1545+ await harness . runEffect (
14941546 harness . engine . dispatch ( {
14951547 type : "thread.meta.update" ,
14961548 commandId : CommandId . make ( "cmd-thread-branch" ) ,
@@ -1515,15 +1567,15 @@ describe("ProviderCommandReactor", () => {
15151567 } ) ,
15161568 ) ;
15171569
1518- await Effect . runPromise (
1570+ await harness . runEffect (
15191571 harness . engine . dispatch ( {
15201572 type : "thread.turn.start" ,
15211573 commandId : CommandId . make ( "cmd-turn-start-branch-model" ) ,
15221574 threadId : ThreadId . make ( "thread-1" ) ,
15231575 message : {
15241576 messageId : asMessageId ( "user-message-branch-model" ) ,
15251577 role : "user" ,
1526- text : "Add a safer reconnect backoff." ,
1578+ text : prompt ,
15271579 attachments : [ ] ,
15281580 } ,
15291581 interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
@@ -1532,12 +1584,19 @@ describe("ProviderCommandReactor", () => {
15321584 } ) ,
15331585 ) ;
15341586
1535- await waitFor ( ( ) => harness . generateBranchName . mock . calls . length === 1 ) ;
1536- await waitFor ( ( ) => harness . refreshStatus . mock . calls . length === 1 ) ;
1537- expect ( harness . generateBranchName . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchObject ( {
1538- message : "Add a safer reconnect backoff." ,
1539- } ) ;
1587+ await harness . runEffect ( Deferred . await ( statusRefreshed ) ) ;
1588+ await harness . drain ( ) ;
1589+ expect ( harness . generateBranchName . mock . calls [ 0 ] ?. [ 0 ] . message ) . toBe (
1590+ `Add a safer reconnect backoff. ${ assistantQuoteText } ` ,
1591+ ) ;
1592+ expect ( harness . generateBranchName . mock . calls [ 0 ] ?. [ 0 ] . message ) . not . toContain ( "t3-citation://" ) ;
15401593 expect ( harness . refreshStatus . mock . calls [ 0 ] ?. [ 0 ] ) . toBe ( "/tmp/provider-project-worktree" ) ;
1594+ const readModel = await harness . readModel ( ) ;
1595+ expect (
1596+ readModel . threads
1597+ . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) )
1598+ ?. messages . find ( ( entry ) => entry . id === asMessageId ( "user-message-branch-model" ) ) ?. text ,
1599+ ) . toBe ( prompt ) ;
15411600 } ) ;
15421601
15431602 it ( "recreates a missing worktree from the thread branch before starting a turn" , async ( ) => {
0 commit comments