Multiplayer agent harness for work.
Add qm secrets set for safe in-place .env edits (#32)
Regan Bell3fec30e6b2e2
diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 37c20c81a..d198ab769 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -1,6 +1,7 @@import { spawnSync } from "node:child_process";+import { readFileSync } from "node:fs";import { dirname, join, resolve } from "node:path";-import { CliError, bold, dim, errMessage, note, red } from "./log.ts";+import { CliError, bold, dim, errMessage, note, ok, red } from "./log.ts";import {findConfigPath,isModelProvider,@@ -24,7 +25,7 @@ import { runConformance } from "./commands/conformance.ts";import { renderSlackFiles, runOutputs } from "./commands/outputs.ts";import { cliVersion } from "./manifest.ts";import { renderTerraformVars } from "./terraform.ts";-import { gitTopLevel } from "./util.ts";+import { gitTopLevel, promptHidden, writeEnvValue } from "./util.ts";import { scopeStorageKey } from "./scope-storage-key.ts";interface Parsed {@@ -126,6 +127,9 @@ ${bold("DEPLOY (operator)")} ${dim("— runs in the deployment directory")}infra delete-task-definitions --yes delete the stack's AWS ECS task definition revisionsconformance [dir] [--static] run static gates and compare the live resolved layersecrets push [--from <env-file>] upload the computed secret set to the target store+ secrets set <KEY> [<value>] write one .env value in place (dedupes the key, keeps+ [--from-file <path>] order and file mode); reads stdin or prompts when no+ value is given, so the secret never hits shell historystatus show what's runninglogs [<service>] [-f] [--tail <n>] tail service logs (omit <service> for all, interleaved)down [--purge] stop the deployment (--purge drops docker volumes)@@ -552,7 +556,37 @@ async function dispatch(argv: string[]): Promise<void> {}case "secrets": {- if (positionals[0] !== "push") throw new CliError(`usage: ${CLI_NAME} secrets push [--from <env-file>]`);+ if (positionals[0] === "set") {+ rejectExtraPositionals(positionals, 3);+ rejectUnknownFlags(flags, ["config", "env-file", "sandbox-dir", "target", "from-file"]);+ const key = positionals[1];+ if (!key) {+ throw new CliError(`usage: ${CLI_NAME} secrets set <KEY> [<value>] [--from-file <path>]`, {+ clause: "cli.invocation",+ });+ }+ const fromFile = strFlag(flags, "from-file");+ if (positionals[2] !== undefined && fromFile !== undefined) {+ throw new CliError(`secrets set takes a value argument or --from-file, not both`, {+ clause: "cli.invocation",+ });+ }+ const ctx = deployContext(flags);+ let value = positionals[2];+ if (value === undefined && fromFile !== undefined) value = readFileSync(fromFile, "utf8");+ if (value === undefined) value = process.stdin.isTTY ? await promptHidden(key) : await readStdin();+ value = value.replace(/\r?\n$/, "");+ if (!value) throw new CliError(`secrets set: no value provided for ${key}`, { clause: "cli.invocation" });+ const envPath = ctx.envFile ?? join(ctx.configDir, ".env");+ writeEnvValue(envPath, key, value);+ ok(`set ${key} in ${envPath}`);+ return;+ }+ if (positionals[0] !== "push") {+ throw new CliError(+ `usage: ${CLI_NAME} secrets push [--from <env-file>] | secrets set <KEY> [<value>] [--from-file <path>]`,+ );+ }rejectExtraPositionals(positionals, 1);rejectUnknownFlags(flags, ["config", "env-file", "sandbox-dir", "target", "from"]);const ctx = deployContext(flags);@@ -565,6 +599,12 @@ async function dispatch(argv: string[]): Promise<void> {}}+async function readStdin(): Promise<string> {+ const chunks: Buffer[] = [];+ for await (const chunk of process.stdin) chunks.push(chunk as Buffer);+ return Buffer.concat(chunks).toString("utf8");+}+export async function main(argv: string[]): Promise<void> {try {await dispatch(argv);diff --git a/cli/src/util.ts b/cli/src/util.tsindex 3c9e5de11..c4da960fb 100644--- a/cli/src/util.ts+++ b/cli/src/util.ts@@ -1,5 +1,5 @@import { execFileSync, spawn, spawnSync } from "node:child_process";-import { existsSync, openSync, readFileSync } from "node:fs";+import { chmodSync, existsSync, openSync, readFileSync, statSync, writeFileSync } from "node:fs";import { join, resolve } from "node:path";import { CliError, errMessage } from "./log.ts";@@ -227,6 +227,35 @@ export function readEnvFile(path: string): Map<string, string> {return out;}+export function writeEnvValue(path: string, key: string, value: string): void {+ if (!isEnvVarName(key)) throw new CliError(`${JSON.stringify(key)} is not a valid environment variable name`);+ if (/[\r\n]/.test(value)) throw new CliError(`the value for ${key} must be a single line`);+ const existing = existsSync(path);+ const mode = existing ? statSync(path).mode & 0o777 : 0o600;+ const lines = existing ? readFileSync(path, "utf8").split("\n") : [];+ if (lines.at(-1) === "") lines.pop();+ const matches = (line: string): boolean => {+ const trimmed = line.trim();+ if (!trimmed || trimmed.startsWith("#")) return false;+ const eq = trimmed.indexOf("=");+ return eq > 0 && trimmed.slice(0, eq).trim() === key;+ };+ const entry = `${key}=${value}`;+ const next: string[] = [];+ let placed = false;+ for (const line of lines) {+ if (!matches(line)) {+ next.push(line);+ } else if (!placed) {+ next.push(entry);+ placed = true;+ }+ }+ if (!placed) next.push(entry);+ writeFileSync(path, `${next.join("\n")}\n`, { mode });+ chmodSync(path, mode);+}+export function deploymentSecretValue(name: string, fileValue: string | undefined): string | undefined {return fileValue === undefined || fileValue.trim() === "" ? process.env[name] : fileValue;}diff --git a/cli/test/util.test.ts b/cli/test/util.test.tsindex 92235f202..f4a661efe 100644--- a/cli/test/util.test.ts+++ b/cli/test/util.test.ts@@ -1,9 +1,9 @@import { test } from "node:test";import assert from "node:assert/strict";-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { chmodSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";-import { canonicalJson, flyBin, isInvalidSecret, readEnvFile } from "../src/util.ts";+import { canonicalJson, flyBin, isInvalidSecret, readEnvFile, writeEnvValue } from "../src/util.ts";test("managed credential encryption keys require strong material", () => {assert.equal(isInvalidSecret("CONNECTOR_SECRET_KEY", "short"), true);@@ -55,6 +55,55 @@ test("readEnvFile preserves hashes in unquoted values", (t) => {);});+test("writeEnvValue appends a new key with 0600 on a fresh file", (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-env-"));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const file = join(dir, ".env");+ writeEnvValue(file, "NEW_KEY", "value-1");+ assert.equal(readFileSync(file, "utf8"), "NEW_KEY=value-1\n");+ assert.equal(statSync(file).mode & 0o777, 0o600);+ writeEnvValue(file, "SECOND", "two");+ assert.equal(readFileSync(file, "utf8"), "NEW_KEY=value-1\nSECOND=two\n");+});++test("writeEnvValue replaces an existing key in place, preserving order and comments", (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-env-"));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const file = join(dir, ".env");+ writeFileSync(file, "# comment\nA=1\nB=old\nC=3\n");+ writeEnvValue(file, "B", "new#value");+ assert.equal(readFileSync(file, "utf8"), "# comment\nA=1\nB=new#value\nC=3\n");+ assert.equal(readEnvFile(file).get("B"), "new#value");+});++test("writeEnvValue collapses duplicate occurrences into the first", (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-env-"));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const file = join(dir, ".env");+ writeFileSync(file, "A=1\nB=first\nC=3\nB=second\nB=third\n");+ writeEnvValue(file, "B", "only");+ assert.equal(readFileSync(file, "utf8"), "A=1\nB=only\nC=3\n");+});++test("writeEnvValue preserves the existing file mode", (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-env-"));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const file = join(dir, ".env");+ writeFileSync(file, "A=1\n", { mode: 0o640 });+ chmodSync(file, 0o640);+ writeEnvValue(file, "A", "2");+ assert.equal(statSync(file).mode & 0o777, 0o640);+ assert.equal(readFileSync(file, "utf8"), "A=2\n");+});++test("writeEnvValue rejects invalid keys and multi-line values", (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-env-"));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const file = join(dir, ".env");+ assert.throws(() => writeEnvValue(file, "BAD KEY", "x"));+ assert.throws(() => writeEnvValue(file, "GOOD_KEY", "a\nb"));+});+async function withFakeStdin<T>(fn: (emit: (bytes: Buffer) => void) => Promise<T>): Promise<T> {const { EventEmitter } = await import("node:events");const fake = Object.assign(new EventEmitter(), {