Skip to content

Commit c5e6ecd

Browse files
committed
fix(threads): preserve snooze wake signals for input cards
1 parent 04e8e1f commit c5e6ecd

4 files changed

Lines changed: 128 additions & 16 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8588,6 +8588,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
85888588
...(command.delivery === undefined ? {} : { delivery: command.delivery }),
85898589
secretStatus: command.secretStatus,
85908590
};
8591+
if (
8592+
!unchanged &&
8593+
command.secretStatus === "saved" &&
8594+
projection.thread.snoozedUntil != null
8595+
) {
8596+
yield* emit(
8597+
events,
8598+
command,
8599+
)({
8600+
type: "thread.unsnoozed",
8601+
threadId: command.threadId,
8602+
providerInstanceId: projection.thread.providerInstanceId,
8603+
occurredAt: now,
8604+
payload: { ...projection.thread, snoozedUntil: null, snoozedAt: null, updatedAt: now },
8605+
});
8606+
}
85918607
yield* emit(
85928608
events,
85938609
command,
@@ -8714,6 +8730,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
87148730
updatedAt: now,
87158731
type: "link_request",
87168732
};
8733+
if (!unchanged && command.linkStatus === "linked" && projection.thread.snoozedUntil != null) {
8734+
yield* emit(
8735+
events,
8736+
command,
8737+
)({
8738+
type: "thread.unsnoozed",
8739+
threadId: command.threadId,
8740+
providerInstanceId: projection.thread.providerInstanceId,
8741+
occurredAt: now,
8742+
payload: { ...projection.thread, snoozedUntil: null, snoozedAt: null, updatedAt: now },
8743+
});
8744+
}
87178745
yield* emit(
87188746
events,
87198747
command,

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5290,6 +5290,41 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
52905290
},
52915291
});
52925292

5293+
for (const fresh of [true, false]) {
5294+
yield* projectionStore.apply({
5295+
id: EventId.make("event:" + suffix + ":fresh-" + fresh),
5296+
type: "runtime-request.updated",
5297+
threadId,
5298+
runId,
5299+
nodeId,
5300+
driver,
5301+
occurredAt: now,
5302+
payload: {
5303+
id: requestId,
5304+
nodeId,
5305+
providerTurnId: null,
5306+
nativeRequestRef: null,
5307+
kind: "user_input",
5308+
status: "pending",
5309+
responseCapability: { type: "message" },
5310+
createdAt: fresh
5311+
? DateTime.add(now, { seconds: 5 })
5312+
: DateTime.subtract(now, { seconds: 1 }),
5313+
resolvedAt: null,
5314+
},
5315+
});
5316+
for (const shell of [
5317+
(yield* projectionStore.getShellSnapshot()).threads.find(
5318+
(thread) => thread.id === threadId,
5319+
),
5320+
ProjectionStore.threadShellFromProjection(
5321+
yield* projectionStore.getThreadProjection(threadId),
5322+
),
5323+
]) {
5324+
assert.equal(shell?.pendingRuntimeRequest?.id, fresh ? requestId : "secret:b");
5325+
}
5326+
}
5327+
52935328
for (const status of [
52945329
"preparing",
52955330
"starting",

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,15 +1462,22 @@ export function threadShellFromProjection(
14621462
)[0] ?? null,
14631463
);
14641464
if (pendingRuntimeRequest === null || pendingRuntimeRequest.blocking === false) {
1465-
pendingRuntimeRequest =
1466-
secretRequestAsPendingInput(
1467-
projection.turnItems
1468-
.filter((item) => item.status === "waiting" && waitsOnUser(item, liveRunIds))
1469-
.toSorted(
1470-
(left, right) =>
1471-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1472-
)[0] ?? null,
1473-
) ?? pendingRuntimeRequest;
1465+
const waitingCard = secretRequestAsPendingInput(
1466+
projection.turnItems
1467+
.filter((item) => item.status === "waiting" && waitsOnUser(item, liveRunIds))
1468+
.toSorted(
1469+
(left, right) =>
1470+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1471+
)[0] ?? null,
1472+
);
1473+
if (
1474+
waitingCard !== null &&
1475+
(pendingRuntimeRequest === null ||
1476+
DateTime.toEpochMillis(waitingCard.createdAt) >=
1477+
DateTime.toEpochMillis(pendingRuntimeRequest.createdAt))
1478+
) {
1479+
pendingRuntimeRequest = waitingCard;
1480+
}
14741481
}
14751482
const userMessages = projection.messages
14761483
.filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
@@ -6493,13 +6500,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64936500
(pendingRuntimeRequest === null || pendingRuntimeRequest.blocking === false) &&
64946501
waitingCards.length > 0
64956502
) {
6496-
pendingRuntimeRequest =
6497-
secretRequestAsPendingInput(
6498-
waitingCards.toSorted(
6499-
(left, right) =>
6500-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
6501-
)[0] ?? null,
6502-
) ?? pendingRuntimeRequest;
6503+
const waitingCard = secretRequestAsPendingInput(
6504+
waitingCards.toSorted(
6505+
(left, right) =>
6506+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
6507+
)[0] ?? null,
6508+
);
6509+
if (
6510+
waitingCard !== null &&
6511+
(pendingRuntimeRequest === null ||
6512+
DateTime.toEpochMillis(waitingCard.createdAt) >=
6513+
DateTime.toEpochMillis(pendingRuntimeRequest.createdAt))
6514+
) {
6515+
pendingRuntimeRequest = waitingCard;
6516+
}
65036517
}
65046518
let terminalFailureItem =
65056519
row.terminal_failure_payload_json === null

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,31 @@ it.live("the agent asks without waiting, and the user links the box after its tu
482482
{ kind: "user_input" },
483483
);
484484

