/**
 * Judgment tools — expose log_decision to the agent in autonomous mode.
 *
 * The agent is instructed (via the system prompt) to call this tool when
 * making non-trivial calls so the user can review reasoning at milestone close.
 */
import { Type } from "@sinclair/typebox";
import { appendJudgment } from "../judgment-log.js";
export function registerJudgmentTools(pi) {
	pi.registerTool({
		name: "log_decision",
		label: "Log Judgment",
		description:
			"Record a judgment call made at an ambiguous decision point for user review at milestone close. " +
			"Returns immediately with no blocking effect. " +
			"Use this whenever you choose between valid alternatives — approach, scope, trade-off — so the rationale is visible for review.",
		promptSnippet:
			"Log a judgment call: decision taken, alternatives considered, reasoning, confidence",
		promptGuidelines: [
			"Call whenever you choose one approach over plausible alternatives.",
			"Set confidence=low when the decision is speculative or you lacked context.",
			"Set confidence=high when you had strong evidence for the choice.",
			"This call is fire-and-forget — never wait for it or re-read the log mid-task.",
		],
		parameters: Type.Object({
			unitId: Type.String({
				description:
					"Current unit ID (e.g. M001/S01/T01). Use the active task ID.",
			}),
			decision: Type.String({
				description: "Short description of the decision taken (1-2 sentences).",
			}),
			alternatives: Type.Array(Type.String(), {
				description: "Alternatives that were considered but not chosen.",
			}),
			reasoning: Type.String({
				description:
					"Why this decision was made over the alternatives (1-3 sentences).",
			}),
			confidence: Type.Union(
				[Type.Literal("low"), Type.Literal("medium"), Type.Literal("high")],
				{
					description:
						"Agent confidence in the decision: low = speculative, medium = reasonable, high = well-evidenced.",
				},
			),
		}),
		execute: async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
			appendJudgment(process.cwd(), {
				unitId: params.unitId,
				decision: params.decision,
				alternatives: params.alternatives,
				reasoning: params.reasoning,
				confidence: params.confidence,
			});
			return {
				content: [
					{
						type: "text",
						text: `Judgment logged for unit ${params.unitId}: "${params.decision}" (confidence: ${params.confidence})`,
					},
				],
				details: {
					operation: "judgment_log",
					unitId: params.unitId,
					confidence: params.confidence,
				},
			};
		},
	});
}
