/**
 * Phase 2 — stale compiled-truth.
 *
 * Finds `:Person | :Organization | :Concept` entities whose
 * `compiledTruthUpdatedAt` is more than 180 days old AND that have at
 * least three new `:TimelineEvent` rows linked since the last compile.
 *
 * Surfaces to the operator-review queue (returns the list as `deferred`).
 * Does NOT auto-dispatch the compiled-truth rewriter — operator triggers
 * that manually on the entities that warrant it.
 *
 * No new label is written: phase 2 is read-only. The returned list is
 * embedded in the dream-cycle `:Report` body so the operator can see
 * which entities need re-compiling.
 */
const STALE_LABELS = ["Person", "Organization", "Concept"];
const STALE_DAYS = 180;
const MIN_NEW_EVENTS = 3;
export async function phaseStaleTruth(ctx) {
    const start = Date.now();
    const { accountId, session } = ctx;
    const stale = [];
    for (const label of STALE_LABELS) {
        const result = await session.run(`MATCH (n:${label})
       WHERE n.accountId = $accountId
         AND NOT n:Trashed
         AND n.compiledTruthUpdatedAt IS NOT NULL
         AND n.compiledTruthUpdatedAt < datetime() - duration({days: $days})
       OPTIONAL MATCH (n)-[:HAS_TIMELINE_EVENT]->(t:TimelineEvent)
         WHERE t.createdAt > n.compiledTruthUpdatedAt
       WITH n, count(t) AS newEventCount
       WHERE newEventCount >= $minEvents
       RETURN elementId(n) AS elementId,
              coalesce(n.name, n.title, '<unnamed>') AS name,
              toString(n.compiledTruthUpdatedAt) AS compiledTruthUpdatedAt,
              newEventCount`, { accountId, days: STALE_DAYS, minEvents: MIN_NEW_EVENTS });
        for (const rec of result.records) {
            stale.push({
                label,
                elementId: String(rec.get("elementId")),
                name: String(rec.get("name")),
                compiledTruthUpdatedAt: String(rec.get("compiledTruthUpdatedAt")),
                newEventCount: Number(rec.get("newEventCount")),
            });
        }
    }
    const durationSec = Math.round((Date.now() - start) / 1000);
    const proposed = stale.length;
    process.stderr.write(`[dream-cycle:phase-2] durationSec=${durationSec} proposed=${proposed} applied=0 deferred=${proposed}\n`);
    return {
        phase: 2,
        name: "stale-truth",
        durationSec,
        proposed,
        applied: 0,
        deferred: proposed,
        extra: { stale },
    };
}
//# sourceMappingURL=phase-stale-truth.js.map