@openpalm/lib 0.11.0-beta.8 → 0.11.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 (63) 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 +67 -30
  11. package/src/control-plane/compose-args.ts +63 -8
  12. package/src/control-plane/config-persistence.ts +95 -136
  13. package/src/control-plane/core-assets.ts +21 -44
  14. package/src/control-plane/docker.ts +15 -14
  15. package/src/control-plane/env.test.ts +10 -10
  16. package/src/control-plane/env.ts +1 -1
  17. package/src/control-plane/extends-support.test.ts +8 -8
  18. package/src/control-plane/fs-atomic.ts +15 -0
  19. package/src/control-plane/home.ts +34 -46
  20. package/src/control-plane/host-akm-sharing.test.ts +145 -0
  21. package/src/control-plane/host-akm-sharing.ts +129 -0
  22. package/src/control-plane/host-opencode.test.ts +82 -10
  23. package/src/control-plane/host-opencode.ts +42 -13
  24. package/src/control-plane/install-edge-cases.test.ts +98 -105
  25. package/src/control-plane/install-lock.ts +7 -7
  26. package/src/control-plane/lifecycle.ts +37 -36
  27. package/src/control-plane/markdown-task.ts +30 -50
  28. package/src/control-plane/opencode-client.ts +1 -1
  29. package/src/control-plane/paths.ts +61 -46
  30. package/src/control-plane/profile-ids.ts +21 -0
  31. package/src/control-plane/provider-models.ts +3 -3
  32. package/src/control-plane/registry.test.ts +107 -90
  33. package/src/control-plane/registry.ts +288 -109
  34. package/src/control-plane/rollback.ts +8 -38
  35. package/src/control-plane/scheduler.ts +10 -7
  36. package/src/control-plane/secret-audit.test.ts +159 -0
  37. package/src/control-plane/secret-audit.ts +255 -0
  38. package/src/control-plane/secret-mappings.ts +2 -2
  39. package/src/control-plane/secrets-files.test.ts +99 -0
  40. package/src/control-plane/secrets-files.ts +113 -0
  41. package/src/control-plane/secrets.ts +113 -86
  42. package/src/control-plane/setup-config.schema.json +1 -1
  43. package/src/control-plane/setup-status.ts +6 -11
  44. package/src/control-plane/setup.test.ts +140 -44
  45. package/src/control-plane/setup.ts +85 -62
  46. package/src/control-plane/skeleton-guardrail.test.ts +64 -55
  47. package/src/control-plane/spec-to-env.test.ts +63 -26
  48. package/src/control-plane/spec-to-env.ts +49 -12
  49. package/src/control-plane/stack-spec.test.ts +15 -11
  50. package/src/control-plane/stack-spec.ts +31 -10
  51. package/src/control-plane/task-files.test.ts +45 -0
  52. package/src/control-plane/task-files.ts +51 -0
  53. package/src/control-plane/types.ts +2 -4
  54. package/src/control-plane/ui-assets.test.ts +130 -0
  55. package/src/control-plane/ui-assets.ts +132 -57
  56. package/src/control-plane/validate.ts +13 -15
  57. package/src/index.ts +86 -16
  58. package/src/control-plane/akm-vault.test.ts +0 -105
  59. package/src/control-plane/akm-vault.ts +0 -311
  60. package/src/control-plane/core-assets.test.ts +0 -104
  61. package/src/control-plane/migrate-0110.test.ts +0 -177
  62. package/src/control-plane/migrate-0110.ts +0 -99
  63. package/src/control-plane/registry-components.test.ts +0 -391
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
 
