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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('generated artifact visibility', () => {
'synthesis_cache_block',
'history_compact_block',
'history_compact_source',
'provider_request_capture',
'user_upload',
];

Expand Down
21 changes: 21 additions & 0 deletions apps/desktop/src/main/session-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
buildLlmHistorySummarizer,
buildMcpTools,
buildProviderOptions,
createProviderRequestCaptureRecorder,
getAIModel,
loadHistoryCompactBlocksFromArtifacts,
loadSynthesisCacheBlocksFromArtifacts,
Expand Down Expand Up @@ -38,6 +39,7 @@ import {
createAttachmentByteReader,
createTelemetryRepo,
openRuntimeEventPersistence,
persistProviderRequestCaptureArtifact,
} from '@maka/storage';
import { WEB_SEARCH_TOOL_NAME } from './web-search/agent-tool.js';
import {
Expand Down Expand Up @@ -252,6 +254,25 @@ export function createAiSdkBackendFactory(deps: AiSdkBackendFactoryDeps): Backen
},
}),
recordRunTrace: ctx.recordRunTrace,
...(ctx.recordProviderRequestCapture
? {
recordProviderRequestCapture: createProviderRequestCaptureRecorder({
persistArtifact: async (capture) => {
const artifact = await persistProviderRequestCaptureArtifact(artifactStore, {
sessionId: ctx.sessionId,
turnId: capture.turnId,
captureId: capture.captureId,
step: capture.step,
serializedRequest: capture.serializedRequest,
now: Date.now(),
});
return { artifactId: artifact.id };
},
recordLedger: ctx.recordProviderRequestCapture,
}),
recordProviderRequestAttempt: ctx.recordProviderRequestAttempt,
}
: {}),
recordHistoryCompactCheckpoint: ctx.recordHistoryCompactCheckpoint,
loadTurnRuntimeEvents: ctx.loadTurnRuntimeEvents,
recordActiveFullCompactBlock: ctx.recordActiveFullCompactBlock,
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/renderer/artifact-visibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const USER_VISIBLE_ARTIFACT_SOURCES = {
synthesis_cache_block: false,
history_compact_block: false,
history_compact_source: false,
provider_request_capture: false,
user_upload: false,
export: true,
snapshot: true,
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/runtime-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
buildRuntimeEventModelReplayPlan,
buildChildAgentTools,
createBuiltinSandboxManager,
createProviderRequestCaptureRecorder,
createFilesystemWorkerLaunchSpecProvider,
createLocalContinuationSafetyInspector,
FilesystemWorkerClient,
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
createSettingsStore,
createShellRunStore,
type ForeignSessionStore,
persistProviderRequestCaptureArtifact,
} from '@maka/storage';
import type { ToolPermissionRule } from '@maka/core/permission';
import { fetchProviderModels } from '@maka/runtime';
Expand Down Expand Up @@ -631,6 +633,25 @@ export async function createMakaCliRuntimeContext(
turnTailPrompt: ({ cwd }) =>
buildCliTurnTailPrompt({ cwd, sessionId: ctx.sessionId, automationManager, goalManager }),
shellRunContextSummary: ctx.shellRunContextSummary,
...(ctx.recordProviderRequestCapture
? {
recordProviderRequestCapture: createProviderRequestCaptureRecorder({
persistArtifact: async (capture) => {
const artifact = await persistProviderRequestCaptureArtifact(artifactStore, {
sessionId: ctx.sessionId,
turnId: capture.turnId,
captureId: capture.captureId,
step: capture.step,
serializedRequest: capture.serializedRequest,
now: Date.now(),
});
return { artifactId: artifact.id };
},
recordLedger: ctx.recordProviderRequestCapture,
}),
recordProviderRequestAttempt: ctx.recordProviderRequestAttempt,
}
: {}),
newId: randomUUID,
now: Date.now,
...(input.maxSteps !== undefined ? { maxSteps: input.maxSteps } : {}),
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/__tests__/agent-run-provider-request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';

import { decodeAgentRunEvent } from '../agent-run.js';

test('AgentRun accepts provider request capture and attempt trace rows', () => {
for (const type of ['provider_request_captured', 'provider_request_attempt_recorded']) {
const decoded = decodeAgentRunEvent({
type,
id: `${type}-1`,
runId: 'run-1',
sessionId: 'session-1',
turnId: 'turn-1',
ts: 1,
data: { traceId: 'provider-trace-1' },
});
assert.equal(decoded.type, type);
}
});
13 changes: 13 additions & 0 deletions packages/core/src/__tests__/runtime-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import { expect } from '../test-helpers.js';
import {
RUNTIME_EVENT_AUTHORS,
Expand All @@ -7,6 +8,7 @@ import {
RUNTIME_EVENT_STATUSES,
TERMINAL_RUNTIME_EVENT_STATUSES,
createRuntimeEventId,
decodeRuntimeEvent,
isRuntimeEventAuthor,
isRuntimeEventRole,
isRuntimeEventStatus,
Expand Down Expand Up @@ -312,6 +314,17 @@ describe('createRuntimeEventId', () => {
});

describe('RuntimeEvent shape compile-time contract', () => {
test('accepts a provider-request trace reference and rejects a non-string reference', () => {
const event = baseEvent({ refs: { providerRequestTraceId: 'provider-trace-1' } });
expect(decodeRuntimeEvent(event).refs?.providerRequestTraceId).toBe('provider-trace-1');
assert.throws(() =>
decodeRuntimeEvent({
...event,
refs: { providerRequestTraceId: 123 },
}),
);
});

test('a full user event satisfies the type', () => {
const event: RuntimeEvent = {
id: 'evt-u1',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/agent-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export const AGENT_RUN_EVENT_TYPES = [
'sandbox_escalation_failed',
'sandbox_denial_detected',
'usage_recorded',
'provider_request_captured',
'provider_request_attempt_recorded',
'history_compact_checkpoint_recorded',
'active_full_compact_block_recorded',
'semantic_compact_block_recorded',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type ArtifactSource =
| 'synthesis_cache_block'
| 'history_compact_block'
| 'history_compact_source'
| 'provider_request_capture'
| 'user_upload'
| 'export'
| 'snapshot'
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,8 @@ export interface TokenUsageEvent extends BaseEvent {
requestShapeChangeReason?: PrefixChangeReason;
promptSegments?: PromptSegmentEstimate[];
contextBudget?: ContextBudgetDiagnostic;
/** Links this aggregate to per-physical-request AgentRun trace rows. */
providerRequestTraceId?: string;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/runtime-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ export interface RuntimeEventRefs {
traceEventId?: string;
toolCallId?: string;
providerEventId?: string;
/** Trace-group id linking aggregate usage to physical provider attempts. */
providerRequestTraceId?: string;
artifactId?: string;
/** Runtime-owned durable identity for one tool side-effect boundary. */
operationId?: string;
Expand Down Expand Up @@ -453,6 +455,7 @@ const RUNTIME_REFS_SHAPE = defineObjectShape<RuntimeEventRefs>()(
'traceEventId',
'toolCallId',
'providerEventId',
'providerRequestTraceId',
'artifactId',
'operationId',
'stepId',
Expand Down Expand Up @@ -594,6 +597,7 @@ function isRuntimeEventRefs(value: unknown): value is RuntimeEventRefs {
value.traceEventId,
value.toolCallId,
value.providerEventId,
value.providerRequestTraceId,
value.artifactId,
value.operationId,
value.stepId,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ export interface TokenUsageMessage {
requestShapeChangeReason?: PrefixChangeReason;
promptSegments?: PromptSegmentEstimate[];
contextBudget?: ContextBudgetDiagnostic;
providerRequestTraceId?: string;
}

export interface TurnStateMessage {
Expand Down Expand Up @@ -424,6 +425,7 @@ const TOKEN_USAGE_MESSAGE_SHAPE = defineObjectShape<TokenUsageMessage>()(
'requestShapeChangeReason',
'promptSegments',
'contextBudget',
'providerRequestTraceId',
],
);
const TURN_STATE_MESSAGE_SHAPE = defineObjectShape<TurnStateMessage>()(
Expand Down Expand Up @@ -540,7 +542,8 @@ function decodeStoredMessage(
if (
hasExactShape(message, TOKEN_USAGE_MESSAGE_SHAPE) &&
hasMessageEnvelope(message, true) &&
isTokenUsageFields(message)
isTokenUsageFields(message) &&
isOptionalString(message.providerRequestTraceId)
)
return message as unknown as TokenUsageMessage;
break;
Expand Down
Loading
Loading