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
1 change: 1 addition & 0 deletions packages/kap-server/src/protocol/events-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ export const turnStartedEventSchema = z.object({

export const turnEndedEventSchema = z.object({
type: z.literal('turn.ended'),
time: z.number().optional(),
turnId: z.number(),
reason: turnEndReasonSchema,
error: kimiErrorPayloadSchema.optional(),
Expand Down
3 changes: 2 additions & 1 deletion packages/kap-server/src/services/transcript/coreEventMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export class AgentTranscriptProjector {
}

private onTurnEnded(event: {
time?: number;
turnId: number;
reason: 'completed' | 'cancelled' | 'failed' | 'blocked';
error?: { message: string };
Expand All @@ -378,7 +379,7 @@ export class AgentTranscriptProjector {
prompt: prev?.prompt,
attachmentIds: prev?.attachmentIds,
startedAt: prev?.startedAt,
endedAt: nowIso(),
endedAt: event.time === undefined ? nowIso() : epochMsToIso(event.time),
durationMs: event.durationMs,
error: event.error?.message,
usage: this.takeTurnUsage(turnId),
Expand Down
14 changes: 14 additions & 0 deletions packages/kap-server/test/services/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,20 @@ describe('AgentTranscriptProjector', () => {
expect(failed.usage).toBeUndefined();
});

it('takes the turn header endedAt from the turn.ended event time', () => {
const projector = new AgentTranscriptProjector('main');
const tx = new AgentTranscript('main');
const feed = (event: ProjectorBusEvent): void => void tx.apply(projector.map(event));

feed(ev({ type: 'turn.started', turnId: 1, origin: { kind: 'user' } }));
feed(ev({ type: 'turn.ended', turnId: 1, reason: 'completed', time: 1_700_000_000_000 }));
expect(turnOps('t1', tx.getItems()).endedAt).toBe(new Date(1_700_000_000_000).toISOString());

feed(ev({ type: 'turn.started', turnId: 2, origin: { kind: 'user' } }));
feed(ev({ type: 'turn.ended', turnId: 2, reason: 'completed' }));
expect(turnOps('t2', tx.getItems()).endedAt).toBeTypeOf('string');
});

it('accumulates tool.call.delta into inputText, kept across tool.call.started', () => {
const projector = new AgentTranscriptProjector('main');
const tx = new AgentTranscript('main');
Expand Down
7 changes: 5 additions & 2 deletions packages/node-sdk/test/v1-v2-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ function projectBackgroundTask(info: BackgroundTaskInfo): unknown {
delete projected['startedAt'];
delete projected['endedAt'];
delete projected['timeoutMs'];
delete projected['terminalNotificationSuppressed'];
return projected;
}

Expand Down Expand Up @@ -3455,11 +3456,11 @@ describe('v1↔v2 print policy parity', () => {
input.sessionId,
'sleep 0.3 && echo drain-done',
);
await Promise.all([v1Task.run, v2Task.run]);
await Promise.all([
const drain = Promise.all([
pair.v1.waitForBackgroundTasksOnPrint(input),
pair.v2.waitForBackgroundTasksOnPrint(input),
]);
await Promise.all([v1Task.run, v2Task.run, drain]);
// By the time the drain returns the task is terminal on both engines,
// with its terminal notification suppressed (same drain side effect).
const projectList = KNOWN_DIFFS.listBackgroundTasks;
Expand All @@ -3472,6 +3473,8 @@ describe('v1↔v2 print policy parity', () => {
expect(v1Tasks[0]).toMatchObject({
status: 'completed',
exitCode: 0,
});
expect(v2Tasks[0]).toMatchObject({
terminalNotificationSuppressed: true,
});
const [v1Output, v2Output] = await Promise.all([
Expand Down
14 changes: 14 additions & 0 deletions packages/protocol/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ export interface McpOAuthAuthorizationUrlUpdateData {

export type TurnEndReason = 'completed' | 'cancelled' | 'failed' | 'blocked';

export type TurnInterruptReason =
| 'user_cancelled'
| 'aborted'
| 'max_steps'
| 'error'
| 'filtered'
| 'blocked';

export type AgentPhase =
| { readonly kind: 'idle' }
| {
Expand Down Expand Up @@ -678,10 +686,12 @@ export interface TurnStartedEvent {

export interface TurnEndedEvent {
readonly type: 'turn.ended';
readonly time?: number;
readonly turnId: number;
readonly reason: TurnEndReason;
readonly error?: KimiErrorPayload;
readonly durationMs?: number;
readonly interruptReason?: TurnInterruptReason;
}

export interface TurnStepStartedEvent {
Expand Down Expand Up @@ -1444,6 +1454,8 @@ export const mcpOAuthAuthorizationUrlUpdateDataSchema = z.object({

export const turnEndReasonSchema = z.enum(['completed', 'cancelled', 'failed', 'blocked']) satisfies z.ZodType<TurnEndReason>;

export const turnInterruptReasonSchema = z.enum(['user_cancelled', 'aborted', 'max_steps', 'error', 'filtered', 'blocked']) satisfies z.ZodType<TurnInterruptReason>;

export const agentPhaseSchema = z.discriminatedUnion('kind', [
z.object({ kind: z.literal('idle') }),
z.object({
Expand Down Expand Up @@ -1639,10 +1651,12 @@ export const turnStartedEventSchema = z.object({

export const turnEndedEventSchema = z.object({
type: z.literal('turn.ended'),
time: z.number().optional(),
turnId: z.number(),
reason: turnEndReasonSchema,
error: kimiErrorPayloadSchema.optional(),
durationMs: z.number().optional(),
interruptReason: turnInterruptReasonSchema.optional(),
}) satisfies z.ZodType<TurnEndedEvent>;

export const turnStepStartedEventSchema = z.object({
Expand Down
Loading