akanjs 3.3.1-rc.0 → 3.4.0-rc.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 (76) hide show
  1. package/common/PipelinePlanSchema.ts +31 -7
  2. package/common/index.ts +6 -0
  3. package/common/pipelinePlan.ts +33 -6
  4. package/index.ts +7 -5
  5. package/package.json +1 -1
  6. package/pipelineConfig.ts +25 -2
  7. package/server/console.ts +1 -1
  8. package/server/gateway/childSupervisor.ts +20 -7
  9. package/server/gateway/gatewayConfig.ts +9 -2
  10. package/server/lifecycle/unixSocketPath.ts +79 -0
  11. package/server/logging/logControlSocket.ts +6 -4
  12. package/service/agentLoop/agentLoop.ts +19 -12
  13. package/service/predefinedAdaptor/anthropicTools.ts +8 -2
  14. package/types/common/index.d.ts +1 -1
  15. package/types/common/pipelinePlan.d.ts +37 -7
  16. package/types/index.d.ts +7 -5
  17. package/types/pipelineConfig.d.ts +20 -3
  18. package/types/server/gateway/gatewayConfig.d.ts +2 -0
  19. package/types/server/lifecycle/unixSocketPath.d.ts +22 -0
  20. package/types/server/logging/rotatingLogWriter.d.ts +1 -1
  21. package/types/service/predefinedAdaptor/anthropicTools.d.ts +1 -0
  22. package/types/vendor/@akanjs/native/packages/cli/src/api-updates.d.ts +149 -0
  23. package/types/vendor/@akanjs/native/packages/cli/src/api.d.ts +16 -162
  24. package/types/vendor/@akanjs/native/packages/cli/src/config.d.ts +3 -2
  25. package/types/vendor/@akanjs/native/packages/cli/src/lib/api-task.d.ts +79 -0
  26. package/types/vendor/@akanjs/native/packages/cli/src/lib/prepare.d.ts +1 -1
  27. package/types/vendor/@akanjs/native/packages/cli/src/lib/publish.d.ts +17 -5
  28. package/types/vendor/@akanjs/native/packages/cli/src/lib/updates.d.ts +31 -2
  29. package/types/vendor/@akanjs/native/packages/cli/src/platforms/desktop-update.d.ts +36 -0
  30. package/types/vendor/@akanjs/native/packages/cli/src/platforms/linux-install.d.ts +15 -0
  31. package/types/vendor/@akanjs/native/packages/core/src/kernel.d.ts +16 -5
  32. package/types/vendor/@akanjs/native/packages/desktop/src/commands.d.ts +85 -0
  33. package/types/vendor/@akanjs/native/packages/desktop/src/deeplinks.d.ts +5 -1
  34. package/types/vendor/@akanjs/native/packages/desktop/src/dispatcher.d.ts +3 -0
  35. package/types/vendor/@akanjs/native/packages/desktop/src/lifecycle.d.ts +8 -1
  36. package/types/vendor/@akanjs/native/packages/desktop/src/plugin.d.ts +22 -0
  37. package/types/vendor/@akanjs/native/packages/desktop/src/server.d.ts +1 -0
  38. package/types/vendor/@akanjs/native/plugins/dock/src/index.d.ts +3 -2
  39. package/types/vendor/@akanjs/native/plugins/menu/src/index.d.ts +4 -4
  40. package/types/vendor/@akanjs/native/plugins/updates/src/desktop.d.ts +4 -4
  41. package/types/vendor/@akanjs/native/plugins/updates/src/reports.d.ts +49 -0
  42. package/types/vendor/@akanjs/native/plugins/updates/src/signer.d.ts +21 -0
  43. package/types/vendor/@akanjs/native/plugins/window/src/index.d.ts +58 -3
  44. package/vendor/@akanjs/native/native/desktop/src/lib.rs +210 -55
  45. package/vendor/@akanjs/native/native/desktop/src/linux/dock.rs +3 -2
  46. package/vendor/@akanjs/native/native/desktop/src/linux/menu.rs +13 -10
  47. package/vendor/@akanjs/native/native/desktop/src/linux/mod.rs +5 -3
  48. package/vendor/@akanjs/native/packages/cli/src/api-updates.ts +332 -0
  49. package/vendor/@akanjs/native/packages/cli/src/api.ts +67 -419
  50. package/vendor/@akanjs/native/packages/cli/src/commands/build.ts +4 -1
  51. package/vendor/@akanjs/native/packages/cli/src/config.ts +3 -2
  52. package/vendor/@akanjs/native/packages/cli/src/lib/api-task.ts +223 -0
  53. package/vendor/@akanjs/native/packages/cli/src/lib/prepare.ts +2 -7
  54. package/vendor/@akanjs/native/packages/cli/src/lib/publish.ts +164 -19
  55. package/vendor/@akanjs/native/packages/cli/src/lib/updates.ts +105 -3
  56. package/vendor/@akanjs/native/packages/cli/src/platforms/desktop-update.ts +195 -43
  57. package/vendor/@akanjs/native/packages/cli/src/platforms/linux-appimage.ts +1 -1
  58. package/vendor/@akanjs/native/packages/cli/src/platforms/linux-install.ts +206 -0
  59. package/vendor/@akanjs/native/packages/cli/src/platforms/linux.ts +5 -1
  60. package/vendor/@akanjs/native/packages/core/src/kernel.ts +21 -6
  61. package/vendor/@akanjs/native/packages/core/vectors/updates.json +2 -1
  62. package/vendor/@akanjs/native/packages/desktop/src/commands.ts +387 -0
  63. package/vendor/@akanjs/native/packages/desktop/src/deeplinks.ts +20 -3
  64. package/vendor/@akanjs/native/packages/desktop/src/dispatcher.ts +28 -13
  65. package/vendor/@akanjs/native/packages/desktop/src/host.ts +6 -18
  66. package/vendor/@akanjs/native/packages/desktop/src/lifecycle.ts +8 -1
  67. package/vendor/@akanjs/native/packages/desktop/src/plugin.ts +22 -0
  68. package/vendor/@akanjs/native/packages/desktop/src/server.ts +1 -1
  69. package/vendor/@akanjs/native/plugins/dock/src/index.ts +3 -2
  70. package/vendor/@akanjs/native/plugins/menu/src/index.ts +4 -4
  71. package/vendor/@akanjs/native/plugins/updates/src/desktop.ts +90 -164
  72. package/vendor/@akanjs/native/plugins/updates/src/reports.ts +255 -0
  73. package/vendor/@akanjs/native/plugins/updates/src/signer.ts +133 -0
  74. package/vendor/@akanjs/native/plugins/window/native-plugin.json +6 -1
  75. package/vendor/@akanjs/native/plugins/window/src/desktop.ts +20 -4
  76. package/vendor/@akanjs/native/plugins/window/src/index.ts +58 -3
