mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 03:31:44 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - When someone messages an agent, the reply must start fast. The durable ownership plan (#19831) makes time to first token (TTFT) a hard invariant. > - Phase 0 of that plan records a TTFT baseline per adapter and environment, measured from the moment the message or wake is accepted. > - Today `task.time_to_first_token` starts at the wake comment, the answered question, or the run row's creation. A wake request that was accepted earlier (deferred, coalesced or retried) is not part of the measurement, and the span does not say which start it used. > - This pull request moves the start back to the earliest accepted input and names it on the span. > - The benefit is that the baseline and later budgets measure what the user waits for, with no new work before `turn.start`. ## Linked Issues or Issue Description Refs #19831 **What is the problem?** `task.time_to_first_token` does not include the time between an accepted wake request and the creation of its run row. It also does not record which input it started from. **What is the expected outcome?** The span starts at the earliest accepted input and carries an `anchor` attribute: `comment`, `question_response`, `wake_request` or `run_created`. **Why does it matter?** Phase 0 of the durable ownership plan sets TTFT budgets from this baseline. A start that skips queue time hides the latency that deferred wakes add. ## What Changed - `resolveNativeTimeToFirstTokenStart` (pure) in `native-run-trace.ts` picks the start and the anchor. `anchor` joins the closed span-attribute allowlist. - `readNativeRunWakeRequestedAtMs` reads `agent_wakeup_requests.requested_at` for the run. It never throws. - `native-session-executor.ts` calls it only after the first deliverable text, so it adds no wait before `turn.start`. The span now ends at the text commit time, not after the trace write. - Updated `doc/run-log-events.md` and `doc/observability.md`. ## Verification - `cd server && npx vitest run src/services/native-runtime/native-run-trace.test.ts src/services/native-runtime/native-run-wake-anchor.test.ts` (26 pass). - `cd server && npx vitest run src/services/native-runtime/native-session-executor.test.ts -t first` (41 pass). The existing TTFT test now asserts the anchor for each ingress. ## Risks - Low risk. This changes only the run-log and OTLP span. It is not Telemetry (AGENTS.md §5.7), and it does not change runtime behavior. - One indexed primary-key read per native dispatch, after the first text. A read failure drops the anchor and keeps the old start. - TTFT values can grow for deferred wakes. That is the intended correction. `startOffsetMs` can be negative when the wake request comes before the trace start. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { agentWakeupRequests, heartbeatRuns, type Db } from "@paperclipai/db";
|
|
import { eq, sql } from "drizzle-orm";
|
|
|
|
/** A slow database never holds this diagnostic longer than this. */
|
|
export const NATIVE_RUN_WAKE_ANCHOR_READ_TIMEOUT_MS = 2_000;
|
|
|
|
/**
|
|
* When the durable wake request that created this run was accepted, in epoch
|
|
* milliseconds. Diagnostic only: it anchors `task.time_to_first_token` to the
|
|
* accepted input. Callers run it detached after the first deliverable text, so
|
|
* it never adds a wait before `turn.start` or to event delivery.
|
|
*
|
|
* The read is bounded on both sides. PostgreSQL cancels the statement at
|
|
* `timeoutMs` (a transaction-local `statement_timeout`, which also covers lock
|
|
* waits), so a stalled read does not keep a shared connection busy after the
|
|
* caller stops waiting. The caller also stops waiting at `timeoutMs`, which
|
|
* covers a wait for a pool connection. Any failure returns null.
|
|
*/
|
|
export async function readNativeRunWakeRequestedAtMs(
|
|
db: Db,
|
|
runId: string,
|
|
timeoutMs = NATIVE_RUN_WAKE_ANCHOR_READ_TIMEOUT_MS,
|
|
): Promise<number | null> {
|
|
const boundedMs = Math.max(1, Math.floor(timeoutMs));
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
const timeout = new Promise<null>((resolve) => {
|
|
timer = setTimeout(() => resolve(null), boundedMs);
|
|
timer.unref?.();
|
|
});
|
|
try {
|
|
return await Promise.race([readWakeRequestedAtMs(db, runId, boundedMs), timeout]);
|
|
} finally {
|
|
if (timer) clearTimeout(timer);
|
|
}
|
|
}
|
|
|
|
async function readWakeRequestedAtMs(db: Db, runId: string, timeoutMs: number): Promise<number | null> {
|
|
try {
|
|
return await db.transaction(async (tx) => {
|
|
await tx.execute(sql`select set_config('statement_timeout', ${`${timeoutMs}ms`}, true)`);
|
|
const [row] = await tx
|
|
.select({ requestedAt: agentWakeupRequests.requestedAt })
|
|
.from(heartbeatRuns)
|
|
.innerJoin(
|
|
agentWakeupRequests,
|
|
eq(agentWakeupRequests.id, heartbeatRuns.wakeupRequestId),
|
|
)
|
|
.where(eq(heartbeatRuns.id, runId))
|
|
.limit(1);
|
|
const at = row?.requestedAt?.getTime();
|
|
return typeof at === "number" && Number.isFinite(at) ? at : null;
|
|
});
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|