advisers 0.2.3 → 0.3.1

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.
Files changed (70) hide show
  1. package/LICENSE +0 -0
  2. package/NOTICE +0 -0
  3. package/README-zh.md +15 -6
  4. package/README.md +22 -6
  5. package/THIRD-PARTY-NOTICES.md +1 -1
  6. package/bin/advisers.js +3323 -222
  7. package/index.js +1 -1
  8. package/lib/agent.js +0 -0
  9. package/lib/attachments.js +0 -0
  10. package/lib/audit-anchor.js +683 -0
  11. package/lib/backup.js +1345 -0
  12. package/lib/capture.js +0 -0
  13. package/lib/context-window.js +0 -0
  14. package/lib/digest.js +0 -0
  15. package/lib/doc-review.js +0 -0
  16. package/lib/egress.js +0 -0
  17. package/lib/error-codes.js +1373 -0
  18. package/lib/evidence.js +202 -26
  19. package/lib/fs-safe.js +70 -7
  20. package/lib/ical.js +0 -0
  21. package/lib/imap.js +6 -2
  22. package/lib/importers.js +0 -0
  23. package/lib/learning.js +0 -0
  24. package/lib/ledger-read.js +608 -0
  25. package/lib/mcp-server.js +294 -38
  26. package/lib/model.js +0 -0
  27. package/lib/node-warning-filter.js +15 -0
  28. package/lib/notify.js +13 -3
  29. package/lib/nudge.js +0 -0
  30. package/lib/ocr.js +0 -0
  31. package/lib/pdf.js +0 -0
  32. package/lib/pet-brief-store.js +0 -0
  33. package/lib/pet-brief.js +102 -39
  34. package/lib/receipt-verify.js +213 -15
  35. package/lib/relative-date.js +0 -0
  36. package/lib/remind.js +91 -36
  37. package/lib/research.js +0 -0
  38. package/lib/review.js +0 -0
  39. package/lib/routine.js +0 -0
  40. package/lib/safety/classifier.js +0 -0
  41. package/lib/safety/explain.js +0 -0
  42. package/lib/safety/index.js +0 -0
  43. package/lib/safety/redact.js +0 -0
  44. package/lib/safety/rules.js +0 -0
  45. package/lib/scheduler.js +0 -0
  46. package/lib/source.js +0 -0
  47. package/lib/sqlite.js +1214 -24
  48. package/lib/sync.js +0 -0
  49. package/lib/text-safe.js +229 -0
  50. package/lib/timestamp.js +0 -0
  51. package/lib/tos/index.js +1 -1
  52. package/lib/tos/manifest.json +6 -6
  53. package/lib/tos/privacy.zh-CN.md +12 -9
  54. package/lib/tos/safety.zh-CN.md +4 -3
  55. package/lib/tos/summary.zh-CN.md +3 -3
  56. package/lib/tos/terms.zh-CN.md +8 -6
  57. package/lib/tui/color.js +164 -0
  58. package/lib/tui/index.js +33 -0
  59. package/lib/tui/json.js +124 -0
  60. package/lib/tui/labels.js +50 -0
  61. package/lib/tui/policy.js +108 -0
  62. package/lib/tui/render.js +371 -0
  63. package/lib/tui/sanitize.js +142 -0
  64. package/lib/tui/text-file.js +116 -0
  65. package/lib/tui/width.js +577 -0
  66. package/lib/ui.js +0 -0
  67. package/lib/undo.js +0 -0
  68. package/lib/untrusted.js +0 -0
  69. package/lib/vault.js +0 -0
  70. package/package.json +6 -5
package/lib/evidence.js CHANGED
@@ -19,8 +19,9 @@
19
19
  * directory offline.
20
20
  *
21
21
  * Constraints baked into the design:
22
- * - Offline: no network calls; imports only node:crypto/node:fs/node:path (plus
23
- * an optional in-process `advisers-office` build for the receipt DOCX).
22
+ * - Offline: no network calls; imports only node:crypto/node:fs/node:path, the
23
+ * pure sibling `./egress.js` secrecy scale, and an optional in-process
24
+ * `advisers-office` build for the receipt DOCX.
24
25
  * - No fabrication: an unknown turn, a session mismatch or an unparseable
