@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
@@ -22,9 +22,57 @@ export interface Secrets {
22
22
  * The secret's value, or `null` when it isn't configured.
23
23
  *
24
24
  * @throws RangeError when the name breaks the rule above
25
+ *
26
+ * Any error `get` throws, for this or any other reason, never holds the
27
+ * secret's value in its message or its cause, so a caller may show the
28
+ * message.
25
29
  */
26
30
  get(name: string): Promise<string | null>;
27
31
  }
32
+ /**
33
+ * The secrets a person sets and removes with `halonic secret`: {@link Secrets}
34
+ * that can also be written. A host that only reads secrets (a Worker, the
35
+ * environment) implements `Secrets` alone; only a store the person owns, such
36
+ * as a file in the profile, implements this. It follows the version of
37
+ * {@link Secrets}, which it extends without changing.
38
+ *
39
+ * The same name rule and the same matching apply: every method runs
40
+ * {@link secretKey} on a name before it touches storage, so a bad name is a
41
+ * `RangeError` that changes nothing, and `Open-AI` and `open_ai` are one secret.
42
+ * **A value never appears in an error, a log or a listing.**
43
+ */
44
+ export interface SecretStore extends Secrets {
45
+ /**
46
+ * Saves a value, replacing the one stored under the same name. The name is
47
+ * kept as it was written last, so a listing shows what the person typed.
48
+ *
49
+ * @throws RangeError when the name breaks the rule, or the value is empty or
50
+ * longer than {@link maxSecretValueLength}; the message never repeats the value
51
+ */
52
+ set(name: string, value: string): Promise<void>;
53
+ /**
54
+ * Removes a secret. Removing one that isn't stored succeeds and changes nothing.
55
+ *
56
+ * @throws RangeError when the name breaks the rule
57
+ */
58
+ remove(name: string): Promise<void>;
59
+ /** The names of the stored secrets, sorted by their `secretKey` form, and never a value. */
60
+ names(): Promise<readonly string[]>;
61
+ }
62
+ /** The longest value a {@link SecretStore} saves, in UTF-16 code units. Bounds what one secret may cost a store. */
63
+ export declare const maxSecretValueLength: number;
64
+ /**
65
+ * Checks a value before a {@link SecretStore} saves it, so every store refuses
66
+ * the same ones: an empty value (`EnvSecrets` treats it as unset, so it would
67
+ * look like no secret) and one over {@link maxSecretValueLength}.
68
+ *
69
+ * @throws RangeError; the message never repeats the value, only its length
70
+ *
71
+ * @example
72
+ * checkSecretValue("sk-live-1234"); // fine
73
+ * checkSecretValue(""); // throws a RangeError
74
+ */
75
+ export declare function checkSecretValue(value: string): void;
28
76
  /** The longest a `Secrets` or `TokenStore` name may be. */
29
77
  export declare const maxSecretNameLength = 128;
30
78
  /**
@@ -98,6 +146,14 @@ export declare function isHalonicSecretName(name: string): boolean;
98
146
  * belongs to a plugin, so no workspace may pick it.
99
147
  */
100
148
  export declare const pluginSecretNamePrefix = "plugin__";
149
+ /**
150
+ * Comes between the plugin's id and the secret's name in a plugin's secret
151
+ * name, and ends {@link pluginSecretNamePrefix}'s share of it. `Secrets`
152
+ * takes `-` and `_` as the same character, so a single one can't tell
153
+ * `a-b` + `c` from `a` + `b-c`. Ids and secret names never hold two `-` in a
154
+ * row, so a doubled `_` can't come from either of them.
155
+ */
156
+ export declare const pluginSecretSeparator = "__";
101
157
  /**
102
158
  * Whether a secret name belongs to a plugin: it starts with
103
159
  * {@link pluginSecretNamePrefix}. Names are compared in their `secretKey`
@@ -126,3 +182,20 @@ export declare function isPluginSecretName(name: string): boolean;
126
182
  * await picks.get("plugin__weather__acme-key"); // throws a RangeError
127
183
  */
