fix(web): the journal reads the answer of the route field by field (#205)
"Plus anciennes" cast the JSON of GET /api/agent-actions to a page and rendered it. readJournalPage() now reads each field as the scalar it must be, keeps nothing else, and gives null for any other shape: the journal then shows its load error instead of handing an object to the JSX or an invalid date to the formatter. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
67d390cc37
commit
7c73f8c8ac
4 changed files with 71 additions and 3 deletions
|
|
@ -47,7 +47,7 @@ const { GET } = await import('./route');
|
|||
const { db } = await import('@/db/client');
|
||||
const { listActions, recordAction } = await import('@/lib/agentJournal');
|
||||
const { createAgentToken } = await import('@/lib/agentTokens');
|
||||
const { JOURNAL_PAGE_SIZE, journalPageView } = await import('@/lib/assistants');
|
||||
const { JOURNAL_PAGE_SIZE, journalPageView, readJournalPage } = await import('@/lib/assistants');
|
||||
|
||||
// Origin of `request.url` in production: the container's, not the site's.
|
||||
const CONTAINER = 'http://0.0.0.0:3000';
|
||||
|
|
@ -214,6 +214,8 @@ describe('GET /api/agent-actions', () => {
|
|||
// Neither the account, nor the touched rows, nor the token id leave.
|
||||
for (const hidden of [A, taskId, gone, tokenId]) expect(a.text).not.toContain(hidden);
|
||||
expect(h.limits).toEqual([[A, 'read']]);
|
||||
// The journal component accepts the answer as it is.
|
||||
expect(readJournalPage(a.json)).toEqual(a.json);
|
||||
});
|
||||
|
||||
it('gives the first page the same shape as the page the server component reads', async () => {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { useTranslation } from "react-i18next";
|
|||
import {
|
||||
actionLabelKey,
|
||||
formatDayTime,
|
||||
readJournalPage,
|
||||
type DateLocale,
|
||||
type JournalLine,
|
||||
type JournalPageView,
|
||||
|
|
@ -43,7 +44,11 @@ export function AssistantsJournal({ first, locale, timeZone }: AssistantsJournal
|
|||
setLoadError(true);
|
||||
return;
|
||||
}
|
||||
const page = (await res.json()) as JournalPageView;
|
||||
const page = readJournalPage(await res.json());
|
||||
if (page === null) {
|
||||
setLoadError(true);
|
||||
return;
|
||||
}
|
||||
setOlder((lines) => [...lines, ...page.actions]);
|
||||
setCursor(page.nextCursor);
|
||||
} catch {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import {
|
|||
SCOPE_LABEL_KEYS, TOKEN_DURATIONS, TOKEN_NAME_MAX, TOKEN_PLACEHOLDER, TOKEN_SCOPES,
|
||||
actionLabelKey, claudeCodeTemplate, codeSpans, dateLocale, decodeJournalCursor, encodeJournalCursor,
|
||||
formatDay, formatDayTime, genericTemplate, hermesTemplate, isExpired, journalKey, journalPageView,
|
||||
mcpServerUrl, tokenView, validateTokenName,
|
||||
mcpServerUrl, readJournalPage, tokenView, validateTokenName,
|
||||
type JournalPageView, type TokenView,
|
||||
} from './assistants';
|
||||
|
||||
|
|
@ -381,6 +381,43 @@ describe('journal sent to the browser', () => {
|
|||
expect(JOURNAL_PAGE_SIZE).toBe(50);
|
||||
});
|
||||
|
||||
it('reads back the page it sent, and leaves behind what a line does not name', () => {
|
||||
const view = journalPageView(page);
|
||||
expect(readJournalPage(JSON.parse(JSON.stringify(view)))).toEqual(view);
|
||||
expect(readJournalPage({ actions: [], nextCursor: null })).toEqual({ actions: [], nextCursor: null });
|
||||
const padded = {
|
||||
...view,
|
||||
extra: 'not a field',
|
||||
actions: view.actions.map((line) => ({ ...line, entityId: 'not a field', nested: { deep: true } })),
|
||||
};
|
||||
expect(readJournalPage(padded)).toEqual(view);
|
||||
expect(readJournalPage({ actions: [{ ...view.actions[0], entityName: null }], nextCursor: null })?.actions[0].entityName).toBeNull();
|
||||
});
|
||||
|
||||
// What must never reach the JSX or the date formatter.
|
||||
const LINE = journalPageView(page).actions[0];
|
||||
it.each([
|
||||
['nothing', null],
|
||||
['a text', 'page'],
|
||||
['a list', [LINE]],
|
||||
['the body of an error', { error: 'Too many requests' }],
|
||||
['no cursor at all', { actions: [LINE] }],
|
||||
['a cursor that is a number', { actions: [LINE], nextCursor: 5 }],
|
||||
['actions that are not a list', { actions: { 0: LINE }, nextCursor: null }],
|
||||
['a line that is a text', { actions: [LINE, 'line'], nextCursor: null }],
|
||||
['a line that is null', { actions: [null], nextCursor: null }],
|
||||
['a line without an id', { actions: [{ ...LINE, id: undefined }], nextCursor: null }],
|
||||
['a tool that is a number', { actions: [{ ...LINE, tool: 7 }], nextCursor: null }],
|
||||
['a token name that is an object', { actions: [{ ...LINE, tokenName: { name: 'Hermes' } }], nextCursor: null }],
|
||||
['an item name that is an object', { actions: [{ ...LINE, entityName: { title: 'x' } }], nextCursor: null }],
|
||||
['an item name that is missing', { actions: [{ ...LINE, entityName: undefined }], nextCursor: null }],
|
||||
['a revoked flag that is a text', { actions: [{ ...LINE, tokenRevoked: 'false' }], nextCursor: null }],
|
||||
['a date that is a number', { actions: [{ ...LINE, createdAt: 1794670330123 }], nextCursor: null }],
|
||||
['a date that is not one', { actions: [{ ...LINE, createdAt: 'yesterday' }], nextCursor: null }],
|
||||
])('refuses an answer with %s', (_label, value) => {
|
||||
expect(readJournalPage(value)).toBeNull();
|
||||
});
|
||||
|
||||
it('a cursor survives the round trip, to the millisecond', () => {
|
||||
const cursor = { createdAt: new Date('2026-11-14T15:32:10.123Z'), id: ID };
|
||||
const encoded = encodeJournalCursor(cursor);
|
||||
|
|
|
|||
|
|
@ -208,6 +208,30 @@ export function journalPageView(page: JournalPage): JournalPageView {
|
|||
};
|
||||
}
|
||||
|
||||
// The answer of GET /api/agent-actions as the journal component may use it,
|
||||
// or null when it does not have the shape of a page. Each field is read as
|
||||
// the scalar it must be and nothing else of the answer is kept: what is not
|
||||
// a text never reaches the JSX, and a date that is not one never reaches the
|
||||
// formatter, which would throw.
|
||||
export function readJournalPage(value: unknown): JournalPageView | null {
|
||||
if (value === null || typeof value !== 'object') return null;
|
||||
const { actions, nextCursor } = value as { actions?: unknown; nextCursor?: unknown };
|
||||
if (!Array.isArray(actions)) return null;
|
||||
if (nextCursor !== null && typeof nextCursor !== 'string') return null;
|
||||
|
||||
const lines: JournalLine[] = [];
|
||||
for (const action of actions as unknown[]) {
|
||||
if (action === null || typeof action !== 'object') return null;
|
||||
const { id, tool, tokenName, tokenRevoked, entityName, createdAt } = action as Record<string, unknown>;
|
||||
if (typeof id !== 'string' || typeof tool !== 'string' || typeof tokenName !== 'string') return null;
|
||||
if (typeof tokenRevoked !== 'boolean') return null;
|
||||
if (entityName !== null && typeof entityName !== 'string') return null;
|
||||
if (typeof createdAt !== 'string' || Number.isNaN(Date.parse(createdAt))) return null;
|
||||
lines.push({ id, tool, tokenName, tokenRevoked, entityName, createdAt });
|
||||
}
|
||||
return { actions: lines, nextCursor };
|
||||
}
|
||||
|
||||
// The cursor of listActions (agentJournal.ts) as a query parameter: the
|
||||
// instant in milliseconds since the epoch, an underscore, the id of the line.
|
||||
const CURSOR = /^(0|[1-9]\d{0,14})_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue