@halonicjs/testing 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/NOTICE +4 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/http-kit/src/address-checking.d.ts +19 -0
  4. package/dist/adapters/http-kit/src/address-checking.js +1 -0
  5. package/dist/adapters/http-kit/src/connector-host-class.d.ts +14 -0
  6. package/dist/adapters/http-kit/src/connector-host-class.js +34 -0
  7. package/dist/adapters/http-kit/src/index.d.ts +3 -0
  8. package/dist/adapters/http-kit/src/index.js +2 -0
  9. package/dist/adapters/http-kit/src/ollama-tags.d.ts +20 -0
  10. package/dist/adapters/http-kit/src/ollama-tags.js +38 -0
  11. package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
  12. package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
  13. package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
  14. package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
  15. package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
  16. package/dist/adapters/integration-kit/src/declaration.js +4 -3
  17. package/dist/adapters/integration-kit/src/host.d.ts +7 -1
  18. package/dist/adapters/integration-kit/src/host.js +1 -0
  19. package/dist/adapters/integration-kit/src/index.d.ts +5 -2
  20. package/dist/adapters/integration-kit/src/index.js +2 -0
  21. package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
  22. package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
  23. package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
  24. package/dist/adapters/integration-kit/src/limited-http.js +53 -10
  25. package/dist/adapters/integration-kit/src/loader.d.ts +28 -11
  26. package/dist/adapters/integration-kit/src/loader.js +36 -21
  27. package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
  28. package/dist/adapters/integration-kit/src/own-request.js +1 -1
  29. package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
  30. package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
  31. package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
  32. package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
  33. package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
  34. package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
  35. package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
  36. package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
  37. package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
  38. package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
  39. package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
  40. package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
  41. package/dist/adapters/integration-kit/src/workspace-choices.d.ts +37 -0
  42. package/dist/adapters/integration-kit/src/workspace-choices.js +37 -0
  43. package/dist/core/events/src/blocked-sites.d.ts +57 -0
  44. package/dist/core/events/src/blocked-sites.js +89 -0
  45. package/dist/core/events/src/connector-listing-failed.d.ts +18 -0
  46. package/dist/core/events/src/connector-listing-failed.js +1 -0
  47. package/dist/core/events/src/home-path.d.ts +4 -1
  48. package/dist/core/events/src/home-path.js +14 -3
  49. package/dist/core/events/src/index.d.ts +8 -2
  50. package/dist/core/events/src/index.js +5 -0
  51. package/dist/core/events/src/provider-notes.d.ts +16 -0
  52. package/dist/core/events/src/provider-notes.js +24 -0
  53. package/dist/core/events/src/run-basics.d.ts +131 -10
  54. package/dist/core/events/src/run-event.d.ts +120 -11
  55. package/dist/core/events/src/run-reads.d.ts +25 -0
  56. package/dist/core/events/src/run-reads.js +44 -0
  57. package/dist/core/events/src/run-sends.d.ts +47 -0
  58. package/dist/core/events/src/run-sends.js +68 -0
  59. package/dist/core/events/src/run-summary.d.ts +11 -1
  60. package/dist/core/events/src/sandbox-opened-read.d.ts +2 -1
  61. package/dist/core/events/src/sandbox-opened-read.js +27 -2
  62. package/dist/core/events/src/sandbox-opened.d.ts +37 -4
  63. package/dist/core/events/src/sandbox-opened.js +13 -3
  64. package/dist/core/events/src/summarize.js +8 -1
  65. package/dist/core/events/src/timeline-entry.js +1 -0
  66. package/dist/core/events/src/tool-failure-cause.d.ts +25 -0
  67. package/dist/core/events/src/tool-failure-cause.js +66 -0
  68. package/dist/core/format/src/index.d.ts +9 -6
  69. package/dist/core/format/src/index.js +7 -3
  70. package/dist/core/format/src/integration/index.d.ts +3 -2
  71. package/dist/core/format/src/integration/index.js +2 -1
  72. package/dist/core/format/src/kinds/agent/harness-limits.d.ts +14 -0
  73. package/dist/core/format/src/kinds/agent/harness-limits.js +27 -0
  74. package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
  75. package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
  76. package/dist/core/format/src/kinds/agent/schema.d.ts +5 -0
  77. package/dist/core/format/src/kinds/agent/schema.js +5 -2
  78. package/dist/core/format/src/kinds/connector/is-local.js +1 -1
  79. package/dist/core/format/src/kinds/connector/kind.js +122 -2
  80. package/dist/core/format/src/kinds/connector/schema.d.ts +38 -0
  81. package/dist/core/format/src/kinds/connector/schema.js +71 -2
  82. package/dist/core/format/src/kinds/deployments/kind.js +25 -1
  83. package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
  84. package/dist/core/format/src/kinds/deployments/schema.js +52 -0
  85. package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
  86. package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
  87. package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
  88. package/dist/core/format/src/kinds/integration/host-key.js +8 -0
  89. package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
  90. package/dist/core/format/src/kinds/integration/kind.js +49 -12
  91. package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
  92. package/dist/core/format/src/kinds/integration/policy.js +61 -2
  93. package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
  94. package/dist/core/format/src/kinds/integration/schema.js +133 -7
  95. package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +14 -0
  96. package/dist/core/format/src/kinds/integration/sign-in-checks.js +150 -0
  97. package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
  98. package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
  99. package/dist/core/format/src/kinds/plugin/checks.d.ts +3 -2
  100. package/dist/core/format/src/kinds/plugin/checks.js +24 -6
  101. package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
  102. package/dist/core/format/src/kinds/plugin/schema.js +26 -6
  103. package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
  104. package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
  105. package/dist/core/format/src/kinds/provider/kind.js +40 -1
  106. package/dist/core/format/src/kinds/provider/types.d.ts +19 -0
  107. package/dist/core/format/src/kinds/provider/types.js +22 -0
  108. package/dist/core/format/src/kinds/routine/kind.js +38 -1
  109. package/dist/core/format/src/kinds/routine/schema.d.ts +20 -5
  110. package/dist/core/format/src/kinds/routine/schema.js +42 -14
  111. package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
  112. package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
  113. package/dist/core/format/src/kinds/workflow/schema.js +1 -1
  114. package/dist/core/format/src/kinds/workspace/kind.js +1 -1
  115. package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
  116. package/dist/core/format/src/kinds/workspace/schema.js +6 -5
  117. package/dist/core/format/src/parse-file.js +3 -1
  118. package/dist/core/format/src/shared/fields.d.ts +12 -8
  119. package/dist/core/format/src/shared/fields.js +48 -19
  120. package/dist/core/format/src/shared/guard.d.ts +8 -0
  121. package/dist/core/format/src/shared/guard.js +50 -1
  122. package/dist/core/format/src/shared/missing-keys.d.ts +17 -0
  123. package/dist/core/format/src/shared/missing-keys.js +110 -0
  124. package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
  125. package/dist/core/ports/src/agent-runs-not-available.js +25 -0
  126. package/dist/core/ports/src/blob-store.d.ts +12 -0
  127. package/dist/core/ports/src/blob-store.js +22 -0
  128. package/dist/core/ports/src/chat.d.ts +8 -0
  129. package/dist/core/ports/src/connector-client.d.ts +48 -4
  130. package/dist/core/ports/src/connector-client.js +17 -0
  131. package/dist/core/ports/src/connector-start-failure.d.ts +31 -0
  132. package/dist/core/ports/src/connector-start-failure.js +32 -0
  133. package/dist/core/ports/src/connector-tool-error.d.ts +33 -0
  134. package/dist/core/ports/src/connector-tool-error.js +40 -0
  135. package/dist/core/ports/src/definition-store-left-out.d.ts +46 -0
  136. package/dist/core/ports/src/definition-store-left-out.js +47 -0
  137. package/dist/core/ports/src/definition-store.d.ts +36 -5
  138. package/dist/core/ports/src/definition-store.js +1 -1
  139. package/dist/core/ports/src/deploy-target.d.ts +358 -0
  140. package/dist/core/ports/src/deploy-target.js +2 -0
  141. package/dist/core/ports/src/harness-bridge-server.d.ts +67 -7
  142. package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
  143. package/dist/core/ports/src/harness-sign-in-command.js +31 -0
  144. package/dist/core/ports/src/harness.d.ts +79 -15
  145. package/dist/core/ports/src/harness.js +1 -1
  146. package/dist/core/ports/src/host-settings-store.d.ts +116 -0
  147. package/dist/core/ports/src/host-settings-store.js +106 -0
  148. package/dist/core/ports/src/host-too-old.d.ts +23 -0
  149. package/dist/core/ports/src/host-too-old.js +27 -0
  150. package/dist/core/ports/src/index.d.ts +32 -13
  151. package/dist/core/ports/src/index.js +28 -8
  152. package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
  153. package/dist/core/ports/src/instruction-trust-store.js +43 -0
  154. package/dist/core/ports/src/integration.d.ts +71 -7
  155. package/dist/core/ports/src/integration.js +22 -4
  156. package/dist/core/ports/src/llm-provider.d.ts +39 -6
  157. package/dist/core/ports/src/llm-provider.js +46 -3
  158. package/dist/core/ports/src/pairing-codes.d.ts +111 -0
  159. package/dist/core/ports/src/pairing-codes.js +336 -0
  160. package/dist/core/ports/src/pairing-qr-bytes.d.ts +32 -0
  161. package/dist/core/ports/src/pairing-qr-bytes.js +116 -0
  162. package/dist/core/ports/src/password-hasher.d.ts +36 -20
  163. package/dist/core/ports/src/port-versions.d.ts +13 -6
  164. package/dist/core/ports/src/port-versions.js +14 -0
  165. package/dist/core/ports/src/program-probe.d.ts +25 -2
  166. package/dist/core/ports/src/program-probe.js +1 -1
  167. package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
  168. package/dist/core/ports/src/provider-default-base-url.js +18 -0
  169. package/dist/core/ports/src/record-store.d.ts +17 -0
  170. package/dist/core/ports/src/record-store.js +70 -0
  171. package/dist/core/ports/src/remote-control.d.ts +159 -0
  172. package/dist/core/ports/src/remote-control.js +26 -0
  173. package/dist/core/ports/src/remote-gateway-protocol.d.ts +220 -0
  174. package/dist/core/ports/src/remote-gateway-protocol.js +62 -0
  175. package/dist/core/ports/src/remote-gateway-session.d.ts +194 -0
  176. package/dist/core/ports/src/remote-gateway-session.js +241 -0
  177. package/dist/core/ports/src/run-executor.d.ts +44 -7
  178. package/dist/core/ports/src/run-executor.js +1 -1
  179. package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
  180. package/dist/core/ports/src/run-target-not-found.js +41 -0
  181. package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
  182. package/dist/core/ports/src/sandbox-setting-store.js +33 -0
  183. package/dist/core/ports/src/sandbox.d.ts +149 -13
  184. package/dist/core/ports/src/sandbox.js +52 -2
  185. package/dist/core/ports/src/secrets.d.ts +105 -1
  186. package/dist/core/ports/src/secrets.js +75 -2
  187. package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
  188. package/dist/core/ports/src/tool-approval-store.js +59 -0
  189. package/dist/core/ports/src/tunnel-provider.d.ts +117 -0
  190. package/dist/core/ports/src/tunnel-provider.js +65 -0
  191. package/dist/core/ports/src/tunnel-resolver.d.ts +83 -0
  192. package/dist/core/ports/src/tunnel-resolver.js +36 -0
  193. package/dist/core/ports/src/vcs-status.d.ts +8 -0
  194. package/dist/core/ports/src/vcs-status.js +90 -0
  195. package/dist/core/ports/src/vcs.d.ts +30 -0
  196. package/dist/core/schema-kit/src/builders.d.ts +5 -0
  197. package/dist/core/schema-kit/src/builders.js +5 -0
  198. package/dist/core/std/src/amount.d.ts +13 -0
  199. package/dist/core/std/src/amount.js +22 -0
  200. package/dist/core/std/src/hidden-characters.d.ts +28 -0
  201. package/dist/core/std/src/hidden-characters.js +72 -0
  202. package/dist/core/std/src/index.d.ts +7 -0
  203. package/dist/core/std/src/index.js +7 -0
  204. package/dist/core/std/src/path-overlap.d.ts +22 -0
  205. package/dist/core/std/src/path-overlap.js +57 -0
  206. package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
  207. package/dist/core/std/src/public-suffixes.generated.js +10428 -0
  208. package/dist/core/std/src/registrable-domain.d.ts +10 -0
  209. package/dist/core/std/src/registrable-domain.js +62 -0
  210. package/dist/core/std/src/secret-variable-name.d.ts +12 -0
  211. package/dist/core/std/src/secret-variable-name.js +34 -0
  212. package/dist/core/std/src/site-name.d.ts +35 -0
  213. package/dist/core/std/src/site-name.js +111 -0
  214. package/dist/core/std/src/url-host.d.ts +35 -0
  215. package/dist/core/std/src/url-host.js +170 -0
  216. package/dist/core/std/src/word-diff.d.ts +28 -0
  217. package/dist/core/std/src/word-diff.js +77 -0
  218. package/dist/core/yaml/src/edit-data.d.ts +7 -0
  219. package/dist/core/yaml/src/edit-data.js +62 -3
  220. package/dist/core/yaml/src/edit.d.ts +7 -0
  221. package/dist/core/yaml/src/edit.js +34 -3
  222. package/dist/core/yaml/src/splice-node.d.ts +6 -0
  223. package/dist/core/yaml/src/splice-node.js +10 -0
  224. package/dist/core/yaml/src/splice.js +20 -10
  225. package/dist/testing/integration/src/checks.d.ts +5 -3
  226. package/dist/testing/integration/src/checks.js +31 -3
  227. package/dist/testing/integration/src/load.js +3 -1
  228. package/package.json +9 -2
