Skip to content
Merged
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
321 changes: 321 additions & 0 deletions apps/api/tests/workers/project-data-message-ingest-metrics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
import type { ProjectData } from '@simple-agent-manager/project-data/internal/do/index';
import type { MessageIngestMetrics } from '@simple-agent-manager/project-data/internal/do/message-ingest-metrics';
import { projectDataPorts } from '@simple-agent-manager/project-data/internal/do/ports';
import type { RowUsage } from '@simple-agent-manager/project-data/internal/do/row-usage';
import { env, runInDurableObject } from 'cloudflare:test';
import { afterEach, describe, expect, it, vi } from 'vitest';

const source = {
workspaceId: 'metrics-workspace',
callbackGeneration: null,
outboxIncarnation: 'cbce9fe8-c5e5-464c-9900-4bec2bd30cb1',
};
const config = { enabled: true, maxRowBytes: 65536, recentIds: 200 };
function chunk(sequence: number, role = 'assistant') {
return {
messageId: `chunk-${sequence}`,
sequence,
role,
toolMetadata: null,
content: `part${sequence} `,
timestamp: new Date(1700000000000 + sequence).toISOString(),
};
}
function reports(spy: ReturnType<typeof vi.spyOn>): Array<{ messageIngest: MessageIngestMetrics }> {
return spy.mock.calls.flatMap(([line]) => {
try {
const value = JSON.parse(String(line));
return value.event === 'project_data.row_usage' ? [value] : [];
} catch {
return [];
}
});
}
async function setup(enabled = true) {
vi.spyOn(projectDataPorts().operations, 'messageMergeConfig').mockResolvedValue({
...config,
enabled,
});
const stub = env.PROJECT_DATA.get(
env.PROJECT_DATA.newUniqueId()
) as DurableObjectStub<ProjectData>;
await stub.ensureProjectId('metrics-project');
const session = await stub.createSession(source.workspaceId, 'Metrics topic');
return { stub, session };
}
async function flush(stub: DurableObjectStub<ProjectData>) {
await runInDurableObject(stub, (instance) => {
const usage = (instance as unknown as { rowUsage: RowUsage }).rowUsage;
const start = (usage as unknown as { windowStart: number }).windowStart;
usage.flush(start + usage.intervalMs);
});
}
async function captureBatchError(stub: DurableObjectStub<ProjectData>, session: string) {
const response = await stub.fetch(
new Request('https://test/__capture-expected-error', {
method: 'POST',
body: JSON.stringify({
operation: 'persistMessageBatch',
args: [session, [chunk(1), chunk(2)], source],
}),
})
);
return response.json();
}
afterEach(() => vi.restoreAllMocks());

describe('content-free message ingestion counters through real workerd', () => {
it('counts accepted roles, same-batch coalesces, cross-batch SQL appends and replay separately', async () => {
const { stub, session } = await setup();
const log = vi.spyOn(console, 'log');
await stub.persistMessageBatch(session, [chunk(1), chunk(2)], source);
await stub.persistMessageBatch(session, [chunk(3), chunk(4)], source);
await stub.persistMessageBatch(session, [chunk(1), chunk(2), chunk(3)], source);
await stub.persistMessageBatch(
session,
[chunk(5, 'thinking'), chunk(6, 'thinking'), chunk(7, 'tool')],
source
);
await flush(stub);
const metrics = reports(log).at(-1)!.messageIngest;
expect(metrics).toMatchObject({
batches: 4,
received: { assistant: 7, thinking: 2, tool: 1 },
accepted: { assistant: 4, thinking: 2, tool: 1 },
inserted: { assistant: 1, thinking: 2, tool: 1 },
eligible: 4,
coalesced: 3,
appendStatements: 1,
duplicates: 3,
mergeReadyBatches: 4,
refused: {},
breaks: { no_open: 1, role: 1 },
});
const rows = (await stub.getMessages(session, 50, null, null, undefined, false, 'asc'))
.messages;
expect(rows.map((row) => row.role)).toEqual(['assistant', 'thinking', 'thinking', 'tool']);
expect(rows[0]!.content).toBe('part1 part2 part3 part4 ');
log.mockClear();
await stub.persistMessageBatch(session, [chunk(8, 'plan')], source);
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
batches: 1,
received: { assistant: 0, plan: 1 },
coalesced: 0,
appendStatements: 0,
duplicates: 0,
});
});

