@@ -1390,35 +1390,47 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
13901390 detail : `Proposed plan '${ sourceProposedPlan ?. planId } ' belongs to thread '${ sourceThread . id } ' in a different project.` ,
13911391 } ) ;
13921392 }
1393- const userMessageEvent : Omit < OrchestrationEvent , "sequence" > = {
1394- ...( yield * withEventBase ( {
1395- aggregateKind : "thread" ,
1396- aggregateId : command . threadId ,
1397- occurredAt : command . createdAt ,
1398- commandId : command . commandId ,
1399- } ) ) ,
1400- type : "thread.message-sent" ,
1401- payload : {
1402- threadId : command . threadId ,
1403- messageId : command . message . messageId ,
1404- role : "user" ,
1405- text : command . message . text ,
1406- attachments : command . message . attachments ,
1407- ...( command . message . context !== undefined ? { context : command . message . context } : { } ) ,
1408- turnId : null ,
1409- streaming : false ,
1410- createdAt : command . createdAt ,
1411- updatedAt : command . createdAt ,
1412- } ,
1413- } ;
1393+ // A worktree bootstrap persists the message ahead of the turn with
1394+ // `thread.message.user.append`; the turn then only references it.
1395+ const persistedUserMessage = targetThread . messages . find (
1396+ ( message ) =>
1397+ message . id === command . message . messageId &&
1398+ message . role === "user" &&
1399+ message . turnId === null ,
1400+ ) ;
1401+ const userMessageEvent : Omit < OrchestrationEvent , "sequence" > | null = persistedUserMessage
1402+ ? null
1403+ : {
1404+ ...( yield * withEventBase ( {
1405+ aggregateKind : "thread" ,
1406+ aggregateId : command . threadId ,
1407+ occurredAt : command . createdAt ,
1408+ commandId : command . commandId ,
1409+ } ) ) ,
1410+ type : "thread.message-sent" ,
1411+ payload : {
1412+ threadId : command . threadId ,
1413+ messageId : command . message . messageId ,
1414+ role : "user" ,
1415+ text : command . message . text ,
1416+ attachments : command . message . attachments ,
1417+ ...( command . message . context !== undefined
1418+ ? { context : command . message . context }
1419+ : { } ) ,
1420+ turnId : null ,
1421+ streaming : false ,
1422+ createdAt : command . createdAt ,
1423+ updatedAt : command . createdAt ,
1424+ } ,
1425+ } ;
14141426 const turnStartRequestedEvent : Omit < OrchestrationEvent , "sequence" > = {
14151427 ...( yield * withEventBase ( {
14161428 aggregateKind : "thread" ,
14171429 aggregateId : command . threadId ,
14181430 occurredAt : command . createdAt ,
14191431 commandId : command . commandId ,
14201432 } ) ) ,
1421- causationEventId : userMessageEvent . eventId ,
1433+ ... ( userMessageEvent ? { causationEventId : userMessageEvent . eventId } : { } ) ,
14221434 type : "thread.turn-start-requested" ,
14231435 payload : {
14241436 threadId : command . threadId ,
@@ -1471,7 +1483,53 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
14711483 } ,
14721484 } ) ;
14731485 }
1474- return [ ...lifecycleResetEvents , userMessageEvent , turnStartRequestedEvent ] ;
1486+ return [
1487+ ...lifecycleResetEvents ,
1488+ ...( userMessageEvent ? [ userMessageEvent ] : [ ] ) ,
1489+ turnStartRequestedEvent ,
1490+ ] ;
1491+ }
1492+
1493+ case "thread.message.user.append" : {
1494+ if ( isImportedAgentSessionMessageId ( command . message . messageId ) ) {
1495+ return yield * new OrchestrationCommandInvariantError ( {
1496+ commandType : command . type ,
1497+ detail : `Message id '${ command . message . messageId } ' uses the reserved imported-session namespace.` ,
1498+ } ) ;
1499+ }
1500+ const thread = yield * requireThread ( {
1501+ readModel,
1502+ command,
1503+ threadId : command . threadId ,
1504+ } ) ;
1505+ if ( thread . messages . some ( ( message ) => message . id === command . message . messageId ) ) {
1506+ return yield * new OrchestrationCommandInvariantError ( {
1507+ commandType : command . type ,
1508+ detail : `Message '${ command . message . messageId } ' already exists on thread '${ command . threadId } '.` ,
1509+ } ) ;
1510+ }
1511+ return {
1512+ ...( yield * withEventBase ( {
1513+ aggregateKind : "thread" ,
1514+ aggregateId : command . threadId ,
1515+ occurredAt : command . createdAt ,
1516+ commandId : command . commandId ,
1517+ metadata : { deferredTurn : true } ,
1518+ } ) ) ,
1519+ type : "thread.message-sent" ,
1520+ payload : {
1521+ threadId : command . threadId ,
1522+ messageId : command . message . messageId ,
1523+ role : "user" ,
1524+ text : command . message . text ,
1525+ attachments : command . message . attachments ,
1526+ ...( command . message . context !== undefined ? { context : command . message . context } : { } ) ,
1527+ turnId : null ,
1528+ streaming : false ,
1529+ createdAt : command . createdAt ,
1530+ updatedAt : command . createdAt ,
1531+ } ,
1532+ } ;
14751533 }
14761534
14771535 case "thread.turn.interrupt" : {
0 commit comments