deepclause-sdk 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/README.md +148 -2
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +154 -28
  4. package/dist/agent.js.map +1 -1
  5. package/dist/cli/config.d.ts +150 -140
  6. package/dist/cli/config.d.ts.map +1 -1
  7. package/dist/cli/config.js +3 -0
  8. package/dist/cli/config.js.map +1 -1
  9. package/dist/cli/login.d.ts +2 -1
  10. package/dist/cli/login.d.ts.map +1 -1
  11. package/dist/cli/login.js +15 -2
  12. package/dist/cli/login.js.map +1 -1
  13. package/dist/cli/repl.d.ts +54 -1
  14. package/dist/cli/repl.d.ts.map +1 -1
  15. package/dist/cli/repl.js +467 -53
  16. package/dist/cli/repl.js.map +1 -1
  17. package/dist/cli/run.d.ts.map +1 -1
  18. package/dist/cli/run.js +4 -0
  19. package/dist/cli/run.js.map +1 -1
  20. package/dist/cli/set-backend.d.ts +2 -2
  21. package/dist/cli/set-backend.d.ts.map +1 -1
  22. package/dist/cli/set-backend.js +54 -8
  23. package/dist/cli/set-backend.js.map +1 -1
  24. package/dist/cli/work.d.ts +16 -0
  25. package/dist/cli/work.d.ts.map +1 -0
  26. package/dist/cli/work.js +45 -0
  27. package/dist/cli/work.js.map +1 -0
  28. package/dist/index.d.ts +8 -6
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +4 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/prolog-src/deepclause_mi.pl +2596 -268
  33. package/dist/prolog-src/deepclause_strings.pl +36 -3
  34. package/dist/repl/agent.d.ts +12 -0
  35. package/dist/repl/agent.d.ts.map +1 -1
  36. package/dist/repl/agent.js +40 -16
  37. package/dist/repl/agent.js.map +1 -1
  38. package/dist/repl/auth.d.ts +6 -3
  39. package/dist/repl/auth.d.ts.map +1 -1
  40. package/dist/repl/auth.js +55 -5
  41. package/dist/repl/auth.js.map +1 -1
  42. package/dist/repl/backend.d.ts +24 -0
  43. package/dist/repl/backend.d.ts.map +1 -1
  44. package/dist/repl/backend.js +292 -12
  45. package/dist/repl/backend.js.map +1 -1
  46. package/dist/repl/compaction.d.ts +2 -0
  47. package/dist/repl/compaction.d.ts.map +1 -1
  48. package/dist/repl/compaction.js +9 -0
  49. package/dist/repl/compaction.js.map +1 -1
  50. package/dist/repl/event-history.d.ts +4 -0
  51. package/dist/repl/event-history.d.ts.map +1 -0
  52. package/dist/repl/event-history.js +33 -0
  53. package/dist/repl/event-history.js.map +1 -0
  54. package/dist/repl/harness-tools.d.ts +26 -18
  55. package/dist/repl/harness-tools.d.ts.map +1 -1
  56. package/dist/repl/harness-tools.js +103 -34
  57. package/dist/repl/harness-tools.js.map +1 -1
  58. package/dist/repl/history.d.ts +75 -3
  59. package/dist/repl/history.d.ts.map +1 -1
  60. package/dist/repl/history.js +155 -4
  61. package/dist/repl/history.js.map +1 -1
  62. package/dist/repl/index.d.ts +33 -5
  63. package/dist/repl/index.d.ts.map +1 -1
  64. package/dist/repl/index.js +30 -0
  65. package/dist/repl/index.js.map +1 -1
  66. package/dist/repl/reference.d.ts +31 -0
  67. package/dist/repl/reference.d.ts.map +1 -0
  68. package/dist/repl/reference.js +143 -0
  69. package/dist/repl/reference.js.map +1 -0
  70. package/dist/repl/render.d.ts +3 -1
  71. package/dist/repl/render.d.ts.map +1 -1
  72. package/dist/repl/render.js +17 -0
  73. package/dist/repl/render.js.map +1 -1
  74. package/dist/repl/session.d.ts +166 -23
  75. package/dist/repl/session.d.ts.map +1 -1
  76. package/dist/repl/session.js +980 -188
  77. package/dist/repl/session.js.map +1 -1
  78. package/dist/repl/tui/autocomplete.d.ts.map +1 -1
  79. package/dist/repl/tui/autocomplete.js +5 -1
  80. package/dist/repl/tui/autocomplete.js.map +1 -1
  81. package/dist/repl/tui/renderer.d.ts +2 -1
  82. package/dist/repl/tui/renderer.d.ts.map +1 -1
  83. package/dist/repl/tui/renderer.js +20 -3
  84. package/dist/repl/tui/renderer.js.map +1 -1
  85. package/dist/repl/tui/status-bar.d.ts +5 -2
  86. package/dist/repl/tui/status-bar.d.ts.map +1 -1
  87. package/dist/repl/tui/status-bar.js +8 -3
  88. package/dist/repl/tui/status-bar.js.map +1 -1
  89. package/dist/repl/tui/tool-pane.d.ts +1 -1
  90. package/dist/repl/tui/tool-pane.d.ts.map +1 -1
  91. package/dist/repl/tui/tool-pane.js +13 -7
  92. package/dist/repl/tui/tool-pane.js.map +1 -1
  93. package/dist/repl/tui/work-view.d.ts +10 -0
  94. package/dist/repl/tui/work-view.d.ts.map +1 -0
  95. package/dist/repl/tui/work-view.js +28 -0
  96. package/dist/repl/tui/work-view.js.map +1 -0
  97. package/dist/runner.d.ts +7 -1
  98. package/dist/runner.d.ts.map +1 -1
  99. package/dist/runner.js +81 -23
  100. package/dist/runner.js.map +1 -1
  101. package/dist/runtime-identity.json +10 -0
  102. package/dist/system/assets/docs/AGENT_PROMPT.md +265 -2943
  103. package/dist/system/assets/docs/DML_AUTHORING.md +81 -295
  104. package/dist/system/assets/docs/DML_BASICS.md +93 -0
  105. package/dist/system/assets/docs/DML_DEBUGGING.md +73 -0
  106. package/dist/system/assets/docs/DML_FORMS.md +94 -728
  107. package/dist/system/assets/docs/DML_MEDIA.md +63 -0
  108. package/dist/system/assets/docs/DML_PROCESSES.md +100 -0
  109. package/dist/system/assets/docs/DML_PYTHON.md +74 -0
  110. package/dist/system/assets/docs/DML_REFERENCE.md +224 -1912
  111. package/dist/system/assets/docs/DML_SHELL.md +107 -0
  112. package/dist/system/assets/docs/DML_TASKS.md +112 -0
  113. package/dist/system/assets/docs/HARNESS_EXAMPLES.md +255 -179
  114. package/dist/system/assets/docs/HARNESS_PATTERNS.md +65 -538
  115. package/dist/system/assets/docs/LOCAL_AGENT_PROMPT.md +186 -0
  116. package/dist/system/assets/docs/PROLOG_SNIPPETS.md +61 -154
  117. package/dist/system/assets/index.d.ts +1 -1
  118. package/dist/system/assets/index.d.ts.map +1 -1
  119. package/dist/system/assets/index.js +7 -0
  120. package/dist/system/assets/index.js.map +1 -1
  121. package/dist/system/environment.d.ts +1 -1
  122. package/dist/system/environment.d.ts.map +1 -1
  123. package/dist/system/environment.js +10 -6
  124. package/dist/system/environment.js.map +1 -1
  125. package/dist/system/runtime/shell-manager.d.ts +1 -1
  126. package/dist/system/runtime/shell-manager.d.ts.map +1 -1
  127. package/dist/types.d.ts +62 -2
  128. package/dist/types.d.ts.map +1 -1
  129. package/dist/work/index.d.ts +82 -0
  130. package/dist/work/index.d.ts.map +1 -0
  131. package/dist/work/index.js +232 -0
  132. package/dist/work/index.js.map +1 -0
  133. package/package.json +12 -3
  134. package/src/prolog-src/deepclause_mi.pl +2596 -268
  135. package/src/prolog-src/deepclause_strings.pl +36 -3
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  **Effortlessly build efficient model-, task-, or domain-specific harnesses.**
4
4
 