it.each([false, true])(
'observes actual scoped and unscoped legacy socket admission; only scoped refuses (sessionScoped=%s)',
async (sessionScoped) => {
const { stub, session } = await setup();
const response = await stub.fetch(
new Request(`https://test/ws${sessionScoped ? `?sessionId=${session}` : ''}`, {
headers: { Upgrade: 'websocket' },
})
);
expect(response.status).toBe(101);
response.webSocket!.accept();
const log = vi.spyOn(console, 'log');
await stub.persistMessageBatch(session, [chunk(1), chunk(2)], source);
await stub.persistMessageBatch(session, [chunk(3)], source);
await flush(stub);
// A legacy session socket refuses merging for its session. A legacy project socket gets no
// transcript frames, so it is counted for diagnostics but never refuses.
expect(reports(log).at(-1)!.messageIngest).toMatchObject(
sessionScoped
? {
batches: 2,
eligible: 3,
coalesced: 0,
inserted: { assistant: 3 },
refused: { legacy_client: 2 },
legacySessionSocketsMax: 1,
legacyProjectSocketsMax: 0,
}
: {
batches: 2,
eligible: 3,
coalesced: 2,
appendStatements: 1,
inserted: { assistant: 1 },
refused: {},
legacySessionSocketsMax: 0,
legacyProjectSocketsMax: 1,
}
);
if (!sessionScoped)
expect(reports(log).at(-1)!.messageIngest.refused.legacy_client).toBeUndefined();
expect((await stub.getMessages(session, 50)).messages).toHaveLength(sessionScoped ? 3 : 1);
response.webSocket!.close();
}
);

it.each(['capable reader', 'unrelated legacy reader'])(
'merges with a %s connected',
async (reader) => {
const { stub, session } = await setup();
const watched =
reader === 'capable reader'
? session
: await stub.createSession(source.workspaceId, 'Unrelated');
const capability = reader === 'capable reader' ? '&messageAppend=message-append-v1' : '';
const response = await stub.fetch(
new Request(`https://test/ws?sessionId=${watched}${capability}`, {
headers: { Upgrade: 'websocket' },
})
);
expect(response.status).toBe(101);
response.webSocket!.accept();
const log = vi.spyOn(console, 'log');
await stub.persistMessageBatch(session, [chunk(1), chunk(2)], source);
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
coalesced: 1,
refused: {},
legacySessionSocketsMax: 0,
legacyProjectSocketsMax: 0,
});
response.webSocket!.close();
}
);

it.each(['setting_disabled', 'missing_incarnation', 'invalid_sequence', 'receipt_cap'] as const)(
'explains primary refusal %s',
async (reason) => {
const { stub, session } = await setup(reason !== 'setting_disabled');
if (reason === 'receipt_cap')
vi.mocked(projectDataPorts().operations.messageMergeConfig!).mockResolvedValue({
...config,
maxRowBytes: 1024,
});
const log = vi.spyOn(console, 'log');
const input =
reason === 'receipt_cap'
? Array.from({ length: 50 }, (_, index) => ({
...chunk(index + 1),
messageId: crypto.randomUUID(),
}))
: [chunk(2), chunk(reason === 'invalid_sequence' ? 1 : 3)];
await stub.persistMessageBatch(
session,
input,
reason === 'missing_incarnation' ? { ...source, outboxIncarnation: undefined } : source
);
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
batches: 1,
inserted: { assistant: input.length },
accepted: { assistant: input.length },
coalesced: 0,
refused: { [reason]: 1 },
});
}
);

