@herbertgao/pi-extensions 2026.11.13 → 2026.11.14

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 (175) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +4 -4
  3. package/node_modules/@herbertgao/pi-inline-skill-commands/README.md +6 -2
  4. package/node_modules/@herbertgao/pi-inline-skill-commands/package.json +1 -1
  5. package/node_modules/@herbertgao/pi-inline-skill-commands/src/index.ts +41 -15
  6. package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +10 -0
  7. package/node_modules/@herbertgao/pi-subagents/README.md +37 -7
  8. package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +113 -34
  9. package/node_modules/@herbertgao/pi-subagents/package.json +9 -4
  10. package/node_modules/@herbertgao/pi-subagents/skills/workflow-authoring/SKILL.md +147 -0
  11. package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +48 -32
  12. package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +16 -8
  13. package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +16 -6
  14. package/node_modules/@herbertgao/pi-subagents/src/index.ts +99 -7
  15. package/node_modules/@herbertgao/pi-subagents/src/mention.ts +5 -7
  16. package/node_modules/@herbertgao/pi-subagents/src/settings.ts +26 -0
  17. package/node_modules/@herbertgao/pi-subagents/src/structured-output.ts +13 -5
  18. package/node_modules/@herbertgao/pi-subagents/src/ui/fleet-list.ts +55 -8
  19. package/node_modules/@herbertgao/pi-subagents/src/ui/workflow-card-redraw.ts +86 -0
  20. package/node_modules/@herbertgao/pi-subagents/src/ui/workflow-card.ts +5 -5
  21. package/node_modules/@herbertgao/pi-subagents/src/ui/workflow-dialog.ts +478 -115
  22. package/node_modules/@herbertgao/pi-subagents/src/ui/workflow-menu.ts +77 -6
  23. package/node_modules/@herbertgao/pi-subagents/src/workflow/agent-detail.ts +148 -0
  24. package/node_modules/@herbertgao/pi-subagents/src/workflow/host.ts +88 -14
  25. package/node_modules/@herbertgao/pi-subagents/src/workflow/journal.ts +8 -0
  26. package/node_modules/@herbertgao/pi-subagents/src/workflow/json-schema.ts +507 -57
  27. package/node_modules/@herbertgao/pi-subagents/src/workflow/progress.ts +41 -11
  28. package/node_modules/@herbertgao/pi-subagents/src/workflow/runtime.ts +106 -34
  29. package/node_modules/@herbertgao/pi-subagents/src/workflow/task.ts +43 -5
  30. package/node_modules/@herbertgao/pi-subagents/src/workflow/tool-description.ts +93 -153
  31. package/node_modules/@herbertgao/pi-subagents/src/workflow/worker-source.ts +283 -71
  32. package/node_modules/@pi-plugins/fast-mode/dist/index.mjs +288 -177
  33. package/node_modules/@pi-plugins/fast-mode/dist/index.mjs.map +1 -1
  34. package/node_modules/@pi-plugins/fast-mode/package.json +7 -7
  35. package/node_modules/pi-lens/CHANGELOG-USER.md +6 -6
  36. package/node_modules/pi-lens/CHANGELOG.md +132 -0
  37. package/node_modules/pi-lens/README.md +2 -0
  38. package/node_modules/pi-lens/config/dependency-cruiser-eager-allowlist.json +1 -0
  39. package/node_modules/pi-lens/dist/{chunk-LGH2HHZG.js → chunk-25CAONWT.js} +36 -25
  40. package/node_modules/pi-lens/dist/{chunk-ZQSSO7LO.js → chunk-2DWNYVUN.js} +3 -3
  41. package/node_modules/pi-lens/dist/{chunk-YCR6ZYTE.js → chunk-2RQREVGO.js} +1 -1
  42. package/node_modules/pi-lens/dist/{chunk-VWEBZ2JV.js → chunk-2XALJI2K.js} +40 -40
  43. package/node_modules/pi-lens/dist/{chunk-PQXUZYTQ.js → chunk-2YQ5LI7R.js} +4 -4
  44. package/node_modules/pi-lens/dist/{chunk-VSQT6IHZ.js → chunk-35UJUF27.js} +18 -4
  45. package/node_modules/pi-lens/dist/{chunk-OLWGYBYT.js → chunk-3IWMNN2S.js} +17 -17
  46. package/node_modules/pi-lens/dist/chunk-3UTDLQWO.js +88 -0
  47. package/node_modules/pi-lens/dist/chunk-3Z7Z3B7V.js +27 -0
  48. package/node_modules/pi-lens/dist/{chunk-UNP6Y4JV.js → chunk-4E4OCW3L.js} +16 -16
  49. package/node_modules/pi-lens/dist/{chunk-37IDWT6O.js → chunk-4GLSO2EM.js} +13 -11
  50. package/node_modules/pi-lens/dist/{chunk-QV3JPCMK.js → chunk-5HVDD7IA.js} +1 -1
  51. package/node_modules/pi-lens/dist/{chunk-ZRQMENT4.js → chunk-62PIU4TO.js} +1 -1
  52. package/node_modules/pi-lens/dist/{chunk-3TSMQZG6.js → chunk-6MDWNZ7E.js} +2 -2
  53. package/node_modules/pi-lens/dist/{chunk-WLD6BGON.js → chunk-6MWX2J5H.js} +4 -0
  54. package/node_modules/pi-lens/dist/{chunk-SCU62JLX.js → chunk-6MZLS6CK.js} +7 -7
  55. package/node_modules/pi-lens/dist/{chunk-B42AIWPJ.js → chunk-7BWPQZHE.js} +3 -3
  56. package/node_modules/pi-lens/dist/{chunk-I3NZVAMN.js → chunk-7JMEPY6P.js} +10 -10
  57. package/node_modules/pi-lens/dist/{chunk-7DPXUPRF.js → chunk-7ORBRDFY.js} +2 -2
  58. package/node_modules/pi-lens/dist/{chunk-6E5QLMWI.js → chunk-7WAQ3K26.js} +23 -23
  59. package/node_modules/pi-lens/dist/{chunk-2H5GHZKC.js → chunk-AB26NAHA.js} +7 -7
  60. package/node_modules/pi-lens/dist/{chunk-BBSDJTOV.js → chunk-AFSG6YUB.js} +13 -13
  61. package/node_modules/pi-lens/dist/{chunk-XDNWR7SG.js → chunk-ALTHJUZO.js} +2 -2
  62. package/node_modules/pi-lens/dist/{chunk-UN72ZAJ5.js → chunk-AOEVZXR6.js} +9 -9
  63. package/node_modules/pi-lens/dist/{chunk-HRLF35WL.js → chunk-ARTCVE6I.js} +9 -9
  64. package/node_modules/pi-lens/dist/{chunk-LQQNFCQJ.js → chunk-B2QO7GHY.js} +1 -1
  65. package/node_modules/pi-lens/dist/{chunk-I2FYOZNW.js → chunk-BJGWP4UX.js} +10 -10
  66. package/node_modules/pi-lens/dist/{chunk-G53LBDNU.js → chunk-BKJ6WZKP.js} +6 -6
  67. package/node_modules/pi-lens/dist/{chunk-P5MSLVUW.js → chunk-BQ72NQQM.js} +11 -11
  68. package/node_modules/pi-lens/dist/{chunk-K7O6AHSJ.js → chunk-BWGNVM33.js} +11 -11
  69. package/node_modules/pi-lens/dist/{chunk-ADYQRETD.js → chunk-BYIEU4E4.js} +2 -2
  70. package/node_modules/pi-lens/dist/{chunk-7ZVTFLCC.js → chunk-CDAJQBKJ.js} +11 -11
  71. package/node_modules/pi-lens/dist/{chunk-BKWFDWYM.js → chunk-D3UQWOCB.js} +16 -16
  72. package/node_modules/pi-lens/dist/{chunk-3PAS2ERX.js → chunk-DHFJS2I4.js} +8 -8
  73. package/node_modules/pi-lens/dist/{chunk-KNSOA43J.js → chunk-DKXIUGNA.js} +2 -2
  74. package/node_modules/pi-lens/dist/{chunk-LOEJFTF2.js → chunk-DLGJYQML.js} +2 -2
  75. package/node_modules/pi-lens/dist/{chunk-VGRGW4UL.js → chunk-DNJJVKJZ.js} +31 -31
  76. package/node_modules/pi-lens/dist/{chunk-5HBQL7DH.js → chunk-EBXCYEII.js} +3 -3
  77. package/node_modules/pi-lens/dist/chunk-EH6EHFW2.js +73 -0
  78. package/node_modules/pi-lens/dist/{chunk-W26QZ3OM.js → chunk-EKQDQPX6.js} +10 -10
  79. package/node_modules/pi-lens/dist/{chunk-TWEOJE3D.js → chunk-EXHSWN3P.js} +844 -377
  80. package/node_modules/pi-lens/dist/{chunk-4IZZEN63.js → chunk-F7BFIENK.js} +33 -33
  81. package/node_modules/pi-lens/dist/{chunk-ZI7YHV6I.js → chunk-FD4LJQ6Y.js} +5 -5
  82. package/node_modules/pi-lens/dist/{chunk-RIH3UBK4.js → chunk-GFCPPCNR.js} +11 -11
  83. package/node_modules/pi-lens/dist/{chunk-5JB4WENL.js → chunk-GIK6VUCY.js} +1 -1
  84. package/node_modules/pi-lens/dist/{chunk-PPQHMSPH.js → chunk-GNK2IHVG.js} +15 -15
  85. package/node_modules/pi-lens/dist/{chunk-CXQYK4YT.js → chunk-H6DNTQ4V.js} +2 -2
  86. package/node_modules/pi-lens/dist/{chunk-NGNBJIUO.js → chunk-HERQPNY6.js} +8 -8
  87. package/node_modules/pi-lens/dist/{chunk-INBK3HIT.js → chunk-HFIFEH46.js} +3 -3
  88. package/node_modules/pi-lens/dist/{chunk-J2ZB5MDS.js → chunk-HKRESBMC.js} +20 -20
  89. package/node_modules/pi-lens/dist/{chunk-AZG5UJYY.js → chunk-HMXWZUQW.js} +1 -1
  90. package/node_modules/pi-lens/dist/{chunk-XSC3BCN3.js → chunk-HOWHAKXO.js} +6 -6
  91. package/node_modules/pi-lens/dist/{chunk-7CLWC2UH.js → chunk-HUWW77QY.js} +1 -1
  92. package/node_modules/pi-lens/dist/{chunk-KZ732ZVW.js → chunk-IO6P3NBP.js} +1 -1
  93. package/node_modules/pi-lens/dist/{chunk-7WKVCZI4.js → chunk-J7VNGHLN.js} +2 -2
  94. package/node_modules/pi-lens/dist/{chunk-QE7BJQ6K.js → chunk-JGCBMCCS.js} +8 -8
  95. package/node_modules/pi-lens/dist/{chunk-PFJLYRO3.js → chunk-KIQSNH3F.js} +34 -34
  96. package/node_modules/pi-lens/dist/{chunk-JGUDT5KD.js → chunk-KSQJBGRO.js} +5 -5
  97. package/node_modules/pi-lens/dist/{chunk-RJ3APNIP.js → chunk-KXKM2DFP.js} +4 -4
  98. package/node_modules/pi-lens/dist/chunk-L3Q72QGQ.js +25 -0
  99. package/node_modules/pi-lens/dist/{chunk-6GLMMGCX.js → chunk-LDPY22FS.js} +2 -2
  100. package/node_modules/pi-lens/dist/{chunk-3PO4YYOX.js → chunk-M3QGXGY4.js} +3793 -3814
  101. package/node_modules/pi-lens/dist/{chunk-NNEQOC3U.js → chunk-MET776XM.js} +9 -7
  102. package/node_modules/pi-lens/dist/{chunk-X33QECRJ.js → chunk-MKQFSW2Q.js} +2 -2
  103. package/node_modules/pi-lens/dist/{chunk-7U36VQL6.js → chunk-MKW7ZUTT.js} +3 -3
  104. package/node_modules/pi-lens/dist/{chunk-PQF6YGYB.js → chunk-N2U644ZW.js} +4 -18
  105. package/node_modules/pi-lens/dist/{chunk-DI4AZZNY.js → chunk-NAACKIKT.js} +2 -2
  106. package/node_modules/pi-lens/dist/{chunk-O6LCSHHI.js → chunk-NHHS5T52.js} +1 -1
  107. package/node_modules/pi-lens/dist/{chunk-DCA2MXVQ.js → chunk-NIZKGBOU.js} +51 -27
  108. package/node_modules/pi-lens/dist/{chunk-PKWEBMCA.js → chunk-NNDZ7DOP.js} +194 -111
  109. package/node_modules/pi-lens/dist/{chunk-Q7J3JYJ2.js → chunk-NZ7NSKIW.js} +13 -13
  110. package/node_modules/pi-lens/dist/{chunk-S6MCMD27.js → chunk-OG3UCAHV.js} +3 -3
  111. package/node_modules/pi-lens/dist/{chunk-3QXFUP6T.js → chunk-OHTHVILU.js} +12 -12
  112. package/node_modules/pi-lens/dist/{chunk-NHA5K226.js → chunk-P7EV5ISR.js} +17 -2
  113. package/node_modules/pi-lens/dist/{chunk-L6BDRYUK.js → chunk-PVV27TOX.js} +5 -5
  114. package/node_modules/pi-lens/dist/{chunk-HIRBC72R.js → chunk-Q32RJDYC.js} +4 -4
  115. package/node_modules/pi-lens/dist/{chunk-QE5DJ7MX.js → chunk-Q4UQNTLB.js} +4 -4
  116. package/node_modules/pi-lens/dist/{chunk-SDYLT45T.js → chunk-QQGZY2BR.js} +2 -2
  117. package/node_modules/pi-lens/dist/{chunk-QMWKSYWB.js → chunk-S247BOH2.js} +4 -4
  118. package/node_modules/pi-lens/dist/{chunk-ARTYCUGF.js → chunk-S7KUDW73.js} +121 -18
  119. package/node_modules/pi-lens/dist/{chunk-6CFSDLHE.js → chunk-SJ2SR432.js} +17 -17
  120. package/node_modules/pi-lens/dist/{chunk-CQY2HEH7.js → chunk-SL4G67AP.js} +2 -2
  121. package/node_modules/pi-lens/dist/{chunk-LVLPA32Q.js → chunk-T2D2WWP6.js} +48 -11
  122. package/node_modules/pi-lens/dist/{chunk-HIJ4CHFF.js → chunk-T7KHGEJ5.js} +3 -3
  123. package/node_modules/pi-lens/dist/{chunk-74AMPWNC.js → chunk-TABDCX35.js} +2 -2
  124. package/node_modules/pi-lens/dist/chunk-TFSBHRL2.js +25 -0
  125. package/node_modules/pi-lens/dist/{chunk-KNJH6WJQ.js → chunk-TQKWR5AZ.js} +7 -7
  126. package/node_modules/pi-lens/dist/chunk-TWAQFMLW.js +110 -0
  127. package/node_modules/pi-lens/dist/{chunk-TKOFTLGB.js → chunk-UARVPNAU.js} +33 -33
  128. package/node_modules/pi-lens/dist/chunk-UWBD3JMH.js +56 -0
  129. package/node_modules/pi-lens/dist/{chunk-K4Z7MK6T.js → chunk-VEF3GFOL.js} +15 -15
  130. package/node_modules/pi-lens/dist/{chunk-4DAHGU3V.js → chunk-W2JPGW7Q.js} +27 -25
  131. package/node_modules/pi-lens/dist/{chunk-CZJGAOKL.js → chunk-W2YHHNY3.js} +7 -7
  132. package/node_modules/pi-lens/dist/{chunk-R4L7KZ7R.js → chunk-WA6U2URH.js} +9 -9
  133. package/node_modules/pi-lens/dist/{chunk-OZQQ3AIR.js → chunk-WAK2AXQQ.js} +9 -9
  134. package/node_modules/pi-lens/dist/{chunk-DFKOA74L.js → chunk-WH6O4IFD.js} +10 -10
  135. package/node_modules/pi-lens/dist/{chunk-27JQHK7Y.js → chunk-WJ7GY5WJ.js} +1 -1
  136. package/node_modules/pi-lens/dist/{chunk-KXGTLAUW.js → chunk-WWZLT6AM.js} +2 -2
  137. package/node_modules/pi-lens/dist/{chunk-O2MHF7QM.js → chunk-XRLZ75YY.js} +38 -38
  138. package/node_modules/pi-lens/dist/chunk-XWWFESYZ.js +27 -0
  139. package/node_modules/pi-lens/dist/chunk-XZSXSM4K.js +27 -0
  140. package/node_modules/pi-lens/dist/{chunk-C5PLBTNR.js → chunk-Y2LIOCA7.js} +1 -1
  141. package/node_modules/pi-lens/dist/{chunk-AXAR5ZW2.js → chunk-YIEHUKX2.js} +84 -169
  142. package/node_modules/pi-lens/dist/{chunk-B2OXE4AH.js → chunk-YOMLAKU6.js} +182 -180
  143. package/node_modules/pi-lens/dist/{chunk-MOAVEPC4.js → chunk-YV7XVYSJ.js} +11 -11
  144. package/node_modules/pi-lens/dist/{chunk-ZULZS2I6.js → chunk-ZVQUYCBU.js} +996 -365
  145. package/node_modules/pi-lens/dist/index.js +4911 -2376
  146. package/node_modules/pi-lens/dist/mcp/analyze-cli.js +8 -8
  147. package/node_modules/pi-lens/dist/mcp/cli.js +40 -40
  148. package/node_modules/pi-lens/dist/mcp/server.js +321 -234
  149. package/node_modules/pi-lens/dist/mcp/worker.js +58 -58
  150. package/node_modules/pi-lens/dist/probes/installer.js +21 -21
  151. package/node_modules/pi-lens/docs/ci-bot-pr-checks.md +18 -9
  152. package/node_modules/pi-lens/docs/ci.md +7 -0
  153. package/node_modules/pi-lens/docs/defect-shapes.md +43 -28
  154. package/node_modules/pi-lens/docs/globalconfig.md +1 -0
  155. package/node_modules/pi-lens/docs/invariants/dispatch-runners.md +15 -0
  156. package/node_modules/pi-lens/docs/lsp-capability-matrix.md +3 -3
  157. package/node_modules/pi-lens/docs/lsp-idle-eviction.md +3 -2
  158. package/node_modules/pi-lens/docs/module-report-read-symbol.md +6 -2
  159. package/node_modules/pi-lens/docs/pi-lens-merge-policy.md +3 -2
  160. package/node_modules/pi-lens/docs/pi-lens-orchestration.md +2 -1
  161. package/node_modules/pi-lens/docs/pi-lens-subagent.md +2 -1
  162. package/node_modules/pi-lens/docs/servercapabilities.md +1 -1
  163. package/node_modules/pi-lens/docs/settings.md +1 -0
  164. package/node_modules/pi-lens/docs/usage.md +1 -0
  165. package/node_modules/pi-lens/package.json +1 -1
  166. package/package.json +7 -5
  167. package/node_modules/pi-lens/dist/chunk-2H7NFZ4Q.js +0 -25
  168. package/node_modules/pi-lens/dist/chunk-5YJXF2IS.js +0 -56
  169. package/node_modules/pi-lens/dist/chunk-GCSDF5IE.js +0 -27
  170. package/node_modules/pi-lens/dist/chunk-H5API74E.js +0 -67
  171. package/node_modules/pi-lens/dist/chunk-HBX55MWO.js +0 -27
  172. package/node_modules/pi-lens/dist/chunk-JOIKBUQN.js +0 -88
  173. package/node_modules/pi-lens/dist/chunk-M2YMBT5G.js +0 -73
  174. package/node_modules/pi-lens/dist/chunk-MJRGPEQ5.js +0 -25
  175. package/node_modules/pi-lens/dist/chunk-TFKN5EIR.js +0 -27