5
+ **Web app:** [web.deepclause.ai](https://web.deepclause.ai)
6
+
5
7
  Follow us on [X (@hilbertshelper)](https://x.com/hilbertshelper) · Blog: [deepclause.substack.com](https://deepclause.substack.com)
6
8
 
7
9
  DeepClause is a **Prolog-based runtime for self-modifying agent harnesses**. A
@@ -76,9 +78,87 @@ dependencies and as conceptual foundations.
76
78
  It is likely not worth it when a single prompt or a short script is enough, or
77
79
  when there is no structure you would want to reuse, review, or reproduce.
78
80
 
81
+ ## Develop and Work
82
+
83
+ DeepClause has two ways to use the same harness:
84
+
85
+ | | Develop | Work |
86
+ |---|---|---|
87
+ | Purpose | Author and edit the harness | Use the harness |
88
+ | Input | plain English, or `/goal <dml>` | chat with the declared tools, or run a declared workflow |
89
+ | Can change DML | yes, through transactional commits | no |
90
+ | Needs `shell/1` | no | yes |
91
+
92
+ **Develop is the default.** Describe a skill, tool, or change in plain English;
93
+ the authoring agent writes, tests, and commits it. **Work** is the stable,
94
+ non-authoring surface for a harness that is ready to use.
95
+
96
+ ### Work needs a shell
97
+
98
+ A harness becomes usable in Work when it declares a `shell/1` fact:
99
+
100
+ ```prolog
101
+ shell(_{
102
+ kind: agent,
103
+ system: "You are a concise writing assistant.",
104
+ host_tools: [],
105
+ tools: [
106
+ _{name: "text_length", goal: "length_of",
107
+ description: "Count characters in text.",
108
+ inputs: [_{name: "Text", type: string}],
109
+ outputs: [_{name: "N", type: integer}]}
110
+ ],
111
+ workflows: [
112
+ _{name: "Uppercase", goal: "uppercase",
113
+ inputs: [_{name: "Text", type: string}],
114
+ outputs: [_{name: "Upper", type: string}]}
115
+ ],
116
+ welcome: "Ask about text, or choose Uppercase."
117
+ }).
118
+ ```
119
+
120
+ - `kind: agent` gives a chat assistant that can call only the declared tools and
121
+ workflows. `kind: workflow` gives a workflow-only surface with no assistant.
122
+ - `tools` are declared DML tools; `host_tools` lists host capabilities the
123
+ harness may use (for example `bash`). Host grants and sandbox boundaries still
124
+ apply.
125
+ - Workflow inputs and outputs are typed. Missing or invalid inputs fail before
126
+ execution; a failed, cancelled, or suspended workflow is not success.
127
+ - A harness without `shell/1` has no Work interface. Open Develop or an explicit
128
+ Run instead.
129
+
130
+ ### Entering Work
131
+
132
+ In the TUI:
133
+
134
+ ```text
135
+ /mode work [harness] open a Work conversation
136
+ /workflows list declared workflows
137
+ /workflow "Uppercase" {"Text":"hello"} run exactly that workflow
138
+ /mode develop [harness] return to authoring
139
+ ```
140
+
141
+ From the CLI (assuming a harness named `example`):
142
+
143
+ ```bash
144
+ deepclause repl --mode work --harness example
145
+ deepclause repl --mode work --harness example \
146
+ --workflow "Uppercase" --inputs '{"Text":"hello"}' --json
147
+ ```
148
+
149
+ A manual `--workflow` run executes exactly that workflow and makes **zero
150
+ assistant calls**. An agent shell calls the model only when the conversation
151
+ needs it. Work and Develop are separate conversations with separate snapshots.
152
+ Work is pinned to the harness version it started with; to change the shell,
153
+ tools, or workflows, return to Develop (or open a fresh Work conversation after
154
+ the edit).
155
+
156
+ See [Work mode](docs/WORK_MODE.md) for the full contract and
157
+ [TUI/web reconciliation](docs/TUI_WEB_RECONCILIATION.md) for implementation status.
158
+
79
159
  ## The REPL: a living, self-modifying harness
80
160
 
81
- The REPL keeps a persistent DML module ("the harness"). You talk to it in two ways:
161
+ In default Develop mode, the REPL keeps a persistent DML module ("the harness"). You talk to it in two ways:
82
162
 
83
163
  | Input | Mode |
84
164
  |---|---|
@@ -329,6 +409,20 @@ deepclause repl --session last # resume the most recent session
329
409
  deepclause repl --session repl_m9x1_1 # …or one by id
330
410
  ```
331
411
 
412
+ A turn can also **suspend itself** at a safe point and be continued on resume —
413
+ the continuation is a goal, so the program says where it is safe:
414
+
415
+ ```prolog
416
+ loop(N) :-
417
+ ( N >= 2 -> suspend(loop(N)) % stop here; resume with loop(N)
418
+ ; M is N + 1, loop(M)
419
+ ).
420
+ ```
421
+
422
+ `suspend/1` refuses while the turn holds something a snapshot cannot carry — an
423
+ open stream or an open form — so a resumed session never inherits a half-written
424
+ file. See [`docs/DML_REPL_SUSPEND_RESUME.md`](docs/DML_REPL_SUSPEND_RESUME.md).
425
+
332
426
  ### Why Prolog/DML for a self-modifying harness
333
427
 
334
428
  An RLM-style harness works best when the harness *is* the program the model
@@ -464,6 +558,58 @@ overrides it. Existing pi installations stay untouched.
464
558
 
465
559
  ### Local models (Ollama, llama.cpp, vLLM)
466
560
 
561
+ #### llama.cpp (router or single-model)
562
+
563
+ When `llama-server` runs in **router mode** (started without `--model` or `-m`),
564
+ the `pi` backend registers pi's built-in `llama.cpp` provider and discovers its
565
+ models live. Point it at the router with `LLAMA_BASE_URL`, or sign in once:
566
+
567
+ ```bash
568
+ # one-off / environment-driven
569
+ export LLAMA_BASE_URL=http://127.0.0.1:8080
570
+ deepclause repl
571
+
572
+ # or persist the router URL + an optional API key and pick a default model
573
+ deepclause login --provider llama.cpp
574
+
575
+ # non-default host and port
576
+ deepclause login --provider llama.cpp --host 192.168.1.50 --port 9000
577
+ deepclause login --provider llama.cpp --base-url http://gpu-box:9000
578
+ deepclause set-backend pi --provider llama.cpp --host 192.168.1.50 --port 9000 \
579
+ --model llama.cpp/<id>
580
+ ```
581
+
582
+ `--base-url` wins over `--host`/`--port`; a bare `--host` defaults the port to
583
+ `8080`, and `--port` alone defaults the host to `127.0.0.1`. Passing an explicit
584
+ endpoint re-runs the login, so the stored router URL is updated. The interactive
585
+ login prompt also accepts a full URL, a bare `host`, or `host:port` when these
586
+ flags are omitted, and `--provider llama.cpp` may be omitted when one of these
587
+ endpoint flags is present. For a router that requires an API key, add
588
+ `--api-key <key>`; the URL is resolved from the explicit flags, then
589
+ `LLAMA_BASE_URL`, then `http://127.0.0.1:8080`.
590
+
591
+ Inside the REPL, `/login` lists `llama.cpp`. Selecting it prompts for a base
592
+ URL, a `host:port`, or a host and then a port. You can also pass the endpoint
593
+ directly:
594
+
595
+ ```text
596
+ /login llama.cpp --host 192.168.1.50 --port 9000
597
+ /login --base-url http://gpu-box:9000
598
+ ```
599
+
600
+ After `/login`, `/model` includes the models discovered by that login (the
601
+ choice still applies on the next start). Use `/model llama.cpp/<id>` to set the
602
+ default, or pass `--model llama.cpp/<id>` for this run. Start the router with
603
+ `--jinja` when you need tool calling for `task/N`.
604
+
605
+ Single-model mode also works: when `/models` does not return router metadata,
606
+ DeepClause falls back to `/v1/models` and registers the reported model. If the
607
+ server exposes neither a model id nor a model path, it uses the placeholder id
608
+ `local-model`. Use `models.json` if you need to pin an exact id for a server
609
+ that rejects arbitrary model names.
610
+
611
+ #### Other OpenAI-compatible endpoints (Ollama, vLLM)
612
+
467
613
  The default `pi` backend reads pi's `models.json`, which can declare **custom
468
614
  OpenAI-compatible providers**, so a local server needs no SDK changes:
469
615
 
@@ -491,7 +637,7 @@ OpenAI-compatible providers**, so a local server needs no SDK changes:
491
637
 
492
638
  ```bash
493
639
  deepclause set-backend pi --list # lists "loc (Local server) — configured [api-key]"
494
- # and the model "loc/lecho"
640
+ # and the model "loc/llama3.1"
495
641
  deepclause repl --model loc:llama3.1
496
642
  ```
497
643
 
@@ -1 +1 @@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,MAAM,CAAC,CAAC;IACjC,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IAC1F,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC3D,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,sEAAsE;IACtE,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;CACjE;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,mGAAmG;IACnG,QAAQ,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAA;KAAE,CAAC,CAAC;CACtF;AAo5BD;;GAEG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CA+wBtF"}
package/dist/agent.js CHANGED
@@ -306,16 +306,43 @@ function describeSchemaFields(typedVar) {
306
306
  const fields = typedVar.fields;
307
307
  if (!fields || fields.length === 0)
308
308
  return undefined;
309
- return fields
310
- .map((field) => {
311
- const notes = [field.type];
312
- if (field.required)
313
- notes.push('required');
314
- else if (field.default !== undefined)
315
- notes.push(`default ${JSON.stringify(field.default)}`);
316
- return `${field.name} (${notes.join(', ')})`;
317
- })
318
- .join(', ');
309
+ return fields.map(describeSchemaField).join(', ');
310
+ }
311
+ /** One field, including a nested schema's own fields. */
312
+ function describeSchemaField(field) {
313
+ const type = field.itemType ? `array<${field.itemType}>` : field.type;
314
+ const notes = [type];
315
+ if (field.required)
316
+ notes.push('required');
317
+ else if (field.default !== undefined)
318
+ notes.push(`default ${JSON.stringify(field.default)}`);
319
+ const head = `${field.name} (${notes.join(', ')})`;
320
+ return field.fields && field.fields.length > 0
321
+ ? `${head} {${field.fields.map(describeSchemaField).join(', ')}}`
322
+ : head;
323
+ }
324
+ /** The type text shown to the nested agent, including a declared schema's fields. */
325
+ function describeOutputType(variable) {
326
+ const base = variable.type === 'array' && variable.itemType
327
+ ? `array<${variable.itemType}>`
328
+ : variable.type;
329
+ const fields = describeSchemaFields(variable);
330
+ return fields ? `${base} {${fields}}` : base;
331
+ }
332
+ /**
333
+ * Match the `variable` argument of `set_result` to a requested output.
334
+ *
335
+ * Models occasionally omit it (providers differ in how they serialise an enum),
336
+ * and an empty name used to abort the whole task with `Unknown variable: `.
337
+ * With a single requested output the intent is unambiguous, so infer it; names
338
+ * are trimmed and compared case-insensitively, as `dict_field/3` does.
339
+ */
340
+ function matchOutputVar(vars, raw) {
341
+ const wanted = typeof raw === 'string' ? raw.trim() : '';
342
+ if (!wanted)
343
+ return vars.length === 1 ? vars[0] : undefined;
344
+ return vars.find((variable) => variable.name === wanted)
345
+ ?? vars.find((variable) => variable.name.toLowerCase() === wanted.toLowerCase());
319
346
  }
320
347
  /**
321
348
  * A declared schema is checked here as well as in the runtime, so the agent is
@@ -329,20 +356,53 @@ function validateSchemaFields(typedVar, value) {
329
356
  return null;
330
357
  if (!isPlainObject(value))
331
358
  return 'Expected an object value';
359
+ return validateFields(fields, value, '');
360
+ }
361
+ /** Validate a value against a field list, naming the nested path in the error. */
362
+ function validateFields(fields, value, prefix) {
363
+ if (!isPlainObject(value)) {
364
+ return prefix ? `Field "${prefix}" should be an object` : 'Expected an object value';
365
+ }
332
366
  const record = value;
333
367
  for (const field of fields) {
334
368
  const present = Object.prototype.hasOwnProperty.call(record, field.name)
335
369
  || Object.keys(record).some((key) => normaliseFieldKey(key) === normaliseFieldKey(field.name));
370
+ const path = prefix ? `${prefix}.${field.name}` : field.name;
336
371
  if (!present) {
337
372
  if (field.required)
338
- return `Missing required field "${field.name}"`;
373
+ return `Missing required field "${path}"`;
339
374
  continue;
340
375
  }
341
376
  const found = record[field.name] ?? Object.entries(record)
342
377
  .find(([key]) => normaliseFieldKey(key) === normaliseFieldKey(field.name))?.[1];
343
- if (!fieldTypeAccepts(field.type, found)) {
344
- return `Field "${field.name}" should be ${field.type}`;
378
+ const error = validateFieldValue(field, found, path);
379
+ if (error)
380
+ return error;
381
+ }
382
+ return null;
383
+ }
384
+ /** Validate one field value, recursing into a list element or nested schema. */
385
+ function validateFieldValue(field, value, path) {
386
+ if (field.itemType) {
387
+ if (!Array.isArray(value))
388
+ return `Field "${path}" should be array<${field.itemType}>`;
389
+ for (let index = 0; index < value.length; index += 1) {
390
+ const item = value[index];
391
+ if (field.fields && field.fields.length > 0) {
392
+ const nested = validateFields(field.fields, item, `${path}[${index}]`);
393
+ if (nested)
394
+ return nested;
395
+ }
396
+ else if (!fieldTypeAccepts(field.itemType, item)) {
397
+ return `Field "${path}[${index}]" should be ${field.itemType}`;
398
+ }
345
399
  }
400
+ return null;
401
+ }
402
+ if (!fieldTypeAccepts(field.type, value))
403
+ return `Field "${path}" should be ${field.type}`;
404
+ if (field.fields && field.fields.length > 0) {
405
+ return validateFields(field.fields, value, path);
346
406
  }
347
407
  return null;
348
408
  }
@@ -526,15 +586,21 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
526
586
  }];
527
587
  if (normalizedOutputVars.length > 0) {
528
588
  const resultTypes = normalizedOutputVars
529
- .map((variable) => `${variable.name}: ${variable.type}${variable.itemType ? `<${variable.itemType}>` : ''}`)
589
+ .map((variable) => `${variable.name}: ${describeOutputType(variable)}`)
530
590
  .join(', ');
531
591
  backendTools.push({
532
592
  name: 'set_result',
533
- description: `Set one requested output variable before calling finish. Required results: ${resultTypes}.`,
593
+ description: `Set one requested output variable before calling finish. Required results: ${resultTypes}. Call it once per variable; the value must match the declared type.`,
534
594
  parameters: {
535
595
  type: 'object',
536
596
  properties: {
537
- variable: { type: 'string', enum: requiredVarNames },
597
+ variable: {
598
+ type: 'string',
599
+ enum: requiredVarNames,
600
+ description: normalizedOutputVars.length === 1
601
+ ? `The output variable name: ${requiredVarNames[0]}.`
602
+ : `One of: ${requiredVarNames.join(', ')}.`,
603
+ },
538
604
  value: {
539
605
  anyOf: [
540
606
  { type: 'string' },
@@ -543,6 +609,7 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
543
609
  { type: 'array' },
544
610
  { type: 'object' },
545
611
  ],
612
+ description: `The value. Expected: ${resultTypes}.`,
546
613
  },
547
614
  },
548
615
  required: ['variable', 'value'],
@@ -580,6 +647,14 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
580
647
  { role: 'user', content: options.conversational ? options.taskDescription : `Subtask: ${options.taskDescription}` },
581
648
  ];
582
649
  const maxIterations = Math.max(1, Number(process.env.DC_MAX_ITERATIONS) || 1000);
650
+ // A nested agent that cannot satisfy `set_result`/`finish` used to retry until
651
+ // the whole turn's model-call budget was gone (100 calls in one observed run).
652
+ // The validation message is the useful part; repeating it does not help, so
653
+ // after a few rejected attempts the task fails with it.
654
+ let rejectedAttempts = 0;
655
+ const maxRejectedAttempts = 4;
656
+ let lastRejection;
657
+ let lastSetResultError;
583
658
  for (let iteration = 0; iteration < maxIterations && !state.finished; iteration++) {
584
659
  if (options.signal?.aborted)
585
660
  break;
@@ -654,12 +729,41 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
654
729
  }
655
730
  }
656
731
  else if (call.name === 'set_result') {
657
- const variableName = String(call.arguments.variable ?? '');
658
- const typedVar = normalizedOutputVars.find((variable) => variable.name === variableName);
732
+ // A provider can deliver a truncated tool call as `arguments: {}`. That
733
+ // is not a type error in the user's data: the value never arrived. Say
734
+ // so, and name the size problem, which is usually the cause.
735
+ const hasValue = Object.prototype.hasOwnProperty.call(call.arguments, 'value');
736
+ if (!hasValue) {
737
+ const typedVarNames = normalizedOutputVars.map((variable) => variable.name).join(', ') || 'none';
738
+ result = {
739
+ success: false,
740
+ error: `set_result received no "value" (the call arrived with no arguments). `
741
+ + `Expected outputs: ${typedVarNames}. The value may be too large for one tool call — `
742
+ + 'return a smaller object, or set the result in a script and return a path.',
743
+ };
744
+ messages.push({ role: 'tool', content: JSON.stringify(result), toolCallId: call.id, toolName: call.name });
745
+ rejectedAttempts += 1;
746
+ lastRejection = JSON.stringify(result);
747
+ lastSetResultError = lastRejection;
748
+ if (rejectedAttempts >= maxRejectedAttempts) {
749
+ throw new Error(`task protocol failed after ${maxRejectedAttempts} attempts: ${lastRejection}`);
750
+ }
751
+ continue;
752
+ }
753
+ const typedVar = matchOutputVar(normalizedOutputVars, call.arguments.variable);
754
+ const requested = typeof call.arguments.variable === 'string'
755
+ ? call.arguments.variable.trim()
756
+ : '';
659
757
  if (!typedVar) {
660
- result = { success: false, error: `Unknown variable: ${variableName}` };
758
+ result = {
759
+ success: false,
760
+ error: requested
761
+ ? `Unknown result variable "${requested}"; expected one of: ${requiredVarNames.join(', ')}.`
762
+ : `Missing "variable"; expected one of: ${requiredVarNames.join(', ')}.`,
763
+ };
661
764
  }
662
765
  else {
766
+ const variableName = typedVar.name;
663
767
  const value = coerceTypedResultValue(typedVar, call.arguments.value);
664
768
  const validationError = validateTypedResultValue(typedVar, value);
665
769
  if (validationError) {
@@ -707,6 +811,33 @@ async function runInjectedAgentLoop(options, normalizedOutputVars, combinedSyste
707
811
  toolCallId: call.id,
708
812
  toolName: call.name,
709
813
  });
814
+ // Count only rejections of the task protocol itself. A user tool that
815
+ // fails is the task's business and may be retried with a fixed argument.
816
+ const rejected = call.name === 'finish'
817
+ ? result.finished === false
818
+ : call.name === 'set_result'
819
+ ? result.success === false
820
+ : false;
821
+ if (rejected) {
822
+ rejectedAttempts += 1;
823
+ lastRejection = JSON.stringify(result);
824
+ if (call.name === 'set_result')
825
+ lastSetResultError = lastRejection;
826
+ }
827
+ else {
828
+ rejectedAttempts = 0;
829
+ }
830
+ if (rejectedAttempts >= maxRejectedAttempts) {
831
+ // `finish` is usually the last call, so quoting only it hides the
832
+ // `set_result` mistake that caused the loop. Report both and name the
833
+ // expected outputs.
834
+ const detail = [lastRejection, lastSetResultError && lastSetResultError !== lastRejection
835
+ ? `last set_result: ${lastSetResultError}`
836
+ : undefined].filter(Boolean).join('; ');
837
+ throw new Error(`task protocol failed after ${maxRejectedAttempts} attempts: ${detail}. `
838
+ + `Expected outputs: ${requiredVarNames.join(', ') || 'none'}. `
839
+ + 'Return them with `set_result` using the declared type, or explain what is missing.');
840
+ }
710
841
  }
711
842
  if (toolCalls.length === 0 && !state.finished) {
712
843
  if (options.conversational) {
@@ -789,11 +920,9 @@ export async function runAgentLoop(options) {
789
920
  // Add set_result tool for output variables
790
921
  if (normalizedOutputVars.length > 0) {
791
922
  const varNames = normalizedOutputVars.map(v => v.name);
792
- const typeSummary = normalizedOutputVars.map(v => {
793
- const renderedType = v.type === 'array' && v.itemType ? `array<${v.itemType}>` : v.type;
794
- const fields = describeSchemaFields(v);
795
- return fields ? `${v.name}: array of objects, each ${fields}` : `${v.name}: ${renderedType}`;
796
- }).join(', ');
923
+ const typeSummary = normalizedOutputVars
924
+ .map((variable) => `${variable.name}: ${describeOutputType(variable)}`)
925
+ .join(', ');
797
926
  const variableSchema = varNames.length === 1
798
927
  ? z.literal(varNames[0])
799
928
  : z.enum(varNames);
@@ -1484,10 +1613,7 @@ async function buildSystemPrompt(options) {
1484
1613
  const resultSection = normalizedOutputVars.length > 0
1485
1614
  ? [
1486
1615
  'You must set all named results before finishing successfully.',
1487
- ...normalizedOutputVars.map((variable) => {
1488
- const typeStr = variable.type === 'array' && variable.itemType ? `array<${variable.itemType}>` : variable.type;
1489
- return `- ${variable.name}: ${typeStr}`;
1490
- }),
1616
+ ...normalizedOutputVars.map((variable) => `- ${variable.name}: ${describeOutputType(variable)}`),
1491
1617
  ].join('\n')
1492
1618
  : 'No named result variables are required. Call finish(true) as soon as the subtask is complete.';
1493
1619
  const stallGuidance = [