@joe-broadhead/opencode-mem 0.11.3 → 0.11.4
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.
- package/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/tools/wiki.js +12 -2
- package/src/wiki-ingest.js +70 -15
- package/src/wiki-queue.js +252 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
The format follows Keep a Changelog 1.1.0: one `## [x.y.z] - date` section per
|
|
4
4
|
release.
|
|
5
5
|
|
|
6
|
+
## [0.11.4] - 2026-11-16
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
|
|
10
|
+
- A wiki write that arrives while the wiki is ingesting is saved to disk before the answer, and
|
|
11
|
+
written after that ingest finishes, even after a restart. If it cannot be saved, the answer is
|
|
12
|
+
refused and says to try again, never queued.
|
|
13
|
+
|
|
14
|
+
### Deploy
|
|
15
|
+
|
|
16
|
+
No server code change, and no migration.
|
|
17
|
+
|
|
6
18
|
## [0.11.3] - 2026-11-16
|
|
7
19
|
|
|
8
20
|
### Added
|
package/package.json
CHANGED
package/src/tools/wiki.js
CHANGED
|
@@ -3,6 +3,7 @@ import { RECORDS_FILE, RECORDS_WIKI, isRecordsText } from "../records.js"
|
|
|
3
3
|
import { codedMessage } from "../errors.js"
|
|
4
4
|
import { errorMessage, neutralizeFenceClosers, oneLine, redactSecrets } from "../util.js"
|
|
5
5
|
import { createWikiIngest, WIKI_INGEST_DELAY_MS } from "../wiki-ingest.js"
|
|
6
|
+
import { createWikiQueue } from "../wiki-queue.js"
|
|
6
7
|
import { wikiCache, wikiErrorOf, wikiStateOf } from "../wiki-state.js"
|
|
7
8
|
import { discoverKnowledge, isRecordsWikiName, searchKnowledge } from "./knowledge.js"
|
|
8
9
|
import { failedAnswer, refusedAnswer, wikiPlace, withWriteState } from "../views/write-state.js"
|
|
@@ -103,6 +104,10 @@ export function registerWiki(editor, toolkit) {
|
|
|
103
104
|
backend,
|
|
104
105
|
log,
|
|
105
106
|
delay: config.wikiIngestMs ?? WIKI_INGEST_DELAY_MS,
|
|
107
|
+
// Durable before the answer says queued (jelly #89, 0.11.4): the entry
|
|
108
|
+
// outlives the engine, and the file holds the write's body, owner-only,
|
|
109
|
+
// inside Memory's state directory.
|
|
110
|
+
queue: createWikiQueue({ stateDir: config.stateDir, log }),
|
|
106
111
|
rememberError: (wikiID, message) => cache.rememberError(wikiID, message),
|
|
107
112
|
clearError: (wikiID) => cache.clearError(wikiID),
|
|
108
113
|
rememberPending: (wikiID, scope) => cache.rememberPending(wikiID, scope),
|
|
@@ -295,7 +300,7 @@ export function registerWiki(editor, toolkit) {
|
|
|
295
300
|
*/
|
|
296
301
|
function ingestNote(written) {
|
|
297
302
|
if (written.queued) {
|
|
298
|
-
return "the wiki is ingesting an earlier write, so this write is
|
|
303
|
+
return "the wiki is ingesting an earlier write, so this write is written after that ingest finishes, even after a restart"
|
|
299
304
|
}
|
|
300
305
|
return "ingestion starts on its own a moment after the last write, so a burst of writes makes one ingest; search finds the changes once it finishes"
|
|
301
306
|
}
|
|
@@ -812,7 +817,12 @@ export function registerWiki(editor, toolkit) {
|
|
|
812
817
|
}
|
|
813
818
|
// Say what the write guarantees, so the agent does not read its own
|
|
814
819
|
// write back to check it: the source file now holds exactly this.
|
|
815
|
-
// A queued write guarantees nothing yet: the wiki is ingesting
|
|
820
|
+
// A queued write guarantees nothing yet: the wiki is ingesting, and
|
|
821
|
+
// it is on disk until the hub accepts it. A queue the disk refused
|
|
822
|
+
// saved nothing, so the answer is `refused` (jelly #89, 0.11.4).
|
|
823
|
+
if (written.refused) {
|
|
824
|
+
return withWriteState(text(`${note}not written: the wiki is busy, nothing was saved, try again in about a minute`), { state: "refused" })
|
|
825
|
+
}
|
|
816
826
|
if (written.queued) {
|
|
817
827
|
const what = action === "append" ? "the append to" : ""
|
|
818
828
|
return withWriteState(
|
package/src/wiki-ingest.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { errorMessage } from "./util.js"
|
|
2
|
+
import { wikiWriteDigest } from "./wiki-queue.js"
|
|
2
3
|
|
|
3
4
|
/** How long a write waits for the rest of its burst before one ingest starts. */
|
|
4
5
|
export const WIKI_INGEST_DELAY_MS = 3000
|
|
@@ -20,10 +21,13 @@ export function isWikiBusy(error) {
|
|
|
20
21
|
* (`delay`, 3 seconds by default); a burst of writes makes one ingest. Three
|
|
21
22
|
* seconds is longer than the gap between the calls of one writing burst and
|
|
22
23
|
* short next to an ingest, which takes minutes.
|
|
23
|
-
* - **Queued, not refused.** The hub refuses a write while it
|
|
24
|
-
* "wiki is processing"), so such a write is
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* - **Queued, not refused, and durable.** The hub refuses a write while it
|
|
25
|
+
* ingests (409 "wiki is processing"), so such a write is saved to the
|
|
26
|
+
* durable queue (`src/wiki-queue.js`) before the answer says `queued`,
|
|
27
|
+
* written when the ingest finishes, and one ingest follows it. After a
|
|
28
|
+
* restart the saved entries are replayed in order, and one whose body the
|
|
29
|
+
* wiki already holds is skipped (its filename and digest), so a crash
|
|
30
|
+
* between the hub's accept and the entry's removal never writes it twice.
|
|
27
31
|
* - **One ingest per wiki at a time here.** All of a wiki's queue work is
|
|
28
32
|
* serialised (`running`), and the hub's 409 is the lock across engines.
|
|
29
33
|
*
|
|
@@ -33,6 +37,7 @@ export function isWikiBusy(error) {
|
|
|
33
37
|
* backend: any,
|
|
34
38
|
* log?: any,
|
|
35
39
|
* delay?: number,
|
|
40
|
+
* queue: { append: Function, entries: Function, remove: Function },
|
|
36
41
|
* rememberError?: (wikiID: string, message: string) => void,
|
|
37
42
|
* clearError?: (wikiID: string) => void,
|
|
38
43
|
* rememberPending?: (wikiID: string, scope: object) => void,
|
|
@@ -44,25 +49,44 @@ export function createWikiIngest({
|
|
|
44
49
|
backend,
|
|
45
50
|
log = null,
|
|
46
51
|
delay = WIKI_INGEST_DELAY_MS,
|
|
52
|
+
queue: durable,
|
|
47
53
|
rememberError = () => {},
|
|
48
54
|
clearError = () => {},
|
|
49
55
|
rememberPending = () => {},
|
|
50
56
|
clearPending = () => {},
|
|
51
57
|
pending = [],
|
|
52
58
|
} = {}) {
|
|
59
|
+
if (!durable || typeof durable.append !== "function" || typeof durable.entries !== "function") {
|
|
60
|
+
// A held write is never taken without a durable record (jelly #89, 0.11.4).
|
|
61
|
+
throw new Error("the wiki ingest queue needs its durable store")
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
/** `${team}/${space}/${user}/${wiki}` -> one queue, so one ingest at a time. */
|
|
54
65
|
const queues = new Map()
|
|
55
66
|
let stopped = false
|
|
56
67
|
let startTimer = null
|
|
57
68
|
|
|
58
69
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* after setup returns, so a start never waits on the state
|
|
70
|
+
* What an earlier engine start left: the writes it queued, replayed in the
|
|
71
|
+
* order they were saved, and the ingests its debounce did not finish. Read
|
|
72
|
+
* after setup returns, so a start never waits on the state files.
|
|
62
73
|
*/
|
|
63
74
|
function recover() {
|
|
64
75
|
startTimer = null
|
|
65
76
|
if (stopped) return
|
|
77
|
+
let saved = []
|
|
78
|
+
try {
|
|
79
|
+
saved = typeof durable.entries === "function" ? durable.entries() : []
|
|
80
|
+
} catch (error) {
|
|
81
|
+
log?.warn?.("the wiki write queue could not be read", { error: errorMessage(error) })
|
|
82
|
+
}
|
|
83
|
+
for (const entry of saved) {
|
|
84
|
+
if (!entry?.wiki || !entry?.file) continue
|
|
85
|
+
const queue = queueFor(entry.scope, entry.wiki)
|
|
86
|
+
queue.writes.push(entry)
|
|
87
|
+
// Replay promptly: a busy hub answers 409, and the retry waits.
|
|
88
|
+
arm(queue, 10)
|
|
89
|
+
}
|
|
66
90
|
const waiting = typeof pending === "function" ? pending() : pending
|
|
67
91
|
for (const entry of waiting ?? []) {
|
|
68
92
|
if (!entry?.wikiID || !entry?.scope) continue
|
|
@@ -102,13 +126,29 @@ export function createWikiIngest({
|
|
|
102
126
|
}
|
|
103
127
|
|
|
104
128
|
/**
|
|
105
|
-
*
|
|
106
|
-
* the ingest finishes, in the order it arrived, and one
|
|
129
|
+
* Save a write the hub refused because it is ingesting, before answering.
|
|
130
|
+
* It is written when the ingest finishes, in the order it arrived, and one
|
|
131
|
+
* ingest follows. A disk that refuses the save throws, so the answer is
|
|
132
|
+
* `refused`, never `queued` (jelly #89, 0.11.4).
|
|
107
133
|
*/
|
|
108
134
|
function hold(queue, filename, content) {
|
|
109
|
-
|
|
135
|
+
const entry = durable.append({ wikiID: queue.wikiID, scope: queue.scope, filename, content })
|
|
136
|
+
queue.writes.push(entry)
|
|
110
137
|
queue.failures += 1
|
|
111
138
|
arm(queue, retryDelay(queue))
|
|
139
|
+
return entry
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** True when the wiki already holds exactly this body, so a replay skips it. */
|
|
143
|
+
async function alreadyLanded(queue, entry) {
|
|
144
|
+
try {
|
|
145
|
+
const data = await backend.readWikiRaw({ scope: queue.scope, wikiID: queue.wikiID, filenames: [entry.file] })
|
|
146
|
+
const item = (data?.items ?? []).find((row) => row?.filename === entry.file)
|
|
147
|
+
return typeof item?.content === "string" && wikiWriteDigest(item.content) === entry.digest
|
|
148
|
+
} catch {
|
|
149
|
+
// An unreadable file is not proof it landed: attempt the write.
|
|
150
|
+
return false
|
|
151
|
+
}
|
|
112
152
|
}
|
|
113
153
|
|
|
114
154
|
async function flush(queue) {
|
|
@@ -118,8 +158,15 @@ export function createWikiIngest({
|
|
|
118
158
|
try {
|
|
119
159
|
while (queue.writes.length > 0) {
|
|
120
160
|
const next = queue.writes[0]
|
|
161
|
+
if (await alreadyLanded(queue, next)) {
|
|
162
|
+
// The hub accepted it and the engine stopped before the removal:
|
|
163
|
+
// the same body is never written twice.
|
|
164
|
+
queue.writes.shift()
|
|
165
|
+
durable.remove(next.id)
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
121
168
|
try {
|
|
122
|
-
await backend.writeWikiRaw({ scope: queue.scope, wikiID: queue.wikiID, files: [{ filename: next.
|
|
169
|
+
await backend.writeWikiRaw({ scope: queue.scope, wikiID: queue.wikiID, files: [{ filename: next.file, content: next.content }] })
|
|
123
170
|
} catch (error) {
|
|
124
171
|
if (isWikiBusy(error)) {
|
|
125
172
|
queue.dirty = true
|
|
@@ -134,11 +181,13 @@ export function createWikiIngest({
|
|
|
134
181
|
// The same refusal three times is not going to pass: say so, and do
|
|
135
182
|
// not repeat it forever.
|
|
136
183
|
queue.writes.shift()
|
|
137
|
-
|
|
138
|
-
|
|
184
|
+
durable.remove(next.id)
|
|
185
|
+
rememberError(queue.wikiID, `A queued write to ${next.file} failed: ${errorMessage(error)}`)
|
|
186
|
+
log?.warn?.("a queued wiki write was dropped", { wikiID: queue.wikiID, filename: next.file, error: errorMessage(error) })
|
|
139
187
|
continue
|
|
140
188
|
}
|
|
141
189
|
queue.writes.shift()
|
|
190
|
+
durable.remove(next.id)
|
|
142
191
|
queue.dirty = true
|
|
143
192
|
landed = true
|
|
144
193
|
}
|
|
@@ -182,7 +231,13 @@ export function createWikiIngest({
|
|
|
182
231
|
await backend.writeWikiRaw({ scope, wikiID, files: [{ filename, content }] })
|
|
183
232
|
} catch (error) {
|
|
184
233
|
if (!isWikiBusy(error)) throw error
|
|
185
|
-
|
|
234
|
+
try {
|
|
235
|
+
hold(queue, filename, content)
|
|
236
|
+
} catch (queueError) {
|
|
237
|
+
// No durable record, so this is not queued: the answer is refused.
|
|
238
|
+
log?.warn?.("a queued wiki write could not be saved", { wikiID, filename, error: errorMessage(queueError) })
|
|
239
|
+
return { queued: false, refused: true, reason: String(queueError?.code ?? "") }
|
|
240
|
+
}
|
|
186
241
|
return { queued: true }
|
|
187
242
|
}
|
|
188
243
|
queue.dirty = true
|
|
@@ -200,7 +255,7 @@ export function createWikiIngest({
|
|
|
200
255
|
return count
|
|
201
256
|
},
|
|
202
257
|
|
|
203
|
-
/** Stop every timer;
|
|
258
|
+
/** Stop every timer; what waited is on disk, and the next start replays it. */
|
|
204
259
|
stop() {
|
|
205
260
|
stopped = true
|
|
206
261
|
if (startTimer) clearTimeout(startTimer)
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The durable queue of wiki writes the hub refused because it was ingesting
|
|
7
|
+
* (jelly #89, 0.11.4). A write held only in the engine's memory dies with it,
|
|
8
|
+
* and that is the silent loss the ask exists to end: each one is written here
|
|
9
|
+
* **before** the tool answers `state: queued`, and replayed at the next start.
|
|
10
|
+
*
|
|
11
|
+
* - **Where.** One file per write in `wiki-queue/` under the plugin's own state
|
|
12
|
+
* directory (the one `config.stateDir` names, beside the spool and the pause
|
|
13
|
+
* file). Each file holds the write's body, so it is owner-only (`0600`), in a
|
|
14
|
+
* `0700` directory, and its content never goes into a log line.
|
|
15
|
+
* - **Bounds.** At most `WIKI_QUEUE_MAX_ENTRIES` writes and
|
|
16
|
+
* `WIKI_QUEUE_MAX_BYTES` of bodies (8 MiB); a write that would pass either
|
|
17
|
+
* is not saved, and the answer is `state: refused` rather than `queued`.
|
|
18
|
+
* - **Atomic.** Each file is written as a private temp file in the same
|
|
19
|
+
* directory, fsynced, renamed into place, and the directory is fsynced, so a
|
|
20
|
+
* crash leaves no file or the whole one, never a torn one. One file per
|
|
21
|
+
* write also means two engine processes on one machine never overwrite each
|
|
22
|
+
* other's queue: an append only ever creates its own file.
|
|
23
|
+
* - **Once.** A file is removed only after the hub accepted the write. A
|
|
24
|
+
* crash between the accept and the removal leaves it, and the replay checks
|
|
25
|
+
* the wiki's own file (its filename and a digest of the body) before
|
|
26
|
+
* writing, so the same body is never written twice.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export const WIKI_QUEUE_DIR = "wiki-queue"
|
|
30
|
+
/** Writes the queue holds. */
|
|
31
|
+
export const WIKI_QUEUE_MAX_ENTRIES = 100
|
|
32
|
+
/** Bodies the queue holds, together. */
|
|
33
|
+
export const WIKI_QUEUE_MAX_BYTES = 8 * 1024 * 1024
|
|
34
|
+
|
|
35
|
+
const ENTRY = /^wq_[a-f0-9]{16}\.json$/
|
|
36
|
+
|
|
37
|
+
/** A queue failure the answer can name without repeating the file system's words. */
|
|
38
|
+
export class WikiQueueError extends Error {
|
|
39
|
+
constructor(code, message) {
|
|
40
|
+
super(message)
|
|
41
|
+
this.name = "WikiQueueError"
|
|
42
|
+
this.code = code
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A body's key, so a replay never writes one the wiki already holds. */
|
|
47
|
+
export function wikiWriteDigest(content) {
|
|
48
|
+
return createHash("sha256").update(String(content ?? ""), "utf8").digest("hex")
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The scope a replay needs, without a session or any text. */
|
|
52
|
+
function scopeOf(scope) {
|
|
53
|
+
const kept = {}
|
|
54
|
+
for (const key of ["team_id", "space_id", "user_id", "agent_id"]) {
|
|
55
|
+
const value = String(scope?.[key] ?? "").trim()
|
|
56
|
+
if (value) kept[key] = value
|
|
57
|
+
}
|
|
58
|
+
return kept
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function validEntry(entry) {
|
|
62
|
+
return Boolean(
|
|
63
|
+
entry &&
|
|
64
|
+
typeof entry.id === "string" &&
|
|
65
|
+
ENTRY.test(`${entry.id}.json`) &&
|
|
66
|
+
typeof entry.wiki === "string" &&
|
|
67
|
+
entry.wiki &&
|
|
68
|
+
typeof entry.file === "string" &&
|
|
69
|
+
entry.file &&
|
|
70
|
+
typeof entry.content === "string" &&
|
|
71
|
+
typeof entry.digest === "string" &&
|
|
72
|
+
entry.digest,
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {{ stateDir?: string, log?: any }} input
|
|
78
|
+
*/
|
|
79
|
+
export function createWikiQueue({ stateDir = "", log = null } = {}) {
|
|
80
|
+
const base = String(stateDir ?? "")
|
|
81
|
+
const dir = base ? path.join(base, WIKI_QUEUE_DIR) : ""
|
|
82
|
+
let ensured = false
|
|
83
|
+
/** This process's append counter, so entries of one millisecond keep their order. */
|
|
84
|
+
let sequence = 0
|
|
85
|
+
|
|
86
|
+
/** Create the queue directory when it is missing; never override an operator's mode. */
|
|
87
|
+
function ensureDir() {
|
|
88
|
+
if (ensured) return
|
|
89
|
+
const existed = fs.existsSync(dir)
|
|
90
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
91
|
+
if (!existed) {
|
|
92
|
+
try {
|
|
93
|
+
fs.chmodSync(dir, 0o700)
|
|
94
|
+
} catch {
|
|
95
|
+
/* best effort on platforms without POSIX modes */
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
ensured = true
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Make a rename or a new file in the directory survive a crash. Best effort. */
|
|
102
|
+
function syncDir() {
|
|
103
|
+
let fd
|
|
104
|
+
try {
|
|
105
|
+
fd = fs.openSync(dir, "r")
|
|
106
|
+
fs.fsyncSync(fd)
|
|
107
|
+
} catch {
|
|
108
|
+
/* not supported on every platform (Windows) */
|
|
109
|
+
} finally {
|
|
110
|
+
if (fd !== undefined) {
|
|
111
|
+
try {
|
|
112
|
+
fs.closeSync(fd)
|
|
113
|
+
} catch {
|
|
114
|
+
/* ignore */
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Every entry file, newest last, read from disk so a second process's are seen. */
|
|
121
|
+
function read() {
|
|
122
|
+
if (!dir) return []
|
|
123
|
+
ensureDir()
|
|
124
|
+
let names
|
|
125
|
+
try {
|
|
126
|
+
names = fs.readdirSync(dir)
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error?.code !== "ENOENT") log?.debug?.("the wiki write queue is unreadable", { error: String(error?.code ?? error) })
|
|
129
|
+
return []
|
|
130
|
+
}
|
|
131
|
+
const entries = []
|
|
132
|
+
for (const name of names) {
|
|
133
|
+
if (!ENTRY.test(name)) continue
|
|
134
|
+
let parsed
|
|
135
|
+
try {
|
|
136
|
+
parsed = JSON.parse(fs.readFileSync(path.join(dir, name), "utf8"))
|
|
137
|
+
} catch {
|
|
138
|
+
log?.warn?.("a wiki write queue entry is unreadable and is left alone", { entry: name })
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (!validEntry(parsed)) {
|
|
142
|
+
log?.warn?.("a wiki write queue entry is not one, and is left alone", { entry: name })
|
|
143
|
+
continue
|
|
144
|
+
}
|
|
145
|
+
entries.push({
|
|
146
|
+
id: parsed.id,
|
|
147
|
+
wiki: parsed.wiki,
|
|
148
|
+
file: parsed.file,
|
|
149
|
+
digest: parsed.digest,
|
|
150
|
+
bytes: Number(parsed.bytes) || Buffer.byteLength(parsed.content),
|
|
151
|
+
scope: scopeOf(parsed.scope),
|
|
152
|
+
content: parsed.content,
|
|
153
|
+
at: typeof parsed.at === "string" ? parsed.at : "",
|
|
154
|
+
seq: Number(parsed.seq) || 0,
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
// The order the writes were queued: time, then this process's own counter.
|
|
158
|
+
entries.sort((a, b) => String(a.at).localeCompare(String(b.at)) || a.seq - b.seq || a.id.localeCompare(b.id))
|
|
159
|
+
return entries.map(({ seq: _seq, ...entry }) => entry)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One entry, atomically: temp file, fsync, rename, directory fsync. */
|
|
163
|
+
function saveFile(entry) {
|
|
164
|
+
ensureDir()
|
|
165
|
+
const target = path.join(dir, `${entry.id}.json`)
|
|
166
|
+
const temp = path.join(dir, `.${entry.id}.${process.pid}-${randomBytes(4).toString("hex")}.tmp`)
|
|
167
|
+
const body = Buffer.from(JSON.stringify(entry, null, 2))
|
|
168
|
+
let fd
|
|
169
|
+
try {
|
|
170
|
+
fd = fs.openSync(temp, "wx", 0o600)
|
|
171
|
+
try {
|
|
172
|
+
fs.fchmodSync(fd, 0o600)
|
|
173
|
+
} catch {
|
|
174
|
+
/* best effort on platforms without POSIX modes */
|
|
175
|
+
}
|
|
176
|
+
let offset = 0
|
|
177
|
+
while (offset < body.length) offset += fs.writeSync(fd, body, offset, body.length - offset)
|
|
178
|
+
fs.fsyncSync(fd)
|
|
179
|
+
fs.closeSync(fd)
|
|
180
|
+
fd = undefined
|
|
181
|
+
fs.renameSync(temp, target)
|
|
182
|
+
syncDir()
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (fd !== undefined) {
|
|
185
|
+
try {
|
|
186
|
+
fs.closeSync(fd)
|
|
187
|
+
} catch {
|
|
188
|
+
/* ignore */
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
fs.unlinkSync(temp)
|
|
193
|
+
} catch {
|
|
194
|
+
/* the temp file never existed */
|
|
195
|
+
}
|
|
196
|
+
throw new WikiQueueError("queue_io", `the queue could not be written (${String(error?.code ?? error?.message ?? error)})`)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
/** Every waiting write, in the order it was queued. */
|
|
202
|
+
entries() {
|
|
203
|
+
return read()
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Save one write, or throw `queue_full` / `queue_io`. The entry is on disk
|
|
208
|
+
* when this returns.
|
|
209
|
+
*/
|
|
210
|
+
append({ wikiID, scope, filename, content }) {
|
|
211
|
+
const body = String(content ?? "")
|
|
212
|
+
const bytes = Buffer.byteLength(body)
|
|
213
|
+
const waiting = read()
|
|
214
|
+
if (waiting.length >= WIKI_QUEUE_MAX_ENTRIES) throw new WikiQueueError("queue_full", "the wiki write queue is full")
|
|
215
|
+
if (waiting.reduce((sum, entry) => sum + entry.bytes, 0) + bytes > WIKI_QUEUE_MAX_BYTES) {
|
|
216
|
+
throw new WikiQueueError("queue_full", "the wiki write queue holds too many bytes")
|
|
217
|
+
}
|
|
218
|
+
const entry = {
|
|
219
|
+
id: `wq_${randomBytes(8).toString("hex")}`,
|
|
220
|
+
wiki: String(wikiID),
|
|
221
|
+
file: String(filename),
|
|
222
|
+
digest: wikiWriteDigest(body),
|
|
223
|
+
bytes,
|
|
224
|
+
scope: scopeOf(scope),
|
|
225
|
+
content: body,
|
|
226
|
+
at: new Date().toISOString(),
|
|
227
|
+
seq: (sequence += 1),
|
|
228
|
+
}
|
|
229
|
+
saveFile(entry)
|
|
230
|
+
const { seq: _seq, ...saved } = entry
|
|
231
|
+
return saved
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Forget a write the hub accepted. A removal the disk refuses is logged
|
|
236
|
+
* and left to the replay's digest check, which writes nothing twice.
|
|
237
|
+
*/
|
|
238
|
+
remove(id) {
|
|
239
|
+
if (!dir || !ENTRY.test(`${id}.json`)) return
|
|
240
|
+
try {
|
|
241
|
+
fs.unlinkSync(path.join(dir, `${id}.json`))
|
|
242
|
+
} catch (error) {
|
|
243
|
+
if (error?.code !== "ENOENT") log?.warn?.("a wiki write could not be removed from the queue; the replay will skip it by its content", { error: String(error?.code ?? error) })
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
/** A write with the same file and body already waits here. */
|
|
248
|
+
has(wikiID, filename, digest) {
|
|
249
|
+
return read().some((entry) => entry.wiki === wikiID && entry.file === filename && entry.digest === digest)
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
}
|