128
184
  export declare function workspaceSecrets(secrets: Secrets): Secrets;
185
+ /**
186
+ * The secrets as one plugin's backend may read them: only the names stored
187
+ * for that plugin, `plugin__<id>__<secret>` in `secretKey` form, and nothing
188
+ * else, so a plugin can't read a workspace's secret, Halonic's own or
189
+ * another plugin's. The app picks each name from the plugin's own manifest;
190
+ * this is the second check, at the place that reads.
191
+ *
192
+ * @throws RangeError from `get`, for a name that isn't one of the plugin's;
193
+ * the message never repeats the name
194
+ *
195
+ * @example
196
+ * const own = pluginSecrets(secrets, "weather");
197
+ * await own.get("plugin__weather__acme-key"); // the stored key, or null
198
+ * await own.get("openai-key"); // throws a RangeError
199
+ * await own.get("plugin__rain__acme-key"); // throws a RangeError
200
+ */
201
+ export declare function pluginSecrets(secrets: Secrets, pluginId: string): Secrets;
@@ -1,5 +1,26 @@
1
1
  /** Version of the {@link Secrets} port. A breaking change bumps it and comes with an ADR. */
2
2
  export const secretsVersion = 1;
3
+ /** The longest value a {@link SecretStore} saves, in UTF-16 code units. Bounds what one secret may cost a store. */
4
+ export const maxSecretValueLength = 64 * 1024;
5
+ /**
6
+ * Checks a value before a {@link SecretStore} saves it, so every store refuses
7
+ * the same ones: an empty value (`EnvSecrets` treats it as unset, so it would
8
+ * look like no secret) and one over {@link maxSecretValueLength}.
9
+ *
10
+ * @throws RangeError; the message never repeats the value, only its length
11
+ *
12
+ * @example
13
+ * checkSecretValue("sk-live-1234"); // fine
14
+ * checkSecretValue(""); // throws a RangeError
15
+ */
16
+ export function checkSecretValue(value) {
17
+ if (value === "") {
18
+ throw new RangeError("A secret's value can't be empty. Remove the secret instead.");
19
+ }
20
+ if (value.length > maxSecretValueLength) {
21
+ throw new RangeError(`A secret's value can be at most ${maxSecretValueLength} characters; this one has ${value.length}.`);
22
+ }
23
+ }
3
24
  /** The longest a `Secrets` or `TokenStore` name may be. */
4
25
  export const maxSecretNameLength = 128;
5
26
  const secretNamePattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
