const MILESTONE_CONTEXT_RE = /M\d+(?:-[a-z0-9]{6})?-CONTEXT\.md$/;
let depthVerificationDone = false;
let activeQueuePhase = false;
export function isDepthVerified() {
    return depthVerificationDone;
}
export function isQueuePhaseActive() {
    return activeQueuePhase;
}
export function setQueuePhaseActive(active) {
    activeQueuePhase = active;
}
export function resetWriteGateState() {
    depthVerificationDone = false;
}
export function clearDiscussionFlowState() {
    depthVerificationDone = false;
    activeQueuePhase = false;
}
export function markDepthVerified() {
    depthVerificationDone = true;
}
export function shouldBlockContextWrite(toolName, inputPath, milestoneId, depthVerified, queuePhaseActive) {
    if (toolName !== "write")
        return { block: false };
    const inDiscussion = milestoneId !== null;
    const inQueue = queuePhaseActive ?? false;
    if (!inDiscussion && !inQueue)
        return { block: false };
    if (!MILESTONE_CONTEXT_RE.test(inputPath))
        return { block: false };
    if (depthVerified)
        return { block: false };
    return {
        block: true,
        reason: `Blocked: Cannot write to milestone CONTEXT.md during discussion phase without depth verification. Call ask_user_questions with question id "depth_verification" first to confirm discussion depth before writing context.`,
    };
}