25
26
  * `session#turn` ref goes to `missing`; nothing is guessed.
26
27
  * - No leaks: no absolute filesystem paths, no full turn/transcript bodies and
@@ -49,6 +50,8 @@ import {
49
50
  } from 'node:fs'
50
51
  import { dirname, join, resolve } from 'node:path'
51
52
 
53
+ import { secrecyLevel } from './egress.js'
54
+
52
55
  /** Quotes are verbatim evidence but capped, so a pack never carries a full body. */
53
56
  export const MAX_QUOTE_CHARS = 500
54
57
  export const MANIFEST_FORMAT = 'advisers-evidence-pack'
@@ -80,6 +83,23 @@ export class EvidencePackLimitError extends Error {
80
83
  }
81
84
  }
82
85
 
86
+ /**
87
+ * Raised (instead of silently omitting or leaking) when the caller explicitly
88
+ * selected evidence that belongs to a protected matter (matter.secrecy above the
89
+ * requested clearance). The message/count never contain a title, creditor,
90
+ * action or quote; only the number of blocked ids.
91
+ */
92
+ export class ProtectedEvidenceError extends Error {
93
+ constructor(count, clearance = 0) {
94
+ super(`refusing to export ${count} evidence item(s): their matter secrecy is above the clearance ${clearance}`)
95
+ this.name = 'ProtectedEvidenceError'
96
+ this.code = 'E_SECRECY_BLOCKED'
97
+ this.count = count
98
+ this.clearance = clearance
99
+ this.hint = 'Remove the protected ids from --ids, or lower the matter secrecy with a local command. Protected matters are excluded from packs by default.'
100
+ }
101
+ }
102
+
83
103
  /** Header for the optional DOCX table helper, kept next to the rows it describes. */
84
104
  export const DOCX_EVIDENCE_HEADERS = ['类型', '证据ID', '来源(session#turn)', '摘录', '状态']
85
105
 
@@ -246,6 +266,70 @@ function lookupTurn(store, sessionId, turnId) {
246
266
  return { turn: undefined, failed }
247
267
  }
248
268
 
269
+ // ---------------------------------------------------------------------------
270
+ // per-item secrecy (A-1 fix): a pack/read model may span many matters, so the
271
+ // clearance decision is made for every included record, never only for the
272
+ // active matter.
273
+ // ---------------------------------------------------------------------------
274
+
275
+ /**
276
+ * Matter id an evidence record belongs to.
277
+ *
278
+ * A commitment always carries `matterId`; a fact may (otherwise it is a global
279
+ * fact); a preference is matter-scoped only through
280
+ * `scope: { level: 'matter', value: <matterId> }`.
281
+ *
282
+ * @returns {string|undefined}
283
+ */
284
+ export function evidenceRecordMatterId(record, kind) {
285
+ if (!record || typeof record !== 'object' || Array.isArray(record)) return undefined
286
+ if (kind === 'preference') {
287
+ const scope = record.scope
288
+ if (!scope || typeof scope !== 'object' || Array.isArray(scope)) return undefined
289
+ if (scope.level !== 'matter') return undefined
290
+ return typeof scope.value === 'string' && scope.value ? scope.value : undefined
291
+ }
292
+ return typeof record.matterId === 'string' && record.matterId ? record.matterId : undefined
293
+ }
294
+
295
+ /**
296
+ * Secrecy level (0–3, same scale as `lib/egress.js`) of the matter a record
297
+ * belongs to.
298
+ *
299
+ * - no matter association -> 0 (a global record is not matter-protected);
300
+ * - a matter id the ledger cannot resolve -> Infinity, i.e. treated as
301
+ * protected. A clearance gate must fail closed; it never guesses a level for
302
+ * a row it cannot see.
303
+ *
304
+ * @returns {number} 0..3 or Infinity
305
+ */
306
+ export function evidenceRecordSecrecy(ledger, kind, record) {
307
+ const matterId = evidenceRecordMatterId(record, kind)
308
+ if (!matterId) return 0
309
+ let matter
310
+ try {
311
+ if (typeof ledger?.getMatter === 'function') matter = ledger.getMatter(matterId)
312
+ } catch {
313
+ matter = undefined
314
+ }
315
+ if (matter === undefined) {
316
+ try {
317
+ if (typeof ledger?.listMatters === 'function') matter = ledger.listMatters().find((row) => row?.id === matterId)
318
+ } catch {
319
+ matter = undefined
320
+ }
321
+ }
322
+ if (!matter) return Number.POSITIVE_INFINITY
323
+ return secrecyLevel(matter)
324
+ }
325
+
326
+ /** True when a record must stay out of an export/projection at `clearance`. */
327
+ export function isEvidenceRecordAboveClearance(ledger, kind, record, clearance = 0) {
328
+ const limit = Number(clearance)
329
+ if (!Number.isFinite(limit) || limit < 0) return true
330
+ return evidenceRecordSecrecy(ledger, kind, record) > limit
331
+ }
332
+
249
333
  // ---------------------------------------------------------------------------
