Skip to content

Commit 45b9607

Browse files
authored
Revert "fix: use stable per-project tmp log directories (#9)" (#10)
This reverts commit 2f71f86.
1 parent 2f71f86 commit 45b9607

3 files changed

Lines changed: 19 additions & 41 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Yes. Set `OPENCODE_MEMORY_AUTODREAM=0`. You can also tune gates with:
174174

175175
### Logs
176176

177-
Logs are written to `/tmp/opencode-claude-memory/<project-hash>/`:
177+
Logs are written to `$TMPDIR/opencode-memory-logs/`:
178178
- `extract-*.log`: automatic memory extraction
179179
- `dream-*.log`: auto-dream consolidation
180180

bin/opencode-memory

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ AUTODREAM_STALE_LOCK_SECS=$((60 * 60))
204204

205205
WORKING_DIR="${OPENCODE_MEMORY_DIR:-$(pwd)}"
206206

207-
LOG_BASE_DIR="/tmp/opencode-claude-memory"
207+
TMP_BASE_DIR="${TMPDIR:-/tmp}"
208+
while [ "$TMP_BASE_DIR" != "/" ] && [ "${TMP_BASE_DIR%/}" != "$TMP_BASE_DIR" ]; do
209+
TMP_BASE_DIR="${TMP_BASE_DIR%/}"
210+
done
211+
if [ -z "$TMP_BASE_DIR" ]; then
212+
TMP_BASE_DIR="/"
213+
fi
208214

209215
# Scope lock files at project root granularity (not per-subdirectory).
210216
PROJECT_SCOPE_DIR="$WORKING_DIR"
@@ -215,7 +221,7 @@ fi
215221
PROJECT_KEY="$(printf '%s' "$PROJECT_SCOPE_DIR" | cksum | awk '{print $1}')"
216222

217223
# Lock files (prevent concurrent work on the same project)
218-
LOCK_DIR="/tmp/opencode-memory-locks"
224+
LOCK_DIR="$TMP_BASE_DIR/opencode-memory-locks"
219225
mkdir -p "$LOCK_DIR"
220226
EXTRACT_LOCK_FILE="$LOCK_DIR/${PROJECT_KEY}.extract.lock"
221227

@@ -224,7 +230,7 @@ mkdir -p "$STATE_DIR"
224230
CONSOLIDATION_LOCK_FILE="$STATE_DIR/${PROJECT_KEY}.consolidate-lock"
225231

226232
# Logs
227-
LOG_DIR="$LOG_BASE_DIR/${PROJECT_KEY}"
233+
LOG_DIR="$TMP_BASE_DIR/opencode-memory-logs"
228234
mkdir -p "$LOG_DIR"
229235
TASK_LOG_PREFIX="$(date +%Y%m%d-%H%M%S)-${PROJECT_KEY}"
230236
EXTRACT_LOG_FILE="$LOG_DIR/extract-${TASK_LOG_PREFIX}.log"

test/opencode-memory.test.ts

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { afterEach, describe, expect, test } from "bun:test"
2-
import { chmodSync, existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "fs"
2+
import { chmodSync, existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "fs"
33
import { tmpdir } from "os"
4-
import { dirname, join } from "path"
4+
import { join } from "path"
55
import { spawnSync } from "child_process"
66

77
const tempRoots: string[] = []
8-
const tempLogDirs = new Set<string>()
98
const scriptPath = join(process.cwd(), "bin", "opencode-memory")
109

1110
function makeTempRoot(): string {
@@ -19,42 +18,17 @@ function writeExecutable(filePath: string, content: string): void {
1918
chmodSync(filePath, 0o755)
2019
}
2120

22-
function rememberLogDirFromPath(logPath: string): void {
23-
tempLogDirs.add(dirname(logPath))
24-
}
25-
26-
function findRecentExtractLog(startedAt: number): string {
27-
const logRoot = join("/tmp", "opencode-claude-memory")
28-
const projectDirs = readdirSync(logRoot).map((entry) => join(logRoot, entry))
29-
30-
const extractLogs = projectDirs.flatMap((dir) =>
31-
readdirSync(dir)
32-
.filter((name) => name.startsWith("extract-"))
33-
.map((name) => join(dir, name)),
34-
)
35-
36-
const recentLog = extractLogs.find((logPath) => statSync(logPath).mtimeMs >= startedAt)
37-
expect(recentLog).toBeDefined()
38-
39-
return recentLog ?? ""
40-
}
41-
4221
afterEach(() => {
4322
while (tempRoots.length > 0) {
4423
const root = tempRoots.pop()
4524
if (root) {
4625
rmSync(root, { recursive: true, force: true })
4726
}
4827
}
49-
50-
for (const logDir of tempLogDirs) {
51-
rmSync(logDir, { recursive: true, force: true })
52-
}
53-
tempLogDirs.clear()
5428
})
5529

5630
describe("opencode-memory wrapper", () => {
57-
test("writes extraction logs under a stable per-project /tmp directory", () => {
31+
test("normalizes TMPDIR before composing extraction log paths", () => {
5832
const root = makeTempRoot()
5933
const fakeBin = join(root, "bin")
6034
const homeDir = join(root, "home")
@@ -86,7 +60,6 @@ exit 0
8660
`,
8761
)
8862

89-
const startedAt = Date.now()
9063
const result = spawnSync("bash", [scriptPath, "--help"], {
9164
cwd: root,
9265
encoding: "utf-8",
@@ -109,10 +82,7 @@ exit 0
10982
expect(logPathMatch).not.toBeNull()
11083

11184
const logPath = logPathMatch?.[1].trim() ?? ""
112-
rememberLogDirFromPath(logPath)
113-
expect(logPath).toMatch(/^\/tmp\/opencode-claude-memory\/[^/]+\/extract-/)
114-
expect(statSync(logPath).mtimeMs).toBeGreaterThanOrEqual(startedAt)
115-
expect(logPath).toContain("/extract-")
85+
expect(logPath.startsWith(join(root, "tmp", "opencode-memory-logs", "extract-"))).toBe(true)
11686
expect(existsSync(logPath)).toBe(true)
11787
expect(readFileSync(logPath, "utf-8")).toContain("extraction ok")
11888
})
@@ -149,7 +119,6 @@ exit 0
149119
`,
150120
)
151121

152-
const startedAt = Date.now()
153122
const result = spawnSync("bash", [scriptPath, "--help"], {
154123
cwd: root,
155124
encoding: "utf-8",
@@ -168,8 +137,11 @@ exit 0
168137
expect(result.status).toBe(0)
169138
expect(result.stderr).toBe("")
170139

171-
const logPath = findRecentExtractLog(startedAt)
172-
rememberLogDirFromPath(logPath)
140+
const logDir = join(root, "tmp", "opencode-memory-logs")
141+
const logFiles = readdirSync(logDir)
142+
expect(logFiles).toHaveLength(1)
143+
144+
const logPath = join(logDir, logFiles[0] ?? "")
173145
expect(readFileSync(logPath, "utf-8")).toContain("extraction ok")
174146
})
175147
})

0 commit comments

Comments
 (0)