@@ -161,10 +161,7 @@ export type ProjectionThreadPullRequests = Pick<
161161
162162/**
163163 * Thread activity needed by settlement, without transcript or fork history.
164- * `latestUserAuthoredMessageAt` is the last message the user wrote. Agent,
165- * provider, and server notifications also use the user role, so
166- * `latestUserMessageAt` moves when background work or a PR watch wakes the
167- * agent.
164+ * Settlement always loads `latestUserAuthoredMessageAt`, so it is required here.
168165 */
169166export type ProjectionSettlementCandidate = Pick <
170167 OrchestrationV2ThreadShell ,
@@ -917,6 +914,7 @@ type ShellThreadRow = {
917914 readonly blocking_failure_payload_json : string | null ;
918915 readonly pending_request_payload_json : string | null ;
919916 readonly latest_user_message_at : string | null ;
917+ readonly latest_user_authored_message_at : string | null ;
920918 readonly has_actionable_proposed_plan : number ;
921919 readonly item_count : number ;
922920 readonly runless_item_count : number ;
@@ -938,7 +936,8 @@ type SettlementThreadRow = Pick<
938936 | "latest_run_started_at"
939937 | "latest_run_completed_at"
940938 | "latest_user_message_at"
941- > & { readonly latest_user_authored_message_at : string | null } ;
939+ | "latest_user_authored_message_at"
940+ > ;
942941
943942type ShellRunItemCountRow = {
944943 readonly thread_id : string ;
@@ -1329,13 +1328,13 @@ export function threadShellFromProjection(
13291328 ( left , right ) =>
13301329 DateTime . toEpochMillis ( right . createdAt ) - DateTime . toEpochMillis ( left . createdAt ) ,
13311330 ) [ 0 ] ?? null ;
1332- const latestUserMessage =
1333- projection . messages
1334- . filter ( ( message ) => message . role === "user" )
1335- . toSorted (
1336- ( left , right ) =>
1337- DateTime . toEpochMillis ( right . updatedAt ) - DateTime . toEpochMillis ( left . updatedAt ) ,
1338- ) [ 0 ] ?? null ;
1331+ const userMessages = projection . messages
1332+ . filter ( ( message ) => message . role === "user" )
1333+ . toSorted (
1334+ ( left , right ) =>
1335+ DateTime . toEpochMillis ( right . updatedAt ) - DateTime . toEpochMillis ( left . updatedAt ) ,
1336+ ) ;
1337+ const latestUserMessage = userMessages [ 0 ] ?? null ;
13391338 const pendingBackgroundTasks = derivePendingBackgroundWork ( {
13401339 latestRun,
13411340 providerThreads : projection . providerThreads ,
@@ -1396,6 +1395,8 @@ export function threadShellFromProjection(
13961395 // initial hydration and streaming updates independent of transcript size.
13971396 latestVisibleMessage : null ,
13981397 latestUserMessageAt : latestUserMessage ?. updatedAt ?? null ,
1398+ latestUserAuthoredMessageAt :
1399+ userMessages . find ( ( message ) => message . createdBy === "user" ) ?. updatedAt ?? null ,
13991400 hasActionableProposedPlan : projection . plans . some (
14001401 ( plan ) => plan . kind === "proposed_plan" && plan . status === "active" ,
14011402 ) ,
@@ -1480,6 +1481,7 @@ type ShellThreadState = {
14801481 readonly usageLimitResetAt : OrchestrationV2ThreadShell [ "usageLimitResetAt" ] ;
14811482 readonly pendingRuntimeRequest : OrchestrationV2ThreadProjection [ "runtimeRequests" ] [ number ] | null ;
14821483 readonly latestUserMessageAt : DateTime . Utc | null ;
1484+ readonly latestUserAuthoredMessageAt : DateTime . Utc | null ;
14831485 readonly hasActionableProposedPlan : boolean ;
14841486 readonly pendingBackgroundTasks : OrchestrationV2ThreadShell [ "pendingBackgroundTasks" ] ;
14851487 readonly providerInstanceHistory : OrchestrationV2ThreadShell [ "providerInstanceHistory" ] ;
@@ -1625,6 +1627,7 @@ function shellFromState(input: {
16251627 } ,
16261628 latestVisibleMessage : null ,
16271629 latestUserMessageAt : input . state . latestUserMessageAt ,
1630+ latestUserAuthoredMessageAt : input . state . latestUserAuthoredMessageAt ,
16281631 hasActionableProposedPlan : input . state . hasActionableProposedPlan ,
16291632 pendingBackgroundTasks : input . state . pendingBackgroundTasks ,
16301633 providerInstanceHistory : input . state . providerInstanceHistory ,
@@ -4887,6 +4890,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
48874890 ORDER BY message.updated_at DESC, message.message_id DESC
48884891 LIMIT 1
48894892 ) AS latest_user_message_at,
4893+ (
4894+ SELECT message.updated_at
4895+ FROM orchestration_v2_projection_messages message
4896+ WHERE message.thread_id = t.thread_id
4897+ AND message.role = 'user'
4898+ AND json_extract(message.payload_json, '$.createdBy') = 'user'
4899+ ORDER BY message.updated_at DESC, message.message_id DESC
4900+ LIMIT 1
4901+ ) AS latest_user_authored_message_at,
48904902 EXISTS (
48914903 SELECT 1
48924904 FROM orchestration_v2_projection_plans plan
@@ -5325,6 +5337,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53255337 row . latest_user_message_at === null
53265338 ? null
53275339 : DateTime . makeUnsafe ( row . latest_user_message_at ) ,
5340+ latestUserAuthoredMessageAt :
5341+ row . latest_user_authored_message_at === null
5342+ ? null
5343+ : DateTime . makeUnsafe ( row . latest_user_authored_message_at ) ,
53285344 hasActionableProposedPlan : row . has_actionable_proposed_plan === 1 ,
53295345 pendingBackgroundTasks,
53305346 providerInstanceHistory : providerInstanceHistoryForShell ( {
@@ -5630,16 +5646,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
56305646 ! runs . some ( isActivityRunForShell ) &&
56315647 ! runtimeRequests . some ( ( request ) => request . status === "pending" ) ,
56325648 )
5633- . map ( ( projection ) => ( {
5634- ...threadShellFromProjection ( projection ) ,
5635- latestUserAuthoredMessageAt :
5636- projection . messages
5637- . filter ( ( message ) => message . role === "user" && message . createdBy === "user" )
5638- . map ( ( message ) => message . updatedAt )
5639- . toSorted (
5640- ( left , right ) => DateTime . toEpochMillis ( right ) - DateTime . toEpochMillis ( left ) ,
5641- ) [ 0 ] ?? null ,
5642- } ) )
5649+ . map ( ( projection ) => {
5650+ const shell = threadShellFromProjection ( projection ) ;
5651+ return {
5652+ ...shell ,
5653+ latestUserAuthoredMessageAt : shell . latestUserAuthoredMessageAt ?? null ,
5654+ } ;
5655+ } )
56435656 . toSorted (
56445657 ( left , right ) =>
56455658 DateTime . toEpochMillis ( left . updatedAt ) - DateTime . toEpochMillis ( right . updatedAt ) ||
0 commit comments