@@ -5,17 +5,18 @@
5
5
  * be used two ways: as a tool's `parameters` (so the provider fills the fields)
6
6
  * and as the check that decides whether what came back is usable.
7
7
  *
8
- * ## Which typebox
8
+ * ## Ajv, for compatibility with Claude Code
9
9
  *
10
- * **`typebox`, not `@sinclair/typebox`.** They are different packages.
11
- * `@sinclair/typebox` (0.34) dispatches on a `Kind` symbol that a schema
12
- * arriving over the wire does not carry, so `Value.Check` throws `Unknown type`
13
- * on a plain JSON Schema — and `Type.Unsafe` does not help, it stamps a `Kind`
14
- * that is not registered. `typebox` v1 is a standards JSON Schema validator and
15
- * takes the schema as-is. It is also the package pi itself types
16
- * `ToolDefinition.parameters` against and supplies to extensions at runtime
17
- * (a peer dependency here), so the same schema object serves both roles with no
18
- * conversion.
10
+ * Both jobs — deciding whether a schema is usable, and whether a payload
11
+ * matches it — use Ajv, so a Claude Code workflow script sees the same
12
+ * refusals here, with the same text. The checks run in this order: a size
13
+ * guard, `$async`, the draft-07 meta-schema, `compile` (bad `pattern`, unresolvable `$ref`, and, in Ajv's
14
+ * default strict mode, any unknown keyword), then the unsatisfiability lint
15
+ * below. Formats are not validated, in the schema or the payload.
16
+ *
17
+ * `logger: false` is the one option of ours: strict mode *logs* some findings
18
+ * (`strictTypes`) rather than throwing, and the default logger would write
19
+ * them into pi's TUI. It changes no accept/refuse decision.
19
20
  *
