dsh-vision-router 3.2.0 → 3.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.
Files changed (56) hide show
  1. package/README.md +6 -6
  2. package/README.zh.md +21 -6
  3. package/THIRD_PARTY_NOTICES.md +167 -0
  4. package/assets/licenses/apache-2.0.txt +202 -0
  5. package/assets/licenses/llvm-mingw-LICENSE.txt +279 -0
  6. package/assets/licenses/mingw-w64-runtime-COPYING.txt +265 -0
  7. package/assets/licenses/moxcms-BSD-3-Clause.txt +26 -0
  8. package/assets/licenses/ncnn-LICENSE.txt +86 -0
  9. package/assets/licenses/roots-BSD-2-Clause.txt +24 -0
  10. package/assets/licenses/unicode-ident-Unicode-3.0.txt +39 -0
  11. package/assets/licenses/vtracer-MIT.txt +25 -0
  12. package/assets/licenses/zlib-rs-Zlib.txt +19 -0
  13. package/docs/architecture/dsh-support-window.md +25 -6
  14. package/docs/compatibility/dsh-0.2.2-rc2-audit.md +40 -0
  15. package/docs/compatibility/dsh-0.2.2-rc3-audit.md +131 -0
  16. package/docs/compliance/vtracer-alpha4-provenance.md +47 -0
  17. package/docs/compliance/vtracer-alpha4-reconstructed-Cargo.lock +567 -0
  18. package/docs/compliance/vtracer-alpha4-rust-inventory.json +421 -0
  19. package/docs/compliance/vtracer-alpha4-rust-license-texts.md +13018 -0
  20. package/docs/releases/v3.2.1.md +21 -0
  21. package/docs/releases/v3.2.2.md +25 -0
  22. package/docs/releases/v3.3.0.md +34 -0
  23. package/docs/remote-settings.md +83 -8
  24. package/index.js +280 -169
  25. package/lib/client.js +17 -17
  26. package/lib/core-primitives.js +155 -214
  27. package/lib/dsh-support-window.js +6 -3
  28. package/lib/native-ocr-annotation-risk.js +33 -0
  29. package/lib/native-ocr-canary.js +156 -0
  30. package/lib/native-ocr-chemistry-review.js +37 -0
  31. package/lib/native-ocr-document.js +243 -0
  32. package/lib/native-ocr-installation.js +17 -0
  33. package/lib/native-ocr-integrity.js +93 -0
  34. package/lib/native-ocr-question-labels.js +167 -0
  35. package/lib/native-ocr-supervisor.js +268 -0
  36. package/lib/remote-settings-bridge.js +93 -57
  37. package/lib/remote-settings-risk-client-boundary.js +2 -2
  38. package/lib/runtime-reliability.js +7 -0
  39. package/lib/session-query-readers.js +8 -2
  40. package/lib/session-vision-attachment-index.js +61 -14
  41. package/lib/settings-client-017-compat.js +4 -2
  42. package/lib/settings-ia-client-prelude.js +26 -10
  43. package/lib/tesseract-image-preprocess.js +35 -0
  44. package/lib/trace-vtracer-process-runtime.js +133 -0
  45. package/lib/trace-vtracer-process.js +43 -0
  46. package/lib/trace-vtracer-worker.js +163 -0
  47. package/lib/vision-attachment-handle-runtime.js +11 -2
  48. package/lib/vision-backend-runtime-policy.js +5 -3
  49. package/lib/vision-image-input-reader.js +67 -0
  50. package/lib/vision-image-input-verdict.js +24 -3
  51. package/lib/vision-tool-runtime-boundary.js +7 -10
  52. package/lib/vision-tool-stage.js +58 -0
  53. package/lib/web/dsh-settings-017-root-transport.js +52 -6
  54. package/lib/web-capability-boundary.js +6 -0
  55. package/package.json +10 -6
  56. package/scripts/install-native-ocr.mjs +144 -0
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Conservative question-heading recovery for photographed Chinese textbooks.
3
+ * Runs only on OCR observations: no image-specific expected answers are injected.
4
+ */
5
+ const chinese = String.raw`\\p{Script=Han}`;
6
+ const headingPattern = /^\s*(1[0-2]|[1-9])[..、]\s*(?=\S)/u;
7
+ const attachedPattern = /^\s*(1[0-2]|[1-9])\s*(\p{Script=Han}.{3,})/u;
8
+ const marginPattern = /^\s*(1[0-2]|[1-9])([..、]?)\s*(\p{Script=Han}.{3,})/u;
9
+ const compact = (s) => s.normalize('NFKC').replace(/∆/gu,'Δ')
10
+ .replace(/[^\p{L}\p{N}]/gu, '').toLowerCase();
11
+ const midpoint = (box) => (box[1] + box[3]) / 2;
12
+ function oneCharacterDisagreement(a,b) {
13
+ if (a.length < 5 || b.length < 5) return false
14
+ const n = Math.min(7,a.length,b.length)
15
+ let edits=0
16
+ for(let i=0;i<n;i++) if(a[i]!==b[i]) edits++
17
+ return edits===1
18
+ }
19
+
20
+ function prefixEditDistance(a,b) {
21
+ const n=Math.min(7,Array.from(a).length,Array.from(b).length)
22
+ const x=Array.from(a).slice(0,n), y=Array.from(b).slice(0,n)
23
+ const dist=Array.from({length:y.length+1},(_,i)=>i)
24
+ for(const c of x) {
25
+ const next=[dist[0]+1]
26
+ for(let j=0;j<y.length;j++) {
27
+ next.push(Math.min(next[j]+1,dist[j+1]+1,dist[j]+(c===y[j]?0:1)))
28
+ }
29
+ for(let j=0;j<next.length;j++)dist[j]=next[j]
30
+ }
31
+ return dist.at(-1)
32
+ }
33
+ function anchorNumber(region) {
34
+ const m = String(region.text ?? '').match(headingPattern);
35
+ return m ? Number(m[1]) : null;
36
+ }
37
+
38
+ function isSandwichedBetweenConsecutiveHeadings(regions,target,n) {
39
+ const y=midpoint(target.box)
40
+ return regions.some(r=>midpoint(r.box)<y-8 && anchorNumber(r)===n-1) &&
41
+ regions.some(r=>midpoint(r.box)>y+8 && anchorNumber(r)===n+1)
42
+ }
43
+ function sequentiallyPlausible(regions, target, value) {
44
+ const y = midpoint(target.box);
45
+ const earlier = regions.filter(r => r !== target && midpoint(r.box) < y - 8)
46
+ .map(anchorNumber).filter(Number.isFinite);
47
+ const later = regions.filter(r => r !== target && midpoint(r.box) > y + 8)
48
+ .map(anchorNumber).filter(Number.isFinite);
49
+ const previous = earlier.length ? Math.max(...earlier) : null;
50
+ const next = later.length ? Math.min(...later) : null;
51
+ // Do not manufacture a number from an isolated OCR hallucination.
52
+ // Require a preceding observed heading, then strictly increasing order.
53
+ return previous !== null && value > previous &&
54
+ (next === null || value < next) &&
55
+ (next === null || value - previous <= 3);
56
+ }
57
+
58
+ function reconcileOneMissingFormulaDigit(row,n,marginBody,label) {
59
+ // Only a single missing digit may be inserted into an observed chemical
60
+ // formula. Never invent a decimal point or substitute an uncertain digit.
61
+ if(label.confidence < 0.95 || row.confidence < 0.90) return false
62
+ const alternatives=marginBody.match(/[A-Z]\d+[A-Z]\d+(?:[A-Z]\d*)*/g) ?? []
63
+ for(const observed of alternatives) {
64
+ if(row.text.includes(observed)) continue
65
+ for(let k=0;k<observed.length;k++) {
66
+ if(!/[0-9]/.test(observed[k]))continue
67
+ const missing=observed.slice(0,k)+observed.slice(k+1)
68
+ const occurrence=row.text.indexOf(missing)
69
+ if(occurrence<0 || row.text.indexOf(missing,occurrence+1)>=0)continue
70
+ const proposed=row.text.slice(0,occurrence)+observed+row.text.slice(occurrence+missing.length)
71
+ const reference=compact(n+'. '+marginBody)
72
+ if(reference.length < 10 || !compact(proposed).startsWith(reference.slice(0,Math.min(15,reference.length)))) continue
73
+ row.text=proposed
74
+ row.uncertain=true
75
+ row.formulaDigitReconciled=true
76
+ row.formulaRecoveryEvidence={from:missing,to:observed,sourceConfidence:label.confidence}
77
+ return true
78
+ }
79
+ }
80
+ return false
81
+ }
82
+ export function recoverPhotoQuestionHeadings(regions, leftMarginLabels = []) {
83
+ const recovered = [];
84
+ const rejected = [];
85
+ const byY = [...leftMarginLabels].sort((a, b) => midpoint(a.box) - midpoint(b.box));
86
+
87
+ // Recover a missing separator only when digit + Chinese text are already
88
+ // present in the main OCR line and surrounding headings agree.
89
+ for (const row of regions) {
90
+ if (anchorNumber(row) !== null) continue;
91
+ const m = String(row.text ?? '').match(attachedPattern);
92
+ if (!m) continue; // 2C8H18 is a chemical coefficient, NOT question 2
93
+ const n = Number(m[1]);
94
+ if (!sequentiallyPlausible(regions, row, n)) {
95
+ rejected.push({ number: n, reason: 'heading_sequence_conflict' });
96
+ continue;
97
+ }
98
+ row.text = n + '. ' + m[2];
99
+ row.uncertain = true;
100
+ row.numberRecovered = true;
101
+ row.numberRecoveryMethod = 'inline_digit_context';
102
+ recovered.push(n);
103
+ }
104
+
105
+ for (const label of byY) {
106
+ if (!Number.isFinite(label?.confidence) || label.confidence < 0.85 ||
107
+ !Array.isArray(label.box) || label.box.length !== 4) continue;
108
+ const m = String(label.text ?? '').match(marginPattern);
109
+ if (!m) continue;
110
+ const n = Number(m[1]);
111
+ const fragment = compact(m[3]).slice(0, 7);
112
+ if (fragment.length < 4) continue;
113
+ const y = midpoint(label.box);
114
+ const hits = regions.filter(row => {
115
+ if (!Array.isArray(row.box) || row.box.length !== 4) return false;
116
+ if (Math.abs(midpoint(row.box) - y) > 14) return false;
117
+ const body = compact(row.text).replace(/^\d{1,2}/, '');
118
+ return body.startsWith(fragment.slice(0, 4)) ||
119
+ body.includes(fragment.slice(0, 6)) ||
120
+ (row.confidence >= 0.85 && (
121
+ oneCharacterDisagreement(body,fragment) ||
122
+ (isSandwichedBetweenConsecutiveHeadings(regions,row,n) &&
123
+ prefixEditDistance(body,fragment)<=2)
124
+ ));
125
+ });
126
+ if (hits.length !== 1) continue;
127
+ const target = hits[0];
128
+ const existing = anchorNumber(target);
129
+ if (existing === n) continue;
130
+ if (existing !== null) {
131
+ target.uncertain = true;
132
+ rejected.push({ number: n, reason: 'observed_number_conflict' });
133
+ continue;
134
+ }
135
+ if (!sequentiallyPlausible(regions, target, n)) {
136
+ rejected.push({ number: n, reason: 'heading_sequence_conflict' });
137
+ continue;
138
+ }
139
+ // A number without a printed/recognized period can be accepted only
140
+ // with monotonic neighboring evidence, and remains uncertain.
141
+ const original = target.text;
142
+ const fuzzyUsed = !compact(original).replace(/^\d{1,2}/,'')
143
+ .startsWith(fragment.slice(0,4));
144
+ target.text = n + '. ' + original.trim();
145
+ target.uncertain = target.uncertain || !m[2] || fuzzyUsed;
146
+ target.numberRecoveryDisagreement = fuzzyUsed;
147
+ target.numberRecovered = true;
148
+ target.numberRecoveryMethod = 'margin_correlated';
149
+ target.numberRawEvidence = label.text;
150
+ recovered.push(n);
151
+ }
152
+ const formulaReconciled=[]
153
+ // Compare the beginning of a line with the independent high-resolution
154
+ // left-margin view, including lines whose heading was already present.
155
+ for(const label of byY) {
156
+ const m=String(label.text??'').match(marginPattern)
157
+ if(!m)continue
158
+ const n=Number(m[1]),y=midpoint(label.box)
159
+ const hits=regions.filter(row=>anchorNumber(row)===n &&
160
+ Math.abs(midpoint(row.box)-y)<=14)
161
+ if(hits.length!==1)continue
162
+ if(reconcileOneMissingFormulaDigit(hits[0],n,m[3],label)){
163
+ formulaReconciled.push(n)
164
+ }
165
+ }
166
+ return { regions, recovered, rejected, formulaReconciled };
167
+ }
@@ -0,0 +1,268 @@
1
+ import { spawn } from 'node:child_process'
2
+ import { isAbsolute } from 'node:path'
3
+ import { performance } from 'node:perf_hooks'
4
+
5
+ const DEFAULT_TIMEOUT_MS = 12_000
6
+ const MAX_TIMEOUT_MS = 30_000
7
+ const MAX_INPUT_BYTES = 24 * 1024 * 1024
8
+ const MAX_OUTPUT_BYTES = 2 * 1024 * 1024
9
+ const MAX_IMAGE_PIXELS = 4_000_000
10
+ const MAX_IMAGE_SIDE = 4096
11
+ const MAX_ACTIVE_WORKERS = 2
12
+ const MAX_QUEUED_WORKERS = 16
13
+ let activeWorkers = 0
14
+ const waitingWorkers = []
15
+
16
+ function releaseNextWorker() {
17
+ while (waitingWorkers.length > 0) {
18
+ const next = waitingWorkers.shift()
19
+ if (next.finished) continue
20
+ next.finished = true
21
+ clearTimeout(next.timer)
22
+ next.signal?.removeEventListener('abort', next.abort)
23
+ activeWorkers++
24
+ next.resolve(makeReleaseWorker())
25
+ break
26
+ }
27
+ }
28
+ function makeReleaseWorker() {
29
+ let used = false
30
+ return () => {
31
+ if (used) return
32
+ used = true
33
+ activeWorkers--
34
+ releaseNextWorker()
35
+ }
36
+ }
37
+ function acquireWorker(signal, waitMs) {
38
+ if (signal?.aborted) return Promise.reject(error('OCR_CANCELLED', 'OCR cancelled'))
39
+ if (activeWorkers < MAX_ACTIVE_WORKERS) {
40
+ activeWorkers++
41
+ return Promise.resolve(makeReleaseWorker())
42
+ }
43
+ if (waitingWorkers.length >= MAX_QUEUED_WORKERS) {
44
+ return Promise.reject(error('OCR_BUSY', 'local OCR worker queue is full'))
45
+ }
46
+ return new Promise((resolve, reject) => {
47
+ const task = { resolve, reject, signal, finished: false, timer: undefined, abort: undefined }
48
+ const fail = (code, message) => {
49
+ if (task.finished) return
50
+ task.finished = true
51
+ clearTimeout(task.timer)
52
+ signal?.removeEventListener('abort', task.abort)
53
+ const index = waitingWorkers.indexOf(task)
54
+ if (index !== -1) waitingWorkers.splice(index, 1)
55
+ reject(error(code, message))
56
+ }
57
+ task.abort = () => fail('OCR_CANCELLED', 'OCR cancelled')
58
+ task.timer = setTimeout(() => fail('OCR_QUEUE_TIMEOUT', 'local OCR queue deadline exhausted'), waitMs)
59
+ task.timer.unref?.()
60
+ waitingWorkers.push(task)
61
+ signal?.addEventListener('abort', task.abort, { once: true })
62
+ if (signal?.aborted) task.abort()
63
+ })
64
+ }
65
+
66
+ /**
67
+ * In-memory, validated RGB24 transport for the native worker.
68
+ * Image decoders remain Host-owned (sharp); native inference never parses
69
+ * arbitrary PNG/JPEG/WebP bytes or reads user-controlled filesystem paths.
70
+ */
71
+ export function encodeNativeOcrRgbFrame(rgb, width, height) {
72
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) ||
73
+ width < 1 || height < 1 ||
74
+ width > MAX_IMAGE_SIDE || height > MAX_IMAGE_SIDE ||
75
+ width * height > MAX_IMAGE_PIXELS) {
76
+ throw error('OCR_INVALID_DIMENSIONS', 'invalid local OCR image dimensions')
77
+ }
78
+ if (!(rgb instanceof Uint8Array) || rgb.byteLength !== width * height * 3) {
79
+ throw error('OCR_INVALID_RGB', 'invalid local OCR RGB pixel buffer')
80
+ }
81
+ const frame = Buffer.allocUnsafe(20 + rgb.byteLength)
82
+ frame.write('DVR3OCR1', 0, 'ascii')
83
+ frame.writeUInt32LE(width, 8)
84
+ frame.writeUInt32LE(height, 12)
85
+ frame.writeUInt32LE(rgb.byteLength, 16)
86
+ frame.set(rgb, 20)
87
+ return frame
88
+ }
89
+
90
+ export class LocalOcrProcessError extends Error {
91
+ constructor(code, message) {
92
+ super(message)
93
+ this.name = 'LocalOcrProcessError'
94
+ this.code = code
95
+ }
96
+ }
97
+
98
+ function error(code, message) {
99
+ return new LocalOcrProcessError(code, message)
100
+ }
101
+
102
+ function isValidOcrResult(value) {
103
+ return value !== null &&
104
+ typeof value === 'object' &&
105
+ !Array.isArray(value) &&
106
+ typeof value.text === 'string' &&
107
+ value.text.length <= 250_000 &&
108
+ (value.confidence === undefined || (
109
+ typeof value.confidence === 'number' &&
110
+ Number.isFinite(value.confidence) &&
111
+ value.confidence >= 0 && value.confidence <= 1
112
+ )) &&
113
+ (value.uncertain === undefined || typeof value.uncertain === 'boolean')
114
+ }
115
+
116
+ /**
117
+ * Experimental stability boundary. Does not enable/replace the production OCR engine.
118
+ * The caller must provide a trusted, package-owned absolute executable path.
119
+ * The native executable reads image bytes from stdin and writes one JSON result to stdout.
120
+ *
121
+ * No shell, no image path, no implicit cloud fallback, no inherited credentials.
122
+ */
123
+ function runIsolatedLocalOcrProcess(imageBytes, {
124
+ executable,
125
+ args = [],
126
+ timeoutMs = DEFAULT_TIMEOUT_MS,
127
+ signal,
128
+ maxOutputBytes = MAX_OUTPUT_BYTES,
129
+ } = {}) {
130
+ if (!Buffer.isBuffer(imageBytes) || imageBytes.length === 0 || imageBytes.length > MAX_INPUT_BYTES) {
131
+ return Promise.reject(error('OCR_INVALID_INPUT', 'invalid OCR image size'))
132
+ }
133
+ if (typeof executable !== 'string' || !isAbsolute(executable) || executable.includes('\0')) {
134
+ return Promise.reject(error('OCR_UNTRUSTED_RUNNER', 'OCR executable path must be absolute and trusted'))
135
+ }
136
+ if (!Array.isArray(args) || args.some((value) => typeof value !== 'string' || value.includes('\0'))) {
137
+ return Promise.reject(error('OCR_INVALID_ARGUMENTS', 'invalid OCR executable arguments'))
138
+ }
139
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
140
+ return Promise.reject(error('OCR_INVALID_TIMEOUT', 'invalid OCR timeout'))
141
+ }
142
+ const deadlineMs = Math.max(1, Math.min(Math.floor(timeoutMs), MAX_TIMEOUT_MS))
143
+ const limit = Number.isSafeInteger(maxOutputBytes) && maxOutputBytes > 0
144
+ ? Math.min(maxOutputBytes, MAX_OUTPUT_BYTES)
145
+ : MAX_OUTPUT_BYTES
146
+ if (signal?.aborted) return Promise.reject(error('OCR_CANCELLED', 'OCR cancelled'))
147
+
148
+ return new Promise((resolve, reject) => {
149
+ let done = false
150
+ let child
151
+ let timer
152
+ let size = 0
153
+ const buffers = []
154
+ let spawnFailure = false
155
+
156
+ const terminate = () => {
157
+ try { child?.kill('SIGKILL') } catch {}
158
+ }
159
+ const abort = () => {
160
+ terminate()
161
+ settle(error('OCR_CANCELLED', 'OCR cancelled'))
162
+ }
163
+ const settle = (failure, result) => {
164
+ if (done) return
165
+ done = true
166
+ if (timer !== undefined) clearTimeout(timer)
167
+ signal?.removeEventListener('abort', abort)
168
+ if (failure) reject(failure)
169
+ else resolve(result)
170
+ }
171
+
172
+ try {
173
+ // Limit the environment to OS runtime variables, not secrets or user-provided
174
+ // proxy/configuration variables. The executable MUST be bundled and self-contained.
175
+ const env = {}
176
+ for (const name of ['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'TMPDIR', 'HOME', 'LANG', 'LC_ALL']) {
177
+ if (typeof process.env[name] === 'string') env[name] = process.env[name]
178
+ }
179
+ // Linux worker registers PR_SET_PDEATHSIG before reading RGB input.
180
+ // A non-secret expected PID closes the fork/exec vs parent-SIGKILL race.
181
+ if (process.platform === 'linux') env.DVR_OCR_PARENT_PID = String(process.pid)
182
+ child = spawn(executable, args, {
183
+ shell: false,
184
+ windowsHide: true,
185
+ stdio: ['pipe', 'pipe', 'pipe'],
186
+ env,
187
+ })
188
+ } catch {
189
+ settle(error('OCR_SPAWN_FAILED', 'unable to start local OCR worker'))
190
+ return
191
+ }
192
+
193
+ signal?.addEventListener('abort', abort, { once: true })
194
+ timer = setTimeout(() => {
195
+ terminate()
196
+ settle(error('OCR_TIMEOUT', 'local OCR worker exceeded its deadline'))
197
+ }, deadlineMs)
198
+ timer.unref?.()
199
+
200
+ child.on('error', () => {
201
+ spawnFailure = true
202
+ settle(error('OCR_SPAWN_FAILED', 'unable to start local OCR worker'))
203
+ })
204
+ // Some platforms write EPIPE after the worker exits early. Its exit status,
205
+ // not this stream error, determines the OCR result.
206
+ child.stdin.on('error', () => {})
207
+ child.stderr.on('data', () => {}) // Never leak native stderr or sensitive images.
208
+ child.stdout.on('data', (chunk) => {
209
+ if (done) return
210
+ size += chunk.length
211
+ if (size > limit) {
212
+ terminate()
213
+ settle(error('OCR_OUTPUT_LIMIT', 'local OCR output exceeded the size limit'))
214
+ return
215
+ }
216
+ buffers.push(chunk)
217
+ })
218
+ child.on('close', (exitCode, exitSignal) => {
219
+ if (done) return
220
+ if (spawnFailure || exitCode !== 0 || exitSignal !== null) {
221
+ settle(error('OCR_WORKER_FAILED', 'local OCR worker exited unsuccessfully'))
222
+ return
223
+ }
224
+ try {
225
+ const result = JSON.parse(Buffer.concat(buffers, size).toString('utf8'))
226
+ if (!isValidOcrResult(result)) {
227
+ settle(error('OCR_INVALID_RESULT', 'local OCR returned an invalid result'))
228
+ return
229
+ }
230
+ settle(undefined, { ...result, engine: 'ncnn' })
231
+ } catch {
232
+ settle(error('OCR_INVALID_RESULT', 'local OCR returned invalid JSON'))
233
+ }
234
+ })
235
+ if (signal?.aborted) {
236
+ abort()
237
+ return
238
+ }
239
+ child.stdin.end(imageBytes)
240
+ })
241
+ }
242
+
243
+ /**
244
+ * At most two native model instances can execute simultaneously. All other
245
+ * requests wait in a bounded, abortable FIFO. This protects the Host against
246
+ * a burst of 100+ concurrent images creating hundreds of native model copies.
247
+ */
248
+ export async function runIsolatedLocalOcr(imageBytes, options = {}) {
249
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS
250
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
251
+ throw error('OCR_INVALID_TIMEOUT', 'invalid OCR timeout')
252
+ }
253
+ const budgetMs = Math.max(1, Math.min(Math.floor(timeoutMs), MAX_TIMEOUT_MS))
254
+ const started = performance.now()
255
+ const release = await acquireWorker(options.signal, budgetMs)
256
+ try {
257
+ const remainingMs = budgetMs - (performance.now() - started)
258
+ if (remainingMs <= 0) {
259
+ throw error('OCR_QUEUE_TIMEOUT', 'local OCR queue deadline exhausted')
260
+ }
261
+ return await runIsolatedLocalOcrProcess(imageBytes, {
262
+ ...options,
263
+ timeoutMs: remainingMs,
264
+ })
265
+ } finally {
266
+ release()
267
+ }
268
+ }
@@ -11,53 +11,71 @@ export const REMOTE_SETTINGS_CHANNEL = '/vision-router-settings'
11
11
  export const REMOTE_SETTINGS_PERMISSION = 'allowRemoteSettings'
