Skip to content
TypeScript

fix(server): an old link card no longer keeps a thread asking for input (#28491)

George Nijo625b9ac5c586
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index 6c3106d6356..63e144b5039 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -1419,12 +1419,24 @@ function secretRequestAsPendingInput(
/**
* A secret card is answered while its agent waits in the run. A link card's
- * agent asked without waiting, so the card waits on the user after its run.
+ * agent asked without waiting, so the card waits on the user after its run,
+ * until the user writes in the thread again and so has moved past it.
*/
-const waitsOnUser = (item: OrchestrationV2TurnItem, liveRunIds: ReadonlySet<RunId>) =>
- item.type === "link_request" ||
+const waitsOnUser = (
+ item: OrchestrationV2TurnItem,
+ liveRunIds: ReadonlySet<RunId>,
+ latestUserAuthoredMessageAt: DateTime.Utc | null,
+) =>
+ (item.type === "link_request" && linkCardStillAsks(item, latestUserAuthoredMessageAt)) ||
(item.type === "secret_request" && item.runId !== null && liveRunIds.has(item.runId));
+const linkCardStillAsks = (
+ item: OrchestrationV2TurnItem,
+ latestUserAuthoredMessageAt: DateTime.Utc | null,
+) =>
+ latestUserAuthoredMessageAt === null ||
+ !DateTime.isGreaterThan(latestUserAuthoredMessageAt, item.startedAt ?? item.updatedAt);
+
export function threadShellFromProjection(
projection: OrchestrationV2ThreadProjection,
): OrchestrationV2ThreadShell {
@@ -1460,23 +1472,29 @@ export function threadShellFromProjection(
DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
)[0] ?? null,
);
+ const userMessages = projection.messages
+ .filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
+ .toSorted(
+ (left, right) =>
+ DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
+ );
+ const latestUserAuthoredMessageAt =
+ userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null;
if (pendingRuntimeRequest === null || pendingRuntimeRequest.blocking === false) {
pendingRuntimeRequest =
secretRequestAsPendingInput(
projection.turnItems
- .filter((item) => item.status === "waiting" && waitsOnUser(item, liveRunIds))
+ .filter(
+ (item) =>
+ item.status === "waiting" &&
+ waitsOnUser(item, liveRunIds, latestUserAuthoredMessageAt),
+ )
.toSorted(
(left, right) =>
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
)[0] ?? null,
) ?? pendingRuntimeRequest;
}
- const userMessages = projection.messages
- .filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
- .toSorted(
- (left, right) =>
- DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
- );
const latestUserMessage = userMessages[0] ?? null;
const pendingBackgroundTasks = derivePendingBackgroundWork({
latestRun,
@@ -1545,8 +1563,7 @@ export function threadShellFromProjection(
// initial hydration and streaming updates independent of transcript size.
latestVisibleMessage: null,
latestUserMessageAt: latestUserMessage?.updatedAt ?? null,
- latestUserAuthoredMessageAt:
- userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null,
+ latestUserAuthoredMessageAt,
hasActionableProposedPlan: projection.plans.some(
(plan) => plan.kind === "proposed_plan" && plan.status === "active",
),
@@ -6026,6 +6043,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
WHERE message.thread_id = t.thread_id
AND message.role = 'user'
AND json_extract(message.payload_json, '$.createdBy') = 'user'
+ AND NOT ${isIdleCompactionMessage("message")}
ORDER BY message.updated_at DESC, message.message_id DESC
LIMIT 1
) AS latest_user_authored_message_at,
@@ -6280,6 +6298,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
FROM orchestration_v2_projection_messages message
WHERE message.thread_id = t.thread_id AND message.role = 'user'
AND json_extract(message.payload_json, '$.createdBy') = 'user'
+ AND NOT ${isIdleCompactionMessage("message")}
ORDER BY message.updated_at DESC, message.message_id DESC
LIMIT 1
) AS latest_user_authored_message_at
@@ -6475,6 +6494,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
pendingTurnItemsByThreadId,
} = input;
const thread = yield* decodeThreadPayload(row.payload_json);
+ const latestUserAuthoredMessageAt =
+ row.latest_user_authored_message_at === null
+ ? null
+ : DateTime.makeUnsafe(row.latest_user_authored_message_at);
const waitingCards = [
row.pending_secret_request_payload_json === null
? null
@@ -6482,7 +6505,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
row.pending_link_request_payload_json === null
? null
: yield* decodeTurnItemPayload(row.pending_link_request_payload_json),
- ].filter((item) => item !== null);
+ ]
+ .filter((item) => item !== null)
+ .filter(
+ (item) =>
+ item.type !== "link_request" || linkCardStillAsks(item, latestUserAuthoredMessageAt),
+ );
let pendingRuntimeRequest =
row.pending_request_payload_json !== null
? pendingRuntimeRequestSummary(
@@ -6601,10 +6629,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
row.latest_user_message_at === null
? null
: DateTime.makeUnsafe(row.latest_user_message_at),
- latestUserAuthoredMessageAt:
- row.latest_user_authored_message_at === null
- ? null
- : DateTime.makeUnsafe(row.latest_user_authored_message_at),
+ latestUserAuthoredMessageAt,
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
pendingBackgroundTasks,
providerInstanceHistory: providerInstanceHistoryForShell({
diff --git a/apps/server/src/peer/PeerLinkRequests.test.ts b/apps/server/src/peer/PeerLinkRequests.test.ts
index f59202bb140..ed45167a353 100644
--- a/apps/server/src/peer/PeerLinkRequests.test.ts
+++ b/apps/server/src/peer/PeerLinkRequests.test.ts
@@ -495,6 +495,41 @@ it.live("the agent asks without waiting, and the user links the box after its tu
).toMatchObject({ kind: "user_input" });
expect(yield* a.projections.getSettlementCandidates(threadId)).toEqual([]);
+ // Once the user writes in the thread again, the card stops asking but stays answerable.
+ const wroteAt = DateTime.add(card.startedAt!, { seconds: 1 });
+ yield* a.sink.write({
+ commandId: CommandId.make("command:user-writes:laptop"),
+ events: [
+ {
+ id: EventId.make("event:user-writes"),
+ type: "message.updated",
+ threadId,
+ occurredAt: wroteAt,
+ payload: {
+ createdBy: "user",
+ creationSource: "web",
+ id: MessageId.make("message:user-writes"),
+ threadId,
+ runId: null,
+ nodeId: null,
+ role: "user",
+ text: "Something else",
+ attachments: [],
+ streaming: false,
+ createdAt: wroteAt,
+ updatedAt: wroteAt,
+ },
+ },
+ ],
+ });
+ expect((yield* a.orchestrator.getThreadShell(threadId))?.pendingRuntimeRequest).toBeNull();
+ expect(
+ ProjectionStore.threadShellFromProjection(
+ yield* a.orchestrator.getThreadProjection(threadId),
+ ).pendingRuntimeRequest,
+ ).toBeNull();
+ expect(yield* cardOf(a)).toMatchObject({ status: "waiting", linkStatus: "pending" });
+
// The user's client mints a code on the box and picks more than the agent suggested.
const pairing = yield* b.auth.issuePairingCredential();
yield* a.requests.answer({