485+
for (const secretStatus of ["pending", "saved"] as const) {
486+
yield* a.orchestrator.dispatch({
487+
type: "secret_request.record",
488+
commandId: CommandId.make("command:secret-" + secretStatus),
489+
threadId,
490+
runId,
491+
nodeId: rootNode,
492+
turnItemId: TurnItemId.make("turn-item:snoozed-secret"),
493+
label: "Credential",
494+
reason: "Connect",
495+
secretStatus,
496+
});
497+
if (secretStatus === "pending") {
498+
yield* a.orchestrator.dispatch({
499+
type: "thread.snooze",
500+
commandId: CommandId.make("command:snooze-secret"),
501+
threadId,
502+
snoozedUntil: "2099-01-01T00:00:00.000Z",
503+
});
504+
expect((yield* a.projections.getThread(threadId)).snoozedUntil).not.toBeNull();
505+
} else {
506+
expect((yield* a.projections.getThread(threadId)).snoozedUntil).toBeNull();
507+
}
508+
}
509+
485510
// The agent ends its turn; the card stays answerable, and the thread still asks.
486511
yield* endRun(a);
487512
expect(yield* cardOf(a)).toMatchObject({ status: "waiting", linkStatus: "pending" });
@@ -495,6 +520,14 @@ it.live("the agent asks without waiting, and the user links the box after its tu
495520
).toMatchObject({ kind: "user_input" });
496521
expect(yield* a.projections.getSettlementCandidates(threadId)).toEqual([]);
497522

523+
yield* a.orchestrator.dispatch({
524+
type: "thread.snooze",
525+
commandId: CommandId.make("command:snooze-link"),
526+
threadId,
527+
snoozedUntil: "2099-01-01T00:00:00.000Z",
528+
});
529+
expect((yield* a.projections.getThread(threadId)).snoozedUntil).not.toBeNull();
530+
498531
// The user's client mints a code on the box and picks more than the agent suggested.
499532
const pairing = yield* b.auth.issuePairingCredential();
500533
yield* a.requests.answer({
@@ -503,6 +536,8 @@ it.live("the agent asks without waiting, and the user links the box after its tu
503536
answer: { type: "link", urls: [b.url], access: "auto", pairingCode: pairing.credential },
504537
});
505538

539+
expect((yield* a.projections.getThread(threadId)).snoozedUntil).toBeNull();
540+
506541
const link = yield* a.links.get(box.environmentId);
507542
expect(Option.getOrUndefined(link)?.access).toBe("auto");
508543
expect(yield* cardOf(a)).toMatchObject({

0 commit comments

Comments
 (0)