threadwire 0.1.22 → 0.1.23

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 CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Complete every short filesystem write while spooling oversized worker JSONL
6
+ records, preventing truncated or corrupted worker output. Reject stalled,
7
+ invalid, or impossible write counts and clean up the private spool artifact
8
+ instead of silently accepting partial data.
9
+
5
10
  - Fix Node 24 release-prepublish timeouts in `run-worker` process-tree cleanup.
6
11
  The dedicated `npm-release` service now runs with Docker's `init: true` so
7
12
  terminated descendants are reaped instead of becoming zombies that keep the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "threadwire",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "description": "Stream Codex, Claude, Kimi Code, and OpenCode worker progress to an explicit Telegram destination",
5
5
  "keywords": [
6
6
  "ai-agent",
@@ -4,6 +4,14 @@ import {closeSync, mkdtempSync, openSync, readFileSync, rmSync, unlinkSync, writ
4
4
  import {tmpdir} from "node:os"
5
5
  import {isAbsolute, join, normalize} from "node:path"
6
6
 
7
+ const filesystemOperations = {writeSync}
8
+
9
+ /**
10
+ * @typedef {object} FilesystemOperations
11
+ * @property {(fileDescriptor: number, buffer: Buffer, offset: number, length: number) => number} writeSync
12
+ * Write bytes to a file descriptor and return the count written.
13
+ */
14
+
7
15
  /**
8
16
  * Single-owner byte-oriented spool for one worker's pending JSONL record.
9
17
  *
@@ -21,13 +29,14 @@ import {isAbsolute, join, normalize} from "node:path"
21
29
  * owner calls it on every settlement path.
22
30
  */
23
31
  export class JsonlRecordSpool {
24
- /** @param {{memoryBytes: number, maxBytes: number, directory?: string}} options */
32
+ /** @param {{memoryBytes: number, maxBytes: number, directory?: string, filesystem?: FilesystemOperations}} options */
25
33
  constructor(options) {
26
34
  this.memoryBytes = positiveSafeInteger(options.memoryBytes, "memoryBytes")
27
35
  this.maxBytes = positiveSafeInteger(options.maxBytes, "maxBytes")
28
36
  const directory = options.directory ?? tmpdir()
29
37
  if (!isAbsolute(directory) || normalize(directory) !== directory) throw new Error("directory must be a normalized absolute path")
30
38
  this.directory = directory
39
+ this.filesystem = options.filesystem ?? filesystemOperations
31
40
  /** @type {Buffer[]} */
32
41
  this.segments = []
33
42
  this.pendingBytes = 0
@@ -120,7 +129,7 @@ export class JsonlRecordSpool {
120
129
  if (this.file === null) this.append(finalSegment)
121
130
  else {
122
131
  this.pendingBytes = recordBytes
123
- writeSync(this.file.fd, finalSegment)
132
+ this.writeAll(this.file.fd, finalSegment)
124
133
  }
125
134
  const file = this.file
126
135
  if (file === null) throw new Error("JSONL record spool file is unavailable")
@@ -137,7 +146,7 @@ export class JsonlRecordSpool {
137
146
  this.pendingBytes += segment.length
138
147
  if (this.pendingBytes > this.maxBytes) throw new StdoutRecordTooLargeError()
139
148
  if (this.file !== null) {
140
- writeSync(this.file.fd, segment)
149
+ this.writeAll(this.file.fd, segment)
141
150
  return
142
151
  }
143
152
  if (this.pendingBytes <= this.memoryBytes) {
@@ -150,9 +159,26 @@ export class JsonlRecordSpool {
150
159
  const path = join(this.ownedDirectory, "pending-record")
151
160
  const fd = openSync(path, "wx", 0o600)
152
161
  this.file = {fd, path}
153
- for (const buffered of this.segments) writeSync(fd, buffered)
162
+ for (const buffered of this.segments) this.writeAll(fd, buffered)
154
163
  this.segments = []
155
- writeSync(fd, segment)
164
+ this.writeAll(fd, segment)
165
+ }
166
+
167
+ /**
168
+ * Write a complete Buffer to one file descriptor, looping over short writes.
169
+ * @param {number} fd
170
+ * @param {Buffer} buffer
171
+ */
172
+ writeAll(fd, buffer) {
173
+ let offset = 0
174
+ while (offset < buffer.length) {
175
+ const written = this.filesystem.writeSync(fd, buffer, offset, buffer.length - offset)
176
+ if (written === 0) throw new Error("Spool write made zero bytes of progress")
177
+ if (!Number.isSafeInteger(written) || written < 0 || written > buffer.length - offset) {
178
+ throw new Error(`Spool write returned invalid byte count: ${written}`)
179
+ }
180
+ offset += written
181
+ }
156
182
  }
157
183
 
158
184
  /** Remove the current record's private spill directory, if one exists. */