mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-16 08:25:14 +02:00
* feat(cli): profile ingest runs to find where wall-clock time goes Add opt-in profiling for `ktx ingest`. Each timed phase, work unit, and agent loop now records durationMs / step count / token usage in the trace, and a post-run aggregator rolls them up into a "where did the time go" report printed to stderr. Enable per run with KTX_PROFILE_INGEST (1/true -> human table, json -> raw structured profile) or persistently via `ingest.profile` in ktx.yaml. The json form emits raw milliseconds, token counts, and a summary.headline one-line diagnosis so coding agents can parse it directly; json wins when both env and config request profiling. - runtime-port: RunLoopMetrics (totalMs, usage, stepCount, stepBoundariesMs) plus onMetrics callbacks on text/object generation - ai-sdk + claude-code runtimes: capture per-loop timing and token usage - work-unit-executor and stages 3/4: thread metrics into trace events - ingest-bundle.runner: time worktree / triage / clustering / index / reconcile / squash phases and emit the profile in a finally block (best-effort; never affects the run outcome) - ingest-profile: new trace+transcript aggregator with table/json formatters - config: ingest.profile flag; docs: profiling section in ktx-ingest.mdx * fix(cli): flush tool-call logs before reading ingest profile Tool transcripts are appended fire-and-forget so the agent hot path never blocks on logging. The ingest profiler read them before the writes settled, so per-work-unit toolMs (and the model-vs-tool split derived from it) could be incomplete. Track in-flight appends and expose flushToolCallLogs() — bounded by a timeout so it can never hang — and flush before the profiler reads the transcript.
97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import type { KtxModelRole } from '../../llm/types.js';
|
|
import type { z } from 'zod';
|
|
|
|
export interface KtxRuntimeToolOutput<TOutput = unknown> {
|
|
markdown: string;
|
|
structured?: TOutput;
|
|
}
|
|
|
|
export interface KtxRuntimeToolDescriptor<TInput = unknown, TOutput = unknown> {
|
|
name: string;
|
|
description: string;
|
|
inputSchema: z.ZodObject<z.ZodRawShape>;
|
|
execute(input: TInput): Promise<KtxRuntimeToolOutput<TOutput>>;
|
|
}
|
|
|
|
export type KtxRuntimeToolSet = Record<string, KtxRuntimeToolDescriptor>;
|
|
|
|
export type RunLoopStopReason = 'budget' | 'natural' | 'error';
|
|
|
|
/** @internal */
|
|
export interface RunLoopStepInfo {
|
|
stepIndex: number;
|
|
stepBudget: number;
|
|
}
|
|
|
|
export interface LlmTokenUsage {
|
|
inputTokens?: number;
|
|
outputTokens?: number;
|
|
totalTokens?: number;
|
|
}
|
|
|
|
/** Timing and token metrics for a multi-step agent loop, used for ingest profiling. */
|
|
export interface RunLoopMetrics {
|
|
/** Wall-clock time around the whole `generateText` call, in milliseconds. */
|
|
totalMs: number;
|
|
/** Aggregate token usage across all steps. */
|
|
usage: LlmTokenUsage;
|
|
/** Number of agent steps (model round-trips) that actually ran. */
|
|
stepCount: number;
|
|
/** Wall-clock offset (ms from loop start) at which each step finished. */
|
|
stepBoundariesMs: number[];
|
|
}
|
|
|
|
export interface RunLoopParams {
|
|
modelRole: KtxModelRole;
|
|
systemPrompt: string;
|
|
userPrompt: string;
|
|
toolSet: KtxRuntimeToolSet;
|
|
stepBudget: number;
|
|
telemetryTags: Record<string, string>;
|
|
onStepFinish?: (info: RunLoopStepInfo) => void | Promise<void>;
|
|
}
|
|
|
|
export interface RunLoopResult {
|
|
stopReason: RunLoopStopReason;
|
|
error?: Error;
|
|
metrics?: RunLoopMetrics;
|
|
}
|
|
|
|
export interface KtxGenerateTextInput {
|
|
role: KtxModelRole;
|
|
prompt: string;
|
|
system?: string;
|
|
tools?: KtxRuntimeToolSet;
|
|
temperature?: number;
|
|
onMetrics?: (metrics: { totalMs: number; usage: LlmTokenUsage }) => void;
|
|
}
|
|
|
|
export interface KtxGenerateObjectInput<TOutput, TSchema extends z.ZodType<TOutput>> {
|
|
role: KtxModelRole;
|
|
prompt: string;
|
|
system?: string;
|
|
tools?: KtxRuntimeToolSet;
|
|
temperature?: number;
|
|
schema: TSchema;
|
|
onMetrics?: (metrics: { totalMs: number; usage: LlmTokenUsage }) => void;
|
|
}
|
|
|
|
export interface KtxLlmRuntimePort {
|
|
generateText(input: KtxGenerateTextInput): Promise<string>;
|
|
generateObject<TOutput, TSchema extends z.ZodType<TOutput>>(
|
|
input: KtxGenerateObjectInput<TOutput, TSchema>,
|
|
): Promise<TOutput>;
|
|
runAgentLoop(params: RunLoopParams): Promise<RunLoopResult>;
|
|
}
|
|
|
|
export interface AgentRunnerPort {
|
|
runLoop(params: RunLoopParams): Promise<RunLoopResult>;
|
|
}
|
|
|
|
export class RuntimeAgentRunner implements AgentRunnerPort {
|
|
constructor(private readonly runtime: KtxLlmRuntimePort) {}
|
|
|
|
runLoop(params: RunLoopParams): Promise<RunLoopResult> {
|
|
return this.runtime.runAgentLoop(params);
|
|
}
|
|
}
|