diff --git a/docs/prd/current/06-diagnostic-agent-tool-runtime.md b/docs/prd/current/06-diagnostic-agent-tool-runtime.md index d935df6..189b647 100644 --- a/docs/prd/current/06-diagnostic-agent-tool-runtime.md +++ b/docs/prd/current/06-diagnostic-agent-tool-runtime.md @@ -693,9 +693,22 @@ interface ToolObservation { 정본 경계에서는 정제되고 정규화된 관찰 결과만 전달한다. 위 `rawResult` 분기는 PR #11 시제품의 진단 Tool message 경로를 설명하며 정본 인계 허용을 뜻하지 않는다. +### 수동 진단 실행 기록 시제품 (#14) + +현재 시제품의 `DiagnosisHandoff`는 AMDC가 순번, `tool_call_id`, 도구 입력, +원천 어댑터의 제한된 결과 또는 정제된 오류를 기록하고, 모델이 기존 호출 ID에 +선택적 `observation`·`hypothesis`·`limitation` comment를 연결한 내부 산출물이다. +실행 기록은 모델이 재작성하지 않으며 미존재·중복·자기 참조 ID를 거부한다. +도구 호출이 없으면 `completion_reason`은 `insufficient_evidence`로 고정한다. +이는 현재 `/diagnose`의 임시 표시 및 JSON 첨부를 위한 시제품 출력이다. +원시 결과를 포함할 수 있으므로 정본 `ReportAgentInputV1`, 정본 Evidence, +영속 Report 또는 영속화된 리포트 전용 Discord 전달에 직접 연결하지 않는다. +그 연결에는 PRD 02/03/06의 정제·정규화·검증 어댑터가 별도로 필요하다. + ### 진단 에이전트 출력 -진단 에이전트는 DiagnosisResult를 반환한다. +아래 `DiagnosisResult`는 정본 인계에 요구되는 계약이다. 현재 수동 진단 +시제품은 위 `DiagnosisHandoff`를 반환하며 이 정본 계약의 구현 완료를 뜻하지 않는다. ~~~ts interface DiagnosisResult { diff --git a/docs/prd/current/README.md b/docs/prd/current/README.md index 9da8db3..fc211d9 100644 --- a/docs/prd/current/README.md +++ b/docs/prd/current/README.md @@ -8,6 +8,8 @@ 리포트 전달 계약 게이트 상태: ready_for_implementation 라이브 소스 게이트 상태: ready_for_design 전달 상태: not_started +수동 진단 시제품: 실행 기록과 선택적 comment를 `DiagnosisHandoff`로 조립한다. +원시 결과를 포함할 수 있어 정본 Report Agent 입력·영속 Report의 완료 증거가 아니다. 아키텍처 결정일: 2026-07-07 최종 검토: 2026-09-11 (리뷰 보정안, 팀 승인 대기) diff --git a/src/adapters/discord/bot.ts b/src/adapters/discord/bot.ts index ae3096f..9ccf001 100644 --- a/src/adapters/discord/bot.ts +++ b/src/adapters/discord/bot.ts @@ -9,7 +9,7 @@ import { import type { AppConfig } from "../../config/env.js"; import { runDiagnosis } from "../../app/run-diagnosis.js"; import { buildDiagnoseCommand } from "./commands.js"; -import { formatDiagnosticPresentation } from "./format-report.js"; +import { formatDiagnosticPresentationMessages } from "./format-report.js"; import { toSafeErrorMetadata, type SafeErrorMetadata @@ -78,7 +78,18 @@ async function handleDiagnoseCommand( } ); - await interaction.editReply(formatDiagnosticPresentation(result.presentation)); + const messages = formatDiagnosticPresentationMessages(result.presentation); + await interaction.editReply({ + content: messages[0], + files: [{ + attachment: Buffer.from(JSON.stringify(result.presentation, null, 2), "utf8"), + name: "amdc-diagnosis.json" + }], + allowedMentions: { parse: [] } + }); + for (const content of messages.slice(1)) { + await interaction.followUp({ content, allowedMentions: { parse: [] } }); + } } catch (error) { const safeError = toSafeDiagnosticError(error); console.error("Failed to handle /diagnose interaction", safeError); diff --git a/src/adapters/discord/format-report.ts b/src/adapters/discord/format-report.ts index 8d59d65..23a6fe6 100644 --- a/src/adapters/discord/format-report.ts +++ b/src/adapters/discord/format-report.ts @@ -1,30 +1,61 @@ import type { DiagnosticPresentation } from "../../report/types.js"; export function formatDiagnosticPresentation(presentation: DiagnosticPresentation): string { - const actions = presentation.recommendedActions - .map((action, index) => `${index + 1}. ${action}`) - .join("\n"); + return truncateDiscordMessage(buildMessage(presentation)); +} + +export function formatDiagnosticPresentationMessages(presentation: DiagnosticPresentation): string[] { + const chunks: string[] = []; + let current = ""; + for (const line of buildMessage(presentation).split("\n")) { + if (current.length + line.length + 1 > 1900 && current) { + chunks.push(current); + current = ""; + } + let rest = line; + while (rest.length > 1900) { + const boundary = rest.lastIndexOf(" ", 1900); + const cut = boundary > 0 ? boundary : 1900; + chunks.push(rest.slice(0, cut)); + rest = rest.slice(cut); + } + current += (current ? "\n" : "") + rest; + } + if (current) chunks.push(current); + return chunks; +} +function buildMessage(presentation: DiagnosticPresentation): string { const message = [ - "## AMDC Diagnostic Agent Result", - "", - `**Status:** ${presentation.status}`, - `**Summary:** ${presentation.summary}`, - `**Suspected Cause:** ${presentation.suspectedCause}`, - "", - "**Recommended Actions**", - actions + "## AMDC 진단 인계 자료", "", + `진단 ID: ${presentation.diagnosis_id}`, + `요청: ${presentation.request}`, + `종료 사유: ${presentation.completion_reason}`, + "전체 구조화 결과: amdc-diagnosis.json 첨부 파일", + ...presentation.observations.flatMap(call => [ + "", `**${call.seq}. ${call.plugin} / ${call.tool}**`, + `호출 ID: ${call.tool_call_id}`, + `조회 시각: ${call.observed_at} · 실행 상태: ${call.status}`, + `입력: ${JSON.stringify(call.input)}`, + `결과: ${resultPreview(call.result)}`, + ...(call.error ? [`실행 오류: ${call.error.code} — ${call.error.message}`] : []), + ...(call.comment?.observation ? [`관찰 comment: ${call.comment.observation}`] : []), + ...(call.comment?.hypothesis ? [`가설 comment: ${call.comment.hypothesis}`] : []), + ...(call.comment?.limitation ? [`한계 comment: ${call.comment.limitation}`] : []), + ...(call.related_call_ids.length ? [`관련 호출: ${call.related_call_ids.join(", ")}`] : []) + ]), + ...(!presentation.observations.length ? ["", "도구 실행 결과 없음. 정상 판정을 의미하지 않습니다."] : []) ].join("\n"); + return message; +} - return truncateDiscordMessage(message); +function resultPreview(result: unknown): string { + const text = JSON.stringify(result); + return text.length <= 600 ? text : text.slice(0, 600) + " … [표시 생략: 전체 결과는 JSON 첨부에 보존]"; } function truncateDiscordMessage(message: string): string { const maxLength = 1900; - - if (message.length <= maxLength) { - return message; - } - - return `${message.slice(0, maxLength)}\n\n...truncated`; + const notice = "\n\n… 표시 길이를 초과했습니다. 전체 조사 내용은 진단 결과에 보존됩니다."; + return message.length <= maxLength ? message : message.slice(0, maxLength - notice.length) + notice; } diff --git a/src/agent/langchain-diagnostic-agent.ts b/src/agent/langchain-diagnostic-agent.ts index cb153d2..150c656 100644 --- a/src/agent/langchain-diagnostic-agent.ts +++ b/src/agent/langchain-diagnostic-agent.ts @@ -8,26 +8,16 @@ import { } from "../observability/diagnostic-trace.js"; import { toSafeErrorMetadata } from "../observability/safe-error-metadata.js"; import { PluginRegistry } from "../tools/plugin-registry.js"; -import type { - ObservationStatus, - RawToolResult, - PluginName, - SanitizedToolError, - ToolObservation, - ToolRuntime -} from "../tools/types.js"; -import { isPluginName } from "../tools/types.js"; +import type { ToolRuntime } from "../tools/types.js"; +import { assertSafeHandoffData, DiagnosisLedger } from "../report/diagnosis-handoff.js"; import { type AgentDiagnosisResult, type DiagnosticAgentInput, - type DiagnosticAgentPort, - type InferredDomain, - type PreliminaryFinding, - type SuspectedCause + type DiagnosticAgentPort } from "./types.js"; import { createAmdcLangChainTools } from "./langchain-tool-wrapper.js"; import { createPluginLazyLoadingMiddleware } from "./plugin-lazy-loading.js"; -import { type DiagnosisDraft, diagnosisDraftSchema } from "./langchain-schemas.js"; +import { diagnosisDraftSchema } from "./langchain-schemas.js"; export interface LangChainDiagnosticAgentOptions { readonly model: string; @@ -44,7 +34,9 @@ export class LangChainDiagnosticAgent implements DiagnosticAgentPort { ) {} async diagnose(input: DiagnosticAgentInput): Promise { - const runId = randomUUID(); + assertSafeHandoffData(input); + const runId = `diag-${randomUUID()}`; + const ledger = new DiagnosisLedger(runId); const startedAt = Date.now(); const traceSink = this.options.traceSink ?? new NoopDiagnosticTraceSink(); const referenceTime = new Date(input.receivedAt); @@ -66,9 +58,10 @@ export class LangChainDiagnosticAgent implements DiagnosticAgentPort { const tools = createAmdcLangChainTools(toolDescriptors, this.toolRuntime, { environment: input.environment, referenceTime, - disabledTools, runId, - traceSink + traceSink, + ledger, + disabledTools }); const pluginMiddleware = createPluginLazyLoadingMiddleware(this.registry, { runId, @@ -108,32 +101,14 @@ export class LangChainDiagnosticAgent implements DiagnosticAgentPort { errorStage = "structured_response_validation"; const draft = diagnosisDraftSchema.parse(result.structuredResponse); errorStage = "diagnosis_assembly"; - const toolArtifacts = extractToolArtifacts(result.messages); - const diagnosis = { - symptom: input.symptom, - environment: input.environment, - inferredDomains: toInferredDomains(draft), - selectedTools: toolDescriptors.filter((descriptor) => - toolArtifacts.toolNames.has(descriptor.name) - ), - observations: toolArtifacts.observations, - rawResults: toolArtifacts.rawResults, - toolErrors: toolArtifacts.toolErrors, - preliminaryFindings: toPreliminaryFindings(draft), - suspectedCauses: toSuspectedCauses(draft), - recommendedChecks: draft.recommendedChecks, - incompleteReasons: [ - ...draft.incompleteReasons, - ...toolArtifacts.toolErrors.map((error) => `${error.toolName}: ${error.message}`) - ] - } satisfies AgentDiagnosisResult; + const diagnosis = ledger.assemble(input.symptom, draft); await traceSink.record({ event: "diagnosis.completed", runId, occurredAt: new Date().toISOString(), durationMs: Date.now() - startedAt, - selectedTools: diagnosis.selectedTools.map((tool) => tool.name) + selectedTools: [...new Set(diagnosis.observations.map((call) => call.tool))] }); return diagnosis; @@ -155,7 +130,13 @@ export class LangChainDiagnosticAgent implements DiagnosticAgentPort { function buildSystemPrompt(environment: AmdcEnvironment): string { return [ "You are AMDC Diagnostic Agent.", - "Your job is to inspect AMDB infrastructure state with AMDC-provided read-only tools and produce structured diagnosis data for a separate Report Agent.", + "Your job is to inspect AMDB infrastructure state with AMDC-provided read-only tools and hand off an evidence-based first-pass diagnosis to a separate Report Agent.", + "Record what you checked, what the tool returned, and which observed fact motivated each follow-up read. Keep these links short and evidence-based, not a narration of private reasoning.", + "Explain the operational meaning of results and identify evidence-supported anomalies or suspicious conditions. Connect each concern to its observations and follow-up outcome, distinguishing confirmed impact from possible impact that was not observed.", + "Include relevant identifiers, values, scope and observation times where they support interpretation. Distinguish current samples, historical counters, empty results, and failed queries. Omit routine returnedRows, limit, truncated=false, daemon listings, and diagnostic sessions unless they affect the conclusion. Counts of real connections or waits can be meaningful evidence.", + "Continue relevant read-only investigation when returned facts warrant follow-up. Changing the output format does not mean stopping after the first check.", + "First-pass interpretation and evidence-supported suspicion are required when warranted. Do not assign an unobserved root cause, a global severity/health verdict, recommended actions or remediation. Do not call a count abnormal without a relevant baseline, limit or other evidence. Include counter-evidence and uncertainty, and do not manufacture suspicious findings when none were observed.", + "State unobserved areas as limitations. Do not turn missing data into a service fault or a clean bill of health. Write human-readable text in Korean while preserving source identifiers.", "Do not write Discord messages. Do not produce the final operator report.", "Do not ask for shell, SSH, database credentials, endpoint URLs, PromQL, LogQL, SQL, or arbitrary HTTP access.", "Use only the provided AMDC tools. Tool execution commands, endpoints, and credentials are owned by AMDC and are not model inputs.", @@ -165,7 +146,12 @@ function buildSystemPrompt(environment: AmdcEnvironment): string { `The server-owned environment is ${environment}. Never change or override it.`, "Call tools based on their functional capability, not because a tool is tied to a specific incident case.", "Prefer a small set of relevant tools, but use multiple tools when needed to correlate health, logs, metrics, proxy, MySQL, and backup state.", - "Return structured diagnosis data only." + "AMDC records tool results independently. Each execution returns tool_call_id and seq. Never invent or rewrite execution IDs, inputs, results or timestamps.", + "Return only completion_reason and comments referencing those exact tool_call_id values. Do not reference plugin selection or model calls.", + "Each optional comment separates observation (what this result establishes), hypothesis (tentative interpretation), and limitation (what it cannot establish). Use null for absent fields or omit the annotation; never fill them with speculation.", + "related_call_ids may reference other existing calls in this diagnosis regardless of execution order, including parallel or later reads. Do not reference the annotated call itself or repeat IDs. These are evidence links, not causal ordering or an internal chain of thought. A Report Agent must assess original results independently of your comments.", + "completion_reason is investigation_complete when relevant investigation is finished, or insufficient_evidence when unavailable evidence prevents finishing. Neither is a health verdict.", + "Return structured annotation data only." ].join("\n"); } @@ -184,81 +170,6 @@ function buildUserPrompt( .join("\n"), "", "Select the smallest relevant plugin first. Use its loaded tools, then expand to another plugin only if the observations require it.", - "Return inferred domains, preliminary findings, suspected causes, recommended checks, and incomplete reasons." + "Return completion_reason and optional per-call comments using the tool_call_id supplied in each execution result. Separate observations, hypotheses and limitations. No final root-cause verdict or recommended actions." ].join("\n"); } - -export function extractToolArtifacts(messages: readonly unknown[]): { - readonly observations: readonly ToolObservation[]; - readonly rawResults: readonly RawToolResult[]; - readonly toolErrors: readonly SanitizedToolError[]; - readonly toolNames: ReadonlySet; -} { - const observations: ToolObservation[] = []; - const rawResults: RawToolResult[] = []; - const toolErrors: SanitizedToolError[] = []; - const toolNames = new Set(); - - for (const message of messages) { - const maybeMessage = message as { content?: unknown; name?: unknown }; - - if (typeof maybeMessage.name === "string") { - toolNames.add(maybeMessage.name); - } - - const text = typeof maybeMessage.content === "string" ? maybeMessage.content : null; - if (!text) { - continue; - } - - try { - const parsed = JSON.parse(text) as unknown; - if (!isRecord(parsed) || typeof parsed.ok !== "boolean") { - continue; - } - - if (parsed.ok === true && isRecord(parsed.observation)) { - observations.push(parsed.observation as unknown as ToolObservation); - } else if (parsed.ok === true && isRecord(parsed.rawResult)) { - rawResults.push(parsed.rawResult as unknown as RawToolResult); - } else if (parsed.ok === false && isRecord(parsed.error)) { - toolErrors.push(parsed.error as unknown as SanitizedToolError); - } - } catch { - continue; - } - } - - return { observations, rawResults, toolErrors, toolNames }; -} - -function toInferredDomains(draft: DiagnosisDraft): readonly InferredDomain[] { - return draft.inferredDomains.map((domain) => ({ - domain: toPluginName(domain.domain), - reason: domain.reason - })); -} - -function toPreliminaryFindings(draft: DiagnosisDraft): readonly PreliminaryFinding[] { - return draft.preliminaryFindings.map((finding) => ({ - finding: finding.finding, - basis: finding.basis, - level: finding.level as ObservationStatus - })); -} - -function toSuspectedCauses(draft: DiagnosisDraft): readonly SuspectedCause[] { - return draft.suspectedCauses.map((cause) => ({ - cause: cause.cause, - reason: cause.reason, - confidence: cause.confidence - })); -} - -function toPluginName(value: string): PluginName { - return isPluginName(value) ? value : "system"; -} - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} diff --git a/src/agent/langchain-schemas.ts b/src/agent/langchain-schemas.ts index b5ec81c..905fa02 100644 --- a/src/agent/langchain-schemas.ts +++ b/src/agent/langchain-schemas.ts @@ -1,38 +1,9 @@ import { z } from "zod/v3"; import type { JsonObjectSchema, JsonSchemaProperty } from "../tools/types.js"; -export const diagnosisDraftSchema = z.object({ - inferredDomains: z - .array( - z.object({ - domain: z.string().min(1), - reason: z.string().min(1) - }) - ) - .default([]), - preliminaryFindings: z - .array( - z.object({ - finding: z.string().min(1), - basis: z.array(z.string().min(1)).default([]), - level: z.enum(["normal", "warning", "critical", "unknown"]) - }) - ) - .default([]), - suspectedCauses: z - .array( - z.object({ - cause: z.string().min(1), - reason: z.string().min(1), - confidence: z.enum(["low", "medium", "high"]) - }) - ) - .default([]), - recommendedChecks: z.array(z.string().min(1)).default([]), - incompleteReasons: z.array(z.string().min(1)).default([]) -}); - -export type DiagnosisDraft = z.infer; +export { handoffDraftSchema as diagnosisDraftSchema } from "../report/diagnosis-handoff.js"; + +export type { HandoffDraft as DiagnosisDraft } from "../report/diagnosis-handoff.js"; export function jsonObjectSchemaToZod(schema: JsonObjectSchema): z.ZodObject> { const properties = schema.properties ?? {}; diff --git a/src/agent/langchain-tool-wrapper.ts b/src/agent/langchain-tool-wrapper.ts index 4f4405e..14a74eb 100644 --- a/src/agent/langchain-tool-wrapper.ts +++ b/src/agent/langchain-tool-wrapper.ts @@ -4,12 +4,14 @@ import type { AmdcEnvironment } from "../config/env.js"; import type { DiagnosticTraceSink } from "../observability/diagnostic-trace.js"; import { jsonObjectSchemaToZod } from "./langchain-schemas.js"; import type { AgentVisibleToolDescriptor, ToolRuntime } from "../tools/types.js"; +import { DiagnosisLedger } from "../report/diagnosis-handoff.js"; export interface LangChainToolWrapperContext { readonly environment: AmdcEnvironment; readonly referenceTime: Date; readonly runId: string; readonly traceSink: DiagnosticTraceSink; + readonly ledger?: DiagnosisLedger; readonly disabledTools?: Set; } @@ -20,12 +22,14 @@ export function createAmdcLangChainTools( ) { // One set per diagnosis; reserve synchronously before parallel tool execution. const executed = new Set(); + const ledger = context.ledger ?? new DiagnosisLedger(context.runId); const disabledTools = context.disabledTools ?? new Set(); const attemptsByTool = new Map(); let tail: Promise = Promise.resolve(); return descriptors.map((descriptor) => { const wrapped = tool( async (args: Record) => { + const call = ledger.begin(descriptor, args); const startedAt = Date.now(); await context.traceSink.record({ event: "tool.started", @@ -59,11 +63,17 @@ export function createAmdcLangChainTools( } ).catch(() => ({ ok: false as const, - error: { toolName: descriptor.name, pluginName: descriptor.pluginName, - code: "source_request_failed" as const, message: "Tool execution failed unexpectedly.", - occurredAt: new Date().toISOString() } + error: { + toolName: descriptor.name, + pluginName: descriptor.pluginName, + code: "source_request_failed" as const, + message: "Tool execution failed unexpectedly.", + occurredAt: new Date().toISOString() + } })); + ledger.finish(call, result); + await context.traceSink.record({ event: "tool.finished", runId: context.runId, @@ -81,7 +91,7 @@ export function createAmdcLangChainTools( ...(!result.ok ? { errorCode: result.error.code } : {}) }); - return JSON.stringify(result); + return JSON.stringify({ tool_call_id: call.tool_call_id, seq: call.seq, ...result }); }, { name: descriptor.name, @@ -89,22 +99,24 @@ export function createAmdcLangChainTools( schema: jsonObjectSchemaToZod(descriptor.inputSchema) } ); - const limited = tool(async () => JSON.stringify({ ok: false, error: { - toolName: descriptor.name, pluginName: descriptor.pluginName, - code: "tool_call_limit_reached", - message: "This tool is disabled for this diagnosis after 8 attempts. Use its earlier results or another available tool. If evidence is insufficient, report that limitation.", - occurredAt: new Date().toISOString() - } }), { name: descriptor.name, description: descriptor.description, - schema: jsonObjectSchemaToZod({ type: "object", properties: {} }) }); + const limited = tool(async (args: Record) => { + const call = ledger.begin(descriptor, args); + const result = { ok: false as const, error: { + toolName: descriptor.name, pluginName: descriptor.pluginName, + code: "tool_call_limit_reached" as const, + message: "This tool is disabled for this diagnosis after 8 attempts. Use its earlier results or another available tool. If evidence is insufficient, report that limitation.", + occurredAt: new Date().toISOString() + } }; + ledger.finish(call, result); + return JSON.stringify({ tool_call_id: call.tool_call_id, seq: call.seq, ...result }); + }, { name: descriptor.name, description: descriptor.description, + schema: jsonObjectSchemaToZod(descriptor.inputSchema) }); const invoke = wrapped.invoke.bind(wrapped); wrapped.invoke = (input, config) => { - // Count by tool identity, independent of arguments, before any await. const attempts = (attemptsByTool.get(descriptor.name) ?? 0) + 1; attemptsByTool.set(descriptor.name, Math.min(attempts, 9)); if (attempts >= 8) disabledTools.add(descriptor.name); - if (attempts > 8) { - return limited.invoke(input, config); - } + if (attempts > 8) return limited.invoke(input, config); const pending = tail.then(() => invoke(input, config)); tail = pending.then(() => undefined, () => undefined); return pending; diff --git a/src/agent/types.ts b/src/agent/types.ts index d5950e8..535b14f 100644 --- a/src/agent/types.ts +++ b/src/agent/types.ts @@ -1,12 +1,5 @@ import type { AmdcEnvironment } from "../config/env.js"; -import type { - AgentVisibleToolDescriptor, - ObservationStatus, - PluginName, - SanitizedToolError, - ToolObservation, - RawToolResult -} from "../tools/types.js"; +import type { DiagnosisHandoff } from "../report/diagnosis-handoff.js"; export interface DiagnosticAgentInput { readonly symptom: string; @@ -15,36 +8,7 @@ export interface DiagnosticAgentInput { readonly receivedAt: string; } -export interface InferredDomain { - readonly domain: PluginName; - readonly reason: string; -} - -export interface PreliminaryFinding { - readonly finding: string; - readonly basis: readonly string[]; - readonly level: ObservationStatus; -} - -export interface SuspectedCause { - readonly cause: string; - readonly reason: string; - readonly confidence: "low" | "medium" | "high"; -} - -export interface AgentDiagnosisResult { - readonly symptom: string; - readonly environment: AmdcEnvironment; - readonly inferredDomains: readonly InferredDomain[]; - readonly selectedTools: readonly AgentVisibleToolDescriptor[]; - readonly observations: readonly ToolObservation[]; - readonly rawResults?: readonly RawToolResult[]; - readonly toolErrors: readonly SanitizedToolError[]; - readonly preliminaryFindings: readonly PreliminaryFinding[]; - readonly suspectedCauses: readonly SuspectedCause[]; - readonly recommendedChecks: readonly string[]; - readonly incompleteReasons: readonly string[]; -} +export type AgentDiagnosisResult = DiagnosisHandoff; export interface DiagnosticAgentPort { diagnose(input: DiagnosticAgentInput): Promise; diff --git a/src/diagnostic/mock-diagnostic-runner.ts b/src/diagnostic/mock-diagnostic-runner.ts index 62f72eb..58939fd 100644 --- a/src/diagnostic/mock-diagnostic-runner.ts +++ b/src/diagnostic/mock-diagnostic-runner.ts @@ -1,3 +1,4 @@ +import { randomUUID } from "node:crypto"; import type { DiagnosisRequest, DiagnosisResult, @@ -12,14 +13,10 @@ export class MockDiagnosticRunner implements DiagnosticRunner { ): Promise { return { presentation: { - status: "mock", - summary: `증상 접수 완료: ${request.symptom}`, - suspectedCause: "현재는 Discord 연결과 runner 교체 구조를 검증하는 mock 응답입니다.", - recommendedActions: [ - `현재 실행 환경은 ${context.environment}입니다.`, - "다음 이슈에서 LangChain runner를 연결해 실제 진단 흐름으로 교체합니다.", - "Prometheus, Loki, AMDB read-only tool 연동은 별도 source contract 확정 뒤 진행합니다." - ] + diagnosis_id: `mock-${randomUUID()}`, + request: request.symptom, + completion_reason: "mock", + observations: [] } }; } diff --git a/src/report/diagnosis-handoff.ts b/src/report/diagnosis-handoff.ts new file mode 100644 index 0000000..514abfd --- /dev/null +++ b/src/report/diagnosis-handoff.ts @@ -0,0 +1,112 @@ +import { z } from "zod/v3"; +import type { AgentVisibleToolDescriptor, RawToolResult, ToolObservation, ToolRuntimeResult } from "../tools/types.js"; + +const commentSchema = z.object({ + observation: z.string().max(2000).nullable(), + hypothesis: z.string().max(2000).nullable(), + limitation: z.string().max(2000).nullable() +}).strict(); + +// The model can annotate existing calls, never supply execution records. +export const handoffDraftSchema = z.object({ + completion_reason: z.enum(["investigation_complete", "insufficient_evidence"]) + .describe("Why investigation stopped; complete does not mean healthy or root cause proven."), + comments: z.array(z.object({ + tool_call_id: z.string().min(1), + comment: commentSchema.nullable(), + related_call_ids: z.array(z.string().min(1)).max(100) + .describe("Other existing call IDs in this diagnosis supporting this annotation, regardless of execution order; not private reasoning.") + }).strict()).max(100).describe("Optional annotations for actual calls. Use IDs returned by tools; omit calls needing no comment.") +}).strict(); + +export type HandoffDraft = z.infer; +export interface HandoffObservation { + readonly seq: number; + readonly tool_call_id: string; + readonly plugin: string; + readonly tool: string; + readonly input: Readonly>; + readonly observed_at: string; + readonly status: "success" | "error"; + readonly result: RawToolResult | ToolObservation | null; + readonly error: Extract["error"] | null; + readonly comment: z.infer | null; + readonly related_call_ids: readonly string[]; +} + +export interface DiagnosisHandoff { + readonly diagnosis_id: string; + readonly request: string; + readonly completion_reason: HandoffDraft["completion_reason"] | "mock"; + readonly observations: readonly HandoffObservation[]; +} + +// This boundary must not reintroduce credentials through requests or model comments. +export function assertSafeHandoffData(value: unknown): void { + const text = JSON.stringify(value); + const secrets = Object.entries(process.env) + .filter(([key, value]) => /TOKEN|PASSWORD|SECRET|API_KEY|PRIVATE_KEY|SESSION/i.test(key) && value && value.length >= 8) + .map(([, value]) => value!); + if (secrets.some(secret => text.includes(secret) || text.includes(JSON.stringify(secret).slice(1, -1))) || + /-----BEGIN [A-Z ]*PRIVATE KEY-----|\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/i.test(text)) { + throw new Error("Handoff rejected: sensitive data."); + } +} + +// Per-diagnosis only. Reserve before awaiting so parallel completion cannot reorder IDs. +export class DiagnosisLedger { + private nextSequence = 0; + private readonly records: HandoffObservation[] = []; + constructor(readonly diagnosisId: string) {} + + begin(descriptor: AgentVisibleToolDescriptor, input: Record) { + assertSafeHandoffData(input); + const seq = ++this.nextSequence; + return { + seq, tool_call_id: `${this.diagnosisId}:call-${seq}`, + plugin: descriptor.pluginName, tool: descriptor.name, + input: structuredClone(input) + }; + } + + finish(call: ReturnType, output: ToolRuntimeResult) { + assertSafeHandoffData(output); + this.records.push(structuredClone({ + ...call, + observed_at: output.ok + ? ("rawResult" in output ? output.rawResult.collectedAt : output.observation.collectedAt) + : output.error.occurredAt, + status: output.ok ? "success" : "error", + result: output.ok ? ("rawResult" in output ? output.rawResult : output.observation) : null, + error: output.ok ? null : output.error, + comment: null, related_call_ids: [] + } satisfies HandoffObservation)); + } + + assemble(request: string, candidate: unknown): DiagnosisHandoff { + const draft = handoffDraftSchema.parse(candidate); + const byId = new Map(this.records.map(record => [record.tool_call_id, record])); + const annotations = new Map(); + for (const annotation of draft.comments) { + const record = byId.get(annotation.tool_call_id); + if (!record || annotations.has(annotation.tool_call_id)) throw new Error("Invalid handoff call reference."); + for (const id of annotation.related_call_ids) { + if (!byId.has(id) || id === record.tool_call_id) throw new Error("Invalid handoff related call reference."); + } + if (new Set(annotation.related_call_ids).size !== annotation.related_call_ids.length) { + throw new Error("Duplicate handoff related call reference."); + } + annotations.set(annotation.tool_call_id, annotation); + } + const handoff: DiagnosisHandoff = { + diagnosis_id: this.diagnosisId, request, + completion_reason: this.records.length ? draft.completion_reason : "insufficient_evidence", + observations: [...this.records].sort((a, b) => a.seq - b.seq).map(record => { + const annotation = annotations.get(record.tool_call_id); + return { ...record, comment: annotation?.comment ?? null, related_call_ids: annotation?.related_call_ids ?? [] }; + }) + }; + assertSafeHandoffData(handoff); + return structuredClone(handoff); + } +} diff --git a/src/report/temporary-diagnostic-presenter.ts b/src/report/temporary-diagnostic-presenter.ts index 6c1d5ff..231afac 100644 --- a/src/report/temporary-diagnostic-presenter.ts +++ b/src/report/temporary-diagnostic-presenter.ts @@ -1,39 +1,9 @@ import type { AgentDiagnosisResult } from "../agent/types.js"; import type { DiagnosticPresentation } from "./types.js"; -// Temporary presenter for the diagnostic-agent skeleton. -// The final Report Agent is intentionally out of scope for this slice. +// Preview of the factual handoff. A Report Agent owns final interpretation. export class TemporaryDiagnosticPresenter { createPresentation(diagnosis: AgentDiagnosisResult): DiagnosticPresentation { - const hasCritical = diagnosis.observations.some( - (observation) => observation.status === "critical" - ); - const hasWarning = diagnosis.observations.some( - (observation) => observation.status === "warning" - ); - - return { - status: hasCritical || hasWarning - ? "problem_detected" - : diagnosis.toolErrors.length > 0 || diagnosis.observations.length === 0 || - (diagnosis.rawResults?.length ?? 0) > 0 || diagnosis.incompleteReasons.length > 0 || - diagnosis.observations.some(observation => observation.status !== "normal") || - diagnosis.preliminaryFindings.some(finding => finding.level !== "normal") - ? "insufficient_tools" - : "no_problem_detected", - summary: buildSummary(diagnosis), - suspectedCause: diagnosis.suspectedCauses[0]?.cause ?? "Unknown", - recommendedActions: - diagnosis.recommendedChecks.length > 0 - ? [...diagnosis.recommendedChecks] - : ["Review the structured diagnosis result and run additional read-only checks if needed."] - }; + return structuredClone(diagnosis); } } - -function buildSummary(diagnosis: AgentDiagnosisResult): string { - const domains = diagnosis.inferredDomains.map((domain) => domain.domain).join(", "); - const finding = diagnosis.preliminaryFindings[0]?.finding ?? "No preliminary finding."; - - return `Domains: ${domains || "unknown"}. ${finding}`; -} diff --git a/src/report/types.ts b/src/report/types.ts index a528db1..60397f3 100644 --- a/src/report/types.ts +++ b/src/report/types.ts @@ -1,6 +1,3 @@ -export interface DiagnosticPresentation { - readonly status: "mock" | "problem_detected" | "no_problem_detected" | "insufficient_tools"; - readonly summary: string; - readonly suspectedCause: string; - readonly recommendedActions: readonly string[]; -} +import type { DiagnosisHandoff } from "./diagnosis-handoff.js"; + +export type DiagnosticPresentation = DiagnosisHandoff; diff --git a/tests/diagnosis-handoff.test.ts b/tests/diagnosis-handoff.test.ts new file mode 100644 index 0000000..4b9b7db --- /dev/null +++ b/tests/diagnosis-handoff.test.ts @@ -0,0 +1,168 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { DiagnosisLedger, handoffDraftSchema } from "../src/report/diagnosis-handoff.js"; +import { createAmdcLangChainTools } from "../src/agent/langchain-tool-wrapper.js"; +import type { AgentVisibleToolDescriptor, ToolRuntimeResult } from "../src/tools/types.js"; +import { DiagnosisPipeline } from "../src/app/diagnosis-pipeline.js"; +import { TemporaryDiagnosticPresenter } from "../src/report/temporary-diagnostic-presenter.js"; +import { ChatOpenAI } from "@langchain/openai"; +import { AIMessage, type BaseMessage } from "@langchain/core/messages"; +import { LangChainDiagnosticAgent } from "../src/agent/langchain-diagnostic-agent.js"; +import { PluginRegistry } from "../src/tools/plugin-registry.js"; + +const descriptor: AgentVisibleToolDescriptor = { + name: "mysql_read", pluginName: "mysql", description: "Read rows", readOnly: true, + inputSchema: { type: "object", additionalProperties: false, properties: { connectionId: { type: "integer" } } } +}; +const empty: ToolRuntimeResult = { ok: true, rawResult: { + toolName: descriptor.name, pluginName: "mysql", source: "mysql", transport: "mysql", + collectedAt: "2026-09-19T00:00:00.000Z", rows: [], appliedFilters: {}, returnedRows: 0, limit: 100, truncated: false +} }; +const draft = { completion_reason: "investigation_complete", comments: [] }; + +test("raw empty results, errors and independent model annotations are preserved", () => { + const ledger = new DiagnosisLedger("diag-1"); + const call = ledger.begin(descriptor, {}); + ledger.finish(call, empty); + const failed = ledger.begin(descriptor, { connectionId: 382 }); + ledger.finish(failed, { ok: false, error: { toolName: descriptor.name, pluginName: "mysql", code: "tool_timeout", message: "Timed out", occurredAt: "2026-09-19T00:01:00.000Z" } }); + const comment = { observation: null, hypothesis: null, limitation: "세션 조회 timeout으로 상태를 확인하지 못했다." }; + const handoff = ledger.assemble("느림", { ...draft, completion_reason: "insufficient_evidence", comments: [{ tool_call_id: failed.tool_call_id, comment, related_call_ids: [call.tool_call_id] }] }); + assert.deepEqual(handoff.observations[0].result, empty.rawResult); + assert.equal(handoff.observations[0].comment, null); + assert.equal(handoff.observations[0].status, "success"); + assert.equal(handoff.observations[1].result, null); + assert.equal(handoff.observations[1].error?.code, "tool_timeout"); + assert.deepEqual(handoff.observations[1].comment, comment); + assert.deepEqual(handoff.observations[1].related_call_ids, [call.tool_call_id]); + assert.deepEqual(Object.keys(handoff).sort(), ["completion_reason", "diagnosis_id", "observations", "request"]); +}); + +test("model cannot supply results, fabricate IDs, duplicate annotations or reference itself", () => { + const ledger = new DiagnosisLedger("diag-1"); + const first = ledger.begin(descriptor, {}); ledger.finish(first, empty); + const second = ledger.begin(descriptor, {}); ledger.finish(second, empty); + const annotation = { tool_call_id: first.tool_call_id, comment: null, related_call_ids: [] }; + assert.equal(handoffDraftSchema.safeParse({ ...draft, observations: [] }).success, false); + assert.equal(handoffDraftSchema.safeParse({ ...draft, comments: [{ ...annotation, result: {} }] }).success, false); + for (const comments of [ + [{ ...annotation, tool_call_id: "invented" }], [annotation, annotation], + [{ ...annotation, related_call_ids: [first.tool_call_id] }], + [{ ...annotation, related_call_ids: [second.tool_call_id, second.tool_call_id] }], + [{ ...annotation, related_call_ids: ["invented"] }] + ]) assert.throws(() => ledger.assemble("test", { ...draft, comments }), /reference/); + const handoff = ledger.assemble("test", { ...draft, comments: [ + { ...annotation, related_call_ids: [second.tool_call_id] }, + { ...annotation, tool_call_id: second.tool_call_id, related_call_ids: [first.tool_call_id] } + ] }); + assert.deepEqual(handoff.observations.map(call => call.related_call_ids), [[second.tool_call_id], [first.tool_call_id]]); +}); + +test("concurrent requests execute sequentially and expose ordered IDs to the model", async () => { + const ledger = new DiagnosisLedger("diag-parallel"); + let active = 0; + let peak = 0; + const [tool] = createAmdcLangChainTools([descriptor], { execute: async request => { + peak = Math.max(peak, ++active); + await new Promise(resolve => setTimeout(resolve, 2)); + active--; + return empty; + } }, { environment: "dev", referenceTime: new Date(), runId: "diag-parallel", ledger, traceSink: { record: async () => {} } }); + const results = await Promise.all([tool.invoke({ connectionId: 1 }), tool.invoke({ connectionId: 2 })]); + const handoff = ledger.assemble("test", draft); + assert.equal(peak, 1); + assert.deepEqual(handoff.observations.map(call => call.seq), [1, 2]); + assert.deepEqual(handoff.observations.map(call => call.input.connectionId), [1, 2]); + assert.deepEqual(results.map(result => JSON.parse(String(result)).tool_call_id), handoff.observations.map(call => call.tool_call_id)); +}); + +test("unexpected runtime errors are sanitized and recorded, repeated attempts are not lost", async () => { + const ledger = new DiagnosisLedger("diag-errors"); + let calls = 0; + const [tool] = createAmdcLangChainTools([descriptor], { execute: async () => { calls++; throw new Error("private upstream body"); } }, { environment: "dev", referenceTime: new Date(), runId: "diag-errors", ledger, traceSink: { record: async () => {} } }); + await tool.invoke({}); await tool.invoke({}); + const handoff = ledger.assemble("test", draft); + assert.equal(calls, 1); + assert.deepEqual(handoff.observations.map(call => call.error?.code), ["source_request_failed", "invalid_input"]); + assert.doesNotMatch(JSON.stringify(handoff), /private upstream/); +}); + +test("handoff isolates runs and clones snapshots; zero calls cannot claim completed investigation", async () => { + const runs = await Promise.all(Array.from({ length: 100 }, async (_, index) => { + const ledger = new DiagnosisLedger(`diag-${index}`); + const input = { connectionId: index }; + const call = ledger.begin(descriptor, input); input.connectionId = -1; + ledger.finish(call, empty); + return ledger.assemble("test", draft); + })); + assert.equal(new Set(runs.map(run => run.observations[0].tool_call_id)).size, 100); + runs.forEach((run, index) => assert.equal(run.observations[0].input.connectionId, index)); + assert.equal(new DiagnosisLedger("none").assemble("test", draft).completion_reason, "insufficient_evidence"); +}); + +test("request, tool inputs, result and model comment cannot reintroduce configured secrets", () => { + const key = "AMDC_HANDOFF_TEST_SECRET"; + const previous = process.env[key]; process.env[key] = "fixture-secret-12345"; + try { + const ledger = new DiagnosisLedger("diag-security"); + assert.throws(() => ledger.begin(descriptor, { connectionId: process.env[key] }), /sensitive/); + const call = ledger.begin(descriptor, {}); ledger.finish(call, empty); + assert.throws(() => ledger.assemble(process.env[key]!, draft), /sensitive/); + assert.throws(() => ledger.assemble("test", { ...draft, comments: [{ tool_call_id: call.tool_call_id, comment: { observation: process.env[key], hypothesis: null, limitation: null }, related_call_ids: [] }] }), /sensitive/); + } finally { + if (previous === undefined) delete process.env[key]; else process.env[key] = previous; + } +}); + +test("pipeline passes the execution-backed handoff to the presentation without rewriting it", async () => { + const ledger = new DiagnosisLedger("diag-pipeline"); + ledger.finish(ledger.begin(descriptor, {}), empty); + const handoff = ledger.assemble("느림", draft); + const pipeline = new DiagnosisPipeline({ diagnose: async () => handoff }, new TemporaryDiagnosticPresenter()); + const result = await pipeline.run({ symptom: "느림", requestedBy: "test", source: "discord", receivedAt: new Date().toISOString() }, { environment: "dev", runnerMode: "mock", agentModel: "fake" }); + assert.deepEqual(result.diagnosis, handoff); + assert.deepEqual(result.presentation, handoff); + assert.notEqual(result.presentation, handoff); +}); + +test("actual LangChain loop selects a plugin, receives execution ID and annotates the runtime result", async (t) => { + const registry = new PluginRegistry({ plugins: [{ + name: "mysql", description: "MySQL readers", domainHints: ["mysql"], tools: [{ + ...descriptor, access: { level: 0, readOnly: true }, source: "mysql", allowedEnvironments: ["dev"], timeoutMs: 1000, + execution: { type: "source_adapter", operation: "fixture" } + }] + }] }); + let modelCalls = 0; + let observedId = ""; + t.mock.method(ChatOpenAI.prototype, "_generate", async (messages: BaseMessage[], options: { tools?: { function: { name: string } }[] }) => { + modelCalls++; + const names = options.tools?.map(tool => tool.function.name) ?? []; + let name: string; + let args: Record; + if (modelCalls === 1) { + assert.ok(names.includes("select_plugin")); + assert.ok(!names.includes(descriptor.name)); + name = "select_plugin"; args = { pluginName: "mysql" }; + } else if (modelCalls === 2) { + assert.ok(names.includes(descriptor.name)); + name = descriptor.name; args = {}; + } else { + assert.equal(modelCalls, 3); + const response = messages.find(message => message.name === descriptor.name)!; + const payload = JSON.parse(String(response.content)); + observedId = payload.tool_call_id; + assert.deepEqual(payload.rawResult, empty.rawResult); + name = names.find(name => name.startsWith("extract-"))!; + assert.ok(name); + args = { ...draft, comments: [{ tool_call_id: observedId, comment: { observation: "조회 결과는 빈 배열이다.", hypothesis: null, limitation: "조회 범위 밖의 상태는 확인하지 않았다." }, related_call_ids: [] }] }; + } + return { generations: [{ text: "", message: new AIMessage({ content: "", tool_calls: [{ id: `provider-${modelCalls}`, name, args, type: "tool_call" }] }) }] }; + }); + const agent = new LangChainDiagnosticAgent(registry, { execute: async () => empty }, { model: "fixture-model", apiKey: "fixture-key" }); + const handoff = await agent.diagnose({ symptom: "MySQL 확인", environment: "dev", requestedBy: "fixture", receivedAt: new Date().toISOString() }); + assert.equal(modelCalls, 3); + assert.equal(handoff.observations.length, 1); + assert.equal(handoff.observations[0].tool_call_id, observedId); + assert.deepEqual(handoff.observations[0].result, empty.rawResult); + assert.equal(handoff.observations[0].comment?.observation, "조회 결과는 빈 배열이다."); +}); diff --git a/tests/diagnostic-trace.test.ts b/tests/diagnostic-trace.test.ts index 0b57b50..7491a25 100644 --- a/tests/diagnostic-trace.test.ts +++ b/tests/diagnostic-trace.test.ts @@ -89,3 +89,25 @@ test("writes safe model failure stage and metadata without provider message", as ]); assert.doesNotMatch(lines[0], /prompt|response|body|credential|message/); }); +import { diagnosisDraftSchema } from "../src/agent/langchain-schemas.js"; +import { TemporaryDiagnosticPresenter } from "../src/report/temporary-diagnostic-presenter.js"; +import { formatDiagnosticPresentationMessages } from "../src/adapters/discord/format-report.js"; +import { DiagnosisLedger } from "../src/report/diagnosis-handoff.js"; + +test("handoff annotations reject old narrative contract and Discord preserves full JSON separately", () => { + const ledger = new DiagnosisLedger("diag-test"); + const call = ledger.begin({ name: "mysql_get_all_processlist", pluginName: "mysql", description: "Read sessions", readOnly: true, inputSchema: { type: "object" } }, {}); + const body = "긴 관측값 ".repeat(700); + ledger.finish(call, { ok: true, rawResult: { toolName: call.tool, pluginName: "mysql", source: "mysql", collectedAt: "2026-09-19T00:00:00.000Z", transport: "mysql", rows: [{ body }], appliedFilters: {}, limit: 1, returnedRows: 1, truncated: false } }); + const draft = { completion_reason: "investigation_complete", comments: [{ tool_call_id: call.tool_call_id, comment: { observation: "현재 연결 조회", hypothesis: null, limitation: "이전 시점은 조회하지 않음" }, related_call_ids: [] }] }; + assert.ok(diagnosisDraftSchema.safeParse(draft).success); + assert.equal(diagnosisDraftSchema.safeParse({ ...draft, suspectedCauses: [], recommendedChecks: [] }).success, false); + const presentation = new TemporaryDiagnosticPresenter().createPresentation(ledger.assemble("느림", draft)); + const messages = formatDiagnosticPresentationMessages(presentation); + assert.ok(messages.every(message => message.length <= 1900)); + const text = messages.join(""); + assert.ok(text.includes("현재 연결 조회") && text.includes("이전 시점은 조회하지 않음")); + assert.match(text, /표시 생략/); + assert.equal((JSON.stringify(presentation).match(/긴 관측값/g) ?? []).length, 700); + assert.doesNotMatch(text, /Recommended Actions|Suspected Cause|\*\*Status/); +}); diff --git a/tests/pr11-review.test.ts b/tests/pr11-review.test.ts index 014aea1..e06273c 100644 --- a/tests/pr11-review.test.ts +++ b/tests/pr11-review.test.ts @@ -2,13 +2,12 @@ import assert from 'node:assert/strict'; import { test } from 'node:test'; import { fileURLToPath } from 'node:url'; import { createAmdcLangChainTools } from '../src/agent/langchain-tool-wrapper.js'; -import { extractToolArtifacts } from '../src/agent/langchain-diagnostic-agent.js'; +import { DiagnosisLedger } from '../src/report/diagnosis-handoff.js'; import { TemporaryDiagnosticPresenter } from '../src/report/temporary-diagnostic-presenter.js'; import { executeMysqlTool } from '../src/tools/source-adapters/mysql-adapter.js'; import { executeLocalShellTool } from '../src/tools/source-adapters/local-shell-adapter.js'; import { loadToolCatalogFromYaml } from '../src/tools/catalog-loader.js'; import { PluginRegistry } from '../src/tools/plugin-registry.js'; -import type { AgentDiagnosisResult } from '../src/agent/types.js'; import type { AgentVisibleToolDescriptor, ToolRuntime, ToolRuntimeResult } from '../src/tools/types.js'; const registry = new PluginRegistry(loadToolCatalogFromYaml(fileURLToPath(new URL('../src/tools/catalogs/amdb-tools.yaml', import.meta.url)))); const context = { environment: 'dev' as const, referenceTime: new Date('2026-09-25T00:00:00.000Z'), runId: 'fixture', traceSink: { record: async () => {} } }; @@ -16,18 +15,16 @@ const descriptor: AgentVisibleToolDescriptor = { name: 'read', pluginName: 'mysq inputSchema: { type: 'object', additionalProperties: false, properties: { id: {type:'integer'} }, required:['id'] } }; const raw: ToolRuntimeResult = { ok: true, rawResult: { toolName:'read', pluginName:'mysql', source:'mysql', collectedAt:context.referenceTime.toISOString(), transport:'mysql', rows:[], appliedFilters:{}, limit:1, returnedRows:0, truncated:false } }; -test('raw successes survive assembly and cannot produce a healthy verdict', () => { - const artifacts = extractToolArtifacts([{ name:'read', content:JSON.stringify(raw) }]); - assert.equal(artifacts.rawResults.length, 1); - const diagnosis: AgentDiagnosisResult = { symptom:'fixture', environment:'dev', inferredDomains:[], selectedTools:[], - observations:artifacts.observations, rawResults:artifacts.rawResults, toolErrors:[], preliminaryFindings:[{finding:'Suspected failure',basis:[],level:'critical'}], suspectedCauses:[], recommendedChecks:[], incompleteReasons:[] }; - const presenter = new TemporaryDiagnosticPresenter(); - assert.equal(presenter.createPresentation(diagnosis).status,'insufficient_tools'); - assert.equal(presenter.createPresentation({...diagnosis, rawResults:[], preliminaryFindings:[]}).status,'insufficient_tools'); - const normal = {toolName:'read',pluginName:'mysql' as const,source:'mysql' as const,status:'normal' as const,summary:'fixture',facts:[],collectedAt:context.referenceTime.toISOString()}; - assert.equal(presenter.createPresentation({...diagnosis, observations:[normal], preliminaryFindings:[]}).status,'insufficient_tools'); - assert.equal(presenter.createPresentation({...diagnosis, observations:[normal], rawResults:[], preliminaryFindings:[]}).status,'no_problem_detected'); - assert.equal(presenter.createPresentation({...diagnosis, observations:[{...normal,status:'critical'}]}).status,'problem_detected'); +test('raw successes survive handoff without an invented healthy verdict', () => { + const ledger = new DiagnosisLedger('diag-raw'); + ledger.finish(ledger.begin(descriptor, { id: 1 }), raw); + const diagnosis = ledger.assemble('fixture', { completion_reason: 'investigation_complete', comments: [] }); + const presentation = new TemporaryDiagnosticPresenter().createPresentation(diagnosis); + assert.ok(raw.ok && 'rawResult' in raw); + assert.deepEqual(presentation.observations[0].result, raw.rawResult); + assert.equal(presentation.observations[0].status, 'success'); + assert.equal('status' in presentation, false); + assert.equal('status' in presentation.observations[0].result!, false); }); test('changing IDs cannot bypass each tool limit; other tools and new diagnoses have independent budgets', async () => { @@ -122,7 +119,7 @@ test('LangChain hides exhausted tools, preserves limit responses and continues w calls=[{id:'other-call',name:'other',args:{id:1},type:'tool_call' as const}]; } else { assert.equal(modelCalls,6); - calls=[{id:'final',name:names.find(n=>n.startsWith('extract-'))!,args:{inferredDomains:[],preliminaryFindings:[{finding:'Suspected failure',basis:['read'],level:'critical'}],suspectedCauses:[],recommendedChecks:[],incompleteReasons:[]},type:'tool_call' as const}]; + calls=[{id:'final',name:names.find(n=>n.startsWith('extract-'))!,args:{completion_reason:'insufficient_evidence',comments:[]},type:'tool_call' as const}]; } return {generations:[{text:'',message:new AIMessage({content:'',tool_calls:calls})}]}; }); @@ -130,7 +127,9 @@ test('LangChain hides exhausted tools, preserves limit responses and continues w executions++;peak=Math.max(peak,++active);await new Promise(resolve=>setTimeout(resolve,2));active--;return raw; }},{model:'fixture',apiKey:'fixture'}); const result=await agent.diagnose({symptom:'fixture',environment:'dev',requestedBy:'fixture',receivedAt:context.referenceTime.toISOString()}); - assert.equal(executions,9);assert.equal(peak,1);assert.equal(result.rawResults?.length,9); - assert.ok(result.toolErrors.some(e=>String(e.code)==='tool_call_limit_reached')); - assert.equal(new TemporaryDiagnosticPresenter().createPresentation(result).status,'insufficient_tools'); + assert.equal(executions,9);assert.equal(peak,1); + assert.equal(result.observations.filter(call=>call.tool==='read').length,10); + assert.equal(result.observations.filter(call=>call.error?.code==='tool_call_limit_reached').length,2); + assert.equal(result.completion_reason,'insufficient_evidence'); + assert.equal('status' in new TemporaryDiagnosticPresenter().createPresentation(result),false); }); diff --git a/tests/unit/current-code-foundation.test.ts b/tests/unit/current-code-foundation.test.ts index 7ae8f75..3ddef05 100644 --- a/tests/unit/current-code-foundation.test.ts +++ b/tests/unit/current-code-foundation.test.ts @@ -68,24 +68,10 @@ function createDiagnosis( overrides: Partial = {} ): AgentDiagnosisResult { return { - symptom: "Backend errors increased.", - environment: "dev", - inferredDomains: [ - { domain: "backend", reason: "The symptom names the backend." } - ], - selectedTools: [], + diagnosis_id: "diag-foundation", + request: "Backend errors increased.", + completion_reason: "investigation_complete", observations: [], - toolErrors: [], - preliminaryFindings: [ - { - finding: "Backend evidence requires review.", - basis: ["backend_health"], - level: "warning" - } - ], - suspectedCauses: [], - recommendedChecks: [], - incompleteReasons: [], ...overrides }; } @@ -188,43 +174,32 @@ describe("current-code test foundation", () => { it("keeps the temporary presenter and Discord formatter deterministic", () => { const presenter = new TemporaryDiagnosticPresenter(); - const presentation = presenter.createPresentation( - createDiagnosis({ + const diagnosis = createDiagnosis({ observations: [ { - toolName: "backend_health", - pluginName: "backend", - source: "amdb_backend", - status: "warning", - summary: "Backend health is degraded.", - facts: [], - collectedAt: "2026-09-04T00:00:00.000Z" - } - ], - suspectedCauses: [ - { - cause: "A backend dependency may be degraded.", - reason: "The health observation is a warning.", - confidence: "medium" + seq: 1, + tool_call_id: "diag-foundation:call-1", + plugin: "backend", + tool: "backend_health", + input: {}, + observed_at: "2026-09-04T00:00:00.000Z", + status: "success", + result: { toolName: "backend_health", pluginName: "backend", source: "amdb_backend", + status: "warning", summary: "Backend health is degraded.", facts: [], + collectedAt: "2026-09-04T00:00:00.000Z" }, + error: null, + comment: null, + related_call_ids: [] } - ], - recommendedChecks: ["Review the dependency health response."] + ] }) - ); - - assert.equal(presentation.status, "problem_detected"); - assert.equal( - formatDiagnosticPresentation(presentation), - [ - "## AMDC Diagnostic Agent Result", - "", - "**Status:** problem_detected", - "**Summary:** Domains: backend. Backend evidence requires review.", - "**Suspected Cause:** A backend dependency may be degraded.", - "", - "**Recommended Actions**", - "1. Review the dependency health response." - ].join("\n") - ); + const presentation = presenter.createPresentation(diagnosis); + assert.deepEqual(presentation, diagnosis); + assert.notEqual(presentation, diagnosis); + const formatted = formatDiagnosticPresentation(presentation); + assert.equal(formatted, formatDiagnosticPresentation(presentation)); + assert.match(formatted, /diag-foundation:call-1/); + assert.match(formatted, /Backend health is degraded/); + assert.doesNotMatch(formatted, /no_problem_detected/); }); });