250
334
  // ledger lookups
251
335
  // ---------------------------------------------------------------------------
@@ -399,6 +483,56 @@ function collectSourceRefs(...lists) {
399
483
  return out
400
484
  }
401
485
 
486
+ /** Writer sentinel for "this record came from a turn without a session id". */
487
+ const LEGACY_SESSION_ID = 'legacy'
488
+
489
+ /**
490
+ * A quote is verbatim evidence: it must occur in the turn body it claims to
491
+ * come from. NFC normalization only removes encoding differences; it never
492
+ * repairs, rewrites or fuzzy-matches a quote (no case/whitespace folding).
493
+ * An empty quote is the pre-0.2.3 "pointer only" shape and keeps its old
494
+ * semantics (the turn is still verified, the blank quote is not).
495
+ */
496
+ function quoteMatchesTurn(turn, quote) {
497
+ if (typeof quote !== 'string' || quote === '') return true
498
+ const content = typeof turn?.content === 'string' ? turn.content : ''
499
+ return content.normalize('NFC').includes(quote.normalize('NFC'))
500
+ }
501
+
502
+ /**
503
+ * Normalize the two known-bad pointer shapes writers produced before 0.2.5:
504
+ * - `sessionId === 'legacy'` (memory accept for a candidate with no session);
505
+ * - `sessionId === turnId` (lib/agent.js commitment_add passed the turn id as
506
+ * the session id).
507
+ * The turn store is authoritative for conversationId, so these resolve instead
508
+ * of reporting session_mismatch forever. Any other mismatch stays a mismatch:
509
+ * a real evidence pointer must name its own conversation.
510
+ *
511
+ * @returns {string|null} normalized session id, or null on a real mismatch
512
+ */
513
+ function normalizePointerSession(sessionId, turnId, turn) {
514
+ if (!turn || typeof turn.conversationId !== 'string') return sessionId
515
+ if (turn.conversationId === sessionId) return sessionId
516
+ const knownBadShape = sessionId === LEGACY_SESSION_ID || sessionId === turnId
517
+ return knownBadShape ? turn.conversationId : null
518
+ }
519
+
520
+ /** Resolve one pointer after the turn/quote checks; shared by both resolvers. */
521
+ function resolvePointerTurn(store, sessionId, turnId, quote) {
522
+ const lookup = lookupTurn(store, sessionId, turnId)
523
+ if (!lookup.turn) {
524
+ return { missing: { sessionId, turnId, quote, reason: lookup.failed ? 'lookup_failed' : 'turn_not_found' } }
525
+ }
526
+ const normalizedSessionId = normalizePointerSession(sessionId, turnId, lookup.turn)
527
+ if (normalizedSessionId === null) {
528
+ return { missing: { sessionId, turnId, quote, reason: 'session_mismatch' } }
529
+ }
530
+ if (!quoteMatchesTurn(lookup.turn, quote)) {
531
+ return { missing: { sessionId: normalizedSessionId, turnId, quote, reason: 'quote_mismatch' } }
532
+ }
533
+ return { source: { sessionId: normalizedSessionId, turnId, quote, turn: lookup.turn } }
534
+ }
535
+
402
536
  function resolveSourceRefs(store, items) {
403
537
  const sources = []
404
538
  const missing = []
@@ -423,22 +557,12 @@ function resolveSourceRefs(store, items) {
423
557
  missing.push({ ref, quote, reason: 'unparseable_ref' })
424
558
  continue
425
559
  }
426
- const lookup = lookupTurn(store, parsed.sessionId, parsed.turnId)
427
- if (!lookup.turn) {
428
- missing.push({
429
- ref,
430
- quote,
431
- sessionId: parsed.sessionId,
432
- turnId: parsed.turnId,
433
- reason: lookup.failed ? 'lookup_failed' : 'turn_not_found',
434
- })
435
- continue
436
- }
437
- if (lookup.turn.conversationId !== parsed.sessionId) {
438
- missing.push({ ref, quote, sessionId: parsed.sessionId, turnId: parsed.turnId, reason: 'session_mismatch' })
560
+ const resolved = resolvePointerTurn(store, parsed.sessionId, parsed.turnId, quote)
561
+ if (resolved.missing) {
562
+ missing.push({ ref, ...resolved.missing })
439
563
  continue
440
564
  }
441
- sources.push({ sessionId: parsed.sessionId, turnId: parsed.turnId, quote, turn: lookup.turn })
565
+ sources.push(resolved.source)
442
566
  }
443
567
  return { sources, missing, unverifiable }
444
568
  }