@@ -99,6 +120,14 @@ export function isHalonicSecretName(name) {
99
120
  * belongs to a plugin, so no workspace may pick it.
100
121
  */
101
122
  export const pluginSecretNamePrefix = "plugin__";
123
+ /**
124
+ * Comes between the plugin's id and the secret's name in a plugin's secret
125
+ * name, and ends {@link pluginSecretNamePrefix}'s share of it. `Secrets`
126
+ * takes `-` and `_` as the same character, so a single one can't tell
127
+ * `a-b` + `c` from `a` + `b-c`. Ids and secret names never hold two `-` in a
128
+ * row, so a doubled `_` can't come from either of them.
129
+ */
130
+ export const pluginSecretSeparator = "__";
102
131
  const pluginKeyPrefix = secretKey(pluginSecretNamePrefix);
103
132
  /**
104
133
  * Whether a secret name belongs to a plugin: it starts with
@@ -142,3 +171,30 @@ export function workspaceSecrets(secrets) {
142
171
  },
143
172
  };
144
173
  }
174
+ /**
175
+ * The secrets as one plugin's backend may read them: only the names stored
176
+ * for that plugin, `plugin__<id>__<secret>` in `secretKey` form, and nothing
177
+ * else, so a plugin can't read a workspace's secret, Halonic's own or
178
+ * another plugin's. The app picks each name from the plugin's own manifest;
179
+ * this is the second check, at the place that reads.
180
+ *
181
+ * @throws RangeError from `get`, for a name that isn't one of the plugin's;
182
+ * the message never repeats the name
183
+ *
184
+ * @example
185
+ * const own = pluginSecrets(secrets, "weather");
186
+ * await own.get("plugin__weather__acme-key"); // the stored key, or null
187
+ * await own.get("openai-key"); // throws a RangeError
188
+ * await own.get("plugin__rain__acme-key"); // throws a RangeError
189
+ */
190
+ export function pluginSecrets(secrets, pluginId) {
191
+ const own = secretKey(`${pluginSecretNamePrefix}${pluginId}${pluginSecretSeparator}`);
192
+ return {
193
+ get: async (name) => {
194
+ if (!secretKey(name).startsWith(own)) {
195
+ throw new RangeError("A plugin can read only the secrets stored for it.");
196
+ }
197
+ return await secrets.get(name);
198
+ },
199
+ };
200
+ }
@@ -17,9 +17,14 @@ export declare const toolApprovalStoreVersion = 1;
17
17
  * identity. The headers count too, but only as their {@link WrittenDigest},
18
18
  * because a person may have typed a token straight into one.
19
19
  * - `stdio`: the command and its arguments, the pin when the command is a
20
- * package runner, else `null`, and the environment as its
21
- * {@link WrittenDigest}, for the same reason. So a wrapper pointed at
22
- * another server through its environment asks again.
20
+ * package runner, else `null`, the folder it starts in, normalised as the
21
+ * trust content writes it (`.` when the workspace writes none), the
22
+ * environment as its {@link WrittenDigest}, for the same reason, and the
23
+ * secret environment (variable name to secret name) as its
24
+ * {@link WrittenDigest}, and the names it takes from the app's environment
25
+ * (`inheritEnv`), sorted, only when it names one. So a wrapper pointed at
26
+ * another server through its environment, its folder, another secret or
27
+ * another variable of the person's asks again.
23
28
  * - `integration`: the integration's id, its version and the settings the
24
29
  * workspace gives it, such as the `url` of an MCP server. A new version
25
30
  * asks again, because its reviewed code may talk to the server
@@ -37,7 +42,10 @@ export type ToolServer = {
37
42
  command: string;
38
43
  args: string[];
39
44
  pin: ConnectorPin | null;
45
+ cwd: string;
40
46
  env: WrittenDigest | null;
47
+ secretEnv: WrittenDigest | null;
48
+ inheritEnv?: string[];
41
49
  } | {
42
50
  kind: "integration";
43
51
  id: string;
@@ -105,6 +113,8 @@ export interface ToolApproval extends ToolApprovalKey {
105
113
  /** When the person approved it, as ISO text. */
106
114
  approvedAt: string;
107
115
  }
116
+ /** The `fingerprint` of a stored {@link ToolApproval}: `htl1:` and 64 lowercase hex digits. */
117
+ export declare const listedToolFingerprintPattern: RegExp;
108
118
  /** One version of a listed tool that waited for approval. */
109
119
  export interface WaitingVersion {
110
120
  fingerprint: ListedToolFingerprint;
@@ -153,11 +163,11 @@ export interface ToolApprovalStore {
153
163
  list(location: string, source: string): Promise<ToolApproval[]>;
154
164
  /**
155
165
  * Notes that a version of a listed tool is waiting, and returns the
156
- * versions noted for that tool, oldest first. Noting a fingerprint that
157
- * is already there keeps its first `firstSeenAt`. The store keeps at most
158
- * the newest {@link maxWaitingVersions}, so a server that changes a tool
159
- * on every call can't fill the profile. The host uses them to stop asking
160
- * about a tool its server keeps changing.
166
+ * versions noted for that tool, oldest first. The store follows
167
+ * {@link keptWaitingVersions}, so a server that changes a tool on every
168
+ * call, or lists new names on every call, can't fill the profile. The
169
+ * host uses the versions to stop asking about a tool its server keeps
170
+ * changing.
161
171
  */
162
172
  noteWaiting(key: ToolApprovalKey, version: WaitingVersion): Promise<WaitingVersion[]>;
163
173
  }
@@ -209,3 +219,35 @@ export declare class UnreadableToolApprovalError extends Error {
209
219
  export declare function isUnreadableToolApprovalError(value: unknown): value is UnreadableToolApprovalError;
210
220
  /** How many waiting versions of one listed tool a {@link ToolApprovalStore} keeps. */
211
221
  export declare const maxWaitingVersions = 8;
222
+ /**
223
+ * How many listed tools of one source a {@link ToolApprovalStore} keeps
224
+ * waiting versions for: twice the most tools one listing may return.
225
+ */
226
+ export declare const maxWaitingTools = 256;
227
+ /** How long a {@link ToolApprovalStore} keeps a waiting version after it was first seen: 24 hours. */
228
+ export declare const waitingVersionKeptMs: number;
229
+ /**
230
+ * The waiting versions of one source after noting `version` for the tool
231
+ * `name`, the one rule every {@link ToolApprovalStore} follows:
232
+ *
233
+ * - The time `version` was first seen counts as now. Every version first
234
+ * seen more than {@link waitingVersionKeptMs} before it is dropped, and so
235
+ * is a tool left with none.
236
+ * - A fingerprint that is still noted keeps its first `firstSeenAt`;
237
+ * otherwise `version` is added as the newest.
238
+ * - Each tool keeps its newest {@link maxWaitingVersions} versions.
239
+ * - The source keeps the {@link maxWaitingTools} tools whose newest version
240
+ * was first seen most recently, `name` always among them. Among tools
241
+ * whose newest versions were first seen at the same time, the one first
242
+ * in UTF-16 code-unit order is dropped first.
243
+ *
244
+ * Dropping only ever makes the host ask again, never offer a tool.
245
+ *
246
+ * @param waiting - the source's waiting versions by tool name, each oldest first; not changed
247
+ * @returns the source's waiting versions to store, and those of `name`, as `noteWaiting` returns them
248
+ * @throws RangeError when `version.firstSeenAt` isn't a date
249
+ */
250
+ export declare function keptWaitingVersions(waiting: ReadonlyMap<string, readonly WaitingVersion[]>, name: string, version: WaitingVersion): {
251
+ waiting: Map<string, WaitingVersion[]>;
252
+ versions: WaitingVersion[];
253
+ };
@@ -1,5 +1,7 @@
1
1
  /** Version of the {@link ToolApprovalStore} port. A breaking change bumps it and comes with an ADR. */
2
2
  export const toolApprovalStoreVersion = 1;
3
+ /** The `fingerprint` of a stored {@link ToolApproval}: `htl1:` and 64 lowercase hex digits. */
4
+ export const listedToolFingerprintPattern = /^htl1:[0-9a-f]{64}$/u;
3
5
  /**
4
6
  * What a {@link ToolApprovalStore} throws for an approval that exists but
5
7
  * can't be read: a damaged file, or one a newer Halonic wrote. It names the
@@ -55,3 +57,60 @@ export function isUnreadableToolApprovalError(value) {
55
57
  }
56
58
  /** How many waiting versions of one listed tool a {@link ToolApprovalStore} keeps. */
57
59
  export const maxWaitingVersions = 8;
60
+ /**
61
+ * How many listed tools of one source a {@link ToolApprovalStore} keeps
62
+ * waiting versions for: twice the most tools one listing may return.
63
+ */
64
+ export const maxWaitingTools = 256;
65
+ /** How long a {@link ToolApprovalStore} keeps a waiting version after it was first seen: 24 hours. */
66
+ export const waitingVersionKeptMs = 24 * 60 * 60 * 1000;
67
+ /**
68
+ * The waiting versions of one source after noting `version` for the tool
69
+ * `name`, the one rule every {@link ToolApprovalStore} follows:
70
+ *
71
+ * - The time `version` was first seen counts as now. Every version first
72
+ * seen more than {@link waitingVersionKeptMs} before it is dropped, and so
73
+ * is a tool left with none.
74
+ * - A fingerprint that is still noted keeps its first `firstSeenAt`;
75
+ * otherwise `version` is added as the newest.
76
+ * - Each tool keeps its newest {@link maxWaitingVersions} versions.
77
+ * - The source keeps the {@link maxWaitingTools} tools whose newest version
78
+ * was first seen most recently, `name` always among them. Among tools
79
+ * whose newest versions were first seen at the same time, the one first
80
+ * in UTF-16 code-unit order is dropped first.
81
+ *
82
+ * Dropping only ever makes the host ask again, never offer a tool.
83
+ *
84
+ * @param waiting - the source's waiting versions by tool name, each oldest first; not changed
85
+ * @returns the source's waiting versions to store, and those of `name`, as `noteWaiting` returns them
86
+ * @throws RangeError when `version.firstSeenAt` isn't a date
87
+ */
88
+ export function keptWaitingVersions(waiting, name, version) {
89
+ const now = Date.parse(version.firstSeenAt);
90
+ if (Number.isNaN(now)) {
91
+ throw new RangeError(`The waiting version's firstSeenAt isn't a date: ${version.firstSeenAt}`);
92
+ }
93
+ const recent = (noted) => now - Date.parse(noted.firstSeenAt) <= waitingVersionKeptMs;
94
+ const kept = new Map();
95
+ for (const [other, versions] of waiting) {
96
+ const left = versions.filter(recent).map((noted) => ({ ...noted }));
97
+ if (left.length > 0)
98
+ kept.set(other, left);
99
+ }
100
+ const versions = kept.get(name) ?? [];
101
+ if (!versions.some((noted) => noted.fingerprint === version.fingerprint)) {
102
+ versions.push({ fingerprint: version.fingerprint, firstSeenAt: version.firstSeenAt });
103
+ }
104
+ const toolVersions = versions.slice(-maxWaitingVersions);
105
+ kept.set(name, toolVersions);
106
+ if (kept.size > maxWaitingTools) {
107
+ const lastNoted = (versions) => Math.max(...versions.map((noted) => Date.parse(noted.firstSeenAt)));
108
+ const others = [...kept]
109
+ .filter(([other]) => other !== name)
110
+ .sort(([leftName, left], [rightName, right]) => lastNoted(left) - lastNoted(right) || (leftName < rightName ? -1 : 1));
111
+ for (const [other] of others.slice(0, kept.size - maxWaitingTools)) {
112
+ kept.delete(other);
113
+ }
114
+ }
115
+ return { waiting: kept, versions: toolVersions.map((noted) => ({ ...noted })) };
116
+ }
@@ -0,0 +1,8 @@
1
+ import type { VcsStatus } from "./vcs.js";
2
+ /**
3
+ * Reads the output of `git status --porcelain=v2 -z`, with or without `--branch`: the branch
4
+ * header lines, if any, then one record per changed path. A path changed both in the index and
5
+ * in the working tree gives two changes, one staged and one not. The result is for a repository;
6
+ * a folder that isn't one is for the caller to tell.
7
+ */
8
+ export declare function parseStatus(output: string): VcsStatus;
@@ -0,0 +1,90 @@
1
+ /** What one letter of a porcelain `XY` field means; `.` is no change. */
2
+ const letterStatus = {
3
+ M: "modified",
4
+ T: "modified",
5
+ A: "added",
6
+ C: "added",
7
+ D: "deleted",
8
+ R: "renamed",
9
+ };
10
+ /** The spaces before the path in an ordinary (`1`), renamed (`2`) and conflicted (`u`) record. */
11
+ const pathAfterSpaces = { "1": 8, "2": 9, u: 10 };
12
+ /**
13
+ * Reads the output of `git status --porcelain=v2 -z`, with or without `--branch`: the branch
14
+ * header lines, if any, then one record per changed path. A path changed both in the index and
15
+ * in the working tree gives two changes, one staged and one not. The result is for a repository;
16
+ * a folder that isn't one is for the caller to tell.
17
+ */
18
+ export function parseStatus(output) {
19
+ const status = {
20
+ isRepo: true,
21
+ branch: null,
22
+ upstream: null,
23
+ ahead: 0,
24
+ behind: 0,
25
+ changes: [],
26
+ };
27
+ const fields = output.split("\0");
28
+ for (let at = 0; at < fields.length; at++) {
29
+ const field = fields[at] ?? "";
30
+ if (field.startsWith("# "))
31
+ readHeader(field.slice(2), status);
32
+ else if (field.startsWith("1 "))
33
+ status.changes.push(...ordinary(field, pathAfterSpaces["1"]));
34
+ else if (field.startsWith("2 ")) {
35
+ // A rename's old path is the next field.
36
+ at += 1;
37
+ status.changes.push(...ordinary(field, pathAfterSpaces["2"], fields[at]));
38
+ }
39
+ else if (field.startsWith("u ")) {
40
+ status.changes.push({
41
+ path: pathAfter(field, pathAfterSpaces.u),
42
+ status: "conflicted",
43
+ staged: false,
44
+ });
45
+ }
46
+ else if (field.startsWith("? ")) {
47
+ status.changes.push({ path: field.slice(2), status: "untracked", staged: false });
48
+ }
49
+ }
50
+ return status;
51
+ }
52
+ function readHeader(header, status) {
53
+ const space = header.indexOf(" ");
54
+ const name = header.slice(0, space);
55
+ const value = header.slice(space + 1);
56
+ if (name === "branch.head")
57
+ status.branch = value === "(detached)" ? null : value;
58
+ if (name === "branch.upstream")
59
+ status.upstream = value;
60
+ if (name === "branch.ab") {
61
+ const match = /^\+(\d+) -(\d+)$/u.exec(value);
62
+ status.ahead = Number(match?.[1] ?? 0);
63
+ status.behind = Number(match?.[2] ?? 0);
64
+ }
65
+ }
66
+ /** The changes of an ordinary or renamed record: one for the index letter, one for the tree's. */
67
+ function ordinary(field, spaces, from) {
68
+ const path = pathAfter(field, spaces);
69
+ const changes = [];
70
+ for (const [letter, staged] of [
71
+ [field.charAt(2), true],
72
+ [field.charAt(3), false],
73
+ ]) {
74
+ const status = letterStatus[letter];
75
+ if (status === undefined)
76
+ continue;
77
+ const change = { path, status, staged };
78
+ if (status === "renamed" && from !== undefined)
79
+ change.from = from;
80
+ changes.push(change);
81
+ }
82
+ return changes;
83
+ }
84
+ /** The text after the first `spaces` spaces of a record: the path, which may hold spaces itself. */
85
+ function pathAfter(field, spaces) {
86
+ let at = 0;
87
+ for (let found = 0; found < spaces; found++)
88
+ at = field.indexOf(" ", at) + 1;
89
+ return field.slice(at);
90
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Reads an amount the way someone writes it on a receipt: digits, and at
3
+ * most one `.` or `,` before the decimals. No sign, no grouping, no
4
+ * exponent. A single separator is always the decimal one, so `1,000` is 1.
5
+ *
6
+ * @param text - what the user typed; surrounding spaces are ignored
7
+ * @returns the amount, or `undefined` for empty or anything else
8
+ *
9
+ * @example
10
+ * parseAmount("15,00"); // 15
11
+ * parseAmount("1.000,50"); // undefined
12
+ */
13
+ export declare function parseAmount(text: string): number | undefined;
@@ -0,0 +1,22 @@
1
+ const amountShape = /^(\d+)(?:[.,](\d+))?$/;
2
+ /**
3
+ * Reads an amount the way someone writes it on a receipt: digits, and at
4
+ * most one `.` or `,` before the decimals. No sign, no grouping, no
5
+ * exponent. A single separator is always the decimal one, so `1,000` is 1.
6
+ *
7
+ * @param text - what the user typed; surrounding spaces are ignored
8
+ * @returns the amount, or `undefined` for empty or anything else
9
+ *
10
+ * @example
11
+ * parseAmount("15,00"); // 15
12
+ * parseAmount("1.000,50"); // undefined
13
+ */
14
+ export function parseAmount(text) {
15
+ const match = amountShape.exec(text.trim());
16
+ if (match === null) {
17
+ return undefined;
18
+ }
19
+ const [, whole, decimals] = match;
20
+ const amount = Number(decimals === undefined ? whole : `${whole}.${decimals}`);
21
+ return Number.isFinite(amount) ? amount : undefined;
22
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Whether a code point is one nobody sees on screen but that can still change
3
+ * the text around it, such as a right-to-left override.
4
+ *
5
+ * @example
6
+ * isHiddenCharacter(0x202e); // true
7
+ * isHiddenCharacter("a".codePointAt(0) ?? 0); // false
8
+ */
9
+ export declare function isHiddenCharacter(codePoint: number): boolean;
10
+ /**
11
+ * JSON text with every hidden character written as a `\uXXXX` escape, so the
12
+ * raw JSON shows it too and stays the same JSON. A character above U+FFFF,
13
+ * such as a tag character, becomes its two UTF-16 escapes. Hidden characters
14
+ * can only stand inside JSON strings, where an escape means the same.
15
+ *
16
+ * @example
17
+ * escapeHidden(JSON.stringify(`Refund${String.fromCodePoint(0x202e)}for Sam`)); // '"Refund\\u202efor Sam"'
18
+ */
19
+ export declare function escapeHidden(json: string): string;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The code point ranges a text can carry without anyone seeing them: control
3
+ * characters other than tab (U+0009) and newline (U+000A), the soft hyphen,
4
+ * zero-width characters, direction marks, overrides and isolates, the word
5
+ * joiner and invisible operators, the byte order mark and Unicode tag
6
+ * characters. Written as numbers, so the source holds no invisible character
7
+ * itself.
8
+ */
9
+ const hiddenRanges = [
10
+ [0x0000, 0x0008],
11
+ [0x000b, 0x001f],
12
+ [0x007f, 0x009f],
13
+ [0x00ad, 0x00ad],
14
+ [0x061c, 0x061c],
15
+ [0x200b, 0x200f],
16
+ [0x202a, 0x202e],
17
+ [0x2060, 0x2064],
18
+ [0x2066, 0x2069],
19
+ [0xfeff, 0xfeff],
20
+ [0xe0000, 0xe007f],
21
+ ];
22
+ /**
23
+ * Whether a code point is one nobody sees on screen but that can still change
24
+ * the text around it, such as a right-to-left override.
25
+ *
26
+ * @example
27
+ * isHiddenCharacter(0x202e); // true
28
+ * isHiddenCharacter("a".codePointAt(0) ?? 0); // false
29
+ */
30
+ export function isHiddenCharacter(codePoint) {
31
+ return hiddenRanges.some(([first, last]) => codePoint >= first && codePoint <= last);
32
+ }
33
+ /**
34
+ * JSON text with every hidden character written as a `\uXXXX` escape, so the
35
+ * raw JSON shows it too and stays the same JSON. A character above U+FFFF,
36
+ * such as a tag character, becomes its two UTF-16 escapes. Hidden characters
37
+ * can only stand inside JSON strings, where an escape means the same.
38
+ *
39
+ * @example
40
+ * escapeHidden(JSON.stringify(`Refund${String.fromCodePoint(0x202e)}for Sam`)); // '"Refund\\u202efor Sam"'
41
+ */
42
+ export function escapeHidden(json) {
43
+ let escaped = "";
44
+ for (const character of json) {
45
+ if (!isHiddenCharacter(character.codePointAt(0) ?? 0)) {
46
+ escaped += character;
47
+ continue;
48
+ }
49
+ for (let index = 0; index < character.length; index++) {
50
+ escaped += `\\u${character.charCodeAt(index).toString(16).padStart(4, "0")}`;
51
+ }
52
+ }
53
+ return escaped;
54
+ }
@@ -1,3 +1,4 @@
1
+ export { parseAmount } from "./amount.js";
1
2
  export { asciiHost } from "./ascii-host.js";
2
3
  export { AssertionError, assert, assertDefined, assertNever } from "./assert.js";
3
4
  export { decodeBase64url, encodeBase64url } from "./base64url.js";
@@ -10,6 +11,7 @@ export { deepEqual } from "./deep-equal.js";
10
11
  export { Emitter, type Listener } from "./emitter.js";
11
12
  export { type ErrorKind, errorKindOf, errorKinds, isErrorKind, KindedError, } from "./error-kind.js";
12
13
  export { bytesToHex } from "./hex.js";
14
+ export { escapeHidden, isHiddenCharacter } from "./hidden-characters.js";
13
15
  export { fixedIds, type IdGenerator, type RandomBytes, randomIds } from "./ids.js";
14
16
  export { KeyedQueue } from "./keyed-queue.js";
15
17
  export { applyLineChanges, type DiffLinesOptions, diffLines, type LineChange, MAX_COMPARED_LINE_PAIRS, splitLines, } from "./line-diff.js";
@@ -19,7 +21,10 @@ export { posixPath } from "./posix-path.js";
19
21
  export { type Err, err, mapResult, type Ok, ok, type Result, unwrap } from "./result.js";
20
22
  export { SecretScrubber, secretRemoved, secretTextForms } from "./secret-scrubber.js";
21
23
  export { findSecretShape, type SecretShape } from "./secret-shape.js";
24
+ export { isSecretVariableName } from "./secret-variable-name.js";
25
+ export { type SiteName, siteName, siteNameMaxLength } from "./site-name.js";
22
26
  export { type StopSignal, StopSource } from "./stop-signal.js";
23
27
  export { slugify, uniqueName } from "./text.js";
28
+ export { type UrlHost, urlHost } from "./url-host.js";
24
29
  export { decodeUtf8, encodeUtf8 } from "./utf8.js";
25
30
  export { compareVersions, formatVersion, isAtLeast, readVersion, type Version } from "./version.js";
@@ -1,4 +1,5 @@
1
1
  // The public API of @halonicjs/std. Everything else in src/ is internal.
2
+ export { parseAmount } from "./amount.js";
2
3
  export { asciiHost } from "./ascii-host.js";
3
4
  export { AssertionError, assert, assertDefined, assertNever } from "./assert.js";
4
5
  export { decodeBase64url, encodeBase64url } from "./base64url.js";
@@ -11,6 +12,7 @@ export { deepEqual } from "./deep-equal.js";
11
12
  export { Emitter } from "./emitter.js";
12
13
  export { errorKindOf, errorKinds, isErrorKind, KindedError, } from "./error-kind.js";
13
14
  export { bytesToHex } from "./hex.js";
15
+ export { escapeHidden, isHiddenCharacter } from "./hidden-characters.js";
14
16
  export { fixedIds, randomIds } from "./ids.js";
15
17
  export { KeyedQueue } from "./keyed-queue.js";
16
18
  export { applyLineChanges, diffLines, MAX_COMPARED_LINE_PAIRS, splitLines, } from "./line-diff.js";
@@ -20,7 +22,10 @@ export { posixPath } from "./posix-path.js";
20
22
  export { err, mapResult, ok, unwrap } from "./result.js";
21
23
  export { SecretScrubber, secretRemoved, secretTextForms } from "./secret-scrubber.js";
22
24
  export { findSecretShape } from "./secret-shape.js";
25
+ export { isSecretVariableName } from "./secret-variable-name.js";
26
+ export { siteName, siteNameMaxLength } from "./site-name.js";
23
27
  export { StopSource } from "./stop-signal.js";
24
28
  export { slugify, uniqueName } from "./text.js";
29
+ export { urlHost } from "./url-host.js";
25
30
  export { decodeUtf8, encodeUtf8 } from "./utf8.js";
26
31
  export { compareVersions, formatVersion, isAtLeast, readVersion } from "./version.js";