advisers 0.2.2 → 0.3.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/LICENSE +0 -0
- package/NOTICE +0 -0
- package/README-zh.md +11 -7
- package/README.md +16 -8
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +6347 -542
- package/index.js +1 -1
- package/lib/agent.js +529 -80
- package/lib/attachments.js +0 -0
- package/lib/audit-anchor.js +683 -0
- package/lib/backup.js +1345 -0
- package/lib/capture.js +0 -0
- package/lib/context-window.js +0 -0
- package/lib/digest.js +0 -0
- package/lib/doc-review.js +0 -0
- package/lib/egress.js +0 -0
- package/lib/error-codes.js +1373 -0
- package/lib/evidence.js +352 -43
- package/lib/fs-safe.js +347 -0
- package/lib/ical.js +0 -0
- package/lib/imap.js +6 -2
- package/lib/importers.js +0 -0
- package/lib/learning.js +0 -0
- package/lib/ledger-read.js +608 -0
- package/lib/mcp-server.js +331 -50
- package/lib/model.js +73 -1
- package/lib/node-warning-filter.js +15 -0
- package/lib/notify.js +13 -3
- package/lib/nudge.js +0 -0
- package/lib/ocr.js +29 -9
- package/lib/pdf.js +0 -0
- package/lib/pet-brief-store.js +0 -0
- package/lib/pet-brief.js +126 -64
- package/lib/receipt-verify.js +213 -15
- package/lib/relative-date.js +0 -0
- package/lib/remind.js +91 -36
- package/lib/research.js +0 -0
- package/lib/review.js +0 -0
- package/lib/routine.js +0 -0
- package/lib/safety/classifier.js +55 -2
- package/lib/safety/explain.js +0 -0
- package/lib/safety/index.js +0 -0
- package/lib/safety/redact.js +0 -0
- package/lib/safety/rules.js +28 -0
- package/lib/scheduler.js +0 -0
- package/lib/source.js +0 -0
- package/lib/sqlite.js +2302 -55
- package/lib/sync.js +398 -71
- package/lib/text-safe.js +229 -0
- package/lib/timestamp.js +29 -9
- package/lib/tos/index.js +1 -1
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +12 -9
- package/lib/tos/safety.zh-CN.md +4 -3
- package/lib/tos/summary.zh-CN.md +3 -3
- package/lib/tos/terms.zh-CN.md +8 -6
- package/lib/tui/color.js +164 -0
- package/lib/tui/index.js +33 -0
- package/lib/tui/json.js +124 -0
- package/lib/tui/labels.js +50 -0
- package/lib/tui/policy.js +108 -0
- package/lib/tui/render.js +371 -0
- package/lib/tui/sanitize.js +142 -0
- package/lib/tui/text-file.js +116 -0
- package/lib/tui/width.js +577 -0
- package/lib/ui.js +0 -0
- package/lib/undo.js +0 -0
- package/lib/untrusted.js +186 -0
- package/lib/vault.js +0 -0
- package/package.json +6 -5
package/lib/receipt-verify.js
CHANGED
|
@@ -26,9 +26,18 @@
|
|
|
26
26
|
*
|
|
27
27
|
* The verifier deliberately does not trust manifest.json before SHA256SUMS
|
|
28
28
|
* matches: checksums are verified first, then the manifest schema is checked.
|
|
29
|
+
*
|
|
30
|
+
* Resource limits (0.3.0, 04-0.3-research §4.12): a receipt pack arrives from a
|
|
31
|
+
* counterparty, so every read is capped -- entries, directory depth, single-file
|
|
32
|
+
* size, total bytes, SHA256SUMS size and manifest size (RECEIPT_LIMIT_DEFAULTS).
|
|
33
|
+
* Hits are reported with the stable code E_RECEIPT_LIMIT plus structured
|
|
34
|
+
* metadata {limit, max, actual, file}; files are hashed in 1 MiB chunks instead
|
|
35
|
+
* of being loaded whole, and receipt.docx is scanned in chunks as well. Normal
|
|
36
|
+
* v1/v1.1 packs are far below every limit and are unaffected.
|
|
29
37
|
*/
|
|
30
38
|
|
|
31
|
-
import {
|
|
39
|
+
import { createHash } from 'node:crypto'
|
|
40
|
+
import { closeSync, existsSync, openSync, readFileSync, readdirSync, readSync, statSync } from 'node:fs'
|
|
32
41
|
import { join, resolve } from 'node:path'
|
|
33
42
|
import {
|
|
34
43
|
MAX_QUOTE_CHARS,
|
|
@@ -45,6 +54,56 @@ const MAX_ERRORS = 300
|
|
|
45
54
|
const SUMS_LINE_RE = /^([0-9a-f]{64}) (.+)$/
|
|
46
55
|
const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/
|
|
47
56
|
|
|
57
|
+
/** Stable error code for a receipt pack that exceeds a resource limit. */
|
|
58
|
+
export const RECEIPT_LIMIT_CODE = 'E_RECEIPT_LIMIT'
|
|
59
|
+
/**
|
|
60
|
+
* Resource limits for verifying an UNTRUSTED receipt pack (04-0.3-research
|
|
61
|
+
* §4.12). A receipt pack arrives from a counterparty: without caps, a 100k-file
|
|
62
|
+
* directory, a 100-level path nest, a multi-GiB file or a huge SHA256SUMS could
|
|
63
|
+
* exhaust memory/time before any integrity result is produced. Limits apply to
|
|
64
|
+
* both the directory walk and every read/hash, so a file growing during the
|
|
65
|
+
* check cannot bypass the stat-time cap. Normal packs (5 required files + a
|
|
66
|
+
* handful of deliveries) are orders of magnitude below every limit.
|
|
67
|
+
*/
|
|
68
|
+
export const RECEIPT_LIMIT_DEFAULTS = Object.freeze({
|
|
69
|
+
maxEntries: 5000,
|
|
70
|
+
maxDepth: 8,
|
|
71
|
+
maxFileBytes: 32 * 1024 * 1024,
|
|
72
|
+
maxTotalBytes: 256 * 1024 * 1024,
|
|
73
|
+
maxSumsBytes: 64 * 1024,
|
|
74
|
+
maxManifestBytes: 1024 * 1024,
|
|
75
|
+
})
|
|
76
|
+
const LIMIT_LABELS = Object.freeze({
|
|
77
|
+
maxEntries: 'entry count',
|
|
78
|
+
maxDepth: 'directory depth',
|
|
79
|
+
maxFileBytes: 'single file size',
|
|
80
|
+
maxTotalBytes: 'total pack size',
|
|
81
|
+
maxSumsBytes: 'SHA256SUMS size',
|
|
82
|
+
maxManifestBytes: 'manifest size',
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
function normalizeLimits(input) {
|
|
86
|
+
const limits = { ...RECEIPT_LIMIT_DEFAULTS }
|
|
87
|
+
if (!input || typeof input !== 'object') return limits
|
|
88
|
+
for (const key of Object.keys(RECEIPT_LIMIT_DEFAULTS)) {
|
|
89
|
+
if (!(key in input)) continue
|
|
90
|
+
const value = Number(input[key])
|
|
91
|
+
if (Number.isFinite(value) && value > 0) limits[key] = Math.floor(value)
|
|
92
|
+
}
|
|
93
|
+
return limits
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function limitMessage(violation) {
|
|
97
|
+
const label = LIMIT_LABELS[violation.limit] ?? violation.limit
|
|
98
|
+
return RECEIPT_LIMIT_CODE + ': receipt pack ' + label + ' exceeded the limit ' + violation.max +
|
|
99
|
+
' (actual ' + violation.actual + (violation.file ? ', first at ' + violation.file : '') + '); refusing to read a pack that could exhaust memory or time. ' +
|
|
100
|
+
'A legitimate pack is far below these limits; inspect the pack before retrying.'
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function makeViolation(limits, limit, actual, file) {
|
|
104
|
+
return { code: RECEIPT_LIMIT_CODE, limit, max: limits[limit], actual, file: file ?? null }
|
|
105
|
+
}
|
|
106
|
+
|
|
48
107
|
function addError(errors, message) {
|
|
49
108
|
if (errors.length < MAX_ERRORS) errors.push(message)
|
|
50
109
|
else if (errors.length === MAX_ERRORS) errors.push('... too many errors; further details suppressed')
|
|
@@ -112,12 +171,25 @@ export function parseReceiptSums(text) {
|
|
|
112
171
|
return { entries, errors }
|
|
113
172
|
}
|
|
114
173
|
|
|
115
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Walk the pack directory with hard caps on entries, depth, per-file size and
|
|
176
|
+
* total size. Stops at the first violation so a hostile pack (100k entries, a
|
|
177
|
+
* deep nest, huge files) is rejected in roughly O(limit) work instead of O(pack).
|
|
178
|
+
* Returns the violation as a structured object for stable CLI/JSON reporting.
|
|
179
|
+
*/
|
|
180
|
+
function walkPackFiles(root, limits) {
|
|
116
181
|
const files = []
|
|
117
182
|
const errors = []
|
|
118
|
-
const stack = ['']
|
|
119
|
-
|
|
120
|
-
|
|
183
|
+
const stack = [{ relativeDir: '', depth: 0 }]
|
|
184
|
+
let entryCount = 0
|
|
185
|
+
let totalBytes = 0
|
|
186
|
+
let violation = null
|
|
187
|
+
const trip = (limit, actual, file) => {
|
|
188
|
+
violation = makeViolation(limits, limit, actual, file)
|
|
189
|
+
addError(errors, limitMessage(violation))
|
|
190
|
+
}
|
|
191
|
+
while (stack.length && !violation) {
|
|
192
|
+
const { relativeDir, depth } = stack.pop()
|
|
121
193
|
const directory = relativeDir ? join(root, ...relativeDir.split('/')) : root
|
|
122
194
|
let dirents
|
|
123
195
|
try {
|
|
@@ -127,24 +199,97 @@ function walkPackFiles(root) {
|
|
|
127
199
|
continue
|
|
128
200
|
}
|
|
129
201
|
for (const dirent of dirents) {
|
|
202
|
+
entryCount += 1
|
|
130
203
|
const relativePath = relativeDir ? relativeDir + '/' + dirent.name : dirent.name
|
|
204
|
+
if (entryCount > limits.maxEntries) { trip('maxEntries', entryCount, relativePath); break }
|
|
131
205
|
if (dirent.isSymbolicLink()) {
|
|
132
206
|
addError(errors, 'symbolic links are not allowed in a receipt pack: ' + relativePath)
|
|
133
207
|
continue
|
|
134
208
|
}
|
|
135
209
|
if (dirent.isDirectory()) {
|
|
136
|
-
|
|
210
|
+
const childDepth = depth + 1
|
|
211
|
+
if (childDepth > limits.maxDepth) { trip('maxDepth', childDepth, relativePath); break }
|
|
212
|
+
stack.push({ relativeDir: relativePath, depth: childDepth })
|
|
137
213
|
continue
|
|
138
214
|
}
|
|
139
215
|
if (!dirent.isFile()) {
|
|
140
216
|
addError(errors, 'unsupported file type in receipt pack: ' + relativePath)
|
|
141
217
|
continue
|
|
142
218
|
}
|
|
219
|
+
let info
|
|
220
|
+
try {
|
|
221
|
+
info = statSync(join(root, ...relativePath.split('/')))
|
|
222
|
+
} catch (error) {
|
|
223
|
+
addError(errors, 'cannot stat ' + relativePath + ': ' + (error.code || error.message))
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
if (info.size > limits.maxFileBytes) { trip('maxFileBytes', info.size, relativePath); break }
|
|
227
|
+
totalBytes += info.size
|
|
228
|
+
if (totalBytes > limits.maxTotalBytes) { trip('maxTotalBytes', totalBytes, relativePath); break }
|
|
143
229
|
files.push(relativePath)
|
|
144
230
|
}
|
|
145
231
|
}
|
|
146
|
-
files.sort()
|
|
147
|
-
return { files, errors }
|
|
232
|
+
if (!violation) files.sort()
|
|
233
|
+
return { files, errors, violation, totalBytes, entryCount }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Streaming sha256 with a hard byte cap (1 MiB chunks, bounded memory).
|
|
238
|
+
* Returns {sha256, bytes} or {violation} when the file grew past maxBytes
|
|
239
|
+
* after the stat-time check (a file can be swapped/grown between stat and read).
|
|
240
|
+
*/
|
|
241
|
+
function hashFileWithLimit(file, limits, { chunkSize = 1024 * 1024 } = {}) {
|
|
242
|
+
const hash = createHash('sha256')
|
|
243
|
+
const buffer = Buffer.allocUnsafe(Math.min(chunkSize, 1024 * 1024))
|
|
244
|
+
let bytes = 0
|
|
245
|
+
const fd = openSync(file, 'r')
|
|
246
|
+
try {
|
|
247
|
+
for (;;) {
|
|
248
|
+
const count = readSync(fd, buffer, 0, buffer.length, null)
|
|
249
|
+
if (!count) break
|
|
250
|
+
bytes += count
|
|
251
|
+
if (bytes > limits.maxFileBytes) return { violation: makeViolation(limits, 'maxFileBytes', bytes, null) }
|
|
252
|
+
hash.update(buffer.subarray(0, count))
|
|
253
|
+
}
|
|
254
|
+
} finally {
|
|
255
|
+
try { closeSync(fd) } catch {}
|
|
256
|
+
}
|
|
257
|
+
return { sha256: hash.digest('hex'), bytes }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* receipt.docx structural smoke check without loading the file into memory:
|
|
262
|
+
* ZIP magic in the first bytes + the word/document.xml entry name somewhere in
|
|
263
|
+
* the archive. Never calls Office/WPS. Enforces the single-file byte cap while
|
|
264
|
+
* scanning, since the file may have grown after the walk.
|
|
265
|
+
*/
|
|
266
|
+
function checkDocxStructure(file, limits) {
|
|
267
|
+
const needle = Buffer.from('word/document.xml', 'utf8')
|
|
268
|
+
const chunkSize = 64 * 1024
|
|
269
|
+
const buffer = Buffer.allocUnsafe(chunkSize)
|
|
270
|
+
let position = 0
|
|
271
|
+
let bytes = 0
|
|
272
|
+
let zip = false
|
|
273
|
+
let hasDocument = false
|
|
274
|
+
let carry = Buffer.alloc(0)
|
|
275
|
+
const fd = openSync(file, 'r')
|
|
276
|
+
try {
|
|
277
|
+
for (;;) {
|
|
278
|
+
const count = readSync(fd, buffer, 0, buffer.length, position)
|
|
279
|
+
if (!count) break
|
|
280
|
+
position += count
|
|
281
|
+
bytes += count
|
|
282
|
+
if (bytes > limits.maxFileBytes) return { violation: makeViolation(limits, 'maxFileBytes', bytes, null) }
|
|
283
|
+
const chunk = buffer.subarray(0, count)
|
|
284
|
+
if (bytes === count && count >= 4) zip = chunk[0] === 0x50 && chunk[1] === 0x4b
|
|
285
|
+
const scan = carry.length ? Buffer.concat([carry, chunk]) : chunk
|
|
286
|
+
if (!hasDocument && scan.includes(needle)) hasDocument = true
|
|
287
|
+
carry = scan.subarray(Math.max(0, scan.length - (needle.length - 1)))
|
|
288
|
+
}
|
|
289
|
+
} finally {
|
|
290
|
+
try { closeSync(fd) } catch {}
|
|
291
|
+
}
|
|
292
|
+
return { zip, hasDocument, bytes }
|
|
148
293
|
}
|
|
149
294
|
|
|
150
295
|
// --- manifest schema (format v1; keep it in sync with buildReceiptPack) ------
|
|
@@ -489,17 +634,27 @@ function validateReceiptManifest(manifest, sums, errors, result) {
|
|
|
489
634
|
* Verify a receipt pack directory offline.
|
|
490
635
|
*
|
|
491
636
|
* @param {string} dir pack directory produced by buildReceiptPack({outDir})
|
|
637
|
+
* @param {{limits?: Partial<typeof RECEIPT_LIMIT_DEFAULTS>}} [options]
|
|
638
|
+
* `limits` lowers/raises the resource caps (tests and the future WebCrypto
|
|
639
|
+
* verification page use smaller values; the defaults are the contract).
|
|
492
640
|
* @returns {{ok: boolean, errors: string[], warnings: string[], dir: string,
|
|
493
641
|
* fileCount: number, checksumCount: number, files: string[],
|
|
494
642
|
* checksums: Record<string, string>, manifest: object|null,
|
|
495
|
-
* anchorsRecorded: number, anchorsVerified: boolean, anchored: boolean
|
|
643
|
+
* anchorsRecorded: number, anchorsVerified: boolean, anchored: boolean,
|
|
644
|
+
* limits: object, totalBytes: number, limitViolation: object|null,
|
|
645
|
+
* code: string|null}}
|
|
646
|
+
* `code` is 'E_RECEIPT_LIMIT' when a resource cap was hit (with metadata in
|
|
647
|
+
* limitViolation: {code, limit, max, actual, file}); ok is false in that case
|
|
648
|
+
* and no manifest/checksum result is trusted.
|
|
496
649
|
*/
|
|
497
|
-
export function verifyReceiptPackDir(dir) {
|
|
650
|
+
export function verifyReceiptPackDir(dir, options = {}) {
|
|
498
651
|
const errors = []
|
|
499
652
|
const warnings = []
|
|
500
653
|
const files = []
|
|
654
|
+
const limits = normalizeLimits(options?.limits)
|
|
501
655
|
const result = {
|
|
502
656
|
ok: false,
|
|
657
|
+
code: null,
|
|
503
658
|
errors,
|
|
504
659
|
warnings,
|
|
505
660
|
dir: String(dir),
|
|
@@ -513,6 +668,15 @@ export function verifyReceiptPackDir(dir) {
|
|
|
513
668
|
anchorsRecorded: 0,
|
|
514
669
|
anchorsVerified: false,
|
|
515
670
|
anchored: false,
|
|
671
|
+
limits: { ...limits },
|
|
672
|
+
totalBytes: 0,
|
|
673
|
+
limitViolation: null,
|
|
674
|
+
}
|
|
675
|
+
const failLimit = (violation) => {
|
|
676
|
+
result.code = RECEIPT_LIMIT_CODE
|
|
677
|
+
result.limitViolation = violation
|
|
678
|
+
result.ok = false
|
|
679
|
+
return result
|
|
516
680
|
}
|
|
517
681
|
let root
|
|
518
682
|
try {
|
|
@@ -538,10 +702,12 @@ export function verifyReceiptPackDir(dir) {
|
|
|
538
702
|
return result
|
|
539
703
|
}
|
|
540
704
|
|
|
541
|
-
const walked = walkPackFiles(root)
|
|
705
|
+
const walked = walkPackFiles(root, limits)
|
|
542
706
|
for (const error of walked.errors) addError(errors, error)
|
|
543
707
|
for (const file of walked.files) files.push(file)
|
|
544
708
|
result.fileCount = files.length
|
|
709
|
+
result.totalBytes = walked.totalBytes
|
|
710
|
+
if (walked.violation) return failLimit(walked.violation)
|
|
545
711
|
const fileSet = new Set(files)
|
|
546
712
|
|
|
547
713
|
const sumsPath = join(root, RECEIPT_SHA256SUMS_FILE)
|
|
@@ -551,6 +717,12 @@ export function verifyReceiptPackDir(dir) {
|
|
|
551
717
|
}
|
|
552
718
|
let sumsText
|
|
553
719
|
try {
|
|
720
|
+
const sumsStat = statSync(sumsPath)
|
|
721
|
+
if (sumsStat.size > limits.maxSumsBytes) {
|
|
722
|
+
const violation = makeViolation(limits, 'maxSumsBytes', sumsStat.size, RECEIPT_SHA256SUMS_FILE)
|
|
723
|
+
addError(errors, limitMessage(violation))
|
|
724
|
+
return failLimit(violation)
|
|
725
|
+
}
|
|
554
726
|
sumsText = readFileSync(sumsPath, 'utf8')
|
|
555
727
|
} catch (error) {
|
|
556
728
|
addError(errors, 'cannot read SHA256SUMS: ' + (error.code || error.message))
|
|
@@ -573,11 +745,24 @@ export function verifyReceiptPackDir(dir) {
|
|
|
573
745
|
if (!parsed.entries.has(name)) addError(errors, 'file not covered by SHA256SUMS: ' + name)
|
|
574
746
|
}
|
|
575
747
|
|
|
748
|
+
let hashedTotal = 0
|
|
576
749
|
for (const [name, expected] of parsed.entries) {
|
|
577
750
|
if (!fileSet.has(name)) continue
|
|
578
751
|
const full = join(root, ...name.split('/'))
|
|
579
752
|
try {
|
|
580
|
-
const
|
|
753
|
+
const hashed = hashFileWithLimit(full, limits)
|
|
754
|
+
if (hashed.violation) {
|
|
755
|
+
hashed.violation.file = name
|
|
756
|
+
addError(errors, limitMessage(hashed.violation))
|
|
757
|
+
return failLimit(hashed.violation)
|
|
758
|
+
}
|
|
759
|
+
hashedTotal += hashed.bytes
|
|
760
|
+
if (hashedTotal > limits.maxTotalBytes) {
|
|
761
|
+
const violation = makeViolation(limits, 'maxTotalBytes', hashedTotal, name)
|
|
762
|
+
addError(errors, limitMessage(violation))
|
|
763
|
+
return failLimit(violation)
|
|
764
|
+
}
|
|
765
|
+
const actual = hashed.sha256
|
|
581
766
|
if (actual !== expected) {
|
|
582
767
|
addError(errors, 'hash mismatch: ' + name + ' (expected ' + expected.slice(0, 12) + '..., computed ' + actual.slice(0, 12) + '...)')
|
|
583
768
|
}
|
|
@@ -587,12 +772,19 @@ export function verifyReceiptPackDir(dir) {
|
|
|
587
772
|
}
|
|
588
773
|
|
|
589
774
|
// receipt.docx structural smoke check (never call Office/WPS to verify it).
|
|
775
|
+
// Streamed with a byte cap: an untrusted pack must not be able to make the
|
|
776
|
+
// verifier allocate a multi-GiB Buffer before reporting a limit error.
|
|
590
777
|
if (fileSet.has('receipt.docx')) {
|
|
591
778
|
try {
|
|
592
|
-
const
|
|
593
|
-
if (
|
|
779
|
+
const docx = checkDocxStructure(join(root, 'receipt.docx'), limits)
|
|
780
|
+
if (docx.violation) {
|
|
781
|
+
docx.violation.file = 'receipt.docx'
|
|
782
|
+
addError(errors, limitMessage(docx.violation))
|
|
783
|
+
return failLimit(docx.violation)
|
|
784
|
+
}
|
|
785
|
+
if (!docx.zip) {
|
|
594
786
|
addError(errors, 'receipt.docx is not a ZIP/DOCX file')
|
|
595
|
-
} else if (!
|
|
787
|
+
} else if (!docx.hasDocument) {
|
|
596
788
|
addError(errors, 'receipt.docx is missing word/document.xml')
|
|
597
789
|
}
|
|
598
790
|
} catch (error) {
|
|
@@ -603,6 +795,12 @@ export function verifyReceiptPackDir(dir) {
|
|
|
603
795
|
if (!fileSet.has('manifest.json')) return result
|
|
604
796
|
let manifest = null
|
|
605
797
|
try {
|
|
798
|
+
const manifestStat = statSync(join(root, 'manifest.json'))
|
|
799
|
+
if (manifestStat.size > limits.maxManifestBytes) {
|
|
800
|
+
const violation = makeViolation(limits, 'maxManifestBytes', manifestStat.size, 'manifest.json')
|
|
801
|
+
addError(errors, limitMessage(violation))
|
|
802
|
+
return failLimit(violation)
|
|
803
|
+
}
|
|
606
804
|
manifest = JSON.parse(readFileSync(join(root, 'manifest.json'), 'utf8'))
|
|
607
805
|
} catch (error) {
|
|
608
806
|
addError(errors, 'manifest.json is not valid JSON: ' + (error.message || error))
|
package/lib/relative-date.js
CHANGED
|
File without changes
|
package/lib/remind.js
CHANGED
|
@@ -19,12 +19,6 @@ export const DEFAULT_HORIZON_DAYS = 7
|
|
|
19
19
|
const BUCKETS = new Set(['overdue', 'due-today', 'due-1d', 'upcoming'])
|
|
20
20
|
const LEVELS = new Set(['L1', 'L2', 'L3'])
|
|
21
21
|
|
|
22
|
-
const DAY_FORMAT = new Intl.DateTimeFormat('en-US', {
|
|
23
|
-
timeZone: TIME_ZONE,
|
|
24
|
-
year: 'numeric',
|
|
25
|
-
month: '2-digit',
|
|
26
|
-
day: '2-digit',
|
|
27
|
-
})
|
|
28
22
|
const TIME_FORMAT = new Intl.DateTimeFormat('en-US', {
|
|
29
23
|
timeZone: TIME_ZONE,
|
|
30
24
|
hour: '2-digit',
|
|
@@ -88,18 +82,34 @@ function requireNow(value) {
|
|
|
88
82
|
return requireDate(value, 'now')
|
|
89
83
|
}
|
|
90
84
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Local calendar day in Asia/Shanghai, as `YYYY-MM-DD`.
|
|
87
|
+
*
|
|
88
|
+
* 0.3.0 P0-T2: computed with UTC+8 arithmetic instead of
|
|
89
|
+
* Intl.DateTimeFormat.formatToParts. Shanghai has been a fixed UTC+8 offset
|
|
90
|
+
* without DST since 1991, so the two agree for every date the ledger stores;
|
|
91
|
+
* the arithmetic form is ~20x faster, which matters because the reminder and
|
|
92
|
+
* pet-brief views resolve one local day per due row (66k rows at a 200k
|
|
93
|
+
* ledger).
|
|
94
|
+
*/
|
|
98
95
|
export function localDay(value) {
|
|
99
96
|
const date = toDate(value)
|
|
100
97
|
if (!date) return null
|
|
101
|
-
|
|
102
|
-
|
|
98
|
+
return localDayOf(date)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Local calendar day string for an already-parsed instant. */
|
|
102
|
+
function localDayOf(date) {
|
|
103
|
+
const wall = new Date(date.getTime() + SHANGHAI_OFFSET_MS)
|
|
104
|
+
const year = wall.getUTCFullYear()
|
|
105
|
+
const month = wall.getUTCMonth() + 1
|
|
106
|
+
const day = wall.getUTCDate()
|
|
107
|
+
return `${String(year).padStart(4, '0')}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Day-of-epoch number of the Asia/Shanghai calendar day for an instant. */
|
|
111
|
+
function localDayIndexFromMs(ms) {
|
|
112
|
+
return Math.floor((ms + SHANGHAI_OFFSET_MS) / DAY_MS)
|
|
103
113
|
}
|
|
104
114
|
|
|
105
115
|
/** Minutes since local midnight in Asia/Shanghai (0–1439). */
|
|
@@ -260,6 +270,9 @@ function resolveReminderOffsets(value) {
|
|
|
260
270
|
return resolved
|
|
261
271
|
}
|
|
262
272
|
|
|
273
|
+
/** Shared empty result for the reminder-planning fast path (never mutated). */
|
|
274
|
+
const EMPTY_REMINDER_OFFSETS = Object.freeze([])
|
|
275
|
+
|
|
263
276
|
/** Human label used inside a VALARM DESCRIPTION. */
|
|
264
277
|
export function describeReminderOffset(value) {
|
|
265
278
|
const parsed = parseReminderOffset(value)
|
|
@@ -527,23 +540,61 @@ export function buildReminders(ledger, options = {}) {
|
|
|
527
540
|
const seen = new Set()
|
|
528
541
|
const reminders = []
|
|
529
542
|
|
|
543
|
+
// 0.3.0 P0-T2: per-call caches. Reminder planning needs one parsed due
|
|
544
|
+
// instant and one local day per row, but a real ledger has only a few
|
|
545
|
+
// hundred distinct dueAt strings at 200k rows; caching the parse turns ~60k
|
|
546
|
+
// regex + Date.parse + Intl lookups into a few hundred and keeps remind/tui
|
|
547
|
+
// inside the 0.3.0 read budget.
|
|
548
|
+
const dueCache = new Map()
|
|
549
|
+
const dueInfo = (value) => {
|
|
550
|
+
const key = typeof value === 'string' ? value : value instanceof Date ? `date:${value.getTime()}` : `ms:${String(value)}`
|
|
551
|
+
let info = dueCache.get(key)
|
|
552
|
+
if (info === undefined) {
|
|
553
|
+
const date = toDate(value)
|
|
554
|
+
info = date ? { date, dayIndex: localDayIndexFromMs(date.getTime()), dueDay: localDayOf(date) } : null
|
|
555
|
+
dueCache.set(key, info)
|
|
556
|
+
}
|
|
557
|
+
return info
|
|
558
|
+
}
|
|
559
|
+
// readSnoozeUntil()/readReminderOffsets() can only find state that was
|
|
560
|
+
// injected, set on the row, or exposed by a duck-typed ledger hook. The
|
|
561
|
+
// common CLI path has none of that, so skip the per-row lookup instead of
|
|
562
|
+
// allocating a candidate array for every commitment.
|
|
563
|
+
const injectedSnooze = opts.snoozedUntil
|
|
564
|
+
const snoozeStatePossible = typeof injectedSnooze === 'function' ||
|
|
565
|
+
(injectedSnooze instanceof Map && injectedSnooze.size > 0) ||
|
|
566
|
+
(Boolean(injectedSnooze) && typeof injectedSnooze === 'object' && !(injectedSnooze instanceof Map) && Object.keys(injectedSnooze).length > 0) ||
|
|
567
|
+
typeof ledger.getMeta === 'function'
|
|
568
|
+
const injectedOffsets = opts.reminderOffsets
|
|
569
|
+
const offsetStatePossible = typeof injectedOffsets === 'function' ||
|
|
570
|
+
(injectedOffsets instanceof Map && injectedOffsets.size > 0) ||
|
|
571
|
+
(Boolean(injectedOffsets) && typeof injectedOffsets === 'object' && !(injectedOffsets instanceof Map) && Object.keys(injectedOffsets).length > 0) ||
|
|
572
|
+
opts.defaultReminderOffsets !== undefined ||
|
|
573
|
+
typeof ledger.getMeta === 'function'
|
|
574
|
+
|
|
530
575
|
for (const commitment of commitments) {
|
|
531
576
|
if (!commitment || typeof commitment !== 'object') continue
|
|
532
577
|
if (!commitment.id) continue
|
|
533
578
|
if (commitment.status === 'done' || commitment.status === 'dropped') continue
|
|
534
579
|
if (commitment.dueAt == null || commitment.dueAt === '') continue
|
|
535
|
-
const
|
|
536
|
-
if (!
|
|
537
|
-
|
|
538
|
-
|
|
580
|
+
const info = dueInfo(commitment.dueAt)
|
|
581
|
+
if (!info) continue
|
|
582
|
+
const due = info.date
|
|
583
|
+
|
|
584
|
+
const snoozeUntil = snoozeStatePossible || commitment.snoozedUntil !== undefined || commitment.snoozeUntil !== undefined ||
|
|
585
|
+
commitment.snoozeAt !== undefined || commitment.meta !== undefined
|
|
586
|
+
? readSnoozeUntil(commitment, opts, ledger)
|
|
587
|
+
: null
|
|
539
588
|
// An active snooze hides the commitment; once it expires the commitment
|
|
540
589
|
// becomes visible again and the snooze-wake rule fires.
|
|
541
590
|
if (snoozeUntil && snoozeUntil.getTime() > now.getTime()) continue
|
|
542
591
|
const snoozeExpired = Boolean(snoozeUntil)
|
|
543
|
-
const reminderOffsets =
|
|
592
|
+
const reminderOffsets = offsetStatePossible || commitment.reminderOffsets !== undefined || commitment.meta !== undefined
|
|
593
|
+
? readReminderOffsets(commitment, opts, ledger)
|
|
594
|
+
: EMPTY_REMINDER_OFFSETS
|
|
544
595
|
|
|
545
|
-
const dueDay =
|
|
546
|
-
const dayOffset =
|
|
596
|
+
const dueDay = info.dueDay
|
|
597
|
+
const dayOffset = info.dayIndex - todayIndex
|
|
547
598
|
if (dayOffset > horizonDays) continue
|
|
548
599
|
|
|
549
600
|
const bucket = bucketFor(dayOffset)
|
|
@@ -569,27 +620,31 @@ export function buildReminders(ledger, options = {}) {
|
|
|
569
620
|
if (seen.has(key)) continue
|
|
570
621
|
seen.add(key)
|
|
571
622
|
reminders.push({
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
623
|
+
// Kept out of the output object: the final sort needs the instant,
|
|
624
|
+
// and toDate() per comparator call was the 0.2.x sort's hot spot.
|
|
625
|
+
dueMs: due.getTime(),
|
|
626
|
+
reminder: {
|
|
627
|
+
commitmentId: commitment.id,
|
|
628
|
+
action: commitment.action ?? '',
|
|
629
|
+
dueAt: outputDueAt(commitment.dueAt),
|
|
630
|
+
bucket: result.bucket,
|
|
631
|
+
level: result.level,
|
|
632
|
+
ruleId: result.ruleId,
|
|
633
|
+
// Only present when customised: pre-0.2.0 reminder objects keep their
|
|
634
|
+
// exact shape and toIcs falls back to the legacy relative alarms.
|
|
635
|
+
...(reminderOffsets.length ? { reminderOffsets } : {}),
|
|
636
|
+
},
|
|
581
637
|
})
|
|
582
638
|
}
|
|
583
639
|
}
|
|
584
640
|
|
|
585
|
-
const dueTime = (reminder) => toDate(reminder.dueAt)?.getTime() ?? 0
|
|
586
641
|
reminders.sort(
|
|
587
642
|
(left, right) =>
|
|
588
|
-
|
|
589
|
-
left.commitmentId.localeCompare(right.commitmentId) ||
|
|
590
|
-
left.ruleId.localeCompare(right.ruleId),
|
|
643
|
+
left.dueMs - right.dueMs ||
|
|
644
|
+
left.reminder.commitmentId.localeCompare(right.reminder.commitmentId) ||
|
|
645
|
+
left.reminder.ruleId.localeCompare(right.reminder.ruleId),
|
|
591
646
|
)
|
|
592
|
-
return reminders
|
|
647
|
+
return reminders.map((entry) => entry.reminder)
|
|
593
648
|
}
|
|
594
649
|
|
|
595
650
|
/**
|
package/lib/research.js
CHANGED
|
File without changes
|
package/lib/review.js
CHANGED
|
File without changes
|
package/lib/routine.js
CHANGED
|
File without changes
|
package/lib/safety/classifier.js
CHANGED
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
* crisis: boolean, // self-harm: show crisis hint, never block
|
|
23
23
|
* downgraded: boolean,
|
|
24
24
|
* truncated: boolean, // true when only the first maxChars were scanned
|
|
25
|
+
* injection?: { // additive prompt-injection view (warn only)
|
|
26
|
+
* detected, highConfidence, severity, code, reason, subtype, confidence,
|
|
27
|
+
* ruleIds, action, surface
|
|
28
|
+
* }
|
|
25
29
|
* hits: [{ ruleId, category, subtype, code, severity, action, confidence,
|
|
26
30
|
* hardBlock, crisis, downgraded }]
|
|
27
31
|
* }
|
|
@@ -38,7 +42,7 @@
|
|
|
38
42
|
*/
|
|
39
43
|
|
|
40
44
|
import { createHmac } from 'node:crypto'
|
|
41
|
-
import { DEFAULT_RULESET, actionRank, resolveRuleset } from './rules.js'
|
|
45
|
+
import { DEFAULT_RULESET, INJECTION_HIGH_CONFIDENCE, INJECTION_REASONS, actionRank, resolveRuleset } from './rules.js'
|
|
42
46
|
import { detectPiiInternal } from './redact.js'
|
|
43
47
|
|
|
44
48
|
export const DEFAULT_SURFACE = 'chat.input'
|
|
@@ -220,6 +224,7 @@ function buildHit(rule, snippet, actionOverride) {
|
|
|
220
224
|
blockable: Boolean(rule.blockable),
|
|
221
225
|
downgraded: null,
|
|
222
226
|
snippet: snippet || rule.id,
|
|
227
|
+
reason: rule.category === 'injection' ? (INJECTION_REASONS[rule.subtype] || INJECTION_REASONS.default) : null,
|
|
223
228
|
}
|
|
224
229
|
}
|
|
225
230
|
|
|
@@ -384,9 +389,28 @@ function runClassify(ruleset, text, options = {}) {
|
|
|
384
389
|
hardBlock: hit.hardBlock,
|
|
385
390
|
crisis: hit.crisis,
|
|
386
391
|
downgraded: hit.downgraded,
|
|
392
|
+
...(hit.reason ? { reason: hit.reason } : {}),
|
|
387
393
|
})),
|
|
388
394
|
}
|
|
389
|
-
|
|
395
|
+
const final = applyTruncationFailClosed(result, policy)
|
|
396
|
+
const injectionHits = ordered.filter((hit) => hit.category === 'injection')
|
|
397
|
+
if (injectionHits.length) {
|
|
398
|
+
const maxConfidence = Math.max(...injectionHits.map((hit) => hit.confidence))
|
|
399
|
+
const primary = injectionHits[0]
|
|
400
|
+
final.injection = {
|
|
401
|
+
detected: true,
|
|
402
|
+
highConfidence: maxConfidence >= INJECTION_HIGH_CONFIDENCE,
|
|
403
|
+
severity: primary.severity,
|
|
404
|
+
code: primary.code,
|
|
405
|
+
reason: primary.reason || INJECTION_REASONS.default,
|
|
406
|
+
subtype: primary.subtype,
|
|
407
|
+
confidence: Math.round(maxConfidence * 100) / 100,
|
|
408
|
+
ruleIds: [...new Set(injectionHits.map((hit) => hit.ruleId))],
|
|
409
|
+
action: final.action,
|
|
410
|
+
surface,
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return final
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
/**
|
|
@@ -425,6 +449,35 @@ export function classify(text, options = {}) {
|
|
|
425
449
|
}
|
|
426
450
|
}
|
|
427
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Structured injection view of any classify() result (allow results included).
|
|
454
|
+
* The rule action stays warn; this view is what policy callers (lib/agent.js)
|
|
455
|
+
* use to block/confirm a model call that would carry untrusted instructions.
|
|
456
|
+
* It never contains the matched text.
|
|
457
|
+
*/
|
|
458
|
+
export function injectionAssessment(result) {
|
|
459
|
+
const injection = result && result.injection && result.injection.detected ? result.injection : null
|
|
460
|
+
return {
|
|
461
|
+
detected: Boolean(injection),
|
|
462
|
+
highConfidence: Boolean(injection && injection.highConfidence),
|
|
463
|
+
severity: injection ? injection.severity : 'none',
|
|
464
|
+
code: injection ? injection.code : 'NONE',
|
|
465
|
+
reason: injection ? injection.reason : null,
|
|
466
|
+
subtype: injection ? injection.subtype : null,
|
|
467
|
+
confidence: injection ? injection.confidence : 0,
|
|
468
|
+
ruleIds: injection && Array.isArray(injection.ruleIds) ? [...injection.ruleIds] : [],
|
|
469
|
+
action: result ? result.action : 'allow',
|
|
470
|
+
surface: result ? result.surface : 'unknown',
|
|
471
|
+
truncated: Boolean(result && result.truncated),
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** classify() + injectionAssessment() in one call. */
|
|
476
|
+
export function inspectInjection(text, options = {}) {
|
|
477
|
+
return injectionAssessment(classify(text, options))
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export { INJECTION_HIGH_CONFIDENCE }
|
|
428
481
|
/** Bind a ruleset (and default options such as hmacKey) to a classify function. */
|
|
429
482
|
export function createClassifier(config = {}) {
|
|
430
483
|
let ruleset = DEFAULT_RULESET
|
package/lib/safety/explain.js
CHANGED
|
File without changes
|
package/lib/safety/index.js
CHANGED
|
File without changes
|
package/lib/safety/redact.js
CHANGED
|
File without changes
|
package/lib/safety/rules.js
CHANGED
|
@@ -24,6 +24,34 @@ export const RULESET_VERSION = 'advisers-content-1'
|
|
|
24
24
|
/** Actions a rule/result may carry. `allow` is only ever a classifier result. */
|
|
25
25
|
export const ACTIONS = Object.freeze(['allow', 'warn', 'confirm', 'redact', 'block'])
|
|
26
26
|
export const SEVERITIES = Object.freeze(['low', 'medium', 'high', 'critical'])
|
|
27
|
+
/**
|
|
28
|
+
* Structured prompt-injection metadata (engineering risk, never block).
|
|
29
|
+
*
|
|
30
|
+
* The injection category is a local engineering warning, not a legal
|
|
31
|
+
* violation: classify() keeps action=warn for it and callers decide how to
|
|
32
|
+
* contain the content (see lib/agent.js, which blocks/confirms a model call
|
|
33
|
+
* when untrusted data contains a high-confidence instruction override).
|
|
34
|
+
* The reason strings are safe to print: they never contain the matched text.
|
|
35
|
+
*/
|
|
36
|
+
export const INJECTION_REASONS = Object.freeze({
|
|
37
|
+
instruction_override: '内容试图忽略或覆盖系统/安全指令',
|
|
38
|
+
instruction_override_en: 'content tries to override system instructions',
|
|
39
|
+
prompt_leak: '内容试图获取系统提示或隐藏指令',
|
|
40
|
+
jailbreak_role: '内容试图切换为越狱/无限制角色',
|
|
41
|
+
secret_exfiltration: '内容试图诱导输出密钥或凭据',
|
|
42
|
+
tool_output_injection: '内容声称工具输出/检索结果应被当作指令',
|
|
43
|
+
safety_bypass: '内容试图关闭或绕过安全规则',
|
|
44
|
+
privileged_command: '内容试图诱导越权命令或删除审计/系统数据',
|
|
45
|
+
unrestricted_persona: '内容试图塑造不受限制的人设',
|
|
46
|
+
default: '内容包含提示注入或越权指令特征',
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Hit confidence at or above this is "high confidence" instruction
|
|
51
|
+
* injection for the agent's pre-model gate (block/confirm). Deliberately a
|
|
52
|
+
* named constant so the policy is auditable and testable.
|
|
53
|
+
*/
|
|
54
|
+
export const INJECTION_HIGH_CONFIDENCE = 0.8
|
|
27
55
|
|
|
28
56
|
/**
|
|
29
57
|
* Categories that are allowed to carry a block rule at all. Inside these
|
package/lib/scheduler.js
CHANGED
|
File without changes
|
package/lib/source.js
CHANGED
|
File without changes
|