opencode-repos 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/manifest.ts +21 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-repos",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Repository cache, registry, and cross-codebase intelligence for OpenCode agents",
5
5
  "main": "index.ts",
6
6
  "type": "module",
package/src/manifest.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { homedir } from "node:os"
2
2
  import { join } from "node:path"
3
- import { mkdir, rename, unlink, stat } from "node:fs/promises"
3
+ import { mkdir, rename, unlink, stat, open } from "node:fs/promises"
4
4
 
5
5
  export interface RepoEntry {
6
6
  type: "cached" | "local"
@@ -27,7 +27,7 @@ let cacheDir = join(homedir(), ".cache", "opencode-repos")
27
27
  let manifestPath = join(cacheDir, "manifest.json")
28
28
  let manifestTmpPath = join(cacheDir, "manifest.json.tmp")
29
29
  let lockPath = join(cacheDir, "manifest.lock")
30
- const LOCK_STALE_MS = 5 * 60 * 1000
30
+ const LOCK_STALE_MS = 30_000
31
31
 
32
32
  function createEmptyManifest(): Manifest {
33
33
  return {
@@ -72,31 +72,33 @@ async function isLockStale(): Promise<boolean> {
72
72
  }
73
73
 
74
74
  async function acquireLock(): Promise<void> {
75
- const maxAttempts = 50
76
- const retryDelayMs = 100
75
+ const maxAttempts = 150
76
+ const retryDelayMs = 200
77
+
78
+ await mkdir(cacheDir, { recursive: true })
77
79
 
78
80
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
79
- const lockFile = Bun.file(lockPath)
80
- const exists = await lockFile.exists()
81
-
82
- if (exists) {
83
- if (await isLockStale()) {
84
- await unlink(lockPath).catch(() => {})
85
- } else {
86
- await Bun.sleep(retryDelayMs)
87
- continue
81
+ try {
82
+ const fd = await open(lockPath, "wx")
83
+ await fd.write(String(process.pid))
84
+ await fd.close()
85
+ return
86
+ } catch (error) {
87
+ const code = (error as NodeJS.ErrnoException).code
88
+ if (code !== "EEXIST") {
89
+ throw error
88
90
  }
89
91
  }
90
92
 
91
- try {
92
- await mkdir(cacheDir, { recursive: true })
93
- await Bun.write(lockPath, String(Date.now()))
94
- return
95
- } catch {
96
- await Bun.sleep(retryDelayMs)
93
+ if (await isLockStale()) {
94
+ await unlink(lockPath).catch(() => {})
95
+ continue
97
96
  }
97
+
98
+ await Bun.sleep(retryDelayMs)
98
99
  }
99
100
 
101
+ await unlink(lockPath).catch(() => {})
100
102
  throw new Error("Failed to acquire manifest lock after maximum attempts")
101
103
  }
102
104