20
21
  * ## Why we validate at all
21
22
  *
@@ -25,17 +26,24 @@
25
26
  * provider*, not an enforcement point. Every guarantee the script gets about
26
27
  * the shape of its result is made here.
27
28
  *
29
+ * Ajv compiles validators with `new Function`, so this runs host-side only —
30
+ * never inside the workflow worker, whose vm has code generation disabled.
31
+ *
28
32
  * Pure and pi-free on purpose, so `runtime.ts` can import it without dragging
29
33
  * sessions and models into the runtime's tests.
30
34
  */
31
35
 
32
- import { Check, Errors } from "typebox/value"
36
+ import { Ajv, type ErrorObject, type ValidateFunction } from "ajv"
33
37
 
34
- /** Largest schema we will accept, serialized. */
35
- const MAX_SCHEMA_BYTES = 64 * 1024
38
+ /** The size guard: values visited, and nesting depth. */
39
+ const MAX_SCHEMA_NODES = 100_000
40
+ const MAX_SCHEMA_DEPTH = 10_000
36
41
 
37
- /** How many validation errors are quoted back to the model. */
38
- const MAX_REPORTED_ERRORS = 5
42
+ /** Truncation for a disallowed property name and for a quoted enum/const value. */
43
+ const MAX_ERROR_KEY = 80
44
+ const MAX_ERROR_VALUE = 300
45
+
46
+ const INVALID_PREFIX = "agent({schema}) received an invalid JSON Schema: "
39
47
 
