mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-21 07:33:14 +01:00
fix(cso): count remainingSeconds to the investigation deadline; bound and validate the tool timeline
remainingSeconds now measures to deadline - 60s, the point where scan, verify and record-review refuse with DEADLINE, and inspect carries it. The post-command clock lookup can only omit the field, never fail a command whose state change is already saved. The stream timeline records end times from tool_result, indexes only recorded calls so it stays at the 4096 cap, and receipts reject malformed timeline entries.
This commit is contained in:
1 parent
6a432f00a0
commit
d804fbb2fc
11 files changed
+90
-34
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { spawn } from 'child_process';
|
||||
import type { RunResult } from './types';
|
||||
import type { RunResult, ToolTimelineEntry } from './types';
|
||||
|
||||
/** Usage, turns and the latest text from `claude -p --output-format stream-json --verbose --include-partial-messages`. */
|
||||
export interface ClaudeStreamState {
|
||||
@@ -15,13 +15,14 @@ export interface ClaudeStreamState {
|
||||
result?: Record<string, unknown>;
|
||||
invalidLines: number;
|
||||
startedAt: number;
|
||||
/** Content-free: each tool call's name, start offset and thread, never its input or output. */
|
||||
toolTimeline: NonNullable<RunResult['toolTimeline']>;
|
||||
toolUseIds: Set<string>;
|
||||
/** Content-free: each tool call's name, start and end offsets and thread, never its input or output. */
|
||||
toolTimeline: ToolTimelineEntry[];
|
||||
/** Timeline index by tool-use id, for recorded calls only, so it stays within the timeline cap. */
|
||||
toolUseIndex: Map<string, number>;
|
||||
}
|
||||
|
||||
export function claudeStreamState(): ClaudeStreamState {
|
||||
return { totals: { input: 0, output: 0, cached: 0 }, open: new Map(), subagents: new Map(), turns: 0, resultTurns: 0, lastText: '', invalidLines: 0, startedAt: Date.now(), toolTimeline: [], toolUseIds: new Set() };
|
||||
return { totals: { input: 0, output: 0, cached: 0 }, open: new Map(), subagents: new Map(), turns: 0, resultTurns: 0, lastText: '', invalidLines: 0, startedAt: Date.now(), toolTimeline: [], toolUseIndex: new Map() };
|
||||
}
|
||||
|
||||
const TOOL_TIMELINE_LIMIT = 4096;
|
||||
@@ -29,11 +30,8 @@ const TOOL_TIMELINE_LIMIT = 4096;
|
||||
const count = (value: unknown): number => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : 0;
|
||||
|
||||
function recordToolUse(state: ClaudeStreamState, block: { id?: unknown; name?: unknown }, agent: 'main' | 'subagent'): void {
|
||||
if (typeof block.id === 'string') {
|
||||
if (state.toolUseIds.has(block.id)) return;
|
||||
state.toolUseIds.add(block.id);
|
||||
}
|
||||
if (state.toolTimeline.length >= TOOL_TIMELINE_LIMIT) return;
|
||||
if (typeof block.id !== 'string' || state.toolUseIndex.has(block.id) || state.toolTimeline.length >= TOOL_TIMELINE_LIMIT) return;
|
||||
state.toolUseIndex.set(block.id, state.toolTimeline.length);
|
||||
state.toolTimeline.push({
|
||||
atMs: Date.now() - state.startedAt,
|
||||
tool: typeof block.name === 'string' && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(block.name) ? block.name : 'other',
|
||||
@@ -41,6 +39,11 @@ function recordToolUse(state: ClaudeStreamState, block: { id?: unknown; name?: u
|
||||
});
|
||||
}
|
||||
|
||||
function recordToolResult(state: ClaudeStreamState, block: { tool_use_id?: unknown }): void {
|
||||
const index = typeof block.tool_use_id === 'string' ? state.toolUseIndex.get(block.tool_use_id) : undefined;
|
||||
if (index !== undefined && state.toolTimeline[index].endMs === undefined) state.toolTimeline[index].endMs = Date.now() - state.startedAt;
|
||||
}
|
||||
|
||||
function close(state: ClaudeStreamState, key: string): void {
|
||||
const message = state.open.get(key);
|
||||
if (!message) return;
|
||||
@@ -61,6 +64,10 @@ export function consumeClaudeStreamLine(state: ClaudeStreamState, line: string):
|
||||
if (event.type === 'assistant' && Array.isArray(event.message?.content)) {
|
||||
for (const block of event.message.content) if (block?.type === 'tool_use') recordToolUse(state, block, typeof event.parent_tool_use_id === 'string' ? 'subagent' : 'main');
|
||||
}
|
||||
if (event.type === 'user' && Array.isArray(event.message?.content)) {
|
||||
for (const block of event.message.content) if (block?.type === 'tool_result') recordToolResult(state, block);
|
||||
return;
|
||||
}
|
||||
if (event.type === 'assistant' && typeof event.parent_tool_use_id === 'string') {
|
||||
const usage = event.message?.usage;
|
||||
if (typeof event.message?.id === 'string' && usage) state.subagents.set(event.message.id, { input: count(usage.input_tokens), output: count(usage.output_tokens), cached: count(usage.cache_read_input_tokens) });
|
||||
|
||||
@@ -49,6 +49,14 @@ export type RunError =
|
||||
| 'binary_missing' // CLI not found on PATH.
|
||||
| 'unknown'; // Catch-all with reason populated.
|
||||
|
||||
export interface ToolTimelineEntry {
|
||||
atMs: number;
|
||||
/** Set when the call's tool_result arrived; absent for a call still running when the stream ended. */
|
||||
endMs?: number;
|
||||
tool: string;
|
||||
agent: 'main' | 'subagent';
|
||||
}
|
||||
|
||||
export interface RunResult {
|
||||
/** Provider's textual output for the prompt. */
|
||||
output: string;
|
||||
@@ -58,8 +66,8 @@ export interface RunResult {
|
||||
durationMs: number;
|
||||
/** Count of tool/function calls made during the run (0 if unsupported). */
|
||||
toolCalls: number;
|
||||
/** Tool-call starts in order: name, offset from run start, and thread; no inputs or outputs. Unset if unsupported. */
|
||||
toolTimeline?: Array<{ atMs: number; tool: string; agent: 'main' | 'subagent' }>;
|
||||
/** Tool calls in start order: name, start and end offsets from run start, and thread; no inputs or outputs. Unset if unsupported. */
|
||||
toolTimeline?: ToolTimelineEntry[];
|
||||
/** Actual model ID the provider reports using (may be a variant of the family). */
|
||||
modelUsed: string;
|
||||
/** If the run failed, error code + human reason. output/tokens may be partial. */
|
||||
|
||||
Reference in new issue
Block a user