it('does not publish outcomes when the actual persistence transaction rolls back', async () => {
const { stub, session } = await setup();
const log = vi.spyOn(console, 'log');
await runInDurableObject(stub, (instance) => {
const holder = instance as unknown as { sql: SqlStorage };
const original = holder.sql;
holder.sql = new Proxy(original, {
get(target, key) {
if (key === 'exec')
return (query: string, ...bindings: unknown[]) => {
if (query.startsWith('INSERT INTO chat_messages '))
throw new Error('controlled insert failure');
return target.exec(query, ...bindings);
};
return Reflect.get(target, key, target);
},
});
});
expect(await captureBatchError(stub, session)).toMatchObject({
threw: true,
message: 'controlled insert failure',
});
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
batches: 0,
accepted: { assistant: 0 },
coalesced: 0,
});
expect((await stub.getMessages(session, 50)).messages).toHaveLength(0);
});
it('retains committed counters and rows when later broadcasting fails', async () => {
const { stub, session } = await setup();
const log = vi.spyOn(console, 'log');
await runInDurableObject(stub, (instance) => {
const holder = instance as unknown as {
broadcastEvent: (
type: string,
payload: Record<string, unknown>,
sessionId?: string
) => void;
};
const original = holder.broadcastEvent.bind(holder);
holder.broadcastEvent = (type, payload, sessionId) => {
if (type === 'messages.batch') throw new Error('controlled post-commit broadcast failure');
original(type, payload, sessionId);
};
});
expect(await captureBatchError(stub, session)).toMatchObject({
threw: true,
message: 'controlled post-commit broadcast failure',
});
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
batches: 1,
accepted: { assistant: 2 },
inserted: { assistant: 1 },
coalesced: 1,
});
const rows = (await stub.getMessages(session, 50)).messages;
expect(rows).toHaveLength(1);
expect(rows[0]!.content).toBe('part1 part2 ');
});

it('explains source replacement and a delayed retired-source continuation', async () => {
const { stub, session } = await setup();
const log = vi.spyOn(console, 'log');
await stub.persistMessageBatch(session, [chunk(1), chunk(2)], source);
await stub.persistMessageBatch(
session,
[{ ...chunk(3), messageId: 'new-source', sequence: 1 }],
{ ...source, outboxIncarnation: 'b5c61e7f-ae8d-4e2e-b77b-3c849ed6036b' }
);
await stub.persistMessageBatch(session, [chunk(4)], source);
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
batches: 3,
refused: { retired_source: 1 },
breaks: { source: 2 },
});
expect((await stub.getMessages(session, 50)).messages).toHaveLength(3);
});

it('identifies oversized and older-position initial rows without changing their inserts', async () => {
const { stub, session } = await setup();
const log = vi.spyOn(console, 'log');
await stub.persistMessageBatch(
session,
[{ ...chunk(1), content: 'x'.repeat(config.maxRowBytes) }],
source
);
await stub.persistMessageBatch(
session,
[{ ...chunk(2), timestamp: new Date(1600000000000).toISOString() }],
source
);
await flush(stub);
expect(reports(log).at(-1)!.messageIngest).toMatchObject({
accepted: { assistant: 2 },
inserted: { assistant: 2 },
coalesced: 0,
breaks: { size_cap: 1, out_of_order: 1 },
});
});
});
24 changes: 24 additions & 0 deletions packages/project-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ProjectData diagnostics

The existing `project_data.row_usage` log reports approximate per-operation SQL reads/writes and alarm writes. `alarmWrites` is already included in `rowsWritten`. Counters live only in the Durable Object instance and can be lost on eviction; requested elapsed time is not logged coverage.

`messageIngest` in the same log window explains batch persistence without logging message content, metadata, IDs or source identities:

| Field | Meaning |
| ---------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `batches` | Batch persistence transactions that returned successfully, including duplicate-only batches |
| `received`, `accepted`, `inserted` | Transport chunks, newly accepted logical chunks, and physical inserts respectively, by fixed role (`user`, `assistant`, `thinking`, `tool`, `plan`, `system`, `other`) |
| `eligible` | Accepted assistant chunks with null stored tool metadata, regardless of whether the merge gate is enabled |
| `coalesced` | Logical chunks absorbed into an earlier canonical row, including merges within the same batch |
| `appendStatements` | Successful SQL updates to an existing canonical row; multiple chunks in one batch may share one update |
| `duplicates` | Chunks rejected as duplicates by the existing persistence logic |
| `mergeReadyBatches` | Batches passing setting, socket, source, sequence and receipt prerequisites (may contain no eligible chunks) |
| `refused` | Batches by their first applicable refusal: setting disabled, legacy client, missing incarnation, invalid sequence, retired source or receipt cap |
| `breaks` | Run boundary observations by fixed reason; `no_open` includes normal starts, not only failures |
| `legacyProjectSocketsMax`, `legacySessionSocketsMax` | Window maxima of open sockets without the append capability: on the batch's session (these refuse merging) and project-wide (diagnostic only; they get no transcript frames) |

