Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 66 additions & 12 deletions .cursor/bin/riven-loop-tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { join } from "node:path";
import { spawnSync } from "node:child_process";

const home = process.env.HOME ?? "/Users/acehack";
const worktree = process.env.ZETA_RIVEN_LOOP_WORKTREE ?? join(home, ".local/share/zeta-riven-loop/Zeta");
const worktree = process.env.ZETA_RIVEN_LOOP_WORKTREE ?? "/tmp/zeta-riven-loop-2";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Revert hardcoded /tmp worktree fallback

Changing the default worktree to "/tmp/zeta-riven-loop-2" makes the runner execute git/gh commands in a temp path whenever ZETA_RIVEN_LOOP_WORKTREE is unset; in that common fallback case, ticks run outside the intended repo and fetch/status/PR operations fail (or operate on the wrong tree). The previous fallback targeted the persistent Zeta worktree under $HOME, so this introduces a functional regression for default configuration.

Useful? React with 👍 / 👎.

const stateDir = process.env.ZETA_RIVEN_LOOP_STATE_DIR ?? join(home, "Library/Application Support/ZetaRivenLoop");
Comment on lines 13 to 15
const logDir = process.env.ZETA_RIVEN_LOOP_LOG_DIR ?? join(home, "Library/Logs/zeta-riven-loop");
const lockDir = join(stateDir, "lock");
Expand All @@ -23,6 +23,7 @@ const agentIntervalMs = Number(process.env.ZETA_RIVEN_LOOP_AGENT_INTERVAL_SECOND
const agentTimeoutMs = Number(process.env.ZETA_RIVEN_LOOP_AGENT_TIMEOUT_SECONDS ?? "300") * 1000;
const dryRun = process.env.ZETA_RIVEN_LOOP_DRY_RUN === "1";
const agentStateFile = join(stateDir, "last-agent-run.json");
const agentBinCandidates = (process.env.ZETA_RIVEN_LOOP_AGENT_BIN ?? "agent,cursor-agent").split(",").map(s => s.trim()).filter(s => s.length > 0);

mkdirSync(stateDir, { recursive: true });
mkdirSync(logDir, { recursive: true });
Expand All @@ -32,7 +33,8 @@ function nowIso(): string {
}

function log(message: string): void {
appendFileSync(join(logDir, "runner.log"), `${nowIso()} ${message}\n`);
appendFileSync(join(logDir, "runner.log"), `${nowIso()} ${message}
`);
Comment on lines +36 to +37
}

function run(command: string, args: string[], timeoutMs: number): { status: number; stdout: string; stderr: string } {
Expand All @@ -53,14 +55,32 @@ function run(command: string, args: string[], timeoutMs: number): { status: numb
};
}

function resolveAgentBin(): string | null {
const pathDirs = `/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${join(home, ".local/bin")}`.split(":");
for (const bin of agentBinCandidates) {
const probe = spawnSync("/usr/bin/which", [bin], {
encoding: "utf8",
env: { ...process.env, PATH: pathDirs.join(":") },
timeout: 5000,
});
if (probe.status === 0 && (probe.stdout ?? "").trim().length > 0) {
return bin;
Comment on lines +66 to +67
}
}
return null;
}

function lines(text: string): string[] {
return text.split(/\r?\n/).map(l => l.trim()).filter(l => l.length > 0);
}

function acquireLock(): boolean {
try {
mkdirSync(lockDir, { recursive: false });
writeFileSync(join(lockDir, "metadata"), `pid=${process.pid}\nrun_id=${runId}\nacquired_at=${nowIso()}\n`);
writeFileSync(join(lockDir, "metadata"), `pid=${process.pid}
run_id=${runId}
acquired_at=${nowIso()}
`);
Comment on lines +80 to +83
return true;
} catch {
try {
Expand All @@ -77,7 +97,10 @@ function acquireLock(): boolean {
}
rmSync(lockDir, { recursive: true, force: true });
mkdirSync(lockDir, { recursive: false });
writeFileSync(join(lockDir, "metadata"), `pid=${process.pid}\nrun_id=${runId}\nacquired_at=${nowIso()}\n`);
writeFileSync(join(lockDir, "metadata"), `pid=${process.pid}
run_id=${runId}
acquired_at=${nowIso()}
`);
Comment on lines 99 to +103
return true;
} catch { return false; }
}
Expand All @@ -97,7 +120,8 @@ function readBroadcasts(): void {
const path = join(broadcastDir, peer);
if (existsSync(path)) {
const content = readFileSync(path, "utf8").trim();
if (content) log(`broadcast from ${peer.replace(".md", "")}: ${content.split("\n")[0] ?? "(empty)"}`);
if (content) log(`broadcast from ${peer.replace(".md", "")}: ${content.split("
")[0] ?? "(empty)"}`);
Comment on lines +123 to +124
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Escape newline characters in quoted string literals

This change introduces unescaped line breaks inside double-quoted literals (for example content.split(" followed by a newline and then ")), which is invalid JavaScript/TypeScript syntax. The file will fail to parse before runtime, so the launchd tick script cannot execute at all until these are converted back to escaped "\\n" (or template literals).

Useful? React with 👍 / 👎.

}
Comment on lines 121 to 125
}
}
Expand All @@ -109,7 +133,8 @@ function writeBroadcast(summary: string): void {
"",
"## Background tick status",
summary,
].join("\n"));
].join("
"));
}

