8000 Resolve silo paths from configuration instead of hardcoded directory names by simon-inkie · Pull Request #9 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/base/bin/create-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,18 @@ else
# Do not guess silently. Propose, explain, and record the choice in the
# output, so an inbox created in the wrong place is visible at the moment it
# happens rather than the first time a message goes missing.
#
# This value is the silo layout this project ships, NOT a reading of the
# deployment. It used to be announced as "the value from the application's
# .env.example", which was never true - that file ships INBOX_ROOT empty,
# because there is no default (FND-03) - and it invited the reader to trust a
# guess as though something had confirmed it.
INBOX_ROOT="$HOME/.the-vault/agents"
INBOX_ROOT_SOURCE="default (INBOX_ROOT was unset)"
warn "INBOX_ROOT is not set. The application reads agents from that directory,"
warn "so an agent created outside it exists for the launcher and not for the"
warn "application. Proposing the value from the application's .env.example:"
warn "application. Nothing here knows where YOUR inbox root is: the value"
warn "below is this project's default layout, and it is a guess."
warn " $INBOX_ROOT"
warn "Set INBOX_ROOT, or pass --inbox-root, to put it somewhere else."
fi
Expand Down
47 changes: 47 additions & 0 deletions packages/base/test/create-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,53 @@ describe("creation", () => {
expect(result.output).toContain("--inbox-root");
});

// ---------------------------------------------------------------------------
// Where the inbox root comes from
// ---------------------------------------------------------------------------
//
// The built-in fallback names a silo directory this project ships. An audit
// read that literal as a hardcoded path and called the script broken. It is
// not: the literal is the LAST of three sources, and both of the ones ahead
// of it win. These pin that ordering, because the thing that would actually
// be broken is a fallback that fired when something had been configured.

it("uses INBOX_ROOT from the environment in preference to the built-in default", () => {
const box = sandbox();
const result = run(box, ["wren", "--no-verify"], {
env: { INBOX_ROOT: box.inboxRoot },
});
expect(fs.existsSync(path.join(box.inboxRoot, "wren", "inbox"))).toBe(true);
// The fallback did not fire, so it said nothing.
expect(result.output).not.toContain("INBOX_ROOT is not set");
expect(fs.existsSync(path.join(box.home, ".the-vault"))).toBe(false);
});

it("--inbox-root beats INBOX_ROOT, which beats the built-in default", () => {
const box = sandbox();
const chosen = path.join(box.home, "chosen-inbox");
const result = run(box, ["wren", "--no-verify", "--inbox-root", chosen], {
env: { INBOX_ROOT: box.inboxRoot },
});
expect(fs.existsSync(path.join(chosen, "wren", "inbox"))).toBe(true);
expect(fs.existsSync(path.join(box.inboxRoot, "wren"))).toBe(false);
expect(result.output).toContain(chosen);
});

it("calls the fallback a guess, and does not attribute it to a file that does not say it", () => {
// The warning used to claim the value came from the application's
// .env.example. That file ships INBOX_ROOT empty (there is no default,
// FND-03), so the citation dressed a guess up as a lookup.
const box = sandbox();
const result = run(box, ["wren", "--no-verify"], {
env: { INBOX_ROOT: "" },
});
expect(result.output).toContain("INBOX_ROOT is not set");
expect(result.output).toContain("guess");
expect(result.output).not.toContain(".env.example");
// It still says exactly where the agent went.
expect(result.output).toContain(path.join(box.home, ".the-vault", "agents"));
});

