Skip to content
Open
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
47 changes: 47 additions & 0 deletions packages/worker-utils/src/cloud-agent-queue-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,51 @@ describe('CloudAgentQueueReportSchema', () => {
).success
).toBe(false);
});

it('round-trips a complete reporting anchor for a session without an initial turn', () => {
const parsed = CloudAgentQueueReportSchema.parse({
...reportWithRun({ status: 'queued', queuedAt: '2026-05-26T08:01:00.000Z' }),
session: {
cloudAgentSessionId: 'agent_reporting_session',
kiloSessionId: 'ses_12345678901234567890123456',
initialMessageId: 'msg_anchor_first',
reportingCreatedAt: '2026-05-26T07:59:00.000Z',
},
});

expect(parsed.session).toEqual({
cloudAgentSessionId: 'agent_reporting_session',
kiloSessionId: 'ses_12345678901234567890123456',
initialMessageId: 'msg_anchor_first',
reportingCreatedAt: '2026-05-26T07:59:00.000Z',
});
});

it('keeps the legacy anchor-free session shape valid', () => {
expect(
CloudAgentQueueReportSchema.safeParse({
...reportWithRun({ status: 'queued' }),
session: { cloudAgentSessionId: 'agent_reporting_session' },
}).success
).toBe(true);
});

it('rejects a partial reporting anchor', () => {
for (const partial of [
{ kiloSessionId: 'ses_12345678901234567890123456' },
{ initialMessageId: 'msg_anchor_first' },
{ reportingCreatedAt: '2026-05-26T07:59:00.000Z' },
{
kiloSessionId: 'ses_12345678901234567890123456',
initialMessageId: 'msg_anchor_first',
},
]) {
expect(
CloudAgentQueueReportSchema.safeParse({
...reportWithRun({ status: 'queued' }),
session: { cloudAgentSessionId: 'agent_reporting_session', ...partial },
}).success
).toBe(false);
}
});
});
24 changes: 22 additions & 2 deletions packages/worker-utils/src/cloud-agent-queue-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const DIAGNOSTIC_RETENTION_MS = 30 * 24 * 60 * 60 * 1000;

const IsoTimestampSchema = z.string().datetime({ offset: true });
const OperationalIdentifierSchema = z.string().min(1).max(MAX_OPERATIONAL_IDENTIFIER_LENGTH);
const kiloSessionIdSchema = z.string().startsWith('ses_').length(30);
const WrapperRunIdentifierSchema = OperationalIdentifierSchema.regex(/^wr_[A-Za-z0-9_-]+$/);
const validFailureClassifications = new Set(
CloudAgentRunFailureClassifications.map(
Expand All @@ -59,8 +60,27 @@ const validFailureClassifications = new Set(
);

const CloudAgentQueueSessionIdentitySchema = z
.object({ cloudAgentSessionId: OperationalIdentifierSchema })
.strict();
.object({
cloudAgentSessionId: OperationalIdentifierSchema,
kiloSessionId: kiloSessionIdSchema.optional(),
initialMessageId: OperationalIdentifierSchema.optional(),
reportingCreatedAt: IsoTimestampSchema.optional(),
})
.strict()
.superRefine((session, ctx) => {
const present = [
session.kiloSessionId,
session.initialMessageId,
session.reportingCreatedAt,
].filter(value => value !== undefined).length;
if (present !== 0 && present !== 3) {
ctx.addIssue({
code: 'custom',
message: 'Reporting anchor fields must be provided together',
path: ['kiloSessionId'],
});
}
});

const CloudAgentFailedRunDiagnosticSchema = z
.object({
Expand Down
Loading