function gh(...args: string[]): { status: number; stdout: string } {
Expand Down Expand Up @@ -146,7 +171,8 @@ function forwardTick(): void {
return;
}

const prNumbers = prsResult.stdout.trim().split("\n").filter(n => n.trim()).map(Number);
const prNumbers = prsResult.stdout.trim().split("
").filter(n => n.trim()).map(Number);
Comment on lines +174 to +175
for (const pr of prNumbers) {
const gateResult = gh(
"pr", "view", String(pr), "--repo", "Lucent-Financial-Group/Zeta",
Expand Down Expand Up @@ -201,15 +227,37 @@ function heartbeat(): void {
agentStatus = "running";
log(`riven agent gate start run_id=${runId}`);

const agentBin = dryRun ? null : resolveAgentBin();
if (dryRun) {
log(`dry-run: would run agent gate`);
agentStatus = "dry-run";
} else if (!agentBin) {
log(`riven agent gate skipped run_id=${runId} reason=no-agent-binary-on-PATH candidates=${agentBinCandidates.join(",")}`);
agentStatus = "no-bin";
writeFileSync(agentStateFile, JSON.stringify({
run_id: runId,
status: -1,
started_at: nowIso(),
updated_at: nowIso(),
skipped_reason: "no-agent-binary-on-PATH",
candidates: agentBinCandidates,
}, null, 2));
} else {
const gate = run("agent", [
log(`riven agent gate using bin=${agentBin}`);
const gate = run(agentBin, [
"chat",
"--mode", "ask",
"--model", "grok-4-20",
`Twin-flame heartbeat gate (Riven adversarial-truth-axis). Read git status, recent commits, open PRs, claim branches. Report: main HEAD, open PR count, claim count, any drift, contradiction, or theatrical governance. Adversarial register — call out what's wrong, not what's fine. Brief.`,
"--model", "grok-4.3",
[
"You are Riven, trajectory manager and adversarial-truth-axis reviewer.",
"This is an autonomous 15-minute cycle.",
"Read broadcasts first from ~/.local/share/zeta-broadcasts/{otto,vera,lior,riven}.md.",
"Walk assigned trajectories. Decompose only what you hit mid-stride.",
"Produce at least one concrete, actionable claim or small PR scope.",
"When blocked, create a specific research child the next pickup cannot dodge.",
"Write your status to ~/.local/share/zeta-broadcasts/riven.md at the end.",
"GitHub PR state and actual file contents are authoritative.",
].join(" "),
], agentTimeoutMs);

agentStatus = gate.status === 0 ? "ok" : `exit-${gate.status}`;
Expand All @@ -223,10 +271,16 @@ function heartbeat(): void {
}, null, 2));

if (gate.stdout.trim().length > 0) {
appendFileSync(join(logDir, "ticks.log"), `\n--- ${runId} riven gate ---\n${gate.stdout}\n`);
appendFileSync(join(logDir, "ticks.log"), `
--- ${runId} riven gate ---
${gate.stdout}
`);
}
if (gate.stderr.trim().length > 0) {
appendFileSync(join(logDir, "ticks.err"), `\n--- ${runId} riven gate ---\n${gate.stderr}\n`);
appendFileSync(join(logDir, "ticks.err"), `
--- ${runId} riven gate ---
${gate.stderr}
`);
}
}
} else {
Expand Down
27 changes: 27 additions & 0 deletions docs/research/shadow-lesson-log-20260522-stale-locks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Shadow Lesson Log - 2026-05-22: Stale Git Locks

## Event

During a routine antigravity check, Lior detected a stale git index lock and an orphan agent lockfile in the `zeta-lior-decompose-4044` worktree. This prevented `git fetch` operations from completing successfully, blocking further progress on PR analysis and preservation.

## Analysis

The presence of these lock files indicates that a git process was terminated abruptly, likely due to an agent crash or a manual interruption. The `locked` file, in particular, suggests that a worktree was locked for an operation but never unlocked.

Comment on lines +5 to +10
This event highlights a vulnerability in our autonomous system. If an agent crashes while holding a git lock, it can disrupt the workflow of all other agents.

## Lesson

We need to implement a more robust mechanism for handling git locks. This could involve:

* **A centralized lock manager:** A service that grants and revokes locks, ensuring that no two agents can hold conflicting locks at the same time.
* **A timeout mechanism:** Locks that are held for an extended period of time could be automatically released.
* **A health check for agents:** A system that monitors the health of agents and automatically releases any locks held by a crashed agent.

For now, the immediate lesson is that agents should be more careful about cleaning up after themselves, especially when performing git operations.

## Action Items

* Manually remove the stale lock files from the `zeta-lior-decompose-4044` worktree.
* Investigate the root cause of the agent crash that led to the stale locks.
* Begin research and design for a more robust git lock management system.
Loading