12
12
  export const REMOTE_SETTINGS_AUTHORIZE_ENDPOINT = 'authorize'
13
13
 
14
- // Remote DSH pages are deliberately less privileged than the loopback settings UI.
15
- // This is a capability allow-list, not a blacklist: a future Config field is local-only
16
- // until it is reviewed and explicitly added here.
17
- export const REMOTE_SETTINGS_READABLE_FIELDS = Object.freeze([
18
- 'providers',
19
- 'routing',
20
- 'reverseRouting',
21
- 'routingMode',
22
- 'routingPreference',
23
- 'backgroundBenchmarking',
24
- 'textProvider',
25
- 'tool',
26
- 'structuredVisionBootstrap',
27
- 'visionDepth',
28
- 'visionDepthMaxCalls',
29
- 'guidanceOverrides',
30
- 'progressiveTools',
31
- 'autoActivateOnImage',
32
- 'extraVisionModels',
33
- 'rewriteImages',
34
- 'downscale',
35
- 'downscaleMaxPixels',
36
- 'cache',
37
- 'cacheTtlSeconds',
38
- 'cacheMaxEntries',
39
- 'timeoutMs',
40
- 'visionTaskTimeoutMs',
41
- 'visionTurnBudgetMs',
42
- 'ocrTimeoutMs',
43
- 'ocrEngine',
44
- 'localOnlyVision',
45
- 'freeFallback',
46
- 'freeCloudFirst',
47
- 'autoWrapProviders',
48
- 'wrappedProviders',
49
- 'onboardingSeen',
50
- ])
14
+ // Remote settings are an opt-in view of the *same* Host-owned DVR namespace.
15
+ // No second field allow-list: after the once-per-profile risk acknowledgement,
16
+ // DSH-authenticated clients can edit any ordinary field admitted by the Host
17
+ // schema. The permission flag itself is managed through the explicit
18
+ // risk-confirmation/revocation flow, not as an ordinary settings mutation.
19
+ const REDACTED = '[REDACTED]'
20
+ const SECRET_KEY = /(?:password|passwd|private.?key|(?:api|access|refresh).?token|token|secret|credential|api.?key|authorization)/i
51
21
 
