@halonicjs/testing 0.1.0 → 0.1.1

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 (153) 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/index.d.ts +1 -0
  6. package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
  7. package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
  8. package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
  9. package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
  10. package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
  11. package/dist/adapters/integration-kit/src/declaration.js +4 -3
  12. package/dist/adapters/integration-kit/src/host.d.ts +7 -1
  13. package/dist/adapters/integration-kit/src/host.js +1 -0
  14. package/dist/adapters/integration-kit/src/index.d.ts +3 -1
  15. package/dist/adapters/integration-kit/src/index.js +1 -0
  16. package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
  17. package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
  18. package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
  19. package/dist/adapters/integration-kit/src/limited-http.js +53 -10
  20. package/dist/adapters/integration-kit/src/loader.d.ts +29 -2
  21. package/dist/adapters/integration-kit/src/loader.js +44 -12
  22. package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
  23. package/dist/adapters/integration-kit/src/own-request.js +1 -1
  24. package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
  25. package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
  26. package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
  27. package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
  28. package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
  29. package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
  30. package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
  31. package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
  32. package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
  33. package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
  34. package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
  35. package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
  36. package/dist/core/events/src/blocked-sites.d.ts +57 -0
  37. package/dist/core/events/src/blocked-sites.js +89 -0
  38. package/dist/core/events/src/index.d.ts +5 -1
  39. package/dist/core/events/src/index.js +3 -0
  40. package/dist/core/events/src/run-basics.d.ts +30 -7
  41. package/dist/core/events/src/run-event.d.ts +49 -5
  42. package/dist/core/events/src/run-reads.d.ts +25 -0
  43. package/dist/core/events/src/run-reads.js +44 -0
  44. package/dist/core/events/src/run-sends.d.ts +47 -0
  45. package/dist/core/events/src/run-sends.js +68 -0
  46. package/dist/core/events/src/summarize.js +5 -1
  47. package/dist/core/events/src/timeline-entry.js +1 -0
  48. package/dist/core/format/src/index.d.ts +7 -5
  49. package/dist/core/format/src/index.js +5 -3
  50. package/dist/core/format/src/integration/index.d.ts +3 -2
  51. package/dist/core/format/src/integration/index.js +2 -1
  52. package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
  53. package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
  54. package/dist/core/format/src/kinds/agent/schema.js +2 -2
  55. package/dist/core/format/src/kinds/connector/is-local.js +1 -1
  56. package/dist/core/format/src/kinds/connector/kind.js +99 -0
  57. package/dist/core/format/src/kinds/connector/schema.d.ts +24 -0
  58. package/dist/core/format/src/kinds/connector/schema.js +53 -2
  59. package/dist/core/format/src/kinds/deployments/kind.js +25 -1
  60. package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
  61. package/dist/core/format/src/kinds/deployments/schema.js +52 -0
  62. package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
  63. package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
  64. package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
  65. package/dist/core/format/src/kinds/integration/host-key.js +8 -0
  66. package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
  67. package/dist/core/format/src/kinds/integration/kind.js +49 -12
  68. package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
  69. package/dist/core/format/src/kinds/integration/policy.js +61 -2
  70. package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
  71. package/dist/core/format/src/kinds/integration/schema.js +132 -6
  72. package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +13 -0
  73. package/dist/core/format/src/kinds/integration/sign-in-checks.js +120 -0
  74. package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
  75. package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
  76. package/dist/core/format/src/kinds/plugin/checks.js +7 -1
  77. package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
  78. package/dist/core/format/src/kinds/plugin/schema.js +2 -2
  79. package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
  80. package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
  81. package/dist/core/format/src/kinds/routine/kind.js +38 -1
  82. package/dist/core/format/src/kinds/routine/schema.d.ts +10 -0
  83. package/dist/core/format/src/kinds/routine/schema.js +23 -1
  84. package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
  85. package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
  86. package/dist/core/format/src/kinds/workflow/schema.js +1 -1
  87. package/dist/core/format/src/kinds/workspace/kind.js +1 -1
  88. package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
  89. package/dist/core/format/src/kinds/workspace/schema.js +1 -1
  90. package/dist/core/format/src/shared/fields.d.ts +3 -6
  91. package/dist/core/format/src/shared/fields.js +33 -13
  92. package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
  93. package/dist/core/ports/src/agent-runs-not-available.js +25 -0
  94. package/dist/core/ports/src/blob-store.d.ts +12 -0
  95. package/dist/core/ports/src/blob-store.js +22 -0
  96. package/dist/core/ports/src/connector-client.d.ts +48 -4
  97. package/dist/core/ports/src/connector-client.js +17 -0
  98. package/dist/core/ports/src/definition-store.d.ts +23 -2
  99. package/dist/core/ports/src/harness-bridge-server.d.ts +12 -3
  100. package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
  101. package/dist/core/ports/src/harness-sign-in-command.js +31 -0
  102. package/dist/core/ports/src/harness.d.ts +46 -13
  103. package/dist/core/ports/src/harness.js +1 -1
  104. package/dist/core/ports/src/host-settings-store.d.ts +116 -0
  105. package/dist/core/ports/src/host-settings-store.js +106 -0
  106. package/dist/core/ports/src/index.d.ts +18 -10
  107. package/dist/core/ports/src/index.js +15 -7
  108. package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
  109. package/dist/core/ports/src/instruction-trust-store.js +43 -0
  110. package/dist/core/ports/src/integration.d.ts +71 -7
  111. package/dist/core/ports/src/integration.js +22 -4
  112. package/dist/core/ports/src/llm-provider.d.ts +3 -1
  113. package/dist/core/ports/src/llm-provider.js +3 -0
  114. package/dist/core/ports/src/password-hasher.d.ts +36 -20
  115. package/dist/core/ports/src/port-versions.d.ts +7 -4
  116. package/dist/core/ports/src/port-versions.js +6 -0
  117. package/dist/core/ports/src/program-probe.d.ts +25 -2
  118. package/dist/core/ports/src/program-probe.js +1 -1
  119. package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
  120. package/dist/core/ports/src/provider-default-base-url.js +18 -0
  121. package/dist/core/ports/src/record-store.d.ts +17 -0
  122. package/dist/core/ports/src/record-store.js +70 -0
  123. package/dist/core/ports/src/run-executor.d.ts +5 -4
  124. package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
  125. package/dist/core/ports/src/run-target-not-found.js +41 -0
  126. package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
  127. package/dist/core/ports/src/sandbox-setting-store.js +33 -0
  128. package/dist/core/ports/src/sandbox.d.ts +130 -13
  129. package/dist/core/ports/src/sandbox.js +38 -2
  130. package/dist/core/ports/src/secrets.d.ts +73 -0
  131. package/dist/core/ports/src/secrets.js +56 -0
  132. package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
  133. package/dist/core/ports/src/tool-approval-store.js +59 -0
  134. package/dist/core/ports/src/vcs-status.d.ts +8 -0
  135. package/dist/core/ports/src/vcs-status.js +90 -0
  136. package/dist/core/std/src/amount.d.ts +13 -0
  137. package/dist/core/std/src/amount.js +22 -0
  138. package/dist/core/std/src/hidden-characters.d.ts +19 -0
  139. package/dist/core/std/src/hidden-characters.js +54 -0
  140. package/dist/core/std/src/index.d.ts +5 -0
  141. package/dist/core/std/src/index.js +5 -0
  142. package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
  143. package/dist/core/std/src/public-suffixes.generated.js +10429 -0
  144. package/dist/core/std/src/registrable-domain.d.ts +10 -0
  145. package/dist/core/std/src/registrable-domain.js +62 -0
  146. package/dist/core/std/src/secret-variable-name.d.ts +12 -0
  147. package/dist/core/std/src/secret-variable-name.js +34 -0
  148. package/dist/core/std/src/site-name.d.ts +35 -0
  149. package/dist/core/std/src/site-name.js +111 -0
  150. package/dist/core/std/src/url-host.d.ts +35 -0
  151. package/dist/core/std/src/url-host.js +170 -0
  152. package/dist/testing/integration/src/load.js +3 -1
  153. package/package.json +2 -2
