feat(context): audit tool visibility and isolate diagnostic history

This commit is contained in:
Agent Workbench Dev
2026-11-06 12:22:37 +08:00
parent b14e26e665
commit a6e31dec9d
18 changed files with 420 additions and 36 deletions
+16
View File
@@ -6,6 +6,10 @@ import type { ContextHistory, ContextHistoryGroup, ContextPlan, Session } from '
const number = (value: number | null | undefined) => (value ?? 0).toLocaleString('zh-CN');
const states: Record<string, string> = { prepared: '已准备', requesting: '正在请求', completed: '已收到响应', failed: '请求失败', interrupted: '请求已中断' };
const toolReasons: Record<string, string> = { visible: '本轮可见', not_assembled: '未装配', missing_current_resource: '缺少当前任务资源', disabled: '已停用',
disabled_by_policy: '当前策略未启用', disabled_by_execution_mode: '当前执行方式未启用', disabled_by_worker_contract: '子任务范围外', disabled_by_task_contract: '当前任务约定不执行', not_assembled_for_worker: '子任务未装配' };
const changeLabel = (path: string) => path === 'tools' ? '工具定义' : path === 'model' ? '请求型号' : path === 'system' ? '系统规则'
: path.startsWith('messages[') || path.startsWith('input[') ? '消息段 ' + (Number(path.match(/\[(\d+)\]/)?.[1]) + 1) : path;
const toolNames: Record<string, string> = { read_file: '读取文件', read_artifact: '读取保存工件', inspect_project: '识别工程', inspect_session: '核对运行记录',
index_project: '建立符号索引', query_code: '定位代码', propose_patch: '精确补丁提案', propose_file: '文件提案', run_command: '终端命令', run_checks: '工程检查',
plan_checks: '检查计划', prepare_change_set: '封存变更集', plan_delivery: 'Git交付预览', delivery_report: '交付报告', list_files: '查看文件', search_files: '搜索文件' };
@@ -28,6 +32,8 @@ export function RequestContextPanel({ session, runId, openArtifact, onError }: {
: historical.filter(plan => plan.runId === runId && plan.sessionId === session.id);
const plan = plans.find(plan => plan.id === selected) ?? plans.at(-1);
if (!plan) return null;
const observation = plan.requestObservation, surface = observation?.toolSurface;
const userMessage = observation?.userMessageInstances.filter(message => message.origin === 'user').at(-1);
const total = plan.estimatedInputTokens + (plan.budget.outputReservation ?? 0) + plan.budget.safetyTokens;
return <section className="task-record-section request-context" aria-label="请求上下文">
<div className="record-heading"><BrainCircuit size={15} /><h3>请求上下文</h3><span>{states[plan.status] ?? plan.status}</span></div>
@@ -40,11 +46,21 @@ export function RequestContextPanel({ session, runId, openArtifact, onError }: {
<dt>历史记录</dt><dd>保留 {plan.retainedMessages} 条 · 保存 {plan.archivedGroups} 组原记录</dd>
<dt>上游缓存命中</dt><dd>{plan.actualUsage?.prompt_tokens_details?.cached_tokens === undefined ? '上游未报告' : number(plan.actualUsage.prompt_tokens_details.cached_tokens) + ' tokens'}</dd>
{plan.prefixAudit && <><dt>缓存前缀核对</dt><dd>{plan.prefixAudit.variants.map((variant, index) => <div key={variant.connectionId ?? index}>{variant.model} · {variant.previousPromptBytes === null ? '首次请求或新路线' : '共同前缀 ' + number(variant.commonPrefixBytes) + ' B'}{variant.reason === 'tool_surface_changed' ? ' · 可用工具已变化' : ''}</div>)}<small>共同前缀用于核对稳定性,不等于上游命中。</small></dd></>}
{observation && <><dt>请求归属</dt><dd>{({ main: '主任务', diagnostic: '独立诊断', worker: '子任务' } as Record<string, string>)[observation.path.kind] ?? observation.path.kind}</dd></>}
{userMessage && <><dt>用户消息实例</dt><dd><code title={userMessage.id}>{userMessage.id.slice(0, 12)}</code><small>同样的文字分别记录,不按文本合并。</small></dd></>}
{plan.prefixAudit?.variants.some(variant => variant.firstChange && variant.previousPromptBytes !== null) && <><dt>首次变化位置</dt><dd>{plan.prefixAudit.variants.filter(variant => variant.previousPromptBytes !== null).map((variant, index) => <div key={variant.connectionId ?? index}>{variant.model} · {variant.firstChange ? changeLabel(variant.firstChange.path) : '前缀内容相同'}</div>)}</dd></>}
{!!plan.workingSetEntries && <><dt>当前任务依据</dt><dd>{plan.workingSetEntries} 项代码与执行结果</dd></>}
{!!plan.projectMemoryEntries && <><dt>项目记忆</dt><dd>{plan.projectMemoryEntries} 条来源已核对的记录</dd></>}
{!!plan.skillCatalogEntries && <><dt>技能目录</dt><dd>{plan.skillCatalogEntries} 项本次目录记录</dd></>}
<dt>发送内容</dt><dd>{number(plan.requestBytes)} B<code className="context-sha" title={plan.requestSha256}>{plan.requestSha256.slice(0, 16)}</code></dd>
</dl>
{surface && <details className="context-tool-surface" key={plan.id}><summary>本轮工具 · 可见 {surface.entries.filter(entry => entry.visible).length} / 已登记 {surface.entries.length}</summary>
<p className="record-footnote">这是本次实际发送的工具范围。资源和权限在执行时重新核对;登记不代表本轮可用。</p>
<ul aria-label="本轮工具可见性">{surface.entries.map(entry => <li key={entry.name}>
<div><code>{entry.name}</code><span>{toolReasons[entry.reason] ?? entry.reason}</span></div>
<small title={'schema ' + entry.schemaSha256 + ' · generation ' + entry.generation}>{number(entry.definitionBytes)} B · schema {entry.schemaSha256.slice(0, 12)} · 代次 {entry.generation.slice(0, 8)}</small>
</li>)}</ul>
</details>}
<p className="record-footnote">估算包含系统提示、工具定义和历史。{plan.budget.capacityMode === 'passthrough' ? '本次完整传递历史,不按本地上下文额度裁剪或拒发。' : plan.historyPolicy === 'preserve' ? '本次完整传递历史,容量不足时暂停。' : '任务要求完整保留;整理过的原记录可从本机工件检索。'}实际用量与缓存命中以模型服务为准。</p>
{plan.error && <p className="run-stop">{plan.error}</p>}
<div className="context-actions"><button type="button" className="text-button" onClick={() => openArtifact(plan.requestArtifactId)}><FileText size={13} />查看发送内容</button>
+8
View File
@@ -77,6 +77,14 @@
.context-meter { height: 5px; margin: 15px 0; border-radius: 9px; overflow: hidden; background: #e8eeeb; }
.context-meter i { display: block; height: 100%; background: #56877b; border-radius: inherit; }
.context-sha { display: block; font-size: 10px; color: var(--muted); margin-top: 4px; overflow-wrap: anywhere; }
.context-tool-surface { margin: 12px 0; border-block: 1px solid var(--border); padding: 10px 0; }
.context-tool-surface summary { cursor: pointer; font-size: 12px; font-weight: 600; }
.context-tool-surface ul { list-style: none; margin: 8px 0 0; padding: 0; max-height: 340px; overflow: auto; }
.context-tool-surface li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.context-tool-surface li > div { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; font-size: 12px; }
.context-tool-surface code { overflow-wrap: anywhere; }
.context-tool-surface li small { display: block; margin-top: 4px; color: var(--muted); font-size: 11px; overflow-wrap: anywhere; }
.request-context .record-fields dd small { display: block; margin-top: 4px; }
.context-actions { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.context-actions .text-button { padding-left: 0; }
.context-history-list { flex: 1; min-height: 0; overflow: auto; padding: 1px; }
+7 -1
View File
@@ -185,7 +185,13 @@ export interface ContextPlan {
skillCatalogArtifactId?: string | null; skillCatalogSha256?: string | null; skillCatalogEntries?: number;
actualUsage: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number; prompt_tokens_details?: { cached_tokens?: number; cache_creation_tokens?: number } } | null; requestAttempts: number; error?: string;
historyPolicy?: 'preserve' | 'compact'; contextLayout?: string;
prefixAudit?: { variants: { connectionId: string | null; model: string; commonPrefixBytes: number; previousPromptBytes: number | null; reason: string; toolNames: string[]; toolsSha256: string }[] };
prefixAudit?: { schemaVersion: number; previousPlanId: string | null; variants: { connectionId: string | null; model: string; commonPrefixBytes: number; previousPromptBytes: number | null; reason: string; toolNames: string[]; toolsSha256: string;
firstChange?: { path: string; kind: string; sha256: string | null; previousSha256: string | null } | null;
blocks?: { path: string; role: string | null; sha256: string; bytes: number }[];
}[] };
requestObservation?: { schemaVersion: number; path: { kind: string; key: string }; providerHistorySha256: string;
userMessageInstances: { id: string; providerIndex: number; origin: string; sha256: string }[];
toolSurface: { generation: string; execution: string; entries: { name: string; group: string; registered: boolean; assembled: boolean; visible: boolean; reason: string; schemaSha256: string; definitionSha256: string; definitionBytes: number; generation: string }[] } | null };
budget: { executionMode?: 'unrestricted' | 'restricted';
capacityMode?: 'passthrough'; contextLimit: number | null; outputReservation: number | null; safetyTokens: number; inputBudget: number | null; targetInputTokens: number | null; runRemainingTokens: number | null; taskRemainingTokens: number | null };
}