@namzu/sdk 36.0.0 → 38.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +244 -0
- package/README.md +68 -41
- package/dist/bridge/a2a/mapper.d.ts.map +1 -1
- package/dist/bridge/a2a/mapper.js +4 -0
- package/dist/bridge/a2a/mapper.js.map +1 -1
- package/dist/bridge/sse/mapper.d.ts.map +1 -1
- package/dist/bridge/sse/mapper.js +16 -1
- package/dist/bridge/sse/mapper.js.map +1 -1
- package/dist/compaction/extractor.d.ts +1 -1
- package/dist/compaction/extractor.d.ts.map +1 -1
- package/dist/compaction/extractor.js +15 -7
- package/dist/compaction/extractor.js.map +1 -1
- package/dist/compaction/manager.d.ts +2 -0
- package/dist/compaction/manager.d.ts.map +1 -1
- package/dist/compaction/manager.js +12 -1
- package/dist/compaction/manager.js.map +1 -1
- package/dist/compaction/verifier.d.ts.map +1 -1
- package/dist/compaction/verifier.js +82 -15
- package/dist/compaction/verifier.js.map +1 -1
- package/dist/config/runtime.d.ts +16 -6
- package/dist/config/runtime.d.ts.map +1 -1
- package/dist/config/runtime.js +2 -0
- package/dist/config/runtime.js.map +1 -1
- package/dist/contracts/api.d.ts +1 -1
- package/dist/contracts/api.d.ts.map +1 -1
- package/dist/contracts/schemas.d.ts +10 -10
- package/dist/eval/harness-verification.d.ts +63 -0
- package/dist/eval/harness-verification.d.ts.map +1 -0
- package/dist/eval/harness-verification.js +165 -0
- package/dist/eval/harness-verification.js.map +1 -0
- package/dist/eval/index.d.ts +2 -0
- package/dist/eval/index.d.ts.map +1 -1
- package/dist/eval/index.js +1 -0
- package/dist/eval/index.js.map +1 -1
- package/dist/execution/code-runtime/json-value.d.ts +10 -0
- package/dist/execution/code-runtime/json-value.d.ts.map +1 -0
- package/dist/execution/code-runtime/json-value.js +89 -0
- package/dist/execution/code-runtime/json-value.js.map +1 -0
- package/dist/execution/code-runtime/worker-program.d.ts +8 -0
- package/dist/execution/code-runtime/worker-program.d.ts.map +1 -0
- package/dist/execution/code-runtime/worker-program.js +240 -0
- package/dist/execution/code-runtime/worker-program.js.map +1 -0
- package/dist/execution/code-runtime/worker.d.ts +22 -0
- package/dist/execution/code-runtime/worker.d.ts.map +1 -1
- package/dist/execution/code-runtime/worker.js +148 -111
- package/dist/execution/code-runtime/worker.js.map +1 -1
- package/dist/manager/agent/lifecycle.d.ts +12 -0
- package/dist/manager/agent/lifecycle.d.ts.map +1 -1
- package/dist/manager/agent/lifecycle.js +316 -46
- package/dist/manager/agent/lifecycle.js.map +1 -1
- package/dist/manager/run/persistence.d.ts +2 -0
- package/dist/manager/run/persistence.d.ts.map +1 -1
- package/dist/manager/run/persistence.js +6 -0
- package/dist/manager/run/persistence.js.map +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts +2 -2
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +11 -5
- package/dist/prompt/coding-agent-doctrine.js.map +1 -1
- package/dist/provider/capabilities.d.ts +5 -1
- package/dist/provider/capabilities.d.ts.map +1 -1
- package/dist/provider/capabilities.js +24 -0
- package/dist/provider/capabilities.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +6 -2
- package/dist/provider/fallback.js.map +1 -1
- package/dist/public-runtime.d.ts +4 -1
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +3 -1
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +2 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +2 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +2 -0
- package/dist/public-types.d.ts.map +1 -1
- package/dist/registry/tool/execute.d.ts +3 -0
- package/dist/registry/tool/execute.d.ts.map +1 -1
- package/dist/registry/tool/execute.js +11 -80
- package/dist/registry/tool/execute.js.map +1 -1
- package/dist/run/command-gate.d.ts.map +1 -1
- package/dist/run/command-gate.js +65 -12
- package/dist/run/command-gate.js.map +1 -1
- package/dist/run/index.d.ts +2 -0
- package/dist/run/index.d.ts.map +1 -1
- package/dist/run/index.js +1 -0
- package/dist/run/index.js.map +1 -1
- package/dist/run/memory-promoter.d.ts +3 -35
- package/dist/run/memory-promoter.d.ts.map +1 -1
- package/dist/run/memory-promoter.js +32 -40
- package/dist/run/memory-promoter.js.map +1 -1
- package/dist/run/memory-recall.d.ts +25 -0
- package/dist/run/memory-recall.d.ts.map +1 -0
- package/dist/run/memory-recall.js +228 -0
- package/dist/run/memory-recall.js.map +1 -0
- package/dist/run/reporter.d.ts.map +1 -1
- package/dist/run/reporter.js +2 -0
- package/dist/run/reporter.js.map +1 -1
- package/dist/run/token-budget.d.ts +12 -4
- package/dist/run/token-budget.d.ts.map +1 -1
- package/dist/run/token-budget.js +49 -14
- package/dist/run/token-budget.js.map +1 -1
- package/dist/run/workspace-fingerprint.d.ts +10 -3
- package/dist/run/workspace-fingerprint.d.ts.map +1 -1
- package/dist/run/workspace-fingerprint.js +17 -8
- package/dist/run/workspace-fingerprint.js.map +1 -1
- package/dist/runtime/query/checkpoint.d.ts +14 -0
- package/dist/runtime/query/checkpoint.d.ts.map +1 -1
- package/dist/runtime/query/checkpoint.js +82 -1
- package/dist/runtime/query/checkpoint.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +8 -3
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +94 -30
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +7 -8
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +55 -7
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +12 -0
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +348 -38
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/native-output.d.ts +10 -0
- package/dist/runtime/query/iteration/native-output.d.ts.map +1 -0
- package/dist/runtime/query/iteration/native-output.js +32 -0
- package/dist/runtime/query/iteration/native-output.js.map +1 -0
- package/dist/runtime/query/iteration/phases/advisory.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.js +18 -13
- package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts +2 -0
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +32 -11
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +9 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/iteration/phases/working-memory.d.ts +1 -1
- package/dist/runtime/query/iteration/phases/working-memory.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/working-memory.js +23 -6
- package/dist/runtime/query/iteration/phases/working-memory.js.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.js +12 -0
- package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
- package/dist/runtime/query/observation-context.d.ts +10 -0
- package/dist/runtime/query/observation-context.d.ts.map +1 -0
- package/dist/runtime/query/observation-context.js +84 -0
- package/dist/runtime/query/observation-context.js.map +1 -0
- package/dist/runtime/query/request-context.d.ts +30 -0
- package/dist/runtime/query/request-context.d.ts.map +1 -0
- package/dist/runtime/query/request-context.js +76 -0
- package/dist/runtime/query/request-context.js.map +1 -0
- package/dist/runtime/query/request-rich-content.d.ts.map +1 -1
- package/dist/runtime/query/request-rich-content.js +6 -3
- package/dist/runtime/query/request-rich-content.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +3 -0
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +4 -1
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/steering.d.ts +6 -2
- package/dist/runtime/query/steering.d.ts.map +1 -1
- package/dist/runtime/query/steering.js +17 -2
- package/dist/runtime/query/steering.js.map +1 -1
- package/dist/runtime/query/tool-call-budget.d.ts +19 -0
- package/dist/runtime/query/tool-call-budget.d.ts.map +1 -0
- package/dist/runtime/query/tool-call-budget.js +110 -0
- package/dist/runtime/query/tool-call-budget.js.map +1 -0
- package/dist/runtime/query/tooling.d.ts +3 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +5 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/sandbox/file-walk-program.d.ts.map +1 -1
- package/dist/sandbox/file-walk-program.js +8 -0
- package/dist/sandbox/file-walk-program.js.map +1 -1
- package/dist/sandbox/file-walk.d.ts.map +1 -1
- package/dist/sandbox/file-walk.js +9 -0
- package/dist/sandbox/file-walk.js.map +1 -1
- package/dist/scheduler/completion-inbox.d.ts.map +1 -1
- package/dist/scheduler/completion-inbox.js +3 -0
- package/dist/scheduler/completion-inbox.js.map +1 -1
- package/dist/scheduler/local.d.ts.map +1 -1
- package/dist/scheduler/local.js +1 -0
- package/dist/scheduler/local.js.map +1 -1
- package/dist/session/handoff/capacity.d.ts +2 -2
- package/dist/session/handoff/capacity.d.ts.map +1 -1
- package/dist/session/handoff/capacity.js +3 -2
- package/dist/session/handoff/capacity.js.map +1 -1
- package/dist/store/memory/disk.d.ts +6 -2
- package/dist/store/memory/disk.d.ts.map +1 -1
- package/dist/store/memory/disk.js +42 -25
- package/dist/store/memory/disk.js.map +1 -1
- package/dist/store/memory/index.d.ts +4 -0
- package/dist/store/memory/index.d.ts.map +1 -1
- package/dist/store/memory/index.js +55 -19
- package/dist/store/memory/index.js.map +1 -1
- package/dist/store/memory/memory.d.ts +3 -2
- package/dist/store/memory/memory.d.ts.map +1 -1
- package/dist/store/memory/memory.js +11 -2
- package/dist/store/memory/memory.js.map +1 -1
- package/dist/store/memory/operation-lock.d.ts +11 -0
- package/dist/store/memory/operation-lock.d.ts.map +1 -0
- package/dist/store/memory/operation-lock.js +94 -0
- package/dist/store/memory/operation-lock.js.map +1 -0
- package/dist/store/run/token-budget-disk.js +3 -0
- package/dist/store/run/token-budget-disk.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +60 -10
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +12 -26
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/file-write-result.d.ts +4 -0
- package/dist/tools/builtins/file-write-result.d.ts.map +1 -0
- package/dist/tools/builtins/file-write-result.js +72 -0
- package/dist/tools/builtins/file-write-result.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +8 -1
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +10 -0
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/job.d.ts.map +1 -1
- package/dist/tools/builtins/job.js +6 -0
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +10 -0
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/run-code.d.ts +6 -0
- package/dist/tools/builtins/run-code.d.ts.map +1 -1
- package/dist/tools/builtins/run-code.js +15 -4
- package/dist/tools/builtins/run-code.js.map +1 -1
- package/dist/tools/builtins/search-tools.d.ts.map +1 -1
- package/dist/tools/builtins/search-tools.js +14 -2
- package/dist/tools/builtins/search-tools.js.map +1 -1
- package/dist/tools/builtins/write-file.d.ts.map +1 -1
- package/dist/tools/builtins/write-file.js +30 -44
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/defineTool.d.ts +2 -0
- package/dist/tools/defineTool.d.ts.map +1 -1
- package/dist/tools/defineTool.js +3 -0
- package/dist/tools/defineTool.js.map +1 -1
- package/dist/tools/file-read-tracker.d.ts +4 -0
- package/dist/tools/file-read-tracker.d.ts.map +1 -0
- package/dist/tools/file-read-tracker.js +16 -0
- package/dist/tools/file-read-tracker.js.map +1 -0
- package/dist/tools/memory/delete.d.ts +4 -0
- package/dist/tools/memory/delete.d.ts.map +1 -0
- package/dist/tools/memory/delete.js +26 -0
- package/dist/tools/memory/delete.js.map +1 -0
- package/dist/tools/memory/index.d.ts +2 -0
- package/dist/tools/memory/index.d.ts.map +1 -1
- package/dist/tools/memory/index.js +6 -0
- package/dist/tools/memory/index.js.map +1 -1
- package/dist/tools/memory/save.d.ts.map +1 -1
- package/dist/tools/memory/save.js +3 -2
- package/dist/tools/memory/save.js.map +1 -1
- package/dist/tools/memory/search.d.ts.map +1 -1
- package/dist/tools/memory/search.js +12 -5
- package/dist/tools/memory/search.js.map +1 -1
- package/dist/tools/memory/update.d.ts +4 -0
- package/dist/tools/memory/update.d.ts.map +1 -0
- package/dist/tools/memory/update.js +47 -0
- package/dist/tools/memory/update.js.map +1 -0
- package/dist/tools/task/list.js +2 -2
- package/dist/tools/task/list.js.map +1 -1
- package/dist/types/agent/scheduler.d.ts +5 -0
- package/dist/types/agent/scheduler.d.ts.map +1 -1
- package/dist/types/agent/task.d.ts +10 -0
- package/dist/types/agent/task.d.ts.map +1 -1
- package/dist/types/computer-use/index.d.ts +6 -0
- package/dist/types/computer-use/index.d.ts.map +1 -1
- package/dist/types/computer-use/index.js.map +1 -1
- package/dist/types/errors/catalog.js +1 -1
- package/dist/types/errors/catalog.js.map +1 -1
- package/dist/types/hitl/index.d.ts +11 -1
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/memory/index.d.ts +14 -1
- package/dist/types/memory/index.d.ts.map +1 -1
- package/dist/types/plugin/index.d.ts +7 -0
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/project/entity.d.ts +1 -0
- package/dist/types/project/entity.d.ts.map +1 -1
- package/dist/types/provider/chat.d.ts +4 -0
- package/dist/types/provider/chat.d.ts.map +1 -1
- package/dist/types/provider/config.d.ts +4 -0
- package/dist/types/provider/config.d.ts.map +1 -1
- package/dist/types/provider/model.d.ts +4 -0
- package/dist/types/provider/model.d.ts.map +1 -1
- package/dist/types/provider/stream.d.ts +6 -0
- package/dist/types/provider/stream.d.ts.map +1 -1
- package/dist/types/run/answer-review.d.ts +2 -0
- package/dist/types/run/answer-review.d.ts.map +1 -1
- package/dist/types/run/config.d.ts +2 -0
- package/dist/types/run/config.d.ts.map +1 -1
- package/dist/types/run/events.d.ts +19 -0
- package/dist/types/run/events.d.ts.map +1 -1
- package/dist/types/run/events.js.map +1 -1
- package/dist/types/run/prepare-step.d.ts +19 -1
- package/dist/types/run/prepare-step.d.ts.map +1 -1
- package/dist/types/sandbox/index.d.ts +1 -0
- package/dist/types/sandbox/index.d.ts.map +1 -1
- package/dist/types/sandbox/index.js.map +1 -1
- package/dist/types/structured-output/index.d.ts +8 -2
- package/dist/types/structured-output/index.d.ts.map +1 -1
- package/dist/types/tool/index.d.ts +15 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +4 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/dist/utils/json-snapshot.d.ts +3 -0
- package/dist/utils/json-snapshot.d.ts.map +1 -0
- package/dist/utils/json-snapshot.js +79 -0
- package/dist/utils/json-snapshot.js.map +1 -0
- package/package.json +4 -2
- package/src/bridge/a2a/mapper.ts +4 -0
- package/src/bridge/sse/mapper.ts +16 -1
- package/src/compaction/extractor.ts +21 -8
- package/src/compaction/manager.ts +11 -1
- package/src/compaction/verifier.ts +80 -18
- package/src/config/runtime.ts +2 -0
- package/src/contracts/api.ts +1 -0
- package/src/eval/harness-verification.ts +270 -0
- package/src/eval/index.ts +10 -0
- package/src/execution/code-runtime/json-value.ts +82 -0
- package/src/execution/code-runtime/worker-program.ts +239 -0
- package/src/execution/code-runtime/worker.ts +164 -111
- package/src/manager/agent/lifecycle.ts +352 -54
- package/src/manager/run/persistence.ts +7 -0
- package/src/prompt/coding-agent-doctrine.ts +11 -5
- package/src/provider/capabilities.ts +41 -1
- package/src/provider/fallback.ts +9 -2
- package/src/public-runtime.ts +6 -0
- package/src/public-tools.ts +7 -1
- package/src/public-types.ts +7 -0
- package/src/registry/tool/execute.ts +17 -79
- package/src/run/command-gate.ts +61 -13
- package/src/run/index.ts +2 -0
- package/src/run/memory-promoter.ts +32 -40
- package/src/run/memory-recall.ts +254 -0
- package/src/run/reporter.ts +2 -0
- package/src/run/token-budget.ts +55 -14
- package/src/run/workspace-fingerprint.ts +17 -8
- package/src/runtime/query/checkpoint.ts +96 -2
- package/src/runtime/query/executor.ts +100 -29
- package/src/runtime/query/index.ts +76 -16
- package/src/runtime/query/iteration/index.ts +405 -46
- package/src/runtime/query/iteration/native-output.ts +35 -0
- package/src/runtime/query/iteration/phases/advisory.ts +21 -11
- package/src/runtime/query/iteration/phases/compaction.ts +36 -14
- package/src/runtime/query/iteration/phases/context.ts +9 -0
- package/src/runtime/query/iteration/phases/tool-review.ts +5 -1
- package/src/runtime/query/iteration/phases/working-memory.ts +23 -5
- package/src/runtime/query/iteration/stream-turn.ts +15 -0
- package/src/runtime/query/observation-context.ts +86 -0
- package/src/runtime/query/request-context.ts +103 -0
- package/src/runtime/query/request-rich-content.ts +8 -3
- package/src/runtime/query/review-policy.ts +7 -1
- package/src/runtime/query/steering.ts +19 -2
- package/src/runtime/query/tool-call-budget.ts +115 -0
- package/src/runtime/query/tooling.ts +8 -0
- package/src/sandbox/file-walk-program.ts +8 -0
- package/src/sandbox/file-walk.ts +9 -0
- package/src/scheduler/completion-inbox.ts +3 -0
- package/src/scheduler/local.ts +1 -0
- package/src/session/handoff/capacity.ts +7 -3
- package/src/store/memory/disk.ts +52 -30
- package/src/store/memory/index.ts +75 -27
- package/src/store/memory/memory.ts +18 -6
- package/src/store/memory/operation-lock.ts +99 -0
- package/src/store/run/token-budget-disk.ts +3 -0
- package/src/tools/builtins/computer-use.ts +61 -7
- package/src/tools/builtins/edit.ts +12 -27
- package/src/tools/builtins/file-write-result.ts +84 -0
- package/src/tools/builtins/glob.ts +7 -1
- package/src/tools/builtins/grep.ts +10 -0
- package/src/tools/builtins/job.ts +8 -0
- package/src/tools/builtins/read-file.ts +10 -0
- package/src/tools/builtins/run-code.ts +24 -5
- package/src/tools/builtins/search-tools.ts +18 -3
- package/src/tools/builtins/write-file.ts +39 -44
- package/src/tools/defineTool.ts +5 -0
- package/src/tools/file-read-tracker.ts +16 -0
- package/src/tools/memory/delete.ts +29 -0
- package/src/tools/memory/index.ts +6 -0
- package/src/tools/memory/save.ts +4 -2
- package/src/tools/memory/search.ts +12 -5
- package/src/tools/memory/update.ts +50 -0
- package/src/tools/task/list.ts +2 -2
- package/src/types/agent/scheduler.ts +6 -0
- package/src/types/agent/task.ts +13 -0
- package/src/types/computer-use/index.ts +6 -0
- package/src/types/errors/catalog.ts +1 -1
- package/src/types/hitl/index.ts +11 -1
- package/src/types/memory/index.ts +16 -1
- package/src/types/plugin/index.ts +10 -0
- package/src/types/project/entity.ts +1 -0
- package/src/types/provider/chat.ts +2 -0
- package/src/types/provider/config.ts +4 -0
- package/src/types/provider/model.ts +4 -0
- package/src/types/provider/stream.ts +7 -0
- package/src/types/run/answer-review.ts +2 -0
- package/src/types/run/config.ts +2 -0
- package/src/types/run/events.ts +20 -0
- package/src/types/run/prepare-step.ts +19 -1
- package/src/types/sandbox/index.ts +1 -0
- package/src/types/structured-output/index.ts +15 -2
- package/src/types/tool/index.ts +16 -0
- package/src/types/tool/presentation.ts +4 -0
- package/src/utils/json-snapshot.ts +76 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialize application data before sending it to the interpreter.
|
|
3
|
+
*
|
|
4
|
+
* JSON.stringify alone can allocate an arbitrarily large intermediate string.
|
|
5
|
+
* This writer accounts for each piece, refuses cycles and non-JSON values, and
|
|
6
|
+
* limits nesting. It never installs application objects in the guest realm.
|
|
7
|
+
* Undefined is supported only as a top-level result, represented out of band.
|
|
8
|
+
*/
|
|
9
|
+
export function encodeCodeValue(value: unknown, maxBytes: number): string | undefined {
|
|
10
|
+
if (value === undefined) return undefined
|
|
11
|
+
const pieces: string[] = []
|
|
12
|
+
const ancestors = new Set<object>()
|
|
13
|
+
let bytes = 0
|
|
14
|
+
|
|
15
|
+
const append = (text: string): void => {
|
|
16
|
+
bytes += Buffer.byteLength(text)
|
|
17
|
+
if (bytes > maxBytes) throw new Error(`Code runtime value exceeds ${maxBytes} bytes.`)
|
|
18
|
+
pieces.push(text)
|
|
19
|
+
}
|
|
20
|
+
const quote = (text: string): void => {
|
|
21
|
+
if (text.length > maxBytes - bytes) {
|
|
22
|
+
throw new Error(`Code runtime value exceeds ${maxBytes} bytes.`)
|
|
23
|
+
}
|
|
24
|
+
append(JSON.stringify(text))
|
|
25
|
+
}
|
|
26
|
+
const visit = (item: unknown, depth: number): void => {
|
|
27
|
+
if (depth > 64) throw new Error('Code runtime value exceeds 64 levels of nesting.')
|
|
28
|
+
if (item === null) {
|
|
29
|
+
append('null')
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
if (typeof item === 'string') {
|
|
33
|
+
quote(item)
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
if (typeof item === 'boolean') {
|
|
37
|
+
append(String(item))
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
if (typeof item === 'number' && Number.isFinite(item)) {
|
|
41
|
+
append(String(item))
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
if (typeof item !== 'object') throw new Error('Code runtime values must be JSON-safe.')
|
|
45
|
+
if (ancestors.has(item)) throw new Error('Code runtime values must not contain cycles.')
|
|
46
|
+
const array = Array.isArray(item)
|
|
47
|
+
if (
|
|
48
|
+
!array &&
|
|
49
|
+
Object.getPrototypeOf(item) !== Object.prototype &&
|
|
50
|
+
Object.getPrototypeOf(item) !== null
|
|
51
|
+
) {
|
|
52
|
+
throw new Error('Code runtime values must contain only plain objects and arrays.')
|
|
53
|
+
}
|
|
54
|
+
ancestors.add(item)
|
|
55
|
+
if (array) {
|
|
56
|
+
if (item.length > maxBytes) throw new Error(`Code runtime value exceeds ${maxBytes} bytes.`)
|
|
57
|
+
append('[')
|
|
58
|
+
for (let index = 0; index < item.length; index++) {
|
|
59
|
+
if (index > 0) append(',')
|
|
60
|
+
visit(item[index], depth + 1)
|
|
61
|
+
}
|
|
62
|
+
append(']')
|
|
63
|
+
} else {
|
|
64
|
+
append('{')
|
|
65
|
+
const keys = Object.keys(item)
|
|
66
|
+
for (const [index, key] of keys.entries()) {
|
|
67
|
+
if (index > 0) append(',')
|
|
68
|
+
quote(key)
|
|
69
|
+
append(':')
|
|
70
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, key)
|
|
71
|
+
if (!descriptor || !('value' in descriptor)) {
|
|
72
|
+
throw new Error('Code runtime values must not contain accessors.')
|
|
73
|
+
}
|
|
74
|
+
visit(descriptor.value, depth + 1)
|
|
75
|
+
}
|
|
76
|
+
append('}')
|
|
77
|
+
}
|
|
78
|
+
ancestors.delete(item)
|
|
79
|
+
}
|
|
80
|
+
visit(value, 0)
|
|
81
|
+
return pieces.join('')
|
|
82
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trusted worker bootstrap. Only vm.evalCode executes the untrusted program.
|
|
3
|
+
* Kept as source so installed/bundled SDKs need no separate worker entry file.
|
|
4
|
+
* No Node object or callback is injected into QuickJS: its native functions
|
|
5
|
+
* exchange bounded strings and interpreter-owned promise handles.
|
|
6
|
+
*/
|
|
7
|
+
export const QUICKJS_WORKER_SOURCE = `
|
|
8
|
+
const { parentPort, workerData } = require('node:worker_threads')
|
|
9
|
+
const { newQuickJSWASMModuleFromVariant, newVariant } = require(workerData.corePath)
|
|
10
|
+
const variantModule = require(workerData.variantPath)
|
|
11
|
+
const variant = variantModule.default || variantModule
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
const limits = workerData.limits
|
|
15
|
+
const pages = Math.floor(limits.memoryLimitBytes / 65536)
|
|
16
|
+
const wasmMemory = new WebAssembly.Memory({ initial: Math.min(256, pages), maximum: pages })
|
|
17
|
+
const module = await newQuickJSWASMModuleFromVariant(newVariant(variant, { wasmMemory }))
|
|
18
|
+
const runtime = module.newRuntime()
|
|
19
|
+
runtime.setMemoryLimit(limits.memoryLimitBytes)
|
|
20
|
+
runtime.setMaxStackSize(512 * 1024)
|
|
21
|
+
runtime.setInterruptHandler(() => Date.now() >= workerData.deadline)
|
|
22
|
+
const vm = runtime.newContext()
|
|
23
|
+
const pending = new Map()
|
|
24
|
+
let nextId = 0
|
|
25
|
+
let printBytes = 0
|
|
26
|
+
let printCount = 0
|
|
27
|
+
let truncated = false
|
|
28
|
+
let ended = false
|
|
29
|
+
let pumping = false
|
|
30
|
+
let scheduled = false
|
|
31
|
+
let program
|
|
32
|
+
const owned = []
|
|
33
|
+
|
|
34
|
+
function keep(result) {
|
|
35
|
+
const handle = vm.unwrapResult(result)
|
|
36
|
+
owned.push(handle)
|
|
37
|
+
return handle
|
|
38
|
+
}
|
|
39
|
+
// Capture intrinsics before the program may replace its own globals.
|
|
40
|
+
const encode = keep(vm.evalCode('(' + function () {
|
|
41
|
+
const stringify = JSON.stringify
|
|
42
|
+
const finite = Number.isFinite
|
|
43
|
+
const prototype = Object.getPrototypeOf
|
|
44
|
+
const plain = Object.prototype
|
|
45
|
+
const isArray = Array.isArray
|
|
46
|
+
const keys = Object.keys
|
|
47
|
+
const descriptor = Object.getOwnPropertyDescriptor
|
|
48
|
+
return function (value) {
|
|
49
|
+
if (value === undefined) return undefined
|
|
50
|
+
const parents = new Set()
|
|
51
|
+
function check(item, depth) {
|
|
52
|
+
const kind = typeof item
|
|
53
|
+
if (item === null || kind === 'string' || kind === 'boolean' || (kind === 'number' && finite(item))) return
|
|
54
|
+
if (kind !== 'object' || depth > 64 || parents.has(item)) throw new Error('Code runtime values must be JSON-safe.')
|
|
55
|
+
if (!isArray(item) && prototype(item) !== plain && prototype(item) !== null) throw new Error('Code runtime values must use plain objects and arrays.')
|
|
56
|
+
parents.add(item)
|
|
57
|
+
for (const key of keys(item)) {
|
|
58
|
+
const property = descriptor(item, key)
|
|
59
|
+
if (!property || !('value' in property)) throw new Error('Code runtime values must not contain accessors.')
|
|
60
|
+
check(property.value, depth + 1)
|
|
61
|
+
}
|
|
62
|
+
parents.delete(item)
|
|
63
|
+
}
|
|
64
|
+
check(value, 0)
|
|
65
|
+
return stringify(value)
|
|
66
|
+
}
|
|
67
|
+
}.toString() + ')()'))
|
|
68
|
+
const decode = keep(vm.evalCode('(function () { const parse = JSON.parse; return function(value) { return parse(value); }; })()'))
|
|
69
|
+
const errorText = keep(vm.evalCode('(function () { const string = String; return function(error) { try { return typeof error.message === "string" ? error.message : string(error); } catch { return "The program failed."; } }; })()'))
|
|
70
|
+
|
|
71
|
+
function textValue(handle, maximum) {
|
|
72
|
+
const length = vm.getProp(handle, 'length')
|
|
73
|
+
try {
|
|
74
|
+
if (vm.getNumber(length) > maximum) throw new Error('Code runtime value exceeds ' + maximum + ' bytes.')
|
|
75
|
+
} finally { length.dispose() }
|
|
76
|
+
const text = vm.getString(handle)
|
|
77
|
+
if (Buffer.byteLength(text) > maximum) throw new Error('Code runtime value exceeds ' + maximum + ' bytes.')
|
|
78
|
+
return text
|
|
79
|
+
}
|
|
80
|
+
function serialize(handle, maximum) {
|
|
81
|
+
if (vm.typeof(handle) === 'undefined') return undefined
|
|
82
|
+
const encoded = vm.callFunction(encode, vm.undefined, handle)
|
|
83
|
+
if (encoded.error) {
|
|
84
|
+
encoded.error.dispose()
|
|
85
|
+
throw new Error('Code runtime values must be JSON-safe and serializable.')
|
|
86
|
+
}
|
|
87
|
+
try { return textValue(encoded.value, maximum) }
|
|
88
|
+
finally { encoded.value.dispose() }
|
|
89
|
+
}
|
|
90
|
+
function failureText(handle) {
|
|
91
|
+
const result = vm.callFunction(errorText, vm.undefined, handle)
|
|
92
|
+
if (result.error) { result.error.dispose(); return 'The program failed.' }
|
|
93
|
+
// Diagnostics have their own fixed bound. A deliberately tiny data
|
|
94
|
+
// allowance must not erase the reason or the no-replay guidance for an
|
|
95
|
+
// effect that completed but whose return value could not be delivered.
|
|
96
|
+
try { return textValue(result.value, 4096) }
|
|
97
|
+
catch { return 'The program failed with an oversized error.' }
|
|
98
|
+
finally { result.value.dispose() }
|
|
99
|
+
}
|
|
100
|
+
function dispose() {
|
|
101
|
+
for (const deferred of pending.values()) deferred.dispose()
|
|
102
|
+
pending.clear()
|
|
103
|
+
if (program && program.alive) program.dispose()
|
|
104
|
+
for (const handle of owned) if (handle.alive) handle.dispose()
|
|
105
|
+
vm.dispose()
|
|
106
|
+
runtime.dispose()
|
|
107
|
+
}
|
|
108
|
+
function finish(message) {
|
|
109
|
+
if (ended) return
|
|
110
|
+
ended = true
|
|
111
|
+
// A guest OOM can make QuickJS teardown throw. The worker still owns
|
|
112
|
+
// the whole WASM module and is terminated by its parent on settlement.
|
|
113
|
+
try { dispose() } catch {}
|
|
114
|
+
parentPort.postMessage(message)
|
|
115
|
+
}
|
|
116
|
+
function fail(error) {
|
|
117
|
+
finish(Date.now() >= workerData.deadline
|
|
118
|
+
? { kind: 'timed-out' }
|
|
119
|
+
: { kind: 'error', error: String(error && error.message || error).slice(0, 4096) })
|
|
120
|
+
}
|
|
121
|
+
function schedule() {
|
|
122
|
+
if (ended || scheduled) return
|
|
123
|
+
scheduled = true
|
|
124
|
+
setImmediate(() => { scheduled = false; pump() })
|
|
125
|
+
}
|
|
126
|
+
function pump() {
|
|
127
|
+
if (ended || pumping) return
|
|
128
|
+
pumping = true
|
|
129
|
+
try {
|
|
130
|
+
const jobs = runtime.executePendingJobs(32)
|
|
131
|
+
if (jobs.error) {
|
|
132
|
+
const error = failureText(jobs.error)
|
|
133
|
+
jobs.error.dispose()
|
|
134
|
+
throw new Error(error)
|
|
135
|
+
}
|
|
136
|
+
const state = vm.getPromiseState(program)
|
|
137
|
+
if (state.type === 'fulfilled') {
|
|
138
|
+
let json
|
|
139
|
+
try { json = serialize(state.value, limits.maxValueBytes) }
|
|
140
|
+
finally { state.value.dispose() }
|
|
141
|
+
finish({ kind: 'done', json })
|
|
142
|
+
} else if (state.type === 'rejected') {
|
|
143
|
+
let error
|
|
144
|
+
try { error = failureText(state.error) }
|
|
145
|
+
finally { state.error.dispose() }
|
|
146
|
+
fail(new Error(error))
|
|
147
|
+
} else if (runtime.hasPendingJob()) schedule()
|
|
148
|
+
} catch (error) { fail(error) }
|
|
149
|
+
finally { pumping = false }
|
|
150
|
+
}
|
|
151
|
+
function rejected(message) {
|
|
152
|
+
const deferred = vm.newPromise()
|
|
153
|
+
const error = vm.newError(message)
|
|
154
|
+
deferred.reject(error)
|
|
155
|
+
error.dispose()
|
|
156
|
+
return deferred.handle
|
|
157
|
+
}
|
|
158
|
+
const call = vm.newFunction('call', (name, input = vm.undefined) => {
|
|
159
|
+
try {
|
|
160
|
+
if (nextId >= limits.maxHostCalls) return rejected('Code runtime host-call limit reached.')
|
|
161
|
+
if (pending.size >= limits.maxPendingHostCalls) return rejected('Code runtime pending host-call limit reached.')
|
|
162
|
+
if (vm.typeof(name) !== 'string') return rejected('A host call requires a tool name string.')
|
|
163
|
+
const tool = textValue(name, Math.min(limits.maxValueBytes, 1024))
|
|
164
|
+
const json = serialize(input, limits.maxValueBytes)
|
|
165
|
+
const id = ++nextId
|
|
166
|
+
const deferred = vm.newPromise()
|
|
167
|
+
pending.set(id, deferred)
|
|
168
|
+
parentPort.postMessage({ kind: 'call', id, name: tool, json })
|
|
169
|
+
return deferred.handle
|
|
170
|
+
} catch (error) { return rejected(String(error.message || error)) }
|
|
171
|
+
})
|
|
172
|
+
vm.setProp(vm.global, 'call', call)
|
|
173
|
+
call.dispose()
|
|
174
|
+
const print = vm.newFunction('print', (...values) => {
|
|
175
|
+
if (truncated) return vm.undefined
|
|
176
|
+
try {
|
|
177
|
+
const parts = []
|
|
178
|
+
let remaining = workerData.maxOutputBytes - printBytes - (printCount > 0 ? 1 : 0)
|
|
179
|
+
for (const value of values) {
|
|
180
|
+
if (parts.length) remaining--
|
|
181
|
+
if (remaining < 0) throw new Error('output limit')
|
|
182
|
+
const text = vm.typeof(value) === 'string' ? textValue(value, remaining) : serialize(value, remaining)
|
|
183
|
+
const part = text === undefined ? '' : text
|
|
184
|
+
remaining -= Buffer.byteLength(part)
|
|
185
|
+
parts.push(part)
|
|
186
|
+
}
|
|
187
|
+
const line = parts.join(' ')
|
|
188
|
+
const size = Buffer.byteLength(line) + (printCount > 0 ? 1 : 0)
|
|
189
|
+
if (printBytes + size > workerData.maxOutputBytes) throw new Error('output limit')
|
|
190
|
+
// Empty prints consume a newline after the first, so the byte
|
|
191
|
+
// budget also bounds the number of worker messages.
|
|
192
|
+
printBytes += size
|
|
193
|
+
printCount++
|
|
194
|
+
parentPort.postMessage({ kind: 'print', line })
|
|
195
|
+
} catch {
|
|
196
|
+
truncated = true
|
|
197
|
+
parentPort.postMessage({ kind: 'truncated' })
|
|
198
|
+
}
|
|
199
|
+
return vm.undefined
|
|
200
|
+
})
|
|
201
|
+
vm.setProp(vm.global, 'print', print)
|
|
202
|
+
print.dispose()
|
|
203
|
+
|
|
204
|
+
parentPort.on('message', (message) => {
|
|
205
|
+
if (ended || message.kind !== 'call-result') return
|
|
206
|
+
const deferred = pending.get(message.id)
|
|
207
|
+
if (!deferred) return
|
|
208
|
+
pending.delete(message.id)
|
|
209
|
+
try {
|
|
210
|
+
if (message.ok) {
|
|
211
|
+
let value = vm.undefined
|
|
212
|
+
if (message.json !== undefined) {
|
|
213
|
+
if (typeof message.json !== 'string' || Buffer.byteLength(message.json) > limits.maxValueBytes) throw new Error('Host result exceeds the value limit.')
|
|
214
|
+
const input = vm.newString(message.json)
|
|
215
|
+
try { value = vm.unwrapResult(vm.callFunction(decode, vm.undefined, input)) }
|
|
216
|
+
finally { input.dispose() }
|
|
217
|
+
}
|
|
218
|
+
deferred.resolve(value)
|
|
219
|
+
if (value !== vm.undefined) value.dispose()
|
|
220
|
+
} else {
|
|
221
|
+
const error = vm.newError(String(message.error).slice(0, 4096))
|
|
222
|
+
deferred.reject(error)
|
|
223
|
+
error.dispose()
|
|
224
|
+
}
|
|
225
|
+
deferred.dispose()
|
|
226
|
+
schedule()
|
|
227
|
+
} catch (error) { deferred.dispose(); fail(error) }
|
|
228
|
+
})
|
|
229
|
+
try {
|
|
230
|
+
const result = vm.evalCode('(async () => {\\n' + workerData.source + '\\n})()', 'model-program.js')
|
|
231
|
+
if (result.error) {
|
|
232
|
+
const error = failureText(result.error)
|
|
233
|
+
result.error.dispose()
|
|
234
|
+
fail(new Error(error))
|
|
235
|
+
} else { program = result.value; pump() }
|
|
236
|
+
} catch (error) { fail(error) }
|
|
237
|
+
}
|
|
238
|
+
main().catch((error) => parentPort.postMessage({ kind: 'error', error: String(error && error.message || error).slice(0, 4096) }))
|
|
239
|
+
`
|