@openpalm/lib 0.11.0-beta.9 → 0.11.0-rc.18

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 (66) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/control-plane/akm-sources.test.ts +206 -0
  4. package/src/control-plane/akm-sources.ts +234 -0
  5. package/src/control-plane/akm-user-env.test.ts +142 -0
  6. package/src/control-plane/akm-user-env.ts +167 -0
  7. package/src/control-plane/backup.ts +14 -5
  8. package/src/control-plane/channels.ts +48 -29
  9. package/src/control-plane/cleanup-guardrails.test.ts +1 -1
  10. package/src/control-plane/compose-args.test.ts +69 -30
  11. package/src/control-plane/compose-args.ts +62 -8
  12. package/src/control-plane/config-persistence.ts +102 -136
  13. package/src/control-plane/core-assets.ts +45 -60
  14. package/src/control-plane/defaults.ts +16 -0
  15. package/src/control-plane/docker.ts +15 -14
  16. package/src/control-plane/env.test.ts +10 -10
  17. package/src/control-plane/env.ts +16 -1
  18. package/src/control-plane/extends-support.test.ts +8 -8
  19. package/src/control-plane/fs-atomic.ts +15 -0
  20. package/src/control-plane/home.ts +34 -46
  21. package/src/control-plane/host-akm-sharing.test.ts +145 -0
  22. package/src/control-plane/host-akm-sharing.ts +129 -0
  23. package/src/control-plane/host-opencode.test.ts +82 -10
  24. package/src/control-plane/host-opencode.ts +42 -13
  25. package/src/control-plane/install-edge-cases.test.ts +100 -136
  26. package/src/control-plane/install-lock.ts +7 -7
  27. package/src/control-plane/lifecycle.ts +45 -40
  28. package/src/control-plane/markdown-task.ts +30 -50
  29. package/src/control-plane/migrations.test.ts +272 -0
  30. package/src/control-plane/migrations.ts +423 -0
  31. package/src/control-plane/opencode-client.ts +1 -1
  32. package/src/control-plane/paths.ts +61 -46
  33. package/src/control-plane/profile-ids.ts +21 -0
  34. package/src/control-plane/provider-models.ts +3 -3
  35. package/src/control-plane/registry.test.ts +107 -90
  36. package/src/control-plane/registry.ts +301 -110
  37. package/src/control-plane/rollback.ts +8 -38
  38. package/src/control-plane/scheduler.ts +10 -7
  39. package/src/control-plane/secret-audit.test.ts +159 -0
  40. package/src/control-plane/secret-audit.ts +255 -0
  41. package/src/control-plane/secret-mappings.ts +2 -2
  42. package/src/control-plane/secrets-files.test.ts +99 -0
  43. package/src/control-plane/secrets-files.ts +113 -0
  44. package/src/control-plane/secrets.ts +113 -86
  45. package/src/control-plane/setup-config.schema.json +1 -1
  46. package/src/control-plane/setup-status.ts +6 -11
  47. package/src/control-plane/setup.test.ts +137 -61
  48. package/src/control-plane/setup.ts +82 -63
  49. package/src/control-plane/skeleton-guardrail.test.ts +66 -56
  50. package/src/control-plane/spec-to-env.test.ts +63 -26
  51. package/src/control-plane/spec-to-env.ts +51 -14
  52. package/src/control-plane/task-files.test.ts +45 -0
  53. package/src/control-plane/task-files.ts +51 -0
  54. package/src/control-plane/types.ts +2 -4
  55. package/src/control-plane/ui-assets.test.ts +333 -0
  56. package/src/control-plane/ui-assets.ts +290 -142
  57. package/src/control-plane/validate.ts +13 -15
  58. package/src/index.ts +96 -26
  59. package/src/control-plane/akm-vault.test.ts +0 -105
  60. package/src/control-plane/akm-vault.ts +0 -311
  61. package/src/control-plane/core-assets.test.ts +0 -104
  62. package/src/control-plane/migrate-0110.test.ts +0 -177
  63. package/src/control-plane/migrate-0110.ts +0 -99
  64. package/src/control-plane/registry-components.test.ts +0 -391
  65. package/src/control-plane/stack-spec.test.ts +0 -94
  66. package/src/control-plane/stack-spec.ts +0 -67
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  export {
11
11
  LLM_PROVIDERS,
12
12
  EMBEDDING_DIMS,
13
+ PROVIDER_KEY_MAP,
13
14
  lookupEmbeddingDims,
14
15
  } from "./provider-constants.js";
