@deepseek-ai/dsh-tool-registry-ptc 0.0.0-stage → 0.2.3-alpha.4

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 (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.i18n.yaml +67 -0
  3. package/README.md +180 -2
  4. package/README.zh.md +181 -0
  5. package/lib/index.js +1422 -0
  6. package/lib/types/client.d.ts +3 -0
  7. package/lib/types/client.d.ts.map +1 -0
  8. package/lib/types/client.js +2 -0
  9. package/lib/types/client.js.map +1 -0
  10. package/lib/types/config.d.ts +12 -0
  11. package/lib/types/config.d.ts.map +1 -0
  12. package/lib/types/config.js +8 -0
  13. package/lib/types/config.js.map +1 -0
  14. package/lib/types/errors.d.ts +43 -0
  15. package/lib/types/errors.d.ts.map +1 -0
  16. package/lib/types/errors.js +53 -0
  17. package/lib/types/errors.js.map +1 -0
  18. package/lib/types/index.d.ts +121 -0
  19. package/lib/types/index.d.ts.map +1 -0
  20. package/lib/types/index.js +200 -0
  21. package/lib/types/index.js.map +1 -0
  22. package/lib/types/program-images.d.ts +18 -0
  23. package/lib/types/program-images.d.ts.map +1 -0
  24. package/lib/types/program-images.js +50 -0
  25. package/lib/types/program-images.js.map +1 -0
  26. package/lib/types/ptc-calls.d.ts +43 -0
  27. package/lib/types/ptc-calls.d.ts.map +1 -0
  28. package/lib/types/ptc-calls.js +130 -0
  29. package/lib/types/ptc-calls.js.map +1 -0
  30. package/lib/types/ptc.d.ts +58 -0
  31. package/lib/types/ptc.d.ts.map +1 -0
  32. package/lib/types/ptc.js +802 -0
  33. package/lib/types/ptc.js.map +1 -0
  34. package/lib/types/runtime-types.d.ts +47 -0
  35. package/lib/types/runtime-types.d.ts.map +1 -0
  36. package/lib/types/runtime-types.js +2 -0
  37. package/lib/types/runtime-types.js.map +1 -0
  38. package/lib/types/ts-types.d.ts +39 -0
  39. package/lib/types/ts-types.d.ts.map +1 -0
  40. package/lib/types/ts-types.js +309 -0
  41. package/lib/types/ts-types.js.map +1 -0
  42. package/lib/types/types.d.ts +72 -0
  43. package/lib/types/types.d.ts.map +1 -0
  44. package/lib/types/types.js +7 -0
  45. package/lib/types/types.js.map +1 -0
  46. package/locale/de.json +6 -0
  47. package/locale/en.json +6 -0
  48. package/locale/es.json +6 -0
  49. package/locale/fr.json +6 -0
  50. package/locale/ja.json +6 -0
  51. package/locale/pt.json +6 -0
  52. package/locale/ru.json +6 -0
  53. package/locale/zh.json +6 -0
  54. package/package.json +80 -3
  55. package/src/client.ts +2 -0
  56. package/src/config.ts +16 -0
  57. package/src/errors.ts +60 -0
  58. package/src/index.ts +241 -0
  59. package/src/program-images.ts +52 -0
  60. package/src/ptc-calls.ts +139 -0
  61. package/src/ptc.ts +898 -0
  62. package/src/runtime-types.ts +48 -0
  63. package/src/ts-types.ts +336 -0
  64. package/src/types.ts +70 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DeepSeek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Bilingual-pair consistency record for README.md (docs/i18n/README.md): per heading
2
+ # section, a hash of its English and Chinese blocks outside code blocks and generated regions.
3
+ # After editing either side, bring the other along and re-record with:
4
+ # pnpm run verify-translation-pairing --write packages/core/tool-registry-ptc/README.md
5
+ /:
6
+ en: 93b346254eff025a
7
+ zh: 69aa6364ca2ffa7d
8
+ /deepseek-ai-dsh-tool-registry-ptc:
9
+ en: 4658f251502ba754
10
+ zh: b2732c26a314a984
11
+ /deepseek-ai-dsh-tool-registry-ptc/summary:
12
+ en: e9b9f9044df9dd98
13
+ zh: 79d54cba394bbb67
14
+ /deepseek-ai-dsh-tool-registry-ptc/table-of-contents:
15
+ en: d152484eb41ac6b4
16
+ zh: 09388d293f9be9cb
17
+ /deepseek-ai-dsh-tool-registry-ptc/use-this-package:
18
+ en: 5184050040ac622a
19
+ zh: 5ac0010b1e824452
20
+ /deepseek-ai-dsh-tool-registry-ptc/understand-the-implementation:
21
+ en: 408579c083d4ea27
22
+ zh: a4e03e4fe1feb98e
23
+ /deepseek-ai-dsh-tool-registry-ptc/understand-the-implementation/ptc-mode:
24
+ en: 3e9df1f0e35002c5
25
+ zh: 398a3065351de855
26
+ /deepseek-ai-dsh-tool-registry-ptc/further-exploration:
27
+ en: 4e6282bc80b35d6c
28
+ zh: f6bda6aa2c884b14
29
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience:
30
+ en: 215e7ba838619b7b
31
+ zh: a311da3843709f90
32
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt:
33
+ en: eaefd7303f22977d
34
+ zh: 9c425f9501128224
35
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt/what-the-model-sees:
36
+ en: 2ec19e07636c8662
37
+ zh: c55552fe56b75ca8
38
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt/what-the-model-sees/typescript-ptc-mode-sdk-instructions-with-bash:
39
+ en: 3a8df15e4ae32f9e
40
+ zh: fc4a800ba363e835
41
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt/what-the-model-sees/tool-call-allowance-exhausted:
42
+ en: 079e063e47dff7ed
43
+ zh: 271ee8bb3441eb2e
44
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt/token-effect:
45
+ en: 0ae2c687fcf8f300
46
+ zh: 8a1c5a88a1ca9bbe
47
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/ptc-mode-schema-and-system-prompt/kv-cache-effect:
48
+ en: 9afb9ba68eb2c600
49
+ zh: 447b17b983a58774
50
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/included-program-tools:
51
+ en: d617b89ef75d0c89
52
+ zh: 84ddb49f15ad5536
53
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/included-program-tools/what-the-model-sees:
54
+ en: fa2832b7cb10076e
55
+ zh: 749b062623a14f36
56
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/included-program-tools/token-effect:
57
+ en: 753b5a832a1f7e28
58
+ zh: 91f77ec40d6adc94
59
+ /deepseek-ai-dsh-tool-registry-ptc/model-experience/included-program-tools/kv-cache-effect:
60
+ en: f4703836365dc3d4
61
+ zh: a09ff48a6550fd69
62
+ /deepseek-ai-dsh-tool-registry-ptc/known-limitations-and-deferred-work:
63
+ en: 2ecfbe831a1c6655
64
+ zh: c1cd7030cd6b9cc2
65
+ /deepseek-ai-dsh-tool-registry-ptc/known-limitations-and-deferred-work/dev-note:
66
+ en: e67f633dc6fe7773
67
+ zh: 3bb0c20c93f16f98
package/README.md CHANGED
@@ -1,3 +1,181 @@
1
- # Temporary Holding Version
1
+ ---
2
+ description: "Program-based tool execution, generated SDKs, and nested-call limits."
3
+ kind: "package-reference"
4
+ ---
2
5
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
6
+ # @deepseek-ai/dsh-tool-registry-ptc
7
+
8
+ English | [中文](README.zh.md)
9
+
10
+ ## Summary
11
+
12
+ Use this root registry when models should call tools through `run_code` programs, or include a named program tool whose programs call only the tools you bind. Mount a compatible PTC runtime and tool registry first. Scoped restrictions determine the generated SDK; only the program entry is directly callable. Configure the total tool-call limit per program and concurrency for parallel-safe subcalls.
13
+
14
+ ## Table of Contents
15
+
16
+ - [Use this package](#use-this-package)
17
+ - [Understand the implementation](#understand-the-implementation)
18
+ - [Further Exploration](#further-exploration)
19
+ - [Model Experience](#model-experience)
20
+ - [Known Limitations and Deferred Work](#known-limitations-and-deferred-work)
21
+ - [Dev Note](#dev-note)
22
+
23
+ -----
24
+
25
+ <a id="use-this-package"></a>
26
+ ## Use this package
27
+
28
+ Add this row to a composition with `dsh-tools` mounted. The plugin installs a PTC root in the mounting scope: its only entry, `run_code`, runs programs that call every tool the scope's Agents see, and the system prompt declares those tools as a generated SDK. Agents use the nearest root, and each scope allows one; the [tools package](../tools/README.md#compose-registries) owns root selection and unavailable roots.
29
+
30
+ ```yaml
31
+ - name: '@deepseek-ai/dsh-tool-registry-ptc'
32
+ config:
33
+ maxSubCallsPerMinute: 1024
34
+ maxParallelSubCalls: 10
35
+ ```
36
+
37
+ Root `run_code` calls are exclusive and execute serially. `maxParallelSubCalls` limits parallel-safe nested calls within each program and defaults to `10`; it must be a positive integer, with `1` selecting serial execution. Exclusive calls form barriers.
38
+
39
+ `maxSubCallsPerMinute` defaults to `1024` and must be a positive integer. Each `run_code` gets `floor(resolved timeoutMs / 60000 × maxSubCallsPerMinute)` tool-call attempts: the default Node timeout of two minutes allows 2048. Runtime defaults and timeout caps apply before this calculation; no deadline means no finite call allowance. This is a total allowance, not a rolling window, so calls may happen in a burst. All tool bindings share the count, including queued calls, invalid arguments, and failed tools. The next attempt terminates the entire program through the runtime abort path, cancels active subcalls, and abandons queued calls before returning `CODE_RUN_FAILED`. As with a timeout, catching a binding error cannot keep the program running; retained runtime state is discarded. Completed tool effects remain, and the next program starts a fresh count.
40
+
41
+ `PtcRuntime` providers must report `language: 'typescript'`. A PTC registry rejects other languages during prompt assembly and execution, without falling back to native tools; a registry constructed with a fixed `runtime` rejects it at construction. Schema reads and execution also validate the current provider. The registry generates a TypeScript SDK and the entry's schema; the provider supplies execution.
42
+
43
+ Tool calls retain their declared JSON return values. Passing an unchanged object or array result directly to `inspect` displays the tool's final text-only output. Fields, copies, modified results, and results nested inside another value use ordinary Node formatting. Use `inspect({ ...result })` for an object's raw structure or `inspect(result.lines)` for selected fields. Scalar results use ordinary formatting; Images and mixed text/image outputs follow the image rules below.
44
+
45
+ This package declares `ctx.ptcRuntime` and `ctx.systemPrompt` as required injections; Agent-owned programs need `ctx.workingDirectory`. The plugin installs its root only after its runtime is available; when that runtime unloads, the root selection stays unavailable, and new batches and prompt assemblies fail with `TOOL_REGISTRY_UNAVAILABLE` until the runtime returns. An enabled PTC root without a runtime stops application startup. An unavailable PTC root never falls back to native execution.
46
+
47
+ Shared dispatch events and the `ptc-mode` message-source declaration live in `@deepseek-ai/dsh-tool-registry-ptc/types`; Client code imports the same declarations through `@deepseek-ai/dsh-tool-registry-ptc/client`. Both entries exclude Host service declarations. Host policy extensions use `import type` from the package entry for `PtcDispatchLog` and `tools/ptc-dispatch-log` without mounting the plugin.
48
+
49
+ To offer a named program tool beside the tools an Agent calls directly, construct `new PtcToolRegistry(ctx, { name, description?, runtime?, environment?, config? })`, register the tools its programs call, and include the registry with `ctx.tools.include()`. Its only entry is the program tool named `name`. Its registrations and inclusions are the bindings its programs call: programs can call only those bindings, even when the Agent can call other tools, and the bindings never enter the including scope's tools. Nested calls retain normal approvals, guards, dispatch logs, screenshots, deferred context, and turn completion. `config` accepts the same call-allowance and concurrency settings as the plugin, validated by `PtcToolRegistry.Config`. `environment` is the entry's `ToolDefinition.environment`; the plugin's `run_code` declares `'bound'`, and an entry whose bindings act only on this host omits it.
50
+
51
+ The caller owns a fixed `runtime` and must keep it available until the registry's context finishes disposal; without `runtime`, each program uses the context's `ptcRuntime` service. For runtimes that retain state or enforce a sandbox, disposal of that context cancels active Agent programs and waits for nested calls; Agent disposal releases its retained environment. Environment initialization belongs to the supplied [runtime's registration API](../../ptc-runtime/ptc-runtime/README.md#choose-a-backend); register it before including the program tool. `description` replaces the runtime's default entry description, and the registry appends runtime syntax and execution guidance to it; the caller supplies instructions for its bindings.
52
+
53
+ [Configuration catalog](../../../docs/config-catalog.md#deepseek-aidsh-tool-registry-ptc)
54
+
55
+ -----
56
+
57
+ <a id="understand-the-implementation"></a>
58
+ ## Understand the implementation
59
+
60
+ <details>
61
+ <summary>Implementation internals — click to expand</summary>
62
+
63
+ The plugin's `apply` calls `new PtcToolRegistry(ctx, { name: RUN_CODE_NAME, config, environment: 'bound' }).mountRoot()`. `mountRoot()` selects the registry with `ctx.tools.setRoot()`, which gives it the registrations of the scope's Agents as bindings, and registers the `tools:ptc-only` and `tools:sdk` prompt sections for steps that declare the registry's entry. The prompt sections also apply to a step that declares an entry derived from the registry's entry through `origin`, such as a copy an environment style offers in an execution environment. `run_code` declares `environment: 'bound'`, so a program runs in the environment its call targets and its tool calls inherit it. `ctx.tools.execute()` runs the raw call batch, including parsing, scheduling, cancellation, and optional model-step logging, and resolves each call to the registry's entry. Each program's nested calls run through the registry's own instance of the shared invocation pipeline for policy, tool bodies, and result notifications. Disposing the registry's context, such as unloading the plugin, aborts active Agent-owned programs whose runtime supports sandboxing or retains state, and waits for their nested calls to settle. Accepted batches still settle through that registry, potentially with cancellation errors.
64
+
65
+ ### PTC mode
66
+
67
+ A PTC root exposes the reserved `run_code` entry and a deterministic TypeScript SDK. Each SDK binding captures a frozen ToolSchema and passes it through the scheduler to its execution context. With a model step, the first binding query pins the bindings the step declares, so the SDK, the step's programs, and their nested calls use the same definitions; a binding replaced or added while the model responds fails as `UNKNOWN_TOOL` until the next step. Before policy, a started call records only pairing ids, name, and normalized arguments; its settle event preserves the rendered result, optional presentation metadata, and structured error. Metadata is projected from that call’s canonical value and frozen; it changes neither the program value nor model content. Description and parameters remain transient and never enter Session events or SDK output. Calls are scheduled through a per-run pool that reuses the native concurrency contract. A model-direct call naming a binding fails as `UNKNOWN_TOOL` before `tools/pre-execute`, approval, guards, and execution wrappers, and its message names the entries the Agent can call directly, such as `run_code`; the announced tools and the callable tools stay the same. Intermediate binding values are execution-local; only the outer `run_code` result has a hard size cap.
68
+
69
+ Text inspection reuses the completed pipeline's `output.render` result after content policies. It neither executes the tool nor renders it again. Replacements made only for the dispatch log do not change the program's inspection text.
70
+
71
+ New sub-calls use `<parent>:ptc:<n>` ids. Consumers treat these ids as opaque and correlate events by exact equality; restored historical ids retain their original bytes. The [PTC mode decision](../../../.agents/notes/implemented/feature/2026-06-15-ptc.md) owns durable naming and restoration rules.
72
+
73
+ TypeScript bindings for `read_image` and CUA screenshot operations return an `Image` with a readable `path`, `width`, and `height`. Other tool values are substituted all or nothing: when `{ type: 'image', attachment }` entries reference every image of the finalized result and the program can read each one, those entries become `Image`s, as admitted MCP image blocks do, and the result's text and images reach the model only through `inspect`; otherwise the value stays raw and the complete result content reaches the model as additional context. Passing it to `inspect` places its durable attachment among the captured text in the `run_code` result. Images nested in arrays and objects are displayed with their containing value. On program failure, inspected output accompanies the original diagnostic; outer result replacements remain authoritative. If an output policy removes a screenshot or read_image image, the binding rejects with ToolCallError and includes the retained text notice. Additional contexts retain their producing tool’s attribution.
74
+
75
+ The program entry of every PTC registry, `run_code` included, reports `outputTruncated: true` when captured output or diagnostic text was truncated. With the Node runtime, display-output overflow discards subsequent output while execution continues under the original timeout and cancellation rules. A normally completed program remains a successful tool result, with a truncation notice directing the model to read current state without repeating completed actions. A later failure keeps its actual error kind and truncation notice. Retained images and settled subcall records remain available.
76
+
77
+ When a nested tool changes the Session workspace root, the program stops and queued tools are abandoned. The switching tool can finish and deliver its result directly to the model. Caller cancellation, a subsequent sandbox mode change, environment reset, or registry unload can still cancel that tool. The next program starts in the new workspace after cleanup. If directory recovery changes the workspace during program preparation, the call returns `CODE_RUN_FAILED` before execution. Retrying uses the restored directory.
78
+
79
+ When the caller cancels and the runtime reports `abort`, `run_code` waits for cleanup and returns the native tool cancellation result: `AbortError` / `ABORTED`, with `Error: tool call aborted` as its content. Retained runtimes append their cancellation diagnostic, including whether variables were preserved or cleared. The Agent records the specific cancellation cause in `turn/end`, including a hook's reason. Runtime failures retain `CODE_RUN_FAILED` and their diagnostics, even if the caller also cancels. A runtime rejection keeps a `PtcRuntimeError` code and otherwise becomes `CODE_RUN_FAILED` with the rejection as `cause`. A blank `description`, an invalid `timeoutMs`, or `timeoutMs` or `sandbox_permissions` on a runtime that does not support it fails as `ToolArgsError` with `INVALID_ARGS`; a call after the registry's context is disposed fails with `CODE_RUN_UNAVAILABLE`; and a missing runtime, `workingDirectory`, or `sandboxPolicy` fails as `ToolContextUnavailableError`. Cancellation can discard retained runtime state and does not undo completed tool effects.
80
+
81
+ `run_code` accepts `timeoutMs` when the mounted runtime supports an override; its schema reports the configured default and maximum, and the runtime's usage instructions. The Node default is 120,000 ms with a 600,000 ms cap, including nested tool and approval waits. A wider `sandbox_permissions` mode requires a non-empty `justification` and approval before the program starts. The schema asks the model to write that reason in the current user's language. The grant applies to that complete execution; standing Session policy and nested tools retain their own authority. Programs are never replayed automatically: inspect earlier effects before explicitly retrying a denied program. Retained PTC environments follow the standing file policy. PTC observes committed Session mode changes and immediately cancels affected calls in all Node modes. Later PTC calls wait for their nested-tool cleanup before resolving the latest policy. One-time escalations omit the state owner and complete in a temporary environment; standing variables remain available to ordinary calls.
82
+
83
+ Custom host integrations that mount their own root through `mountRoot()` retain the `PtcToolRegistry` instance and call `reset(agent)` to clear that Agent's environment; reset is not a model tool or SDK binding. It requires a state-retaining runtime, cancels active calls, invokes `release(agent)`, and waits for process and started nested-tool cleanup without requiring the program to respond. When the runtime reports losing an environment, `agent/pre-step` checks whether the Agent can currently call the program directly or through an SDK binding. If so, the step records a reminder to recreate variables and object handles and check completed external actions before repeating them. The Agent keeps the reminder pending across provider reloads and cancelled steps until it enters Session history. An Agent starting with history receives the same reminder on the first model step that offers the program because its new state owner starts empty. Concurrent resets share cleanup. Missing or unsupported runtimes reject without blocking admission. Cleanup failure throws `CodeResetFailedError`; later programs report `CodeRunFailedError` with host-reset guidance until an explicit retry succeeds. Reset does not undo completed tool effects.
84
+
85
+ - [`src/index.ts`](src/index.ts) — `PtcToolRegistry` and its `mountRoot()`, the root plugin, step-pinned bindings, and host environment reset.
86
+ - [`src/ptc.ts`](src/ptc.ts) — The program entry: schema text, runtime checks, nested bindings, and result projection.
87
+ - [`src/ptc-calls.ts`](src/ptc-calls.ts) — Call admission and cancellation; reset waits for runtime release and nested-tool cleanup before admitting new calls.
88
+
89
+ </details>
90
+
91
+ -----
92
+
93
+ <a id="further-exploration"></a>
94
+ ## Further Exploration
95
+
96
+ - [Tool registry](../tools/README.md)
97
+ - [Agent loop](../agent-loop/README.md)
98
+ - [Native tool registry](../tool-registry-native/README.md)
99
+ - [Composable tool registries decision](../../../.agents/notes/implemented/architecture/2026-11-19-composable-tool-registries.md)
100
+
101
+ -----
102
+
103
+ <a id="model-experience"></a>
104
+ ## Model Experience
105
+
106
+ ### PTC mode schema and system prompt
107
+
108
+ #### What the model sees
109
+
110
+ PTC mode exposes the generated [`run_code` schema](../../../docs/tool-catalog.md#deepseek-aidsh-tool-registry-ptc), the SDK instructions below, and the generated exact TypeScript SDK block. The schema lists `code` before `description`; this is presentation order, not an enforced JSON member order. REPL and Script runtimes share the generic tool description and SDK instructions; their `executionInstructions` supply syntax and state rules. The TypeScript SDK declares `inspect(...values)` and uses it in the bash example for every execution mode. The instructions identify generated declarations as program-only bindings. When the current `bash` parameter schema accepts the example arguments, they also show a complete `run_code` call around `tools.bash(...)`. The `tools:sdk` section uses first-party order 5000 and disables prompt-variable interpolation, preserving literal `{{…}}` text in tool descriptions and schemas in TypeScript programs. When a step declares the PTC root's `run_code` entry, the prompt also carries the `tools:ptc-only` rule earlier in the first-party order, so the model reads which tools it may call before it reads what each one is for. Exhausting the tool-call allowance returns a failed `run_code` result with the limit, rejected call number, termination notice, and possible infinite-loop warning. Runtime cleanup diagnostics and captured output remain visible. TypeScript programs show selected values with `inspect(...values)`. Each argument is rendered independently. The registry omits function returns and final expression values. The Node provider retains the complete native console and captures its stdout and stderr writes; model instructions direct programs to use `inspect`.
111
+
112
+ ##### TypeScript PTC mode SDK instructions with bash
113
+
114
+ ```markdown
115
+ ## Writing code for run_code
116
+
117
+ `run_code` takes two required arguments: `code`, the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped), and `description`, a short summary of what the program does. The declarations below are SDK bindings for this program. A declaration does not make its name a directly callable tool; only names supplied as separate tool schemas may be called directly. When no separate `bash` schema is supplied, invoke a declared `bash` binding inside `run_code`:
118
+
119
+ `run_code({ code: "inspect(await tools.bash({ description: 'Show current directory', command: 'pwd' }))", description: "Show current directory" })`
120
+
121
+ Inside the program:
122
+
123
+ - Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to its declared value, which may be an Image. Tool arguments must be lossless JSON.
124
+ - A FAILED tool call rejects with `ToolCallError`, whose `toolName` identifies the failed tool and whose `message` is human-readable — `try/catch` it to handle and continue.
125
+ - Independent read-only calls MAY overlap under `Promise.all` (safe calls run concurrently; mutating calls run alone, in submission order). Sequence dependent work with `await`.
126
+ - Show selected values and images with `inspect(...values)`; every other intermediate result stays out of the conversation, so extract just what you need.
127
+ - Inspecting a tool result directly shows the text that tool call returns; for example, `inspect(await tools.read({ file_path: 'README.md' }))` prints its line-numbered content.
128
+
129
+ Program-only SDK bindings:
130
+ ```
131
+
132
+ ##### Tool-call allowance exhausted
133
+
134
+ ```markdown
135
+ Error: code run failed (abort): run_code exceeded its limit of 2048 tool calls (1024 per minute of its timeout); call 2049 was not dispatched. The run_code program may contain an infinite loop. Check loop termination or split the work into smaller run_code calls.
136
+ ```
137
+
138
+ #### Token effect
139
+
140
+ Fixed per-request cost proportional to the visible definitions. PTC mode trades end-tool schemas for generated SDK text plus one transport schema rather than promising a universal reduction. The allowance adds no SDK or schema text; its diagnostic contributes tokens through the failed run's result.
141
+
142
+ #### KV Cache effect
143
+
144
+ Prefix-stable while the root selection, generated SDK, transport schema, and visible tool set are unchanged. Root or filter changes may invalidate reuse from the first changed prompt or schema token. Changing `maxSubCallsPerMinute` does not change prompt or schema tokens.
145
+
146
+ ### Included program tools
147
+
148
+ #### What the model sees
149
+
150
+ The entry of an included PTC registry presents its caller-supplied name and description with the same program parameters, runtime instructions, and result rendering as `run_code`. Its bindings do not produce direct tool schemas or an SDK prompt section. The consumer owns its binding guidance.
151
+
152
+ #### Token effect
153
+
154
+ Under a native root, one program tool schema joins the Agent's other tool schemas; under a PTC root, the entry is one more SDK binding. The caller's binding guidance adds its own prompt cost; intermediate values remain outside model history unless the program emits them or a nested tool defers context.
155
+
156
+ #### KV Cache effect
157
+
158
+ The schema remains stable while the caller's description and runtime settings remain unchanged. Initialization runs inside the runtime and does not add program source to model requests.
159
+
160
+ ## Known Limitations and Deferred Work
161
+
162
+ <a id="known-limitations-and-deferred-work"></a>
163
+
164
+ These constraints apply to the host integration and accepted calls.
165
+
166
+ - **Program Images need a value reference** — read_image, CUA screenshots, and values whose `{ type: 'image', attachment }` entries reference every result image yield `Image`s; other image-producing tools keep their declared values and automatically attach their pictures as additional model context.
167
+ - **Program Images require attachment path mapping** — the filesystem must map stored host attachments to readable program paths. `fs-ssh` has no such mapping, so read_image and CUA image adaptation rejects in that composition, while value-referenced images stay raw JSON and reach the model as additional context.
168
+ - **No built-in reset control** — the shipped Loader plugin does not expose its registry instance through a service, GUI control or SDK endpoint. Hosts needing reset must construct a `PtcToolRegistry`, mount it with `mountRoot()`, and retain it in their own composition; that integration owns retries after a failed reset.
169
+ - **PTC mode uses TypeScript, and a root presents every tool one way** — PTC execution requires `ctx.ptcRuntime.language === 'typescript'`; a root offers every tool an Agent sees either directly or as SDK bindings, so a program-only tool beside directly called tools needs its own included PTC registry.
170
+ - **PTC mode intermediate values are execution-local and unbounded by bytes** — they cannot be reconstructed from session replay and may exhaust process or worker memory; only the outer `run_code` output has the worker's configurable hard cap.
171
+ - **State depends on the runtime**: Node `per-call` execution starts fresh; the default Node `persistent-in-memory` execution and Script mode with `scriptPersistence: true` retain memory per live Agent. Agent disposal awaits release. Restart and execution failures can clear memory; model-visible guidance explains how to recreate missing variables. Programs and results remain in the Session log, without serializing live objects.
172
+
173
+ <a id="dev-note"></a>
174
+ ### Dev Note
175
+
176
+ <details>
177
+ <summary>Working context for maintainers — click to expand</summary>
178
+
179
+ None.
180
+
181
+ </details>
package/README.zh.md ADDED
@@ -0,0 +1,181 @@
1
+ ---
2
+ description: "基于程序的工具执行、生成 SDK 与嵌套调用限制。"
3
+ kind: "package-reference"
4
+ ---
5
+
6
+ # @deepseek-ai/dsh-tool-registry-ptc
7
+
8
+ [English](README.md) | 中文
9
+
10
+ ## 概述
11
+
12
+ 当模型应通过 `run_code` 程序调用工具时,使用此根注册表;也可以引入一个具名程序工具,其程序只调用你绑定的工具。先挂载兼容的 PTC 运行时和工具注册表。作用域限制决定生成的 SDK;只有程序入口能被直接调用。可配置每个程序的工具调用总数上限,以及并行安全的子调用并发上限。
13
+
14
+ ## 目录
15
+
16
+ - [使用本包](#use-this-package)
17
+ - [理解实现](#understand-the-implementation)
18
+ - [进一步探索](#further-exploration)
19
+ - [模型体验](#model-experience)
20
+ - [已知限制与延期工作](#known-limitations-and-deferred-work)
21
+ - [开发备注](#dev-note)
22
+
23
+ -----
24
+
25
+ <a id="use-this-package"></a>
26
+ ## 使用本包
27
+
28
+ 将此行加入已经挂载 `dsh-tools` 的组合。插件在挂载作用域中安装一个 PTC 根注册表:其唯一条目 `run_code` 运行的程序可以调用该作用域中 Agent 看到的每个工具,系统提示词则把这些工具声明为生成的 SDK。Agent 使用最近的根注册表,每个作用域只允许一个;根注册表的选择与不可用的根注册表由 [tools 包](../tools/README.zh.md#compose-registries)负责。
29
+
30
+ ```yaml
31
+ - name: '@deepseek-ai/dsh-tool-registry-ptc'
32
+ config:
33
+ maxSubCallsPerMinute: 1024
34
+ maxParallelSubCalls: 10
35
+ ```
36
+
37
+ 根 `run_code` 调用是独占调用,按串行执行。`maxParallelSubCalls` 限制每个程序中并行安全的嵌套调用数,默认值为 `10`;值必须是正整数,`1` 表示串行执行。独占调用构成屏障。
38
+
39
+ `maxSubCallsPerMinute` 默认值为 `1024`,必须是正整数。每次 `run_code` 的工具调用额度为 `floor(最终 timeoutMs / 60000 × maxSubCallsPerMinute)`:Node 默认超时为两分钟,因此允许 2048 次。计算前先应用运行时默认值和超时上限;没有截止时间时,调用额度也没有有限上限。这是总额度,不是滑动窗口,因此可以集中调用。所有工具绑定共用计数,排队调用、无效参数和失败调用也计入。达到上限后,下一次尝试会通过运行时取消路径终止整个程序,取消进行中的子调用,并放弃排队调用,然后返回 `CODE_RUN_FAILED`。与超时一样,捕获绑定错误也不能让程序继续运行;运行时保留的状态会被丢弃。已完成的工具操作仍然生效,新的程序重新计数。
40
+
41
+ `PtcRuntime` 提供方必须报告 `language: 'typescript'`。PTC 注册表在提示词组装与执行时拒绝其他语言,不回落到 native 工具;以固定 `runtime` 构造的注册表在构造时即拒绝这种运行时。读取 schema 和执行时也会验证当前提供方。注册表生成 TypeScript SDK 与该条目的 schema;提供方负责执行。
42
+
43
+ 工具调用保留声明的 JSON 返回值。将未修改的对象或数组结果直接传给 `inspect`,会展示工具最终的纯文本输出。字段、拷贝、修改后的结果,以及嵌套在其他值中的结果,使用普通 Node 格式。可用 `inspect({ ...result })` 查看对象的原始结构,或用 `inspect(result.lines)` 展示选定字段。标量结果使用普通格式;Image 和混合文本与图片的输出遵循下文的图片规则。
44
+
45
+ 本包将 `ctx.ptcRuntime` 和 `ctx.systemPrompt` 声明为必需注入;带 Agent 的程序要求 `ctx.workingDirectory`。插件在运行时可用后才安装其根注册表;运行时卸载后,根注册表选择保持不可用,在运行时恢复之前,新批次与提示词组装都以 `TOOL_REGISTRY_UNAVAILABLE` 失败。启用 PTC 根注册表但未提供运行时会阻止应用启动。不可用的 PTC 根注册表绝不会回退到原生执行。
46
+
47
+ 共享分派事件与 `ptc-mode` 消息来源声明位于 `@deepseek-ai/dsh-tool-registry-ptc/types`;Client 代码通过 `@deepseek-ai/dsh-tool-registry-ptc/client` 导入同一组声明。两个入口都不引入 Host 服务声明。Host 策略扩展通过包主入口的 `import type` 获取 `PtcDispatchLog` 与 `tools/ptc-dispatch-log`,无需挂载该插件。
48
+
49
+ 要在 Agent 直接调用的工具旁提供一个具名程序工具,构造 `new PtcToolRegistry(ctx, { name, description?, runtime?, environment?, config? })`,注册其程序调用的工具,再用 `ctx.tools.include()` 引入该注册表。它的唯一条目是名为 `name` 的程序工具。它的注册与引入就是其程序调用的绑定:即使 Agent 能调用其他工具,程序也只能调用这些绑定,而且这些绑定绝不会进入引入方作用域的工具。嵌套调用保留正常审批、guard、派发日志、截图、延后上下文和轮次结束行为。`config` 接受与本插件相同的调用额度与并发设置,由 `PtcToolRegistry.Config` 校验。`environment` 是该条目的 `ToolDefinition.environment`;本插件的 `run_code` 声明 `'bound'`,而绑定只作用于本主机的条目省略它。
50
+
51
+ 调用方拥有固定的 `runtime`,必须使其可用至注册表的上下文完成销毁;未提供 `runtime` 时,每个程序使用该上下文的 `ptcRuntime` 服务。对于保留状态或实施沙箱的运行时,销毁该上下文会取消活动的 Agent 程序并等待嵌套调用;Agent 销毁时释放其保留环境。环境初始化由传入[运行时的注册 API](../../ptc-runtime/ptc-runtime/README.zh.md#choose-a-backend)负责;应在引入程序工具前完成注册。`description` 替换运行时默认的条目描述,注册表再把运行时语法和执行说明追加到其后;其绑定的说明由调用方提供。
52
+
53
+ [配置目录](../../../docs/config-catalog.zh.md#deepseek-aidsh-tool-registry-ptc)
54
+
55
+ -----
56
+
57
+ <a id="understand-the-implementation"></a>
58
+ ## 理解实现
59
+
60
+ <details>
61
+ <summary>实现细节——点击展开</summary>
62
+
63
+ 插件的 `apply` 调用 `new PtcToolRegistry(ctx, { name: RUN_CODE_NAME, config, environment: 'bound' }).mountRoot()`。`mountRoot()` 通过 `ctx.tools.setRoot()` 选择该注册表,`setRoot()` 把该作用域中 Agent 的注册项作为绑定交给它;`mountRoot()` 还为声明该注册表条目的步骤注册 `tools:ptc-only` 与 `tools:sdk` 提示词段落。若步骤声明的条目经 `origin` 派生自本注册表的条目(例如环境风格在执行环境中提供的副本),提示词段落同样生效。`run_code` 声明 `environment: 'bound'`,因此程序在其调用所指向的环境中运行,程序内的工具调用继承该环境。`ctx.tools.execute()` 运行原始调用批次,包括参数解析、调度、取消和可选的模型步骤日志,并把每个调用解析到该注册表的条目。每个程序的嵌套调用经由该注册表自有的共享调用流水线实例运行,以执行策略、工具主体和结果通知。销毁注册表的上下文(例如卸载插件)会中止 Agent 在支持沙箱或保留状态的运行时中执行的程序,并等待其嵌套调用结束。已接纳批次仍由该注册表结算,但可能返回取消错误。
64
+
65
+ ### PTC mode
66
+
67
+ PTC 根注册表公开保留的 `run_code` 条目,以及确定性的 TypeScript SDK。每个 SDK 绑定捕获冻结的 ToolSchema,经由调度器传入该次执行上下文。存在模型步骤时,第一次绑定查询会固定该步骤声明的绑定,因此 SDK、该步骤的程序及其嵌套调用使用相同的定义;模型响应期间被替换或新增的绑定在下一个步骤之前以 `UNKNOWN_TOOL` 失败。已开始的调用在策略之前只记录配对 id、名称和规范化参数;其结算事件保留渲染结果、可选展示元数据与结构化错误;元数据由该次调用的规范值投影并冻结,不改变程序读取的值或模型内容。描述与参数 schema 仅临时存活,不进入 Session 事件或 SDK 输出。调用通过复用原生并发约定的每次运行独有池调度。模型直接调用某个绑定名称时,该调用会在 `tools/pre-execute`、审批、守卫和执行包装层之前以 `UNKNOWN_TOOL` 失败,其消息会列出该 Agent 可以直接调用的条目,例如 `run_code`;通告的工具与可调用的工具保持一致。中间绑定值只存在于执行局部;只有外层 `run_code` 结果有硬大小上限。
68
+
69
+ 文本展示复用已完成流水线中经过内容策略处理的 `output.render` 结果,不会再次执行工具或渲染。仅针对派发日志的替换不改变程序的展示文本。
70
+
71
+ 新子调用使用 `<parent>:ptc:<n>` 标识。消费方将这些标识视为不透明值,并通过精确相等关联事件;恢复的历史标识保留原始字节。[PTC mode 决策](../../../.agents/notes/implemented/feature/2026-06-15-ptc.zh.md) 负责持久化命名与恢复规则。
72
+
73
+ TypeScript 中的 `read_image` 与 CUA 截图操作返回 `Image`,其中包含可读取的 `path`、`width` 和 `height`。其他工具的返回值按全有或全无的方式替换:当 `{ type: 'image', attachment }` 条目引用了最终结果中的每张图片且程序能读取每张图片时,这些条目会变为 `Image`,与已接受的 MCP 图片块相同,结果的文本与图片仅通过 `inspect` 进入模型;否则返回值保持原样,完整的结果内容作为额外上下文进入模型。将其传给 `inspect` 后,持久附件会按顺序与文本一起进入 `run_code` 结果。数组和对象内嵌的图片会随所在值一起展示。程序失败时,已 inspect 的输出随原始诊断一起呈现;外层替换的结果保持生效。输出策略移除截图或 read_image 图片时,绑定以 ToolCallError 拒绝,并包含保留的文本提示。额外上下文保留生成工具的归属。
74
+
75
+ 每个 PTC 注册表的程序入口(包括 `run_code`)在捕获输出或诊断文本被截断时报告 `outputTruncated: true`。使用 Node 运行时时,展示输出超限会丢弃后续输出,执行仍受原有超时和取消规则约束。正常结束的程序仍是成功工具结果,并提示输出已截断,应读取当前状态而不重复已完成的动作。后续失败保留实际错误类型和截断提示。已保留的图片与已结算的子调用记录仍然可用。
76
+
77
+ 嵌套工具切换 Session 的工作区根目录时,程序停止,队列中尚未启动的工具被取消。发起切换的工具可以继续执行,并将结果直接交给模型。调用方取消、后续的沙箱模式变更、环境重置或注册表卸载仍可取消该工具。清理完成后,下一个程序在新工作区启动。如果准备程序时因目录恢复而切换工作区,本次调用会在执行前返回 `CODE_RUN_FAILED`,重试会使用恢复后的目录。
78
+
79
+ 调用方取消且运行时报告 `abort` 时,`run_code` 等待清理完成,并返回原生工具的取消结果:`AbortError` / `ABORTED`,内容为 `Error: tool call aborted`。保留状态的运行时会附加取消诊断,包括变量被保留还是清空。Agent 在 `turn/end` 中记录具体取消原因,包括 hook 的理由。运行时失败保留 `CODE_RUN_FAILED` 及其诊断,即使调用方同时取消。运行时拒绝保留 `PtcRuntimeError` 的 code,否则变为 `CODE_RUN_FAILED` 并以该拒绝为 `cause`。`description` 为空白、`timeoutMs` 无效,或运行时不支持却传入 `timeoutMs` 或 `sandbox_permissions` 时,以带 `INVALID_ARGS` 的 `ToolArgsError` 失败;注册表的上下文销毁后,调用以 `CODE_RUN_UNAVAILABLE` 失败;缺少运行时、`workingDirectory` 或 `sandboxPolicy` 时以 `ToolContextUnavailableError` 失败。取消可能丢弃保留的运行时状态,也不会撤销已完成的工具操作。
80
+
81
+ 当已挂载运行时支持覆盖时,`run_code` 接受 `timeoutMs`;其 schema 报告配置的默认值和上限以及运行时使用说明。Node 默认值为 120,000 ms,上限为 600,000 ms,包含嵌套工具和审批等待。更宽的 `sandbox_permissions` 模式要求非空 `justification`,并在程序启动前获得审批。schema 提示模型使用当前用户的语言撰写理由。授权仅用于该次完整执行;常驻 Session 策略与嵌套工具保留各自权限。程序不会自动重放:显式重试被拒程序前,应检查先前已发生的效果。 保留状态的 PTC 环境遵循会话文件策略。PTC 观察已提交的 Session 模式变化,并立即取消所有 Node 模式下受影响的调用。后续 PTC 调用等待这些调用的嵌套工具清理后,再解析最新策略。单次提权省略状态所有者,在临时环境中执行;普通调用仍可访问原环境的变量。
82
+
83
+ 通过 `mountRoot()` 挂载自有根注册表的自定义宿主集成保留该 `PtcToolRegistry` 实例,并调用 `reset(agent)` 清空该 Agent 的环境;重置不是模型工具或 SDK 绑定。该操作要求运行时保留状态,会取消活动调用、调用 `release(agent)`,并等待进程及已启动嵌套工具的清理,无需程序响应。运行时报告持久环境失效后,`agent/pre-step` 检查 Agent 当前能否直接或通过 SDK 绑定调用该程序。如果可以,该步骤会记录一条提醒,要求重新创建变量和对象句柄,并先检查已完成的外部操作再决定是否重做。即使提供方重新加载或步骤被取消,Agent 也会保留待投递的提醒,直到它写入 Session 历史。Agent 带历史启动时,新的状态所有者从空环境开始,模型首次能调用该程序时也会收到这条提醒。并发重置共享清理过程。运行时缺失或不受支持时,操作拒绝但不阻止后续准入。清理失败会抛出 `CodeResetFailedError`;后续程序报告带宿主重置指引的 `CodeRunFailedError`,直到显式重试成功。重置不会撤销已完成的工具操作。
84
+
85
+ - [`src/index.ts`](src/index.ts) — `PtcToolRegistry` 及其 `mountRoot()`、根注册表插件、按步骤固定的绑定与宿主环境重置。
86
+ - [`src/ptc.ts`](src/ptc.ts) — 程序入口:schema 文本、运行时检查、嵌套绑定与结果投影。
87
+ - [`src/ptc-calls.ts`](src/ptc-calls.ts) — 调用准入和取消;重置等待运行时释放及嵌套工具清理后,才接纳新调用。
88
+
89
+ </details>
90
+
91
+ -----
92
+
93
+ <a id="further-exploration"></a>
94
+ ## 进一步探索
95
+
96
+ - [工具注册表](../tools/README.zh.md)
97
+ - [Agent 循环](../agent-loop/README.zh.md)
98
+ - [原生工具注册表](../tool-registry-native/README.zh.md)
99
+ - [可组合工具注册表决策](../../../.agents/notes/implemented/architecture/2026-11-19-composable-tool-registries.zh.md)
100
+
101
+ -----
102
+
103
+ <a id="model-experience"></a>
104
+ ## 模型体验
105
+
106
+ ### PTC mode schema 与系统提示词
107
+
108
+ #### 模型看到什么
109
+
110
+ PTC mode 会公开生成的 [`run_code` schema](../../../docs/tool-catalog.zh.md#deepseek-aidsh-tool-registry-ptc)、下方 SDK 说明,以及生成的精确 TypeScript SDK 块。schema 将 `code` 列在 `description` 前;这是呈现顺序,不强制 JSON 成员顺序。REPL 与 Script 运行时共用通用工具描述和 SDK 说明;语法与状态规则由各自的 `executionInstructions` 提供。TypeScript SDK 声明 `inspect(...values)`,并在所有执行模式的 bash 示例中使用它。说明将生成声明明确标为只能在程序内使用的绑定。当当前 `bash` 参数 schema 接受示例参数时,说明还会给出以 `run_code` 包住 `tools.bash(...)` 的完整调用。`tools:sdk` 段使用 first-party 顺序 5000,并关闭提示词变量插值,使 TypeScript 程序原样保留工具描述和 schema 中的 `{{…}}` 文本。当某个步骤声明 PTC 根注册表的 `run_code` 条目时,提示词还会带上处于更早 first-party 顺序的 `tools:ptc-only` 规则,让模型先读到「可以调用哪些工具」再读「每个工具做什么」。 工具调用额度耗尽时,`run_code` 返回失败结果,说明额度、被拒绝的调用序号、程序已终止,以及可能存在死循环。运行时清理诊断和已捕获的输出仍然可见。 TypeScript 程序通过 `inspect(...values)` 展示选定的值。每个参数独立渲染。注册表省略函数返回值和末尾表达式的值。Node 提供方保留完整的原生 console,并捕获其 stdout 和 stderr 写入;模型指引要求程序使用 `inspect`。
111
+
112
+ ##### 带 bash 的 TypeScript PTC mode SDK 说明
113
+
114
+ ```markdown
115
+ ## Writing code for run_code
116
+
117
+ `run_code` takes two required arguments: `code`, the body of an async TypeScript function (erasable syntax only — no `enum` or namespaces; type annotations are advisory, the code runs type-stripped), and `description`, a short summary of what the program does. The declarations below are SDK bindings for this program. A declaration does not make its name a directly callable tool; only names supplied as separate tool schemas may be called directly. When no separate `bash` schema is supplied, invoke a declared `bash` binding inside `run_code`:
118
+
119
+ `run_code({ code: "inspect(await tools.bash({ description: 'Show current directory', command: 'pwd' }))", description: "Show current directory" })`
120
+
121
+ Inside the program:
122
+
123
+ - Call tools as `await tools.name(args)` — quoted access for exotic names: `tools["my-tool"](args)`. Every call resolves to its declared value, which may be an Image. Tool arguments must be lossless JSON.
124
+ - A FAILED tool call rejects with `ToolCallError`, whose `toolName` identifies the failed tool and whose `message` is human-readable — `try/catch` it to handle and continue.
125
+ - Independent read-only calls MAY overlap under `Promise.all` (safe calls run concurrently; mutating calls run alone, in submission order). Sequence dependent work with `await`.
126
+ - Show selected values and images with `inspect(...values)`; every other intermediate result stays out of the conversation, so extract just what you need.
127
+ - Inspecting a tool result directly shows the text that tool call returns; for example, `inspect(await tools.read({ file_path: 'README.md' }))` prints its line-numbered content.
128
+
129
+ Program-only SDK bindings:
130
+ ```
131
+
132
+ ##### 工具调用额度耗尽
133
+
134
+ ```markdown
135
+ Error: code run failed (abort): run_code exceeded its limit of 2048 tool calls (1024 per minute of its timeout); call 2049 was not dispatched. The run_code program may contain an infinite loop. Check loop termination or split the work into smaller run_code calls.
136
+ ```
137
+
138
+ #### Token 影响
139
+
140
+ 每次请求的固定成本与可见定义成正比。PTC mode 使用生成的 SDK 文本加一个传输 schema 取代最终工具 schema,但不承诺普遍减少成本。调用额度不增加 SDK 或 schema 文本;超限诊断通过该次运行的失败结果消耗 token。
141
+
142
+ #### KV Cache 影响
143
+
144
+ 只要根注册表选择、生成的 SDK、传输 schema 与可见工具集合不变,前缀就保持稳定。根注册表或筛选器变更可能从第一个改变的提示词或 schema token 起使复用失效。修改 `maxSubCallsPerMinute` 不会改变提示词或 schema token。
145
+
146
+ ### 引入的程序工具
147
+
148
+ #### 模型可见内容
149
+
150
+ 被引入的 PTC 注册表的条目显示调用方提供的名称和描述,并使用与 `run_code` 相同的程序参数、运行时说明和结果渲染。其绑定不生成直接工具 schema 或 SDK 提示词段落。绑定指引由消费方负责。
151
+
152
+ #### Token 影响
153
+
154
+ 在原生根注册表下,Agent 的其他工具 schema 中加入一个程序工具 schema;在 PTC 根注册表下,该条目是多出的一个 SDK 绑定。调用方的绑定指引产生相应提示词成本;中间值只有在程序输出它们或嵌套工具提供延后上下文时才进入模型历史。
155
+
156
+ #### KV Cache 影响
157
+
158
+ 只要调用方描述和运行时设置不变,schema 就保持稳定。初始化在运行时内部执行,不向模型请求添加程序源码。
159
+
160
+ ## 已知限制与延期工作
161
+
162
+ <a id="known-limitations-and-deferred-work"></a>
163
+
164
+ 以下约束适用于宿主集成及已开始的调用。
165
+
166
+ - **程序 Image 需要返回值引用**:read_image、CUA 截图以及其 `{ type: 'image', attachment }` 条目引用了每张结果图片的返回值会产生 `Image`;其他图片工具保留其声明的返回值,并将图片自动附加到模型的额外上下文。
167
+ - **程序 Image 要求附件路径映射**:文件系统必须将宿主存储的附件映射为程序可读的路径。`fs-ssh` 没有这项映射,因此该组合中的 read_image 与 CUA 图片适配会拒绝,而返回值引用的图片保持原始 JSON,并作为额外上下文进入模型。
168
+ - **没有内置重置控件**:自带的 Loader 插件不通过服务、GUI 控件或 SDK 端点暴露其注册表实例。需要重置的宿主必须构造 `PtcToolRegistry`、用 `mountRoot()` 挂载它,并在自己的组合中保留它,该集成负责失败后的重试。
169
+ - **PTC mode 使用 TypeScript,且一个根注册表以同一种方式呈现所有工具**:PTC 执行要求 `ctx.ptcRuntime.language === 'typescript'`;根注册表要么直接提供 Agent 看到的每个工具,要么把它们全部作为 SDK 绑定提供,因此要在直接调用的工具旁提供仅供程序调用的工具,需要为它单独引入一个 PTC 注册表。
170
+ - **PTC mode 中间值只存在于执行局部,且没有字节上限**:它们无法从会话回放重建,并可能耗尽进程或 worker 内存;只有外层 `run_code` 输出受 worker 可配置的硬上限约束。
171
+ - **状态由运行时决定**:Node 的 `per-call` 执行每次重新开始;默认的 Node `persistent-in-memory` 执行和启用 `scriptPersistence: true` 的 Script 模式按存活 Agent 保留内存。Agent 销毁时等待环境释放。重启与执行失败可能清除内存;模型说明会指导重新创建缺失变量。程序与结果仍写入 Session 日志,不序列化运行中的对象。
172
+
173
+ <a id="dev-note"></a>
174
+ ### 开发备注
175
+
176
+ <details>
177
+ <summary>维护者的工作上下文——点击展开</summary>
178
+
179
+ 无。
180
+
181
+ </details>