@@ -10,6 +10,20 @@ export declare const connectorClientVersion = 1;
10
10
  * sending a mail. Approvals use it.
11
11
  */
12
12
  export type ToolRisk = "read" | "write" | "send";
13
+ /**
14
+ * Whether a text passes the name rule of `ports.untrusted-tool-listings`:
15
+ * 1 to 64 ASCII letters, digits, `_` or `-`. The model sends tool names
16
+ * back in calls, so they can't be fenced, and the rule keeps them too short
17
+ * and plain to carry much. A connector file's `tools.read` names pass the
18
+ * same rule, so a look-alike letter or an invisible character can't hide
19
+ * in one; widen both together or neither.
20
+ *
21
+ * @example
22
+ * passesNameRule("search_issues"); // true
23
+ * passesNameRule("search_\u0456ssues"); // false: a Cyrillic "і" in "issues"
24
+ * passesNameRule("notes.lookup"); // false
25
+ */
26
+ export declare function passesNameRule(text: unknown): text is string;
13
27
  /** A tool a connector offers. */
14
28
  export interface ConnectorTool {
15
29
  /** The tool's name, unique within its connector. */
@@ -49,13 +63,20 @@ export interface ConnectorCallOptions {
49
63
  export interface ConnectorClient {
50
64
  /** The connector's id in the workspace. */
51
65
  readonly id: string;
66
+ /**
67
+ * Whether the connector runs on this computer, as `connectorIsLocal` in
68
+ * `@halonicjs/format` says. In a local-only workspace the engine uses only
69
+ * the connectors that set it to `true`.
70
+ */
71
+ readonly local?: boolean;
52
72
  /**
53
73
  * The tools the connector offers now. Connects on the first call.
54
74
  *
55
75
  * For an `http` or `stdio` connector, a server the workspace chose wrote
56
76
  * these tools. The host rebuilds their parameters from an allow-list,
57
77
  * fences off their descriptions and results as untrusted text, treats
58
- * their `risk` as `write` unless the workspace sets a lower one, and
78
+ * their `risk` as `write`, or `send` when they say so, unless the
79
+ * workspace sets a lower one for a `write` tool, and
59
80
  * refuses tools that are too large or whose names it can't check
60
81
  * (requirement `ports.untrusted-tool-listings`).
61
82
  * Don't fence them here, and don't mark them trusted.
@@ -122,7 +143,19 @@ export type ConnectorTransport = {
122
143
  command: string;
123
144
  args: string[];
124
145
  env: Record<string, string>;
146
+ /**
147
+ * Variables the program takes from the app's own environment by name,
148
+ * on top of the base every program gets (`app.connector-base-env`).
149
+ * Never a secret's name: the format refuses one (`format.connector-inherit-env`).
150
+ */
151
+ inheritEnv?: string[];
125
152
  cwd?: string;
153
+ /**
154
+ * The only sites the program may reach when it starts in a sandbox,
155
+ * lowercase and in Punycode (`format.connector-sites`). Absent means
156
+ * none. A program that starts without a sandbox isn't held to them.
157
+ */
158
+ sites?: string[];
126
159
  /**
127
160
  * The version to start, for package-runner connectors. The app fills it
128
161
  * from the trust record, **never from workspace YAML**, so a workspace
@@ -154,9 +187,10 @@ export type ConnectorTransport = {
154
187
  * `CURL_CA_BUNDLE`; or a preload, `LD_PRELOAD`, `LD_LIBRARY_PATH`,
155
188
  * `DYLD_INSERT_LIBRARIES` or `DYLD_LIBRARY_PATH`. It drops the
156
189
  * package-manager and language variables it inherits from its own
157
- * process and sets the registry from `registryUrl` itself. It keeps an
158
- * inherited proxy, certificate or preload: those are the person's own
159
- * machine settings, such as a company proxy, not the workspace's.
190
+ * process and sets the registry from `registryUrl` itself. Of what it
191
+ * inherits it keeps only the base (`app.connector-base-env`), which
192
+ * holds a proxy, certificate or library folder: those are the person's
193
+ * own machine settings, such as a company proxy, not the workspace's.
160
194
  *
161
195
  * And it refuses to start when an argument is a runner option that
162
196
  * fetches from somewhere else: `--registry`, an npm scope's
@@ -175,6 +209,16 @@ export type ConnectorTransport = {
175
209
  kind: "google";
176
210
  account: string;
177
211
  client: string;
212
+ }
213
+ /** A local integration in the I5 runner: a folder under `integrations/`. */
214
+ | {
215
+ kind: "integration";
216
+ folder: string;
217
+ }
218
+ /** A plugin's backend in the I5 runner. */
219
+ | {
220
+ kind: "plugin";
221
+ id: string;
178
222
  };
179
223
  /** A connector as the workspace configures it. */
180
224
  export interface ConnectorConfig {
@@ -1,2 +1,19 @@
1
1
  /** Version of the {@link ConnectorClient} and {@link ConnectorFactory} ports. A breaking change bumps it and comes with an ADR. */
2
2
  export const connectorClientVersion = 1;
3
+ const toolNamePattern = /^[A-Za-z0-9_-]{1,64}$/;
4
+ /**
5
+ * Whether a text passes the name rule of `ports.untrusted-tool-listings`:
6
+ * 1 to 64 ASCII letters, digits, `_` or `-`. The model sends tool names
7
+ * back in calls, so they can't be fenced, and the rule keeps them too short
8
+ * and plain to carry much. A connector file's `tools.read` names pass the
9
+ * same rule, so a look-alike letter or an invisible character can't hide
10
+ * in one; widen both together or neither.
11
+ *
12
+ * @example
13
+ * passesNameRule("search_issues"); // true
14
+ * passesNameRule("search_\u0456ssues"); // false: a Cyrillic "і" in "issues"
15
+ * passesNameRule("notes.lookup"); // false
16
+ */
17
+ export function passesNameRule(text) {
18
+ return typeof text === "string" && toolNamePattern.test(text);
19
+ }
@@ -87,6 +87,22 @@ export type WriteResult = {
87
87
  readonly status: "changed-on-disk";
88
88
  readonly current: string | null;
89
89
  };
90
+ /** Options for {@link DefinitionStore.remove}. */
91
+ export interface RemoveOptions {
92
+ /** Remove only if the file still holds this. Leave it out to remove the file whatever it holds. */
93
+ readonly expected?: ExpectedContent;
94
+ }
95
+ /**
96
+ * What {@link DefinitionStore.remove} did. `changed-on-disk` means the file
97
+ * no longer held what the caller expected, so nothing was removed; `current`
98
+ * is its text now, or `null` when it is gone.
99
+ */
100
+ export type RemoveResult = {
101
+ readonly status: "removed";
102
+ } | {
103
+ readonly status: "changed-on-disk";
104
+ readonly current: string | null;
105
+ };
90
106
  /**
91
107
  * One workspace file that changed outside Halonic. `removed` means it is gone
92
108
  * at the end of the batch; a file removed and created again, as many editors
@@ -120,8 +136,13 @@ export interface DefinitionStore extends DefinitionSource {
120
136
  * read and, if not, writes nothing and returns `changed-on-disk`.
121
137
  */
122
138
  write(path: string, content: string, options?: WriteOptions): Promise<WriteResult>;
123
- /** Deletes a file. Deleting a file that isn't there is not an error. */
124
- remove(path: string): Promise<void>;
139
+ /**
140
+ * Deletes a file. Deleting a file that isn't there is not an error. With
141
+ * `expected`, it first checks the file still holds what the caller last
142
+ * read and, if not, removes nothing and returns `changed-on-disk`, so an
143
+ * edit made since is not deleted.
144
+ */
145
+ remove(path: string, options?: RemoveOptions): Promise<RemoveResult>;
125
146
  /**
126
147
  * Reports the files that change from outside Halonic (save model 6):
127
148
  * another editor, a sync tool, `git pull`. A change is one whose text
@@ -35,6 +35,11 @@ export type BridgeEvent = Extract<HarnessEvent, {
35
35
  }>;
36
36
  /** Writes the bridge's events to the run's log. The engine adds `runId`, `seq`, `at` and the `state`. */
37
37
  export interface BridgeRecorder {
38
+ /**
39
+ * @throws KindedError `limit_reached` for an `llm.request` once the run's
40
+ * budget is reached: the bridge then sends the model call nowhere and
41
+ * answers the program with 429 and the error's message
42
+ */
38
43
  record(event: BridgeEvent): Promise<void>;
39
44
  }
40
45
  /**
@@ -161,7 +166,8 @@ export interface OpenHarnessBridge extends HarnessBridge {
161
166
  close(): Promise<void>;
162
167
  }
163
168
  /**
164
- * Opens the Halonic bridge for one run: a server on loopback that passes the
169
+ * Opens the Halonic bridge for one run: a server on loopback, or on a Unix
170
+ * socket only this user can open, that passes the
165
171
  * program's model requests to the provider and serves Halonic's tools and
166
172
  * approvals over MCP (requirement `ports.harness-bridge-server`). The engine
167
173
  * opens it before the sandbox and closes it when the run ends. It is a port
@@ -181,9 +187,12 @@ export interface OpenHarnessBridge extends HarnessBridge {
181
187
  */
182
188
  export interface HarnessBridgeServer {
183
189
  /**
184
- * Starts serving one run on a free loopback port.
190
+ * Starts serving one run on a free loopback port, or on a new Unix socket
191
+ * of its own when the adapter is set up for sockets. A socket bridge gives
192
+ * its path as {@link HarnessBridge.socketPath}. One that can't create its
193
+ * socket fails: it never falls back to a loopback port.
185
194
  *
186
- * @throws when it can't listen on loopback
195
+ * @throws when it can't listen
187
196
  */
188
197
  open(options: HarnessBridgeOptions): Promise<OpenHarnessBridge>;
189
198
  }
@@ -0,0 +1,27 @@
1
+ import type { HarnessSignIn } from "./harness.js";
2
+ /**
3
+ * The terminal a person types a sign-in into: PowerShell, the default
4
+ * terminal on Windows, or `sh` and the shells that read it like `sh` elsewhere.
5
+ */
6
+ export type SignInShell = "powershell" | "sh";
7
+ /**
8
+ * A harness's sign-in for one agent, as a person types it into their own
9
+ * terminal (requirement `ports.harness-sign-in-command`). Studio shows it
10
+ * when it can't open a window, and `halonic doctor` shows it for each
11
+ * agent that isn't signed in, so both print the same command.
12
+ *
13
+ * It names the program as the person's `PATH` finds it and sets the
14
+ * agent's folder for this command only. The folder is single-quoted, so the
15
+ * shell takes it as it is: no variable, `~` or wildcard in it is expanded.
16
+ *
17
+ * @param signIn - the harness's own sign-in
18
+ * @param settingsFolder - the agent's settings folder, absolute
19
+ * @param shell - the terminal it is typed into
20
+ *
21
+ * @example
22
+ * signInCommandText(claudeCodeSignIn, "/data/agents/writer", "sh");
23
+ * // CLAUDE_CONFIG_DIR='/data/agents/writer' claude auth login
24
+ * signInCommandText(claudeCodeSignIn, agentFolder, "powershell");
25
+ * // $env:CLAUDE_CONFIG_DIR = '<agentFolder>'; claude auth login
26
+ */
27
+ export declare function signInCommandText(signIn: HarnessSignIn, settingsFolder: string, shell: SignInShell): string;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * A harness's sign-in for one agent, as a person types it into their own
3
+ * terminal (requirement `ports.harness-sign-in-command`). Studio shows it
4
+ * when it can't open a window, and `halonic doctor` shows it for each
5
+ * agent that isn't signed in, so both print the same command.
6
+ *
7
+ * It names the program as the person's `PATH` finds it and sets the
8
+ * agent's folder for this command only. The folder is single-quoted, so the
9
+ * shell takes it as it is: no variable, `~` or wildcard in it is expanded.
10
+ *
11
+ * @param signIn - the harness's own sign-in
12
+ * @param settingsFolder - the agent's settings folder, absolute
13
+ * @param shell - the terminal it is typed into
14
+ *
15
+ * @example
16
+ * signInCommandText(claudeCodeSignIn, "/data/agents/writer", "sh");
17
+ * // CLAUDE_CONFIG_DIR='/data/agents/writer' claude auth login
18
+ * signInCommandText(claudeCodeSignIn, agentFolder, "powershell");
19
+ * // $env:CLAUDE_CONFIG_DIR = '<agentFolder>'; claude auth login
20
+ */
21
+ export function signInCommandText(signIn, settingsFolder, shell) {
22
+ const words = [signIn.program, ...signIn.args].join(" ");
23
+ if (shell === "powershell") {
24
+ // PowerShell keeps a single-quoted string as it is, apart from `''` for one `'`.
25
+ const folder = `'${settingsFolder.replaceAll("'", "''")}'`;
26
+ return `$env:${signIn.folderVariable} = ${folder}; ${words}`;
27
+ }
28
+ // `sh` keeps a single-quoted string as it is; a `'` closes it, so it is written `'\''`.
29
+ const folder = `'${settingsFolder.replaceAll("'", "'\\''")}'`;
30
+ return `${signIn.folderVariable}=${folder} ${words}`;
31
+ }
@@ -1,7 +1,7 @@
1
1
  import type { AgentResume, RunEventBody, RunEventType } from "../../events/src/index.js";
2
2
  import type { SandboxProfile, SandboxSession } from "./sandbox.js";
3
3
  /** Version of the {@link Harness} port. A breaking change bumps it and comes with an ADR. */
4
- export declare const harnessVersion = 2;
4
+ export declare const harnessVersion = 3;
5
5
  /**
6
6
  * Whether a harness can run on this machine, as `detect()` finds it. Studio
7
7
  * turns it into the sentence next to the harness choice, so each case
@@ -17,14 +17,20 @@ export declare const harnessVersion = 2;
17
17
  * if (found.status === "signed-out") show(`Found ${codex.name} ${found.version} · not signed in`);
18
18
  */
19
19
  export type HarnessDetection =
20
- /** Installed in a version Halonic supports, and signed in, or its sign-in can't be checked. */
20
+ /**
21
+ * Installed in a version Halonic supports, and signed in, or its sign-in
22
+ * can't be checked. Asked about an agent's settings folder, "signed in"
23
+ * means that agent is; otherwise it means the person is, in their own
24
+ * settings, which a run never uses.
25
+ */
21
26
  {
22
27
  status: "ready";
23
28
  version: string;
24
29
  }
25
30
  /**
26
31
  * Installed in a version Halonic supports, but the program says no one is
27
- * signed in on this computer. It can still run when Halonic holds a key
32
+ * signed in: for the agent whose settings folder `detect()` was given, or
33
+ * else on this computer. It can still run when Halonic holds a key
28
34
  * for the agent's model provider, so this isn't an error on its own.
29
35
  */
30
36
  | {
@@ -50,6 +56,16 @@ export type HarnessDetection =
50
56
  status: "failed";
51
57
  error: string;
52
58
  };
59
+ /** What `detect()` may be asked about. */
60
+ export interface HarnessDetectOptions {
61
+ /**
62
+ * The agent's own settings folder, the one its runs get as
63
+ * `settingsFolder`. With it, the sign-in answer is about that agent, which
64
+ * is what a run needs. Without it, `detect()` checks the program and the
65
+ * person's own sign-in, as `halonic doctor` does outside an agent.
66
+ */
67
+ settingsFolder?: string;
68
+ }
53
69
  /** What a harness run is asked to do: one agent of one run, with the agent's own settings. */
54
70
  export interface HarnessTask {
55
71
  /** The run this agent belongs to. Every event the harness reports is written to its log. */
@@ -72,19 +88,29 @@ export interface HarnessTask {
72
88
  */
73
89
  export declare const harnessBridgeTokenHeader = "x-halonic-run-token";
74
90
  /**
75
- * The Halonic bridge of one run: a loopback address in Halonic that serves
76
- * the model gateway and Halonic's tools over MCP. The harness points its
77
- * program at `url` and has it send `token` in the
78
- * {@link harnessBridgeTokenHeader} header on every request.
91
+ * The Halonic bridge of one run: an address in Halonic that serves the model
92
+ * gateway and Halonic's tools over MCP. The harness points its program at
93
+ * `url` and has it send `token` in the {@link harnessBridgeTokenHeader}
94
+ * header on every request.
79
95
  *
80
96
  * The token is a credential for this run only. A harness never writes it into
81
97
  * an event, a log line or an error message.
82
98
  */
83
99
  export interface HarnessBridge {
84
- /** The bridge's address on loopback, such as "http://127.0.0.1:41817". */
100
+ /**
101
+ * The address the program calls: on loopback, such as
102
+ * "http://127.0.0.1:41817", or, when the bridge serves a Unix socket, the
103
+ * name the sandbox's proxy sends to it, such as
104
+ * "http://bridge.halonic.invalid".
105
+ */
85
106
  url: string;
86
107
  /** The run's random token. */
87
108
  token: string;
109
+ /**
110
+ * The absolute path of the Unix socket the bridge serves, when it serves
111
+ * one instead of a loopback port. Absent for a loopback bridge.
112
+ */
113
+ socketPath?: string;
88
114
  }
89
115
  /** The body of the run log event with this `type`. */
90
116
  type RunEventOf<Type extends RunEventType> = Extract<RunEventBody, {
@@ -151,7 +177,11 @@ export interface HarnessRun extends AsyncIterable<HarnessEvent> {
151
177
  * @throws when the run has ended, or no `approval.requested` with that `callId` is waiting
152
178
  */
153
179
  send(input: HarnessInput): Promise<void>;
154
- /** Stops the run. Resolves once every program it started has stopped; does nothing on a run that has ended. */
180
+ /**
181
+ * Stops the run. Resolves once every program it started has stopped; does nothing on a run that has ended.
182
+ *
183
+ * @throws when a program it started can't be stopped, so it may still be running; the error never contains the bridge token
184
+ */
155
185
  cancel(): Promise<void>;
156
186
  }
157
187
  /**
@@ -167,7 +197,8 @@ export interface HarnessRun extends AsyncIterable<HarnessEvent> {
167
197
  * `start` throws, so the same loop runs there without stdio connectors.
168
198
  *
169
199
  * @example
170
- * if ((await harness.detect()).status !== "ready") return;
200
+ * const found = await harness.detect({ settingsFolder: agentSettingsFolder });
201
+ * if (found.status !== "ready") return;
171
202
  * const extra = harness.environment?.(bridge, profile) ?? {};
172
203
  * const session = await sandbox.open({ ...profile, env: addHarnessEnvironment(profile.env, extra) });
173
204
  * const run = harness.start(task, session, bridge);
@@ -183,10 +214,12 @@ export interface Harness {
183
214
  readonly name: string;
184
215
  /**
185
216
  * Finds the harness's program on this machine, checks its version and,
186
- * where the program can tell, whether someone is signed in to it. Never
187
- * throws: a problem is a {@link HarnessDetection}.
217
+ * where the program can tell, whether someone is signed in to it. Given
218
+ * an agent's settings folder, it asks the program about that folder's
219
+ * sign-in (requirement `ports.harness-detect-folder`). Never throws: a
220
+ * problem is a {@link HarnessDetection}.
188
221
  */
189
- detect(): Promise<HarnessDetection>;
222
+ detect(options?: HarnessDetectOptions): Promise<HarnessDetection>;
190
223
  /**
191
224
  * The environment variables the harness's program needs, such as the
192
225
  * bridge's address, the token header and the program's own settings and
@@ -1,5 +1,5 @@
1
1
  /** Version of the {@link Harness} port. A breaking change bumps it and comes with an ADR. */
2
- export const harnessVersion = 2;
2
+ export const harnessVersion = 3;
3
3
  /**
4
4
  * The request header that carries the run's bridge token. The token has a
5
5
  * header of its own because `Authorization` isn't free: with a subscription,
@@ -0,0 +1,116 @@
1
+ /** Version of the {@link HostSettingsStore} port. A breaking change bumps it and comes with an ADR. */
2
+ export declare const hostSettingsStoreVersion = 1;
3
+ /** A saved setting's value: anything `JSON.parse` can give back. */
4
+ export type HostSettingValue = null | boolean | number | string | readonly HostSettingValue[] | {
5
+ readonly [name: string]: HostSettingValue;
6
+ };
7
+ /**
8
+ * Every saved setting, by key.
9
+ *
10
+ * @example
11
+ * const values: HostSettingValues = { "assistant.turn-limit": 16, "assistant.connector-tools": true };
12
+ */
13
+ export type HostSettingValues = Readonly<Record<string, HostSettingValue>>;
14
+ /**
15
+ * The most bytes every saved value may take together, as compact JSON of
16
+ * the values by key (`JSON.stringify(values)` in UTF-8), so long
17
+ * instructions fit and a runaway value doesn't.
18
+ */
19
+ export declare const hostSettingsMaxBytes: number;
20
+ /**
21
+ * Whether `key` can name a host setting: at most 64 characters, the section
22
+ * first, such as `assistant.turn-limit` or `sandbox.new-agents`. Adapters
23
+ * refuse any other key, so no key can be `__proto__` or hold a path.
24
+ *
25
+ * @example
26
+ * isHostSettingKey("assistant.turn-limit"); // true
27
+ * isHostSettingKey("turn-limit"); // false: no section
28
+ */
29
+ export declare function isHostSettingKey(key: string): boolean;
30
+ /**
31
+ * The settings kept where Halonic runs, for every workspace on that
32
+ * computer: the assistant and the sandbox default (`host`-scoped settings
33
+ * in Studio). They live in the user's profile, `settings/host.json` for the
34
+ * CLI, `serve` and the desktop app, memory for tests and the browser demo,
35
+ * and **never in a workspace file**. A provider key is never one of them,
36
+ * only its secret name.
37
+ *
38
+ * The port only stores JSON values by key. `HostSettings` in `core/app`
39
+ * gives each setting its schema and default, so a value that doesn't fit
40
+ * reads as the default. The saved settings are `{ "version": 1, "values":
41
+ * {…} }`; the contract suite's golden cases
42
+ * (`packages/testing/contracts/src/golden/host-settings-limits.json`) list
43
+ * the keys, sizes and saved shapes every adapter, the desktop shell's Rust
44
+ * side too, agrees on.
45
+ *
46
+ * - Saved settings that can't be parsed read as none saved, so every
47
+ * setting starts from its default. The first save over them keeps them
48
+ * first as a broken copy (`host.json.broken`, replacing an older one),
49
+ * so they can still be recovered.
50
+ * - A save keeps everything it doesn't change exactly as it is: other
51
+ * keys, keys `read` leaves out, and the fields and `version` of settings
52
+ * saved by a newer Halonic.
53
+ * - Saves through one store run one at a time, so two saves of different
54
+ * keys keep both, and so do two windows of the desktop app, which save
55
+ * through one process. Between processes (the CLI and the desktop app
56
+ * saving at the same moment) the last save wins.
57
+ * - Settings that can't be read at all (no permission, a link) make `read`
58
+ * throw, so Studio can say it couldn't load them.
59
+ */
60
+ export interface HostSettingsStore {
61
+ /** Every saved value by key; an empty object when none is saved or they can't be parsed. */
62
+ read(): Promise<HostSettingValues>;
63
+ /**
64
+ * Saves one key's value and leaves every other key as it is. A reader
65
+ * sees the settings from before the save or after it, never half of them.
66
+ *
67
+ * @throws RangeError when the key isn't one {@link isHostSettingKey} allows, a
68
+ * number isn't finite, or the settings would be over {@link hostSettingsMaxBytes};
69
+ * nothing is saved then
70
+ */
71
+ set(key: string, value: HostSettingValue): Promise<void>;
72
+ /**
73
+ * Removes one key and leaves every other key as it is. Removing a key
74
+ * that isn't saved is not an error, and a removal is never refused for
75
+ * size, even when the saved settings are already over
76
+ * {@link hostSettingsMaxBytes}.
77
+ *
78
+ * @throws RangeError when the key isn't one {@link isHostSettingKey} allows; nothing is saved then
79
+ */
80
+ remove(key: string): Promise<void>;
81
+ /**
82
+ * Calls `listener` after the saved settings may have changed, by this
83
+ * store or by anything else (another window, the CLI). It may call more
84
+ * than once for one change, and once for several, so the caller reads
85
+ * again and compares. It never calls after the returned stop function.
86
+ */
87
+ watch(listener: () => void): () => void;
88
+ }
89
+ /**
90
+ * The values in saved settings' text, leaving out any key
91
+ * {@link isHostSettingKey} refuses; `undefined` when the text can't be
92
+ * parsed as `{ "values": {…} }`. For adapters, which only read and write.
93
+ *
94
+ * @example
95
+ * readSavedHostSettings('{"version":1,"values":{"assistant.turn-limit":20}}'); // { "assistant.turn-limit": 20 }
96
+ * readSavedHostSettings('{"version":1,"values":{'); // undefined
97
+ */
98
+ export declare function readSavedHostSettings(text: string): HostSettingValues | undefined;
99
+ /** What an adapter writes for one save, from {@link changeSavedHostSettings}. */
100
+ export interface SavedHostSettingsChange {
101
+ /** The settings' new text. */
102
+ readonly text: string;
103
+ /** Whether the old text couldn't be parsed, so the adapter keeps it as a broken copy first. */
104
+ readonly broken: boolean;
105
+ }
106
+ /**
107
+ * The saved settings' text with one key saved, or removed when `value` is
108
+ * `undefined`, and everything else kept as it is. For adapters, which only
109
+ * read and write.
110
+ *
111
+ * @param text - the saved settings' text, `undefined` when none is saved
112
+ * @throws RangeError when the key isn't one {@link isHostSettingKey} allows, a
113
+ * number isn't finite, or a saved value would bring the values over
114
+ * {@link hostSettingsMaxBytes}; a removal is never refused for size
115
+ */
116
+ export declare function changeSavedHostSettings(text: string | undefined, key: string, value: HostSettingValue | undefined): SavedHostSettingsChange;
@@ -0,0 +1,106 @@
1
+ /** Version of the {@link HostSettingsStore} port. A breaking change bumps it and comes with an ADR. */
2
+ export const hostSettingsStoreVersion = 1;
3
+ /**
4
+ * The most bytes every saved value may take together, as compact JSON of
5
+ * the values by key (`JSON.stringify(values)` in UTF-8), so long
6
+ * instructions fit and a runaway value doesn't.
7
+ */
8
+ export const hostSettingsMaxBytes = 256 * 1024;
9
+ /** A key is two or more lowercase words joined by dots, each word letters and digits with single `-`. */
10
+ const keyPattern = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*(?:\.[a-z][a-z0-9]*(?:-[a-z0-9]+)*)+$/;
11
+ /**
12
+ * Whether `key` can name a host setting: at most 64 characters, the section
13
+ * first, such as `assistant.turn-limit` or `sandbox.new-agents`. Adapters
14
+ * refuse any other key, so no key can be `__proto__` or hold a path.
15
+ *
16
+ * @example
17
+ * isHostSettingKey("assistant.turn-limit"); // true
18
+ * isHostSettingKey("turn-limit"); // false: no section
19
+ */
20
+ export function isHostSettingKey(key) {
21
+ return key.length <= 64 && keyPattern.test(key);
22
+ }
23
+ /**
24
+ * The values in saved settings' text, leaving out any key
25
+ * {@link isHostSettingKey} refuses; `undefined` when the text can't be
26
+ * parsed as `{ "values": {…} }`. For adapters, which only read and write.
27
+ *
28
+ * @example
29
+ * readSavedHostSettings('{"version":1,"values":{"assistant.turn-limit":20}}'); // { "assistant.turn-limit": 20 }
30
+ * readSavedHostSettings('{"version":1,"values":{'); // undefined
31
+ */
32
+ export function readSavedHostSettings(text) {
33
+ const saved = parseSaved(text);
34
+ if (saved === undefined)
35
+ return undefined;
36
+ return Object.fromEntries(Object.entries(saved.values).filter(([key]) => isHostSettingKey(key)));
37
+ }
38
+ /**
39
+ * The saved settings' text with one key saved, or removed when `value` is
40
+ * `undefined`, and everything else kept as it is. For adapters, which only
41
+ * read and write.
42
+ *
43
+ * @param text - the saved settings' text, `undefined` when none is saved
44
+ * @throws RangeError when the key isn't one {@link isHostSettingKey} allows, a
45
+ * number isn't finite, or a saved value would bring the values over
46
+ * {@link hostSettingsMaxBytes}; a removal is never refused for size
47
+ */
48
+ export function changeSavedHostSettings(text, key, value) {
49
+ if (!isHostSettingKey(key)) {
50
+ throw new RangeError(`${JSON.stringify(key)} can't name a setting.`);
51
+ }
52
+ if (value !== undefined && !holdsOnlyFiniteNumbers(value)) {
53
+ throw new RangeError(`The value for ${key} has a number that isn't finite.`);
54
+ }
55
+ const saved = text === undefined ? undefined : parseSaved(text);
56
+ const values = { ...saved?.values };
57
+ if (value === undefined)
58
+ delete values[key];
59
+ else
60
+ values[key] = value;
61
+ // A removal is never refused for size, so settings already over it (saved
62
+ // by a newer Halonic, or edited by hand) can still be brought back under it.
63
+ if (value !== undefined && utf8Length(JSON.stringify(values)) > hostSettingsMaxBytes) {
64
+ throw new RangeError(`The settings would be over ${hostSettingsMaxBytes} bytes with ${key}.`);
65
+ }
66
+ return {
67
+ text: `${JSON.stringify({ version: 1, ...saved, values }, null, 2)}\n`,
68
+ broken: text !== undefined && saved === undefined,
69
+ };
70
+ }
71
+ function parseSaved(text) {
72
+ let parsed;
73
+ try {
74
+ parsed = JSON.parse(text);
75
+ }
76
+ catch {
77
+ return undefined;
78
+ }
79
+ return isObject(parsed) && isObject(parsed["values"]) ? parsed : undefined;
80
+ }
81
+ function isObject(value) {
82
+ return typeof value === "object" && value !== null && !Array.isArray(value);
83
+ }
84
+ /** `text`'s length in UTF-8 bytes. The core has no `TextEncoder`; JSON text has no lone surrogates. */
85
+ function utf8Length(text) {
86
+ let bytes = 0;
87
+ for (const character of text) {
88
+ const code = character.codePointAt(0) ?? 0;
89
+ if (code < 0x80)
90
+ bytes += 1;
91
+ else if (code < 0x800)
92
+ bytes += 2;
93
+ else if (code < 0x10000)
94
+ bytes += 3;
95
+ else
96
+ bytes += 4;
97
+ }
98
+ return bytes;
99
+ }
100
+ function holdsOnlyFiniteNumbers(value) {
101
+ if (typeof value === "number")
102
+ return Number.isFinite(value);
103
+ if (typeof value !== "object" || value === null)
104
+ return true;
105
+ return Object.values(value).every(holdsOnlyFiniteNumbers);
106
+ }