typeclaw 0.50.0 → 0.51.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.
Files changed (90) hide show
  1. package/dist/types/agent/bash-sandbox-env.d.ts +15 -0
  2. package/dist/types/agent/bash-sandbox-proc.d.ts +8 -0
  3. package/dist/types/agent/bash-sandbox.d.ts +94 -0
  4. package/dist/types/agent/compaction.d.ts +4 -4
  5. package/dist/types/agent/idle-compaction.d.ts +1 -0
  6. package/dist/types/agent/index.d.ts +5 -17
  7. package/dist/types/agent/output-allowance.d.ts +4 -0
  8. package/dist/types/agent/plugin-tools.d.ts +4 -75
  9. package/dist/types/agent/system-prompt-composition.d.ts +17 -0
  10. package/dist/types/agent/tools/ddg.d.ts +9 -0
  11. package/dist/types/agent/tools/stream-snapshot.d.ts +1 -1
  12. package/dist/types/agent/tools/webfetch/tool.d.ts +1 -1
  13. package/dist/types/channels/github-review-check.d.ts +1 -1
  14. package/dist/types/channels/router.d.ts +11 -49
  15. package/dist/types/config/config.d.ts +24 -11
  16. package/dist/types/config/providers.d.ts +3 -1
  17. package/dist/types/container/start.d.ts +1 -0
  18. package/dist/types/eval/cell.d.ts +26 -0
  19. package/dist/types/eval/config.d.ts +19 -0
  20. package/dist/types/eval/dispatch.d.ts +7 -0
  21. package/dist/types/eval/kernel.d.ts +28 -0
  22. package/dist/types/eval/process.d.ts +23 -0
  23. package/dist/types/eval/protocol.d.ts +34 -0
  24. package/dist/types/eval/sandbox.d.ts +10 -0
  25. package/dist/types/eval/tool.d.ts +17 -0
  26. package/dist/types/eval/wire.d.ts +33 -0
  27. package/dist/types/sandbox/env-exposure.d.ts +2 -0
  28. package/package.json +5 -5
  29. package/src/agent/bash-sandbox-env.ts +104 -0
  30. package/src/agent/bash-sandbox-proc.ts +70 -0
  31. package/src/agent/bash-sandbox.ts +225 -0
  32. package/src/agent/compaction.ts +22 -10
  33. package/src/agent/idle-compaction.ts +7 -3
  34. package/src/agent/index.ts +66 -157
  35. package/src/agent/output-allowance.ts +7 -0
  36. package/src/agent/plugin-tools.ts +29 -379
  37. package/src/agent/session-origin.ts +2 -1
  38. package/src/agent/subagent-completion-reminder.ts +2 -2
  39. package/src/agent/system-prompt-composition.ts +141 -0
  40. package/src/agent/system-prompt.ts +4 -4
  41. package/src/agent/todo/scope.ts +1 -1
  42. package/src/agent/tools/ddg.ts +71 -11
  43. package/src/agent/tools-without-local-file-operands.ts +4 -0
  44. package/src/browser/fill-secret.ts +307 -0
  45. package/src/bundled-plugins/explorer/explorer.ts +1 -1
  46. package/src/bundled-plugins/github-cli-auth/gh-review-detect.ts +40 -16
  47. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +12 -7
  48. package/src/bundled-plugins/github-cli-auth/index.ts +10 -1
  49. package/src/bundled-plugins/managed-browser/index.ts +107 -2
  50. package/src/bundled-plugins/memory/dreaming.ts +4 -1
  51. package/src/bundled-plugins/memory/load-memory.ts +2 -4
  52. package/src/bundled-plugins/memory/memory-logger.ts +74 -2
  53. package/src/bundled-plugins/memory/transcript-preload.ts +103 -0
  54. package/src/bundled-plugins/memory/vector/hybrid.ts +25 -14
  55. package/src/bundled-plugins/security/policies/private-surface-read.ts +166 -201
  56. package/src/channels/adapters/discord-bot-reference.ts +5 -4
  57. package/src/channels/adapters/discord-bot.ts +1 -0
  58. package/src/channels/adapters/discord-classify.ts +8 -3
  59. package/src/channels/adapters/discord.ts +29 -0
  60. package/src/channels/github-review-check.ts +5 -1
  61. package/src/channels/github-review-verdict-coordinator.ts +68 -2
  62. package/src/channels/router.ts +88 -107
  63. package/src/channels/subagent-completion-bridge.ts +3 -2
  64. package/src/cli/hostd.ts +3 -0
  65. package/src/config/config.ts +15 -0
  66. package/src/config/providers.ts +10 -4
  67. package/src/container/start.ts +37 -1
  68. package/src/doctor/checks.ts +25 -16
  69. package/src/doctor/index.ts +48 -12
  70. package/src/doctor/operational-incidents.ts +16 -1
  71. package/src/doctor/plugin-bridge.ts +14 -1
  72. package/src/doctor/stage.ts +14 -0
  73. package/src/doctor/types.ts +6 -0
  74. package/src/eval/cell.ts +93 -0
  75. package/src/eval/config.ts +32 -0
  76. package/src/eval/dispatch.ts +67 -0
  77. package/src/eval/entry.ts +261 -0
  78. package/src/eval/kernel.ts +154 -0
  79. package/src/eval/process.ts +113 -0
  80. package/src/eval/protocol.ts +26 -0
  81. package/src/eval/redeclare.ts +174 -0
  82. package/src/eval/sandbox.ts +114 -0
  83. package/src/eval/tool.ts +98 -0
  84. package/src/eval/wire.ts +93 -0
  85. package/src/operations/classify-tool-outcome.ts +1 -1
  86. package/src/plugin/core-tool-names.ts +6 -1
  87. package/src/sandbox/env-exposure.ts +20 -0
  88. package/src/server/index.ts +46 -11
  89. package/src/skills/typeclaw-config/SKILL.md +20 -2
  90. package/typeclaw.schema.json +52 -0