@@ -7,6 +7,7 @@ import {
7
7
  type PipelinePlan,
8
8
  type PipelinePlanActionStep,
9
9
  type PipelinePlanContext,
10
+ type PipelinePlanDesktopOtaStep,
10
11
  type PipelinePlanOtaStep,
11
12
  type PipelinePlanPhase,
12
13
  type PipelinePlanPost,
@@ -18,6 +19,8 @@ import {
18
19
  type PipelinePlanStep,
19
20
  pipelineActionNames,
20
21
  pipelineActions,
22
+ pipelineDesktopOtaArchs,
23
+ pipelineDesktopOtaSystems,
21
24
  pipelineMilestones,
22
25
  pipelineOtaPlatforms,
23
26
  pipelinePlanLimits,
@@ -366,6 +369,8 @@ export class PipelinePlanSchema {
366
369
  const actionVersion = pipelineActions[action].version;
367
370
  if (action === "ota")
368
371
  return { ...base, kind: "action", action, actionVersion, with: this.#otaWith(step.with, path) };
372
+ if (action === "desktop-ota")
373
+ return { ...base, kind: "action", action, actionVersion, with: this.#desktopOtaWith(step.with, path) };
369
374
  const app = this.#read.object(step.with, `${path}.with`, ["app"]).app;
370
375
  return {
371
376
  ...base,
@@ -382,16 +387,35 @@ export class PipelinePlanSchema {
382
387
  const platforms = this.#read
383
388
  .list(raw.platforms, `${path}.with.platforms`)
384
389
  .map((platform, idx) => this.#read.oneOf(platform, `${path}.with.platforms[${idx}]`, pipelineOtaPlatforms));
385
- for (const [key, list] of [
386
- ["targets", targets],
387
- ["platforms", platforms],
388
- ] as const) {
389
- if (!list.length) this.#fail(`${path}.with.${key}`, "may not be empty");
390
- if (new Set(list).size !== list.length) this.#fail(`${path}.with.${key}`, "lists one twice");
391
- }
390
+ this.#eachOnce(targets, `${path}.with.targets`);
391
+ this.#eachOnce(platforms, `${path}.with.platforms`);
392
392
  return { app: this.#read.string(raw.app, `${path}.with.app`), targets, platforms };
393
393
  }
394
394
 
395
+ #desktopOtaWith(value: unknown, path: string): PipelinePlanDesktopOtaStep["with"] {
396
+ const raw = this.#read.object(value, `${path}.with`, ["app", "targets", "platforms"]);
397
+ const targets = this.#stringList(raw.targets, `${path}.with.targets`);
398
+ const platforms = this.#read.list(raw.platforms, `${path}.with.platforms`).map((platform, idx) => {
399
+ const entryPath = `${path}.with.platforms[${idx}]`;
400
+ const entry = this.#read.object(platform, entryPath, ["os", "arch"]);
401
+ const os = this.#read.oneOf(entry.os, `${entryPath}.os`, pipelineDesktopOtaSystems);
402
+ const arch = this.#read.oneOf(entry.arch, `${entryPath}.arch`, pipelineDesktopOtaArchs);
403
+ if (os === "macos" && arch === "x64") this.#fail(entryPath, "a macOS app is built for arm64 only");
404
+ return { os, arch };
405
+ });
406
+ this.#eachOnce(targets, `${path}.with.targets`);
407
+ this.#eachOnce(
408
+ platforms.map(({ os, arch }) => `${os}-${arch}`),
409
+ `${path}.with.platforms`,
410
+ );
411
+ return { app: this.#read.string(raw.app, `${path}.with.app`), targets, platforms };
412
+ }
413
+
414
+ #eachOnce(items: readonly string[], path: string) {
415
+ if (!items.length) this.#fail(path, "may not be empty");
416
+ if (new Set(items).size !== items.length) this.#fail(path, "lists one twice");
417
+ }
418
+
395
419
  #post(value: unknown, path: string): PipelinePlanPost {
396
420
  const post = this.#read.object(value, path, ["success", "failure", "canceled", "always"]);
397
421
  const steps = (result: keyof PipelinePlanPost) =>
package/common/index.ts CHANGED
@@ -186,6 +186,9 @@ export { parseSearchParams } from "./parseSearchParams";
186
186
  export { formatPhone, isPhoneNumber } from "./phone";
187
187
  export {
188
188
  type PipelineActionName,
189
+ type PipelineDesktopOtaArch,
190
+ type PipelineDesktopOtaPlatform,
191
+ type PipelineDesktopOtaSystem,
189
192
  type PipelineDiagnostic,
190
193
  type PipelineMilestone,
191
194
  type PipelineOtaPlatform,
@@ -193,6 +196,7 @@ export {
193
196
  type PipelinePlanActionStep,
194
197
  type PipelinePlanAppActionStep,
195
198
  type PipelinePlanContext,
199
+ type PipelinePlanDesktopOtaStep,
196
200
  type PipelinePlanOtaStep,
197
201
  type PipelinePlanPhase,
198
202
  type PipelinePlanPost,
@@ -207,6 +211,8 @@ export {
207
211
  type PipelineTrigger,
208
212
  pipelineActionNames,
209
213
  pipelineActions,
214
+ pipelineDesktopOtaArchs,
215
+ pipelineDesktopOtaSystems,
210
216
  pipelineMilestones,
211
217
  pipelineOtaPlatforms,
212
218
  pipelinePlanLimits,
@@ -17,27 +17,46 @@ export type PipelineTrigger = (typeof pipelineTriggers)[number];
17
17
  /**
18
18
  * The executor's own operations, declared per app. Within one app each appears at most once, in this order, and
19
19
  * `docker` needs a `build` before it: the image is made from this run's `akan build` output. An action whose app's
20
- * previous action in the run was skipped or did not succeed is skipped, never run. `deploy` is the only way any part
20
+ * previous action in the run was skipped or did not succeed is skipped, never run; for `desktop-ota` that is the action
21
+ * before `ota`, since a phone bundle and a desktop app are separate releases. `deploy` is the only way any part
21
22
  * of a plan reaches the master host, and its phase runs the apps one at a time. `ota` packs an over-the-air update of
22
23
  * the app's native web bundle per target and platform (`akan pack-update`) for the executor to sign and publish; it
23
- * makes its own CSR build, so it needs no `build` before it.
24
+ * makes its own CSR build, so it needs no `build` before it. `desktop-ota` is the same for a desktop app, which is
25
+ * replaced whole: per target and platform entry the executor runs, on a build host of that entry's OS,
26
+ * `akan pack-update <app> --platform desktop --arch <arch> --target <target>`, which writes an unsigned app archive and
27
+ * a manifest template for the executor to sign and publish. The runner holds no signing key.
24
28
  */
25
29
  export const pipelineActions = {
26
30
  build: { version: 1, milestone: "build", name: "Build" },
27
31
  docker: { version: 1, milestone: "package", name: "Docker" },
28
32
  deploy: { version: 1, milestone: "deploy", name: "Deploy" },
29
33
  ota: { version: 1, milestone: "deploy", name: "OTA" },
34
+ "desktop-ota": { version: 1, milestone: "deploy", name: "Desktop OTA" },
30
35
  } as const satisfies Record<string, { version: number; milestone: PipelineMilestone; name: string }>;
31
36
  export type PipelineActionName = keyof typeof pipelineActions;
32
37
  export const pipelineActionNames = /* @__PURE__ */ Object.keys(pipelineActions) as readonly PipelineActionName[];
33
38
 
34
- /** The phones an `ota` action packs for; a desktop app updates itself whole, outside a pipeline. */
39
+ /** The phones an `ota` action packs for; a desktop app's update is the `desktop-ota` action's. */
35
40
  export const pipelineOtaPlatforms = ["ios", "android"] as const;
36
41
  export type PipelineOtaPlatform = (typeof pipelineOtaPlatforms)[number];
37
42
 
43
+ export const pipelineDesktopOtaSystems = ["macos", "windows", "linux"] as const;
44
+ export type PipelineDesktopOtaSystem = (typeof pipelineDesktopOtaSystems)[number];
45
+ export const pipelineDesktopOtaArchs = ["arm64", "x64"] as const;
46
+ export type PipelineDesktopOtaArch = (typeof pipelineDesktopOtaArchs)[number];
47
+
48
+ /**
49
+ * One desktop build a `desktop-ota` action packs for. A desktop app is packed only on its own OS, so `os` names the
50
+ * build host the executor needs and `arch` is the pack's `--arch`. A macOS app is arm64 only.
51
+ */
52
+ export interface PipelineDesktopOtaPlatform {
53
+ os: PipelineDesktopOtaSystem;
54
+ arch: PipelineDesktopOtaArch;
55
+ }
56
+
38
57
  /**
39
58
  * Where a phase came from. Workspace `before`/`after` stages become a phase each; app stages fill the slots around the
40
- * three action phases, the k-th custom stage of every app in a slot sharing one phase (`after-build`, `after-build-2`…).
59
+ * action phases, the k-th custom stage of every app in a slot sharing one phase (`after-build`, `after-build-2`…).
41
60
  */
42
61
  export const pipelinePlanSlots = [
43
62
  "before",
@@ -50,6 +69,8 @@ export const pipelinePlanSlots = [
50
69
  "after-deploy",
51
70
  "ota",
52
71
  "after-ota",
72
+ "desktop-ota",
73
+ "after-desktop-ota",
53
74
  "after",
54
75
  ] as const;
55
76
  export type PipelinePlanSlot = (typeof pipelinePlanSlots)[number];
@@ -163,7 +184,7 @@ interface PipelinePlanActionStepBase extends PipelinePlanStepBase {
163
184
  }
164
185
 
165
186
  export interface PipelinePlanAppActionStep extends PipelinePlanActionStepBase {
166
- action: Exclude<PipelineActionName, "ota">;
187
+ action: Exclude<PipelineActionName, "ota" | "desktop-ota">;
167
188
  with: { app: string };
168
189
  }
169
190
 
@@ -173,7 +194,13 @@ export interface PipelinePlanOtaStep extends PipelinePlanActionStepBase {
173
194
  with: { app: string; targets: string[]; platforms: PipelineOtaPlatform[] };
174
195
  }
175
196
 
176
- export type PipelinePlanActionStep = PipelinePlanAppActionStep | PipelinePlanOtaStep;
197
+ /** One pack per target and platform entry, each on a build host of the entry's OS; the targets are as `ota`'s. */
198
+ export interface PipelinePlanDesktopOtaStep extends PipelinePlanActionStepBase {
199
+ action: "desktop-ota";
200
+ with: { app: string; targets: string[]; platforms: PipelineDesktopOtaPlatform[] };
201
+ }
202
+
203
+ export type PipelinePlanActionStep = PipelinePlanAppActionStep | PipelinePlanOtaStep | PipelinePlanDesktopOtaStep;
177
204
 
178
205
  export type PipelinePlanStep = PipelinePlanShellStep | PipelinePlanActionStep;
179
206
 
package/index.ts CHANGED
@@ -157,7 +157,8 @@ export interface AkanNativeDeepLinks {
157
157
  /**
158
158
  * Where an installed app looks for newer releases of itself (the native runtime's updates plugin): the whole app on a
159
159
  * desktop, the web bundle on a phone. `akan publish-update` writes and signs a release with a key on this machine;
160
- * `akan pack-update` writes an unsigned phone bundle for whoever holds the key to sign.
160
+ * `akan pack-update` writes an unsigned pack, a phone's web bundle or a desktop app's archive, for whoever holds the
161
+ * key to sign.
161
162
  */
162
163
  export interface AkanNativeUpdatesConfig {
163
164
  /**
@@ -167,8 +168,8 @@ export interface AkanNativeUpdatesConfig {
167
168
  url: string;
168
169
  /**
169
170
  * The keys a binary trusts, raw 32-byte Ed25519 public keys in base64 (`akan update-keygen` prints one), 1 to 3:
170
- * the online signing key first, offline spares after it, since a key may revoke only keys listed before it. A desktop
171
- * app built before protocol v2 trusts the first.
171
+ * the online signing key first, offline spares after it, since a key may revoke only keys listed before it. A phone
172
+ * and a desktop app trust all of them; only a desktop app built before protocol v2 (akanjs 3.1.1) trusts the first.
172
173
  */
173
174
  publicKeys?: string[];
174
175
  /** The one-key form of `publicKeys`, never beside it: a target overriding native.updates' keys keeps its form. */
@@ -187,8 +188,9 @@ export interface AkanNativeUpdatesConfig {
187
188
  /** How long a newly applied release has to call notifyReady() before it is rolled back, ms. Default 10000. */
188
189
  readyTimeout?: number;
189
190
  /**
190
- * Where a phone posts what became of each release (downloaded, applied, confirmed, rolled back, failed) at its next
191
- * launch or return to the foreground, so rollback rates are visible. Left out, nothing is sent.
191
+ * Where an app posts what became of each release (downloaded, applied, confirmed, rolled back, failed), so rollback
192
+ * rates are visible: a phone at its next launch or return to the foreground, a desktop app at launch and with each
193
+ * check, with its CPU and the key that signed each release. Left out, nothing is sent.
192
194
  */
193
195
  report?: { url: string };
194
196
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.1-rc.0",
3
+ "version": "3.4.0-rc.1",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/pipelineConfig.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  PipelineActionName,
3
+ PipelineDesktopOtaPlatform,
3
4
  PipelineMilestone,
4
5
  PipelineOtaPlatform,
5
6
  PipelinePlanContext,
@@ -62,7 +63,7 @@ export interface PipelineShellStageInput extends PipelineStageBase {
62
63
 
63
64
  /** No action parameters yet: rollout wait and prepull stay the executor's settings. */
64
65
  export interface PipelineActionStageInput extends PipelineStageBase {
65
- use: Exclude<PipelineActionName, "ota">;
66
+ use: Exclude<PipelineActionName, "ota" | "desktop-ota">;
66
67
  name?: string;
67
68
  retry?: number;
68
69
  targets?: never;
@@ -91,7 +92,29 @@ export interface PipelineOtaStageInput extends PipelineStageBase {
91
92
  matrix?: never;
92
93
  }
93
94
 
94
- export type AppPipelineStageInput = PipelineActionStageInput | PipelineOtaStageInput | PipelineShellStageInput;
95
+ /**
96
+ * A desktop app's update, packed whole per target and platform entry for the executor to sign and publish. Every target
97
+ * named must declare `native.updates`.
98
+ */
99
+ export interface PipelineDesktopOtaStageInput extends PipelineStageBase {
100
+ use: "desktop-ota";
101
+ name?: string;
102
+ retry?: number;
103
+ /** Default: every native target that declares `native.updates`. */
104
+ targets?: readonly string[];
105
+ /** No default: each entry is packed on a build host of its own OS. A macOS app is arm64 only. */
106
+ platforms: readonly PipelineDesktopOtaPlatform[];
107
+ sh?: never;
108
+ steps?: never;
109
+ parallel?: never;
110
+ matrix?: never;
111
+ }
112
+
113
+ export type AppPipelineStageInput =
114
+ | PipelineActionStageInput
115
+ | PipelineOtaStageInput
116
+ | PipelineDesktopOtaStageInput
117
+ | PipelineShellStageInput;
95
118
 
96
119
  export interface AppPipelineInput {
97
120
  schemaVersion?: 1;
package/server/console.ts CHANGED
@@ -49,7 +49,7 @@ const createAkanConsoleLogCommands = (appName: string, runtimeDir: string): Reco
49
49
  });
50
50
  } catch (error) {
51
51
  if (error instanceof LogControlUnavailableError) {
52
- session.write(`${appName} is not running (no akan-control.sock in ${runtimeDir})\n`);
52
+ session.write(`${appName} is not running (no log control socket at ${socketPath})\n`);
53
53
  return null;
54
54
  }
55
55
  throw error;
@@ -5,6 +5,7 @@ import type { AkanChildRole, AkanIpcMessage } from "akanjs/service";
5
5
  import { RestartSchedule } from "../../common/restartSchedule";
6
6
  import { CrossSiteGuard } from "../../signal/CrossSiteGuard";
7
7
  import type { BuilderMessage } from "../artifact";
8
+ import { UnixSocketPath, UnixSocketPathTooLongError } from "../lifecycle/unixSocketPath";
8
9
  import { ChildOutputReader } from "../logging/childOutputReader";
9
10
  import type { DevHostChannel } from "./devHostChannel";
10
11
  import type { ChildState, GatewayChildren } from "./gatewayChildren";
@@ -50,14 +51,26 @@ export class ChildSupervisor {
50
51
  }
51
52
 
52
53
  async prepareRuntimeDir() {
53
- await mkdir(this.#config.runtimeDir, { recursive: true });
54
+ const { runtimeDir, childSocketDir, replica } = this.#config;
55
+ try {
56
+ await UnixSocketPath.prepare(runtimeDir, this.#config.childSocketName(replica.total - 1));
57
+ } catch (error) {
58
+ if (error instanceof UnixSocketPathTooLongError) {
59
+ this.#logger.error(error.message);
60
+ this.#host.reportBackendBuildStatus({ ok: false, message: error.message });
61
+ }
62
+ throw error;
63
+ }
64
+ await mkdir(runtimeDir, { recursive: true });
54
65
 
55
- const entries = await readdir(this.#config.runtimeDir).catch(() => []);
56
- await Promise.all(
57
- entries
58
- .filter((name) => /^akan-(child-.*|control)\.sock$/.test(name))
59
- .map((name) => rm(path.join(this.#config.runtimeDir, name), { force: true }).catch(() => undefined)),
60
- );
66
+ for (const dir of new Set([runtimeDir, childSocketDir])) {
67
+ const entries = await readdir(dir).catch(() => []);
68
+ await Promise.all(
69
+ entries
70
+ .filter((name) => /^akan-(child-.*|control)\.sock$/.test(name))
71
+ .map((name) => rm(path.join(dir, name), { force: true }).catch(() => undefined)),
72
+ );
73
+ }
61
74
  }
62
75
 
63
76
  spawn(idx: number) {
@@ -5,6 +5,7 @@ import { getApiPrefix, getWsPrefix, normalizeRoutePrefix } from "../../base/base
5
5
  import type { RestartPolicy } from "../../common/restartSchedule";
6
6
  import { ClientReportId } from "../clientReportId";
7
7
  import { resolveRuntimeDir } from "../lifecycle/runtimeDir";
8
+ import { UnixSocketPath } from "../lifecycle/unixSocketPath";
8
9
  import { getWebConfigFromEnv } from "../types";
9
10
 
10
11
  export interface AkanAppOptions {
@@ -58,7 +59,8 @@ export class GatewayConfig {
58
59
  readonly artifactDir: string;
59
60
  readonly replica: AkanReplicaConfig;
60
61
  readonly runtimeDir: string;
61
- readonly socketRunId = `${process.pid}-${Date.now().toString(36)}`;
62
+ readonly socketRunId = randomBytes(4).toString("hex");
63
+ readonly childSocketDir: string;
62
64
  readonly port: number;
63
65
  readonly wsBasePort: number;
64
66
  readonly openapi?: boolean;
@@ -86,6 +88,7 @@ export class GatewayConfig {
86
88
  this.disableModules = options.disableModules ?? [];
87
89
  this.disableLibs = options.disableLibs ?? [];
88
90
  this.solo = GatewayConfig.#resolveSolo(options, this.replica);
91
+ this.childSocketDir = UnixSocketPath.dirFor(this.runtimeDir, this.childSocketName(this.replica.total - 1));
89
92
  }
90
93
 
91
94
  static #resolveSolo(options: AkanAppOptions, replica: AkanReplicaConfig) {
@@ -174,9 +177,13 @@ export class GatewayConfig {
174
177
  return "all";
175
178
  }
176
179
 
180
+ childSocketName(idx: number) {
181
+ return `akan-child-${this.socketRunId}-${String(idx).padStart(2, "0")}.sock`;
182
+ }
183
+
177
184
  childUpstreamOf(idx: number, role: AkanChildRole): GatewayUpstream {
178
185
  return {
179
- http: { type: "unix", socketPath: path.join(this.runtimeDir, `akan-child-${this.socketRunId}-${idx}.sock`) },
186
+ http: { type: "unix", socketPath: path.join(this.childSocketDir, this.childSocketName(idx)) },
180
187
  ws: role === "batch" ? undefined : { type: "tcp", host: "127.0.0.1", port: this.wsBasePort + idx },
181
188
  };
182
189
  }
@@ -0,0 +1,79 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdir } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import path from "node:path";
5
+ import { AkanError } from "akanjs/base";
6
+
7
+ export interface UnixSocketSite {
8
+ platform?: NodeJS.Platform;
9
+ limit?: number | null;
10
+ tempDir?: string;
11
+ }
12
+
13
+ export class UnixSocketPathTooLongError extends AkanError<"unixSocketPathTooLong"> {
14
+ readonly socketPath: string;
15
+ readonly length: number;
16
+ readonly limit: number;
17
+
18
+ constructor(socketPath: string, limit: number, platform: NodeJS.Platform) {
19
+ const length = UnixSocketPath.lengthOf(socketPath);
20
+ const system = platform === "win32" ? "Windows" : platform;
21
+ super(
22
+ "unixSocketPathTooLong",
23
+ `The unix socket path ${socketPath} is ${length} bytes long, and ${system} allows at most ${limit}, so nothing could listen on it. Move the runtime folder (AKAN_RUNTIME_DIR) or the temp folder its sockets fall back to (TEMP on Windows) to a shorter path.`,
24
+ );
25
+ this.name = "UnixSocketPathTooLongError";
26
+ this.socketPath = socketPath;
27
+ this.length = length;
28
+ this.limit = limit;
29
+ }
30
+ }
31
+
32
+ export class UnixSocketPath {
33
+
34
+ static limitOf(platform: NodeJS.Platform = process.platform): number | null {
35
+ return platform === "win32" ? 107 : null;
36
+ }
37
+
38
+ static lengthOf(socketPath: string) {
39
+ return Buffer.byteLength(socketPath);
40
+ }
41
+
42
+ static fallbackDirOf(runtimeDir: string, { platform = process.platform, tempDir }: UnixSocketSite = {}) {
43
+
44
+ const key = platform === "win32" ? runtimeDir.toLowerCase() : runtimeDir;
45
+ const hash = createHash("sha256").update(key).digest("hex").slice(0, 12);
46
+ const temp = tempDir ?? ((platform !== "win32" && process.env.XDG_RUNTIME_DIR) || tmpdir());
47
+ return UnixSocketPath.#join(platform, temp, `akan-${hash}`);
48
+ }
49
+
50
+ static dirFor(runtimeDir: string, name: string, site: UnixSocketSite = {}) {
51
+ const fits = UnixSocketPath.#limitPassed(UnixSocketPath.#join(site.platform, runtimeDir, name), site) === null;
52
+ return fits ? runtimeDir : UnixSocketPath.fallbackDirOf(runtimeDir, site);
53
+ }
54
+
55
+ static pathFor(runtimeDir: string, name: string, site: UnixSocketSite = {}) {
56
+ return UnixSocketPath.#join(site.platform, UnixSocketPath.dirFor(runtimeDir, name, site), name);
57
+ }
58
+
59
+ static assertFits(socketPath: string, site: UnixSocketSite = {}) {
60
+ const limit = UnixSocketPath.#limitPassed(socketPath, site);
61
+ if (limit !== null) throw new UnixSocketPathTooLongError(socketPath, limit, site.platform ?? process.platform);
62
+ }
63
+
64
+ static async prepare(runtimeDir: string, name: string) {
65
+ const dir = UnixSocketPath.dirFor(runtimeDir, name);
66
+ const socketPath = path.join(dir, name);
67
+ UnixSocketPath.assertFits(socketPath);
68
+ await mkdir(dir, { recursive: true, ...(dir === runtimeDir ? {} : { mode: 0o700 }) });
69
+ return socketPath;
70
+ }
71
+
72
+ static #limitPassed(socketPath: string, { platform, limit = UnixSocketPath.limitOf(platform) }: UnixSocketSite) {
73
+ return limit !== null && UnixSocketPath.lengthOf(socketPath) > limit ? limit : null;
74
+ }
75
+
76
+ static #join(platform = process.platform, ...parts: string[]) {
77
+ return (platform === "win32" ? path.win32 : path.posix).join(...parts);
78
+ }
79
+ }
@@ -1,6 +1,6 @@
1
- import { chmod, mkdir, rm } from "node:fs/promises";
2
- import path from "node:path";
1
+ import { chmod, rm } from "node:fs/promises";
3
2
  import { Logger } from "akanjs/common";
3
+ import { UnixSocketPath } from "../lifecycle/unixSocketPath";
4
4
  import type { LogHub, LogHubCoverage, LogHubEntry } from "./logHub";
5
5
  import { type LogQuery, LogQueryMatcher } from "./logQuery";
6
6
 
@@ -34,10 +34,11 @@ export class LogControlSocket {
34
34
  static readonly #turns = new Map<string, Promise<void>>();
35
35
 
36
36
  static pathIn(runtimeDir: string) {
37
- return path.join(runtimeDir, LogControlSocket.fileName);
37
+ return UnixSocketPath.pathFor(runtimeDir, LogControlSocket.fileName);
38
38
  }
39
39
 
40
40
  readonly path: string;
41
+ readonly #runtimeDir: string;
41
42
  readonly #hub: LogHub;
42
43
  readonly #logger = new Logger("LogControlSocket");
43
44
  #server: ReturnType<typeof Bun.listen<ClientState>> | null = null;
@@ -45,6 +46,7 @@ export class LogControlSocket {
45
46
 
46
47
  constructor(hub: LogHub, runtimeDir: string) {
47
48
  this.#hub = hub;
49
+ this.#runtimeDir = runtimeDir;
48
50
  this.path = LogControlSocket.pathIn(runtimeDir);
49
51
  }
50
52
 
@@ -88,7 +90,7 @@ export class LogControlSocket {
88
90
  }
89
91
 
90
92
  async #listen(generation: number) {
91
- await mkdir(path.dirname(this.path), { recursive: true });
93
+ await UnixSocketPath.prepare(this.#runtimeDir, LogControlSocket.fileName);
92
94
 
93
95
  if (await LogControlSocket.#answers(this.path)) throw new Error("another server is listening on it");
94
96
  if (this.#abandoned(generation)) return;
@@ -370,17 +370,18 @@ export class AgentLoop {
370
370
 
371
371
  const verdicts: Verdict[] = [];
372
372
  for (const call of calls) verdicts.push(await this.#verdictOf(call, byName.get(call.name)));
373
- const deferred = calls.filter((_, at) => verdicts[at] === "defer");
374
- if (deferred.length) {
375
- for (const call of deferred) yield { type: "awaiting", call };
376
- return "awaiting";
377
- }
373
+ const held: AgentWireToolCall[] = [];
374
+ let park: AgentLoopEvent | null = null;
378
375
  const results: AgentWireToolResult[] = [];
379
376
  let stop: AgentLoopStop | null = null;
380
377
  try {
381
378
  for (const [at, call] of calls.entries()) {
382
379
  if (call.argsError) continue;
383
380
  const verdict = verdicts[at] ?? { deny: AgentLoop.unjudged };
381
+ if (verdict === "defer") {
382
+ held.push(call);
383
+ continue;
384
+ }
384
385
  const asked = this.#askUser.isMeta(call.name);
385
386
  const meta = asked || this.#toolkits.isMeta(call.name);
386
387
  stop = this.#haltStop() ?? (verdict === "allow" && !meta ? this.#callBudgetStop() : null);
@@ -388,22 +389,19 @@ export class AgentLoop {
388
389
  const unasked = asked ? LoopAsk.refusalOf(call) : null;
389
390
 
390
391
  if (asked && !unasked) {
391
- yield { type: "awaiting", call, question: true };
392
- stop = "awaiting";
392
+ park = { type: "awaiting", call, question: true };
393
393
  break;
394
394
  }
395
395
  if (verdict === "allow" && this.#lent.has(call.name)) {
396
396
  this.#toolCalls += 1;
397
- yield { type: "awaiting", call, client: true };
398
- stop = "awaiting";
397
+ park = { type: "awaiting", call, client: true };
399
398
  break;
400
399
  }
401
400
  const kit = this.#toolkits.externalOf(call);
402
401
  const opened = kit ? await this.#openRemote(call, kit) : null;
403
402
 
404
403
  if (kit && !opened) {
405
- yield { type: "awaiting", call, connect: kit };
406
- stop = "awaiting";
404
+ park = { type: "awaiting", call, connect: kit };
407
405
  break;
408
406
  }
409
407
  const whole =
@@ -424,7 +422,16 @@ export class AgentLoop {
424
422
 
425
423
  await this.#record(results);
426
424
  }
427
- return stop;
425
+ stop ??= this.#haltStop();
426
+ if (stop) return stop;
427
+
428
+ if (held.length) {
429
+ for (const call of held) yield { type: "awaiting", call };
430
+ return "awaiting";
431
+ }
432
+ if (!park) return null;
433
+ yield park;
434
+ return "awaiting";
428
435
  }
429
436
 
430
437
  async *#accept(answer: AgentLoopCallAnswer): Events<void> {
@@ -22,12 +22,18 @@ export class AnthropicTools {
22
22
  return {
23
23
  name: tool.name,
24
24
  ...(tool.description ? { description: tool.description } : {}),
25
-
26
- input_schema: tool.parameters ?? { type: "object", properties: {} },
25
+ input_schema: AnthropicTools.#objectSchema(tool.parameters),
27
26
  ...(deferred ? { defer_loading: true as const } : {}),
28
27
  };
29
28
  }
30
29
 
30
+ static #objectSchema(parameters: Record<string, unknown> | undefined): Record<string, unknown> {
31
+ if (!parameters) return { type: "object", properties: {} };
32
+ if (parameters.type !== undefined) return parameters;
33
+ const { type: _untyped, ...schema } = parameters;
34
+ return { type: "object", properties: {}, ...schema };
35
+ }
36
+
31
37
  static declared({ tools, deferred = [] }: Pick<LlmTurnRequest, "tools" | "deferred">): AgentWireTool[] {
32
38
  const seen = new Set<string>();
33
39
  return [...tools, ...deferred].filter((tool) => !seen.has(tool.name) && !!seen.add(tool.name));
@@ -51,7 +51,7 @@ export { PipelinePlanSchema } from "./PipelinePlanSchema.d.ts";
51
51
  export { type PipelineValueProblem, PipelineValueReader } from "./PipelineValueReader.d.ts";
52
52
  export { parseSearchParams } from "./parseSearchParams.d.ts";
53
53
  export { formatPhone, isPhoneNumber } from "./phone.d.ts";
54
- export { type PipelineActionName, type PipelineDiagnostic, type PipelineMilestone, type PipelineOtaPlatform, type PipelinePlan, type PipelinePlanActionStep, type PipelinePlanAppActionStep, type PipelinePlanContext, type PipelinePlanOtaStep, type PipelinePlanPhase, type PipelinePlanPost, type PipelinePlanResult, type PipelinePlanRetry, type PipelinePlanShellStep, type PipelinePlanSlot, type PipelinePlanSource, type PipelinePlanStage, type PipelinePlanStep, type PipelineSkip, type PipelineTrigger, pipelineActionNames, pipelineActions, pipelineMilestones, pipelineOtaPlatforms, pipelinePlanLimits, pipelinePlanSlots, pipelinePlanVersion, pipelineReservedEnv, pipelineRuntimeEnv, pipelineTriggers, } from "./pipelinePlan.d.ts";
54
+ export { type PipelineActionName, type PipelineDesktopOtaArch, type PipelineDesktopOtaPlatform, type PipelineDesktopOtaSystem, type PipelineDiagnostic, type PipelineMilestone, type PipelineOtaPlatform, type PipelinePlan, type PipelinePlanActionStep, type PipelinePlanAppActionStep, type PipelinePlanContext, type PipelinePlanDesktopOtaStep, type PipelinePlanOtaStep, type PipelinePlanPhase, type PipelinePlanPost, type PipelinePlanResult, type PipelinePlanRetry, type PipelinePlanShellStep, type PipelinePlanSlot, type PipelinePlanSource, type PipelinePlanStage, type PipelinePlanStep, type PipelineSkip, type PipelineTrigger, pipelineActionNames, pipelineActions, pipelineDesktopOtaArchs, pipelineDesktopOtaSystems, pipelineMilestones, pipelineOtaPlatforms, pipelinePlanLimits, pipelinePlanSlots, pipelinePlanVersion, pipelineReservedEnv, pipelineRuntimeEnv, pipelineTriggers, } from "./pipelinePlan.d.ts";
55
55
  export { type RouteLayer, RouteLayering } from "./RouteLayering.d.ts";
56
56
  export { randomPick, randomPicks } from "./randomPick.d.ts";
57
57
  export { RemoteAuthCode } from "./remoteAuthCode.d.ts";