From 6955183748759585a0e94aecff7a1a5e4fd9ed0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9E=AC=EA=B2=BD?= Date: Thu, 24 Sep 2026 01:47:19 +0900 Subject: [PATCH] feat(report): pass diagnostic execution records and comments to report --- .../06-diagnostic-agent-tool-runtime.md | 113 ++++++++---- docs/prd/current/README.md | 4 + src/adapters/discord/bot.ts | 15 +- src/adapters/discord/format-report.ts | 67 +++++-- src/agent/langchain-diagnostic-agent.ts | 133 +++----------- src/agent/langchain-schemas.ts | 35 +--- src/agent/langchain-tool-wrapper.ts | 19 +- src/agent/types.ts | 38 +--- src/diagnostic/mock-diagnostic-runner.ts | 13 +- src/report/diagnosis-handoff.ts | 112 ++++++++++++ src/report/temporary-diagnostic-presenter.ts | 32 +--- src/report/types.ts | 9 +- tests/diagnosis-handoff.test.ts | 166 ++++++++++++++++++ tests/diagnostic-trace.test.ts | 22 +++ 14 files changed, 500 insertions(+), 278 deletions(-) create mode 100644 src/report/diagnosis-handoff.ts create mode 100644 tests/diagnosis-handoff.test.ts diff --git a/docs/prd/current/06-diagnostic-agent-tool-runtime.md b/docs/prd/current/06-diagnostic-agent-tool-runtime.md index 5db436e..1e8562b 100644 --- a/docs/prd/current/06-diagnostic-agent-tool-runtime.md +++ b/docs/prd/current/06-diagnostic-agent-tool-runtime.md @@ -1,9 +1,14 @@ # PRD 06. Diagnostic Agent And YAML Tool Runtime Status: draft amendment -Last reviewed: 2026-08-28 +Last reviewed: 2026-09-19 Owns: Diagnostic Agent 판단 범위, plugin/tool 선택 구조, YAML 기반 Tool Runtime 실행 경계 +2026-09-19 manual diagnostic handoff amendment: 이 문서의 Diagnostic Agent Output이 +현재 수동 진단의 Report 인계 계약을 소유한다. 아래 과거 vertical-slice 설명과 다른 +경우 이 계약이 우선한다. 최종 Report 형식(PRD 03), 자동 Trigger, 지식 DB 연동은 +이번 변경 범위가 아니다. + ## Gate Review [Critical Review] @@ -338,11 +343,11 @@ interface ToolObservation { } ~~~ -Raw source response is not passed to Report Agent and is not stored as evidence. -Prometheus vertical slice의 raw response는 secret scan, JSON 형식 검증, 64 KiB 제한을 -통과한 경우에만 현재 Diagnostic Agent의 Tool message로 전달한다. raw response는 -DiagnosisResult, Report, trace log 또는 persistence에 복사하지 않는다. HTTP 오류 -body도 Agent에 반환하지 않는다. 후속 Evidence 저장 계약은 별도 gate에서 정한다. +2026-09-19 manual handoff에서는 기존 adapter의 검증·보안 검사·출력 제한을 통과한 +Tool 결과를 DiagnosisResult의 `observations[].result`에 그대로 보존한다. LLM이 +결과를 재작성하지 않는다. HTTP 오류 body나 credential은 포함하지 않으며, 기본 +trace log와 DB에 결과 전문을 추가 저장하지 않는다. Discord의 임시 검증 출력은 +미리보기와 전체 handoff JSON 첨부를 사용한다. 영속 Evidence 저장은 별도 계약이다. Prometheus HTTP YAML execution은 다음 값을 server-owned contract로 고정한다. @@ -363,40 +368,76 @@ actual abort, same-origin URL 결합, prod HTTPS, response byte limit을 강제 ## Diagnostic Agent Output -Diagnostic Agent returns DiagnosisResult. +### System container observation boundary (2026-09-19) + +The `system` plugin separates container enumeration (`system_list_containers`, +empty input) from detail (`system_get_container_status`, exact `containerName`). +Existing backend HTTP health remains a separate tool. Container execution and +recorded Docker healthcheck state do not replace MySQL/ProxySQL/Prometheus queries +and do not establish application health. + +YAML `docker_http` execution permits only fixed container-list and inspect GET +paths over a server-configured Unix socket, scoped to an exact Compose project. +The runtime verifies project scope, resolves names to immutable IDs, applies one +deadline and byte bounds, and excludes configuration/credential fields from the +returned state projection. The result uses the existing `RawHttpToolResult` +envelope; Report receives it unchanged with optional separate comments. +Socket access is an independent deployment security decision, not authorized by +the tool's read-only flag. See [container reader details](../../system-container-tools.md). + +### DiagnosisHandoff + +Diagnostic Agent returns a runtime-assembled DiagnosisHandoff. ~~~ts -interface DiagnosisResult { - readonly symptom: string; - readonly environment: "dev" | "prod"; - readonly inferredDomains: readonly { - readonly domain: string; - readonly reason: string; +interface DiagnosisHandoff { + readonly diagnosis_id: string; + readonly request: string; + readonly completion_reason: "investigation_complete" | "insufficient_evidence" | "mock"; + readonly observations: readonly { + 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: SanitizedToolError | null; + readonly comment: { + readonly observation: string | null; + readonly hypothesis: string | null; + readonly limitation: string | null; + } | null; + readonly related_call_ids: readonly string[]; }[]; - readonly observations: readonly ToolObservation[]; - readonly preliminaryFindings: readonly { - readonly finding: string; - readonly basis: readonly string[]; - readonly level: "normal" | "warning" | "critical" | "unknown"; - }[]; - readonly suspectedCauses: readonly { - readonly cause: string; - readonly reason: string; - readonly confidence: "low" | "medium" | "high"; - }[]; - readonly recommendedChecks: readonly string[]; - readonly incompleteReasons: readonly string[]; } ~~~ -Example diagnosis wording: - -~~~text -Backend 로그에서 최근 10분간 database connection timeout 패턴이 반복적으로 확인됨. -Prometheus 기준 Backend 5xx 비율이 직전 baseline보다 높음. -ProxySQL backend server 상태는 정상으로 확인됨. -현재로서는 Backend와 MySQL 연결 구간 문제가 우선 의심됨. -~~~ +Rules: + +- AMDC가 진단별 ID, 실행 순번·ID, tool/plugin, 입력, 결과·오류를 기록한다. + 호출 ID는 provider의 ID가 아니라 `diagnosis_id:call-N`인 AMDC 소유 ID다. +- 순번은 wrapper 진입 순서이며 병렬 완료 순서나 인과관계를 의미하지 않는다. +- 모델은 `completion_reason`과 실제 ID에 연결하는 `comments`만 생성한다. + comment와 각 세부 필드는 null을 허용한다. comment가 없는 실행도 누락하지 않는다. +- 가설은 확정 사실이 아니다. Report Agent는 원본 결과를 우선하고 comment는 참고한다. +- 관련 호출은 순서와 무관하게 같은 진단에 존재하는 다른 ID를 허용한다. 병렬·후속 + 조회 근거도 연결할 수 있으며 인과관계를 의미하지 않는다. 미존재/중복/self 참조는 + 조립 오류로 거부한다. 모델의 결과·입력·시각 덮어쓰기는 schema에서 거부한다. +- 성공은 `result`와 `error: null`, 실패는 `result: null`과 sanitized `error`다. + 성공한 빈 결과도 보존하며, 실행 성공은 서비스 정상 판정이 아니다. +- `observed_at`은 adapter의 collectedAt 또는 오류 occurredAt이다. 시계열의 실제 + 관측 구간은 result 내부 데이터를 확인해야 한다. referenceTime과 조회 시각은 다르다. +- `completion_reason`은 모델의 조사 종료 설명이며 root cause/health 판정이 아니다. + 도구 실행이 0건이면 `insufficient_evidence`로 강제한다. `mock`은 mock runner만 쓴다. +- model/provider 실패와 잘못된 최종 참조는 기존 오류 경로로 종료한다. 부분 handoff의 + 영속 저장·재개는 이번 범위가 아니다. +- plugin 선택·모델 호출은 trace에만 남긴다. wrapper에 진입한 조회 및 중복 거부는 + 기록한다. wrapper 진입 전 LangChain schema 검증/미선택 plugin 거부는 이 ledger의 + 실행 기록에 포함되지 않는다. 모든 시도 감사 기록과는 구분한다. +- 입력·결과·최종 handoff에 configured secret/private-key/Bearer 검사를 적용한다. + 이 검사는 자유형 comment의 사실성 검증 또는 완전한 DLP를 의미하지 않는다. ## Report Agent Boundary @@ -494,8 +535,8 @@ Expected behavior: without fabricating observations. - Tool Runtime rejects unknown Tool names and invalid args. - Tool Runtime returns sanitized ToolObservation or SanitizedToolError. -- DiagnosisResult contains inferred domain, observation, preliminary finding, - suspected cause, and recommended check. +- DiagnosisHandoff preserves runtime execution records and separate optional comments, + rejecting fabricated references without a final health verdict or recommended actions. - Existing Discord adapter can call `runDiagnosis()` without knowing Tool Runtime internals. - Typecheck and build pass. diff --git a/docs/prd/current/README.md b/docs/prd/current/README.md index 3c2d647..115bdb3 100644 --- a/docs/prd/current/README.md +++ b/docs/prd/current/README.md @@ -8,6 +8,10 @@ Delivery Status: not_started Architecture decision date: 2026-07-07 Last reviewed: 2026-07-22 +2026-09-19 manual handoff update: PRD 06의 Diagnostic Agent Output에 수동 진단의 +실행 기록 + 선택적 comment 인계 계약을 정의했다. PRD 03의 최종 7-field Report와 +동일한 객체가 아니며, 자동 Trigger/Report Agent/영속 저장 완료를 뜻하지 않는다. + 이 폴더의 문서만 현재 P0 개발 계약으로 사용한다. Notion, archive, 과거 배포본, 기존 MCP config/fixture/script는 의사결정 이력이나 실험 근거일 뿐 구현 기준이 아니다. 과거 자료의 아이디어는 numbered PRD에 반영된 뒤에만 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 046f486..3722369 100644 --- a/src/agent/langchain-diagnostic-agent.ts +++ b/src/agent/langchain-diagnostic-agent.ts @@ -8,25 +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, - 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; @@ -43,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); @@ -65,7 +58,8 @@ export class LangChainDiagnosticAgent implements DiagnosticAgentPort { environment: input.environment, referenceTime, runId, - traceSink + traceSink, + ledger }); const pluginMiddleware = createPluginLazyLoadingMiddleware(this.registry, { runId, @@ -105,31 +99,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, - 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; @@ -151,7 +128,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.", @@ -161,7 +144,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"); } @@ -180,77 +168,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"); } - -function extractToolArtifacts(messages: readonly unknown[]): { - readonly observations: readonly ToolObservation[]; - readonly toolErrors: readonly SanitizedToolError[]; - readonly toolNames: ReadonlySet; -} { - const observations: ToolObservation[] = []; - 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 === false && isRecord(parsed.error)) { - toolErrors.push(parsed.error as unknown as SanitizedToolError); - } - } catch { - continue; - } - } - - return { observations, 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 cd3ccd4..34ac887 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; } export function createAmdcLangChainTools( @@ -19,9 +21,11 @@ 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); return descriptors.map((descriptor) => tool( async (args: Record) => { + const call = ledger.begin(descriptor, args); const startedAt = Date.now(); await context.traceSink.record({ event: "tool.started", @@ -53,7 +57,18 @@ export function createAmdcLangChainTools( environment: context.environment, referenceTime: context.referenceTime } - ); + ).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() + } + })); + + ledger.finish(call, result); await context.traceSink.record({ event: "tool.finished", @@ -72,7 +87,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, diff --git a/src/agent/types.ts b/src/agent/types.ts index f09e9ac..535b14f 100644 --- a/src/agent/types.ts +++ b/src/agent/types.ts @@ -1,11 +1,5 @@ import type { AmdcEnvironment } from "../config/env.js"; -import type { - AgentVisibleToolDescriptor, - ObservationStatus, - PluginName, - SanitizedToolError, - ToolObservation -} from "../tools/types.js"; +import type { DiagnosisHandoff } from "../report/diagnosis-handoff.js"; export interface DiagnosticAgentInput { readonly symptom: string; @@ -14,35 +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 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 d85572e..231afac 100644 --- a/src/report/temporary-diagnostic-presenter.ts +++ b/src/report/temporary-diagnostic-presenter.ts @@ -1,37 +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: - diagnosis.toolErrors.length > 0 - ? "insufficient_tools" - : hasCritical || hasWarning - ? "problem_detected" - : "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..1acf99a --- /dev/null +++ b/tests/diagnosis-handoff.test.ts @@ -0,0 +1,166 @@ +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("parallel tool completion preserves dispatch order and exposes matching IDs to the model", async () => { + const ledger = new DiagnosisLedger("diag-parallel"); + let finishFirst!: () => void; + const gate = new Promise(resolve => { finishFirst = resolve; }); + const [tool] = createAmdcLangChainTools([descriptor], { execute: async request => { + if ((request.args as { connectionId: number }).connectionId === 1) await gate; + else finishFirst(); + 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.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/); +});