@linxiraos/pi-stats 1.4.13 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -5
- package/dist/client/index.js +86 -86
- package/dist/types/client/data/formatters.d.ts +9 -0
- package/dist/types/client/i18n/messages/common.d.ts +2 -2
- package/dist/types/client/i18n/messages/en.d.ts +2 -2
- package/dist/types/client/i18n/messages/models.d.ts +2 -2
- package/dist/types/client/i18n/messages/overview.d.ts +1 -1
- package/dist/types/client/ui/RequestDrawer.d.ts +2 -2
- package/dist/types/server.d.ts +3 -1
- package/dist/types/shared-types.d.ts +4 -3
- package/dist/types/trace.d.ts +8 -5
- package/package.json +4 -4
- package/src/client/data/formatters.ts +47 -6
- package/src/client/i18n/messages/common.ts +2 -2
- package/src/client/i18n/messages/models.ts +2 -2
- package/src/client/i18n/messages/overview.ts +1 -1
- package/src/client/routes/CostsRoute.tsx +24 -28
- package/src/client/routes/ErrorsRoute.tsx +57 -55
- package/src/client/routes/GainRoute.tsx +54 -54
- package/src/client/routes/ModelsRoute.tsx +7 -5
- package/src/client/routes/OverviewRoute.tsx +73 -62
- package/src/client/routes/ProvidersRoute.tsx +188 -176
- package/src/client/routes/RequestsRoute.tsx +98 -96
- package/src/client/routes/ToolsRoute.tsx +11 -10
- package/src/client/routes/TracesRoute.tsx +2 -1
- package/src/client/traces/SpanDrawer.tsx +2 -2
- package/src/client/traces/TimelineCanvas.tsx +16 -12
- package/src/client/traces/TraceView.tsx +18 -14
- package/src/client/ui/RequestDrawer.tsx +16 -6
- package/src/server.ts +7 -3
- package/src/shared-types.ts +4 -3
- package/src/trace.ts +43 -13
|
@@ -44,10 +44,12 @@ export interface ToolsRouteProps {
|
|
|
44
44
|
|
|
45
45
|
type CallMetric = "calls" | "errors";
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
function metricOptions() {
|
|
48
|
+
return [
|
|
49
|
+
{ value: "calls" as const, label: t("tools.calls") },
|
|
50
|
+
{ value: "errors" as const, label: t("tools.errors") },
|
|
51
|
+
];
|
|
52
|
+
}
|
|
51
53
|
|
|
52
54
|
/** Tools stacked individually in the calls chart; the rest fold into "Other". */
|
|
53
55
|
const TOP_TOOLS = 6;
|
|
@@ -59,9 +61,8 @@ function bucketUnit(bucketMs: number): string {
|
|
|
59
61
|
return t("tools.bucket-day");
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
const NO_CALLS = <EmptyState title={t("tools.no-tool-calls")} />;
|
|
63
|
-
|
|
64
64
|
export function ToolsRoute({ active, range }: ToolsRouteProps) {
|
|
65
|
+
const noCalls = <EmptyState title={t("tools.no-tool-calls")} />;
|
|
65
66
|
const tools = useQuery(["tools", range], ({ signal }) => getToolDashboardStats(range, signal), { enabled: active });
|
|
66
67
|
const [metric, setMetric] = useState<CallMetric>("calls");
|
|
67
68
|
const [hidden, toggleHidden] = useHiddenSeries();
|
|
@@ -160,10 +161,10 @@ export function ToolsRoute({ active, range }: ToolsRouteProps) {
|
|
|
160
161
|
index={1}
|
|
161
162
|
title={metric === "calls" ? t("tools.calls-over-time") : t("tools.errors-over-time")}
|
|
162
163
|
description={t("tools.chart-description", { bucket: bucketUnit(meta.bucketMs), count: TOP_TOOLS })}
|
|
163
|
-
actions={<Segmented size="sm" options={
|
|
164
|
+
actions={<Segmented size="sm" options={metricOptions()} value={metric} onChange={setMetric} />}
|
|
164
165
|
stale={tools.stale}
|
|
165
166
|
>
|
|
166
|
-
<QueryView query={tools} skeleton={<ChartSkeleton height={260} />} isEmpty={isEmpty} empty={
|
|
167
|
+
<QueryView query={tools} skeleton={<ChartSkeleton height={260} />} isEmpty={isEmpty} empty={noCalls}>
|
|
167
168
|
{() => (
|
|
168
169
|
<div className="stack" style={{ gap: 12 }}>
|
|
169
170
|
<TimeChart
|
|
@@ -197,7 +198,7 @@ export function ToolsRoute({ active, range }: ToolsRouteProps) {
|
|
|
197
198
|
flush
|
|
198
199
|
stale={tools.stale}
|
|
199
200
|
>
|
|
200
|
-
<QueryView query={tools} skeleton={<TableSkeleton rows={8} />} isEmpty={isEmpty} empty={
|
|
201
|
+
<QueryView query={tools} skeleton={<TableSkeleton rows={8} />} isEmpty={isEmpty} empty={noCalls}>
|
|
201
202
|
{() => (
|
|
202
203
|
<Table
|
|
203
204
|
rows={view.rows}
|
|
@@ -251,7 +252,7 @@ export function ToolsRoute({ active, range }: ToolsRouteProps) {
|
|
|
251
252
|
</div>
|
|
252
253
|
}
|
|
253
254
|
>
|
|
254
|
-
<QueryView query={tools} skeleton={<TableSkeleton rows={8} />} isEmpty={isEmpty} empty={
|
|
255
|
+
<QueryView query={tools} skeleton={<TableSkeleton rows={8} />} isEmpty={isEmpty} empty={noCalls}>
|
|
255
256
|
{data => <ToolModelTable rows={data.byToolModel} tool={toolFilter} colors={view.colors} />}
|
|
256
257
|
</QueryView>
|
|
257
258
|
</Card>
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from "../data/formatters";
|
|
15
15
|
import { useQuery } from "../data/query";
|
|
16
16
|
import { t } from "../i18n";
|
|
17
|
+
import { formatTimestamp } from "../data/formatters";
|
|
17
18
|
import { TraceView } from "../traces/TraceView";
|
|
18
19
|
import type { SessionSummary } from "../types";
|
|
19
20
|
import {
|
|
@@ -90,7 +91,7 @@ export function TracesRoute({ active, session, onOpenSession }: TracesRouteProps
|
|
|
90
91
|
header: t("common.started"),
|
|
91
92
|
align: "right",
|
|
92
93
|
render: row => (
|
|
93
|
-
<span className="muted" title={
|
|
94
|
+
<span className="muted" title={formatTimestamp(row.startedAt)}>
|
|
94
95
|
{formatRelativeTime(row.startedAt)}
|
|
95
96
|
</span>
|
|
96
97
|
),
|
|
@@ -8,7 +8,7 @@ import { toError } from "@linxiraos/pi-utils/type-guards";
|
|
|
8
8
|
import { Check, Copy, X } from "lucide-react";
|
|
9
9
|
import { type ReactNode, useEffect, useRef, useState } from "react";
|
|
10
10
|
import { getSessionEntryDetail } from "../api";
|
|
11
|
-
import { formatDurationMs, formatInteger } from "../data/formatters";
|
|
11
|
+
import { formatDurationMs, formatInteger, formatTimeOfDay } from "../data/formatters";
|
|
12
12
|
import { t } from "../i18n";
|
|
13
13
|
import type { TraceSpan, TraceTrack } from "../types";
|
|
14
14
|
import { Badge, Drawer, type DrawerProps, ErrorState, JsonBlock, KeyValues, Skeleton } from "../ui";
|
|
@@ -156,7 +156,7 @@ export function SpanDrawer({
|
|
|
156
156
|
{
|
|
157
157
|
key: "start",
|
|
158
158
|
label: t("trace.start"),
|
|
159
|
-
value: <span className="num">{
|
|
159
|
+
value: <span className="num">{formatTimeOfDay(span.start)}</span>,
|
|
160
160
|
},
|
|
161
161
|
{ key: "track", label: t("trace.track"), value: <span className="mono">{track?.label ?? "-"}</span> },
|
|
162
162
|
];
|
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
import { ChevronDown, ChevronRight } from "lucide-react";
|
|
10
10
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
11
11
|
import { createPortal } from "react-dom";
|
|
12
|
-
import { formatDurationMs, formatInteger } from "../data/formatters";
|
|
12
|
+
import { formatDurationMs, formatInteger, formatTimeOfDay } from "../data/formatters";
|
|
13
13
|
import { t } from "../i18n";
|
|
14
|
+
import { useLocale } from "../i18n/useLocale";
|
|
14
15
|
import type { TraceMarker, TraceSpan, TraceSpanKind, TraceTrack } from "../types";
|
|
15
16
|
import { buildTicks, formatOffset, type TraceScale } from "./time-scale";
|
|
16
17
|
import { type TraceTheme, useTraceTheme } from "./trace-colors";
|
|
@@ -51,13 +52,15 @@ const SPAN_RADIUS = 3;
|
|
|
51
52
|
/** Opacity of spans that don't match the search. */
|
|
52
53
|
const DIM_ALPHA = 0.3;
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
function laneOrder(): Array<{ kind: TraceSpanKind; label: string }> {
|
|
56
|
+
return [
|
|
57
|
+
{ kind: "turn", label: t("common.input") },
|
|
58
|
+
{ kind: "model", label: t("common.model") },
|
|
59
|
+
{ kind: "tool", label: t("trace.tools") },
|
|
60
|
+
{ kind: "subagent", label: t("trace.agents") },
|
|
61
|
+
{ kind: "background", label: t("trace.bg") },
|
|
62
|
+
];
|
|
63
|
+
}
|
|
61
64
|
|
|
62
65
|
interface LaneRow {
|
|
63
66
|
track: TraceTrack;
|
|
@@ -128,7 +131,7 @@ function buildLayout(tracks: TraceTrack[], collapsed: ReadonlySet<string>): Time
|
|
|
128
131
|
y1: y,
|
|
129
132
|
};
|
|
130
133
|
y += HEADER_H;
|
|
131
|
-
for (const { kind, label } of
|
|
134
|
+
for (const { kind, label } of laneOrder()) {
|
|
132
135
|
const spans = byKind[kind];
|
|
133
136
|
// Main always shows the core lanes; optional lanes appear when populated.
|
|
134
137
|
const isCore = kind === "turn" || kind === "model" || kind === "tool";
|
|
@@ -227,6 +230,7 @@ export function TimelineCanvas({
|
|
|
227
230
|
traceStart,
|
|
228
231
|
autoFocus = true,
|
|
229
232
|
}: TimelineCanvasProps) {
|
|
233
|
+
const locale = useLocale();
|
|
230
234
|
const colors = useTraceTheme();
|
|
231
235
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
232
236
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
@@ -247,7 +251,7 @@ export function TimelineCanvas({
|
|
|
247
251
|
const viewportRef = useRef(viewport);
|
|
248
252
|
viewportRef.current = viewport;
|
|
249
253
|
|
|
250
|
-
const layout = useMemo(() => buildLayout(tracks, collapsed), [tracks, collapsed]);
|
|
254
|
+
const layout = useMemo(() => buildLayout(tracks, collapsed), [tracks, collapsed, locale]);
|
|
251
255
|
const laneAxes = useMemo(() => buildLaneAxes(layout, scale), [layout, scale]);
|
|
252
256
|
|
|
253
257
|
// Observe container width (gutter excluded).
|
|
@@ -851,7 +855,7 @@ function renderTooltip(hover: Hit, traceStart: number, ref: React.RefObject<HTML
|
|
|
851
855
|
<div ref={ref} className="traces-tooltip" style={style}>
|
|
852
856
|
<div className="traces-tooltip-title">{marker.label}</div>
|
|
853
857
|
<div className="muted num">
|
|
854
|
-
{
|
|
858
|
+
{formatTimeOfDay(marker.time)} ({formatOffset(marker.time - traceStart)})
|
|
855
859
|
</div>
|
|
856
860
|
</div>
|
|
857
861
|
);
|
|
@@ -862,7 +866,7 @@ function renderTooltip(hover: Hit, traceStart: number, ref: React.RefObject<HTML
|
|
|
862
866
|
<div ref={ref} className="traces-tooltip" style={style}>
|
|
863
867
|
<div className="traces-tooltip-title truncate">{span.label}</div>
|
|
864
868
|
<div className="muted num">
|
|
865
|
-
{formatDurationMs(span.end - span.start)} · {
|
|
869
|
+
{formatDurationMs(span.end - span.start)} · {formatTimeOfDay(span.start)} (
|
|
866
870
|
{formatOffset(span.start - traceStart)}){span.unterminated ? ` · ${t("trace.unterminated")}` : ""}
|
|
867
871
|
</div>
|
|
868
872
|
{span.kind === "model" && (
|
|
@@ -26,19 +26,23 @@ export interface TraceViewProps {
|
|
|
26
26
|
onBack: () => void;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
function modeOptions(): Array<{ value: AxisMode; label: string; title?: string }> {
|
|
30
|
+
return [
|
|
31
|
+
{ value: "time", label: t("common.duration"), title: t("trace.mode-time-title") },
|
|
32
|
+
{ value: "turns", label: t("trace.turns"), title: t("trace.mode-turns-title") },
|
|
33
|
+
{ value: "calls", label: t("trace.calls"), title: t("trace.mode-calls-title") },
|
|
34
|
+
];
|
|
35
|
+
}
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
function legend(): Array<{ kind: TraceSpanKind; label: string }> {
|
|
38
|
+
return [
|
|
39
|
+
{ kind: "turn", label: t("common.input") },
|
|
40
|
+
{ kind: "model", label: t("common.model") },
|
|
41
|
+
{ kind: "tool", label: t("trace.tool") },
|
|
42
|
+
{ kind: "subagent", label: t("trace.agent") },
|
|
43
|
+
{ kind: "background", label: t("trace.background") },
|
|
44
|
+
];
|
|
45
|
+
}
|
|
42
46
|
|
|
43
47
|
export function TraceView({ file, active, onBack }: TraceViewProps) {
|
|
44
48
|
const query = useQuery<SessionTrace>(
|
|
@@ -307,7 +311,7 @@ export function SessionTraceView({
|
|
|
307
311
|
<div className="stack">
|
|
308
312
|
<div className="traces-toolbar">
|
|
309
313
|
<Segmented
|
|
310
|
-
options={
|
|
314
|
+
options={modeOptions()}
|
|
311
315
|
value={mode}
|
|
312
316
|
onChange={setMode}
|
|
313
317
|
size="sm"
|
|
@@ -369,7 +373,7 @@ export function SessionTraceView({
|
|
|
369
373
|
)}
|
|
370
374
|
</div>
|
|
371
375
|
<div className="traces-legend">
|
|
372
|
-
{
|
|
376
|
+
{legend().map(item => (
|
|
373
377
|
<span key={item.kind} className="traces-legend-item">
|
|
374
378
|
<span className="swatch" style={{ background: `var(${CATEGORY_VARS[item.kind]})` }} />
|
|
375
379
|
{item.label}
|
|
@@ -21,13 +21,23 @@ import { JsonBlock } from "./JsonBlock";
|
|
|
21
21
|
import { ErrorState, Skeleton } from "./States";
|
|
22
22
|
import "./request-drawer.css";
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
failed: { label: t("common.failed"), tone: "bad" },
|
|
24
|
+
const REQUEST_STATUS_TONE: Record<RequestStatus, Tone> = {
|
|
25
|
+
ok: "ok",
|
|
26
|
+
aborted: "warn",
|
|
27
|
+
failed: "bad",
|
|
29
28
|
};
|
|
30
29
|
|
|
30
|
+
const REQUEST_STATUS_KEY = {
|
|
31
|
+
ok: "ui.status-ok",
|
|
32
|
+
aborted: "common.aborted",
|
|
33
|
+
failed: "common.failed",
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
/** Label and tone for each request outcome; shared by the request tables. */
|
|
37
|
+
export function requestStatusMeta(status: RequestStatus): { label: string; tone: Tone } {
|
|
38
|
+
return { label: t(REQUEST_STATUS_KEY[status]), tone: REQUEST_STATUS_TONE[status] };
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
export interface RequestDrawerProps {
|
|
32
42
|
id: number | null;
|
|
33
43
|
onClose: () => void;
|
|
@@ -40,7 +50,7 @@ export function RequestDrawer({ id, onClose }: RequestDrawerProps) {
|
|
|
40
50
|
});
|
|
41
51
|
// Never show the previous request's payload under a new id.
|
|
42
52
|
const details = query.stale ? null : query.data;
|
|
43
|
-
const status = details ?
|
|
53
|
+
const status = details ? requestStatusMeta(requestStatus(details)) : null;
|
|
44
54
|
|
|
45
55
|
return (
|
|
46
56
|
<Drawer
|
package/src/server.ts
CHANGED
|
@@ -47,6 +47,7 @@ import {
|
|
|
47
47
|
listSessionSummaries,
|
|
48
48
|
TRACE_ETAG_VERSION,
|
|
49
49
|
type TraceFingerprint,
|
|
50
|
+
traceEtagForFingerprint,
|
|
50
51
|
traceFingerprintForEtag,
|
|
51
52
|
TracePathError,
|
|
52
53
|
} from "./trace";
|
|
@@ -372,7 +373,7 @@ export async function handleApi(req: Request): Promise<Response> {
|
|
|
372
373
|
if (clientEtag) {
|
|
373
374
|
fingerprint = await traceFingerprintForEtag(file);
|
|
374
375
|
if (fingerprint !== undefined) {
|
|
375
|
-
const etag = `"${TRACE_ETAG_VERSION}:${fingerprint
|
|
376
|
+
const etag = `"${TRACE_ETAG_VERSION}:${traceEtagForFingerprint(fingerprint)}"`;
|
|
376
377
|
if (clientEtag === etag) return new Response(null, { status: 304 });
|
|
377
378
|
}
|
|
378
379
|
}
|
|
@@ -559,10 +560,12 @@ function liveEventStream(headers: Record<string, string>, signal: AbortSignal):
|
|
|
559
560
|
});
|
|
560
561
|
}
|
|
561
562
|
|
|
562
|
-
/** A dashboard this process started or found; `
|
|
563
|
+
/** A dashboard this process started or found; `reused` distinguishes another process's server from one we own. */
|
|
563
564
|
export interface StatsServerHandle {
|
|
564
565
|
hostname: string;
|
|
565
566
|
port: number;
|
|
567
|
+
/** True when the dashboard belongs to another process and this process has no server to keep alive. */
|
|
568
|
+
reused: boolean;
|
|
566
569
|
stop: () => void;
|
|
567
570
|
}
|
|
568
571
|
|
|
@@ -613,6 +616,7 @@ export async function startServer(
|
|
|
613
616
|
const handle: StatsServerHandle = {
|
|
614
617
|
hostname,
|
|
615
618
|
port: boundPort,
|
|
619
|
+
reused: false,
|
|
616
620
|
stop: () => {
|
|
617
621
|
for (const key of keys) activeServers.delete(key);
|
|
618
622
|
server.stop(true);
|
|
@@ -632,7 +636,7 @@ export async function startServer(
|
|
|
632
636
|
if (server) return register(server);
|
|
633
637
|
claim = await recoverStatsPort(port, hostname);
|
|
634
638
|
}
|
|
635
|
-
if (claim === "reuse") return { hostname, port, stop: () => {} };
|
|
639
|
+
if (claim === "reuse") return { hostname, port, reused: true, stop: () => {} };
|
|
636
640
|
if (claim === "bind") {
|
|
637
641
|
const server = bindDashboardServer(port, hostname);
|
|
638
642
|
if (server) return register(server);
|
package/src/shared-types.ts
CHANGED
|
@@ -1230,9 +1230,10 @@ export interface SessionTrace {
|
|
|
1230
1230
|
/** Root transcript mtime, or recorded root activity/header time for offline snapshots. */
|
|
1231
1231
|
mtimeMs: number;
|
|
1232
1232
|
/**
|
|
1233
|
-
* Conditional-request fingerprint `"<rootMs>:<
|
|
1234
|
-
* traceFingerprintForEtag): the
|
|
1235
|
-
* subagent-only append changes the ETag even when the
|
|
1233
|
+
* Conditional-request fingerprint `"<rootMs>:<rootSize>:<digest>"` (see
|
|
1234
|
+
* traceFingerprintForEtag): the digest folds the child-transcript set into a
|
|
1235
|
+
* fixed-size key, so a subagent-only append changes the ETag even when the
|
|
1236
|
+
* root is untouched without making `If-None-Match` unbounded.
|
|
1236
1237
|
* Offline snapshots use "snapshot" and do not support HTTP revalidation.
|
|
1237
1238
|
*/
|
|
1238
1239
|
etag: string;
|
package/src/trace.ts
CHANGED
|
@@ -35,7 +35,7 @@ export class TracePathError extends Error {}
|
|
|
35
35
|
* span assembly changes so browsers don't revalidate stale cached bodies
|
|
36
36
|
* against an unchanged transcript mtime.
|
|
37
37
|
*/
|
|
38
|
-
export const TRACE_ETAG_VERSION =
|
|
38
|
+
export const TRACE_ETAG_VERSION = 5;
|
|
39
39
|
|
|
40
40
|
const MAX_TRACK_DEPTH = 6;
|
|
41
41
|
const LABEL_MAX = 80;
|
|
@@ -778,10 +778,12 @@ let traceMemo: { file: string; entry: TraceMemoEntry } | undefined;
|
|
|
778
778
|
/**
|
|
779
779
|
* Fingerprint of the child-transcript set: per-directory file COUNT plus
|
|
780
780
|
* sorted `name@mtimeMs` pairs, mirroring buildTrackTree's discovery (stats
|
|
781
|
-
* only, no parse). The count
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
781
|
+
* only, no parse). The count makes an empty directory distinguishable from a
|
|
782
|
+
* missing one and keeps renames visible. The walk is depth-capped; the raw
|
|
783
|
+
* string still grows with child count, but the wire ETag hashes it, so a large
|
|
784
|
+
* tree cannot inflate the request header. Best-effort: a raced gc deletion is
|
|
785
|
+
* recorded as `name@gone` (which itself changes the fingerprint and misses the
|
|
786
|
+
* memo — the safe direction).
|
|
785
787
|
*/
|
|
786
788
|
async function childTranscriptsFingerprint(rootFile: string, depth = 0): Promise<string> {
|
|
787
789
|
if (depth > MAX_TRACK_DEPTH) return "";
|
|
@@ -810,24 +812,42 @@ async function childTranscriptsFingerprint(rootFile: string, depth = 0): Promise
|
|
|
810
812
|
return [`n=${transcriptNames.length}`, ...childParts].join(",");
|
|
811
813
|
}
|
|
812
814
|
|
|
813
|
-
/** Freshness of a root trace: root mtime plus the child-transcript set fingerprint. */
|
|
815
|
+
/** Freshness of a root trace: root mtime+size plus the child-transcript set fingerprint. */
|
|
814
816
|
export interface TraceFingerprint {
|
|
815
817
|
rootMtimeMs: number;
|
|
818
|
+
rootSize: number;
|
|
816
819
|
childFingerprint: string;
|
|
817
820
|
}
|
|
818
821
|
|
|
822
|
+
/**
|
|
823
|
+
* Compact wire ETag key. The raw child fingerprint is unbounded — a
|
|
824
|
+
* subagent-heavy session can produce tens of kilobytes of `name@mtime` pairs.
|
|
825
|
+
* Sending that string back in `If-None-Match` exceeds Bun's 16 KiB request
|
|
826
|
+
* header limit (HTTP 431), so hash it to a fixed-size key first. Root size is
|
|
827
|
+
* part of the key because a coarse mtime can miss an append.
|
|
828
|
+
*/
|
|
829
|
+
function traceEtagKey(rootMtimeMs: number, rootSize: number, childFingerprint: string): string {
|
|
830
|
+
const digest = new Bun.CryptoHasher("sha256").update(childFingerprint).digest("hex");
|
|
831
|
+
return `${rootMtimeMs}:${rootSize}:${digest}`;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/** Build the wire ETag from a fingerprint already computed by the caller. */
|
|
835
|
+
export function traceEtagForFingerprint(fingerprint: TraceFingerprint): string {
|
|
836
|
+
return traceEtagKey(fingerprint.rootMtimeMs, fingerprint.rootSize, fingerprint.childFingerprint);
|
|
837
|
+
}
|
|
838
|
+
|
|
819
839
|
/** Stat the root and walk its child transcripts (readdir+stat, no parse). */
|
|
820
840
|
async function readTraceFingerprint(resolved: string): Promise<TraceFingerprint> {
|
|
821
|
-
const
|
|
841
|
+
const stat = await fs.stat(resolved);
|
|
822
842
|
const childFingerprint = await childTranscriptsFingerprint(resolved);
|
|
823
|
-
return { rootMtimeMs, childFingerprint };
|
|
843
|
+
return { rootMtimeMs: stat.mtimeMs, rootSize: stat.size, childFingerprint };
|
|
824
844
|
}
|
|
825
845
|
|
|
826
846
|
/**
|
|
827
847
|
* Freshness fingerprint for the trace ETag pre-check; `SessionTrace.etag` is
|
|
828
|
-
* `${rootMtimeMs}:${
|
|
829
|
-
* when the path is rejected or missing (the caller falls
|
|
830
|
-
* build, which maps those to 400/404).
|
|
848
|
+
* the compact `${rootMtimeMs}:${rootSize}:${digest}` key for an unraced build.
|
|
849
|
+
* Returns undefined when the path is rejected or missing (the caller falls
|
|
850
|
+
* through to the full build, which maps those to 400/404).
|
|
831
851
|
*/
|
|
832
852
|
export async function traceFingerprintForEtag(fileParam: string): Promise<TraceFingerprint | undefined> {
|
|
833
853
|
try {
|
|
@@ -856,7 +876,11 @@ export async function buildSessionTrace(fileParam: string, fingerprint?: TraceFi
|
|
|
856
876
|
// alike): the post-build race guard compares against this, so a child
|
|
857
877
|
// append landing mid-parse is detected even on first builds and
|
|
858
878
|
// memo-less rebuilds, where no prior fingerprint exists to compare.
|
|
859
|
-
const {
|
|
879
|
+
const {
|
|
880
|
+
rootMtimeMs,
|
|
881
|
+
rootSize,
|
|
882
|
+
childFingerprint: preChildFingerprint,
|
|
883
|
+
} = fingerprint ?? (await readTraceFingerprint(resolved));
|
|
860
884
|
if (memo && memo.mtimeMs === rootMtimeMs) {
|
|
861
885
|
// Equality, not <=: a LOWER mark (delete/older-mtime replace) is
|
|
862
886
|
// also stale — only an identical child set may reuse the trace.
|
|
@@ -884,7 +908,13 @@ export async function buildSessionTrace(fileParam: string, fingerprint?: TraceFi
|
|
|
884
908
|
// indefinitely cached one. Unraced builds use the post-build key, which
|
|
885
909
|
// equals the pre-build key by construction.
|
|
886
910
|
const etagFingerprint = raced || rootChanged ? preChildFingerprint : childFingerprint;
|
|
887
|
-
const trace = assembleSessionTrace(
|
|
911
|
+
const trace = assembleSessionTrace(
|
|
912
|
+
rootScan,
|
|
913
|
+
tracks,
|
|
914
|
+
scans,
|
|
915
|
+
rootMtimeMs,
|
|
916
|
+
traceEtagKey(rootMtimeMs, rootSize, etagFingerprint),
|
|
917
|
+
);
|
|
888
918
|
if (!raced && !rootChanged) {
|
|
889
919
|
traceMemo = {
|
|
890
920
|
file: resolved,
|