/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * Default `service.name` per connector protocol/key, mirroring `AGENTS.md`'s
 * "Trace correlation conventions" table. Used when the agent config doesn't
 * set an explicit `traceServiceName`. Keyed by `string` rather than
 * `ConnectorProtocol` so user-configured connectors (e.g. `kiro`, which
 * isn't in the upstream union) still resolve to a default.
 */
const PROTOCOL_TO_SERVICE_NAME = {
    'claude-code': 'claude-code-agent',
    'kiro': 'kiro-agent',
    'pi': 'pi-agent',
    'agui-streaming': 'observio-sample-agent',
    'subprocess': 'subprocess-agent',
};
/** Time-window slack on each side of the run window to absorb clock skew. */
const SLACK_MS = 60_000;
/**
 * Lookback used when `report.performanceMetrics.durationMs` is missing
 * (older runs persisted before that field). Wide enough to cover any
 * realistic run, narrow enough to keep noise on a shared OTel cluster
 * minimal. Mirrors the run-detail Traces tab fallback.
 */
const FALLBACK_LOOKBACK_MS = 30 * 60_000;
/**
 * Resolve the `service.name` an agent emits OTel spans under. Priority:
 *   1. Explicit `agentConfig.traceServiceName` (the per-agent override).
 *   2. Protocol-default from the table above.
 *   3. `<connectorProtocol>-agent` heuristic (last-resort fallback).
 *   4. `<agentKey>-agent` as a final guess when protocol is unknown.
 *
 * Returns `undefined` when none of the above is derivable; the caller
 * should then skip Strategy C and fall back to Strategy B alone.
 */
export function resolveAgentServiceName(args) {
    if (args.agentTraceServiceName)
        return args.agentTraceServiceName;
    if (args.connectorProtocol && PROTOCOL_TO_SERVICE_NAME[args.connectorProtocol]) {
        return PROTOCOL_TO_SERVICE_NAME[args.connectorProtocol];
    }
    if (args.connectorProtocol)
        return `${args.connectorProtocol}-agent`;
    if (args.agentKey)
        return `${args.agentKey}-agent`;
    return undefined;
}
/**
 * Build the `agents: [{...}]` array to forward as `JudgeRequest.agents`.
 *
 * `endedAt` defaults to the report's persisted timestamp; `startedAt`
 * defaults to that minus `performanceMetrics.durationMs` (with slack), or
 * a 30-minute lookback when duration is missing. The trace tool sends
 * these through to `/api/traces` which intersects them with
 * `service.name = serviceName`.
 *
 * Returns an empty array when we can't derive a meaningful service name —
 * the caller can still pass that and the route just falls back to
 * Strategy B (runIds-only) without breaking.
 */
export function buildJudgeAgentsHints(report, agentTraceServiceName) {
    const serviceName = resolveAgentServiceName({
        agentTraceServiceName,
        connectorProtocol: report.connectorProtocol,
        agentKey: report.agentKey,
    });
    if (!serviceName)
        return [];
    const endedAt = Date.parse(report.timestamp || '') || Date.now();
    const durationMs = report.performanceMetrics?.durationMs ?? 0;
    const lookbackMs = durationMs > 0 ? durationMs + SLACK_MS : FALLBACK_LOOKBACK_MS;
    const startedAt = endedAt - lookbackMs;
    return [{ serviceName, startedAt, endedAt: endedAt + SLACK_MS }];
}
//# sourceMappingURL=judgeAgentsHints.js.map