@deepseek-ai/dsh-ptc-runtime-node 0.2.2-rc.1 → 0.2.2-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +19 -16
- package/README.md +36 -10
- package/README.zh.md +36 -10
- package/lib/index.js +102 -63
- package/lib/persistent-process.js +528 -112
- package/lib/process.js +523 -109
- package/lib/types/bindings.d.ts.map +1 -0
- package/lib/types/bindings.js.map +1 -0
- package/lib/types/bootstrap.d.ts.map +1 -0
- package/lib/types/bootstrap.js.map +1 -0
- package/lib/types/config.d.ts +2 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/configuration.d.ts.map +1 -0
- package/lib/types/configuration.js.map +1 -0
- package/lib/types/environment.d.ts +3 -2
- package/lib/types/environment.d.ts.map +1 -0
- package/lib/types/environment.js.map +1 -0
- package/lib/types/evaluators/async-function.d.ts +3 -3
- package/lib/types/evaluators/async-function.d.ts.map +1 -0
- package/lib/types/evaluators/async-function.js.map +1 -0
- package/lib/types/evaluators/index.d.ts +3 -2
- package/lib/types/evaluators/index.d.ts.map +1 -0
- package/lib/types/evaluators/index.js.map +1 -0
- package/lib/types/evaluators/repl.d.ts +3 -3
- package/lib/types/evaluators/repl.d.ts.map +1 -0
- package/lib/types/evaluators/repl.js.map +1 -0
- package/lib/types/evaluators/types.d.ts +44 -0
- package/lib/types/evaluators/types.d.ts.map +1 -0
- package/lib/types/evaluators/types.js.map +1 -0
- package/lib/types/evaluators/vm-script-await.d.ts.map +1 -0
- package/lib/types/evaluators/vm-script-await.js.map +1 -0
- package/lib/types/evaluators/vm-script.d.ts +3 -2
- package/lib/types/evaluators/vm-script.d.ts.map +1 -0
- package/lib/types/evaluators/vm-script.js.map +1 -0
- package/lib/types/execution.d.ts.map +1 -0
- package/lib/types/execution.js.map +1 -0
- package/lib/types/imports/evaluation-ended.d.ts +7 -0
- package/lib/types/imports/evaluation-ended.d.ts.map +1 -0
- package/lib/types/imports/evaluation-ended.js.map +1 -0
- package/lib/types/imports/module-hooks.d.ts +37 -0
- package/lib/types/imports/module-hooks.d.ts.map +1 -0
- package/lib/types/imports/module-hooks.js.map +1 -0
- package/lib/types/imports/vm-modules.d.ts +35 -0
- package/lib/types/imports/vm-modules.d.ts.map +1 -0
- package/lib/types/imports/vm-modules.js.map +1 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/json-wire.d.ts.map +1 -0
- package/lib/types/json-wire.js.map +1 -0
- package/lib/types/launch.d.ts +1 -2
- package/lib/types/launch.d.ts.map +1 -0
- package/lib/types/launch.js.map +1 -0
- package/lib/types/output-json.d.ts.map +1 -0
- package/lib/types/output-json.js.map +1 -0
- package/lib/types/output-ledger.d.ts.map +1 -0
- package/lib/types/output-ledger.js.map +1 -0
- package/lib/types/output-stream.d.ts.map +1 -0
- package/lib/types/output-stream.js.map +1 -0
- package/lib/types/persistent-process-entry.d.ts.map +1 -0
- package/lib/types/persistent-process-entry.js.map +1 -0
- package/lib/types/persistent-process.d.ts +6 -2
- package/lib/types/persistent-process.d.ts.map +1 -0
- package/lib/types/persistent-process.js.map +1 -0
- package/lib/types/policy-processes.d.ts.map +1 -0
- package/lib/types/policy-processes.js.map +1 -0
- package/lib/types/process-entry.d.ts.map +1 -0
- package/lib/types/process-entry.js.map +1 -0
- package/lib/types/process.d.ts +4 -2
- package/lib/types/process.d.ts.map +1 -0
- package/lib/types/process.js.map +1 -0
- package/lib/types/protocol.d.ts +10 -3
- package/lib/types/protocol.d.ts.map +1 -0
- package/lib/types/protocol.js.map +1 -0
- package/lib/types/rpc.d.ts.map +1 -0
- package/lib/types/rpc.js.map +1 -0
- package/lib/types/worker.d.ts +5 -3
- package/lib/types/worker.d.ts.map +1 -0
- package/lib/types/worker.js.map +1 -0
- package/package.json +24 -21
- package/src/bindings.ts +66 -0
- package/src/bootstrap.ts +286 -0
- package/src/config.ts +68 -0
- package/src/configuration.ts +19 -0
- package/src/environment.ts +19 -0
- package/src/evaluators/async-function.ts +42 -0
- package/src/evaluators/index.ts +25 -0
- package/src/evaluators/repl.ts +46 -0
- package/src/evaluators/types.ts +64 -0
- package/src/evaluators/vm-script-await.ts +290 -0
- package/src/evaluators/vm-script.ts +83 -0
- package/src/execution.ts +171 -0
- package/src/imports/evaluation-ended.ts +11 -0
- package/src/imports/module-hooks.ts +125 -0
- package/src/imports/vm-modules.ts +233 -0
- package/src/index.ts +104 -0
- package/src/json-wire.ts +420 -0
- package/src/launch.ts +71 -0
- package/src/output-json.ts +179 -0
- package/src/output-ledger.ts +66 -0
- package/src/output-stream.ts +50 -0
- package/src/persistent-process-entry.ts +5 -0
- package/src/persistent-process.ts +22 -0
- package/src/policy-processes.ts +79 -0
- package/src/process-entry.ts +5 -0
- package/src/process.ts +167 -0
- package/src/protocol.ts +89 -0
- package/src/rpc.ts +88 -0
- package/src/worker.ts +381 -0
package/src/json-wire.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lossless-JSON snapshots shared by the host and the source bootstrap closure.
|
|
3
|
+
* @module @deepseek-ai/dsh-ptc-runtime-node/json-wire
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { PtcJsonValue } from '@deepseek-ai/dsh-ptc-runtime'
|
|
7
|
+
|
|
8
|
+
/* jscpd:ignore-start -- the source bootstrap mirrors session JSON helpers without workspace runtime imports */
|
|
9
|
+
type IntrinsicCallable = (this: unknown, ...args: unknown[]) => unknown
|
|
10
|
+
|
|
11
|
+
const intrinsicFunctionToString = Reflect.get(Function.prototype, 'toString') as IntrinsicCallable
|
|
12
|
+
const intrinsicReflectApply = Reflect.get(Reflect, 'apply') as (
|
|
13
|
+
target: IntrinsicCallable,
|
|
14
|
+
thisArgument: unknown,
|
|
15
|
+
argumentsList: readonly unknown[],
|
|
16
|
+
) => unknown
|
|
17
|
+
const IntrinsicError = Error
|
|
18
|
+
const IntrinsicSet = Set
|
|
19
|
+
const intrinsicArrayIsArray = Array.isArray
|
|
20
|
+
const intrinsicArrayPrototype = Array.prototype
|
|
21
|
+
const intrinsicNumberIsFinite = Number.isFinite
|
|
22
|
+
const intrinsicNumberIsSafeInteger = Number.isSafeInteger
|
|
23
|
+
const intrinsicObjectCreate = Object.create
|
|
24
|
+
const intrinsicObjectDefineProperty = Object.defineProperty
|
|
25
|
+
const intrinsicObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
|
|
26
|
+
const intrinsicObjectGetPrototypeOf = Object.getPrototypeOf
|
|
27
|
+
const intrinsicObjectHasOwn = Object.hasOwn
|
|
28
|
+
const intrinsicObjectIs = Object.is
|
|
29
|
+
const intrinsicObjectKeys = Object.keys
|
|
30
|
+
const intrinsicObjectPrototype = Object.prototype
|
|
31
|
+
const intrinsicObjectPropertyIsEnumerable = Reflect.get(intrinsicObjectPrototype, 'propertyIsEnumerable') as IntrinsicCallable
|
|
32
|
+
const intrinsicReflectOwnKeys = Reflect.ownKeys
|
|
33
|
+
const intrinsicSetAdd = Reflect.get(Set.prototype, 'add') as IntrinsicCallable
|
|
34
|
+
const intrinsicSetDelete = Reflect.get(Set.prototype, 'delete') as IntrinsicCallable
|
|
35
|
+
const intrinsicSetHas = Reflect.get(Set.prototype, 'has') as IntrinsicCallable
|
|
36
|
+
|
|
37
|
+
/** Build a data descriptor that cannot inherit model-defined accessor fields. */
|
|
38
|
+
function dataDescriptor(value: unknown): PropertyDescriptor {
|
|
39
|
+
const descriptor = intrinsicObjectCreate(null) as PropertyDescriptor
|
|
40
|
+
descriptor.value = value
|
|
41
|
+
return descriptor
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Define an ordinary enumerable data slot without a prototype-bearing descriptor. */
|
|
45
|
+
function defineEnumerableDataProperty(target: object, key: PropertyKey, value: unknown): void {
|
|
46
|
+
const descriptor = dataDescriptor(value)
|
|
47
|
+
descriptor.enumerable = true
|
|
48
|
+
descriptor.configurable = true
|
|
49
|
+
descriptor.writable = true
|
|
50
|
+
intrinsicObjectDefineProperty(target, key, descriptor)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Append without consulting a model-mutated `Array.prototype`. */
|
|
54
|
+
function append<T>(target: T[], value: T): void {
|
|
55
|
+
defineEnumerableDataProperty(target, target.length, value)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Pop without consulting a model-mutated `Array.prototype`. */
|
|
59
|
+
function takeLast<T>(target: T[]): T | undefined {
|
|
60
|
+
if (target.length === 0) return undefined
|
|
61
|
+
const index = target.length - 1
|
|
62
|
+
const value = target[index]
|
|
63
|
+
intrinsicObjectDefineProperty(target, 'length', dataDescriptor(index))
|
|
64
|
+
return value
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether one captured-intrinsic Set contains a value. */
|
|
68
|
+
function setHas<T>(target: Set<T>, value: T): boolean {
|
|
69
|
+
return intrinsicReflectApply(intrinsicSetHas, target, [value]) as boolean
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Add to one captured-intrinsic Set. */
|
|
73
|
+
function setAdd<T>(target: Set<T>, value: T): void {
|
|
74
|
+
intrinsicReflectApply(intrinsicSetAdd, target, [value])
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Delete from one captured-intrinsic Set. */
|
|
78
|
+
function setDelete<T>(target: Set<T>, value: T): void {
|
|
79
|
+
intrinsicReflectApply(intrinsicSetDelete, target, [value])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether a realm-owned intrinsic prototype is backed by its native constructor. */
|
|
83
|
+
function hasIntrinsicConstructor(prototype: object, name: 'Array' | 'Object'): boolean {
|
|
84
|
+
const descriptor = intrinsicObjectGetOwnPropertyDescriptor(prototype, 'constructor')
|
|
85
|
+
const constructor: unknown = descriptor?.value
|
|
86
|
+
if (typeof constructor !== 'function') return false
|
|
87
|
+
try {
|
|
88
|
+
return constructor.name === name
|
|
89
|
+
&& constructor.prototype === prototype
|
|
90
|
+
&& intrinsicReflectApply(intrinsicFunctionToString, constructor, []) === `function ${name}() { [native code] }`
|
|
91
|
+
} catch {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Whether a candidate is a foreign realm's intrinsic `Object.prototype`. */
|
|
97
|
+
function isForeignIntrinsicObjectPrototype(value: object): boolean {
|
|
98
|
+
return intrinsicObjectGetPrototypeOf(value) === null && hasIntrinsicConstructor(value, 'Object')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Whether an array uses one realm's intrinsic `Array.prototype`, not a subclass or forged prototype. */
|
|
102
|
+
function hasPlainArrayPrototype(value: unknown[]): boolean {
|
|
103
|
+
const prototype: unknown = intrinsicObjectGetPrototypeOf(value)
|
|
104
|
+
if (prototype === intrinsicArrayPrototype) return true
|
|
105
|
+
if (!intrinsicArrayIsArray(prototype) || !hasIntrinsicConstructor(prototype, 'Array')) return false
|
|
106
|
+
const objectPrototype: unknown = intrinsicObjectGetPrototypeOf(prototype)
|
|
107
|
+
return typeof objectPrototype === 'object'
|
|
108
|
+
&& objectPrototype !== null
|
|
109
|
+
&& isForeignIntrinsicObjectPrototype(objectPrototype)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Whether an object is a plain or null-prototype record from any JavaScript realm. */
|
|
113
|
+
function hasPlainObjectPrototype(value: object): boolean {
|
|
114
|
+
const prototype: unknown = intrinsicObjectGetPrototypeOf(value)
|
|
115
|
+
return prototype === null
|
|
116
|
+
|| prototype === intrinsicObjectPrototype
|
|
117
|
+
|| typeof prototype === 'object' && isForeignIntrinsicObjectPrototype(prototype)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Return every JSON-visible object key, or reject own data JSON would discard. */
|
|
121
|
+
function enumerableStringKeys(value: object): string[] | undefined {
|
|
122
|
+
const keys = intrinsicReflectOwnKeys(value)
|
|
123
|
+
for (let index = 0; index < keys.length; index++) {
|
|
124
|
+
const key = keys[index]
|
|
125
|
+
if (typeof key !== 'string' || !intrinsicReflectApply(intrinsicObjectPropertyIsEnumerable, value, [key])) return undefined
|
|
126
|
+
}
|
|
127
|
+
return keys as string[]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type SnapshotDestination =
|
|
131
|
+
| { kind: 'root' }
|
|
132
|
+
| { kind: 'array'; target: PtcJsonValue[]; index: number }
|
|
133
|
+
| { kind: 'object'; target: Record<string, PtcJsonValue>; key: string }
|
|
134
|
+
|
|
135
|
+
type SnapshotTask =
|
|
136
|
+
| { kind: 'visit'; value: unknown; destination: SnapshotDestination }
|
|
137
|
+
| { kind: 'array-item'; source: unknown[]; index: number; target: PtcJsonValue[] }
|
|
138
|
+
| { kind: 'object-property'; source: Record<string, unknown>; key: string; target: Record<string, PtcJsonValue> }
|
|
139
|
+
| { kind: 'leave'; source: object }
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Validate and detach one process-boundary value without loading another
|
|
143
|
+
* workspace package at runtime. This mirrors the session-owned canonical
|
|
144
|
+
* JSON boundary while remaining safe to import from the unbuilt bootstrap.
|
|
145
|
+
* Its iterative traversal adds no JavaScript call-stack depth limit.
|
|
146
|
+
*
|
|
147
|
+
* @param value - the candidate completion value.
|
|
148
|
+
* @returns a detached lossless-JSON snapshot, or `undefined` when invalid.
|
|
149
|
+
*/
|
|
150
|
+
export function snapshotPtcJsonValue(value: unknown): PtcJsonValue | undefined {
|
|
151
|
+
const active = new IntrinsicSet<object>()
|
|
152
|
+
let root: PtcJsonValue | undefined
|
|
153
|
+
const assign = (destination: SnapshotDestination, item: PtcJsonValue): void => {
|
|
154
|
+
if (destination.kind === 'root') {
|
|
155
|
+
root = item
|
|
156
|
+
} else if (destination.kind === 'array') {
|
|
157
|
+
defineEnumerableDataProperty(destination.target, destination.index, item)
|
|
158
|
+
} else {
|
|
159
|
+
defineEnumerableDataProperty(destination.target, destination.key, item)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const tasks: SnapshotTask[] = [{ kind: 'visit', value, destination: { kind: 'root' } }]
|
|
164
|
+
for (let task = takeLast(tasks); task !== undefined; task = takeLast(tasks)) {
|
|
165
|
+
if (task.kind === 'leave') {
|
|
166
|
+
setDelete(active, task.source)
|
|
167
|
+
continue
|
|
168
|
+
}
|
|
169
|
+
if (task.kind === 'array-item') {
|
|
170
|
+
if (!intrinsicObjectHasOwn(task.source, task.index)) return undefined
|
|
171
|
+
append(tasks, {
|
|
172
|
+
kind: 'visit',
|
|
173
|
+
value: task.source[task.index],
|
|
174
|
+
destination: { kind: 'array', target: task.target, index: task.index },
|
|
175
|
+
})
|
|
176
|
+
continue
|
|
177
|
+
}
|
|
178
|
+
if (task.kind === 'object-property') {
|
|
179
|
+
append(tasks, {
|
|
180
|
+
kind: 'visit',
|
|
181
|
+
value: task.source[task.key],
|
|
182
|
+
destination: { kind: 'object', target: task.target, key: task.key },
|
|
183
|
+
})
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const candidate = task.value
|
|
188
|
+
if (candidate === null) {
|
|
189
|
+
assign(task.destination, null)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
if (typeof candidate === 'boolean' || typeof candidate === 'string') {
|
|
193
|
+
assign(task.destination, candidate)
|
|
194
|
+
continue
|
|
195
|
+
}
|
|
196
|
+
if (typeof candidate === 'number') {
|
|
197
|
+
if (!intrinsicNumberIsFinite(candidate) || intrinsicObjectIs(candidate, -0)) return undefined
|
|
198
|
+
assign(task.destination, candidate)
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
if (typeof candidate !== 'object') return undefined
|
|
202
|
+
if (setHas(active, candidate)) return undefined
|
|
203
|
+
|
|
204
|
+
if (intrinsicArrayIsArray(candidate)) {
|
|
205
|
+
if (!hasPlainArrayPrototype(candidate)) return undefined
|
|
206
|
+
const length = candidate.length
|
|
207
|
+
if (intrinsicReflectOwnKeys(candidate).length !== length + 1) return undefined
|
|
208
|
+
const target: PtcJsonValue[] = []
|
|
209
|
+
assign(task.destination, target)
|
|
210
|
+
setAdd(active, candidate)
|
|
211
|
+
append(tasks, { kind: 'leave', source: candidate })
|
|
212
|
+
for (let index = length - 1; index >= 0; index--) {
|
|
213
|
+
append(tasks, { kind: 'array-item', source: candidate, index, target })
|
|
214
|
+
}
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!hasPlainObjectPrototype(candidate)) return undefined
|
|
219
|
+
const keys = enumerableStringKeys(candidate)
|
|
220
|
+
if (keys === undefined) return undefined
|
|
221
|
+
const target: Record<string, PtcJsonValue> = {}
|
|
222
|
+
assign(task.destination, target)
|
|
223
|
+
setAdd(active, candidate)
|
|
224
|
+
append(tasks, { kind: 'leave', source: candidate })
|
|
225
|
+
for (let index = keys.length - 1; index >= 0; index--) {
|
|
226
|
+
const key = keys[index]
|
|
227
|
+
/* v8 ignore next -- the loop is bounded by the captured key count. */
|
|
228
|
+
if (key === undefined) return undefined
|
|
229
|
+
append(tasks, { kind: 'object-property', source: candidate as Record<string, unknown>, key, target })
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return root
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
interface ArrayWireToken {
|
|
236
|
+
kind: 'array'
|
|
237
|
+
length: number
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface ObjectWireToken {
|
|
241
|
+
kind: 'object'
|
|
242
|
+
keys: string[]
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
type PtcJsonToken = null | boolean | number | string | ArrayWireToken | ObjectWireToken
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* A pre-order, bounded-depth transport for one lossless JSON value. Container
|
|
249
|
+
* markers and scalar leaves share one flat token array, so JSON serialization
|
|
250
|
+
* does not recurse through the value's application nesting.
|
|
251
|
+
*/
|
|
252
|
+
export type PtcJsonWire = PtcJsonToken[]
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Flatten one validated JSON value for the process control channel.
|
|
256
|
+
* @param value - the lossless JSON value to transport.
|
|
257
|
+
* @returns a pre-order token stream whose own nesting is bounded.
|
|
258
|
+
*/
|
|
259
|
+
export function encodePtcJsonWire(value: PtcJsonValue): PtcJsonWire {
|
|
260
|
+
const wire: PtcJsonWire = []
|
|
261
|
+
const pending: PtcJsonValue[] = [value]
|
|
262
|
+
for (let current = takeLast(pending); current !== undefined; current = takeLast(pending)) {
|
|
263
|
+
if (current === null || typeof current === 'boolean' || typeof current === 'number' || typeof current === 'string') {
|
|
264
|
+
append(wire, current)
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
if (intrinsicArrayIsArray(current)) {
|
|
268
|
+
append(wire, { kind: 'array', length: current.length })
|
|
269
|
+
for (let index = current.length - 1; index >= 0; index--) {
|
|
270
|
+
const item = current[index]
|
|
271
|
+
if (item === undefined) throw new IntrinsicError('cannot encode a sparse JSON array')
|
|
272
|
+
append(pending, item)
|
|
273
|
+
}
|
|
274
|
+
continue
|
|
275
|
+
}
|
|
276
|
+
const keys = intrinsicObjectKeys(current)
|
|
277
|
+
append(wire, { kind: 'object', keys })
|
|
278
|
+
for (let index = keys.length - 1; index >= 0; index--) {
|
|
279
|
+
const key = keys[index]
|
|
280
|
+
/* v8 ignore next -- the loop is bounded by the captured key count. */
|
|
281
|
+
if (key === undefined) throw new IntrinsicError('cannot encode a missing JSON object key')
|
|
282
|
+
const item = current[key]
|
|
283
|
+
if (item === undefined) throw new IntrinsicError('cannot encode an undefined JSON object property')
|
|
284
|
+
append(pending, item)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return wire
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
type DecodeFrame =
|
|
291
|
+
| { kind: 'array'; target: PtcJsonValue[]; length: number; index: number }
|
|
292
|
+
| { kind: 'object'; target: Record<string, PtcJsonValue>; keys: string[]; index: number }
|
|
293
|
+
|
|
294
|
+
/** Whether an array contains exactly its dense indexed slots and `length`. */
|
|
295
|
+
function isDenseArray(value: unknown[]): boolean {
|
|
296
|
+
if (!hasPlainArrayPrototype(value) || intrinsicReflectOwnKeys(value).length !== value.length + 1) return false
|
|
297
|
+
for (let index = 0; index < value.length; index++) {
|
|
298
|
+
if (!intrinsicObjectHasOwn(value, index)) return false
|
|
299
|
+
}
|
|
300
|
+
return true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Whether one exact string-key list contains a key, without consulting its prototype. */
|
|
304
|
+
function keysContain(keys: string[], expected: string): boolean {
|
|
305
|
+
for (let index = 0; index < keys.length; index++) {
|
|
306
|
+
if (keys[index] === expected) return true
|
|
307
|
+
}
|
|
308
|
+
return false
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Return one exact container marker, or reject any extra/missing fields. */
|
|
312
|
+
function containerToken(value: object): ArrayWireToken | ObjectWireToken | undefined {
|
|
313
|
+
if (intrinsicArrayIsArray(value) || !hasPlainObjectPrototype(value)) return undefined
|
|
314
|
+
const keys = enumerableStringKeys(value)
|
|
315
|
+
if (keys === undefined) return undefined
|
|
316
|
+
const token = value as Record<string, unknown>
|
|
317
|
+
if (token.kind === 'array') {
|
|
318
|
+
if (keys.length !== 2 || !keysContain(keys, 'kind') || !keysContain(keys, 'length')) return undefined
|
|
319
|
+
const length = token.length
|
|
320
|
+
return typeof length === 'number' && intrinsicNumberIsSafeInteger(length) && length >= 0
|
|
321
|
+
? { kind: 'array', length }
|
|
322
|
+
: undefined
|
|
323
|
+
}
|
|
324
|
+
if (token.kind === 'object') {
|
|
325
|
+
if (keys.length !== 2 || !keysContain(keys, 'kind') || !keysContain(keys, 'keys')) return undefined
|
|
326
|
+
const objectKeys = token.keys
|
|
327
|
+
if (!intrinsicArrayIsArray(objectKeys) || !isDenseArray(objectKeys)) return undefined
|
|
328
|
+
const unique = new IntrinsicSet<string>()
|
|
329
|
+
const normalizedKeys: string[] = []
|
|
330
|
+
const objectKeyValues = objectKeys as unknown[]
|
|
331
|
+
for (let index = 0; index < objectKeyValues.length; index++) {
|
|
332
|
+
const key = objectKeyValues[index]
|
|
333
|
+
if (typeof key !== 'string' || setHas(unique, key)) return undefined
|
|
334
|
+
setAdd(unique, key)
|
|
335
|
+
append(normalizedKeys, key)
|
|
336
|
+
}
|
|
337
|
+
return { kind: 'object', keys: normalizedKeys }
|
|
338
|
+
}
|
|
339
|
+
return undefined
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Rebuild one lossless JSON value from the flat process wire format.
|
|
344
|
+
* Malformed or incomplete traffic returns `undefined`; traversal is iterative
|
|
345
|
+
* and therefore independent of the transported value's application depth.
|
|
346
|
+
* @param input - untrusted message-port payload.
|
|
347
|
+
* @returns the detached JSON value, or `undefined` when the wire is invalid.
|
|
348
|
+
*/
|
|
349
|
+
export function decodePtcJsonWire(input: unknown): PtcJsonValue | undefined {
|
|
350
|
+
try {
|
|
351
|
+
if (!intrinsicArrayIsArray(input) || !isDenseArray(input) || input.length === 0) return undefined
|
|
352
|
+
const wire = input as unknown[]
|
|
353
|
+
const frames: DecodeFrame[] = []
|
|
354
|
+
let root: PtcJsonValue | undefined
|
|
355
|
+
let rootAssigned = false
|
|
356
|
+
|
|
357
|
+
const attach = (value: PtcJsonValue): boolean => {
|
|
358
|
+
const parent = frames[frames.length - 1]
|
|
359
|
+
if (!parent) {
|
|
360
|
+
if (rootAssigned) return false
|
|
361
|
+
root = value
|
|
362
|
+
rootAssigned = true
|
|
363
|
+
return true
|
|
364
|
+
}
|
|
365
|
+
/* v8 ignore next -- completed frames are popped before another token can attach. */
|
|
366
|
+
if (parent.index >= (parent.kind === 'array' ? parent.length : parent.keys.length)) return false
|
|
367
|
+
if (parent.kind === 'array') {
|
|
368
|
+
append(parent.target, value)
|
|
369
|
+
} else {
|
|
370
|
+
const key = parent.keys[parent.index]
|
|
371
|
+
/* v8 ignore next -- object frames are built from validated keys and their exact length. */
|
|
372
|
+
if (key === undefined) return false
|
|
373
|
+
defineEnumerableDataProperty(parent.target, key, value)
|
|
374
|
+
}
|
|
375
|
+
parent.index += 1
|
|
376
|
+
return true
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
for (let tokenIndex = 0; tokenIndex < wire.length; tokenIndex++) {
|
|
380
|
+
const token = wire[tokenIndex]
|
|
381
|
+
let value: PtcJsonValue
|
|
382
|
+
let frame: DecodeFrame | undefined
|
|
383
|
+
if (token === null || typeof token === 'boolean' || typeof token === 'string') {
|
|
384
|
+
value = token
|
|
385
|
+
} else if (typeof token === 'number') {
|
|
386
|
+
if (!intrinsicNumberIsFinite(token) || intrinsicObjectIs(token, -0)) return undefined
|
|
387
|
+
value = token
|
|
388
|
+
} else {
|
|
389
|
+
if (typeof token !== 'object') return undefined
|
|
390
|
+
const marker = containerToken(token)
|
|
391
|
+
if (!marker) return undefined
|
|
392
|
+
const remainingTokens = wire.length - tokenIndex - 1
|
|
393
|
+
if (marker.kind === 'array') {
|
|
394
|
+
if (marker.length > remainingTokens) return undefined
|
|
395
|
+
const target: PtcJsonValue[] = []
|
|
396
|
+
value = target
|
|
397
|
+
if (marker.length > 0) frame = { kind: 'array', target, length: marker.length, index: 0 }
|
|
398
|
+
} else {
|
|
399
|
+
if (marker.keys.length > remainingTokens) return undefined
|
|
400
|
+
const target: Record<string, PtcJsonValue> = {}
|
|
401
|
+
value = target
|
|
402
|
+
if (marker.keys.length > 0) frame = { kind: 'object', target, keys: marker.keys, index: 0 }
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (!attach(value)) return undefined
|
|
406
|
+
if (frame) append(frames, frame)
|
|
407
|
+
while (frames.length > 0) {
|
|
408
|
+
const current = frames[frames.length - 1]
|
|
409
|
+
/* v8 ignore next -- the loop condition guarantees a final frame. */
|
|
410
|
+
if (current === undefined) break
|
|
411
|
+
if (current.index < (current.kind === 'array' ? current.length : current.keys.length)) break
|
|
412
|
+
takeLast(frames)
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return frames.length === 0 ? root : undefined
|
|
416
|
+
} catch {
|
|
417
|
+
return undefined
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/* jscpd:ignore-end */
|
package/src/launch.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Select source or built bootstrap assets in the mounted execution world. */
|
|
2
|
+
import { dirname, isAbsolute, resolve } from 'node:path'
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
4
|
+
import type { FileSystem } from '@deepseek-ai/dsh-fs'
|
|
5
|
+
import { assertNever } from '@deepseek-ai/dsh-util-values'
|
|
6
|
+
|
|
7
|
+
/** Deployment-owned worker invocation in the subprocess execution world. */
|
|
8
|
+
export type LaunchConfig = {
|
|
9
|
+
/** Start a separately installed Node executable and JavaScript bootstrap. */
|
|
10
|
+
kind: 'node-script'
|
|
11
|
+
/** Executable name or path resolved by the subprocess provider. */
|
|
12
|
+
executable: string
|
|
13
|
+
/** Absolute preinstalled bootstrap; omitted maps this package's bootstrap into the execution world. */
|
|
14
|
+
bootstrapPath?: string
|
|
15
|
+
} | {
|
|
16
|
+
/** Start the private PTC worker embedded in a packaged executable. */
|
|
17
|
+
kind: 'embedded'
|
|
18
|
+
/** Packaged executable name or path resolved by the subprocess provider. */
|
|
19
|
+
executable: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the local carrier default or validate an explicit execution-world invocation.
|
|
24
|
+
* @param config - explicit remote or local worker configuration; omitted selects this carrier.
|
|
25
|
+
* @returns the worker invocation without guessing from the execution world's paths.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveLaunch(config?: LaunchConfig): LaunchConfig {
|
|
28
|
+
const launch: LaunchConfig = config ?? { kind: 'pkg' in process ? 'embedded' : 'node-script', executable: process.execPath }
|
|
29
|
+
if (launch.executable.length === 0) throw new Error('ptc-runtime-node: launch.executable must be non-empty')
|
|
30
|
+
if (launch.kind === 'node-script' && launch.bootstrapPath !== undefined && !isAbsolute(launch.bootstrapPath)) {
|
|
31
|
+
throw new Error('ptc-runtime-node: launch.bootstrapPath must be absolute')
|
|
32
|
+
}
|
|
33
|
+
return launch
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Select explicit arguments without inheriting host loader or inspector flags.
|
|
38
|
+
* @param fs - Filesystem mapping host bootstrap assets into the process world.
|
|
39
|
+
* @param config - resolved worker invocation.
|
|
40
|
+
* @param maxMessageBytes - Validated frame and queued-write limit.
|
|
41
|
+
* @returns Arguments following the resolved Node executable.
|
|
42
|
+
*/
|
|
43
|
+
export function bootstrapArgs(fs: Pick<FileSystem, 'processPathFromHostPath'>, config: LaunchConfig, maxMessageBytes: number): string[] {
|
|
44
|
+
switch (config.kind) {
|
|
45
|
+
case 'embedded':
|
|
46
|
+
return [String(maxMessageBytes)]
|
|
47
|
+
case 'node-script': return ['--experimental-vm-modules', ...scriptBootstrapArgs(fs, config.bootstrapPath, maxMessageBytes)]
|
|
48
|
+
/* v8 ignore next -- Callers supply the resolved, closed LaunchConfig union. */
|
|
49
|
+
default: return assertNever(config)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function scriptBootstrapArgs(
|
|
54
|
+
fs: Pick<FileSystem, 'processPathFromHostPath'>, bootstrapPath: string | undefined, maxMessageBytes: number,
|
|
55
|
+
): string[] {
|
|
56
|
+
if (bootstrapPath !== undefined) return [bootstrapPath, String(maxMessageBytes)]
|
|
57
|
+
const mapped = (path: string): string => {
|
|
58
|
+
const result = fs.processPathFromHostPath(path)
|
|
59
|
+
if (result === undefined) throw new Error(`PTC runtime bootstrap is unavailable in the subprocess execution world: ${path}`)
|
|
60
|
+
return result
|
|
61
|
+
}
|
|
62
|
+
/* v8 ignore next 3 -- built-lib.e2e.ts executes the bundled provider and sibling process.js under plain Node. */
|
|
63
|
+
if (!new URL(import.meta.url).pathname.endsWith('.ts')) {
|
|
64
|
+
return [mapped(fileURLToPath(new URL('./process.js', import.meta.url))), String(maxMessageBytes)]
|
|
65
|
+
}
|
|
66
|
+
const entry = mapped(fileURLToPath(new URL('./process.ts', import.meta.url)))
|
|
67
|
+
const subprocess = dirname(fileURLToPath(import.meta.resolve('@deepseek-ai/dsh-subprocess/package.json')))
|
|
68
|
+
const helper = mapped(resolve(subprocess, 'src/control.ts'))
|
|
69
|
+
const source = `const {openInheritedControlChannel}=await import(${JSON.stringify(pathToFileURL(helper).href)});const {runNodeMain}=await import(${JSON.stringify(pathToFileURL(entry).href)});await runNodeMain(openInheritedControlChannel(),${maxMessageBytes},process);`
|
|
70
|
+
return ['--input-type=module', '--eval', source]
|
|
71
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/** JSON string-prefix accounting for the outer-output ledger. @module @deepseek-ai/dsh-ptc-runtime-node/output-json */
|
|
2
|
+
|
|
3
|
+
import type { PtcJsonValue } from '@deepseek-ai/dsh-ptc-runtime'
|
|
4
|
+
|
|
5
|
+
type IntrinsicCallable = (this: unknown, ...args: unknown[]) => unknown
|
|
6
|
+
|
|
7
|
+
const intrinsicReflectApply = Reflect.apply as (
|
|
8
|
+
target: IntrinsicCallable,
|
|
9
|
+
thisArgument: unknown,
|
|
10
|
+
argumentsList: readonly unknown[],
|
|
11
|
+
) => unknown
|
|
12
|
+
const intrinsicArrayIsArray = Array.isArray
|
|
13
|
+
const IntrinsicBuffer = Buffer
|
|
14
|
+
const intrinsicBufferByteLength = Reflect.get(Buffer, 'byteLength') as IntrinsicCallable
|
|
15
|
+
const intrinsicObjectCreate = Object.create
|
|
16
|
+
const intrinsicObjectDefineProperty = Object.defineProperty
|
|
17
|
+
const intrinsicObjectKeys = Object.keys
|
|
18
|
+
const intrinsicString = String
|
|
19
|
+
const intrinsicStringCharCodeAt = Reflect.get(String.prototype, 'charCodeAt') as IntrinsicCallable
|
|
20
|
+
const intrinsicStringCodePointAt = Reflect.get(String.prototype, 'codePointAt') as IntrinsicCallable
|
|
21
|
+
const intrinsicStringSlice = Reflect.get(String.prototype, 'slice') as IntrinsicCallable
|
|
22
|
+
|
|
23
|
+
/** Build a data descriptor that cannot inherit model-defined accessor fields. */
|
|
24
|
+
function dataDescriptor(value: unknown): PropertyDescriptor {
|
|
25
|
+
const descriptor = intrinsicObjectCreate(null) as PropertyDescriptor
|
|
26
|
+
descriptor.value = value
|
|
27
|
+
return descriptor
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Define an ordinary enumerable data slot without a prototype-bearing descriptor. */
|
|
31
|
+
function defineEnumerableDataProperty(target: object, key: PropertyKey, value: unknown): void {
|
|
32
|
+
const descriptor = dataDescriptor(value)
|
|
33
|
+
descriptor.enumerable = true
|
|
34
|
+
descriptor.configurable = true
|
|
35
|
+
descriptor.writable = true
|
|
36
|
+
intrinsicObjectDefineProperty(target, key, descriptor)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** UTF-8 byte length through the module-captured Node intrinsic. */
|
|
40
|
+
function byteLength(text: string): number {
|
|
41
|
+
return intrinsicReflectApply(intrinsicBufferByteLength, IntrinsicBuffer, [text, 'utf8']) as number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Append without consulting a model-mutated `Array.prototype`. */
|
|
45
|
+
function append<T>(target: T[], value: T): void {
|
|
46
|
+
defineEnumerableDataProperty(target, target.length, value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Pop without consulting a model-mutated `Array.prototype`. */
|
|
50
|
+
function takeLast<T>(target: T[]): T | undefined {
|
|
51
|
+
if (target.length === 0) return undefined
|
|
52
|
+
const index = target.length - 1
|
|
53
|
+
const value = target[index]
|
|
54
|
+
intrinsicObjectDefineProperty(target, 'length', dataDescriptor(index))
|
|
55
|
+
return value
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One code-point-aligned character from a string. */
|
|
59
|
+
function characterAt(text: string, index: number): string {
|
|
60
|
+
const codePoint = intrinsicReflectApply(intrinsicStringCodePointAt, text, [index]) as number
|
|
61
|
+
const width = codePoint > 0xffff ? 2 : 1
|
|
62
|
+
return intrinsicReflectApply(intrinsicStringSlice, text, [index, index + width]) as string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Serialized bytes contributed by one complete Unicode code point inside JSON quotes. */
|
|
66
|
+
function serializedCharacterBytes(character: string): number {
|
|
67
|
+
if (character.length === 2) return 4
|
|
68
|
+
if (character === '"' || character === '\\') return 2
|
|
69
|
+
const code = intrinsicReflectApply(intrinsicStringCharCodeAt, character, [0]) as number
|
|
70
|
+
if (code >= 0xd800 && code <= 0xdfff) return 6
|
|
71
|
+
if (code < 0x20) return code === 0x08 || code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d ? 2 : 6
|
|
72
|
+
return byteLength(character)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Measure one JSON string without materializing its complete escaped form.
|
|
77
|
+
* @param text - the candidate string.
|
|
78
|
+
* @param maxBytes - largest serialized size the caller can admit.
|
|
79
|
+
* @returns Exact serialized bytes, or `undefined` as soon as the cap is crossed.
|
|
80
|
+
*/
|
|
81
|
+
export function jsonStringBytesUpTo(text: string, maxBytes: number): number | undefined {
|
|
82
|
+
if (maxBytes < 2) return undefined
|
|
83
|
+
let bytes = 2
|
|
84
|
+
for (let index = 0; index < text.length;) {
|
|
85
|
+
const character = characterAt(text, index)
|
|
86
|
+
bytes += serializedCharacterBytes(character)
|
|
87
|
+
if (bytes > maxBytes) return undefined
|
|
88
|
+
index += character.length
|
|
89
|
+
}
|
|
90
|
+
return bytes
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Measure one lossless JSON value without allocating its serialized form.
|
|
95
|
+
* @param value - already validated lossless JSON.
|
|
96
|
+
* @param maxBytes - largest serialized size the caller can admit.
|
|
97
|
+
* @returns Exact serialized bytes, or `undefined` as soon as the cap is crossed.
|
|
98
|
+
*/
|
|
99
|
+
export function jsonValueBytesUpTo(value: PtcJsonValue, maxBytes: number): number | undefined {
|
|
100
|
+
type Task =
|
|
101
|
+
| { kind: 'value'; value: PtcJsonValue }
|
|
102
|
+
| { kind: 'array'; value: PtcJsonValue[]; index: number }
|
|
103
|
+
| { kind: 'object'; value: Record<string, PtcJsonValue>; keys: string[]; index: number }
|
|
104
|
+
|
|
105
|
+
let bytes = 0
|
|
106
|
+
const add = (cost: number): boolean => {
|
|
107
|
+
bytes += cost
|
|
108
|
+
return bytes <= maxBytes
|
|
109
|
+
}
|
|
110
|
+
const tasks: Task[] = [{ kind: 'value', value }]
|
|
111
|
+
for (let task = takeLast(tasks); task !== undefined; task = takeLast(tasks)) {
|
|
112
|
+
if (task.kind === 'value') {
|
|
113
|
+
const current = task.value
|
|
114
|
+
if (current === null) {
|
|
115
|
+
if (!add(4)) return undefined
|
|
116
|
+
} else if (typeof current === 'string') {
|
|
117
|
+
const stringBytes = jsonStringBytesUpTo(current, maxBytes - bytes)
|
|
118
|
+
if (stringBytes === undefined) return undefined
|
|
119
|
+
bytes += stringBytes
|
|
120
|
+
} else if (typeof current === 'number') {
|
|
121
|
+
if (!add(byteLength(intrinsicString(current)))) return undefined
|
|
122
|
+
} else if (typeof current === 'boolean') {
|
|
123
|
+
if (!add(current ? 4 : 5)) return undefined
|
|
124
|
+
} else if (intrinsicArrayIsArray(current)) {
|
|
125
|
+
if (!add(2)) return undefined
|
|
126
|
+
if (current.length > 0) append(tasks, { kind: 'array', value: current, index: 0 })
|
|
127
|
+
} else {
|
|
128
|
+
if (!add(2)) return undefined
|
|
129
|
+
const keys = intrinsicObjectKeys(current)
|
|
130
|
+
if (keys.length > 0) append(tasks, { kind: 'object', value: current, keys, index: 0 })
|
|
131
|
+
}
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (task.index > 0 && !add(1)) return undefined
|
|
136
|
+
if (task.kind === 'array') {
|
|
137
|
+
const item = task.value[task.index]
|
|
138
|
+
if (item === undefined) return undefined
|
|
139
|
+
if (task.index + 1 < task.value.length) append(tasks, { ...task, index: task.index + 1 })
|
|
140
|
+
append(tasks, { kind: 'value', value: item })
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const key = task.keys[task.index]
|
|
145
|
+
/* v8 ignore next -- an object frame is created and advanced only for an existing Object.keys entry. */
|
|
146
|
+
if (key === undefined) return undefined
|
|
147
|
+
const keyBytes = jsonStringBytesUpTo(key, maxBytes - bytes)
|
|
148
|
+
if (keyBytes === undefined) return undefined
|
|
149
|
+
if (!add(keyBytes + 1)) return undefined
|
|
150
|
+
const item = task.value[key]
|
|
151
|
+
if (item === undefined) return undefined
|
|
152
|
+
if (task.index + 1 < task.keys.length) append(tasks, { ...task, index: task.index + 1 })
|
|
153
|
+
append(tasks, { kind: 'value', value: item })
|
|
154
|
+
}
|
|
155
|
+
return bytes
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Return the longest code-point-aligned prefix whose JSON string encoding,
|
|
160
|
+
* including its surrounding quotes, fits `maxBytes`.
|
|
161
|
+
*
|
|
162
|
+
* @param text - the candidate string.
|
|
163
|
+
* @param maxBytes - serialized JSON-string bytes available.
|
|
164
|
+
* @returns the fitting prefix, or an empty string when even useful content cannot fit.
|
|
165
|
+
*/
|
|
166
|
+
export function truncateJsonStringBytes(text: string, maxBytes: number): string {
|
|
167
|
+
if (maxBytes < 2) return ''
|
|
168
|
+
let bytes = 2
|
|
169
|
+
let end = 0
|
|
170
|
+
for (let index = 0; index < text.length;) {
|
|
171
|
+
const character = characterAt(text, index)
|
|
172
|
+
const cost = serializedCharacterBytes(character)
|
|
173
|
+
if (bytes + cost > maxBytes) break
|
|
174
|
+
bytes += cost
|
|
175
|
+
end += character.length
|
|
176
|
+
index += character.length
|
|
177
|
+
}
|
|
178
|
+
return end === text.length ? text : intrinsicReflectApply(intrinsicStringSlice, text, [0, end]) as string
|
|
179
|
+
}
|