@@ -1,73 +1,31 @@
1
1
  import { IntegrationRefusedError } from "@halonicjs/adapters/integration-kit";
2
2
  import { isLoopback } from "./local-address.js";
3
3
  import { hostIn, } from "./policy.js";
4
- /**
5
- * Copies the placements the integration's code gave into frozen objects of
6
- * text fields, reading each field once. The kit checks and places only
7
- * these copies: the code keeps its own objects and could change them, or
8
- * give a getter, after the check and before the secret is placed. A field
9
- * that isn't text is refused here, because an object could turn into other
10
- * text each time it is read.
11
- */
12
- export function copyPlacements(policy, placements) {
13
- return Object.freeze(placements.map((placement) => copyPlacement(policy, placement)));
14
- }
15
- function copyPlacement(policy, placement) {
16
- if (typeof placement !== "object" || placement === null) {
17
- throw notText(policy, "a secret placement");
4
+ /** Checks that the secret is declared, isn't a client secret, and is placed as the sheet says. */
5
+ export function checkSecretPlacement(policy, placement) {
6
+ const declared = declaredSecret(policy, placement.secret);
7
+ if (declared === undefined) {
8
+ if (isClientSecret(policy, placement.secret)) {
9
+ throw new IntegrationRefusedError("secret-sign-in-only", `${policy.name}: the secret ${quoted(placement.secret)} is the client secret of a sign-in, which only Halonic's sign-in code sends`);
10
+ }
11
+ throw new IntegrationRefusedError("secret-not-declared", `${policy.name}: the secret ${quoted(placement.secret)} is not in its permission sheet`);
18
12
  }
19
- const given = placement;
20
- const secret = given.secret;
21
- const as = given.as;
22
- if (typeof secret !== "string")
23
- throw notText(policy, "the secret's name");
24
- if (typeof as !== "string")
25
- throw notText(policy, `the "as" of the secret ${quoted(secret)}`);
26
- const copy = { secret, as };
27
- for (const key of ["header", "prefix", "field"]) {
28
- const value = given[key];
29
- if (value === undefined)
30
- continue;
31
- if (typeof value !== "string")
32
- throw notText(policy, `the "${key}" of the secret ${quoted(secret)}`);
33
- copy[key] = value;
13
+ if (!matchesSheet(placement, declared.placement)) {
14
+ throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the secret ${quoted(placement.secret)} goes ${describe(declared.placement)} in its permission sheet, not ${describe(placement)}`);
34
15
  }
35
- // Whether the fields fit together is for `checkPlacements` to say.
36
- return Object.freeze(copy);
37
16
  }
38
- function notText(policy, what) {
39
- return new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: ${what} in a secret placement is not text`);
17
+ /** Whether a sign-in names the secret as its client secret, by own key only. */
18
+ function isClientSecret(policy, secret) {
19
+ return Object.values(policy.signIns).some((signIn) => signIn.clientSecret?.name === secret);
40
20
  }
41
- /**
42
- * Checks every placement against the sheet before a secret value is read.
43
- * Give it the copies from {@link copyPlacements}, never the code's own
44
- * objects. Where the secret may go is for {@link checkPlacementHosts}.
45
- */
46
- export function checkPlacements(policy, placements) {
47
- for (const placement of placements) {
48
- const declared = declaredSecret(policy, placement.secret);
49
- if (declared === undefined) {
50
- throw new IntegrationRefusedError("secret-not-declared", `${policy.name}: the secret ${quoted(placement.secret)} is not in its permission sheet`);
51
- }
52
- if (!matchesSheet(placement, declared.placement)) {
53
- throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the secret ${quoted(placement.secret)} goes ${describe(declared.placement)} in its permission sheet, not ${describe(placement)}`);
54
- }
21
+ /** Checks that the secret may go to this host, over this scheme. */
22
+ export function checkSecretHost(policy, placement, url) {
23
+ const declared = declaredSecret(policy, placement.secret);
24
+ if (declared === undefined || !hostIn(declared.hosts, url)) {
25
+ throw new IntegrationRefusedError("secret-not-allowed-for-host", `${policy.name}: the secret ${quoted(placement.secret)} may not go to ${url.host}`);
55
26
  }
56
- }
57
- /**
58
- * Checks that every placed secret may go to the request's first host, once
59
- * the URL is parsed. {@link checkPlacements} has already run, so every
60
- * secret is declared.
61
- */
62
- export function checkPlacementHosts(policy, placements, url) {
63
- for (const placement of placements) {
64
- const declared = declaredSecret(policy, placement.secret);
65
- if (declared === undefined || !hostIn(declared.hosts, url)) {
66
- throw new IntegrationRefusedError("secret-not-allowed-for-host", `${policy.name}: the secret ${quoted(placement.secret)} may not go to ${url.host}`);
67
- }
68
- if (!mayCarrySecret(url)) {
69
- throw new IntegrationRefusedError("secret-over-plain-http", `${policy.name}: the secret ${quoted(placement.secret)} may not go over plain http to another machine (${url.host})`);
70
- }
27
+ if (!mayCarrySecret(url)) {
28
+ throw new IntegrationRefusedError("secret-over-plain-http", `${policy.name}: the secret ${quoted(placement.secret)} may not go over plain http to another machine (${url.host})`);
71
29
  }
72
30
  }
73
31
  /** The sheet's entry for a secret, looked up by own key only, so `toString` is never a secret. */
@@ -82,7 +40,7 @@ function declaredSecret(policy, secret) {
82
40
  * form field names are case-sensitive. The placement comes from the
83
41
  * integration's code, so nothing in it is trusted to have the right type.
84
42
  */
85
- function matchesSheet(placement, sheet) {
43
+ export function matchesSheet(placement, sheet) {
86
44
  switch (sheet.as) {
87
45
  case "bearer":
88
46
  return placement.as === "bearer";
@@ -103,7 +61,7 @@ function asciiLowerCase(text) {
103
61
  return text.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
104
62
  }
105
63
  /** A placement in words, for a refusal message. It never holds a value. */
106
- function describe(placement) {
64
+ export function describe(placement) {
107
65
  switch (placement.as) {
108
66
  case "bearer":
109
67
  return "as a bearer token";
@@ -124,7 +82,7 @@ function describe(placement) {
124
82
  * the message. It is cut by characters before quoting, so the cut never
125
83
  * falls inside an escape or a surrogate pair.
126
84
  */
127
- function quoted(text) {
85
+ export function quoted(text) {
128
86
  const characters = Array.from(String(text));
129
87
  const shown = characters.length <= 50 ? characters.join("") : `${characters.slice(0, 50).join("")}…`;
130
88
  return JSON.stringify(shown);
@@ -139,7 +97,7 @@ function mayCarrySecret(url) {
139
97
  */
140
98
  export async function readSecrets(policy, secrets, placements) {
141
99
  const values = new Map();
142
- for (const { secret } of placements) {
100
+ for (const { secret } of placements.filter((placement) => "secret" in placement)) {
143
101
  if (values.has(secret))
144
102
  continue;
145
103
  const value = await secrets.get(secret);
@@ -149,42 +107,54 @@ export async function readSecrets(policy, secrets, placements) {
149
107
  continue;
150
108
  throw new IntegrationRefusedError("secret-not-configured", `${policy.name}: the secret ${quoted(secret)} is not configured`);
151
109
  }
110
+ checkSecretLength(policy.name, secret, value);
152
111
  values.set(secret, value);
153
112
  }
154
113
  return values;
155
114
  }
156
115
  /**
157
- * Puts the secrets allowed for this hop's host into the headers and the form
158
- * body. A placement whose secret may not go to this hop is left out, which
159
- * happens after a redirect to another declared host.
116
+ * The fewest UTF-8 bytes a secret value may have. The filter replaces every
117
+ * copy of a value in what the integration sees, so a shorter one would
118
+ * remove ordinary text, and where it was removed would give the value away.
119
+ */
120
+ export const minSecretBytes = 8;
121
+ /**
122
+ * @throws {IntegrationRefusedError} `secret-too-short` when `value` has fewer than {@link minSecretBytes} bytes
123
+ */
124
+ export function checkSecretLength(integration, secret, value) {
125
+ if (new TextEncoder().encode(value).length >= minSecretBytes)
126
+ return;
127
+ throw new IntegrationRefusedError("secret-too-short", `${integration}: the secret ${quoted(secret)} is shorter than ${minSecretBytes} bytes, too short to keep out of what the integration sees`);
128
+ }
129
+ /**
130
+ * Puts one secret into the headers or the form body, when it may go to this
131
+ * hop's host. A secret that may not go to this hop is left out, which happens
132
+ * after a redirect to another declared host.
160
133
  *
161
- * @returns the form body to send, when a placement wrote into it
134
+ * @returns the form body to send, which a `form` placement writes into
162
135
  */
163
- export function place(policy, placements, values, url, headers, form) {
164
- let body = form;
165
- for (const placement of placements) {
166
- const declared = policy.secrets[placement.secret];
167
- const value = values.get(placement.secret);
168
- if (declared === undefined ||
169
- value === undefined ||
170
- !hostIn(declared.hosts, url) ||
171
- !mayCarrySecret(url))
172
- continue;
173
- switch (placement.as) {
174
- case "bearer":
175
- headers.set("authorization", `Bearer ${value}`);
176
- break;
177
- case "header":
178
- headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
179
- break;
180
- case "form":
181
- if (body === undefined) {
182
- throw new IntegrationRefusedError("secret-needs-form-body", `${policy.name}: the secret ${quoted(placement.secret)} goes in a form field, so the body must be a form`);
183
- }
184
- body = new URLSearchParams(body);
185
- body.set(placement.field, value);
186
- break;
136
+ export function placeSecret(policy, placement, value, url, headers, form) {
137
+ const declared = policy.secrets[placement.secret];
138
+ if (declared === undefined ||
139
+ value === undefined ||
140
+ !hostIn(declared.hosts, url) ||
141
+ !mayCarrySecret(url)) {
142
+ return form;
143
+ }
144
+ switch (placement.as) {
145
+ case "bearer":
146
+ headers.set("authorization", `Bearer ${value}`);
147
+ return form;
148
+ case "header":
149
+ headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
150
+ return form;
151
+ case "form": {
152
+ if (form === undefined) {
153
+ throw new IntegrationRefusedError("secret-needs-form-body", `${policy.name}: the secret ${quoted(placement.secret)} goes in a form field, so the body must be a form`);
154
+ }
155
+ const written = new URLSearchParams(form);
156
+ written.set(placement.field, value);
157
+ return written;
187
158
  }
188
159
  }
189
- return body;
190
160
  }
@@ -0,0 +1,29 @@
1
+ import { type SignInPlacement, type SignInTokens } from "../../../core/ports/src/index.js";
2
+ import { type IntegrationPolicy } from "./policy.js";
3
+ /**
4
+ * Checks that the sign-in is declared and that the code places its token the
5
+ * way the sheet's `access` block says, with the same check as a secret's
6
+ * placement (`placement-not-in-sheet`). Runs before any token is asked for.
7
+ */
8
+ export declare function checkSignInPlacement(policy: IntegrationPolicy, placement: SignInPlacement): void;
9
+ /**
10
+ * Checks that the token may go to the request's first host. Unlike a
11
+ * secret, a token never goes over plain `http`, not even to this machine.
12
+ */
13
+ export declare function checkSignInPlacementHost(policy: IntegrationPolicy, placement: SignInPlacement, url: URL): void;
14
+ /**
15
+ * The access tokens this hop will carry, one per sign-in whose token may go
16
+ * to the hop's host. A sign-in that may not go there is not asked for, so a
17
+ * redirect to another declared host neither refreshes nor places its token.
18
+ *
19
+ * @throws {IntegrationRefusedError} `sign-in-needed` when the user has to sign in (again), or when no sign-ins are set up at all
20
+ */
21
+ export declare function readTokens(policy: IntegrationPolicy, tokens: SignInTokens | undefined, placements: readonly SignInPlacement[], url: URL, signal: AbortSignal): Promise<Map<string, string>>;
22
+ /** Puts the token into the headers, when this hop was asked for it. */
23
+ export declare function placeToken(placement: SignInPlacement, value: string | undefined, headers: Headers): void;
24
+ /**
25
+ * Tells the sign-in the server answered 401 to the tokens this hop placed.
26
+ * It counts a value only while it is the sign-in's current token and decides
27
+ * about refreshing; the kit never resends the request.
28
+ */
29
+ export declare function reportRejected(tokens: SignInTokens | undefined, placed: ReadonlyMap<string, string>): void;
@@ -0,0 +1,92 @@
1
+ import { SignInNeededError } from "../../../core/ports/src/index.js";
2
+ import { IntegrationRefusedError } from "@halonicjs/adapters/integration-kit";
3
+ import { hostIn } from "./policy.js";
4
+ import { checkSecretLength, describe, matchesSheet, quoted } from "./secret-placement.js";
5
+ /** The sheet's entry for a sign-in, looked up by own key only, so `toString` is never a sign-in. */
6
+ function declaredSignIn(policy, signIn) {
7
+ return Object.hasOwn(policy.signIns, signIn) ? policy.signIns[signIn] : undefined;
8
+ }
9
+ /**
10
+ * Checks that the sign-in is declared and that the code places its token the
11
+ * way the sheet's `access` block says, with the same check as a secret's
12
+ * placement (`placement-not-in-sheet`). Runs before any token is asked for.
13
+ */
14
+ export function checkSignInPlacement(policy, placement) {
15
+ const declared = declaredSignIn(policy, placement.signIn);
16
+ if (declared === undefined) {
17
+ throw new IntegrationRefusedError("sign-in-not-declared", `${policy.name}: the sign-in ${quoted(placement.signIn)} is not in its permission sheet`);
18
+ }
19
+ if (!matchesSheet(placement, declared)) {
20
+ throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} goes ${describe(declared)} in its permission sheet, not ${describe(placement)}`);
21
+ }
22
+ }
23
+ /**
24
+ * Checks that the token may go to the request's first host. Unlike a
25
+ * secret, a token never goes over plain `http`, not even to this machine.
26
+ */
27
+ export function checkSignInPlacementHost(policy, placement, url) {
28
+ const declared = declaredSignIn(policy, placement.signIn);
29
+ if (declared === undefined || !hostIn(declared.hosts, url)) {
30
+ throw new IntegrationRefusedError("token-not-allowed-for-host", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} may not go to ${url.host}`);
31
+ }
32
+ if (!mayCarryToken(url)) {
33
+ throw new IntegrationRefusedError("token-over-plain-http", `${policy.name}: the token of the sign-in ${quoted(placement.signIn)} may not go over plain http (${url.host})`);
34
+ }
35
+ }
36
+ /** A token goes over `https` only. */
37
+ function mayCarryToken(url) {
38
+ return url.protocol === "https:";
39
+ }
40
+ /** Whether this hop may carry the token of the sign-in. */
41
+ function mayGoTo(policy, signIn, url) {
42
+ const declared = declaredSignIn(policy, signIn);
43
+ return declared !== undefined && hostIn(declared.hosts, url) && mayCarryToken(url);
44
+ }
45
+ /**
46
+ * The access tokens this hop will carry, one per sign-in whose token may go
47
+ * to the hop's host. A sign-in that may not go there is not asked for, so a
48
+ * redirect to another declared host neither refreshes nor places its token.
49
+ *
50
+ * @throws {IntegrationRefusedError} `sign-in-needed` when the user has to sign in (again), or when no sign-ins are set up at all
51
+ */
52
+ export async function readTokens(policy, tokens, placements, url, signal) {
53
+ const values = new Map();
54
+ for (const { signIn } of placements) {
55
+ if (values.has(signIn) || !mayGoTo(policy, signIn, url))
56
+ continue;
57
+ const value = await accessToken(policy, tokens, signIn, signal);
58
+ checkSecretLength(policy.name, `sign-in ${signIn}`, value);
59
+ values.set(signIn, value);
60
+ }
61
+ return values;
62
+ }
63
+ async function accessToken(policy, tokens, signIn, signal) {
64
+ try {
65
+ if (tokens === undefined)
66
+ throw new SignInNeededError(signIn);
67
+ return await tokens.access(signIn, signal);
68
+ }
69
+ catch (error) {
70
+ if (!(error instanceof SignInNeededError))
71
+ throw error;
72
+ throw new IntegrationRefusedError("sign-in-needed", `${policy.name}: the sign-in ${quoted(signIn)} needs you to sign in again`);
73
+ }
74
+ }
75
+ /** Puts the token into the headers, when this hop was asked for it. */
76
+ export function placeToken(placement, value, headers) {
77
+ if (value === undefined)
78
+ return;
79
+ if (placement.as === "bearer")
80
+ headers.set("authorization", `Bearer ${value}`);
81
+ else
82
+ headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
83
+ }
84
+ /**
85
+ * Tells the sign-in the server answered 401 to the tokens this hop placed.
86
+ * It counts a value only while it is the sign-in's current token and decides
87
+ * about refreshing; the kit never resends the request.
88
+ */
89
+ export function reportRejected(tokens, placed) {
90
+ for (const [signIn, value] of placed)
91
+ tokens?.rejected(signIn, value);
92
+ }
@@ -0,0 +1,37 @@
1
+ import { type Secrets } from "../../../core/ports/src/index.js";
2
+ /** What the workspace chose for one integration. */
3
+ export interface WorkspaceChoices {
4
+ /** Its settings, such as the URL a `{ fromSetting }` host reads. Never a secret value. */
5
+ readonly settings?: Readonly<Record<string, unknown>>;
6
+ /**
7
+ * For each secret in the sheet, the workspace secret the user picked for
8
+ * it. A secret left out isn't configured: an optional one is left off the
9
+ * request, any other is refused when a request needs it.
10
+ */
11
+ readonly secretNames?: Readonly<Record<string, string>>;
12
+ }
13
+ /**
14
+ * The settings the workspace chose. The loader and the host's sign-ins
15
+ * (`createIntegrationSignIns`'s `current()`) both read them here, from the
16
+ * same `WorkspaceChoices`, so they never hold two copies.
17
+ *
18
+ * @example
19
+ * choiceSettings({ settings: { url: "http://localhost:11434" } }); // { url: "http://localhost:11434" }
20
+ * choiceSettings(undefined); // {}
21
+ */
22
+ export declare function choiceSettings(choices: WorkspaceChoices | undefined): Readonly<Record<string, unknown>>;
23
+ /**
24
+ * The raw secrets seen through the user's picks: the kit asks by the sheet's
25
+ * name and reads the secret picked for it, or nothing. A workspace chose
26
+ * those names, so they can't reach Halonic's own secrets, tokens or a
27
+ * plugin's. A plugin's backend's names come from its manifest and can reach
28
+ * only that plugin's own secrets (`plugin`).
29
+ *
30
+ * The host's sign-ins read the client secret through this same view, so the
31
+ * sheet's name is translated to the workspace's in one place.
32
+ *
33
+ * @example
34
+ * const secrets = chosenSecrets(raw, { secretNames: { "client-secret": "gitlab-secret" } });
35
+ * await secrets.get("client-secret"); // the workspace's "gitlab-secret"
36
+ */
37
+ export declare function chosenSecrets(raw: Secrets, choices: WorkspaceChoices | undefined, plugin?: string): Secrets;
@@ -0,0 +1,37 @@
1
+ import { pluginSecrets, workspaceSecrets } from "../../../core/ports/src/index.js";
2
+ /**
3
+ * The settings the workspace chose. The loader and the host's sign-ins
4
+ * (`createIntegrationSignIns`'s `current()`) both read them here, from the
5
+ * same `WorkspaceChoices`, so they never hold two copies.
6
+ *
7
+ * @example
8
+ * choiceSettings({ settings: { url: "http://localhost:11434" } }); // { url: "http://localhost:11434" }
9
+ * choiceSettings(undefined); // {}
10
+ */
11
+ export function choiceSettings(choices) {
12
+ return choices?.settings ?? {};
13
+ }
14
+ /**
15
+ * The raw secrets seen through the user's picks: the kit asks by the sheet's
16
+ * name and reads the secret picked for it, or nothing. A workspace chose
17
+ * those names, so they can't reach Halonic's own secrets, tokens or a
18
+ * plugin's. A plugin's backend's names come from its manifest and can reach
19
+ * only that plugin's own secrets (`plugin`).
20
+ *
21
+ * The host's sign-ins read the client secret through this same view, so the
22
+ * sheet's name is translated to the workspace's in one place.
23
+ *
24
+ * @example
25
+ * const secrets = chosenSecrets(raw, { secretNames: { "client-secret": "gitlab-secret" } });
26
+ * await secrets.get("client-secret"); // the workspace's "gitlab-secret"
27
+ */
28
+ export function chosenSecrets(raw, choices, plugin) {
29
+ const picks = new Map(Object.entries(choices?.secretNames ?? {}));
30
+ const readable = plugin === undefined ? workspaceSecrets(raw) : pluginSecrets(raw, plugin);
31
+ return {
32
+ get: async (name) => {
33
+ const picked = picks.get(name);
34
+ return picked === undefined ? null : await readable.get(picked);
35
+ },
36
+ };
37
+ }
@@ -0,0 +1,57 @@
1
+ import type { RunEvent } from "./run-event.js";
2
+ /** One site a step's sandbox refused, with how many times it was tried. */
3
+ export interface BlockedSite {
4
+ /** The host, `A`–`Z` lowercased and without a trailing dot. Show it with `siteName` from std. */
5
+ readonly host: string;
6
+ /** The refused connections to it in the step, all ports together. */
7
+ readonly tries: number;
8
+ }
9
+ /** The blocked sites of one visit of a step. */
10
+ export interface StepBlockedSites {
11
+ readonly state: string;
12
+ readonly agent: string;
13
+ /** Set for a step in one branch of a parallel state. */
14
+ readonly branch?: string;
15
+ /** Set for a step in one item of a for-each state. */
16
+ readonly item?: number;
17
+ /** The `seq` of the step's first blocked site: where the run log view shows the line. */
18
+ readonly firstSeq: number;
19
+ /** The sites to show, most tries first, then A to Z, at most the `limit` asked for. */
20
+ readonly sites: readonly BlockedSite[];
21
+ /** How many more sites were blocked than `sites` shows. */
22
+ readonly moreSites: number;
23
+ /** The tries of those further sites together. */
24
+ readonly moreTries: number;
25
+ /**
26
+ * Refused accesses past the targets the log names (`others`), files and
27
+ * sites together, which the log doesn't tell apart. 0 when there are none.
28
+ */
29
+ readonly otherBlocks: number;
30
+ }
31
+ /** How many sites {@link blockedSites} lists per step. */
32
+ export interface BlockedSitesOptions {
33
+ /**
34
+ * 5 for a step's entry on the run page, 20 for the run log view
35
+ * (sandbox-screens 2.2). A whole number, 0 or more.
36
+ */
37
+ readonly limit: number;
38
+ }
39
+ /**
40
+ * Names each site a step's sandbox refused once, with its tries
41
+ * (requirement `events.blocked-sites`). A step is one visit of a state: a
42
+ * new `state.entered` starts a new one. A `sandbox.blocked` event's `count`
43
+ * is a running total, so the latest one for a site and port counts, and
44
+ * the ports of one site add up. Blocked files aren't sites and are left
45
+ * out; a step with no blocked site isn't listed.
46
+ *
47
+ * @param events - one run's log, in `seq` order
48
+ * @param options - how many sites to list per step
49
+ * @returns the steps with blocked sites, in the order of their first one
50
+ * @throws RangeError when `limit` isn't a whole number, 0 or more
51
+ *
52
+ * @example
53
+ * blockedSites(events, { limit: 5 });
54
+ * // => [{ state: "install", agent: "writer", firstSeq: 12, sites: [{ host: "registry.npmjs.org", tries: 12 }],
55
+ * // moreSites: 0, moreTries: 0, otherBlocks: 0 }]
56
+ */
57
+ export declare function blockedSites(events: readonly RunEvent[], options: BlockedSitesOptions): StepBlockedSites[];
@@ -0,0 +1,89 @@
1
+ // The sites each step's sandbox refused, worked out from a run's
2
+ // `sandbox.blocked` events, so the run page and the run log view can name
3
+ // each site once with its tries (HAL-1708), not once per retry. Data only:
4
+ // the words come from the message catalogue.
5
+ /**
6
+ * Names each site a step's sandbox refused once, with its tries
7
+ * (requirement `events.blocked-sites`). A step is one visit of a state: a
8
+ * new `state.entered` starts a new one. A `sandbox.blocked` event's `count`
9
+ * is a running total, so the latest one for a site and port counts, and
10
+ * the ports of one site add up. Blocked files aren't sites and are left
11
+ * out; a step with no blocked site isn't listed.
12
+ *
13
+ * @param events - one run's log, in `seq` order
14
+ * @param options - how many sites to list per step
15
+ * @returns the steps with blocked sites, in the order of their first one
16
+ * @throws RangeError when `limit` isn't a whole number, 0 or more
17
+ *
18
+ * @example
19
+ * blockedSites(events, { limit: 5 });
20
+ * // => [{ state: "install", agent: "writer", firstSeq: 12, sites: [{ host: "registry.npmjs.org", tries: 12 }],
21
+ * // moreSites: 0, moreTries: 0, otherBlocks: 0 }]
22
+ */
23
+ export function blockedSites(events, options) {
24
+ if (!Number.isInteger(options.limit) || options.limit < 0) {
25
+ throw new RangeError(`blockedSites: limit must be a whole number, 0 or more, not ${options.limit}`);
26
+ }
27
+ const visits = new Map();
28
+ const tallies = new Map();
29
+ const others = new Map();
30
+ for (const event of events) {
31
+ if (event.type === "state.entered")
32
+ visits.set(event.state, event.seq);
33
+ if (event.type !== "sandbox.blocked" || event.block.kind === "file")
34
+ continue;
35
+ const key = JSON.stringify([event.state, event.branch, event.item, visits.get(event.state)]);
36
+ if (event.block.kind === "others") {
37
+ others.set(key, event.count);
38
+ continue;
39
+ }
40
+ const tally = tallies.get(key) ?? newTally(event);
41
+ tallies.set(key, tally);
42
+ const host = plainHost(event.block.host);
43
+ tally.counts.set(JSON.stringify([host, event.block.port]), { host, count: event.count });
44
+ }
45
+ return [...tallies].map(([key, tally]) => summary(tally, others.get(key) ?? 0, options.limit));
46
+ }
47
+ function newTally(event) {
48
+ const step = {
49
+ state: event.state,
50
+ agent: event.agent,
51
+ ...(event.branch === undefined ? {} : { branch: event.branch }),
52
+ ...(event.item === undefined ? {} : { item: event.item }),
53
+ firstSeq: event.seq,
54
+ };
55
+ return { step, counts: new Map() };
56
+ }
57
+ /**
58
+ * One host however the sandbox wrote it: `API.Example.com.` is
59
+ * `api.example.com`. Only `A`–`Z` are lowercased, as `siteName` does:
60
+ * Unicode lowercasing turns the Kelvin sign (U+212A) into a plain `k`, so a
61
+ * look-alike would pass for the real name.
62
+ */
63
+ function plainHost(host) {
64
+ const lower = host.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
65
+ return lower.endsWith(".") ? lower.slice(0, -1) : lower;
66
+ }
67
+ function summary(tally, otherBlocks, limit) {
68
+ const triesByHost = new Map();
69
+ for (const { host, count } of tally.counts.values()) {
70
+ triesByHost.set(host, (triesByHost.get(host) ?? 0) + count);
71
+ }
72
+ const sites = [...triesByHost].map(([host, tries]) => ({ host, tries })).sort(mostTriesFirst);
73
+ const more = sites.slice(limit);
74
+ return {
75
+ ...tally.step,
76
+ sites: sites.slice(0, limit),
77
+ moreSites: more.length,
78
+ moreTries: more.reduce((total, site) => total + site.tries, 0),
79
+ otherBlocks,
80
+ };
81
+ }
82
+ /** Most tries first, then A to Z by code point, the same on every computer. */
83
+ function mostTriesFirst(first, second) {
84
+ if (first.tries !== second.tries)
85
+ return second.tries - first.tries;
86
+ if (first.host === second.host)
87
+ return 0;
88
+ return first.host < second.host ? -1 : 1;
89
+ }
@@ -0,0 +1,18 @@
1
+ import type { ToolFailureCause } from "./run-basics.js";
2
+ /**
3
+ * A connector the agent's file lists couldn't list its tools when a model
4
+ * turn started, so the turn offered none of them and went on without them
5
+ * (requirement `events.connector-listing-failed`). `connector`: its id in
6
+ * the workspace. `cause`: why, as on `tool.result`: `why` is
7
+ * `program-missing` or `secret-not-set` with the program or the secret's
8
+ * `name` when the engine knows, else `other` with no name. It never holds
9
+ * the error's own text.
10
+ */
11
+ export interface ConnectorListingFailedEvent {
12
+ type: "connector.listing-failed";
13
+ agent: string;
14
+ connector: string;
15
+ cause: Extract<ToolFailureCause, {
16
+ code: "connector-not-started";
17
+ }>;
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * `path` with the person's home folder written as `~`, or `path` as it is
3
3
  * when it is outside the home folder or the home folder is unknown. A folder
4
- * next to the home folder whose name starts the same stays as it is.
4
+ * next to the home folder whose name starts the same stays as it is. A
5
+ * Windows home folder (one that starts with a drive letter) is matched
6
+ * whatever the letter case, as Windows does, so a path that went through a
7
+ * canonical-case step is shortened too.
5
8
  *
6
9
  * @param path - an absolute path
7
10
  * @param home - the person's home folder, or `null` when it is unknown
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * `path` with the person's home folder written as `~`, or `path` as it is
3
3
  * when it is outside the home folder or the home folder is unknown. A folder
4
- * next to the home folder whose name starts the same stays as it is.
4
+ * next to the home folder whose name starts the same stays as it is. A
5
+ * Windows home folder (one that starts with a drive letter) is matched
6
+ * whatever the letter case, as Windows does, so a path that went through a
7
+ * canonical-case step is shortened too.
5
8
  *
6
9
  * @param path - an absolute path
7
10
  * @param home - the person's home folder, or `null` when it is unknown
@@ -16,11 +19,19 @@ export function shortenHome(path, home) {
16
19
  const root = home.replace(/[/\\]+$/, "");
17
20
  if (root === "")
18
21
  return path;
19
- if (path === root)
22
+ const foldCase = isWindowsPath(root)
23
+ ? (text) => text.toLowerCase()
24
+ : (text) => text;
25
+ if (foldCase(path) === foldCase(root))
20
26
  return "~";
21
27
  const separator = path.charAt(root.length);
22
- if (path.startsWith(root) && (separator === "/" || separator === "\\")) {
28
+ if (foldCase(path.slice(0, root.length)) === foldCase(root) &&
29
+ (separator === "/" || separator === "\\")) {
23
30
  return `~${path.slice(root.length)}`;
24
31
  }
25
32
  return path;
26
33
  }
34
+ /** Whether `path` starts with a drive letter, such as `C:\`, so it is on Windows, where names ignore case. */
35
+ function isWindowsPath(path) {
36
+ return /^[a-z]:[/\\]/i.test(path);
37
+ }