Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,14 @@ from the turn's own credential (`seq`, the lane's turn count when it was
minted), only for a check-in or a turn nobody waits on (`unaskedTurn`),
and only while nothing of the turn has reached the person: no text
previewed or saved, no card, file or open question, none of their words
in it or on their way in (`SteerHolds`). A refusal is a 409 with a
in it or on their way in (`SteerHolds`). A question an earlier turn left
open is not this turn's (`heldByRequest`, by the lane turn it was asked
in): it stays open, on its card and waiting, and a turn finished quietly
past it gives back the conversation's dot it took on starting, unless the
person has opened it since. An approval, a question asked since the work
began, one nobody knows the turn of, or any question in a retry, the
backup or a pickup (whose work may be older than the conversation's
latest turn) still refuses. A refusal is a 409 with a
reason, and tells the agent to answer substantively. Once accepted
(`QuietGate` in `server/quiet-finish.ts`), what the turn says is saved
`quiet` and not previewed; a card, a file, a team proposal or a question
Expand Down
46 changes: 41 additions & 5 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { ENGINE_PACKAGES, engineUpdates } from "./engine-updates.ts";
import { FEATURES } from "./features.ts";
import { ReplyNotifications, settledFrame, settledReply } from "./reply-notifications.ts";
import { TextPreview } from "./text-preview.ts";
import { belongs, QUIET_ACCEPTED, quietKind, quietRefused, SteerHolds, writer, type QuietGate, type QuietRefusal } from "./quiet-finish.ts";
import { belongs, heldByRequest, QUIET_ACCEPTED, quietKind, quietRefused, SteerHolds, writer, type OpenAsk, type QuietGate, type QuietRefusal } from "./quiet-finish.ts";
import { LaneTurns, buildTrace, emptyTrace, readTurn, turnAt, turnCovers, type LiveTraces, type Trace } from "./trace.ts";
import { AnswerReady, answerDue, answersAllowed, attentionFrom, Viewing, viewingReport, type Attention } from "./answer-ready.ts";
import { Presence, presenceReport, quietWake } from "./presence.ts";
Expand Down Expand Up @@ -3470,6 +3470,9 @@ function fitTranscripts(lists: Message[][], tail = RELAY_TAIL): Array<{ messages
* one it judged (judgeGoal), so a judge that took a minute never starts a
* turn behind the person's back, or on a reply no longer the latest. */
const laneTurns = new Map<string, number>();
/** The lane turn each lane's current work began in: the turn itself, or
* for a retry, the backup or a pickup, the one it goes on from. */
const workSince = new Map<string, number>();
/** Lanes whose goal is being judged right now, for the chip to say so. */
const goalJudging = new Set<string>();
/** The active choice survives client reloads, never transcript/history storage. */
Expand Down Expand Up @@ -5184,6 +5187,7 @@ function forgetAsk(requestId: string) {
askMessageByRequest.delete(requestId);
askCardThread.delete(requestId);
askThreadByRequest.delete(requestId);
askedIn.delete(requestId);
askEngineByRequest.delete(requestId);
asyncAskOrigin.delete(requestId);
withdrawing.delete(requestId);
Expand Down Expand Up @@ -6023,6 +6027,8 @@ async function onRuntimeEvent(event: RuntimeEvent) {
}
// answers must reach the lane that asked, not the active one
askThreadByRequest.set(event.requestId, event.threadId);
// and in which of its turns: asked again, it is the later one's
askedIn.set(event.requestId, { seq: laneTurns.get(event.threadId) ?? 0, question: asking });
// and are read against the questions it asked (/respond)
if (questions) askQuestions.set(event.requestId, questions);
if (event.providerInstanceId) askEngineByRequest.set(event.requestId, event.providerInstanceId);
Expand Down Expand Up @@ -6524,7 +6530,7 @@ async function onRuntimeEvent(event: RuntimeEvent) {
const quietGate = quietGates.get(event.threadId)?.turnId === event.turnId ? quietGates.get(event.threadId) : undefined;
const quietAction = quietGate?.state === "accepted" && !inRoom && event.ok !== false && !handedOver &&
event.stopReason !== "interrupted" && !pickingUp && !refolding &&
!turnsForYou.has(event.threadId) && ![...askThreadByRequest.values()].includes(event.threadId);
!turnsForYou.has(event.threadId) && !requestHolds(event.threadId, quietGate.since);
if (quietGate && !quietAction) cancelQuiet(event.threadId, quietGate);
if (quietGate) quietGates.delete(event.threadId);
// Restored words take the same notification path as ordinary ones,
Expand Down Expand Up @@ -6594,7 +6600,9 @@ async function onRuntimeEvent(event: RuntimeEvent) {
spokeInTurn.delete(event.threadId);
if (origin?.turnId === event.turnId) codexOrigins.delete(event.threadId);
if (quietAgentTurn || quietRun || quietEnd) {
// nothing new here
// nothing new here, and an earlier question still waiting keeps
// the dot its reply had, unless the person has opened it since
if (quietAction && quietGate.passed && dotTaken.get(event.threadId) === quietGate.since) store.markLane(bot.id, event.threadId, true);
} else if (bot.tasks.some((t) => t.id === event.threadId)) store.markLane(bot.id, event.threadId, true);
else store.patchBot(bot.id, { unread: true });
broadcast({ kind: "bot", bot: clientBot(store.bot(bot.id)) });
Expand Down Expand Up @@ -7246,6 +7254,12 @@ function callConflict(token?: string): { device: string; targetId: string } | nu

/** Which lane raised a live ask, so the answer lands in the right one. */
const askThreadByRequest = new Map<string, string>();
/** And in which of its turns, and whether it only asks (requestHolds). */
const askedIn = new Map<string, OpenAsk>();
/** The lane turn whose work took an unread lane's dot as it started, until
* the person opens the lane: a turn finished quietly past an earlier
* question gives it back (the reply's dot is that question's attention). */
const dotTaken = new Map<string, number>();
/**
* When each running turn began.
*
Expand Down Expand Up @@ -8824,6 +8838,11 @@ async function startClaimedTurn(
if (opts.serviceMessages?.length || !(opts.fallback || opts.retry || opts.carriedOn) || !answerAsked.has(task.id)) answerAsked.set(task.id, askedForAnswer);
// a goal judged before this turn began is not judging this one
laneTurns.set(task.id, (laneTurns.get(task.id) ?? 0) + 1);
// A retry, the backup or a pickup goes on with work that need not be
// the lane's latest (an incident is tried again after other turns), so
// when its work began is not known, and every open request holds it.
if (goesOn) workSince.delete(task.id);
else workSince.set(task.id, laneTurns.get(task.id)!);
// the busy flag holds the lane from here
if (claimedLanes.get(task.id) === claim) claimedLanes.delete(task.id);
codexDispatches.delete(task.id);
Expand Down Expand Up @@ -8852,6 +8871,10 @@ async function startClaimedTurn(
// hour would otherwise read the lane for the person each time, and a
// reply they have not seen yet would lose its dot to a run that, being
// quiet, puts none back.
// The dot new work takes is given back only if it finishes quietly past
// an earlier question, which a retry, the backup or a pickup never does.
if (!goesOn && !checkIn && store.taskByThread(task.id)?.task.unread) dotTaken.set(task.id, laneTurns.get(task.id)!);
else dotTaken.delete(task.id);
if (!checkIn) store.markLane(bot.id, task.id, false);
artifactTurns.begin(task.id, bot.id);
turnTokens.delete(task.id);
Expand Down Expand Up @@ -13907,6 +13930,7 @@ function readOn(threadId: string, personId: string, through: unknown, visible: (
* opening it still finds the line where it was.
*/
function readByOpening(threadId: string, body: { keepsPlace?: unknown } | null | undefined) {
dotTaken.delete(threadId);
if (body?.keepsPlace === true) return;
lastRead.readToEnd(threadId, "owner", placeOrder(threadId, () => true).map((m) => m.id));
}
Expand Down Expand Up @@ -16377,6 +16401,15 @@ function foldFinished(laneId: string, gate: QuietGate): boolean {
return true;
}

/** Whether a request still open in the lane keeps its turn, whose work
* began at lane turn `since`, from finishing quietly (heldByRequest). */
function requestHolds(laneId: string, since: number | undefined): boolean {
for (const [requestId, lane] of askThreadByRequest) {
if (lane === laneId && heldByRequest(askedIn.get(requestId), since)) return true;
}
return false;
}

/** `POST /api/bots/:me/tasks/:id/quiet`: checked and taken in one step,
* with nothing awaited, so a steer or an event cannot fall in between. */
function acceptQuiet(credential: { seq?: number }, laneId: string): { status: number; body: Record<string, unknown> } {
Expand All @@ -16395,7 +16428,10 @@ function acceptQuiet(credential: { seq?: number }, laneId: string): { status: nu
if (turnsForYou.has(laneId)) return refuse("person_in_turn");
const checkIn = checkInTurns.has(laneId);
if (!checkIn && !unaskedTurn(laneId)) return refuse("not_eligible");
if ([...askThreadByRequest.values()].includes(laneId)) return refuse("open_request");
const since = workSince.get(laneId);
if (requestHolds(laneId, since)) return refuse("open_request");
// what is still open here is an earlier turn's question
const passed = [...askThreadByRequest.values()].includes(laneId);
if (shownInTurn.get(laneId) === seq || spokeInTurn.has(laneId)) return refuse("already_shown");
// the turn so far: after the words it started on
const said = store.messagesFor(laneId).filter((m) => !m.queued && !m.unsent);
Expand All @@ -16410,7 +16446,7 @@ function acceptQuiet(credential: { seq?: number }, laneId: string): { status: nu
const quietAsk = (prompt?.via === "routine" && prompt.routine?.quiet) || (prompt?.via === "incident" && prompt.incident?.kind === "note");
if (checkIn && (!prompt || prompt.deleted || !quietAsk)) return refuse("not_eligible");
quietGates.set(laneId, {
seq, turnId, state: "accepted",
seq, turnId, since, passed, state: "accepted",
...(checkIn && prompt ? { promptId: prompt.id } : {}),
checked: checkIn ? turn.map((m) => m.id) : [],
rows: [], hidden: [],
Expand Down
19 changes: 19 additions & 0 deletions server/quiet-finish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface QuietGate {
/** The lane turn it was accepted in (laneTurns) and its runtime turn. */
seq: number;
turnId: string;
/** The lane turn its work began in (heldByRequest). Not known for a
* retry, the backup or a pickup, whose work may be older than the
* lane's latest. */
since?: number;
/** An earlier turn's question was open when it was accepted. */
passed?: boolean;
/** superseded: something for the person came after, so the turn ends
* as usual, and a later call is refused. */
state: "accepted" | "superseded";
Expand All @@ -52,6 +58,19 @@ export function belongs(gate: QuietGate, laneId: string, m: Message, w: Writer |
return w.turnId !== undefined ? w.turnId === gate.turnId : w.seq !== undefined && w.seq === gate.seq;
}

/** An open request, by the lane turn it was asked in (laneTurns) and
* whether it only asks, as a question does, rather than wanting a yes. */
export type OpenAsk = { seq: number; question: boolean };

/** Whether an open request keeps a turn whose work began at lane turn
* `since` from finishing quietly. A question asked before then stays
* open, on its card and waiting, for the person to answer when they
* choose, and does not hold it. An approval, a question of the turn's own
* work, or a request whose turn is not known always does. */
export function heldByRequest(asked: OpenAsk | undefined, since: number | undefined): boolean {
return !asked || !asked.question || since === undefined || asked.seq >= since;
}

/** Words on their way into a lane's running turn (steerOne), counted per
* lane turn, so a quiet finish cannot pass words the engine may already
* have. A settle from an earlier turn changes nothing of a later one. */
Expand Down
Loading