dsh-seal-scripts 0.5.1 → 0.6.0
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/README.en.md +185 -165
- package/README.md +179 -184
- package/lib/client.js +818 -7
- package/lib/core/assembly.js +8 -8
- package/lib/core/host-limits.js +5 -5
- package/lib/core/index.js +38 -31
- package/lib/core/inject.js +1 -1
- package/lib/core/lang.js +418 -75
- package/lib/core/locale.js +1 -1
- package/lib/core/memory-file.js +2 -5
- package/lib/core/migrate.js +1 -1
- package/lib/core/path-guard.js +4 -4
- package/lib/core/schema-runner.js +10 -10
- package/lib/core/schema-tools.js +88 -88
- package/lib/core/schema.js +49 -61
- package/lib/core/search.js +1 -1
- package/lib/core/settings.js +1 -1
- package/lib/core/store.js +1 -1
- package/lib/core/tool-transfer.js +14 -14
- package/lib/core/tool.js +71 -71
- package/lib/core/zip.js +35 -35
- package/lib/core/zstd-js.js +50 -50
- package/lib/core/zstd.js +1 -1
- package/lib/host/capabilities.js +3 -3
- package/lib/host/compact-delete.js +191 -27
- package/lib/host/delete.js +48 -20
- package/lib/host/host-contract.js +2 -2
- package/lib/host/http-util.js +1 -1
- package/lib/host/lifecycle.js +2 -2
- package/lib/host/register.js +3 -3
- package/lib/host/routes/memories.js +7 -7
- package/lib/host/routes/sessions.js +8 -8
- package/lib/host/routes/tools.js +6 -6
- package/package.json +4 -3
package/lib/core/zstd-js.js
CHANGED
|
@@ -72,9 +72,9 @@ class ForwardBitReader {
|
|
|
72
72
|
|
|
73
73
|
class BackwardBitReader {
|
|
74
74
|
constructor(buf) {
|
|
75
|
-
if (buf.length === 0) throw fail('
|
|
75
|
+
if (buf.length === 0) throw fail('empty bit stream')
|
|
76
76
|
const last = buf[buf.length - 1]
|
|
77
|
-
if (last === 0) throw fail('
|
|
77
|
+
if (last === 0) throw fail('last byte of the bit stream is 0 (no end marker)')
|
|
78
78
|
const hb = highbit32(last)
|
|
79
79
|
// 最后一个有用位:末字节最高置位(结束标记)的前一位。
|
|
80
80
|
this.bitPos = (buf.length - 1) * 8 + hb - 1
|
|
@@ -127,7 +127,7 @@ function readFseTable(data, pos, maxAccuracyLog, expectedSymbols) {
|
|
|
127
127
|
const bits = new ForwardBitReader(data, pos)
|
|
128
128
|
const accuracyLog = bits.peek(4) + 5
|
|
129
129
|
bits.skip(4)
|
|
130
|
-
if (accuracyLog > maxAccuracyLog) throw fail('FSE accuracyLog
|
|
130
|
+
if (accuracyLog > maxAccuracyLog) throw fail('FSE accuracyLog out of range: ' + accuracyLog + ' > ' + maxAccuracyLog)
|
|
131
131
|
|
|
132
132
|
const tableSize = 1 << accuracyLog
|
|
133
133
|
// 每个符号的归一化概率;没出现的符号保持 0(零重复就是跳过这些槽位)。
|
|
@@ -169,8 +169,8 @@ function readFseTable(data, pos, maxAccuracyLog, expectedSymbols) {
|
|
|
169
169
|
} while (rep === 3)
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
|
-
if (probs !== 0) throw fail('FSE
|
|
173
|
-
if (sym >= expectedSymbols) throw fail('FSE
|
|
172
|
+
if (probs !== 0) throw fail('FSE distribution table not fully consumed: probs=' + probs)
|
|
173
|
+
if (sym >= expectedSymbols) throw fail('FSE distribution table has more symbols than the cap ' + expectedSymbols)
|
|
174
174
|
|
|
175
175
|
return { table: buildFseTable(counts, accuracyLog), bytes: bits.bytesUsed() }
|
|
176
176
|
}
|
|
@@ -201,12 +201,12 @@ function buildFseTable(counts, accuracyLog) {
|
|
|
201
201
|
while (position > highThreshold) position = (position + step) & mask
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
|
-
if (position !== 0) throw fail('FSE
|
|
204
|
+
if (position !== 0) throw fail('FSE table spread did not return to 0')
|
|
205
205
|
|
|
206
206
|
const cells = new Array(tableSize)
|
|
207
207
|
for (let u = 0; u < tableSize; u++) {
|
|
208
208
|
const symbol = symbols[u]
|
|
209
|
-
if (symbol < 0) throw fail('FSE
|
|
209
|
+
if (symbol < 0) throw fail('FSE table has holes')
|
|
210
210
|
const nextState = symbolNext[symbol]++
|
|
211
211
|
const nbBits = accuracyLog - highbit32(nextState)
|
|
212
212
|
cells[u] = { symbol, nbBits, newState: (nextState << nbBits) - tableSize }
|
|
@@ -250,7 +250,7 @@ for (let i = 0; i < ML_REST.length; i++) { ML_BASE[32 + i] = ML_REST[i][0]; ML_B
|
|
|
250
250
|
function decodeFseWeights(data) {
|
|
251
251
|
const { table, bytes } = readFseTable(data, 0, 6, 256)
|
|
252
252
|
const stream = data.subarray(bytes)
|
|
253
|
-
if (stream.length === 0) throw fail('Huffman weight
|
|
253
|
+
if (stream.length === 0) throw fail('Huffman weight bit stream is empty')
|
|
254
254
|
const bits = new BackwardBitReader(stream)
|
|
255
255
|
let s1 = bits.readBits(table.accuracyLog)
|
|
256
256
|
let s2 = bits.readBits(table.accuracyLog)
|
|
@@ -259,13 +259,13 @@ function decodeFseWeights(data) {
|
|
|
259
259
|
|
|
260
260
|
const advance = (state) => {
|
|
261
261
|
const cell = table.cells[state]
|
|
262
|
-
if (cell === undefined) throw fail('FSE
|
|
262
|
+
if (cell === undefined) throw fail('FSE state out of range')
|
|
263
263
|
const { value, overflow } = bits.readBitsLenient(cell.nbBits)
|
|
264
264
|
return { state: cell.newState + value, overflow }
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
for (;;) {
|
|
268
|
-
if (out.length > 255) throw fail('Huffman
|
|
268
|
+
if (out.length > 255) throw fail('too many Huffman weights')
|
|
269
269
|
if (first) {
|
|
270
270
|
out.push(table.cells[s1].symbol)
|
|
271
271
|
const r = advance(s1)
|
|
@@ -285,7 +285,7 @@ function decodeFseWeights(data) {
|
|
|
285
285
|
/** 读 Huffman 树描述,返回 { tree, bytes }。 */
|
|
286
286
|
function readHuffmanTree(data, pos) {
|
|
287
287
|
const header = data[pos]
|
|
288
|
-
if (header === undefined) throw fail('Huffman
|
|
288
|
+
if (header === undefined) throw fail('Huffman tree description truncated')
|
|
289
289
|
let weights = []
|
|
290
290
|
let bytes
|
|
291
291
|
if (header >= 128) {
|
|
@@ -294,14 +294,14 @@ function readHuffmanTree(data, pos) {
|
|
|
294
294
|
const start = pos + 1
|
|
295
295
|
for (let i = 0; i < n; i++) {
|
|
296
296
|
const b = data[start + (i >> 1)]
|
|
297
|
-
if (b === undefined) throw fail('Huffman weight
|
|
297
|
+
if (b === undefined) throw fail('Huffman weight truncated')
|
|
298
298
|
weights.push(i % 2 === 0 ? (b >> 4) : (b & 0x0f))
|
|
299
299
|
}
|
|
300
300
|
} else {
|
|
301
|
-
if (header === 0) throw fail('Huffman
|
|
301
|
+
if (header === 0) throw fail('Huffman tree description length is 0')
|
|
302
302
|
bytes = 1 + header
|
|
303
303
|
const segment = data.subarray(pos + 1, pos + 1 + header)
|
|
304
|
-
if (segment.length !== header) throw fail('Huffman
|
|
304
|
+
if (segment.length !== header) throw fail('Huffman tree description truncated')
|
|
305
305
|
weights = decodeFseWeights(segment)
|
|
306
306
|
}
|
|
307
307
|
|
|
@@ -309,14 +309,14 @@ function readHuffmanTree(data, pos) {
|
|
|
309
309
|
for (const w of weights) {
|
|
310
310
|
if (w > 0) sum += 2 ** (w - 1)
|
|
311
311
|
}
|
|
312
|
-
if (sum === 0) throw fail('Huffman
|
|
312
|
+
if (sum === 0) throw fail('all Huffman weights are 0')
|
|
313
313
|
const maxBits = Math.floor(Math.log2(sum)) + 1
|
|
314
314
|
// 最后一个 weight 的“贡献”是 2^(w-1) = tableSize - sum;所以先要求它是 2 的幂,
|
|
315
315
|
// 再取 w = log2(rem) + 1(RFC 例子里 rem=1,看起来像直接相减,其实不是)。
|
|
316
316
|
const rem = 2 ** maxBits - sum
|
|
317
|
-
if (rem <= 0 || (rem & (rem - 1)) !== 0) throw fail('Huffman
|
|
317
|
+
if (rem <= 0 || (rem & (rem - 1)) !== 0) throw fail('Huffman last remaining sum is not a power of two: ' + rem)
|
|
318
318
|
const lastWeight = Math.log2(rem) + 1
|
|
319
|
-
if (lastWeight > maxBits) throw fail('Huffman
|
|
319
|
+
if (lastWeight > maxBits) throw fail('invalid last Huffman weight: ' + lastWeight)
|
|
320
320
|
weights.push(lastWeight)
|
|
321
321
|
|
|
322
322
|
const lengths = weights.map((w) => (w === 0 ? 0 : maxBits + 1 - w))
|
|
@@ -346,7 +346,7 @@ function decodeHuffmanStream(data, count, tree) {
|
|
|
346
346
|
const hit = tree.codes[len].get(code)
|
|
347
347
|
if (hit !== undefined) { sym = hit; break }
|
|
348
348
|
}
|
|
349
|
-
if (sym < 0) throw fail('Huffman
|
|
349
|
+
if (sym < 0) throw fail('Huffman code cannot be matched')
|
|
350
350
|
out[i] = sym
|
|
351
351
|
}
|
|
352
352
|
return out
|
|
@@ -359,7 +359,7 @@ function decodeHuffmanStream(data, count, tree) {
|
|
|
359
359
|
/** 读 literals section,返回 { literals, end }。 */
|
|
360
360
|
function readLiterals(block, p, ctx) {
|
|
361
361
|
const b0 = block[p]
|
|
362
|
-
if (b0 === undefined) throw fail('literals
|
|
362
|
+
if (b0 === undefined) throw fail('literals header truncated')
|
|
363
363
|
const type = b0 & 3
|
|
364
364
|
const sf = (b0 >> 2) & 3
|
|
365
365
|
|
|
@@ -381,7 +381,7 @@ function readLiterals(block, p, ctx) {
|
|
|
381
381
|
return { literals: Buffer.alloc(regen, block[p + headerSize] ?? 0), end: p + headerSize + 1 }
|
|
382
382
|
}
|
|
383
383
|
const start = p + headerSize
|
|
384
|
-
if (start + regen > block.length) throw fail('raw literals
|
|
384
|
+
if (start + regen > block.length) throw fail('raw literals out of range')
|
|
385
385
|
return { literals: block.subarray(start, start + regen), end: start + regen }
|
|
386
386
|
}
|
|
387
387
|
|
|
@@ -402,12 +402,12 @@ function readLiterals(block, p, ctx) {
|
|
|
402
402
|
treeBytes = parsed.bytes
|
|
403
403
|
ctx.huffman = tree
|
|
404
404
|
} else if (!tree) {
|
|
405
|
-
throw fail('treeless literals
|
|
405
|
+
throw fail('treeless literals but no previous Huffman tree')
|
|
406
406
|
}
|
|
407
407
|
|
|
408
408
|
const streamsStart = p + headerSize + treeBytes
|
|
409
409
|
const totalStreams = compSize - treeBytes
|
|
410
|
-
if (totalStreams < 0) throw fail('Huffman
|
|
410
|
+
if (totalStreams < 0) throw fail('invalid Huffman stream length')
|
|
411
411
|
|
|
412
412
|
if (sf === 0) {
|
|
413
413
|
const data = block.subarray(streamsStart, streamsStart + totalStreams)
|
|
@@ -416,23 +416,23 @@ function readLiterals(block, p, ctx) {
|
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
// 4 个流:前 3 个长度在 jump table 里
|
|
419
|
-
if (totalStreams < 6) throw fail('jump table
|
|
419
|
+
if (totalStreams < 6) throw fail('jump table truncated')
|
|
420
420
|
const jump = streamsStart
|
|
421
421
|
const s1 = block.readUInt16LE(jump)
|
|
422
422
|
const s2 = block.readUInt16LE(jump + 2)
|
|
423
423
|
const s3 = block.readUInt16LE(jump + 4)
|
|
424
424
|
const s4 = totalStreams - 6 - s1 - s2 - s3
|
|
425
|
-
if (s4 < 0) throw fail('Huffman
|
|
425
|
+
if (s4 < 0) throw fail('invalid Huffman stream length (jump table)')
|
|
426
426
|
const sizes = [s1, s2, s3, s4]
|
|
427
427
|
const segment = Math.ceil(regen / 4)
|
|
428
428
|
const regenSizes = [segment, segment, segment, regen - 3 * segment]
|
|
429
|
-
if (regenSizes[3] < 0) throw fail('4
|
|
429
|
+
if (regenSizes[3] < 0) throw fail('4-stream literals too small')
|
|
430
430
|
const literals = Buffer.allocUnsafe(regen)
|
|
431
431
|
let cursor = jump + 6
|
|
432
432
|
let outPos = 0
|
|
433
433
|
for (let i = 0; i < 4; i++) {
|
|
434
434
|
const data = block.subarray(cursor, cursor + sizes[i])
|
|
435
|
-
if (data.length !== sizes[i]) throw fail('Huffman
|
|
435
|
+
if (data.length !== sizes[i]) throw fail('Huffman stream truncated')
|
|
436
436
|
const part = decodeHuffmanStream(data, regenSizes[i], tree)
|
|
437
437
|
part.copy(literals, outPos)
|
|
438
438
|
outPos += part.length
|
|
@@ -449,7 +449,7 @@ function initState(bits, table) {
|
|
|
449
449
|
/** 更新一个状态,返回 { state, overflow }。 */
|
|
450
450
|
function stepState(bits, table, state) {
|
|
451
451
|
const cell = table.cells[state]
|
|
452
|
-
if (cell === undefined) throw fail('FSE
|
|
452
|
+
if (cell === undefined) throw fail('FSE state out of range')
|
|
453
453
|
const { value, overflow } = bits.readBitsLenient(cell.nbBits)
|
|
454
454
|
return { state: cell.newState + value, overflow }
|
|
455
455
|
}
|
|
@@ -457,7 +457,7 @@ function stepState(bits, table, state) {
|
|
|
457
457
|
/** 解 sequences 段并把结果写进输出缓冲区。 */
|
|
458
458
|
function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
459
459
|
let p = seqStart
|
|
460
|
-
if (p >= block.length) throw fail('
|
|
460
|
+
if (p >= block.length) throw fail('missing sequences header')
|
|
461
461
|
const b0 = block[p++]
|
|
462
462
|
let count
|
|
463
463
|
if (b0 === 0) {
|
|
@@ -474,21 +474,21 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
474
474
|
}
|
|
475
475
|
|
|
476
476
|
const modes = block[p++]
|
|
477
|
-
if (modes === undefined) throw fail('
|
|
478
|
-
if ((modes & 3) !== 0) throw fail('symbol compression modes
|
|
477
|
+
if (modes === undefined) throw fail('missing symbol compression modes')
|
|
478
|
+
if ((modes & 3) !== 0) throw fail('symbol compression modes reserved bits are not 0')
|
|
479
479
|
const modeLL = (modes >> 6) & 3
|
|
480
480
|
const modeOF = (modes >> 4) & 3
|
|
481
481
|
const modeML = (modes >> 2) & 3
|
|
482
482
|
|
|
483
483
|
const readTable = (mode, key, maxLog, symbols) => {
|
|
484
484
|
if (mode === 3) {
|
|
485
|
-
if (!ctx[key]) throw fail('repeat
|
|
485
|
+
if (!ctx[key]) throw fail('repeat mode has no previous ' + key + ' table')
|
|
486
486
|
return ctx[key]
|
|
487
487
|
}
|
|
488
488
|
if (mode === 0) return PREDEFINED[key]
|
|
489
489
|
if (mode === 1) {
|
|
490
490
|
const symbol = block[p++]
|
|
491
|
-
if (symbol === undefined) throw fail('RLE
|
|
491
|
+
if (symbol === undefined) throw fail('RLE table is missing a symbol')
|
|
492
492
|
return rleTable(symbol)
|
|
493
493
|
}
|
|
494
494
|
const parsed = readFseTable(block, p, maxLog, symbols)
|
|
@@ -514,7 +514,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
514
514
|
for (let i = 0; i < count; i++) {
|
|
515
515
|
const cellOF = tableOF.cells[stateOF]
|
|
516
516
|
const offsetCode = cellOF.symbol
|
|
517
|
-
if (offsetCode > 31) throw fail('offset code
|
|
517
|
+
if (offsetCode > 31) throw fail('offset code out of range')
|
|
518
518
|
const offsetValue = 2 ** offsetCode + bits.readBits(offsetCode)
|
|
519
519
|
|
|
520
520
|
const cellML = tableML.cells[stateML]
|
|
@@ -525,7 +525,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
525
525
|
const llCode = cellLL.symbol
|
|
526
526
|
const literalLength = LL_BASE[llCode] + bits.readBits(LL_BITS[llCode])
|
|
527
527
|
|
|
528
|
-
if (litPos + literalLength > literals.length) throw fail('literal
|
|
528
|
+
if (litPos + literalLength > literals.length) throw fail('literal length out of range')
|
|
529
529
|
out.pushBuffer(literals.subarray(litPos, litPos + literalLength))
|
|
530
530
|
litPos += literalLength
|
|
531
531
|
|
|
@@ -546,7 +546,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
546
546
|
rep[0] = offset
|
|
547
547
|
}
|
|
548
548
|
}
|
|
549
|
-
if (!Number.isInteger(offset) || offset <= 0 || offset > out.pos) throw fail('match offset
|
|
549
|
+
if (!Number.isInteger(offset) || offset <= 0 || offset > out.pos) throw fail('match offset out of range: ' + offset)
|
|
550
550
|
for (let k = 0; k < matchLength; k++) {
|
|
551
551
|
out.ensure(1)
|
|
552
552
|
out.buf[out.pos] = out.buf[out.pos - offset]
|
|
@@ -555,11 +555,11 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
555
555
|
|
|
556
556
|
if (i + 1 < count) {
|
|
557
557
|
const sLL = stepState(bits, tableLL, stateLL); stateLL = sLL.state
|
|
558
|
-
if (sLL.overflow) throw fail('sequences
|
|
558
|
+
if (sLL.overflow) throw fail('sequences bit stream ran out early')
|
|
559
559
|
const sML = stepState(bits, tableML, stateML); stateML = sML.state
|
|
560
|
-
if (sML.overflow) throw fail('sequences
|
|
560
|
+
if (sML.overflow) throw fail('sequences bit stream ran out early')
|
|
561
561
|
const sOF = stepState(bits, tableOF, stateOF); stateOF = sOF.state
|
|
562
|
-
if (sOF.overflow) throw fail('sequences
|
|
562
|
+
if (sOF.overflow) throw fail('sequences bit stream ran out early')
|
|
563
563
|
}
|
|
564
564
|
}
|
|
565
565
|
|
|
@@ -606,18 +606,18 @@ class OutputBuffer {
|
|
|
606
606
|
/** 解一帧,返回 { output, length }。 */
|
|
607
607
|
function decodeFrame(buf, start) {
|
|
608
608
|
let p = start + 4
|
|
609
|
-
if (p >= buf.length) throw fail('
|
|
609
|
+
if (p >= buf.length) throw fail('frame header truncated')
|
|
610
610
|
const fhd = buf[p++]
|
|
611
611
|
const fcsFlag = fhd >> 6
|
|
612
612
|
const single = (fhd >> 5) & 1
|
|
613
613
|
const checksum = (fhd >> 2) & 1
|
|
614
614
|
const dictFlag = fhd & 3
|
|
615
|
-
if (((fhd >> 3) & 1) === 1) throw fail('
|
|
615
|
+
if (((fhd >> 3) & 1) === 1) throw fail('reserved bits are not 0')
|
|
616
616
|
|
|
617
617
|
let windowSize = 0
|
|
618
618
|
if (!single) {
|
|
619
619
|
const wd = buf[p++]
|
|
620
|
-
if (wd === undefined) throw fail('
|
|
620
|
+
if (wd === undefined) throw fail('window descriptor truncated')
|
|
621
621
|
const exp = wd >> 3
|
|
622
622
|
const mant = wd & 7
|
|
623
623
|
windowSize = 2 ** (10 + exp) + (2 ** (10 + exp) / 8) * mant
|
|
@@ -627,7 +627,7 @@ function decodeFrame(buf, start) {
|
|
|
627
627
|
let dictId = 0
|
|
628
628
|
for (let i = 0; i < dictSize; i++) dictId += (buf[p + i] ?? 0) * (256 ** i)
|
|
629
629
|
p += dictSize
|
|
630
|
-
if (dictId !== 0) throw fail('
|
|
630
|
+
if (dictId !== 0) throw fail('frames with a dictionary are not supported (dictID=' + dictId + ')', 'zstd-unsupported')
|
|
631
631
|
|
|
632
632
|
let fcsSize = [0, 2, 4, 8][fcsFlag]
|
|
633
633
|
if (fcsFlag === 0 && single) fcsSize = 1
|
|
@@ -651,7 +651,7 @@ function decodeFrame(buf, start) {
|
|
|
651
651
|
|
|
652
652
|
let last = false
|
|
653
653
|
while (!last) {
|
|
654
|
-
if (p + 3 > buf.length) throw fail('block header
|
|
654
|
+
if (p + 3 > buf.length) throw fail('block header truncated')
|
|
655
655
|
const h = buf[p] | (buf[p + 1] << 8) | (buf[p + 2] << 16)
|
|
656
656
|
p += 3
|
|
657
657
|
last = (h & 1) === 1
|
|
@@ -659,20 +659,20 @@ function decodeFrame(buf, start) {
|
|
|
659
659
|
const size = h >>> 3
|
|
660
660
|
|
|
661
661
|
if (type === 0) {
|
|
662
|
-
if (p + size > buf.length) throw fail('raw block
|
|
662
|
+
if (p + size > buf.length) throw fail('raw block truncated')
|
|
663
663
|
out.pushBuffer(buf.subarray(p, p + size))
|
|
664
664
|
p += size
|
|
665
665
|
} else if (type === 1) {
|
|
666
|
-
if (p >= buf.length) throw fail('RLE block
|
|
666
|
+
if (p >= buf.length) throw fail('RLE block truncated')
|
|
667
667
|
out.pushByte(buf[p++], size)
|
|
668
668
|
} else if (type === 2) {
|
|
669
|
-
if (p + size > buf.length) throw fail('compressed block
|
|
669
|
+
if (p + size > buf.length) throw fail('compressed block truncated')
|
|
670
670
|
const block = buf.subarray(p, p + size)
|
|
671
671
|
const lit = readLiterals(block, 0, ctx)
|
|
672
672
|
decodeSequences(block, lit.end, lit.literals, ctx, out)
|
|
673
673
|
p += size
|
|
674
674
|
} else {
|
|
675
|
-
throw fail('
|
|
675
|
+
throw fail('reserved block type')
|
|
676
676
|
}
|
|
677
677
|
}
|
|
678
678
|
|
|
@@ -689,10 +689,10 @@ export function decodeFrames(buf) {
|
|
|
689
689
|
const chunks = []
|
|
690
690
|
let off = 0
|
|
691
691
|
while (off < buf.length) {
|
|
692
|
-
if (off + 4 > buf.length) throw fail('
|
|
693
|
-
if (buf.readUInt32LE(off) !== MAGIC) throw fail('
|
|
692
|
+
if (off + 4 > buf.length) throw fail('trailing bytes shorter than one frame header')
|
|
693
|
+
if (buf.readUInt32LE(off) !== MAGIC) throw fail('bad magic number @' + off)
|
|
694
694
|
const { output, length } = decodeFrame(buf, off)
|
|
695
|
-
if (length <= 0) throw fail('
|
|
695
|
+
if (length <= 0) throw fail('invalid frame length')
|
|
696
696
|
chunks.push(output)
|
|
697
697
|
off += length
|
|
698
698
|
}
|
package/lib/core/zstd.js
CHANGED
|
@@ -47,7 +47,7 @@ function frameLength(buf, off) {
|
|
|
47
47
|
if (fcsFlag === 0 && single) fcs = 1
|
|
48
48
|
q += fcs
|
|
49
49
|
for (;;) {
|
|
50
|
-
if (q + 3 > buf.length) throw new Error('
|
|
50
|
+
if (q + 3 > buf.length) throw new Error('log truncated @' + q)
|
|
51
51
|
const h = buf[q] | (buf[q + 1] << 8) | (buf[q + 2] << 16)
|
|
52
52
|
q += 3
|
|
53
53
|
const last = h & 1
|
package/lib/host/capabilities.js
CHANGED
|
@@ -43,7 +43,7 @@ export function probeCapabilities(ctx, { only } = {}) {
|
|
|
43
43
|
const missing = []
|
|
44
44
|
const need = (block, name, got) => {
|
|
45
45
|
table[block] = got === true
|
|
46
|
-
if (!got) missing.push({ block, why: '
|
|
46
|
+
if (!got) missing.push({ block, why: 'missing ' + name })
|
|
47
47
|
}
|
|
48
48
|
const more = (block, got) => { table[block] = got === true }
|
|
49
49
|
|
|
@@ -92,7 +92,7 @@ export function probeCapabilities(ctx, { only } = {}) {
|
|
|
92
92
|
// 档 5 起「可用」= 原生可用或被内置解码器兜住——所以这条在本插件里恒为 true;
|
|
93
93
|
// 探测函数单独留着,是为了让宿主能力表仍然是一个能查询的真值。
|
|
94
94
|
const zstd = canDecodeZstd()
|
|
95
|
-
need('zstd.decompress', 'zlib.zstdDecompressSync()
|
|
95
|
+
need('zstd.decompress', 'zlib.zstdDecompressSync() or the built-in decoder', zstd)
|
|
96
96
|
|
|
97
97
|
if (Array.isArray(only)) {
|
|
98
98
|
// 只报「这块此刻应该就绪」的条目:apply 刚进来时别把还没起来的服务当成缺失。
|
|
@@ -127,7 +127,7 @@ export function mergeCapability(base, next) {
|
|
|
127
127
|
// 把「还没看到」当成「没有」会误报一整屏。
|
|
128
128
|
base.missing = REQUIRED.filter((k) => Object.prototype.hasOwnProperty.call(base.table, k) && base.table[k] !== true).map((block) => {
|
|
129
129
|
const hit = (next.missing ?? []).find((m) => m.block === block)
|
|
130
|
-
return hit ?? { block, why: '
|
|
130
|
+
return hit ?? { block, why: 'missing ' + block }
|
|
131
131
|
})
|
|
132
132
|
base.ok = base.missing.length === 0
|
|
133
133
|
return base
|
|
@@ -3,12 +3,47 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 任何一步失败都不删,所以最坏情况是「压缩成功、拆失败、会话还在」——
|
|
5
5
|
* 代价是 compactNow 已经改写了历史,这一步不可逆。
|
|
6
|
+
*
|
|
7
|
+
* 0.5.3 本地修复(三处,都是 0.2.2 web profile 的部署形状逼出来的):
|
|
8
|
+
* 1. 压缩后端按预设隔离:profile 根上下文没有 compaction,「delete」以前必抛。
|
|
9
|
+
* 现在先问 agent 自己的上下文(预设插件是挂进 agent 作用域的)。
|
|
10
|
+
* 2. 会话不在内存里时要 resume;裸 resume 不会挂预设插件,也不带模型,
|
|
11
|
+
* 压缩会失败。优先走宿主自己的组合入口 sessionController.agents.resume(),
|
|
12
|
+
* 它会把预设(含压缩)挂进 agent 作用域并装上模型选择。
|
|
13
|
+
* 3. 每一步失败都留一行到 session-memory/_compact-delete.log,
|
|
14
|
+
* 否则设置页只有一句英文,谁也看不出卡在哪。
|
|
6
15
|
*/
|
|
7
16
|
|
|
17
|
+
import { appendFile, mkdir, readFile, stat, writeFile } from 'node:fs/promises'
|
|
18
|
+
import { join } from 'node:path'
|
|
8
19
|
import { listMemories } from '../core/tool.js'
|
|
20
|
+
import { captureCompaction } from '../core/store.js'
|
|
21
|
+
import { readLocaleSync } from '../core/locale.js'
|
|
9
22
|
import { deleteSessionPermanently } from './delete.js'
|
|
10
23
|
|
|
11
24
|
const TIMEOUT_MS = 120_000
|
|
25
|
+
const LOG_NAME = '_compact-delete.log'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 找出会话里最新的一条压缩事件(0.5.3 本地修复)。
|
|
29
|
+
*
|
|
30
|
+
* 事件监听挂在 profile 根上,压缩却在预设的隔离作用域里发生——那边发出来的
|
|
31
|
+
* session/event 未必传到根插件,于是「等拆解」会一直等到超时、什么都拆不出来。
|
|
32
|
+
* 与其猜监听收不收得到,不如压缩完自己回会话里把那条事件捞出来,直接拆。
|
|
33
|
+
* @param {object|undefined} session
|
|
34
|
+
* @returns {object|undefined}
|
|
35
|
+
*/
|
|
36
|
+
function latestCompactionEvent(session) {
|
|
37
|
+
try {
|
|
38
|
+
const end = typeof session?.seq === 'number' ? session.seq : undefined
|
|
39
|
+
if (end === undefined || typeof session.eventAt !== 'function') return undefined
|
|
40
|
+
for (let seq = end - 1; seq >= 0 && seq > end - 2000; seq--) {
|
|
41
|
+
const event = session.eventAt(seq)
|
|
42
|
+
if (event?.type === 'compaction/summary') return event
|
|
43
|
+
}
|
|
44
|
+
} catch { /* 读不到就不拆,交给后面「没有新回顾」的报错 */ }
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
12
47
|
|
|
13
48
|
function withTimeout(promise, ms, text) {
|
|
14
49
|
return new Promise((yes, no) => {
|
|
@@ -17,60 +52,189 @@ function withTimeout(promise, ms, text) {
|
|
|
17
52
|
})
|
|
18
53
|
}
|
|
19
54
|
|
|
55
|
+
const message = (e) => (e instanceof Error ? e.message : String(e))
|
|
56
|
+
|
|
57
|
+
/** 一行诊断,失败也不遮住真正的报错。 */
|
|
58
|
+
async function note(dshHome, sessionId, text) {
|
|
59
|
+
try {
|
|
60
|
+
const dir = join(dshHome, 'session-memory')
|
|
61
|
+
await mkdir(dir, { recursive: true })
|
|
62
|
+
const file = join(dir, LOG_NAME)
|
|
63
|
+
// 日志不能无限长:超过 256 KB 就只留最后 64 KB(按整行切)。
|
|
64
|
+
try {
|
|
65
|
+
const info = await stat(file)
|
|
66
|
+
if (info.size > 256 * 1024) {
|
|
67
|
+
const kept = (await readFile(file, 'utf8')).slice(-64 * 1024)
|
|
68
|
+
const cut = kept.indexOf('\n')
|
|
69
|
+
await writeFile(file, cut >= 0 ? kept.slice(cut + 1) : kept)
|
|
70
|
+
}
|
|
71
|
+
} catch { /* 文件还没有就算了 */ }
|
|
72
|
+
await appendFile(file, new Date().toISOString() + ' ' + sessionId + ' ' + text + '\n')
|
|
73
|
+
} catch { /* 诊断写不出来就算了 */ }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 关掉我们自己拉起来的 agent;宿主没钩子就用拿到的句柄。 */
|
|
77
|
+
async function release(agent, handle) {
|
|
78
|
+
// 1) 插件自己抓到的句柄
|
|
79
|
+
try { if (handle !== undefined) return await handle.dispose() } catch { /* 落到自造 */ }
|
|
80
|
+
// 2) 没有句柄(例如经 sessionController 恢复的 agent):自己造一把,和删除路径同一套
|
|
81
|
+
try {
|
|
82
|
+
if (typeof agent?.cancel === 'function' && typeof agent.whenIdle === 'function'
|
|
83
|
+
&& typeof agent.scope?.dispose === 'function') {
|
|
84
|
+
agent.cancel({ kind: 'disposed' }, { keepInbox: true })
|
|
85
|
+
await agent.whenIdle()
|
|
86
|
+
await agent.scope.dispose()
|
|
87
|
+
}
|
|
88
|
+
} catch { /* 关不掉也不遮住真正的报错 */ }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 找一个能用的压缩后端。宿主把它挂在预设作用域里,根上下文看不见。
|
|
93
|
+
* @param {object} deps
|
|
94
|
+
* @param {object|undefined} agent
|
|
95
|
+
* @returns {{source:string, service:object}|null}
|
|
96
|
+
*/
|
|
97
|
+
function findCompaction(deps, agent) {
|
|
98
|
+
const seen = []
|
|
99
|
+
const tryOne = (source, service) => {
|
|
100
|
+
const usable = typeof service?.compactNow === 'function'
|
|
101
|
+
seen.push(source + (usable ? '' : '(no compactNow)'))
|
|
102
|
+
return usable ? { source, service } : null
|
|
103
|
+
}
|
|
104
|
+
const found = tryOne('deps.compaction', deps.compaction)
|
|
105
|
+
?? tryOne('agent.ctx', agent?.ctx?.get?.('compaction'))
|
|
106
|
+
?? tryOne('agent.loopCtx', agent?.loopCtx?.get?.('compaction'))
|
|
107
|
+
return found === null ? { seen } : found
|
|
108
|
+
}
|
|
109
|
+
|
|
20
110
|
/**
|
|
21
|
-
* @param {object} deps 含 persistence / sessions / agents / workspaceRegistry / compaction
|
|
111
|
+
* @param {object} deps 含 persistence / sessions / agents / workspaceRegistry / compaction / sessionController
|
|
22
112
|
* @param {string} dshHome
|
|
23
113
|
* @param {string} sessionId
|
|
24
114
|
* @param {(id:string, ms:number) => Promise<object>} waitForSplit 等这一轮拆解落盘
|
|
25
115
|
* @returns {Promise<{ok:true, deleted:string, memory:string}>}
|
|
26
116
|
*/
|
|
27
117
|
export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit) {
|
|
28
|
-
if (typeof sessionId !== 'string' || sessionId === '') throw new Error('sessionId
|
|
29
|
-
if (deps.compaction === undefined) throw new Error('压缩服务不可用,删不了')
|
|
118
|
+
if (typeof sessionId !== 'string' || sessionId === '') throw new Error('sessionId must be a non-empty string')
|
|
30
119
|
|
|
31
120
|
const header = (await deps.persistence.list()).map((r) => r.header ?? r).find((h) => h.id === sessionId)
|
|
32
|
-
if (header === undefined) throw new Error('
|
|
121
|
+
if (header === undefined) throw new Error('no such session: ' + sessionId)
|
|
33
122
|
const cwd = header.cwd
|
|
34
|
-
if (typeof cwd !== 'string' || cwd === '') throw new Error('
|
|
123
|
+
if (typeof cwd !== 'string' || cwd === '') throw new Error('this session has no workspace, so no review can be captured')
|
|
35
124
|
|
|
36
125
|
const before = new Set((await listMemories(dshHome, cwd)).entries.map((e) => e.file))
|
|
37
126
|
|
|
38
127
|
let handle
|
|
39
128
|
let agent = deps.agents?.get(sessionId)
|
|
40
129
|
if (agent === undefined) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (
|
|
130
|
+
// 宿主自己的组合入口:预设插件(含压缩)与模型选择都挂进 agent 作用域。
|
|
131
|
+
const controller = deps.sessionController?.agents
|
|
132
|
+
if (typeof controller?.resume === 'function') {
|
|
133
|
+
try {
|
|
134
|
+
agent = await controller.resume(sessionId)
|
|
135
|
+
await note(dshHome, sessionId, 'resumed via sessionController')
|
|
136
|
+
} catch (error) {
|
|
137
|
+
await note(dshHome, sessionId, 'sessionController.resume failed: ' + message(error))
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (agent === undefined || agent === null) {
|
|
141
|
+
handle = await deps.agents.resume({ resumeSessionId: sessionId })
|
|
142
|
+
agent = handle?.agent
|
|
143
|
+
await note(dshHome, sessionId, 'resumed via agents.resume (bare, no preset mounted)')
|
|
144
|
+
if (agent === undefined) throw new Error('failed to resume the session; cannot delete')
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
await note(dshHome, sessionId, 'agent was already live')
|
|
44
148
|
}
|
|
45
149
|
|
|
150
|
+
// 压缩后端在预设的隔离作用域里,外面任何 ctx.get 都看不到。
|
|
151
|
+
// 但预设里那份 command-compact 是挂在全局命令表上的:让它替我们调,
|
|
152
|
+
// 它闭包里的 ctx 就在隔离作用域内,能看到 compaction。
|
|
153
|
+
void waitForSplit // 不再依赖事件监听:压缩完自己拆(见 latestCompactionEvent)
|
|
46
154
|
const controller = new AbortController()
|
|
47
|
-
const
|
|
155
|
+
const commands = deps.commands
|
|
48
156
|
|
|
49
|
-
let
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
157
|
+
let compacted = null
|
|
158
|
+
let result = null
|
|
159
|
+
if (typeof commands?.execute === 'function') {
|
|
160
|
+
try {
|
|
161
|
+
compacted = await withTimeout(
|
|
162
|
+
Promise.resolve(commands.execute(agent, '/compact', [], controller.signal)),
|
|
163
|
+
TIMEOUT_MS,
|
|
164
|
+
'compaction timed out'
|
|
165
|
+
)
|
|
166
|
+
} catch (error) {
|
|
167
|
+
controller.abort()
|
|
168
|
+
await note(dshHome, sessionId, '/compact threw: ' + message(error))
|
|
169
|
+
throw new Error('compaction failed, session not deleted: ' + message(error))
|
|
170
|
+
}
|
|
171
|
+
if (compacted === undefined) {
|
|
172
|
+
await note(dshHome, sessionId, '/compact is not registered for this session')
|
|
173
|
+
throw new Error('compaction is unavailable for this session (/compact is not registered), session not deleted')
|
|
174
|
+
}
|
|
175
|
+
const outcome = compacted.result ?? {}
|
|
176
|
+
await note(dshHome, sessionId, '/compact -> ' + outcome.kind + ': ' + (outcome.text ?? ''))
|
|
177
|
+
if (outcome.kind !== 'success') {
|
|
178
|
+
throw new Error('compaction failed, session not deleted: ' + (outcome.text ?? 'unknown error'))
|
|
179
|
+
}
|
|
180
|
+
if (/no compactable history/i.test(outcome.text ?? '')) {
|
|
181
|
+
throw new Error('no compactable history left, so there is nothing to review; use plain delete instead (session not deleted)')
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
const found = findCompaction(deps, agent)
|
|
185
|
+
if (found.service === undefined) {
|
|
186
|
+
await note(dshHome, sessionId, 'no compaction backend; looked at: ' + (found.seen ?? []).join(', '))
|
|
187
|
+
await release(agent, handle)
|
|
188
|
+
throw new Error('the compaction service is unavailable; cannot delete (no compaction backend is reachable from this session)')
|
|
189
|
+
}
|
|
190
|
+
await note(dshHome, sessionId, 'compaction from ' + found.source)
|
|
191
|
+
try {
|
|
192
|
+
result = await withTimeout(
|
|
193
|
+
Promise.resolve(found.service.compactNow(agent, controller.signal, 'session-seal-scripts-delete')),
|
|
194
|
+
TIMEOUT_MS,
|
|
195
|
+
'compaction timed out'
|
|
196
|
+
)
|
|
197
|
+
} catch (error) {
|
|
198
|
+
controller.abort()
|
|
199
|
+
await note(dshHome, sessionId, 'compactNow threw: ' + message(error))
|
|
200
|
+
throw new Error('compaction failed, session not deleted: ' + message(error))
|
|
201
|
+
}
|
|
202
|
+
if (result === null) {
|
|
203
|
+
await note(dshHome, sessionId, 'compactNow returned null (nothing to compact)')
|
|
204
|
+
throw new Error('no compactable history left, so there is nothing to review; use plain delete instead (session not deleted)')
|
|
205
|
+
}
|
|
59
206
|
}
|
|
60
|
-
|
|
207
|
+
await note(dshHome, sessionId, 'compaction finished')
|
|
61
208
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
209
|
+
// 自己拆:会话里那条 compaction/summary 就是回顾的原料。
|
|
210
|
+
const session = agent?.session
|
|
211
|
+
const event = latestCompactionEvent(session)
|
|
212
|
+
if (event === undefined) {
|
|
213
|
+
await note(dshHome, sessionId, 'no compaction/summary event on the session after compaction')
|
|
214
|
+
} else {
|
|
215
|
+
try {
|
|
216
|
+
const captured = await captureCompaction({ dshHome, cwd, session, event, lang: readLocaleSync(dshHome) })
|
|
217
|
+
await note(dshHome, sessionId, captured?.file
|
|
218
|
+
? 'captured review ourselves: ' + captured.file
|
|
219
|
+
: 'capture skipped: ' + (captured?.reason ?? 'no file'))
|
|
220
|
+
} catch (error) {
|
|
221
|
+
await note(dshHome, sessionId, 'self capture failed: ' + message(error))
|
|
222
|
+
}
|
|
66
223
|
}
|
|
67
224
|
|
|
68
225
|
const after = await listMemories(dshHome, cwd)
|
|
69
226
|
const fresh = after.entries.filter((e) => !before.has(e.file))
|
|
70
|
-
if (fresh.length === 0)
|
|
227
|
+
if (fresh.length === 0) {
|
|
228
|
+
await note(dshHome, sessionId, 'no new review file appeared')
|
|
229
|
+
throw new Error('compaction finished but produced no new review, session not deleted (nothing was removed)')
|
|
230
|
+
}
|
|
71
231
|
|
|
72
232
|
const target = fresh[fresh.length - 1]
|
|
73
233
|
const deleted = await deleteSessionPermanently(deps, sessionId)
|
|
74
|
-
if (!deleted.ok)
|
|
234
|
+
if (!deleted.ok) {
|
|
235
|
+
await note(dshHome, sessionId, 'delete after review failed: ' + deleted.error.message)
|
|
236
|
+
throw new Error('review was captured (' + target.file + ') but the delete failed: ' + deleted.error.message)
|
|
237
|
+
}
|
|
238
|
+
await note(dshHome, sessionId, 'deleted, review ' + target.file)
|
|
75
239
|
return { ok: true, deleted: sessionId, memory: target.file }
|
|
76
|
-
}
|
|
240
|
+
}
|