@@ -48,7 +48,6 @@ export const GUARD_PRIVATE_SURFACE_READ = 'privateSurfaceRead'
48
48
  // keeps both enforcement points agreeing on which tools read no local path.
49
49
  const UNSCANNED_TOOLS = new Set(['bash', ...TOOLS_WITHOUT_LOCAL_FILE_OPERANDS])
50
50
  const VIRTUAL_FILESYSTEM_ROOTS = ['/proc', '/sys', '/dev', '/run'] as const
51
- const HARDLINK_SCAN_EXCLUDED_AGENT_DIRS = new Set(['.git', '.gitstore', 'node_modules'])
52
51
 
53
52
  // The bash sandbox hides the role's private surface — the working DIRECTORIES
54
53
  // (workspace/, memory/, sessions/), unconditional credential directories, and
@@ -96,7 +95,7 @@ export function checkPrivateSurfaceReadGuard(
96
95
  const localOperands = collectLocalOperandPaths(fileOperands)
97
96
 
98
97
  if (!canonicalEmpty) {
99
- const identityScanner = createHardlinkIdentityScanner(agentDir, canonicalDirs, canonicalFiles, hooks)
98
+ const identityScanner = createHardlinkIdentityScanner(canonicalDirs, canonicalFiles, hooks)
100
99
  for (const candidate of collectPathCandidates(
101
100
  args,
102
101
  tool,
@@ -111,7 +110,7 @@ export function checkPrivateSurfaceReadGuard(
111
110
  }
112
111
 
113
112
  if (!roleEmpty) {
114
- const identityScanner = createHardlinkIdentityScanner(agentDir, roleDirs, roleFiles, hooks)
113
+ const identityScanner = createHardlinkIdentityScanner(roleDirs, roleFiles, hooks)
115
114
  for (const candidate of collectPathCandidates(args, tool, nonFile, localOperands)) {
116
115
  const hit = matchHidden(candidate, agentDir, roleDirs, roleFiles, identityScanner, realpath)
117
116
  if (hit !== undefined) return privateSurfacePathBlock(tool, candidate, hit)
@@ -153,7 +152,7 @@ export function createPrivateSurfaceReadIdentityVerifier(
153
152
  const { canonicalDirs, canonicalFiles, roleDirs, roleFiles } = deniedSurface(options.agentDir, options.hidden)
154
153
  const deniedDirs = [...new Set([...canonicalDirs, ...roleDirs])]
155
154
  const deniedFiles = [...new Set([...canonicalFiles, ...roleFiles])]
156
- const scanner = createHardlinkIdentityScanner(options.agentDir, deniedDirs, deniedFiles, hooks)
155
+ const scanner = createHardlinkIdentityScanner(deniedDirs, deniedFiles, hooks)
157
156
  return {
158
157
  check(identity) {
159
158
  if (identity.nlink <= 1) return undefined
@@ -171,14 +170,12 @@ export function checkPrivateSurfaceReadIdentityGuard(
171
170
  }
172
171
 
173
172
  function createHardlinkIdentityScanner(
174
- agentDir: string,
175
173
  deniedDirs: readonly string[],
176
174
  deniedFiles: readonly string[],
177
175
  hooks: PrivateSurfaceIdentityScanHooks,
178
176
  ): HardlinkIdentityScanner {
179
177
  const lstat = hooks.lstat ?? lstatSync
180
178
  return new HardlinkIdentityScanner({
181
- agentDir,
182
179
  deniedDirs,
183
180
  deniedFiles,
184
181
  maxEntries: hooks.maxEntries ?? MAX_HARDLINK_IDENTITY_ENTRIES,
@@ -221,26 +218,38 @@ function deniedSurface(
221
218
  }
222
219
  }
223
220
 
224
- function privateSurfacePathBlock(tool: string, candidate: string, hit: string): SecurityBlock {
221
+ function privateSurfacePathBlock(tool: string, candidate: string, hit: IdentityHit): SecurityBlock {
222
+ if (hit.kind === 'unverified') return unverifiedIdentityBlock(tool, `argument \`${candidate}\``, hit.reason)
225
223
  return {
226
224
  block: true,
227
225
  reason: [
228
- `Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: argument \`${candidate}\` resolves to ${hit}, which is not available to LLM tools.`,
226
+ `Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: argument \`${candidate}\` resolves to ${hit.path}, which is not available to LLM tools.`,
229
227
  'The bash sandbox masks the same path. Privileged roles cannot bypass canonical agent credential files; use host-side redacted diagnostics such as `typeclaw doctor`, `typeclaw provider list`, or `typeclaw channel list` instead.',
230
228
  ].join(' '),
231
229
  }
232
230
  }
233
231
 
234
- function privateSurfaceBlock(tool: string, hit: string): SecurityBlock {
232
+ function privateSurfaceBlock(tool: string, hit: IdentityHit): SecurityBlock {
233
+ if (hit.kind === 'unverified') return unverifiedIdentityBlock(tool, 'input', hit.reason)
235
234
  return {
236
235
  block: true,
237
236
  reason: [
238
- `Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: input inode aliases ${hit}, which is not available to LLM tools.`,
237
+ `Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: input inode aliases ${hit.path}, which is not available to LLM tools.`,
239
238
  'The bash sandbox masks the same path. Privileged roles cannot bypass canonical agent credential files; use host-side redacted diagnostics instead.',
240
239
  ].join(' '),
241
240
  }
242
241
  }
243
242
 
243
+ function unverifiedIdentityBlock(tool: string, subject: string, reason: string): SecurityBlock {
244
+ return {
245
+ block: true,
246
+ reason: [
247
+ `Guard \`${GUARD_PRIVATE_SURFACE_READ}\` blocked ${tool}: ${subject} is a multiply-linked file, and the guard could not verify that none of its hardlinks lives under a protected credential or private path (${reason}).`,
248
+ 'No protected alias was found; the verification itself failed. Ask the operator to resolve the reported condition or to replace the file with a single-link copy.',
249
+ ].join(' '),
250
+ }
251
+ }
252
+
244
253
  // Field names whose values are ALWAYS free text (prose/queries/ids), NEVER a
245
254
  // filesystem path, for EVERY tool. Scanning them caused false positives: a
246
255
  // guest's `channel_reply({ text: "the memory leak" })` or `web_search({ query:
@@ -470,22 +479,25 @@ function matchHidden(
470
479
  deniedFiles: string[],
471
480
  identityScanner: HardlinkIdentityScanner,
472
481
  realpath: (candidate: string) => string,
473
- ): string | undefined {
482
+ ): IdentityHit | undefined {
474
483
  const lexicalHit = matchLexicallyDenied(candidate, agentDir, deniedDirs, deniedFiles)
475
- if (lexicalHit !== undefined) return lexicalHit
484
+ if (lexicalHit !== undefined) return { kind: 'denied', path: lexicalHit }
476
485
  const lexical = path.resolve(agentDir, candidate)
477
486
  const resolved = realpathRealIntendedPath(lexical, realpath)
478
487
  const virtualRoot = virtualFilesystemRoot(lexical) ?? virtualFilesystemRoot(resolved)
479
- if (virtualRoot !== undefined) return `${virtualRoot}, a virtual or process-backed filesystem`
488
+ if (virtualRoot !== undefined)
489
+ return { kind: 'denied', path: `${virtualRoot}, a virtual or process-backed filesystem` }
480
490
  for (const file of deniedFiles) {
481
- if (resolved === realpathRealIntendedPath(file, realpath) || hasSameFileIdentity(resolved, file)) return file
491
+ if (resolved === realpathRealIntendedPath(file, realpath) || hasSameFileIdentity(resolved, file)) {
492
+ return { kind: 'denied', path: file }
493
+ }
482
494
  }
483
495
  for (const dir of deniedDirs) {
484
496
  const realDir = realpathRealIntendedPath(dir, realpath)
485
497
  // realpathRealIntendedPath joins with the platform separator, so the
486
498
  // under-dir test must use path.sep too — a hardcoded "/" never matches the
487
499
  // "\"-joined paths a win32 test runner produces.
488
- if (resolved === realDir || resolved.startsWith(`${realDir}${path.sep}`)) return dir
500
+ if (resolved === realDir || resolved.startsWith(`${realDir}${path.sep}`)) return { kind: 'denied', path: dir }
489
501
  }
490
502
  const identity = fileIdentity(resolved)
491
503
  if (identity !== undefined && identity.nlink > 1) {
@@ -633,7 +645,6 @@ function openDescriptorAnchoredDirectory(directory: string): HardlinkDirectory {
633
645
  }
634
646
 
635
647
  type HardlinkIdentityScannerOptions = {
636
- agentDir: string
637
648
  deniedDirs: readonly string[]
638
649
  deniedFiles: readonly string[]
639
650
  maxEntries: number
@@ -643,160 +654,159 @@ type HardlinkIdentityScannerOptions = {
643
654
  afterEntryLstat?(candidate: string, stats: Stats): void
644
655
  }
645
656
 
657
+ type IdentityHit = { kind: 'denied'; path: string } | { kind: 'unverified'; reason: string }
658
+
659
+ // A multiply-linked input can expose protected bytes only through a link that
660
+ // lives under a denied file or tree, so the proof inventories exactly those and
661
+ // fails closed only when that inventory is incomplete. Where the input's other
662
+ // links live otherwise — a read-only dev checkout mounted outside the agent
663
+ // tree, a package-manager cache — and how large the visible tree is say nothing
664
+ // about that; demanding every link be found inside the agent tree blocked those
665
+ // legitimate reads outright without protecting anything the denied inventory
666
+ // does not already cover.
646
667
  class HardlinkIdentityScanner {
647
668
  private remaining: number
648
- private readonly visibleCounts = new Map<string, number>()
649
669
  private readonly deniedIdentities = new Map<string, string>()
650
670
  private readonly observedEntries = new Set<string>()
651
671
  private readonly openedDirectories = new Set<string>()
652
- private visibleScanned = false
653
- private deniedScanned = false
654
- private visibleFailure: string | undefined
655
- private deniedFailure: string | undefined
672
+ private readonly deniedFileStates = new Map<string, string>()
673
+ private readonly missingDeniedDirs: string[] = []
656
674
  private deniedFilesScanned = false
675
+ private deniedScanned = false
676
+ private failure: string | undefined
657
677
 
658
678
  constructor(private readonly options: HardlinkIdentityScannerOptions) {
659
679
  this.remaining = options.maxEntries
660
680
  }
661
681
 
662
- check(identity: Pick<Stats, 'dev' | 'ino' | 'nlink'>): string | undefined {
682
+ check(identity: Pick<Stats, 'dev' | 'ino' | 'nlink'>): IdentityHit | undefined {
663
683
  const key = identityKey(identity)
664
684
  this.scanDeniedFiles()
665
- if (this.deniedFailure !== undefined) return this.deniedFailure
685
+ if (this.failure !== undefined) return { kind: 'unverified', reason: this.failure }
666
686
  const deniedFile = this.deniedIdentities.get(key)
667
- if (deniedFile !== undefined) return deniedFile
687
+ if (deniedFile !== undefined) return { kind: 'denied', path: deniedFile }
668
688
 
669
689
  this.scanDeniedTrees()
670
690
  const deniedAlias = this.deniedIdentities.get(key)
671
- if (deniedAlias !== undefined) return deniedAlias
672
- if (this.deniedFailure !== undefined) return this.deniedFailure
673
-
674
- this.scanVisibleTree()
675
- if (this.visibleFailure !== undefined) return this.visibleFailure
676
- if ((this.visibleCounts.get(key) ?? 0) >= identity.nlink) return undefined
677
- return 'hardlink identities could not be fully accounted within the agent tree'
691
+ if (deniedAlias !== undefined) return { kind: 'denied', path: deniedAlias }
692
+ if (this.failure !== undefined) return { kind: 'unverified', reason: this.failure }
693
+ return undefined
678
694
  }
679
695
 
680
696
  private scanDeniedFiles(): void {
681
697
  if (this.deniedFilesScanned) return
682
698
  this.deniedFilesScanned = true
683
699
  for (const file of this.options.deniedFiles) {
684
- try {
685
- const stats = this.options.lstat(file)
686
- if (stats.isFile()) this.deniedIdentities.set(identityKey(stats), file)
687
- } catch (error) {
688
- if (isNotFoundError(error)) continue
689
- this.deniedFailure = `denied-file hardlink identity could not be read: ${file}`
690
- return
700
+ const state = this.deniedFileState(file)
701
+ if (state === undefined) return
702
+ this.deniedFileStates.set(file, state)
703
+ if (state.startsWith('file:')) this.deniedIdentities.set(state.slice('file:'.length), file)
704
+ }
705
+ }
706
+
707
+ // Every root's descriptors and census snapshots stay open until the whole
708
+ // inventory completes, and absent denied paths must still be absent then.
709
+ // Verifying root by root would let a protected link move from a pending root
710
+ // into an already-closed one — or into a denied root or file path that was
711
+ // missing when visited — without either observation seeing it.
712
+ private scanDeniedTrees(): void {
713
+ if (this.deniedScanned) return
714
+ this.deniedScanned = true
715
+ const opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }> = []
716
+ let scanCompleted = false
717
+ try {
718
+ for (const deniedDir of this.options.deniedDirs) {
719
+ if (!this.scanDeniedTree(deniedDir, opened)) return
691
720
  }
721
+ scanCompleted = true
722
+ } finally {
723
+ this.verifyAndCloseDirectories(opened, scanCompleted)
692
724
  }
725
+ this.verifyDeniedPathsUnchanged()
693
726
  }
694
727
 
695
- private scanVisibleTree(): void {
696
- if (this.visibleScanned) return
697
- this.visibleScanned = true
698
- const root = realpathRealIntendedPath(this.options.agentDir, this.options.realpath)
699
- const deniedRoots = this.options.deniedDirs.map((dir) => realpathRealIntendedPath(dir, this.options.realpath))
700
- const deniedFiles = new Set(
701
- this.options.deniedFiles.map((file) => realpathRealIntendedPath(file, this.options.realpath)),
702
- )
703
- const rootStats = this.readRootStats(root, 'visible')
704
- if (rootStats === undefined) return
705
- const rootDirectory = this.openVerifiedDirectory(root, rootStats, 'visible')
706
- if (rootDirectory === undefined) return
728
+ private scanDeniedTree(
729
+ deniedDir: string,
730
+ opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }>,
731
+ ): boolean {
732
+ let rootStats: Stats
733
+ let root: string
734
+ try {
735
+ root = realpathRealIntendedPath(deniedDir, this.options.realpath)
736
+ rootStats = this.options.lstat(root)
737
+ } catch (error) {
738
+ if (isNotFoundError(error)) {
739
+ this.missingDeniedDirs.push(deniedDir)
740
+ return true
741
+ }
742
+ this.failure = `denied-directory hardlink identity could not be read: ${deniedDir}`
743
+ return false
744
+ }
745
+ if (!rootStats.isDirectory()) {
746
+ this.failure = `denied-directory hardlink identity root is not a directory: ${deniedDir}`
747
+ return false
748
+ }
749
+ const rootDirectory = this.openVerifiedDirectory(root, rootStats)
750
+ if (rootDirectory === undefined) return false
751
+ opened.push({ directory: root, ...rootDirectory })
707
752
  const pending: Array<{
708
753
  directory: string
709
754
  reader: HardlinkDirectory
710
755
  identity: Pick<Stats, 'dev' | 'ino'>
711
756
  census: Stats
712
- agentRoot: boolean
713
- }> = [{ directory: root, ...rootDirectory, identity: rootStats, agentRoot: true }]
714
- const opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }> = [
715
- { directory: root, ...rootDirectory },
716
- ]
717
- let scanCompleted = false
718
- try {
719
- while (pending.length > 0) {
720
- const current = pending.pop()
721
- if (current === undefined) break
722
- const completed = this.forEachEntry(current, 'visible', (entry, stats) => {
723
- const entryPath = path.join(current.directory, entry.name)
724
- if (current.agentRoot && HARDLINK_SCAN_EXCLUDED_AGENT_DIRS.has(entry.name)) return true
725
- if (deniedFiles.has(entryPath) || deniedRoots.some((deniedRoot) => isPathAtOrBelow(entryPath, deniedRoot))) {
726
- return true
727
- }
728
- if (stats.isDirectory()) {
729
- const child = this.openVerifiedChild(current, entry.name, stats, 'visible')
730
- if (child === undefined) return false
731
- const openedChild = { directory: entryPath, ...child }
732
- pending.push({ ...openedChild, identity: stats, agentRoot: false })
733
- opened.push(openedChild)
734
- } else if (stats.isFile()) {
735
- this.recordVisibleFile(stats)
736
- }
737
- return true
738
- })
739
- if (!completed) return
740
- }
741
- scanCompleted = true
742
- } finally {
743
- this.verifyAndCloseDirectories(opened, 'visible', scanCompleted)
757
+ }> = [{ directory: root, ...rootDirectory, identity: rootStats }]
758
+ while (pending.length > 0) {
759
+ const current = pending.pop()
760
+ if (current === undefined) break
761
+ const completed = this.forEachEntry(current, (entry, stats) => {
762
+ const entryPath = path.join(current.directory, entry.name)
763
+ if (stats.isDirectory()) {
764
+ const child = this.openVerifiedChild(current, entry.name, stats)
765
+ if (child === undefined) return false
766
+ const openedChild = { directory: entryPath, ...child }
767
+ pending.push({ ...openedChild, identity: stats })
768
+ opened.push(openedChild)
769
+ } else if (stats.isFile()) {
770
+ this.deniedIdentities.set(identityKey(stats), deniedDir)
771
+ }
772
+ return true
773
+ })
774
+ if (!completed) return false
744
775
  }
776
+ return true
745
777
  }
746
778
 
747
- private scanDeniedTrees(): void {
748
- if (this.deniedScanned) return
749
- this.deniedScanned = true
750
- for (const deniedDir of this.options.deniedDirs) {
751
- let rootStats: Stats
752
- let root: string
779
+ private verifyDeniedPathsUnchanged(): void {
780
+ if (this.failure !== undefined) return
781
+ for (const [file, before] of this.deniedFileStates) {
782
+ const after = this.deniedFileState(file)
783
+ if (after === undefined) return
784
+ if (after !== before) {
785
+ this.failure = `denied file changed during hardlink inventory: ${file}`
786
+ return
787
+ }
788
+ }
789
+ for (const deniedDir of this.missingDeniedDirs) {
753
790
  try {
754
- root = realpathRealIntendedPath(deniedDir, this.options.realpath)
755
- rootStats = this.options.lstat(root)
791
+ this.options.lstat(realpathRealIntendedPath(deniedDir, this.options.realpath))
756
792
  } catch (error) {
757
793
  if (isNotFoundError(error)) continue
758
- this.deniedFailure = `denied-directory hardlink identity could not be read: ${deniedDir}`
794
+ this.failure = `denied-directory hardlink identity could not be read: ${deniedDir}`
759
795
  return
760
796
  }
761
- if (!rootStats.isDirectory()) {
762
- this.deniedFailure = `denied-directory hardlink identity root is not a directory: ${deniedDir}`
763
- return
764
- }
765
- const rootDirectory = this.openVerifiedDirectory(root, rootStats, 'denied')
766
- if (rootDirectory === undefined) return
767
- const pending: Array<{
768
- directory: string
769
- reader: HardlinkDirectory
770
- identity: Pick<Stats, 'dev' | 'ino'>
771
- census: Stats
772
- }> = [{ directory: root, ...rootDirectory, identity: rootStats }]
773
- const opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }> = [
774
- { directory: root, ...rootDirectory },
775
- ]
776
- let scanCompleted = false
777
- try {
778
- while (pending.length > 0) {
779
- const current = pending.pop()
780
- if (current === undefined) break
781
- const completed = this.forEachEntry(current, 'denied', (entry, stats) => {
782
- const entryPath = path.join(current.directory, entry.name)
783
- if (stats.isDirectory()) {
784
- const child = this.openVerifiedChild(current, entry.name, stats, 'denied')
785
- if (child === undefined) return false
786
- const openedChild = { directory: entryPath, ...child }
787
- pending.push({ ...openedChild, identity: stats })
788
- opened.push(openedChild)
789
- } else if (stats.isFile()) {
790
- this.recordDeniedFile(stats, deniedDir)
791
- }
792
- return true
793
- })
794
- if (!completed) return
795
- }
796
- scanCompleted = true
797
- } finally {
798
- this.verifyAndCloseDirectories(opened, 'denied', scanCompleted)
799
- }
797
+ this.failure = `denied directory appeared during hardlink inventory: ${deniedDir}`
798
+ return
799
+ }
800
+ }
801
+
802
+ private deniedFileState(file: string): string | undefined {
803
+ try {
804
+ const stats = this.options.lstat(file)
805
+ return stats.isFile() ? `file:${identityKey(stats)}` : `other:${identityKey(stats)}`
806
+ } catch (error) {
807
+ if (isNotFoundError(error)) return 'missing'
808
+ this.failure = `denied-file hardlink identity could not be read: ${file}`
809
+ return undefined
800
810
  }
801
811
  }
802
812
 
@@ -806,7 +816,6 @@ class HardlinkIdentityScanner {
806
816
  reader: HardlinkDirectory
807
817
  identity: Pick<Stats, 'dev' | 'ino'>
808
818
  },
809
- surface: 'visible' | 'denied',
810
819
  visit: (entry: Dirent, stats: Stats) => boolean,
811
820
  ): boolean {
812
821
  let completed = true
@@ -814,11 +823,11 @@ class HardlinkIdentityScanner {
814
823
  while (true) {
815
824
  const entry = current.reader.readSync()
816
825
  if (entry === null) break
817
- if (!this.consumeEntry(surface)) {
826
+ if (!this.consumeEntry()) {
818
827
  completed = false
819
828
  break
820
829
  }
821
- if (!this.claimEntryObservation(current.identity, entry.name, surface)) {
830
+ if (!this.claimEntryObservation(current.identity, entry.name)) {
822
831
  completed = false
823
832
  break
824
833
  }
@@ -827,7 +836,7 @@ class HardlinkIdentityScanner {
827
836
  stats = current.reader.lstatEntrySync(entry.name)
828
837
  this.options.afterEntryLstat?.(path.join(current.directory, entry.name), stats)
829
838
  } catch {
830
- this.recordEntryFailure(path.join(current.directory, entry.name), surface)
839
+ this.recordEntryFailure(path.join(current.directory, entry.name))
831
840
  completed = false
832
841
  break
833
842
  }
@@ -838,59 +847,44 @@ class HardlinkIdentityScanner {
838
847
  try {
839
848
  const confirmed = current.reader.lstatEntrySync(entry.name)
840
849
  if (!sameDirectoryEntry(stats, confirmed)) {
841
- this.recordEntryFailure(path.join(current.directory, entry.name), surface)
850
+ this.recordEntryFailure(path.join(current.directory, entry.name))
842
851
  completed = false
843
852
  break
844
853
  }
845
854
  } catch {
846
- this.recordEntryFailure(path.join(current.directory, entry.name), surface)
855
+ this.recordEntryFailure(path.join(current.directory, entry.name))
847
856
  completed = false
848
857
  break
849
858
  }
850
859
  }
851
860
  } catch {
852
- this.recordDirectoryFailure(current.directory, surface)
861
+ this.recordDirectoryFailure(current.directory)
853
862
  completed = false
854
863
  } finally {
855
864
  try {
856
865
  current.reader.finishSync()
857
866
  } catch {
858
- this.recordDirectoryFailure(current.directory, surface)
867
+ this.recordDirectoryFailure(current.directory)
859
868
  completed = false
860
869
  }
861
870
  }
862
871
  return completed
863
872
  }
864
873
 
865
- private readRootStats(directory: string, surface: 'visible' | 'denied'): Stats | undefined {
866
- try {
867
- const stats = this.options.lstat(directory)
868
- if (!stats.isDirectory()) {
869
- this.recordDirectoryFailure(directory, surface)
870
- return undefined
871
- }
872
- return stats
873
- } catch {
874
- this.recordDirectoryFailure(directory, surface)
875
- return undefined
876
- }
877
- }
878
-
879
874
  private openVerifiedDirectory(
880
875
  directory: string,
881
876
  expected: Pick<Stats, 'dev' | 'ino'>,
882
- surface: 'visible' | 'denied',
883
877
  ): OpenedHardlinkDirectory | undefined {
884
878
  let reader: HardlinkDirectory | undefined
885
879
  try {
886
880
  reader = this.options.openDirectory(directory)
887
881
  const opened = reader.statSync()
888
882
  if (!opened.isDirectory() || opened.dev !== expected.dev || opened.ino !== expected.ino) {
889
- this.recordDirectoryFailure(directory, surface)
883
+ this.recordDirectoryFailure(directory)
890
884
  reader.closeSync()
891
885
  return undefined
892
886
  }
893
- if (!this.claimDirectoryIdentity(opened, directory, surface)) {
887
+ if (!this.claimDirectoryIdentity(opened, directory)) {
894
888
  reader.closeSync()
895
889
  return undefined
896
890
  }
@@ -899,7 +893,7 @@ class HardlinkIdentityScanner {
899
893
  try {
900
894
  reader?.closeSync()
901
895
  } catch {}
902
- this.recordDirectoryFailure(directory, surface)
896
+ this.recordDirectoryFailure(directory)
903
897
  return undefined
904
898
  }
905
899
  }
@@ -908,7 +902,6 @@ class HardlinkIdentityScanner {
908
902
  parent: { directory: string; reader: HardlinkDirectory },
909
903
  name: string,
910
904
  expected: Pick<Stats, 'dev' | 'ino'>,
911
- surface: 'visible' | 'denied',
912
905
  ): OpenedHardlinkDirectory | undefined {
913
906
  const directory = path.join(parent.directory, name)
914
907
  let reader: HardlinkDirectory | undefined
@@ -916,11 +909,11 @@ class HardlinkIdentityScanner {
916
909
  reader = parent.reader.openChildSync(name)
917
910
  const opened = reader.statSync()
918
911
  if (!opened.isDirectory() || opened.dev !== expected.dev || opened.ino !== expected.ino) {
919
- this.recordDirectoryFailure(directory, surface)
912
+ this.recordDirectoryFailure(directory)
920
913
  reader.closeSync()
921
914
  return undefined
922
915
  }
923
- if (!this.claimDirectoryIdentity(opened, directory, surface)) {
916
+ if (!this.claimDirectoryIdentity(opened, directory)) {
924
917
  reader.closeSync()
925
918
  return undefined
926
919
  }
@@ -929,93 +922,69 @@ class HardlinkIdentityScanner {
929
922
  try {
930
923
  reader?.closeSync()
931
924
  } catch {}
932
- this.recordDirectoryFailure(directory, surface)
925
+ this.recordDirectoryFailure(directory)
933
926
  return undefined
934
927
  }
935
928
  }
936
929
 
937
930
  private verifyAndCloseDirectories(
938
931
  opened: Array<{ directory: string; reader: HardlinkDirectory; census: Stats }>,
939
- surface: 'visible' | 'denied',
940
932
  verify: boolean,
941
933
  ): void {
942
934
  for (const current of opened.reverse()) {
943
935
  if (verify) {
944
936
  try {
945
937
  if (!sameDirectoryCensus(current.census, current.reader.statSync())) {
946
- this.recordDirectoryFailure(current.directory, surface)
938
+ this.recordDirectoryFailure(current.directory)
947
939
  }
948
940
  } catch {
949
- this.recordDirectoryFailure(current.directory, surface)
941
+ this.recordDirectoryFailure(current.directory)
950
942
  }
951
943
  }
952
944
  try {
953
945
  current.reader.closeSync()
954
946
  } catch {
955
- this.recordDirectoryFailure(current.directory, surface)
947
+ this.recordDirectoryFailure(current.directory)
956
948
  }
957
949
  }
958
950
  }
959
951
 
960
- private recordDirectoryFailure(directory: string, surface: 'visible' | 'denied'): void {
961
- const reason = `${surface} hardlink identity directory could not be read: ${directory}`
962
- if (surface === 'visible') this.visibleFailure = reason
963
- else this.deniedFailure = reason
952
+ private recordDirectoryFailure(directory: string): void {
953
+ this.failure = `denied hardlink identity directory could not be read: ${directory}`
964
954
  }
965
955
 
966
- private consumeEntry(surface: 'visible' | 'denied'): boolean {
956
+ private consumeEntry(): boolean {
967
957
  if (this.remaining > 0) {
968
958
  this.remaining -= 1
969
959
  return true
970
960
  }
971
- const reason = `hardlink identity entry limit exceeded (${this.options.maxEntries})`
972
- if (surface === 'visible') this.visibleFailure = reason
973
- else this.deniedFailure = reason
961
+ this.failure = `hardlink identity entry limit exceeded (${this.options.maxEntries})`
974
962
  return false
975
963
  }
976
964
 
977
- private recordEntryFailure(candidate: string, surface: 'visible' | 'denied'): void {
978
- const reason = `${surface} hardlink identity could not be read: ${candidate}`
979
- if (surface === 'visible') this.visibleFailure = reason
980
- else this.deniedFailure = reason
965
+ private recordEntryFailure(candidate: string): void {
966
+ this.failure = `denied hardlink identity could not be read: ${candidate}`
981
967
  }
982
968
 
983
- private claimDirectoryIdentity(
984
- identity: Pick<Stats, 'dev' | 'ino'>,
985
- directory: string,
986
- surface: 'visible' | 'denied',
987
- ): boolean {
969
+ private claimDirectoryIdentity(identity: Pick<Stats, 'dev' | 'ino'>, directory: string): boolean {
988
970
  const key = identityKey(identity)
989
971
  if (this.openedDirectories.has(key)) {
990
- this.recordDirectoryFailure(directory, surface)
972
+ this.recordDirectoryFailure(directory)
991
973
  return false
992
974
  }
993
975
  this.openedDirectories.add(key)
994
976
  return true
995
977
  }
996
978
 
997
- private claimEntryObservation(
998
- directoryIdentity: Pick<Stats, 'dev' | 'ino'>,
999
- name: string,
1000
- surface: 'visible' | 'denied',
1001
- ): boolean {
979
+ private claimEntryObservation(directoryIdentity: Pick<Stats, 'dev' | 'ino'>, name: string): boolean {
1002
980
  const key = `${identityKey(directoryIdentity)}:${JSON.stringify(name)}`
1003
981
  if (this.observedEntries.has(key)) {
1004
- this.recordEntryFailure(name, surface)
982
+ this.recordEntryFailure(name)
1005
983
  return false
1006
984
  }
1007
985
  this.observedEntries.add(key)
1008
986
  return true
1009
987
  }
1010
-
1011
- private recordVisibleFile(stats: Stats): void {
1012
- const key = identityKey(stats)
1013
- this.visibleCounts.set(key, (this.visibleCounts.get(key) ?? 0) + 1)
1014
- }
1015
-
1016
- private recordDeniedFile(stats: Stats, deniedRoot: string): void {
1017
- this.deniedIdentities.set(identityKey(stats), deniedRoot)
1018
- }
1019
988
  }
1020
989
 
1021
990
  function identityKey(identity: Pick<Stats, 'dev' | 'ino'>): string {
@@ -1040,10 +1009,6 @@ function sameDirectoryCensus(before: Stats, after: Stats): boolean {
1040
1009
  )
1041
1010
  }
1042
1011
 
1043
- function isPathAtOrBelow(candidate: string, root: string): boolean {
1044
- return candidate === root || candidate.startsWith(`${root}${path.sep}`)
1045
- }
1046
-
1047
1012
  function isPathKey(key: string | undefined): boolean {
1048
1013
  return key !== undefined && (PATH_KEYS.test(key) || CAMEL_PATH_KEYS.test(key))
1049
1014
  }