export const PROMISE_TOKENS = [
    'EPIC_COMPLETED',
    'TASK_COMPLETED',
    'ANALYSIS_DONE',
    'EXISTENCE_IS_PAIN',
    'THE_CITADEL_APPROVES',
    'WORKER_DONE',
    'PRD_COMPLETE',
    'TICKET_SELECTED',
];
/**
 * Promise tokens a per-ticket worker (send-to-morty.md / send-to-morty-review.md)
 * is FORBIDDEN from emitting. The worker's only valid completion signal is
 * `<promise>I AM DONE</promise>`. Every other token is orchestrator-scoped.
 *
 * This list is intentionally a literal of token *string values* (not the
 * symbolic constant names from PROMISE_TOKENS) because:
 * - PROMISE_TOKENS contains `WORKER_DONE` whose string value is `'I AM DONE'` —
 *   that's the worker's valid token, NOT a forbidden one.
 * - Hardcoding the values keeps the forbidden set obvious and grep-friendly.
 *
 * Used by `scrubForbiddenWorkerTokens` to rewrite worker-emitted log content
 * before the manager (or any downstream consumer) reads it.
 */
export const FORBIDDEN_WORKER_TOKENS = [
    'EPIC_COMPLETED',
    'TASK_COMPLETED',
    'PRD_COMPLETE',
    'TICKET_SELECTED',
    'EXISTENCE_IS_PAIN',
    'THE_CITADEL_APPROVES',
    'ANALYSIS_DONE',
];
/**
 * Rewrite worker-emitted promise tokens that the worker has no authority to emit.
 *
 * A per-ticket worker MUST signal completion only via `<promise>I AM DONE</promise>`.
 * If a worker (especially a codex backend, which has been observed to confuse
 * tokens it sees nearby in source/prompt context) emits a forbidden token like
 * `<promise>EPIC_COMPLETED</promise>`, the manager reading the log can parrot it
 * forward, tripping mux-runner's pending-tickets fail-loud guard and killing the
 * pipeline mid-epic.
 *
 * Pure: no I/O, no globals, no side effects. Caller owns the read+write.
 *
 * Whitespace inside the tags is tolerated (matches `hasToken` semantics).
 */
export function scrubForbiddenWorkerTokens(content) {
    if (!content)
        return { scrubbed: content, replacements: {} };
    const replacements = {};
    let scrubbed = content;
    for (const token of FORBIDDEN_WORKER_TOKENS) {
        const escaped = token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
        const re = new RegExp(`<promise>\\s*${escaped}\\s*</promise>`, 'g');
        let count = 0;
        scrubbed = scrubbed.replace(re, () => { count++; return '<promise>I AM DONE</promise>'; });
        if (count > 0)
            replacements[token] = count;
    }
    return { scrubbed, replacements };
}
