Skip to content

Commit 625b9ac

Browse files
georgenijoclaude
andauthored
fix(server): an old link card no longer keeps a thread asking for input (#28491)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8195530 commit 625b9ac

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
@@ -1419,12 +1419,24 @@ function secretRequestAsPendingInput(
14191419

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

1433+
const linkCardStillAsks = (
1434+
item: OrchestrationV2TurnItem,
1435+
latestUserAuthoredMessageAt: DateTime.Utc | null,
1436+
) =>
1437+
latestUserAuthoredMessageAt === null ||
1438+
!DateTime.isGreaterThan(latestUserAuthoredMessageAt, item.startedAt ?? item.updatedAt);
1439+
14281440
export function threadShellFromProjection(
14291441
projection: OrchestrationV2ThreadProjection,
14301442
): OrchestrationV2ThreadShell {
@@ -1460,23 +1472,29 @@ export function threadShellFromProjection(
14601472
DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
14611473
)[0] ?? null,
14621474
);
1475+
const userMessages = projection.messages
1476+
.filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
1477+
.toSorted(
1478+
(left, right) =>
1479+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1480+
);
1481+
const latestUserAuthoredMessageAt =
1482+
userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null;
14631483
if (pendingRuntimeRequest === null || pendingRuntimeRequest.blocking === false) {
14641484
pendingRuntimeRequest =
14651485
secretRequestAsPendingInput(
14661486
projection.turnItems
1467-
.filter((item) => item.status === "waiting" && waitsOnUser(item, liveRunIds))
1487+
.filter(
1488+
(item) =>
1489+
item.status === "waiting" &&
1490+
waitsOnUser(item, liveRunIds, latestUserAuthoredMessageAt),
1491+
)
14681492
.toSorted(
14691493
(left, right) =>
14701494
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
14711495
)[0] ?? null,
14721496
) ?? pendingRuntimeRequest;
14731497
}
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-
);
14801498
const latestUserMessage = userMessages[0] ?? null;
14811499
const pendingBackgroundTasks = derivePendingBackgroundWork({
14821500
latestRun,
@@ -1545,8 +1563,7 @@ export function threadShellFromProjection(
15451563
// initial hydration and streaming updates independent of transcript size.
15461564
latestVisibleMessage: null,
15471565
latestUserMessageAt: latestUserMessage?.updatedAt ?? null,
1548-
latestUserAuthoredMessageAt:
1549-
userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null,
1566+
latestUserAuthoredMessageAt,
15501567
hasActionableProposedPlan: projection.plans.some(
15511568
(plan) => plan.kind === "proposed_plan" && plan.status === "active",
15521569
),
@@ -6026,6 +6043,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60266043
WHERE message.thread_id = t.thread_id
60276044
AND message.role = 'user'
60286045
AND json_extract(message.payload_json, '$.createdBy') = 'user'
6046+
AND NOT ${isIdleCompactionMessage("message")}
60296047
ORDER BY message.updated_at DESC, message.message_id DESC
60306048
LIMIT 1
60316049
) AS latest_user_authored_message_at,
@@ -6280,6 +6298,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62806298
FROM orchestration_v2_projection_messages message
62816299
WHERE message.thread_id = t.thread_id AND message.role = 'user'
62826300
AND json_extract(message.payload_json, '$.createdBy') = 'user'
6301+
AND NOT ${isIdleCompactionMessage("message")}
62836302
ORDER BY message.updated_at DESC, message.message_id DESC
62846303
LIMIT 1
62856304
) AS latest_user_authored_message_at
@@ -6475,14 +6494,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64756494
pendingTurnItemsByThreadId,
64766495
} = input;
64776496
const thread = yield* decodeThreadPayload(row.payload_json);
6497+
const latestUserAuthoredMessageAt =
6498+
row.latest_user_authored_message_at === null
6499+
? null
6500+
: DateTime.makeUnsafe(row.latest_user_authored_message_at);
64786501
const waitingCards = [
64796502
row.pending_secret_request_payload_json === null
64806503
? null
64816504
: yield* decodeTurnItemPayload(row.pending_secret_request_payload_json),
64826505
row.pending_link_request_payload_json === null
64836506
? null
64846507
: yield* decodeTurnItemPayload(row.pending_link_request_payload_json),
6485-
].filter((item) => item !== null);
6508+
]
6509+
.filter((item) => item !== null)
6510+
.filter(
6511+
(item) =>
6512+
item.type !== "link_request" || linkCardStillAsks(item, latestUserAuthoredMessageAt),
6513+
);
64866514
let pendingRuntimeRequest =
64876515
row.pending_request_payload_json !== null
64886516
? pendingRuntimeRequestSummary(
@@ -6601,10 +6629,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66016629
row.latest_user_message_at === null
66026630
? null
66036631
: DateTime.makeUnsafe(row.latest_user_message_at),
6604-
latestUserAuthoredMessageAt:
6605-
row.latest_user_authored_message_at === null
6606-
? null
6607-
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
6632+
latestUserAuthoredMessageAt,
66086633
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
66096634
pendingBackgroundTasks,
66106635
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)