diff --git a/CHANGELOG.md b/CHANGELOG.md index 790ead68..1861c199 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased — OpenTUI 0.8 compatibility source update +- Create `.opencode/opencode-loop/.gitignore` containing an explanatory comment and `*` whenever Loop first creates its state directory, so runtime state, checkpoints, and logs cannot be committed accidentally. An existing `.gitignore` is never overwritten. - Extend optional `@opentui/core` and `@opentui/solid` peer support to 0.8.x while preserving 0.5.14–0.7.x; raise the Solid peer minimum to upstream 1.9.12 (recommend 1.9.16+ for the security update). - Synchronize the npm lockfile's root manifest with package version 0.6.10 and new peer constraints. No bundled renderer, server entry, or scheduler authority changes. - Keep the existing strict packaged-consumer peer assertion and provide manual Bun rendering validation for OpenTUI 0.8.1 before claiming runtime verification. CI/Actions and automatic npm releases remain disabled; npm 0.6.10 is unchanged. diff --git a/README.md b/README.md index 6627fdc2..bc466fad 100644 --- a/README.md +++ b/README.md @@ -455,7 +455,7 @@ Patch checkpoints: .opencode/opencode-loop/checkpoints/ ``` -Add `.opencode/opencode-loop/` to `.gitignore` if runtime state should not be committed. +Loop automatically creates `.opencode/opencode-loop/.gitignore` containing an explanatory comment and `*` when it first creates the state directory, so runtime state, checkpoints, and logs are not committed by accident. An existing `.gitignore` in that directory is left untouched. ## Permissions diff --git a/docs/SCHEDULING.md b/docs/SCHEDULING.md index 6594b5d3..f0ca18f2 100644 --- a/docs/SCHEDULING.md +++ b/docs/SCHEDULING.md @@ -266,6 +266,8 @@ Normal plugin Loop jobs are session-bound and persist under: .opencode/opencode-loop/.json ``` +The state directory is created with a `.gitignore` containing an explanatory comment and `*`, so session state, logs, and checkpoints stay out of the repository automatically. An existing `.gitignore` is never overwritten. + Starting a new OpenCode session does not move old Loop jobs into the new session. `/loop-doctor` reports: diff --git a/scripts/pulse-check.mjs b/scripts/pulse-check.mjs index 7204f7aa..30aaaf51 100644 --- a/scripts/pulse-check.mjs +++ b/scripts/pulse-check.mjs @@ -1,6 +1,7 @@ import { promises as fs } from "node:fs" import path from "node:path" import { dedicatedGoalOwnsContinuation } from "../src/source/runtime/companion-goal.js" +import { ensureStateDir } from "../src/source/core/state.js" const LOCK_STALE_MS = 30_000 const LOCK_RETRY_MS = 25 @@ -96,9 +97,8 @@ async function writeJsonAtomic(file, value) { } async function acquireLock(project) { - const directory = pulseDirectory(project) const lock = pulseLockDirectory(project) - await fs.mkdir(directory, { recursive: true }) + await ensureStateDir(project) for (let attempt = 0; attempt < LOCK_RETRIES; attempt += 1) { try { diff --git a/scripts/state-integrity-test.mjs b/scripts/state-integrity-test.mjs index 56cfaabc..e12824fe 100644 --- a/scripts/state-integrity-test.mjs +++ b/scripts/state-integrity-test.mjs @@ -20,6 +20,16 @@ try { assert.equal(oldSnapshot.jobs.length, 1) assert.equal(oldSnapshot.jobs[0].runCount, 7) + const ignoreFile = path.join(stateDir(root), ".gitignore") + assert.equal( + await readFile(ignoreFile, "utf8"), + "# OpenCode Loop runtime state and logs; do not commit this directory.\n*\n", + "state dir must ignore its runtime files automatically with an explanatory comment", + ) + await writeFile(ignoreFile, "custom-ignore\n") + await writeState(root, sessionID, oldSnapshot) + assert.equal(await readFile(ignoreFile, "utf8"), "custom-ignore\n", "an existing .gitignore must never be overwritten") + const corrupt = '{"version":4,"jobs":[' await mkdir(stateDir(root), { recursive: true }) await writeFile(file, corrupt) diff --git a/src/index.js b/src/index.js index 2dcd5dc5..51708789 100644 --- a/src/index.js +++ b/src/index.js @@ -391,6 +391,10 @@ import { randomUUID as stateLockRandomUUID } from "crypto"; import os from "os"; import path from "path"; var STATE_DIR = ".opencode/opencode-loop"; +var STATE_IGNORE_FILE = ".gitignore"; +var STATE_IGNORE_CONTENT = `# OpenCode Loop runtime state and logs; do not commit this directory. +* +`; var STATE_BASELINE = Symbol("opencode-loop-state-baseline"); var stateWriteLocks = new Map; function stateDir(directory) { @@ -402,6 +406,17 @@ function statePath(directory, sessionID) { async function ensureDir(directory) { await fs.mkdir(directory, { recursive: true }); } +async function ensureStateDir(directory) { + const target = stateDir(directory); + await ensureDir(target); + try { + await fs.writeFile(path.join(target, STATE_IGNORE_FILE), STATE_IGNORE_CONTENT, { flag: "wx" }); + } catch (error) { + if (error?.code !== "EEXIST") + throw error; + } + return target; +} async function pathExists(filePath) { try { await fs.access(filePath); @@ -588,7 +603,7 @@ async function recoverDeadStateProcessLock(directory, canonical, stale, depth = async function acquireStateProcessLock(directory, sessionID) { const targetDirectory = stateDir(directory); await assertStatePathSafe(directory, targetDirectory); - await ensureDir(targetDirectory); + await ensureStateDir(directory); await assertStatePathSafe(directory, targetDirectory); const target = statePath(directory, sessionID); const canonical = `${target}.lock`; @@ -680,7 +695,7 @@ async function readStateFile(directory, sessionID, { backupCorrupt = true } = {} } if (backupCorrupt) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs.copyFile(target, `${target}.corrupt-${Date.now()}`); } catch {} } @@ -845,7 +860,7 @@ async function removeState(directory, sessionID) { // src/source/core/process.js async function appendLoopLog(directory, line, extra = {}) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs2.appendFile(path2.join(stateDir(directory), "loop.log"), JSON.stringify({ time: new Date().toISOString(), line, ...extra }) + ` `); } catch {} @@ -1672,6 +1687,8 @@ async function writeGoalReport(directory, sessionID, job) { if (!isGoalJob(job)) return; const target = job.goalEvidenceFile ? path3.resolve(directory, job.goalEvidenceFile) : goalReportPath(directory, sessionID, job); + if (!job.goalEvidenceFile) + await ensureStateDir(directory); await ensureDir(path3.dirname(target)); await fs3.writeFile(target, goalReportText(job), "utf8"); } @@ -3133,6 +3150,7 @@ ${text.trim().slice(0, 20000)}`); return; const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const checkpointDir = path8.join(stateDir(directory), "checkpoints", safeID(sessionID)); + await ensureStateDir(directory); await ensureDir(checkpointDir); const diff = await runProcess2("git", ["diff", "--binary"], directory, 120000); const staged = await runProcess2("git", ["diff", "--cached", "--binary"], directory, 120000); diff --git a/src/native.js b/src/native.js index 44b044b2..d81de9e9 100644 --- a/src/native.js +++ b/src/native.js @@ -567,6 +567,17 @@ function statePath(directory, sessionID) { async function ensureDir(directory) { await fs.mkdir(directory, { recursive: true }); } +async function ensureStateDir(directory) { + const target = stateDir(directory); + await ensureDir(target); + try { + await fs.writeFile(path3.join(target, STATE_IGNORE_FILE), STATE_IGNORE_CONTENT, { flag: "wx" }); + } catch (error) { + if (error?.code !== "EEXIST") + throw error; + } + return target; +} function stateLockError(kind, detail) { const error = new Error(`OpenCode Loop state lock ${kind}: ${detail}`); error.code = "OPENCODE_LOOP_STATE_LOCK"; @@ -743,7 +754,7 @@ async function recoverDeadStateProcessLock(directory, canonical, stale, depth = async function acquireStateProcessLock(directory, sessionID) { const targetDirectory = stateDir(directory); await assertStatePathSafe(directory, targetDirectory); - await ensureDir(targetDirectory); + await ensureStateDir(directory); await assertStatePathSafe(directory, targetDirectory); const target = statePath(directory, sessionID); const canonical = `${target}.lock`; @@ -835,7 +846,7 @@ async function readStateFile(directory, sessionID, { backupCorrupt = true } = {} } if (backupCorrupt) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs.copyFile(target, `${target}.corrupt-${Date.now()}`); } catch {} } @@ -999,7 +1010,9 @@ async function removeState(directory, sessionID) { } }); } -var STATE_DIR = ".opencode/opencode-loop", STATE_BASELINE, stateWriteLocks, STATE_PROCESS_LOCK_TIMEOUT_MS = 5000, STATE_PROCESS_LOCK_POLL_MS = 20; +var STATE_DIR = ".opencode/opencode-loop", STATE_IGNORE_FILE = ".gitignore", STATE_IGNORE_CONTENT = `# OpenCode Loop runtime state and logs; do not commit this directory. +* +`, STATE_BASELINE, stateWriteLocks, STATE_PROCESS_LOCK_TIMEOUT_MS = 5000, STATE_PROCESS_LOCK_POLL_MS = 20; var init_state = __esm(() => { STATE_BASELINE = Symbol("opencode-loop-state-baseline"); stateWriteLocks = new Map; @@ -1011,7 +1024,7 @@ import path4 from "path"; import { spawn as spawn2 } from "child_process"; async function appendLoopLog(directory, line, extra = {}) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs2.appendFile(path4.join(stateDir(directory), "loop.log"), JSON.stringify({ time: new Date().toISOString(), line, ...extra }) + ` `); } catch {} @@ -1339,6 +1352,7 @@ ${text.trim().slice(0, 20000)}`); return; const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const checkpointDir = path6.join(stateDir(directory), "checkpoints", safeID(sessionID)); + await ensureStateDir(directory); await ensureDir(checkpointDir); const diff = await runProcess2("git", ["diff", "--binary"], directory, 120000); const staged = await runProcess2("git", ["diff", "--cached", "--binary"], directory, 120000); diff --git a/src/server.js b/src/server.js index 8ffad94e..ffca9dd6 100644 --- a/src/server.js +++ b/src/server.js @@ -567,6 +567,17 @@ function statePath(directory, sessionID) { async function ensureDir(directory) { await fs.mkdir(directory, { recursive: true }); } +async function ensureStateDir(directory) { + const target = stateDir(directory); + await ensureDir(target); + try { + await fs.writeFile(path3.join(target, STATE_IGNORE_FILE), STATE_IGNORE_CONTENT, { flag: "wx" }); + } catch (error) { + if (error?.code !== "EEXIST") + throw error; + } + return target; +} function stateLockError(kind, detail) { const error = new Error(`OpenCode Loop state lock ${kind}: ${detail}`); error.code = "OPENCODE_LOOP_STATE_LOCK"; @@ -743,7 +754,7 @@ async function recoverDeadStateProcessLock(directory, canonical, stale, depth = async function acquireStateProcessLock(directory, sessionID) { const targetDirectory = stateDir(directory); await assertStatePathSafe(directory, targetDirectory); - await ensureDir(targetDirectory); + await ensureStateDir(directory); await assertStatePathSafe(directory, targetDirectory); const target = statePath(directory, sessionID); const canonical = `${target}.lock`; @@ -835,7 +846,7 @@ async function readStateFile(directory, sessionID, { backupCorrupt = true } = {} } if (backupCorrupt) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs.copyFile(target, `${target}.corrupt-${Date.now()}`); } catch {} } @@ -999,7 +1010,9 @@ async function removeState(directory, sessionID) { } }); } -var STATE_DIR = ".opencode/opencode-loop", STATE_BASELINE, stateWriteLocks, STATE_PROCESS_LOCK_TIMEOUT_MS = 5000, STATE_PROCESS_LOCK_POLL_MS = 20; +var STATE_DIR = ".opencode/opencode-loop", STATE_IGNORE_FILE = ".gitignore", STATE_IGNORE_CONTENT = `# OpenCode Loop runtime state and logs; do not commit this directory. +* +`, STATE_BASELINE, stateWriteLocks, STATE_PROCESS_LOCK_TIMEOUT_MS = 5000, STATE_PROCESS_LOCK_POLL_MS = 20; var init_state = __esm(() => { STATE_BASELINE = Symbol("opencode-loop-state-baseline"); stateWriteLocks = new Map; @@ -1011,7 +1024,7 @@ import path4 from "path"; import { spawn as spawn2 } from "child_process"; async function appendLoopLog(directory, line, extra = {}) { try { - await ensureDir(stateDir(directory)); + await ensureStateDir(directory); await fs2.appendFile(path4.join(stateDir(directory), "loop.log"), JSON.stringify({ time: new Date().toISOString(), line, ...extra }) + ` `); } catch {} @@ -1339,6 +1352,7 @@ ${text.trim().slice(0, 20000)}`); return; const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const checkpointDir = path6.join(stateDir(directory), "checkpoints", safeID(sessionID)); + await ensureStateDir(directory); await ensureDir(checkpointDir); const diff = await runProcess2("git", ["diff", "--binary"], directory, 120000); const staged = await runProcess2("git", ["diff", "--cached", "--binary"], directory, 120000); diff --git a/src/source/core/process.js b/src/source/core/process.js index 53c2f7a8..a423b119 100644 --- a/src/source/core/process.js +++ b/src/source/core/process.js @@ -1,11 +1,11 @@ import { promises as fs } from "node:fs" import path from "node:path" import { spawn } from "node:child_process" -import { stateDir, ensureDir } from "./state.js" +import { stateDir, ensureStateDir } from "./state.js" export async function appendLoopLog(directory, line, extra = {}) { try { - await ensureDir(stateDir(directory)) + await ensureStateDir(directory) await fs.appendFile(path.join(stateDir(directory), "loop.log"), JSON.stringify({ time: new Date().toISOString(), line, ...extra }) + "\n") } catch {} } diff --git a/src/source/core/state.js b/src/source/core/state.js index 73e25f55..6f2af736 100644 --- a/src/source/core/state.js +++ b/src/source/core/state.js @@ -5,12 +5,26 @@ import path from "node:path" import { safeID } from "./args.js" const STATE_DIR = ".opencode/opencode-loop" +const STATE_IGNORE_FILE = ".gitignore" +const STATE_IGNORE_CONTENT = "# OpenCode Loop runtime state and logs; do not commit this directory.\n*\n" const STATE_BASELINE = Symbol("opencode-loop-state-baseline") const stateWriteLocks = new Map() export function stateDir(directory) { return path.join(directory, STATE_DIR) } function statePath(directory, sessionID) { return path.join(stateDir(directory), `${safeID(sessionID)}.json`) } export async function ensureDir(directory) { await fs.mkdir(directory, { recursive: true }) } + +// Keep runtime state and logs out of the project repository. The ignore file is +// created only when it is missing and is never overwritten, so a user-owned or +// customized .gitignore survives updates. +export async function ensureStateDir(directory) { + const target = stateDir(directory) + await ensureDir(target) + try { await fs.writeFile(path.join(target, STATE_IGNORE_FILE), STATE_IGNORE_CONTENT, { flag: "wx" }) } + catch (error) { if (error?.code !== "EEXIST") throw error } + return target +} + export async function pathExists(filePath) { try { await fs.access(filePath); return true } catch { return false } } const STATE_PROCESS_LOCK_TIMEOUT_MS = 5_000 @@ -183,7 +197,7 @@ async function recoverDeadStateProcessLock(directory, canonical, stale, depth = async function acquireStateProcessLock(directory, sessionID) { const targetDirectory = stateDir(directory) await assertStatePathSafe(directory, targetDirectory) - await ensureDir(targetDirectory) + await ensureStateDir(directory) await assertStatePathSafe(directory, targetDirectory) const target = statePath(directory, sessionID) const canonical = `${target}.lock` @@ -273,7 +287,7 @@ async function readStateFile(directory, sessionID, { backupCorrupt = true } = {} // repeatedly creates an unbounded number of .corrupt-* backups. if (backupCorrupt) { try { - await ensureDir(stateDir(directory)) + await ensureStateDir(directory) await fs.copyFile(target, `${target}.corrupt-${Date.now()}`) } catch {} } diff --git a/src/source/runtime/goal-report.js b/src/source/runtime/goal-report.js index a737f0f5..01bdc726 100644 --- a/src/source/runtime/goal-report.js +++ b/src/source/runtime/goal-report.js @@ -2,7 +2,7 @@ import { promises as fs } from "node:fs" import path from "node:path" import { DEFAULT_GOAL_MAX_NO_PROGRESS, safeID } from "../core/args.js" import { isGoalJob, goalStatusText } from "../core/jobs.js" -import { stateDir, ensureDir } from "../core/state.js" +import { stateDir, ensureDir, ensureStateDir } from "../core/state.js" const GOAL_REPORT_DIR = "goals" @@ -47,6 +47,7 @@ export function goalReportText(job) { export async function writeGoalReport(directory, sessionID, job) { if (!isGoalJob(job)) return const target = job.goalEvidenceFile ? path.resolve(directory, job.goalEvidenceFile) : goalReportPath(directory, sessionID, job) + if (!job.goalEvidenceFile) await ensureStateDir(directory) await ensureDir(path.dirname(target)) await fs.writeFile(target, goalReportText(job), "utf8") } diff --git a/src/source/runtime/job-workspace.js b/src/source/runtime/job-workspace.js index 969bcca9..80c60f57 100644 --- a/src/source/runtime/job-workspace.js +++ b/src/source/runtime/job-workspace.js @@ -2,7 +2,7 @@ import { promises as fs } from "node:fs" import path from "node:path" import { safeID } from "../core/args.js" import { decoratePrompt, isGoalJob } from "../core/jobs.js" -import { stateDir, ensureDir } from "../core/state.js" +import { stateDir, ensureDir, ensureStateDir } from "../core/state.js" import { appendLoopLog as defaultAppendLoopLog, readSmallTextFile as defaultReadSmallTextFile, runProcess as defaultRunProcess } from "../core/process.js" import { buildGoalPrompt as defaultBuildGoalPrompt } from "./goal-runtime.js" @@ -133,6 +133,7 @@ export function createJobWorkspaceRuntime(options = {}) { if (!status.stdout.trim()) return const timestamp = new Date().toISOString().replace(/[:.]/g, "-") const checkpointDir = path.join(stateDir(directory), "checkpoints", safeID(sessionID)) + await ensureStateDir(directory) await ensureDir(checkpointDir) const diff = await runProcess("git", ["diff", "--binary"], directory, 120_000) const staged = await runProcess("git", ["diff", "--cached", "--binary"], directory, 120_000)