it("warns when AGENTS_HOME and AGENTS_ROOT disagree instead of silently picking one", () => {
const box = sandbox();
const result = run(box, ["wren", "--no-verify"], {
Expand Down
20 changes: 16 additions & 4 deletions packages/deck/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,20 @@ MC_BRIEFS_AGENT=
MC_TMUX_SESSION=

# Sandbox mode: when set to a non-empty value, the server REFUSES any root that
# resolves to ~/.the-vault or below, so a sandbox configuration can never be
# pointed at a live agent silo. test/setup.ts sets it automatically, and a dev
# .env should set it too. Production leaves it unset deliberately, as part of
# pointing the application at a real inbox.
# resolves to the live agent silo (see MC_LIVE_SILO_ROOT below) or below it, so
# a sandbox configuration can never be pointed at a live agent silo.
# test/setup.ts sets it automatically, and a dev .env should set it too.
# Production leaves it unset deliberately, as part of pointing the application
# at a real inbox.
MC_SANDBOX_ONLY=

# Optional: absolute path to the live agent silo MC_SANDBOX_ONLY protects.
# Default: ~/.the-vault, the layout this project ships.
#
# Set this if your fleet's silo directory is named anything else. The sandbox
# guard can only refuse a silo it can name, so on a deployment that keeps its
# agents somewhere other than the default, leaving this unset means
# MC_SANDBOX_ONLY is a flag that reads like a protection and refuses nothing.
# It does nothing on its own; it only says WHAT MC_SANDBOX_ONLY is guarding.
# MC_LIVE_SILO_ROOT=/home/you/.my-fleet
MC_LIVE_SILO_ROOT=
77 changes: 61 additions & 16 deletions packages/deck/app/lib/cron.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@
* RMW never rewrites untagged lines. Target validated against live
* ~/agents/* dirs.
* 4. DC4 visibility: nudge script logs fire + delivered timestamps to
* ~/.the-vault/agents/<target>/cron-state/<id>.json; listCrons reads it.
* <INBOX_ROOT>/<target>/cron-state/<id>.json; listCrons reads it.
* INBOX_ROOT is the configured root that holds one directory per agent
* (inbox/ lives beside cron-state/ inside it), resolved through the same
* resolveInboxRoot() every other silo-adjacent module uses. It is NOT
* spelled out as a literal here: a hardcoded silo directory name makes
* the writer and the reader agree with each other and disagree with the
* deployment, so state written by a fired nudge becomes invisible to the
* UI and an edited nudge starts a second, parallel state tree.
* 5. Every exported function degrades gracefully (no throws on missing
* crontab / missing state files).
* crontab / missing state files). An unset INBOX_ROOT is NOT in that
* category: it is a misconfiguration the whole application already
* refuses to start against (FND-03, no default), and guessing a path
* here is the defect this posture exists to prevent.
*
* Delivery (ask-1): agent-mode messages are written to a .prompt.md sibling
* file and pasted into the tmux pane via bracketed-paste (tmux load-buffer +
Expand All @@ -32,6 +42,7 @@ import path from "node:path";
import os from "node:os";
import crypto from "node:crypto";
import { validateCronExpression, validateCronMessage } from "./cron-validate.js";
import { resolveInboxRoot } from "./safe-fs.server.js";

// ---------------------------------------------------------------------------
// Types
Expand Down Expand Up @@ -67,6 +78,14 @@ export interface CronDeps {
readStateFile: (filePath: string) => string | null;
writeStateFile: (filePath: string, content: string) => void;
homedir: () => string;
/**
* The configured root holding one directory per agent, i.e. INBOX_ROOT.
* cron-state/ sits beside inbox/ inside each agent's directory, so the
* nudge script writes and listCrons reads the SAME configured location on
* every deployment. Injectable for the same reason everything else here is:
* the suite must never resolve a real root.
*/
inboxRoot: () => string;
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -168,13 +187,32 @@ export function makeDefaultDeps(): CronDeps {
homedir(): string {
return os.homedir();
},
inboxRoot(): string {
// Lazy on purpose: makeDefaultDeps() runs as a default argument on every
// exported call, including the ones that reject a bad target before they
// ever need a root. Resolving eagerly would turn "unknown agent" into a
// configuration throw.
return resolveInboxRoot();
},
};
}

// ---------------------------------------------------------------------------
// Script template (agent mode) - bracketed-paste delivery
// ---------------------------------------------------------------------------

/**
* Quote a string for safe use inside a single-quoted bash word.
*
* The state root became a configured value rather than a literal, and a
* configured value is one an operator can put a quote, a dollar sign or a
* backtick into. Inside double quotes bash would expand those; the standard
* POSIX single-quote escape ('\'') cannot expand anything at all.
*/
function shSingleQuote(value: string): string {
return `'${value.replaceAll("'", `'\\''`)}'`;
}

/**
* Build the nudge script for agent-mode crons.
*
Expand All @@ -184,14 +222,23 @@ export function makeDefaultDeps(): CronDeps {
*
* Keeps: shebang, set -u, CRON_STATE_DIR, MARKER (per-fire-slot idempotent guard),
* STATE_FILE, has-session guard, double-Enter race defeat, DC4 state writes.
*
* @param inboxRoot - The configured agent-directory root (INBOX_ROOT). Baked
* into the generated script so a fired nudge writes its state where
* listCrons looks for it, on whatever deployment this is.
*/
function buildNudgeScript(target: string, id: string, promptFilePath: string): string {
function buildNudgeScript(
target: string,
id: string,
promptFilePath: string,
inboxRoot: string
): string {
// ASCII only - no em-dashes.
return `#!/usr/bin/env bash
# Generated by Inkie Deck cron admin (TASK-460). Do not edit manually.
# Target: ${target} | ID: ${id}
set -u
CRON_STATE_DIR="$HOME/.the-vault/agents/${target}/cron-state"
CRON_STATE_DIR=${shSingleQuote(path.join(inboxRoot, target, "cron-state"))}
MARKER="$CRON_STATE_DIR/.run-marker-${id}-$(date +%F-%H%M)"
STATE_FILE="$CRON_STATE_DIR/${id}.json"
PROMPT_FILE="${promptFilePath}"
Expand Down Expand Up @@ -224,19 +271,16 @@ interface CronState {
}

function readCronState(
homedir: string,
inboxRoot: string,
target: string,
id: string,
deps: CronDeps
): { lastFired: string | null; lastDelivered: string | null } {
const stateFile = path.join(
homedir,
".the-vault",
"agents",
target,
"cron-state",
`${id}.json`
);
// MUST stay byte-identical to the CRON_STATE_DIR the generated script writes
// (see buildNudgeScript). These two are the reader and the writer of one
// file; the moment they are derived from different sources they agree with
// each other and disagree with the deployment.
const stateFile = path.join(inboxRoot, target, "cron-state", `${id}.json`);
const raw = deps.readStateFile(stateFile);
if (!raw) return { lastFired: null, lastDelivered: null };
try {
Expand Down Expand Up @@ -320,6 +364,7 @@ function parseScriptPath(
export function listCrons(deps: CronDeps = makeDefaultDeps()): CronEntry[] {
const raw = deps.readCrontab();
const home = deps.homedir();
const inboxRoot = deps.inboxRoot();
const lines = raw.split("\n");
const results: CronEntry[] = [];

Expand Down Expand Up @@ -347,7 +392,7 @@ export function listCrons(deps: CronDeps = makeDefaultDeps()): CronEntry[] {
const pathInfo = parseScriptPath(scriptPath, home);
const target = pathInfo?.target ?? "";

const { lastFired, lastDelivered } = readCronState(home, target, id, deps);
const { lastFired, lastDelivered } = readCronState(inboxRoot, target, id, deps);

// Detect agent-mode vs script-mode by looking for a .prompt.md sibling.
// Agent mode: createCron writes <id>.prompt.md alongside the nudge script.
97F7 Expand Down Expand Up @@ -504,7 +549,7 @@ export function createCron(
// .prompt.md is data (not executable) -- use 0o644.
const promptFilePath = scriptPath.replace(/\.sh$/, ".prompt.md");
deps.writeScript(promptFilePath, message, 0o644);
const scriptContent = buildNudgeScript(target, id, promptFilePath);
const scriptContent = buildNudgeScript(target, id, promptFilePath, deps.inboxRoot());
deps.writeScript(scriptPath, scriptContent);
} else {
// Script mode: write the message body directly as the executable script.
Expand Down Expand Up @@ -605,7 +650,7 @@ export function updateCron(
if (targetType === "agent") {
const promptFilePath = scriptPath.replace(/\.sh$/, ".prompt.md");
deps.writeScript(promptFilePath, message, 0o644);
const scriptContent = buildNudgeScript(target, id, promptFilePath);
const scriptContent = buildNudgeScript(target, id, promptFilePath, deps.inboxRoot());
deps.writeScript(scriptPath, scriptContent);
} else {
deps.writeScript(scriptPath, message);
Expand Down
Loading
Loading
0