File size: 1,874 Bytes
a3aed04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * TIA-∞ Worker Loop (Heartbeat Engine)
 * Runs periodic scans, lineage mapping, drift detection,
 * resurrection planning, and guided queue generation.
 * Non-destructive. Safe. Approval-based.
 */

import { scanQGTNL } from "./scanner";
import { generateLineageReport } from "./lineage";
import { generateResurrectionPlan } from "./resurrection";
import { generateDriftReport } from "./drift";
import { buildGuidedQueue } from "./guided";
import { getMode } from "./mode";

export interface WorkerStatus {
  running: boolean;
  lastCycle: number | null;
  cyclesCompleted: number;
}

let status: WorkerStatus = {
  running: false,
  lastCycle: null,
  cyclesCompleted: 0,
};

export function getWorkerStatus(): WorkerStatus {
  return status;
}

export async function startWorkerLoop(intervalMs = 30000) {
  if (status.running) {
    console.log("Worker loop already running.");
    return;
  }

  status.running = true;
  console.log("=== TIA-∞ Worker Loop Started ===");

  while (status.running) {
    console.log("\n=== TIA-∞ Cycle Begin ===");

    const mode = getMode().current;
    console.log("Mode:", mode);

    const scan = scanQGTNL();
    if (scan.status !== "ok") {
      console.log("Scan failed:", scan.message);
      break;
    }

    const lineage = generateLineageReport(scan.structure);
    const resurrection = generateResurrectionPlan(lineage);
    const drift = generateDriftReport(lineage);
    const queue = buildGuidedQueue(resurrection, drift);

    console.log(`Cycle results: ${queue.prompts.length} actionable prompts.`);

    status.lastCycle = Date.now();
    status.cyclesCompleted++;

    console.log("=== TIA-∞ Cycle End ===");

    await new Promise((resolve) => setTimeout(resolve, intervalMs));
  }

  console.log("=== TIA-∞ Worker Loop Stopped ===");
}

export function stopWorkerLoop() {
  status.running = false;
}