feat(cowork-service): prefer canonical audit event ids
This commit is contained in:
parent
69bced0a25
commit
a57b367fe5
@ -164,6 +164,7 @@ describe('agent runtime routes', () => {
|
|||||||
action: 'approval_granted',
|
action: 'approval_granted',
|
||||||
timestamp: '2026-04-04T09:00:00.000Z',
|
timestamp: '2026-04-04T09:00:00.000Z',
|
||||||
details: {
|
details: {
|
||||||
|
eventId: 'evt_cowork_approval_1',
|
||||||
taskId: 'task-1',
|
taskId: 'task-1',
|
||||||
sessionId: 'sess-1',
|
sessionId: 'sess-1',
|
||||||
tool: 'rm',
|
tool: 'rm',
|
||||||
@ -182,7 +183,7 @@ describe('agent runtime routes', () => {
|
|||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
const body = JSON.parse(res.payload);
|
const body = JSON.parse(res.payload);
|
||||||
expect(body.approvals[0]).toMatchObject({
|
expect(body.approvals[0]).toMatchObject({
|
||||||
approvalId: 'audit-1',
|
approvalId: 'evt_cowork_approval_1',
|
||||||
sessionId: 'sess-1',
|
sessionId: 'sess-1',
|
||||||
runId: 'task-1',
|
runId: 'task-1',
|
||||||
status: 'approved',
|
status: 'approved',
|
||||||
@ -201,6 +202,7 @@ describe('agent runtime routes', () => {
|
|||||||
category: 'execution',
|
category: 'execution',
|
||||||
timestamp: '2026-04-04T09:05:00.000Z',
|
timestamp: '2026-04-04T09:05:00.000Z',
|
||||||
details: {
|
details: {
|
||||||
|
eventId: 'evt_cowork_action_1',
|
||||||
taskId: 'task-1',
|
taskId: 'task-1',
|
||||||
sessionId: 'sess-1',
|
sessionId: 'sess-1',
|
||||||
correlationId: 'corr-1',
|
correlationId: 'corr-1',
|
||||||
@ -215,7 +217,7 @@ describe('agent runtime routes', () => {
|
|||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
const body = JSON.parse(res.payload);
|
const body = JSON.parse(res.payload);
|
||||||
expect(body.actions[0]).toMatchObject({
|
expect(body.actions[0]).toMatchObject({
|
||||||
actionLogId: 'audit-2',
|
actionLogId: 'evt_cowork_action_1',
|
||||||
sessionId: 'sess-1',
|
sessionId: 'sess-1',
|
||||||
runId: 'task-1',
|
runId: 'task-1',
|
||||||
eventName: 'task_completed',
|
eventName: 'task_completed',
|
||||||
|
|||||||
@ -168,6 +168,19 @@ function mapResolverSurface(surface: unknown): 'mobile' | 'web' | 'desktop' | nu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCanonicalAuditEventId(
|
||||||
|
record: Record<string, unknown>,
|
||||||
|
details: Record<string, unknown>
|
||||||
|
): string | null {
|
||||||
|
if (typeof details.eventId === 'string' && details.eventId.length > 0) {
|
||||||
|
return details.eventId;
|
||||||
|
}
|
||||||
|
if (typeof record.id === 'string' && record.id.length > 0) {
|
||||||
|
return record.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
function toApprovalCheckpoint(
|
function toApprovalCheckpoint(
|
||||||
record: Record<string, unknown>,
|
record: Record<string, unknown>,
|
||||||
fallbackNow: string
|
fallbackNow: string
|
||||||
@ -178,10 +191,10 @@ function toApprovalCheckpoint(
|
|||||||
: {};
|
: {};
|
||||||
const status = mapApprovalStatus(record.action);
|
const status = mapApprovalStatus(record.action);
|
||||||
const requestedAt = asIsoString(record.timestamp ?? record.createdAt, fallbackNow);
|
const requestedAt = asIsoString(record.timestamp ?? record.createdAt, fallbackNow);
|
||||||
|
const canonicalEventId = getCanonicalAuditEventId(record, details);
|
||||||
|
|
||||||
return AgentApprovalCheckpointSchema.parse({
|
return AgentApprovalCheckpointSchema.parse({
|
||||||
approvalId:
|
approvalId: canonicalEventId ?? `approval_${fallbackNow}`,
|
||||||
typeof record.id === 'string' && record.id.length > 0 ? record.id : `approval_${fallbackNow}`,
|
|
||||||
sessionId:
|
sessionId:
|
||||||
typeof details.sessionId === 'string' && details.sessionId.length > 0
|
typeof details.sessionId === 'string' && details.sessionId.length > 0
|
||||||
? details.sessionId
|
? details.sessionId
|
||||||
@ -226,6 +239,7 @@ function toActionLog(record: Record<string, unknown>, fallbackNow: string): Agen
|
|||||||
record.details && typeof record.details === 'object'
|
record.details && typeof record.details === 'object'
|
||||||
? (record.details as Record<string, unknown>)
|
? (record.details as Record<string, unknown>)
|
||||||
: {};
|
: {};
|
||||||
|
const canonicalEventId = getCanonicalAuditEventId(record, details);
|
||||||
const sessionId =
|
const sessionId =
|
||||||
typeof details.sessionId === 'string' && details.sessionId.length > 0
|
typeof details.sessionId === 'string' && details.sessionId.length > 0
|
||||||
? details.sessionId
|
? details.sessionId
|
||||||
@ -240,8 +254,7 @@ function toActionLog(record: Record<string, unknown>, fallbackNow: string): Agen
|
|||||||
: 'unknown-run';
|
: 'unknown-run';
|
||||||
|
|
||||||
return AgentActionLogSchema.parse({
|
return AgentActionLogSchema.parse({
|
||||||
actionLogId:
|
actionLogId: canonicalEventId ?? `action_${fallbackNow}`,
|
||||||
typeof record.id === 'string' && record.id.length > 0 ? record.id : `action_${fallbackNow}`,
|
|
||||||
sessionId,
|
sessionId,
|
||||||
runId,
|
runId,
|
||||||
eventName:
|
eventName:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user