Primary refusal precedence is `setting_disabled`, `legacy_client`, `missing_incarnation`, `invalid_sequence`, `retired_source`, `receipt_cap`. Run-boundary reasons are `no_open`, `source`, `turn`, `intervening_writer`, `materialized`, `role`, `metadata`, `origin`, `sequence_gap`, `timestamp`, `out_of_order`, `size_cap`. Several independent boundaries can occur in one batch, so boundary counts do not partition batches. Admission of a legacy session socket seals that session's open run before persistence; `legacySessionSocketsMax` and `refused.legacy_client` identify that gate. `legacyProjectSocketsMax` shows stale session-list tabs without affecting merging.

Diagnostics publish after the synchronous storage transaction returns, before asynchronous side effects. A rollback contributes no ingestion outcomes; a later broadcast/summary failure does not erase committed outcomes. Received counts include retry input; accepted/inserted/coalesced counts do not include duplicates. Batches that throw before commit contribute no ingestion counts, so SQL RPC calls and `batches` can differ. Existing SQL cursor attribution remains unchanged. Counters reset with the existing log window, have fixed cardinality, perform no SQL writes, and do not create timers or extra log lines.

To distinguish a disabled merger from low eligible traffic, compare refusal reasons and socket maxima first, then accepted role mix, eligible share and coalesced chunks. A build version or enabled D1 setting alone does not prove a batch can merge. Compare production writes/call alongside accepted chunks/call and role mix; changes between unrelated time windows are not a controlled savings measurement.
17 changes: 12 additions & 5 deletions packages/project-data/src/do/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import * as logicalTranscript from './logical-transcript';
import * as mailbox from './mailbox';
import * as materialization from './materialization';
import * as messageContext from './message-context';
import { emptyMessageIngestMetrics, refuseMerge } from './message-ingest-metrics';
import * as messagePersistence from './message-persistence';
import { DEFAULT_MESSAGE_MERGE_CONFIG, type MessageIngestSource } from './message-stream-ingest';
import * as messages from './messages';
Expand Down Expand Up @@ -125,7 +126,7 @@ import {
admitBrowserSocket,
broadcastToBrowserSockets,
closeQuietly,
hasLegacySessionSocket,
countLegacySockets,
} from './session-sockets';
import * as sessionState from './session-state';
import * as sessionSummarySync from './session-summary-sync';
Expand Down Expand Up @@ -908,16 +909,21 @@ export class ProjectData extends DurableObject<Env> {
? ((await projectDataPorts().operations.messageMergeConfig?.(this.env)) ??
DEFAULT_MESSAGE_MERGE_CONFIG)
: DEFAULT_MESSAGE_MERGE_CONFIG;
// Only a socket watching this session can miss an append; project sockets never get
// transcript frames (session-sockets.ts).
const legacyClient = hasLegacySessionSocket(this.ctx, sessionId);
const metrics = emptyMessageIngestMetrics();
const legacy = countLegacySockets(this.ctx, sessionId);
metrics.legacySessionSocketsMax = legacy.session;
// Informational only: project sockets get no transcript frames and never gate merging.
metrics.legacyProjectSocketsMax = legacy.project;
const legacyClient = legacy.session > 0;
if (!config.enabled) refuseMerge(metrics, 'setting_disabled');
else if (legacyClient) refuseMerge(metrics, 'legacy_client');
return messagePersistence.persistMessageBatchWithSideEffects(
this.sql,
this.env,
this.messagePersistenceHooks(),
sessionId,
batchMessages,
{ source, config: { ...config, enabled: config.enabled && !legacyClient } }
{ source, config: { ...config, enabled: config.enabled && !legacyClient }, metrics }
);
});
}
Expand Down Expand Up @@ -1014,6 +1020,7 @@ export class ProjectData extends DurableObject<Env> {

private messagePersistenceHooks(): messagePersistence.MessagePersistenceHooks {
return {
recordMessageIngest: (metrics) => this.rowUsage.recordMessageIngest(metrics),
transactionSync: (fn) => this.meteredStorage.transactionSync(fn),
recalculateAlarm: () => this.recalculateAlarm(),
scheduleSummarySync: () => this.scheduleSummarySync(),
Expand Down
Loading
Loading