40
48
  export interface CompiledSchema {
41
49
  /** The schema as given, for the tool's `parameters` and the journal key. */
@@ -51,10 +59,11 @@ export type SchemaCompilation =
51
59
  /**
52
60
  * Turn a script-supplied schema into something we can check against.
53
61
  *
54
- * Rejects up front rather than at the first tool call. A schema whose root is
55
- * not an object cannot be a tool's input schema at all, so it would break every
56
- * request the child makes rather than just the last one — and the author should
57
- * hear about that before a model is paid to discover it.
62
+ * Rejects up front rather than at the first tool call, so the author hears
63
+ * about a bad schema before a model is paid to discover it. Something that is
64
+ * not an object at all is refused with our own message; everything else —
65
+ * including an object whose root `type` is not `"object"` — goes through
66
+ * the checks below, in order.
58
67
  */
59
68
  export function compileJsonSchema(schema: unknown): SchemaCompilation {
60
69
  if (typeof schema !== "object" || schema === null || Array.isArray(schema)) {
@@ -64,80 +73,521 @@ export function compileJsonSchema(schema: unknown): SchemaCompilation {
64
73
  }
65
74
  }
66
75
  const root = schema as Record<string, unknown>
67
- if (root.type !== "object") {
68
- return {
69
- ok: false,
70
- message:
71
- 'agent() opts.schema must have `type: "object"` at its root — it becomes the tool\'s input schema, ' +
72
- "and a non-object root is not something a model can be asked to fill.",
73
- }
74
- }
75
76
 
76
- let serialized: string
77
77
  try {
78
- serialized = JSON.stringify(root)
78
+ JSON.stringify(root)
79
79
  } catch {
80
80
  return {
81
81
  ok: false,
82
82
  message: "agent() opts.schema must be JSON-serializable.",
83
83
  }
84
84
  }
85
- if (serialized.length > MAX_SCHEMA_BYTES) {
85
+
86
+ // Checked here so a schema the validator cannot use fails at the call that
87
+ // wrote it, rather than inside a child's tool handler where the only symptom
88
+ // is an agent that never returns.
89
+ let validate: ValidateFunction
90
+ try {
91
+ if (exceedsSizeGuard(root, { remaining: MAX_SCHEMA_NODES }, 0)) {
92
+ return { ok: false, message: `${INVALID_PREFIX}schema too large` }
93
+ }
94
+ if (root.$async)
95
+ return {
96
+ ok: false,
97
+ message: `${INVALID_PREFIX}$async schemas are not supported`,
98
+ }
99
+ // A fresh instance per schema: nothing one script
100
+ // compiles can be resolved by another's `$ref`.
101
+ const ajv = new Ajv({
102
+ allErrors: true,
103
+ validateFormats: false,
104
+ multipleOfPrecision: 6,
105
+ logger: false,
106
+ })
107
+ if (!ajv.validateSchema(root))
108
+ return { ok: false, message: INVALID_PREFIX + ajv.errorsText(ajv.errors) }
109
+ validate = ajv.compile(root)
110
+ } catch (error) {
86
111
  return {
87
112
  ok: false,
88
- message: `agent() opts.schema is too large (${serialized.length} bytes; the limit is ${MAX_SCHEMA_BYTES}).`,
113
+ message:
114
+ INVALID_PREFIX +
115
+ (error instanceof Error ? error.message : String(error)),
89
116
  }
90
117
  }
91
118
 
92
- // Smoke-tested here so a schema the validator cannot walk fails at the call
93
- // that wrote it, with the schema in hand, rather than inside a child's tool
94
- // handler where the only symptom is an agent that never returns.
95
- try {
96
- Check(root, {})
97
- } catch (error) {
119
+ const finding = lintJsonSchema(root)
120
+ if (finding !== undefined) {
121
+ const consequence =
122
+ finding.reason === "root_not_object"
123
+ ? "the API only accepts an object-rooted tool input schema, so every request would be rejected"
124
+ : finding.scope === "whole"
125
+ ? "no output can satisfy this schema, so StructuredOutput validation would fail on every attempt"
126
+ : "one property or item sub-schema admits no value, so any output that fills it in fails validation"
98
127
  return {
99
128
  ok: false,
100
- message: `agent() opts.schema is not a schema this runtime can validate: ${
101
- error instanceof Error ? error.message : String(error)
102
- }`,
129
+ message:
130
+ `agent({schema}) received an unusable JSON Schema — ${consequence}: ${finding.message}. ` +
131
+ "The subagent was not started — fix the schema and call agent() again.",
103
132
  }
104
133
  }
105
134
 
106
135
  return {
107
136
  ok: true,
108
- compiled: { schema: root, check: (value) => checkAgainst(root, value) },
137
+ compiled: { schema: root, check: (value) => checkAgainst(validate, value) },
138
+ }
139
+ }
140
+
141
+ /**
142
+ * The size guard: true once more than `MAX_SCHEMA_NODES` values have
143
+ * been visited or nesting passes `MAX_SCHEMA_DEPTH`. A stack overflow on a
144
+ * pathologically deep schema reads as "too large" too.
145
+ */
146
+ function exceedsSizeGuard(
147
+ value: unknown,
148
+ budget: { remaining: number },
149
+ depth: number,
150
+ ): boolean {
151
+ try {
152
+ if (--budget.remaining < 0 || depth > MAX_SCHEMA_DEPTH) return true
153
+ if (typeof value !== "object" || value === null) return false
154
+ for (const child of Object.values(value)) {
155
+ if (exceedsSizeGuard(child, budget, depth + 1)) return true
156
+ }
157
+ return false
158
+ } catch {
159
+ return true
109
160
  }
110
161
  }
111
162
 
112
163
  function checkAgainst(
113
- schema: Record<string, unknown>,
164
+ validate: ValidateFunction,
114
165
  value: unknown,
115
166
  ): true | string {
116
167
  let valid: boolean
117
168
  try {
118
- valid = Check(schema, value)
169
+ valid = validate(value) as boolean
119
170
  } catch (error) {
120
171
  // Reported rather than thrown: a schema that compiled but trips on a
121
172
  // particular value must fail that call, not the run.
122
173
  return `the value could not be validated: ${error instanceof Error ? error.message : String(error)}`
123
174
  }
124
175
  if (valid) return true
176
+ const errors = validate.errors ?? []
177
+ if (errors.length === 0) return "the value does not match the required schema"
178
+ return errors.map((error) => describeError(error, value)).join(", ")
179
+ }
125
180
 
126
- const reported: string[] = []
127
- try {
128
- for (const error of Errors(schema, value)) {
129
- // `instancePath` is JSON Pointer (`/a/b`); the model wrote the schema in
130
- // JavaScript, so it reads `$.a.b` far more easily.
131
- const path = String(error.instancePath ?? "")
132
- const where = path === "" ? "$" : `$${path.replace(/\//g, ".")}`
133
- reported.push(`${where}: ${error.message}`)
134
- if (reported.length >= MAX_REPORTED_ERRORS) break
181
+ /** One Ajv error as the child reads it: the path, the message, and what it saw. */
182
+ function describeError(error: ErrorObject, root: unknown): string {
183
+ const base = `${error.instancePath || "root"}: ${error.message}`
184
+ const params = error.params as Record<string, unknown>
185
+ switch (error.keyword) {
186
+ case "additionalProperties": {
187
+ const name = params.additionalProperty
188
+ return typeof name === "string"
189
+ ? `${base} ('${truncate(name, MAX_ERROR_KEY)}' is not allowed)`
190
+ : base
191
+ }
192
+ case "minLength":
193
+ case "maxLength": {
194
+ const found = valueAt(root, error.instancePath)
195
+ return typeof found === "string"
196
+ ? `${base} (got ${[...found].length})`
197
+ : base
198
+ }
199
+ case "minItems":
200
+ case "maxItems": {
201
+ const found = valueAt(root, error.instancePath)
202
+ return Array.isArray(found) ? `${base} (got ${found.length})` : base
203
+ }
204
+ case "minProperties":
205
+ case "maxProperties": {
206
+ const found = valueAt(root, error.instancePath)
207
+ return isNode(found) ? `${base} (got ${Object.keys(found).length})` : base
135
208
  }
209
+ case "enum": {
210
+ const allowed = Array.isArray(params.allowedValues)
211
+ ? quoteJson(params.allowedValues)
212
+ : undefined
213
+ return allowed === undefined ? base : `${base}: ${allowed}`
214
+ }
215
+ case "const": {
216
+ const allowed =
217
+ "allowedValue" in params ? quoteJson(params.allowedValue) : undefined
218
+ return allowed === undefined ? base : `${base}: ${allowed}`
219
+ }
220
+ default:
221
+ return base
222
+ }
223
+ }
224
+
225
+ /** The value a JSON Pointer names inside `root`, or undefined. */
226
+ function valueAt(root: unknown, pointer: string): unknown {
227
+ let current = root
228
+ for (const token of pointer.split("/").slice(1)) {
229
+ const key = token.replaceAll("~1", "/").replaceAll("~0", "~")
230
+ if (Array.isArray(current)) current = current[Number(key)]
231
+ else if (isNode(current) && Object.hasOwn(current, key))
232
+ current = current[key]
233
+ else return undefined
234
+ }
235
+ return current
236
+ }
237
+
238
+ function quoteJson(value: unknown): string | undefined {
239
+ let json: string | undefined
240
+ try {
241
+ json = JSON.stringify(value)
136
242
  } catch {
137
- // Errors() can trip where Check() merely returned false. A vaguer message
138
- // still names the right problem.
243
+ return undefined
244
+ }
245
+ return typeof json === "string" ? truncate(json, MAX_ERROR_VALUE) : undefined
246
+ }
247
+
248
+ const truncate = (text: string, max: number) =>
249
+ text.length > max ? `${text.slice(0, max)}…` : text
250
+
251
+ /* ------------------------------------------------------------------ *
252
+ * The unsatisfiability lint
253
+ *
254
+ * Five findings, each worded for the script author. It reports only provable contradictions — a `required` name the
255
+ * schema also forbids, an `enum` or `const` that contradicts `type`, crossed
256
+ * bounds — never style. `whole` means no output can pass; `subschema` means
257
+ * one optional property or array item can never be filled.
258
+ * ------------------------------------------------------------------ */
259
+
260
+ /** Nesting depth and node budget past which the walk stops looking. */
261
+ const LINT_MAX_DEPTH = 32
262
+ const LINT_MAX_NODES = 100_000
263
+ /** Truncation for a property name and for a path, in a finding's message. */
264
+ const LINT_MAX_KEY = 80
265
+ const LINT_MAX_PATH = 300
266
+
267
+ const SIMPLE_TYPES = new Set([
268
+ "object",
269
+ "array",
270
+ "string",
271
+ "integer",
272
+ "number",
273
+ "boolean",
274
+ "null",
275
+ ])
276
+
277
+ export interface SchemaLintFinding {
278
+ reason:
279
+ | "root_not_object"
280
+ | "required_property_forbidden"
281
+ | "enum_type_mismatch"
282
+ | "const_mismatch"
283
+ | "crossed_bounds"
284
+ scope: "whole" | "subschema"
285
+ message: string
286
+ }
287
+
288
+ type SchemaNode = Record<string, unknown>
289
+
290
+ const isNode = (value: unknown): value is SchemaNode =>
291
+ typeof value === "object" && value !== null && !Array.isArray(value)
292
+
293
+ /** Own keys only, so a schema cannot pick a keyword up from `Object.prototype`. */
294
+ const own = (node: SchemaNode, key: string): unknown =>
295
+ Object.hasOwn(node, key) ? node[key] : undefined
296
+
297
+ const finite = (value: unknown): number | undefined =>
298
+ typeof value === "number" && Number.isFinite(value) ? value : undefined
299
+
300
+ /** A `required` entry worth checking: a string that is not an `Object.prototype` member. */
301
+ const isRequiredName = (value: unknown): value is string =>
302
+ typeof value === "string" && !(value in Object.prototype)
303
+
304
+ /** The first contradiction in `schema`, or undefined. A root that is not `type: "object"` is the first one. */
305
+ export function lintJsonSchema(
306
+ schema: SchemaNode,
307
+ ): SchemaLintFinding | undefined {
308
+ // Own key and exact string: `type: ["object"]` is refused too.
309
+ if (own(schema, "type") !== "object") {
310
+ return {
311
+ reason: "root_not_object",
312
+ scope: "whole",
313
+ message:
314
+ "the root schema must declare type: 'object' (the API rejects any other root type for a tool input schema); " +
315
+ "wrap arrays or primitives in an object property",
316
+ }
139
317
  }
140
- return reported.length > 0
141
- ? reported.join("; ")
142
- : "the value does not match the required schema"
318
+ return lintNode(schema, "", true, LINT_MAX_DEPTH, {
319
+ remaining: LINT_MAX_NODES,
320
+ })
321
+ }
322
+
323
+ function lintNode(
324
+ node: unknown,
325
+ path: string,
326
+ whole: boolean,
327
+ depth: number,
328
+ budget: { remaining: number },
329
+ ): SchemaLintFinding | undefined {
330
+ if (depth <= 0 || --budget.remaining < 0) return undefined
331
+ // A `$ref` target is not resolved, so nothing under it can be proven.
332
+ if (!isNode(node) || Object.hasOwn(node, "$ref")) return undefined
333
+
334
+ const types = declaredTypes(node)
335
+ const local = lintLocal(node, path, types)
336
+ if (local !== undefined)
337
+ return { ...local, scope: whole ? "whole" : "subschema" }
338
+
339
+ const properties = own(node, "properties")
340
+ if (allows(types, "object") && isNode(properties)) {
341
+ const requiredList = own(node, "required")
342
+ const required = new Set(
343
+ Array.isArray(requiredList) ? requiredList.filter(isRequiredName) : [],
344
+ )
345
+ // Only a property the object must carry can make the whole schema
346
+ // unsatisfiable; an optional one can simply be left out.
347
+ const mustBeObject = isExactly(types, "object")
348
+ for (const [key, child] of Object.entries(properties)) {
349
+ if (key === "__proto__") continue
350
+ const found = lintNode(
351
+ child,
352
+ `${path}/properties/${pointerToken(key)}`,
353
+ whole && mustBeObject && required.has(key),
354
+ depth - 1,
355
+ budget,
356
+ )
357
+ if (found !== undefined) return found
358
+ }
359
+ }
360
+
361
+ const items = own(node, "items")
362
+ if (allows(types, "array") && isNode(items))
363
+ return lintNode(items, `${path}/items`, false, depth - 1, budget)
364
+ return undefined
143
365
  }
366
+
367
+ function lintLocal(
368
+ node: SchemaNode,
369
+ path: string,
370
+ types: string[] | undefined,
371
+ ): Omit<SchemaLintFinding, "scope"> | undefined {
372
+ const where =
373
+ path === "" ? "the root object" : `the sub-schema at ${truncatePath(path)}`
374
+
375
+ const required = own(node, "required")
376
+ if (
377
+ isExactly(types, "object") &&
378
+ Array.isArray(required) &&
379
+ own(node, "additionalProperties") === false &&
380
+ own(node, "patternProperties") === undefined
381
+ ) {
382
+ const properties = own(node, "properties")
383
+ const declared = isNode(properties) ? properties : {}
384
+ for (const name of required) {
385
+ if (isRequiredName(name) && !Object.hasOwn(declared, name)) {
386
+ return {
387
+ reason: "required_property_forbidden",
388
+ message: `${where} lists "${displayKey(name)}" in required but does not declare it in properties, and additionalProperties is false, so no object can satisfy it — declare the property or drop it from required`,
389
+ }
390
+ }
391
+ }
392
+ }
393
+
394
+ const enumValues = own(node, "enum")
395
+ if (
396
+ types !== undefined &&
397
+ Array.isArray(enumValues) &&
398
+ enumValues.length > 0
399
+ ) {
400
+ if (!enumValues.some((value) => matchesType(types, value))) {
401
+ return {
402
+ reason: "enum_type_mismatch",
403
+ message: `${where} declares type ${typeList(types)} but none of its enum values has that type, so no value can satisfy it — change type to match the enum values or vice versa`,
404
+ }
405
+ }
406
+ }
407
+
408
+ const constValue = own(node, "const")
409
+ if (constValue !== undefined) {
410
+ if (types !== undefined && !matchesType(types, constValue)) {
411
+ return {
412
+ reason: "const_mismatch",
413
+ message: `${where} declares type ${typeList(types)} but its const value does not have that type, so no value can satisfy it`,
414
+ }
415
+ }
416
+ if (
417
+ Array.isArray(enumValues) &&
418
+ enumValues.length > 0 &&
419
+ isPrimitive(constValue) &&
420
+ !enumValues.some((value) => value === constValue)
421
+ ) {
422
+ return {
423
+ reason: "const_mismatch",
424
+ message: `${where} has a const value that is not one of its enum values, so no value can satisfy it`,
425
+ }
426
+ }
427
+ }
428
+
429
+ const bounds = crossedBounds(node, types)
430
+ if (bounds !== undefined) {
431
+ return {
432
+ reason: "crossed_bounds",
433
+ message: `${where} has ${bounds}, so no value can satisfy it`,
434
+ }
435
+ }
436
+ return undefined
437
+ }
438
+
439
+ function crossedBounds(
440
+ node: SchemaNode,
441
+ types: string[] | undefined,
442
+ ): string | undefined {
443
+ if (
444
+ isExactly(types, "number") ||
445
+ isExactly(types, "integer") ||
446
+ isPair(types, "number", "integer")
447
+ ) {
448
+ const minimum = finite(own(node, "minimum"))
449
+ const exclusiveMinimum = finite(own(node, "exclusiveMinimum"))
450
+ const maximum = finite(own(node, "maximum"))
451
+ const exclusiveMaximum = finite(own(node, "exclusiveMaximum"))
452
+ // The tighter of each pair decides; an exclusive bound wins a tie.
453
+ const lower =
454
+ exclusiveMinimum !== undefined &&
455
+ (minimum === undefined || exclusiveMinimum >= minimum)
456
+ ? {
457
+ value: exclusiveMinimum,
458
+ exclusive: true,
459
+ keyword: "exclusiveMinimum",
460
+ }
461
+ : minimum !== undefined
462
+ ? { value: minimum, exclusive: false, keyword: "minimum" }
463
+ : undefined
464
+ const upper =
465
+ exclusiveMaximum !== undefined &&
466
+ (maximum === undefined || exclusiveMaximum <= maximum)
467
+ ? {
468
+ value: exclusiveMaximum,
469
+ exclusive: true,
470
+ keyword: "exclusiveMaximum",
471
+ }
472
+ : maximum !== undefined
473
+ ? { value: maximum, exclusive: false, keyword: "maximum" }
474
+ : undefined
475
+ if (
476
+ lower !== undefined &&
477
+ upper !== undefined &&
478
+ (lower.value > upper.value ||
479
+ (lower.value === upper.value && (lower.exclusive || upper.exclusive)))
480
+ ) {
481
+ return `${lower.keyword} ${lower.value} and ${upper.keyword} ${upper.value}, which admit no number`
482
+ }
483
+ }
484
+ if (isExactly(types, "string")) {
485
+ const crossed = crossedPair(node, "minLength", "maxLength")
486
+ if (crossed !== undefined) return crossed
487
+ }
488
+ if (isExactly(types, "array")) {
489
+ const crossed = crossedPair(node, "minItems", "maxItems")
490
+ if (crossed !== undefined) return crossed
491
+ }
492
+ if (isExactly(types, "object")) {
493
+ const crossed = crossedPair(node, "minProperties", "maxProperties")
494
+ if (crossed !== undefined) return crossed
495
+ const maxProperties = finite(own(node, "maxProperties"))
496
+ const required = own(node, "required")
497
+ if (maxProperties !== undefined && Array.isArray(required)) {
498
+ const count = required.filter(isRequiredName).length
499
+ if (count > maxProperties)
500
+ return `${count} required properties but maxProperties ${maxProperties}`
501
+ }
502
+ }
503
+ return undefined
504
+ }
505
+
506
+ function crossedPair(
507
+ node: SchemaNode,
508
+ minKey: string,
509
+ maxKey: string,
510
+ ): string | undefined {
511
+ const min = finite(own(node, minKey))
512
+ const max = finite(own(node, maxKey))
513
+ return min !== undefined && max !== undefined && min > max
514
+ ? `${minKey} ${min} greater than ${maxKey} ${max}`
515
+ : undefined
516
+ }
517
+
518
+ /** `type` as a list, with `nullable: true` folded in; undefined when absent or unrecognised. */
519
+ function declaredTypes(node: SchemaNode): string[] | undefined {
520
+ const type = own(node, "type")
521
+ let types: string[]
522
+ if (typeof type === "string" && SIMPLE_TYPES.has(type)) types = [type]
523
+ else if (
524
+ Array.isArray(type) &&
525
+ type.length > 0 &&
526
+ type.every((entry) => typeof entry === "string" && SIMPLE_TYPES.has(entry))
527
+ ) {
528
+ types = type.slice()
529
+ } else return undefined
530
+ if (own(node, "nullable") === true && !types.includes("null"))
531
+ types.push("null")
532
+ return types
533
+ }
534
+
535
+ /** No declared type allows anything. */
536
+ const allows = (types: string[] | undefined, type: string) =>
537
+ types === undefined || types.includes(type)
538
+ const isExactly = (types: string[] | undefined, type: string) =>
539
+ types !== undefined && types.length === 1 && types[0] === type
540
+ const isPair = (types: string[] | undefined, a: string, b: string) =>
541
+ types !== undefined &&
542
+ types.length === 2 &&
543
+ ((types[0] === a && types[1] === b) || (types[0] === b && types[1] === a))
544
+
545
+ function matchesType(types: string[], value: unknown): boolean {
546
+ return types.some((type) => {
547
+ switch (type) {
548
+ case "null":
549
+ return value === null
550
+ case "boolean":
551
+ return typeof value === "boolean"
552
+ case "string":
553
+ return typeof value === "string"
554
+ case "number":
555
+ return typeof value === "number" && Number.isFinite(value)
556
+ case "integer":
557
+ return typeof value === "number" && Number.isInteger(value)
558
+ case "array":
559
+ return Array.isArray(value)
560
+ case "object":
561
+ return isNode(value)
562
+ default:
563
+ return true
564
+ }
565
+ })
566
+ }
567
+
568
+ const isPrimitive = (value: unknown) =>
569
+ value === null ||
570
+ typeof value === "string" ||
571
+ typeof value === "number" ||
572
+ typeof value === "boolean"
573
+
574
+ const typeList = (types: string[]) =>
575
+ types.length === 1
576
+ ? `'${types[0]}'`
577
+ : `[${types.map((type) => `'${type}'`).join(", ")}]`
578
+
579
+ /** A property name as quoted in a message: one line, no control characters, bounded. */
580
+ function displayKey(key: string): string {
581
+ const clean = key
582
+ .replace(/\s+/g, " ")
583
+ .replace(/[\u0000-\u001f\u007f-\u009f]/g, "")
584
+ return clean.length > LINT_MAX_KEY
585
+ ? `${clean.slice(0, LINT_MAX_KEY)}…`
586
+ : clean
587
+ }
588
+
589
+ const pointerToken = (key: string) =>
590
+ displayKey(key).replaceAll("~", "~0").replaceAll("/", "~1")
591
+
592
+ const truncatePath = (path: string) =>
593
+ path.length > LINT_MAX_PATH ? `…${path.slice(-LINT_MAX_PATH)}` : path