Skip to content

Commit 31f5c5a

Browse files
georgenijoclaude
andcommitted
fix(server): an old link card no longer keeps a thread asking for input
A link card an agent asked for kept the thread's status on "Input" until the user answered it, even after they had moved on and chatted for many more turns. The card now stops flagging the thread once the user writes in it again, and stays answerable in the timeline. The SQL shell path also ignores idle compactions as user-authored messages, matching the in-memory path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2c59bf6 commit 31f5c5a

2 files changed

Lines changed: 77 additions & 17 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,12 +1418,24 @@ function secretRequestAsPendingInput(
14181418

14191419
/**
14201420
* A secret card is answered while its agent waits in the run. A link card's
1421-
* agent asked without waiting, so the card waits on the user after its run.
1421+
* agent asked without waiting, so the card waits on the user after its run,
1422+
* until the user writes in the thread again and so has moved past it.
14221423
*/
1423-
const waitsOnUser = (item: OrchestrationV2TurnItem, liveRunIds: ReadonlySet<RunId>) =>
1424-
item.type === "link_request" ||
1424+
const waitsOnUser = (
1425+
item: OrchestrationV2TurnItem,
1426+
liveRunIds: ReadonlySet<RunId>,
1427+
latestUserAuthoredMessageAt: DateTime.Utc | null,
1428+
) =>
1429+
(item.type === "link_request" && linkCardStillAsks(item, latestUserAuthoredMessageAt)) ||
14251430
(item.type === "secret_request" && item.runId !== null && liveRunIds.has(item.runId));
14261431

1432+
const linkCardStillAsks = (
1433+
item: OrchestrationV2TurnItem,
1434+
latestUserAuthoredMessageAt: DateTime.Utc | null,
1435+
) =>
1436+
latestUserAuthoredMessageAt === null ||
1437+
!DateTime.isGreaterThan(latestUserAuthoredMessageAt, item.startedAt ?? item.updatedAt);
1438+
14271439
export function threadShellFromProjection(
14281440
projection: OrchestrationV2ThreadProjection,
14291441
): OrchestrationV2ThreadShell {
@@ -1459,23 +1471,29 @@ export function threadShellFromProjection(
14591471
DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
14601472
)[0] ?? null,
14611473
);
1474+
const userMessages = projection.messages
1475+
.filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
1476+
.toSorted(
1477+
(left, right) =>
1478+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1479+
);
1480+
const latestUserAuthoredMessageAt =
1481+
userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null;
14621482
if (pendingRuntimeRequest === null || pendingRuntimeRequest.blocking === false) {
14631483
pendingRuntimeRequest =
14641484
secretRequestAsPendingInput(
14651485
projection.turnItems
1466-
.filter((item) => item.status === "waiting" && waitsOnUser(item, liveRunIds))
1486+
.filter(
1487+
(item) =>
1488+
item.status === "waiting" &&
1489+
waitsOnUser(item, liveRunIds, latestUserAuthoredMessageAt),
1490+
)
14671491
.toSorted(
14681492
(left, right) =>
14691493
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
14701494
)[0] ?? null,
14711495
) ?? pendingRuntimeRequest;
14721496
}
1473-
const userMessages = projection.messages
1474-
.filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
1475-
.toSorted(
1476-
(left, right) =>
1477-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1478-
);
14791497
const latestUserMessage = userMessages[0] ?? null;
14801498
const pendingBackgroundTasks = derivePendingBackgroundWork({
14811499
latestRun,
@@ -1544,8 +1562,7 @@ export function threadShellFromProjection(
15441562
// initial hydration and streaming updates independent of transcript size.
15451563
latestVisibleMessage: null,
15461564
latestUserMessageAt: latestUserMessage?.updatedAt ?? null,
1547-
latestUserAuthoredMessageAt:
1548-
userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null,
1565+
latestUserAuthoredMessageAt,
15491566
hasActionableProposedPlan: projection.plans.some(
15501567
(plan) => plan.kind === "proposed_plan" && plan.status === "active",
15511568
),
@@ -6007,6 +6024,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60076024
WHERE message.thread_id = t.thread_id
60086025
AND message.role = 'user'
60096026
AND json_extract(message.payload_json, '$.createdBy') = 'user'
6027+
AND NOT ${isIdleCompactionMessage("message")}
60106028
ORDER BY message.updated_at DESC, message.message_id DESC
60116029
LIMIT 1
60126030
) AS latest_user_authored_message_at,
@@ -6261,6 +6279,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62616279
FROM orchestration_v2_projection_messages message
62626280
WHERE message.thread_id = t.thread_id AND message.role = 'user'
62636281
AND json_extract(message.payload_json, '$.createdBy') = 'user'
6282+
AND NOT ${isIdleCompactionMessage("message")}
62646283
ORDER BY message.updated_at DESC, message.message_id DESC
62656284
LIMIT 1
62666285
) AS latest_user_authored_message_at
@@ -6456,14 +6475,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64566475
pendingTurnItemsByThreadId,
64576476
} = input;
64586477
const thread = yield* decodeThreadPayload(row.payload_json);
6478+
const latestUserAuthoredMessageAt =
6479+
row.latest_user_authored_message_at === null
6480+
? null
6481+
: DateTime.makeUnsafe(row.latest_user_authored_message_at);
64596482
const waitingCards = [
64606483
row.pending_secret_request_payload_json === null
64616484
? null
64626485
: yield* decodeTurnItemPayload(row.pending_secret_request_payload_json),
64636486
row.pending_link_request_payload_json === null
64646487
? null
64656488
: yield* decodeTurnItemPayload(row.pending_link_request_payload_json),
6466-
].filter((item) => item !== null);
6489+
]
6490+
.filter((item) => item !== null)
6491+
.filter(
6492+
(item) =>
6493+
item.type !== "link_request" || linkCardStillAsks(item, latestUserAuthoredMessageAt),
6494+
);
64676495
let pendingRuntimeRequest =
64686496
row.pending_request_payload_json !== null
64696497
? pendingRuntimeRequestSummary(
@@ -6582,10 +6610,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
65826610
row.latest_user_message_at === null
65836611
? null
65846612
: DateTime.makeUnsafe(row.latest_user_message_at),
6585-
latestUserAuthoredMessageAt:
6586-
row.latest_user_authored_message_at === null
6587-
? null
6588-
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
6613+
latestUserAuthoredMessageAt,
65896614
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
65906615
pendingBackgroundTasks,
65916616
providerInstanceHistory: providerInstanceHistoryForShell({

‎apps/server/src/peer/PeerLinkRequests.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,41 @@ it.live("the agent asks without waiting, and the user links the box after its tu
495495
).toMatchObject({ kind: "user_input" });
496496
expect(yield* a.projections.getSettlementCandidates(threadId)).toEqual([]);
497497

498+
// Once the user writes in the thread again, the card stops asking but stays answerable.
499+
const wroteAt = DateTime.add(card.startedAt!, { seconds: 1 });
500+
yield* a.sink.write({
501+
commandId: CommandId.make("command:user-writes:laptop"),
502+
events: [
503+
{
504+
id: EventId.make("event:user-writes"),
505+
type: "message.updated",
506+
threadId,
507+
occurredAt: wroteAt,
508+
payload: {
509+
createdBy: "user",
510+
creationSource: "web",
511+
id: MessageId.make("message:user-writes"),
512+
threadId,
513+
runId: null,
514+
nodeId: null,
515+
role: "user",
516+
text: "Something else",
517+
attachments: [],
518+
streaming: false,
519+
createdAt: wroteAt,
520+
updatedAt: wroteAt,
521+
},
522+
},
523+
],
524+
});
525+
expect((yield* a.orchestrator.getThreadShell(threadId))?.pendingRuntimeRequest).toBeNull();
526+
expect(
527+
ProjectionStore.threadShellFromProjection(
528+
yield* a.orchestrator.getThreadProjection(threadId),
529+
).pendingRuntimeRequest,
530+
).toBeNull();
531+
expect(yield* cardOf(a)).toMatchObject({ status: "waiting", linkStatus: "pending" });
532+
498533
// The user's client mints a code on the box and picks more than the agent suggested.
499534
const pairing = yield* b.auth.issuePairingCredential();
500535
yield* a.requests.answer({

0 commit comments

Comments
 (0)