@@ -75,8 +76,7 @@ export {
75
76
  resolveConfigDir,
76
77
  resolveStashDir,
77
78
  resolveWorkspaceDir,
78
- resolveCacheDir,
79
- resolveStateDir,
79
+ resolveDataDir,
80
80
  resolveStackDir,
81
81
  resolveLogsDir,
82
82
  ensureHomeDirs,
@@ -109,12 +109,51 @@ export {
109
109
  updateSecretsEnv,
110
110
  writeAuthJsonProviderKeys,
111
111
  readStackEnv,
112
+ readStackSecretEnv,
113
+ readStackRuntimeEnv,
114
+ writeStackSecretEnv,
112
115
  patchSecretsEnvFile,
113
116
  maskSecretValue,
114
117
  ensureOpenCodeConfig,
118
+ assertNoSecretLikeStackEnvKeys,
115
119
  } from "./control-plane/secrets.js";
116
- export { migrateAuth0110 } from "./control-plane/migrate-0110.js";
117
- export type { MigrateAuth0110Result } from "./control-plane/migrate-0110.js";
120
+ export {
121
+ resolveSecretsDir,
122
+ secretPath,
123
+ readSecret,
124
+ writeSecret,
125
+ ensureSecret,
126
+ removeSecret,
127
+ listSecretNames,
128
+ assertSafeSecretFilename,
129
+ listSecretFiles,
130
+ readSecretFile,
131
+ writeSecretFile,
132
+ removeSecretFile,
133
+ } from './control-plane/secrets-files.js';
134
+ export type { SecretFileInfo } from './control-plane/secrets-files.js';
135
+ export {
136
+ assertSafeTaskFilename,
137
+ resolveTasksDir,
138
+ listTaskFiles,
139
+ readTaskFile,
140
+ writeTaskFile,
141
+ removeTaskFile,
142
+ } from './control-plane/task-files.js';
143
+ export type { TaskFileInfo } from './control-plane/task-files.js';
144
+ export type {
145
+ SecretAuditIssue,
146
+ SecretAuditOptions,
147
+ SecretAuditResult,
148
+ SecretAuditSeverity,
149
+ } from "./control-plane/secret-audit.js";
150
+ export {
151
+ auditComposeSecrets,
152
+ auditFileBasedSecrets,
153
+ auditSecretFilesystem,
154
+ auditStackEnv,
155
+ isSecretLikeKey,
156
+ } from "./control-plane/secret-audit.js";
118
157
  // ── Setup Status ────────────────────────────────────────────────────────
119
158
  export {
120
159
  isSetupComplete,
@@ -136,13 +175,35 @@ export {
136
175
  fetchProviderModels,
137
176
  } from "./control-plane/provider-models.js";
138
177
 
178
+ // ── AKM host/assistant source wiring ──────────────────────────────────────
179
+ export {
180
+ HOST_SOURCE_NAME,
181
+ addHostStashToOpenpalmConfig,
182
+ removeHostAkmSource,
183
+ importHostProfiles,
184
+ } from "./control-plane/akm-sources.js";
185
+ export type {
186
+ HostAkmSharingStatus,
187
+ } from "./control-plane/host-akm-sharing.js";
188
+ export {
189
+ enableHostAkmSharing,
190
+ disableHostAkmSharing,
191
+ getHostAkmSharingStatus,
192
+ ensureHostStashEnv,
193
+ isHostAkmAvailable,
194
+ hostAkmStashPath,
195
+ hostAkmConfigPath,
196
+ } from "./control-plane/host-akm-sharing.js";
197
+
198
+ // ── Atomic file write (shared by all control-plane writers) ───────────────
199
+ export { writeFileAtomic } from "./control-plane/fs-atomic.js";
200
+
139
201
  // ── Core Assets ─────────────────────────────────────────────────────────
140
202
  export {
141
203
  ensureCoreCompose,
142
204
  readCoreCompose,
143
205
  ensureOpenCodeSystemConfig,
144
206
  refreshCoreAssets,
145
- seedStashAssets,
146
207
  seedAssistantPersonaFiles,
147
208
  } from "./control-plane/core-assets.js";
148
209
 
@@ -156,8 +217,8 @@ export {
156
217
  buildRuntimeFileMeta,
157
218
  writeRuntimeFiles,
158
219
  writeSystemEnv,
159
- readChannelSecrets,
160
- writeChannelSecrets,
220
+ channelSecretName,
221
+ ensureChannelSecret,
161
222
  ensureComposeVolumeTargets,
162
223
  } from "./control-plane/config-persistence.js";
163
224
 
@@ -230,8 +291,15 @@ export { detectLocalProviders } from "./control-plane/model-runner.js";
230
291
  export {
231
292
  buildComposeOptions,
232
293
  buildComposeCliArgs,
294
+ resolveActiveProfiles,
233
295
  } from "./control-plane/compose-args.js";
234
296
 
297
+ export {
298
+ addonProfileId,
299
+ canonicalAddonProfileSelection,
300
+ resolveHardwareProfileVariant,
301
+ } from "./control-plane/profile-ids.js";
302
+
235
303
  // ── Compose Error Parsing ────────────────────────────────────────────────
236
304
  export type { ComposeServiceFailure } from "./control-plane/compose-errors.js";
237
305
  export {
@@ -289,16 +357,19 @@ export {
289
357
  importHostOpenCode,
290
358
  } from "./control-plane/host-opencode.js";
291
359
 
292
- // ── AKM Vault ────────────────────────────────────────────────────────────
360
+ // ── AKM user env (env:user) ──────────────────────────────────────────────
293
361
  export {
294
- AKM_USER_VAULT_REF,
362
+ AKM_USER_ENV_REF,
363
+ AKM_ENV_KEYS,
295
364
  buildAkmEnv,
296
- ensureAkmUserVault,
297
- writeAkmVaultKey,
298
- deleteAkmVaultKey,
299
- readAkmUserVaultFile,
300
- readUserVaultSync,
301
- } from "./control-plane/akm-vault.js";
365
+ assertAkmEnvComplete,
366
+ ensureAkmUserEnv,
367
+ writeUserEnvKey,
368
+ deleteUserEnvKey,
369
+ readUserEnvFile,
370
+ readUserEnvSync,
371
+ userEnvPathSync,
372
+ } from "./control-plane/akm-user-env.js";
302
373
 
303
374
  // ── UI asset seeding and resolution ─────────────────────────────────────────
304
375
  export type { UiBuildUpdateResult } from "./control-plane/ui-assets.js";
@@ -309,5 +380,4 @@ export {
309
380
  resolveUiBuildDir,
310
381
  seedUiBuild,
311
382
  checkAndUpdateUiBuild,
312
- readCurrentUiBuildVersion,
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
- }
@@ -1,104 +0,0 @@
1
- import { describe, expect, it, beforeEach, afterEach } from "bun:test";
2
- import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync, readFileSync, existsSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { seedStashAssets } from "./core-assets.js";
6
-
7
- describe("seedStashAssets", () => {
8
- let homeDir: string;
9
- const originalHome = process.env.OP_HOME;
10
-
11
- beforeEach(() => {
12
- homeDir = mkdtempSync(join(tmpdir(), "stash-seed-test-"));
13
- process.env.OP_HOME = homeDir;
14
- mkdirSync(join(homeDir, "stash"), { recursive: true });
15
- });
16
-
17
- afterEach(() => {
18
- process.env.OP_HOME = originalHome;
19
- // Restore writable mode in case a test chmod'd the stash dir.
20
- try {
21
- chmodSync(join(homeDir, "stash"), 0o755);
22
- } catch {
23
- // ignore — dir may not exist
24
- }
25
- rmSync(homeDir, { recursive: true, force: true });
26
- });
27
-
28
- it("writes every seed under stash/ on first run", () => {
29
- const seeds = {
30
- "skills/test-skill/SKILL.md": "---\nname: test-skill\ntype: skill\n---\nhello\n",
31
- "commands/test-cmd.md": "---\nname: test-cmd\ntype: command\n---\nrun me\n",
32
- };
33
- const written = seedStashAssets(seeds);
34
-
35
- expect(written.sort()).toEqual(Object.keys(seeds).sort());
36
- for (const [rel, content] of Object.entries(seeds)) {
37
- const target = join(homeDir, "stash", rel);
38
- expect(existsSync(target)).toBe(true);
39
- expect(readFileSync(target, "utf-8")).toBe(content);
40
- }
41
- });
42
-
43
- it("does not overwrite existing files (user edits win)", () => {
44
- const seeds = { "skills/keep-mine/SKILL.md": "ORIGINAL SEED\n" };
45
- const userEdit = "USER EDIT — must not be overwritten\n";
46
-
47
- // Simulate a previous install: seed first.
48
- seedStashAssets(seeds);
49
- const target = join(homeDir, "stash/skills/keep-mine/SKILL.md");
50
- expect(readFileSync(target, "utf-8")).toBe("ORIGINAL SEED\n");
51
-
52
- // User edits the file.
53
- writeFileSync(target, userEdit);
54
-
55
- // Re-run: must return [] and leave the user's content intact.
56
- const written = seedStashAssets(seeds);
57
- expect(written).toEqual([]);
58
- expect(readFileSync(target, "utf-8")).toBe(userEdit);
59
- });
60
-
61
- it("creates nested directories under stash/ as needed", () => {
62
- const seeds = { "skills/deep/nested/asset/SKILL.md": "x" };
63
- seedStashAssets(seeds);
64
- expect(existsSync(join(homeDir, "stash/skills/deep/nested/asset/SKILL.md"))).toBe(true);
65
- });
66
-
67
- it("returns an empty list when called with no seeds", () => {
68
- expect(seedStashAssets({})).toEqual([]);
69
- });
70
-
71
- it("rejects seed keys that escape the stash directory", () => {
72
- // Path-traversal guard: ../ sequences in keys must throw rather than
73
- // silently writing outside stash/.
74
- expect(() =>
75
- seedStashAssets({ "../../etc/cron.d/evil": "owned\n" }),
76
- ).toThrow(/escapes stash dir/);
77
-
78
- // Confirm the malicious payload was NOT written anywhere relative to
79
- // the temp home.
80
- expect(existsSync(join(homeDir, "..", "..", "etc", "cron.d", "evil"))).toBe(false);
81
- });
82
-
83
- it("rejects seed keys that traverse through the stash dir back out", () => {
84
- expect(() =>
85
- seedStashAssets({ "skills/../../../escape.md": "x" }),
86
- ).toThrow(/escapes stash dir/);
87
- });
88
-
89
- it("surfaces errors when the stash directory is read-only", () => {
90
- // Skip when running as root (chmod is a no-op for the superuser).
91
- const uid = process.getuid?.();
92
- if (uid === 0) return;
93
-
94
- const stashDir = join(homeDir, "stash");
95
- chmodSync(stashDir, 0o555);
96
- try {
97
- expect(() =>
98
- seedStashAssets({ "skills/readonly/SKILL.md": "nope\n" }),
99
- ).toThrow();
100
- } finally {
101
- chmodSync(stashDir, 0o755);
102
- }
103
- });
104
- });