52
- // Privacy-critical local-only policy is visible remotely for truthful status,
53
- // but only a loopback/local Settings page may disable it. This prevents an
54
- // already-authorized remote Settings session from re-enabling visual egress.
55
- export const REMOTE_SETTINGS_MUTABLE_FIELDS = Object.freeze(
56
- REMOTE_SETTINGS_READABLE_FIELDS.filter((field) => field !== 'localOnlyVision'),
57
- )
22
+ function redactUrl(value) {
23
+ if (typeof value !== 'string' || !/^[a-z][a-z\d+.-]*:\/\//i.test(value)) return value
24
+ try {
25
+ const url = new URL(value)
26
+ let changed = false
27
+ if (url.username || url.password) {
28
+ url.username = REDACTED
29
+ url.password = REDACTED
30
+ changed = true
31
+ }
32
+ for (const key of url.searchParams.keys()) {
33
+ if (SECRET_KEY.test(key)) { url.searchParams.set(key, REDACTED); changed = true }
34
+ }
35
+ return changed ? url.toString() : value
36
+ } catch { return value }
37
+ }
58
38
 
59
- const readable = new Set(REMOTE_SETTINGS_READABLE_FIELDS)
60
- const mutable = new Set(REMOTE_SETTINGS_MUTABLE_FIELDS)
39
+ function containsRedactedCredential(value, field = '') {
40
+ // A remote descriptor may contain a redacted proxy or endpoint URL. If a
41
+ // user edits that form field and re-saves it, a masked placeholder is NOT a
42
+ // credential and must never overwrite the original value in Host settings.
43
+ if (field !== 'apiKeyEnv' && field !== 'credentialRef' &&
44
+ SECRET_KEY.test(field) && value === REDACTED) return true
45
+ if (typeof value === 'string' && /^[a-z][a-z\d+.-]*:\/\//i.test(value)) {
46
+ try {
47
+ const url = new URL(value)
48
+ if ([url.username, url.password].some((part) => {
49
+ try { return decodeURIComponent(part) === REDACTED } catch { return true }
50
+ })) return true
51
+ for (const [key, part] of url.searchParams) {
52
+ if (SECRET_KEY.test(key) && part === REDACTED) return true
53
+ }
54
+ } catch { /* Leave malformed URLs to the Host schema. */ }
55
+ }
56
+ if (Array.isArray(value)) return value.some((part) => containsRedactedCredential(part))
57
+ if (value && typeof value === 'object') {
58
+ return Object.entries(value).some(([key, part]) => containsRedactedCredential(part, key))
59
+ }
60
+ return false
61
+ }
62
+
63
+ function safeSettingsValue(value, field = '') {
64
+ // The DSH descriptor is already Host-redacted. These additional guards
65
+ // cover plain-text proxy URL userinfo and future nested secret-shaped fields
66
+ // without reintroducing a field-level *capability* allow-list.
67
+ if (field !== 'apiKeyEnv' && field !== 'credentialRef' && SECRET_KEY.test(field)) return REDACTED
68
+ if (Array.isArray(value)) return value.map((item) => safeSettingsValue(item))
69
+ if (value && typeof value === 'object') {
70
+ const out = {}
71
+ for (const [key, item] of Object.entries(value)) {
72
+ if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue
73
+ out[key] = safeSettingsValue(item, key)
74
+ }
75
+ return out
76
+ }
77
+ return redactUrl(value)
78
+ }
61
79
 
62
80
  function badRequest(message) {
63
81
  return { ok: false, error: { code: 'bad-request', message, details: { issues: [] } } }
@@ -67,12 +85,12 @@ function settingsRejected(message) {
67
85
  return { ok: false, error: { code: 'settings-rejected', message, details: { ns: SETTINGS_NS } } }
68
86
  }
69
87
 
70
- function project(value, fields = readable) {
88
+ function project(value) {
71
89
  if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined
72
- const out = Object.create(null)
73
- for (const field of fields) {
74
- if (Object.prototype.hasOwnProperty.call(value, field)) out[field] = structuredClone(value[field])
75
- }
90
+ const out = safeSettingsValue(value)
91
+ // Permission is not a normal remote-editable field. The authenticated
92
+ // describe envelope already exposes enabled/disabled state explicitly.
93
+ delete out[REMOTE_SETTINGS_PERMISSION]
76
94
  return out
77
95
  }
78
96
 
@@ -112,12 +130,15 @@ function validateMutation(payload, descriptor) {
112
130
  if (!Array.isArray(op.path) || op.path.length !== 1 || typeof op.path[0] !== 'string' || op.path[0] === '') return { error: 'remote settings may edit one top-level field at a time' }
113
131
  const field = op.path[0]
114
132
  if (field === '__proto__' || field === 'prototype' || field === 'constructor') return { error: 'unsafe settings field name' }
115
- if (!mutable.has(field)) return { error: 'Vision Router setting ' + JSON.stringify(field) + ' is local-only' }
133
+ if (field === REMOTE_SETTINGS_PERMISSION) return { error: 'Remote permission is managed by risk confirmation and local revocation' }
116
134
  const value = descriptor && descriptor.value
117
135
  if (typeof value !== 'object' || value === null || Array.isArray(value) || !Object.prototype.hasOwnProperty.call(value, field)) return { error: 'unknown Vision Router settings field ' + JSON.stringify(field) }
118
136
  if (op.op === 'set') {
119
137
  const budgetError = remoteConfigFieldAdmissionError(field, op.value)
120
138
  if (budgetError) return { error: budgetError }
139
+ if (containsRedactedCredential(op.value, field)) {
140
+ return { error: 'masked credentials cannot be saved; re-enter the credential value before editing this field' }
141
+ }
121
142
  }
122
143
  return { ops: [op], expectedRevision }
123
144
  }
@@ -144,10 +165,10 @@ function conflictResult(error) {
144
165
  * `acceptedRisk === true` is an acknowledgement bit for the UX flow, not an
145
166
  * authentication/authorization credential, proof of local presence, challenge
146
167
  * response, one-time secret, or administrator approval. Do not strengthen this
147
- * endpoint into a second security gate unless the product policy is explicitly
148
- * changed. The real capability boundary is the narrow remote mutable-field
149
- * allow-list plus the fields that remain local-only. The historical RPC/function
150
- * name uses "authorize"; do not infer security-auth semantics from that name.
168
+ * endpoint into a second authentication mechanism. Security is enforced by
169
+ * the DSH-authenticated Connection carrier, the persisted opt-in, the Host
170
+ * schema and resource bounds. Sensitive stored values are redacted on reads.
171
+ * The historical RPC name "authorize" does not mean user authentication.
151
172
  */
152
173
  export async function authorizeRemoteSettingsAfterRiskConfirmation(settings, payload, logger) {
153
174
  if (!payload || typeof payload !== 'object' || Array.isArray(payload) || payload.acceptedRisk !== true) {
@@ -248,8 +269,23 @@ export function createVisionRouterRemoteSettingsHandler(settings, logger) {
248
269
  logger?.warn?.('vision-router: remote settings mutation rejected: %s', error?.message ?? String(error))
249
270
  return settingsRejected(error?.message ?? 'Vision Router settings write was rejected')
250
271
  }
251
- try { return { ok: true, value: accessSnapshot(settings) } }
252
- catch { return settingsRejected('Vision Router settings changed but the refreshed view could not be read') }
272
+ try {
273
+ const after = namespaceDescriptor(settings)
274
+ const result = accessSnapshot(settings)
275
+ if (validated.ops[0].op === 'set' && result.enabled && after) {
276
+ const field = validated.ops[0].path[0]
277
+ const requested = validated.ops[0].value
278
+ const persisted = after.value?.[field]
279
+ // The Host may normalize values. Only echo what this caller already
280
+ // provided if the authoritative persisted value matches exactly.
281
+ if (JSON.stringify(persisted) === JSON.stringify(requested)
282
+ && JSON.stringify(result.view.value[field]) !== JSON.stringify(requested)) {
283
+ result.view.value[field] = structuredClone(requested)
284
+ if (Object.hasOwn(after.user ?? {}, field)) result.view.user[field] = structuredClone(requested)
285
+ }
286
+ }
287
+ return { ok: true, value: result }
288
+ } catch { return settingsRejected('Vision Router settings changed but the refreshed view could not be read') }
253
289
  }
254
290
  }
255
291
 
@@ -39,8 +39,8 @@ export function createRemoteSettingsRiskClientBoundary(rawOptions) {
39
39
 
40
40
  function riskMessage() {
41
41
  return chineseLocale()
42
- ? '启用远程设置?\n\n当前页面通过远程地址访问 DSH。启用后,能够访问此 trusted host 的客户端可以修改 Vision Router 已开放的设置。\n\nDSH trustedHosts 不是身份认证,请仅在你信任当前网络和访问者时继续。API Key、HTTP Provider 凭据、本地 Ollama / LM Studio、产物路径等未列入远程白名单的敏感配置仍不会开放。\n\n确定要允许远程修改设置吗?'
43
- : 'Enable remote settings?\n\nThis page is accessing DSH through a remote address. After enabling this, clients that can reach this trusted host can change the Vision Router settings exposed by the remote allow-list.\n\nDSH trustedHosts is not authentication. Continue only if you trust the current network and its users. Sensitive settings outside the remote allow-list, including API keys, HTTP-provider credentials, local Ollama / LM Studio, and artifact paths, remain unavailable remotely.\n\nAllow remote settings?'
42
+ ? '启用完整远程设置?\n\n当前页面通过远程地址访问 DSH。确认后,通过 DSH 登录认证且能访问该 Host 的客户端可修改完整的 Vision Router 普通配置,包括代理/网络出口、HTTP Provider、本地 Ollama / LM Studio、产物路径、仅本地视觉策略和桌面截图开关。这些修改会影响运行 DSH 的设备和图片数据流向。\n\n已保存的密钥和 URL 凭据不会以明文回显,但远程用户可以替换相应配置。远程修改桌面截图开关不会自动授予操作系统录屏权限;打开仅本地视觉之前,云端识图仍可能上传图片。\n\nDSH trustedHosts 本身不是身份认证,风险确认也不是身份认证。请只在信任 DSH 登录用户及网络访问环境时继续。\n\n确定允许远程修改完整配置吗?'
43
+ : 'Enable full remote settings?\n\nThis page is accessing DSH through a remote address. After confirmation, clients authenticated to DSH that can reach this Host can edit the full ordinary Vision Router configuration, including proxy/network egress, HTTP providers, local Ollama / LM Studio, artifact paths, the Local-only vision policy and the desktop-capture toggle. Changes affect the machine running DSH and where image data may be sent.\n\nSaved keys and URL credentials are not echoed in plaintext, but remote users can replace their configuration. Editing the capture toggle does not grant OS screen-recording permission. Cloud vision may still upload images until Local-only vision is enabled.\n\nDSH trustedHosts is not authentication, and this risk confirmation is not authentication. Continue only if you trust the authenticated DSH users and network access.\n\nAllow full remote configuration edits?'
44
44
  }
45
45
 
46
46
  function isLoopbackLocation(locationLike) {
@@ -165,6 +165,13 @@ export async function describeCacheKey(ctx, args = {}, exec, revision = 0) {
165
165
  hash.update(`revision:${revision}\n`)
166
166
  const paths = Array.isArray(args.paths) ? args.paths : []
167
167
  const attachments = Array.isArray(args.attachmentIds) ? args.attachmentIds : []
168
+ // Cache hits bypass the actual tool executor, including its Session-owned
169
+ // attachment authorization. Until authorization can be proved at this outer
170
+ // boundary, never cache any upload-identity input (including the paths alias).
171
+ // Ordinary paths remain cacheable only after fs.resolve/readBytes succeeds.
172
+ if (attachments.length > 0 || paths.some((input) => /^sha256:/i.test(String(input).trim()))) {
173
+ return undefined
174
+ }
168
175
  const fs = ctx?.get?.('fs')
169
176
  const cwd = exec?.agent?.session?.header?.cwd
170
177
  const signal = exec?.signal