@@ -476,16 +600,12 @@ export function evidenceForCommitment(store, ledger, commitmentId, index) {
476
600
  })
477
601
  continue
478
602
  }
479
- const lookup = lookupTurn(store, sessionId, turnId)
480
- if (!lookup.turn) {
481
- missing.push({ sessionId, turnId, quote, reason: lookup.failed ? 'lookup_failed' : 'turn_not_found' })
482
- continue
483
- }
484
- if (lookup.turn.conversationId !== sessionId) {
485
- missing.push({ sessionId, turnId, quote, reason: 'session_mismatch' })
603
+ const resolved = resolvePointerTurn(store, sessionId, turnId, quote)
604
+ if (resolved.missing) {
605
+ missing.push(resolved.missing)
486
606
  continue
487
607
  }
488
- sources.push({ sessionId, turnId, quote, turn: lookup.turn })
608
+ sources.push(resolved.source)
489
609
  }
490
610
  return { schemaVersion: 2, commitment, sources, missing, unverifiable }
491
611
  }
@@ -776,6 +896,14 @@ function buildReadme({ createdAt, entries, manifestDigest }) {
776
896
  * @param {number} [options.limit=DEFAULT_MAX_EVIDENCE_ENTRIES] max entries before
777
897
  * `EvidencePackLimitError` (Infinity disables the cap).
778
898
  * @param {boolean} [options.all=false] explicit "build the whole pack" opt-in.
899
+ * @param {number|null} [options.clearance=null] per-item secrecy gate: when a
900
+ * number, every requested record is checked against its OWN matter's
901
+ * secrecy (0–3, see lib/egress.js). `null` keeps the pre-0.2.5 library
902
+ * behavior (no filtering); CLI pack/receipt pass 0.
903
+ * @param {'exclude'|'error'} [options.protectedPolicy='exclude'] what to do with
904
+ * a record above `clearance`: omit it and count it (`exclude`, the bulk
905
+ * default), or throw `ProtectedEvidenceError` (`error`, used when the
906
+ * caller named the ids explicitly and must not get a silent partial pack).
779
907
  * @returns {{ files: Record<string, string>, summary: object }}
780
908
  * files: `{ 'manifest.json': string, 'README.txt': string, 'SHA256SUMS': string }`
781
909
  */
@@ -786,6 +914,8 @@ export function buildEvidencePack({
786
914
  now = new Date(),
787
915
  limit = DEFAULT_MAX_EVIDENCE_ENTRIES,
788
916
  all = false,
917
+ clearance = null,
918
+ protectedPolicy = 'exclude',
789
919
  } = {}) {
790
920
  if (!ledger || typeof ledger !== 'object') throw new TypeError('buildEvidencePack requires a ledger')
791
921
  if (!store || typeof store !== 'object') throw new TypeError('buildEvidencePack requires a store')
@@ -794,18 +924,45 @@ export function buildEvidencePack({
794
924
  if (!(effectiveLimit === Infinity || (Number.isInteger(effectiveLimit) && effectiveLimit >= 0))) {
795
925
  throw new TypeError('buildEvidencePack limit must be a non-negative integer or Infinity')
796
926
  }
927
+ const clearanceValue = clearance === undefined || clearance === null ? null : Number(clearance)
928
+ if (clearanceValue !== null && (!Number.isFinite(clearanceValue) || clearanceValue < 0)) {
929
+ throw new TypeError('buildEvidencePack clearance must be a non-negative number or null')
930
+ }
931
+ if (protectedPolicy !== 'exclude' && protectedPolicy !== 'error') {
932
+ throw new TypeError("buildEvidencePack protectedPolicy must be 'exclude' or 'error'")
933
+ }
797
934
  const requestedAny = ids !== undefined && ids !== null && !(Array.isArray(ids) && ids.length === 0)
798
935
  // Build the lookup index once; every evidenceFor* call below then costs O(1)
799
936
  // instead of re-scanning listFacts()/listCommitments() (P0-3: 80k pack went
800
- // from 113.75s to linear time).
801
- const index = requestedAny ? createEvidenceIndex(ledger) : undefined
937
+ // from 113.75s to linear time). The per-item secrecy check reads the same
938
+ // records, so it shares the index.
939
+ const index = requestedAny || clearanceValue !== null ? createEvidenceIndex(ledger) : undefined
802
940
  const requested = normalizeRequestedIds(ids, ledger, index)
803
941
  const requestedCount =
804
942
  requested.commitments.length + requested.facts.length + requested.preferences.length + requested.unknown.length
805
943
  if (requestedCount > effectiveLimit) throw new EvidencePackLimitError(requestedCount, effectiveLimit)
806
944
  const entries = []
945
+ const protectedIds = []
946
+ let protectedExcluded = 0
947
+ /**
948
+ * A-1 (0.2.5): a pack materializes every requested ledger row, so the
949
+ * clearance gate must judge each row by the secrecy of ITS OWN matter — the
950
+ * old "active matter only" check let a secrecy=3 matter leak into a pack built
951
+ * while another matter was active. With `error` (the CLI's explicit --ids
952
+ * path) the whole pack fails closed; with `exclude` (the default/bulk path)
953
+ * the row is omitted and only a count is reported.
954
+ */
955
+ const blockedByClearance = (kind, record) => {
956
+ if (clearanceValue === null || !record) return false
957
+ if (!isEvidenceRecordAboveClearance(ledger, kind, record, clearanceValue)) return false
958
+ if (protectedPolicy === 'error') protectedIds.push(String(record.id ?? ''))
959
+ else protectedExcluded += 1
960
+ return true
961
+ }
807
962
 
808
963
  for (const id of requested.commitments) {
964
+ const commitment = findCommitment(ledger, id, index)
965
+ if (blockedByClearance('commitment', commitment)) continue
809
966
  const result = evidenceForCommitment(store, ledger, id, index)
810
967
  entries.push({
811
968
  kind: 'commitment',
@@ -817,10 +974,14 @@ export function buildEvidencePack({
817
974
  })
818
975
  }
819
976
  for (const id of requested.facts) {
977
+ const fact = findFact(ledger, id, index)
978
+ if (blockedByClearance('fact', fact)) continue
820
979
  const result = evidenceForFact(store, ledger, id, index)
821
980
  entries.push({ kind: 'fact', id, found: Boolean(result.fact), sources: result.sources, missing: result.missing, unverifiable: result.unverifiable })
822
981
  }
823
982
  for (const id of requested.preferences) {
983
+ const preference = findPreference(ledger, id, index)
984
+ if (blockedByClearance('preference', preference)) continue
824
985
  const result = evidenceForPreference(store, ledger, id, index)
825
986
  entries.push({
826
987
  kind: 'preference',
@@ -834,6 +995,7 @@ export function buildEvidencePack({
834
995
  for (const id of requested.unknown) {
835
996
  entries.push({ kind: 'unknown', id, found: false, sources: [], missing: [{ id, reason: 'record_not_found' }] })
836
997
  }
998
+ if (protectedIds.length) throw new ProtectedEvidenceError(protectedIds.length, clearanceValue)
837
999
 
838
1000
  const compacted = entries
839
1001
  .map(compactEntry)
@@ -878,6 +1040,10 @@ export function buildEvidencePack({
878
1040
  unverifiableCount,
879
1041
  operatorEnteredCount,
880
1042
  truncatedCount,
1043
+ // Additive (0.2.5 A-1): how many requested rows were withheld because
1044
+ // their own matter is protected. Counts only — never a title or quote.
1045
+ protectedExcluded,
1046
+ clearance: clearanceValue,
881
1047
  notFound: compacted.filter((entry) => !entry.found).map((entry) => entry.id),
882
1048
  fileHashes: { 'manifest.json': manifestDigest, 'README.txt': readmeDigest },
883
1049
  entries: compacted,
@@ -1256,6 +1422,13 @@ function receiptEvidenceEntry(entry) {
1256
1422
  function normalizeReceiptEvidence(evidence) {
1257
1423
  const meta = { format: MANIFEST_FORMAT, version: MANIFEST_VERSION }
1258
1424
  let list = []
1425
+ // Additive (0.2.5 A-1): the evidence pack reports how many requested rows it
1426
+ // withheld because their own matter is protected; the receipt summary carries
1427
+ // the same count (never a title/creditor/action/quote).
1428
+ let protectedExcluded = 0
1429
+ const readProtectedExcluded = (summary) => {
1430
+ if (summary && Number.isInteger(summary.protectedExcluded) && summary.protectedExcluded >= 0) protectedExcluded = summary.protectedExcluded
1431
+ }
1259
1432
  if (evidence === undefined || evidence === null) {
1260
1433
  list = []
1261
1434
  } else if (typeof evidence === 'string') {
@@ -1270,6 +1443,7 @@ function normalizeReceiptEvidence(evidence) {
1270
1443
  if (evidence.summary && Array.isArray(evidence.summary.entries)) {
1271
1444
  if (typeof evidence.summary.format === 'string') meta.format = evidence.summary.format
1272
1445
  if (Number.isInteger(evidence.summary.version)) meta.version = evidence.summary.version
1446
+ readProtectedExcluded(evidence.summary)
1273
1447
  list = evidence.summary.entries
1274
1448
  } else if (Array.isArray(evidence.entries)) {
1275
1449
  list = evidence.entries
@@ -1313,6 +1487,7 @@ function normalizeReceiptEvidence(evidence) {
1313
1487
  missingCount,
1314
1488
  unverifiableCount,
1315
1489
  operatorEnteredCount,
1490
+ protectedExcluded,
1316
1491
  entries,
1317
1492
  }
1318
1493
  }
@@ -1999,6 +2174,7 @@ export async function buildReceiptPack(options = {}) {
1999
2174
  missingCount: evidence.missingCount,
2000
2175
  unverifiableCount: evidence.unverifiableCount ?? 0,
2001
2176
  operatorEnteredCount: evidence.operatorEnteredCount ?? 0,
2177
+ protectedExcluded: Number.isInteger(evidence.protectedExcluded) ? evidence.protectedExcluded : 0,
2002
2178
  },
2003
2179
  anchored: anchors.length > 0,
2004
2180
  fileMode: RECEIPT_FILE_MODE,
package/lib/fs-safe.js CHANGED
@@ -14,7 +14,10 @@
14
14
  * its length, then atomically rename it into place; on any failure remove
15
15
  * the temp file and rethrow (no 'best effort' success);
16
16
  * 4. retry transient Windows/AV/OneDrive failures (EPERM/EBUSY/EACCES) with
17
- * bounded exponential backoff instead of failing a user command once.
17
+ * bounded exponential backoff instead of failing a user command once;
18
+ * 5. flush a path through a writable but non-truncating handle ('r+'): Windows
19
+ * rejects fsync on a read-only handle with EPERM, which used to break
20
+ * backup/import/vault-unseal/migration on every Windows machine.
18
21
  *
19
22
  * This is a library-only module: it never imports node:sqlite and never writes
20
23
  * to the user's home. Every function takes an optional `fs` implementation
@@ -117,23 +120,83 @@ export function writeAllSync(fd, data, { fs = nodeFs } = {}) {
117
120
  return written
118
121
  }
119
122
 
120
- /** fsync a file descriptor or a path (opened read-only). */
121
- export function fsyncFileSync(fileOrFd, { fs = nodeFs, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
123
+ /**
124
+ * Errnos a writable open can fail with when the path is read-only for this user.
125
+ * On Windows they mean fsync cannot be performed at all (see openForFsyncSync).
126
+ */
127
+ const WINDOWS_FSYNC_UNWRITABLE_ERRNOS = new Set(['EACCES', 'EPERM', 'EROFS'])
128
+
129
+ /**
130
+ * Open `file` for the path form of fsyncFileSync.
131
+ *
132
+ * Windows: fsyncSync() is FlushFileBuffers(), which only accepts a handle opened
133
+ * with write access. Opening the file read-only and fsyncing it afterwards fails
134
+ * with EPERM, which used to break every durable-write path on Windows
135
+ * (`advisers backup`, `import --replace`, `vault unseal` and the pre-migration
136
+ * backup; see 0.3 research §4.1/R1). The handle must therefore be writable AND
137
+ * must never truncate, hence `'r+'`: it requires the file to exist and keeps its
138
+ * bytes and length exactly as they are. `createIfMissing` opts into `'a+'`
139
+ * (creates a missing zero-byte file, never truncates an existing one) for a
140
+ * caller that genuinely needs to flush a path that may not exist yet.
141
+ *
142
+ * POSIX: fsync(2) accepts an O_RDONLY descriptor, so a read-only-but-readable
143
+ * file (and a read-only filesystem) keeps the historical `'r'` fallback.
144
+ *
145
+ * Windows read-only files are fail-closed on purpose: returning success without
146
+ * flushing would let a caller report a backup as durable when it is not.
147
+ */
148
+ function openForFsyncSync(file, { fs = nodeFs, platform = process.platform, createIfMissing = false } = {}) {
149
+ try {
150
+ return fs.openSync(file, 'r+')
151
+ } catch (error) {
152
+ const code = errnoOf(error)
153
+ if (code === 'ENOENT' && createIfMissing) return fs.openSync(file, 'a+')
154
+ if (platform !== 'win32') {
155
+ // POSIX: flush a readable descriptor when the file cannot be opened writable.
156
+ return fs.openSync(file, 'r')
157
+ }
158
+ if (WINDOWS_FSYNC_UNWRITABLE_ERRNOS.has(code)) {
159
+ const denied = new Error(`${code}: cannot fsync ${file} on Windows: FlushFileBuffers requires a writable handle and the file could not be opened read-write; make the file/directory writable, or close the program (antivirus/OneDrive/sync client) that may be holding it`)
160
+ denied.code = code
161
+ if (error?.errno !== undefined) denied.errno = error.errno
162
+ denied.cause = error
163
+ throw denied
164
+ }
165
+ throw error
166
+ }
167
+ }
168
+
169
+ /**
170
+ * fsync a file descriptor or a path.
171
+ *
172
+ * The numeric form flushes the caller's descriptor (a temp file the caller just
173
+ * wrote is already open for writing). The path form opens a writable,
174
+ * non-truncating handle for the duration of the fsync -- never a read-only one,
175
+ * because Windows rejects fsync on read-only handles with EPERM (see
176
+ * openForFsyncSync). The file's bytes and length are left unchanged.
177
+ */
178
+ export function fsyncFileSync(fileOrFd, { fs = nodeFs, platform = process.platform, createIfMissing = false, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
122
179
  if (typeof fileOrFd === 'number') {
123
180
  return retryFsSync(() => fs.fsyncSync(fileOrFd), { retries, errnos, backoffMs, sleep })
124
181
  }
125
182
  return retryFsSync(() => {
126
- const fd = fs.openSync(fileOrFd, 'r')
183
+ const fd = openForFsyncSync(fileOrFd, { fs, platform, createIfMissing })
127
184
  try { fs.fsyncSync(fd) } finally { try { fs.closeSync(fd) } catch {} }
128
185
  }, { retries, errnos, backoffMs, sleep })
129
186
  }
130
187
 
131
188
  /**
132
189
  * fsync a directory entry so a completed rename survives a crash. Windows
133
- * returns EPERM for directory fsync (and FAT/network shares may not support it),
134
- * so this is best-effort by contract: it returns a boolean, never throws.
190
+ * cannot fsync a directory through Node (and cannot open one read-write), while
191
+ * FAT/network shares may not support it either, so this is best-effort by
192
+ * contract: it returns a boolean, never throws, and is a no-op (false) on
193
+ * Windows.
135
194
  */
136
- export function fsyncDirSync(dir, { fs = nodeFs } = {}) {
195
+ export function fsyncDirSync(dir, { fs = nodeFs, platform = process.platform } = {}) {
196
+ // Windows cannot fsync a directory at all (and cannot open one read-write):
197
+ // FlushFileBuffers has no directory-handle semantics there, so honour the
198
+ // documented best-effort contract without attempting a doomed read-only open.
199
+ if (platform === 'win32') return false
137
200
  try {
138
201
  const fd = fs.openSync(dir, 'r')
139
202
  try { fs.fsyncSync(fd) } finally { try { fs.closeSync(fd) } catch {} }
package/lib/ical.js CHANGED
File without changes
package/lib/imap.js CHANGED
@@ -16,6 +16,10 @@
16
16
  */
17
17
  import net from 'node:net'
18
18
  import tls from 'node:tls'
19
+ // 0.3.0 Wave 3 (docs/TUI.md §4): the server greeting and tagged status lines are
20
+ // remote text. They must never reach stderr with ANSI/OSC/bidi or a second line,
21
+ // so every message that embeds one is sanitised here (bin only prefixes it).
22
+ import { sanitizeConsoleText } from './text-safe.js'
19
23
 
20
24
  const CRLF = '\r\n'
21
25
  const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
@@ -148,12 +152,12 @@ export async function fetchImapMessages({
148
152
  const t = tag()
149
153
  socket.write(`${t} ${text}${CRLF}`)
150
154
  const r = await reader.response(t)
151
- if (!/^OK/i.test(r.status.split(' ').slice(1).join(' '))) throw new Error(`IMAP ${text.split(' ')[0]} failed: ${r.status}`)
155
+ if (!/^OK/i.test(r.status.split(' ').slice(1).join(' '))) throw new Error(`IMAP ${text.split(' ')[0]} failed: ${sanitizeConsoleText(r.status, { max: 500 })}`)
152
156
  return r
153
157
  }
154
158
  try {
155
159
  const greeting = await reader.readLine()
156
- if (!/^\* (OK|PREAUTH)/i.test(greeting)) throw new Error(`IMAP greeting failed: ${greeting}`)
160
+ if (!/^\* (OK|PREAUTH)/i.test(greeting)) throw new Error(`IMAP greeting failed: ${sanitizeConsoleText(greeting, { max: 500 })}`)
157
161
  await command(`LOGIN ${imapQuote(user)} ${imapQuote(pass)}`)
158
162
  const selected = await command(`EXAMINE ${imapQuote(mailbox)}`)
159
163
  const exists = selected.untagged.find((l) => /^\* \d+ EXISTS/i.test(l))
package/lib/importers.js CHANGED
File without changes
package/lib/learning.js CHANGED
File without changes