peaks-loop-shared-channel 0.0.56 → 0.0.58

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.
@@ -171,7 +171,7 @@ function sanitizeSegment(value, field) {
171
171
  }
172
172
  for (let i = 0; i < value.length; i += 1) {
173
173
  const code = value.charCodeAt(i);
174
- if (code <= 0x20 || code === 0x7F) {
174
+ if (code <= 0x20 || code === 0x7f) {
175
175
  throw new Error(`${field} must not contain whitespace or control characters`);
176
176
  }
177
177
  }
package/dist/file-lock.js CHANGED
@@ -82,7 +82,8 @@ export function withFileLockSync(filePath, fn) {
82
82
  try {
83
83
  unlinkSync(lockPath);
84
84
  }
85
- catch { // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
85
+ catch {
86
+ // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
86
87
  // race: another process reaped it; loop will try to acquire directly.
87
88
  }
88
89
  }
@@ -117,13 +118,15 @@ export function withFileLockSync(filePath, fn) {
117
118
  try {
118
119
  closeSync(fd);
119
120
  }
120
- catch { // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
121
+ catch {
122
+ // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
121
123
  // fd may already be closed by the OS on a crash; ignore.
122
124
  }
123
125
  try {
124
126
  unlinkSync(lockPath);
125
127
  }
126
- catch { // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
128
+ catch {
129
+ // TODO(g2): legacy silent catch — grace: 1 minor release (v2.14.0)
127
130
  // Best-effort: stale .lock is harmless; the next acquirer reaps.
128
131
  }
129
132
  }
@@ -0,0 +1,38 @@
1
+ export interface SharedChannelEntry {
2
+ readonly version: 1;
3
+ readonly at: string;
4
+ readonly from: string;
5
+ readonly key: string;
6
+ readonly value: Readonly<Record<string, unknown>>;
7
+ readonly valueSize: number;
8
+ }
9
+ export interface SharedChannel {
10
+ readonly batchId: string;
11
+ readonly createdAt: string;
12
+ readonly updatedAt: string;
13
+ readonly entries: Readonly<Record<string, SharedChannelEntry>>;
14
+ }
15
+ export type WriteSharedEntryResult = {
16
+ readonly ok: true;
17
+ readonly entry: SharedChannelEntry;
18
+ readonly channelSize: number;
19
+ readonly lastWriteWins: boolean;
20
+ readonly softWarning: boolean;
21
+ } | {
22
+ readonly ok: false;
23
+ readonly code: 'VALUE_TOO_LARGE' | 'INVALID_BATCH_ID' | 'WRITE_ERROR';
24
+ readonly message: string;
25
+ };
26
+ declare function readChannelOrEmpty(channelFile: string, batchId: string): SharedChannel;
27
+ declare function writeAtomic(path: string, channel: SharedChannel): void;
28
+ /**
29
+ * Compile a simple key pattern with `*` wildcards to a matcher. Only
30
+ * `*` is special; everything else is a literal. `*` matches zero or
31
+ * more characters. Examples:
32
+ * "rd.*" matches "rd.completed", "rd.found-blocker"
33
+ * "*.completed" matches "rd.completed", "qa.completed"
34
+ * "*" matches everything
35
+ */
36
+ export declare function compileKeyPattern(pattern: string): (key: string) => boolean;
37
+ export { readChannelOrEmpty, writeAtomic };
38
+ //# sourceMappingURL=shared-channel-internals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-channel-internals.d.ts","sourceRoot":"","sources":["../src/shared-channel-internals.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,kBAAkB;IAKjC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC;CAChE;AAED,MAAM,MAAM,sBAAsB,GAC9B;IACE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,kBAAkB,CAAC;IACnC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;CAC/B,GACD;IACE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB,GAAG,aAAa,CAAC;IACtE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEN,iBAAS,kBAAkB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa,CA4B/E;AA0BD,iBAAS,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAa/D;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAU3E;AAED,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * G8 — shared-channel type surface and its non-exported fs internals.
3
+ *
4
+ * Split out of `shared-channel.ts` purely to bring that module under the
5
+ * 300-line file cap. Every declaration here is moved VERBATIM — no behaviour
6
+ * change. `shared-channel.ts` imports these and re-exports the public names
7
+ * (`SharedChannelEntry`, `SharedChannel`, `WriteSharedEntryResult`,
8
+ * `compileKeyPattern`) from its own path, so the package's public surface
9
+ * (`src/index.ts` → `export * from './shared-channel.js'`) is unchanged.
10
+ */
11
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
12
+ import { dirname } from 'node:path';
13
+ function readChannelOrEmpty(channelFile, batchId) {
14
+ if (!existsSync(channelFile)) {
15
+ const now = new Date().toISOString();
16
+ return { batchId, createdAt: now, updatedAt: now, entries: {} };
17
+ }
18
+ let parsed;
19
+ try {
20
+ parsed = JSON.parse(readFileSync(channelFile, 'utf8'));
21
+ }
22
+ catch {
23
+ const now = new Date().toISOString();
24
+ return { batchId, createdAt: now, updatedAt: now, entries: {} };
25
+ }
26
+ if (!isObject(parsed)) {
27
+ const now = new Date().toISOString();
28
+ return { batchId, createdAt: now, updatedAt: now, entries: {} };
29
+ }
30
+ const obj = parsed;
31
+ const batchIdField = typeof obj.batchId === 'string' ? obj.batchId : batchId;
32
+ const createdAt = typeof obj.createdAt === 'string' ? obj.createdAt : new Date().toISOString();
33
+ const updatedAt = typeof obj.updatedAt === 'string' ? obj.updatedAt : createdAt;
34
+ const entriesField = isObject(obj.entries) ? obj.entries : {};
35
+ const entries = {};
36
+ for (const [k, v] of Object.entries(entriesField)) {
37
+ if (isValidEntry(v)) {
38
+ entries[k] = v;
39
+ }
40
+ }
41
+ return { batchId: batchIdField, createdAt, updatedAt, entries };
42
+ }
43
+ function isValidEntry(v) {
44
+ if (!isObject(v))
45
+ return false;
46
+ // Slice 2026-06-23-audit-4th #E2: pre-versioning records (no
47
+ // `version` field) are still accepted on read for backward compat.
48
+ // The writer stamps `version: 1` going forward; readers default
49
+ // missing/legacy entries to version 1 in memory.
50
+ if (!('version' in v) || v.version === 1) {
51
+ return (typeof v.at === 'string' &&
52
+ typeof v.from === 'string' &&
53
+ typeof v.key === 'string' &&
54
+ isObject(v.value) &&
55
+ typeof v.valueSize === 'number');
56
+ }
57
+ // Future versions: read-side handler can branch on `v.version` once
58
+ // v2 lands. For now, anything other than 1 is rejected.
59
+ return false;
60
+ }
61
+ function isObject(v) {
62
+ return typeof v === 'object' && v !== null && !Array.isArray(v);
63
+ }
64
+ function writeAtomic(path, channel) {
65
+ const dir = dirname(path);
66
+ // Slice 2026-06-23-audit-3rd #11: skip mkdirSync when the dir already
67
+ // exists. The `recursive: true` mkdir is a syscall (~50µs on macOS,
68
+ // ~10ms on cold Windows cache); the `existsSync` short-circuit saves
69
+ // it on the hot path (every `peaks sub-agent share` + every
70
+ // `peaks sub-agent heartbeat`).
71
+ if (!existsSync(dir)) {
72
+ mkdirSync(dir, { recursive: true });
73
+ }
74
+ const tmp = `${path}.tmp-${process.pid}-${Date.now()}`;
75
+ writeFileSync(tmp, JSON.stringify(channel, null, 2) + '\n', 'utf8');
76
+ renameSync(tmp, path);
77
+ }
78
+ /**
79
+ * Compile a simple key pattern with `*` wildcards to a matcher. Only
80
+ * `*` is special; everything else is a literal. `*` matches zero or
81
+ * more characters. Examples:
82
+ * "rd.*" matches "rd.completed", "rd.found-blocker"
83
+ * "*.completed" matches "rd.completed", "qa.completed"
84
+ * "*" matches everything
85
+ */
86
+ export function compileKeyPattern(pattern) {
87
+ if (pattern === '*') {
88
+ return () => true;
89
+ }
90
+ const escaped = pattern
91
+ .split('*')
92
+ .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&'))
93
+ .join('.*');
94
+ const re = new RegExp(`^${escaped}$`);
95
+ return (key) => re.test(key);
96
+ }
97
+ export { readChannelOrEmpty, writeAtomic };
@@ -1,32 +1,10 @@
1
- export interface SharedChannelEntry {
2
- readonly version: 1;
3
- readonly at: string;
4
- readonly from: string;
5
- readonly key: string;
6
- readonly value: Readonly<Record<string, unknown>>;
7
- readonly valueSize: number;
8
- }
9
- export interface SharedChannel {
10
- readonly batchId: string;
11
- readonly createdAt: string;
12
- readonly updatedAt: string;
13
- readonly entries: Readonly<Record<string, SharedChannelEntry>>;
14
- }
1
+ import { compileKeyPattern, type SharedChannel, type SharedChannelEntry, type WriteSharedEntryResult } from './shared-channel-internals.js';
2
+ export { compileKeyPattern };
3
+ export type { SharedChannel, SharedChannelEntry, WriteSharedEntryResult };
15
4
  export declare const SHARED_CHANNEL_MAX_VALUE_BYTES: number;
16
5
  export declare const SHARED_CHANNEL_SOFT_VALUE_WARN = 1024;
17
6
  export declare const SHARED_CHANNEL_MAX_FILE_BYTES: number;
18
7
  export declare const SHARED_CHANNEL_TTL_DAYS = 30;
19
- export type WriteSharedEntryResult = {
20
- readonly ok: true;
21
- readonly entry: SharedChannelEntry;
22
- readonly channelSize: number;
23
- readonly lastWriteWins: boolean;
24
- readonly softWarning: boolean;
25
- } | {
26
- readonly ok: false;
27
- readonly code: 'VALUE_TOO_LARGE' | 'INVALID_BATCH_ID' | 'WRITE_ERROR';
28
- readonly message: string;
29
- };
30
8
  /**
31
9
  * Write a shared entry to the per-batch channel file. Atomic-write
32
10
  * (tmp + rename). Returns the new entry + channel size + last-write-wins
@@ -82,13 +60,4 @@ export declare function isOrphanChannel(opts: {
82
60
  batchId: string;
83
61
  now?: Date;
84
62
  }): boolean;
85
- /**
86
- * Compile a simple key pattern with `*` wildcards to a matcher. Only
87
- * `*` is special; everything else is a literal. `*` matches zero or
88
- * more characters. Examples:
89
- * "rd.*" matches "rd.completed", "rd.found-blocker"
90
- * "*.completed" matches "rd.completed", "qa.completed"
91
- * "*" matches everything
92
- */
93
- export declare function compileKeyPattern(pattern: string): (key: string) => boolean;
94
63
  //# sourceMappingURL=shared-channel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared-channel.d.ts","sourceRoot":"","sources":["../src/shared-channel.ts"],"names":[],"mappings":"AAuBA,MAAM,WAAW,kBAAkB;IAKjC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACl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,MAAM,uBAAuB,KAAK,CAAC;AAE1C;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,GAAG,sBAAsB,CAiIzB;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE;IACtC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GAAG,aAAa,CAoBhB;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB,GAAG,OAAO,CAcV;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,GAAG,OAAO,CAUV"}
@@ -16,10 +16,13 @@
16
16
  * See: `.peaks/memory/sub-agent-shared-channel-cross-completion.md` for
17
17
  * the full G8 rule.
18
18
  */
19
- import { existsSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
20
- import { dirname } from 'node:path';
19
+ import { existsSync, statSync, unlinkSync } from 'node:fs';
21
20
  import { assertSafeSharedChannelPath, sharedChannelPath } from './dispatch-context-guard.js';
22
21
  import { withFileLockSync } from './file-lock.js';
22
+ import { compileKeyPattern, readChannelOrEmpty, writeAtomic } from './shared-channel-internals.js';
23
+ // The type surface and `compileKeyPattern` moved to a sibling to clear the
24
+ // file cap; re-exported here so the package's public path is unchanged.
25
+ export { compileKeyPattern };
23
26
  export const SHARED_CHANNEL_MAX_VALUE_BYTES = 64 * 1024; // 64KB hard reject
24
27
  export const SHARED_CHANNEL_SOFT_VALUE_WARN = 1024; // 1KB soft warning
25
28
  export const SHARED_CHANNEL_MAX_FILE_BYTES = 1024 * 1024; // 1MB LRU cap
@@ -221,88 +224,3 @@ export function isOrphanChannel(opts) {
221
224
  const ttlMs = SHARED_CHANNEL_TTL_DAYS * 24 * 60 * 60 * 1000;
222
225
  return ageMs > ttlMs;
223
226
  }
224
- // ─── internals ───────────────────────────────────────────────────────
225
- function readChannelOrEmpty(channelFile, batchId) {
226
- if (!existsSync(channelFile)) {
227
- const now = new Date().toISOString();
228
- return { batchId, createdAt: now, updatedAt: now, entries: {} };
229
- }
230
- let parsed;
231
- try {
232
- parsed = JSON.parse(readFileSync(channelFile, 'utf8'));
233
- }
234
- catch {
235
- const now = new Date().toISOString();
236
- return { batchId, createdAt: now, updatedAt: now, entries: {} };
237
- }
238
- if (!isObject(parsed)) {
239
- const now = new Date().toISOString();
240
- return { batchId, createdAt: now, updatedAt: now, entries: {} };
241
- }
242
- const obj = parsed;
243
- const batchIdField = typeof obj.batchId === 'string' ? obj.batchId : batchId;
244
- const createdAt = typeof obj.createdAt === 'string' ? obj.createdAt : new Date().toISOString();
245
- const updatedAt = typeof obj.updatedAt === 'string' ? obj.updatedAt : createdAt;
246
- const entriesField = isObject(obj.entries) ? obj.entries : {};
247
- const entries = {};
248
- for (const [k, v] of Object.entries(entriesField)) {
249
- if (isValidEntry(v)) {
250
- entries[k] = v;
251
- }
252
- }
253
- return { batchId: batchIdField, createdAt, updatedAt, entries };
254
- }
255
- function isValidEntry(v) {
256
- if (!isObject(v))
257
- return false;
258
- // Slice 2026-06-23-audit-4th #E2: pre-versioning records (no
259
- // `version` field) are still accepted on read for backward compat.
260
- // The writer stamps `version: 1` going forward; readers default
261
- // missing/legacy entries to version 1 in memory.
262
- if (!('version' in v) || v.version === 1) {
263
- return (typeof v.at === 'string' &&
264
- typeof v.from === 'string' &&
265
- typeof v.key === 'string' &&
266
- isObject(v.value) &&
267
- typeof v.valueSize === 'number');
268
- }
269
- // Future versions: read-side handler can branch on `v.version` once
270
- // v2 lands. For now, anything other than 1 is rejected.
271
- return false;
272
- }
273
- function isObject(v) {
274
- return typeof v === 'object' && v !== null && !Array.isArray(v);
275
- }
276
- function writeAtomic(path, channel) {
277
- const dir = dirname(path);
278
- // Slice 2026-06-23-audit-3rd #11: skip mkdirSync when the dir already
279
- // exists. The `recursive: true` mkdir is a syscall (~50µs on macOS,
280
- // ~10ms on cold Windows cache); the `existsSync` short-circuit saves
281
- // it on the hot path (every `peaks sub-agent share` + every
282
- // `peaks sub-agent heartbeat`).
283
- if (!existsSync(dir)) {
284
- mkdirSync(dir, { recursive: true });
285
- }
286
- const tmp = `${path}.tmp-${process.pid}-${Date.now()}`;
287
- writeFileSync(tmp, JSON.stringify(channel, null, 2) + '\n', 'utf8');
288
- renameSync(tmp, path);
289
- }
290
- /**
291
- * Compile a simple key pattern with `*` wildcards to a matcher. Only
292
- * `*` is special; everything else is a literal. `*` matches zero or
293
- * more characters. Examples:
294
- * "rd.*" matches "rd.completed", "rd.found-blocker"
295
- * "*.completed" matches "rd.completed", "qa.completed"
296
- * "*" matches everything
297
- */
298
- export function compileKeyPattern(pattern) {
299
- if (pattern === '*') {
300
- return () => true;
301
- }
302
- const escaped = pattern
303
- .split('*')
304
- .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&'))
305
- .join('.*');
306
- const re = new RegExp(`^${escaped}$`);
307
- return (key) => re.test(key);
308
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peaks-loop-shared-channel",
3
- "version": "0.0.56",
3
+ "version": "0.0.58",
4
4
  "description": "Dispatcher-mediated shared channel and its filesystem safety primitives for peaks-loop.",
5
5
  "keywords": [
6
6
  "peaks-loop",