15
16
 
@@ -38,6 +39,15 @@ export {
38
39
  backupOpenPalmHome,
39
40
  } from "./control-plane/backup.js";
40
41
 
42
+ // ── Layout migration harness ────────────────────────────────────────────────
43
+ export {
44
+ ensureMigrated,
45
+ MigrationError,
46
+ CURRENT_LAYOUT_VERSION,
47
+ LAYOUT_VERSION_KEY,
48
+ } from "./control-plane/migrations.js";
49
+ export type { MigrationReport } from "./control-plane/migrations.js";
50
+
41
51
  // ── Registry Catalog ─────────────────────────────────────────────────────
42
52
  export type {
43
53
  AddonMutationResult,
@@ -75,8 +85,7 @@ export {
75
85
  resolveConfigDir,
76
86
  resolveStashDir,
77
87
  resolveWorkspaceDir,
78
- resolveCacheDir,
79
- resolveStateDir,
88
+ resolveDataDir,
80
89
  resolveStackDir,
81
90
  resolveLogsDir,
82
91
  ensureHomeDirs,
@@ -109,12 +118,51 @@ export {
109
118
  updateSecretsEnv,
110
119
  writeAuthJsonProviderKeys,
111
120
  readStackEnv,
121
+ readStackSecretEnv,
122
+ readStackRuntimeEnv,
123
+ writeStackSecretEnv,
112
124
  patchSecretsEnvFile,
113
125
  maskSecretValue,
114
126
  ensureOpenCodeConfig,
127
+ assertNoSecretLikeStackEnvKeys,
115
128
  } from "./control-plane/secrets.js";
116
- export { migrateAuth0110 } from "./control-plane/migrate-0110.js";
117
- export type { MigrateAuth0110Result } from "./control-plane/migrate-0110.js";
129
+ export {
130
+ resolveSecretsDir,
131
+ secretPath,
132
+ readSecret,
133
+ writeSecret,
134
+ ensureSecret,
135
+ removeSecret,
136
+ listSecretNames,
137
+ assertSafeSecretFilename,
138
+ listSecretFiles,
139
+ readSecretFile,
140
+ writeSecretFile,
141
+ removeSecretFile,
142
+ } from './control-plane/secrets-files.js';
143
+ export type { SecretFileInfo } from './control-plane/secrets-files.js';
144
+ export {
145
+ assertSafeTaskFilename,
146
+ resolveTasksDir,
147
+ listTaskFiles,
148
+ readTaskFile,
149
+ writeTaskFile,
150
+ removeTaskFile,
151
+ } from './control-plane/task-files.js';
152
+ export type { TaskFileInfo } from './control-plane/task-files.js';
153
+ export type {
154
+ SecretAuditIssue,
155
+ SecretAuditOptions,
156
+ SecretAuditResult,
157
+ SecretAuditSeverity,
158
+ } from "./control-plane/secret-audit.js";
159
+ export {
160
+ auditComposeSecrets,
161
+ auditFileBasedSecrets,
162
+ auditSecretFilesystem,
163
+ auditStackEnv,
164
+ isSecretLikeKey,
165
+ } from "./control-plane/secret-audit.js";
118
166
  // ── Setup Status ────────────────────────────────────────────────────────
119
167
  export {
120
168
  isSetupComplete,
@@ -136,13 +184,35 @@ export {
136
184
  fetchProviderModels,
137
185
  } from "./control-plane/provider-models.js";
138
186
 
187
+ // ── AKM host/assistant source wiring ──────────────────────────────────────
188
+ export {
189
+ HOST_SOURCE_NAME,
190
+ addHostStashToOpenpalmConfig,
191
+ removeHostAkmSource,
192
+ importHostProfiles,
193
+ } from "./control-plane/akm-sources.js";
194
+ export type {
195
+ HostAkmSharingStatus,
196
+ } from "./control-plane/host-akm-sharing.js";
197
+ export {
198
+ enableHostAkmSharing,
199
+ disableHostAkmSharing,
200
+ getHostAkmSharingStatus,
201
+ ensureHostStashEnv,
202
+ isHostAkmAvailable,
203
+ hostAkmStashPath,
204
+ hostAkmConfigPath,
205
+ } from "./control-plane/host-akm-sharing.js";
206
+
207
+ // ── Atomic file write (shared by all control-plane writers) ───────────────
208
+ export { writeFileAtomic } from "./control-plane/fs-atomic.js";
209
+
139
210
  // ── Core Assets ─────────────────────────────────────────────────────────
140
211
  export {
141
212
  ensureCoreCompose,
142
213
  readCoreCompose,
143
214
  ensureOpenCodeSystemConfig,
144
215
  refreshCoreAssets,
145
- seedStashAssets,
146
216
  seedAssistantPersonaFiles,
147
217
  } from "./control-plane/core-assets.js";
148
218
 
@@ -156,8 +226,8 @@ export {
156
226
  buildRuntimeFileMeta,
157
227
  writeRuntimeFiles,
158
228
  writeSystemEnv,
159
- readChannelSecrets,
160
- writeChannelSecrets,
229
+ channelSecretName,
230
+ ensureChannelSecret,
161
231
  ensureComposeVolumeTargets,
162
232
  } from "./control-plane/config-persistence.js";
163
233
 
@@ -230,8 +300,15 @@ export { detectLocalProviders } from "./control-plane/model-runner.js";
230
300
  export {
231
301
  buildComposeOptions,
232
302
  buildComposeCliArgs,
303
+ resolveActiveProfiles,
233
304
  } from "./control-plane/compose-args.js";
234
305
 
306
+ export {
307
+ addonProfileId,
308
+ canonicalAddonProfileSelection,
309
+ resolveHardwareProfileVariant,
310
+ } from "./control-plane/profile-ids.js";
311
+
235
312
  // ── Compose Error Parsing ────────────────────────────────────────────────
236
313
  export type { ComposeServiceFailure } from "./control-plane/compose-errors.js";
237
314
  export {
@@ -239,16 +316,6 @@ export {
239
316
  summarizeComposeStderr,
240
317
  } from "./control-plane/compose-errors.js";
241
318
 
242
- // ── Stack Spec (v2) ──────────────────────────────────────────────────────
243
- export type {
244
- StackSpec,
245
- } from "./control-plane/stack-spec.js";
246
- export {
247
- STACK_SPEC_FILENAME,
248
- writeStackSpec,
249
- readStackSpec,
250
- } from "./control-plane/stack-spec.js";
251
-
252
319
  // ── Spec-to-Env Derivation ──────────────────────────────────────────────
253
320
  export type { VoiceVarsConfig } from "./control-plane/spec-to-env.js";
254
321
  export {
@@ -289,16 +356,19 @@ export {
289
356
  importHostOpenCode,
290
357
  } from "./control-plane/host-opencode.js";
291
358
 
292
- // ── AKM Vault ────────────────────────────────────────────────────────────
359
+ // ── AKM user env (env:user) ──────────────────────────────────────────────
293
360
  export {
294
- AKM_USER_VAULT_REF,
361
+ AKM_USER_ENV_REF,
362
+ AKM_ENV_KEYS,
295
363
  buildAkmEnv,
296
- ensureAkmUserVault,
297
- writeAkmVaultKey,
298
- deleteAkmVaultKey,
299
- readAkmUserVaultFile,
300
- readUserVaultSync,
301
- } from "./control-plane/akm-vault.js";
364
+ assertAkmEnvComplete,
365
+ ensureAkmUserEnv,
366
+ writeUserEnvKey,
367
+ deleteUserEnvKey,
368
+ readUserEnvFile,
369
+ readUserEnvSync,
370
+ userEnvPathSync,
371
+ } from "./control-plane/akm-user-env.js";
302
372
 
303
373
  // ── UI asset seeding and resolution ─────────────────────────────────────────
304
374
  export type { UiBuildUpdateResult } from "./control-plane/ui-assets.js";
@@ -309,5 +379,5 @@ export {
309
379
  resolveUiBuildDir,
310
380
  seedUiBuild,
311
381
  checkAndUpdateUiBuild,
312
- readCurrentUiBuildVersion,
382
+ uiUpdateChannel,
313
383
  } from "./control-plane/ui-assets.js";
@@ -1,105 +0,0 @@
1
- /**
2
- * Tests for the akm vault helpers. The vault helpers spawn `akm vault set
3
- * <ref> <key>` and feed the secret VALUE on stdin (akm-cli >= 0.8.0).
4
- *
5
- * Tests gate on the akm CLI being on PATH so the suite stays green in
6
- * environments without akm installed.
7
- */
8
- import { describe, expect, it, beforeEach, afterEach, mock } from "bun:test";
9
- import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
10
- import { tmpdir } from "node:os";
11
- import { join } from "node:path";
12
- import { execFileSync } from "node:child_process";
13
- import {
14
- ensureAkmUserVault,
15
- readAkmUserVaultFile,
16
- writeAkmVaultKey,
17
- deleteAkmVaultKey,
18
- AKM_USER_VAULT_REF,
19
- } from "./akm-vault.js";
20
- import type { ControlPlaneState } from "./types.js";
21
-
22
- function makeState(homeDir: string): ControlPlaneState {
23
- return {
24
- homeDir,
25
- configDir: join(homeDir, "config"),
26
- stashDir: join(homeDir, "stash"),
27
- workspaceDir: join(homeDir, "workspace"),
28
- cacheDir: join(homeDir, "cache"),
29
- stateDir: join(homeDir, "state"),
30
- stackDir: join(homeDir, "stack"),
31
- services: {},
32
- artifacts: { compose: "" },
33
- artifactMeta: [],
34
- };
35
- }
36
-
37
- function hasAkmCli(): boolean {
38
- try {
39
- execFileSync("akm", ["--version"], { stdio: "ignore" });
40
- return true;
41
- } catch {
42
- return false;
43
- }
44
- }
45
-
46
- const AKM_AVAILABLE = hasAkmCli();
47
-
48
-
49
- describe("writeAkmVaultKey", () => {
50
- let homeDir: string;
51
- let state: ControlPlaneState;
52
-
53
- beforeEach(() => {
54
- homeDir = mkdtempSync(join(tmpdir(), "openpalm-akm-write-"));
55
- state = makeState(homeDir);
56
- mkdirSync(state.stashDir, { recursive: true });
57
- mkdirSync(`${state.stateDir}/cache/akm`, { recursive: true });
58
- });
59
-
60
- afterEach(() => {
61
- rmSync(homeDir, { recursive: true, force: true });
62
- });
63
-
64
- it.skipIf(!AKM_AVAILABLE)("writes a key via `akm vault set` (stdin mode, no argv leak)", async () => {
65
- const value = "argv-free-secret-9988";
66
- const ok = await writeAkmVaultKey(state, "TOKEN", value);
67
- expect(ok).toBe(true);
68
-
69
- const vaultPath = await ensureAkmUserVault(state);
70
- expect(vaultPath).not.toBeNull();
71
- if (vaultPath) {
72
- const stored = readAkmUserVaultFile(vaultPath);
73
- expect(stored.TOKEN).toBe(value);
74
- }
75
- });
76
-
77
- it.skipIf(!AKM_AVAILABLE)("deleteAkmVaultKey removes a key via `akm vault unset`", async () => {
78
- await writeAkmVaultKey(state, "TOKEN_A", "value-a");
79
- await writeAkmVaultKey(state, "TOKEN_B", "value-b");
80
-
81
- const ok = await deleteAkmVaultKey(state, "TOKEN_A");
82
- expect(ok).toBe(true);
83
-
84
- const vaultPath = await ensureAkmUserVault(state);
85
- if (vaultPath) {
86
- const stored = readAkmUserVaultFile(vaultPath);
87
- expect(stored.TOKEN_A).toBeUndefined();
88
- expect(stored.TOKEN_B).toBe("value-b");
89
- }
90
- });
91
-
92
- it.skipIf(!AKM_AVAILABLE)("deleteAkmVaultKey is idempotent on a missing key", async () => {
93
- // Deleting a key that was never set should not throw — `akm vault unset`
94
- // either exits 0 or emits a "not found" message we tolerate.
95
- const ok = await deleteAkmVaultKey(state, "NEVER_SET_KEY");
96
- expect(ok).toBe(true);
97
- });
98
- });
99
-
100
-
101
- describe("AKM_USER_VAULT_REF", () => {
102
- it("exports the canonical akm ref string", () => {
103
- expect(AKM_USER_VAULT_REF).toBe("vault:user");
104
- });
105
- });
@@ -1,311 +0,0 @@
1
- /// <reference types="bun-types" />
2
- /**
3
- * akm `vault:user` helpers.
4
- *
5
- * The akm-cli vault store at `${OP_HOME}/stash/vaults/user.env` is the
6
- * canonical home for user-managed environment secrets. The assistant
7
- * entrypoint sources this file directly at startup.
8
- *
9
- * `stack.env` and `guardian.env` are operator-managed and NOT mirrored
10
- * into akm — mirroring them would break guardian's HMAC env_file
11
- * hot-reload contract.
12
- *
13
- * SECURITY: every write into the akm vault is performed by spawning
14
- * `akm vault set <ref> <key>` with the secret VALUE delivered via stdin
15
- * (akm-cli >= 0.8.0). Values never appear in argv, so they cannot leak
16
- * through `/proc/<pid>/cmdline`. The matching delete path uses
17
- * `akm vault unset <ref> <key>` which is naturally argv-safe.
18
- *
19
- * Layout:
20
- * stash/ — AKM_STASH_DIR: asset content (skills, vaults, knowledge, agents)
21
- * state/akm/ — AKM_DATA_DIR / AKM_STATE_DIR / AKM_CONFIG_DIR: operational metadata
22
- * cache/akm/ — AKM_CACHE_DIR: regenerable registry artifacts
23
- */
24
- import { existsSync, readFileSync } from "node:fs";
25
- import { execFile as execFileCb } from "node:child_process";
26
- import { promisify } from "node:util";
27
- import { parseEnvFile } from "./env.js";
28
- import { createLogger } from "../logger.js";
29
- import type { ControlPlaneState } from "./types.js";
30
-
31
- const execFile = promisify(execFileCb);
32
- const logger = createLogger("akm-vault");
33
-
34
- export const AKM_USER_VAULT_REF = "vault:user";
35
-
36
- /**
37
- * Build the env that points akm at the shared OpenPalm stash. We mirror the
38
- * layout that the assistant/admin containers use (see
39
- * `.openpalm/config/stack/core.compose.yml`) so host-side and container-side
40
- * runs resolve to the same vault file.
41
- *
42
- * AKM_CONFIG_DIR lives in config/ (alongside stack.env, auth.json) so
43
- * operators can inspect and version-control akm setup alongside other config.
44
- * AKM_CACHE_DIR lives in cache/ since registry index and downloaded artifacts
45
- * are regenerable and should not be indexed alongside stash assets.
46
- */
47
- export function buildAkmEnv(state: ControlPlaneState): NodeJS.ProcessEnv {
48
- const akmOperational = `${state.stateDir}/akm`;
49
- return {
50
- ...process.env,
51
- AKM_STASH_DIR: state.stashDir,
52
- AKM_CONFIG_DIR: `${state.configDir}/akm`,
53
- AKM_DATA_DIR: `${akmOperational}/data`,
54
- AKM_STATE_DIR: `${akmOperational}/state`,
55
- AKM_CACHE_DIR: `${state.cacheDir}/akm`,
56
- };
57
- }
58
-
59
- /**
60
- * Per-invocation timeout (ms) for every akm subprocess we launch. The CLI is
61
- * a local binary and these probes (`--version`, `vault create`, `vault path`,
62
- * `vault set/unset`) complete in well under a second on a healthy host;
63
- * anything longer means akm is wedged or unreachable. Bounding the call
64
- * keeps `mirrorUserVaultToAkm` truly best-effort: a stuck akm binary cannot
65
- * block install/upgrade.
66
- *
67
- * Why a wall-clock race instead of execFile's built-in `timeout` option:
68
- * node's `child_process.execFile` in Bun is implemented on top of `Bun.spawn`,
69
- * and its `timeout` option only fires once stdout/stderr are wired up. Test
70
- * suites that stub `Bun.spawn` (e.g. `packages/cli/src/main.test.ts`
71
- * `mockDockerCli`) return a fake child whose stdout never closes, so neither
72
- * the underlying promise nor the timeout option ever resolves. A simple
73
- * `Promise.race` against an unref'd setTimeout converts that failure mode
74
- * into a fast rejection that `akmAvailable` swallows as "akm not on PATH",
75
- * without changing behaviour on real hosts.
76
- */
77
- const AKM_EXEC_TIMEOUT_MS = 2_000;
78
-
79
- /**
80
- * Race a promise against an unref'd setTimeout. If the timeout fires first,
81
- * reject with `<label> timed out after <ms>ms`. The timer is always cleared
82
- * in `finally` so it never keeps the event loop alive past resolution. The
83
- * unref means the timer alone won't block process exit — the surrounding
84
- * subprocess work owns the liveness.
85
- */
86
- function raceWithTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
87
- let timer: ReturnType<typeof setTimeout> | undefined;
88
- const timeoutPromise = new Promise<never>((_, reject) => {
89
- timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
90
- timer.unref?.();
91
- });
92
- return Promise.race([promise, timeoutPromise]).finally(() => {
93
- if (timer) clearTimeout(timer);
94
- });
95
- }
96
-
97
- async function execAkm(args: string[], env: NodeJS.ProcessEnv): Promise<{ stdout: string; stderr: string }> {
98
- return raceWithTimeout(
99
- execFile("akm", args, { env }),
100
- AKM_EXEC_TIMEOUT_MS,
101
- `akm ${args[0] ?? "?"}`,
102
- );
103
- }
104
-
105
- async function akmAvailable(env: NodeJS.ProcessEnv): Promise<boolean> {
106
- try {
107
- await execAkm(["--version"], env);
108
- return true;
109
- } catch {
110
- return false;
111
- }
112
- }
113
-
114
- /**
115
- * Return the absolute path of the akm vault file, creating the vault if
116
- * missing. Callers that have already built the akm env (via `buildAkmEnv`)
117
- * can pass it in to avoid rebuilding — the result is identical either way.
118
- */
119
- export async function ensureAkmUserVault(
120
- state: ControlPlaneState,
121
- env: NodeJS.ProcessEnv = buildAkmEnv(state),
122
- ): Promise<string | null> {
123
- if (!(await akmAvailable(env))) {
124
- return null;
125
- }
126
- try {
127
- // `vault create` accepts only the ref on argv — no secret material crosses
128
- // the process boundary here.
129
- await execAkm(["vault", "create", AKM_USER_VAULT_REF], env);
130
- } catch (err) {
131
- // `create` is documented as a no-op when the vault already exists, but
132
- // some build channels emit a non-zero exit. Probe `path` to distinguish
133
- // a real failure from "already exists".
134
- logger.debug("akm vault create returned non-zero", {
135
- ref: AKM_USER_VAULT_REF,
136
- error: err instanceof Error ? err.message : String(err),
137
- });
138
- }
139
- try {
140
- const { stdout } = await execAkm(["vault", "path", AKM_USER_VAULT_REF], env);
141
- const path = stdout.trim();
142
- return path.length > 0 ? path : null;
143
- } catch (err) {
144
- logger.warn("akm vault path failed", {
145
- ref: AKM_USER_VAULT_REF,
146
- error: err instanceof Error ? err.message : String(err),
147
- });
148
- return null;
149
- }
150
- }
151
-
152
- /**
153
- * Spawn `akm vault set <ref> <key>` and feed the secret VALUE via stdin.
154
- * The value never crosses argv, so it cannot leak through
155
- * `/proc/<pid>/cmdline`. Bounded by AKM_EXEC_TIMEOUT_MS — a stuck akm
156
- * binary cannot block the calling install/upgrade flow.
157
- */
158
- async function akmVaultSetViaStdin(
159
- ref: string,
160
- key: string,
161
- value: string,
162
- env: NodeJS.ProcessEnv,
163
- ): Promise<void> {
164
- // We use Bun.spawn directly because it supports an in-memory stdin pipe
165
- // (a buffer/string stream) without dragging in an extra dependency, and
166
- // because akm-cli on >= 0.8.0 reads the value from stdin when no
167
- // positional `<value>` is provided. (The CLI silently switched stdin to
168
- // the default in commit c50f9f4; explicit `--stdin` is still accepted
169
- // for older binaries — but since we pin akm-cli >= 0.8.0-rc2 across all
170
- // images via Dockerfile ARGs, the implicit form is enough.)
171
- const child = Bun.spawn(["akm", "vault", "set", ref, key], {
172
- env,
173
- stdin: "pipe",
174
- stdout: "pipe",
175
- stderr: "pipe",
176
- });
177
-
178
- // Feed the secret. `child.stdin` is a FileSink in Bun — write+end then
179
- // wait for exit. We don't use `await child.stdin.end(value)` because
180
- // some Bun versions return undefined here; explicit write+end is portable.
181
- if (child.stdin) {
182
- // child.stdin is typed as FileSink in Bun
183
- const sink = child.stdin as { write: (data: string) => unknown; end: () => unknown };
184
- sink.write(value);
185
- sink.end();
186
- }
187
-
188
- // Wall-clock bound — mirror of the execAkm pattern. On timeout we also
189
- // SIGKILL the child so the orphaned subprocess doesn't outlive us.
190
- let exitCode: number;
191
- try {
192
- exitCode = await raceWithTimeout(
193
- child.exited,
194
- AKM_EXEC_TIMEOUT_MS,
195
- `akm vault set ${key}`,
196
- );
197
- } catch (err) {
198
- try { child.kill(); } catch { /* best-effort */ }
199
- throw err;
200
- }
201
-
202
- if (exitCode !== 0) {
203
- const stderrText = child.stderr ? await new Response(child.stderr).text() : "";
204
- throw new Error(`akm vault set ${key} failed (exit ${exitCode}): ${stderrText.trim()}`);
205
- }
206
- }
207
-
208
- /**
209
- * Write a single key/value into the akm `vault:user` store via
210
- * `akm vault set <ref> <key>` with the value delivered on stdin.
211
- *
212
- * Returns `true` on success, `false` when akm is unavailable or the vault
213
- * could not be ensured. Throws on akm subprocess failures (non-zero exit
214
- * with a captured stderr, or wall-clock timeout) so callers can surface
215
- * the real error instead of silently dropping the write.
216
- */
217
- export async function writeAkmVaultKey(
218
- state: ControlPlaneState,
219
- key: string,
220
- value: string,
221
- ): Promise<boolean> {
222
- // Build env once and thread it through both the ensure step and the
223
- // subsequent `akm vault set`. Avoids a redundant `buildAkmEnv` call.
224
- const env = buildAkmEnv(state);
225
- const vaultPath = await ensureAkmUserVault(state, env);
226
- if (!vaultPath) return false;
227
- await akmVaultSetViaStdin(AKM_USER_VAULT_REF, key, value, env);
228
- return true;
229
- }
230
-
231
- /**
232
- * Remove a key from the akm `vault:user` store via `akm vault unset`.
233
- * The key name is a normal identifier and crosses argv only — secret
234
- * values are never involved. Returns `true` if the operation completed
235
- * (whether or not the key was present), `false` when akm is unavailable.
236
- */
237
- export async function deleteAkmVaultKey(
238
- state: ControlPlaneState,
239
- key: string,
240
- ): Promise<boolean> {
241
- // Build env once and pass it into ensureAkmUserVault so we don't pay
242
- // for two `buildAkmEnv` calls on a single delete.
243
- const env = buildAkmEnv(state);
244
- const vaultPath = await ensureAkmUserVault(state, env);
245
- if (!vaultPath) return false;
246
- try {
247
- // --yes: newer akm versions require explicit confirmation for any
248
- // destructive operation in non-interactive mode. Without this flag
249
- // the command exits with NON_INTERACTIVE_REQUIRES_YES and our
250
- // delete looks like a hard failure instead of an idempotent unset.
251
- await execAkm(["vault", "unset", "--yes", AKM_USER_VAULT_REF, key], env);
252
- } catch (err) {
253
- // `unset` of a missing key is a benign no-op; many akm versions exit 0
254
- // anyway. If akm hard-fails (non-zero, non-empty stderr) we surface it.
255
- const message = err instanceof Error ? err.message : String(err);
256
- // Heuristic: tolerate "not found" / "no such" messages so re-running
257
- // delete on an already-deleted key stays idempotent for callers.
258
- if (/not\s*found|no\s+such|does\s+not\s+exist/i.test(message)) {
259
- logger.debug("akm vault unset reported missing key", { key, message });
260
- return true;
261
- }
262
- throw err;
263
- }
264
- return true;
265
- }
266
-
267
- /**
268
- * Synchronously resolve the canonical akm `vault:user` file path for a given
269
- * control-plane state. Used by sync read paths (e.g. plaintext secret backend
270
- * `list`/`exists`) that cannot await `ensureAkmUserVault`.
271
- *
272
- * The path is deterministic: `buildAkmEnv` pins `AKM_STASH_DIR` to
273
- * `state.stashDir`, and akm-cli (>= 0.8.0) materializes vault files
274
- * at `${AKM_STASH_DIR}/vaults/<ref>.env`.
275
- *
276
- * Returns the path string regardless of whether the file currently exists —
277
- * callers should `existsSync` if presence matters.
278
- */
279
- export function akmUserVaultPathSync(state: ControlPlaneState): string {
280
- return `${state.stashDir}/vaults/user.env`;
281
- }
282
-
283
- /**
284
- * Read the user-managed env namespace from the akm `vault:user` store.
285
- *
286
- * Returns `{}` when the vault file does not exist yet. Pure sync — no subprocess spawn.
287
- */
288
- export function readUserVaultSync(state: ControlPlaneState): Record<string, string> {
289
- const akmPath = akmUserVaultPathSync(state);
290
- if (existsSync(akmPath)) {
291
- return readAkmUserVaultFile(akmPath);
292
- }
293
- return {};
294
- }
295
-
296
- /** Return the parsed contents of the akm vault file (public API used by admin UI list endpoint). */
297
- export function readAkmUserVaultFile(vaultPath: string): Record<string, string> {
298
- if (!existsSync(vaultPath)) return {};
299
- try {
300
- return parseEnvFile(vaultPath);
301
- } catch {
302
- // Fallback: hand-parse if dotenv chokes (e.g. file with stray BOM).
303
- const raw = readFileSync(vaultPath, "utf-8");
304
- const out: Record<string, string> = {};
305
- for (const line of raw.split("\n")) {
306
- const m = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
307
- if (m) out[m[1]] = m[2];
308
- }
309
- return out;
310
- }
311
- }