@buildaharness/app-node 0.0.0-stage → 0.1.0

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.
package/README.md CHANGED
@@ -1,3 +1,54 @@
1
- # Temporary Holding Version
1
+ # @buildaharness/app-node
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Node.js half of the shared app parts. The parts that also run in a browser are in `@buildaharness/runtime`.
4
+
5
+ | Part | What it does | Failure |
6
+ |---|---|---|
7
+ | `appPaths(identity, home?)` | `{ dataDir, configFile }` under `~/.buildaharness/<legacy.dataDirName or id>` | none |
8
+ | `nodeFsBackend({ privateRoots? })` | `FsBackend`; paths under a private root (default `~/.buildaharness`) are written owner-only | a missing file reads as undefined |
9
+ | `jsonConfigStore<C>(path, { warn? })` | `ConfigStore<C>` in one JSON file: merge on save, atomic owner-only writes, saves queued | a corrupt file warns and reads as `{}` |
10
+ | `fileSettingsStore(path, { warn? })` | `SettingsStore` (`getSetting`/`setSetting`) in one JSON file: owner-only (0600), temp file and rename, writes queued; `undefined` removes a key. For spending totals, activity logs, job markers | a file that is not a JSON object warns and reads as empty |
11
+ | `runShellCommand(command, cwd, { timeoutMs?, maxOutputBytes?, networkAllowlist?, env? })` | Runs an already approved command with a reduced environment (`env` names; default PATH, HOME, LANG) | a non-zero exit is a result; a spawn failure throws |
12
+ | `startNetworkContainment(allowlist)` | A loopback proxy that refuses other hosts; `getNetworkContainmentProxy` is the shared, cached one | not an OS sandbox: raw sockets bypass it |
13
+ | `excludeFromGit(workspaceRoot, dirs, { appName? })` | Adds folders to the repository's `info/exclude` | never rejects |
14
+ | `nodeResolver()` | `AddressResolver` over the system's DNS | rejects as `dns.lookup` does |
15
+
16
+ `AppIdentity`, `ConfigStore` and `defineSettings` (the settings table) are in `@buildaharness/runtime`. Aielia imports
17
+ all of these. Framework exports: these parts are about the app, not a flow.
18
+
19
+ ## Command-line base
20
+
21
+ | Part | What it does | Failure |
22
+ |---|---|---|
23
+ | `createCliHost({ identity, commands?, input?, output?, nonInteractive?, env?, prompt? })` | `print`, `write`, `confirm`, `yesNo`, `ask`, `select`, `serve(handle)`, `help`, `onInterrupt`, `close`. `serve` runs registered commands and passes other lines to `handle`, one at a time, until input closes or `exit`/`quit` | `require-tty` without a terminal throws `StartupError` |
24
+ | `createPrompter({ source, say, decline?, isYes?, pendingAtClose? })` | The prompts alone, over a readline interface | no answer is "no": closed or broken input, a second question while one waits. `select` gives the last option |
25
+ | `createLineQueue(handle, onError)` | One line at a time, in order | a throw goes to `onError`; later lines still run |
26
+ | `resolveNonInteractiveMode(env, variable)` | `decline` or `require-tty`; the host reads `<envPrefix>_NON_INTERACTIVE_APPROVAL` | another value warns and counts as unset |
27
+ | `stripTerminalControls`, `oneLine`, `indentBlock` | Make untrusted text safe to show. The host's `print` and `write` filter already | none |
28
+ | `parseCommandLine(argv, spec)`, `commandLineHelp(spec)` | `app [command] [options] words`, with `--help` and `--version` | an unknown option is `{ kind: 'error' }` |
29
+ | `CommandSpec`, `commandKey`, `formatCommandHelp`, `isQuitCommand` | A slash-command list and its help text | none |
30
+ | `runJob(host, job)`, `formatJobEvent` | Shows a runtime job; Ctrl+C stops softly, twice hard | a failed job prints its message |
31
+ | `reviewApproval(host, approvals, id)`, `formatPending`, `formatApproveResult` | Shows a staged action as it is now and applies it on a yes to that preview | no: rejected. No answer: left pending. Changed: shown and asked again |
32
+ | `runAppJob(host, job)`, `reviewDecision(host, session, decision)`, `formatDecision`, `formatDecisionResult`, `formatAppEvent` | The same for a runtime app session: `runAppJob` prints progress of an `AppJob` and returns `{ outcome, decisions }`; `reviewDecision` shows a `Decision`, lets the person leave items out, and resolves it only on a yes | no or no answer: declined. Changed: the new decision is shown, up to three times. Names are shown as the parts gave them (already escaped) |
33
+
34
+ Without `decline`, piped input can answer a prompt that is already waiting (lines read earlier stay commands, never
35
+ answers). A script that must never approve sets the mode to `decline`.
36
+
37
+ `examples/notes-app.ts` is a whole app on these (`src/example-app.test.ts` drives it). There is no `runSession` yet: it
38
+ needs the runtime's app-to-screen interface.
39
+
40
+ ## Place in the stack
41
+
42
+ Above `runtime`, beside `nodes`. It may import `flow-spec`, `harness` and `runtime`, and Node's built-in modules.
43
+ `aielia` and `node-service` may import it. Browser code must not (`scripts/eslint-layers.mjs`).
44
+
45
+ ## Development
46
+
47
+ ```bash
48
+ npm run typecheck:app-node
49
+ npm run test:app-node
50
+ npm run build:app-node
51
+ ```
52
+
53
+ The repository's root test run uses a browser-like environment, so a test file here starts with
54
+ `// @vitest-environment node`.
@@ -0,0 +1,28 @@
1
+ import { AppEvent, AppJob, AppOutcome, AppSession, Decision } from '@buildaharness/runtime';
2
+ import { CliHost } from './host.js';
3
+ /** What a person sees while an action runs (progress, stages, notes). Other events (decisions, the end) are for the caller. */
4
+ export declare function formatAppEvent(event: AppEvent): string | undefined;
5
+ /** A decision as the person is asked about it. */
6
+ export declare function formatDecision(decision: Decision): string;
7
+ export interface AppJobRun<R> {
8
+ outcome: AppOutcome<R>;
9
+ /** Every decision the action staged, in order. Answer them with `reviewDecision`. */
10
+ decisions: Decision[];
11
+ }
12
+ /**
13
+ * Prints an action's progress until it ends and collects its decisions. Ctrl+C asks it to stop after the current step, a second
14
+ * one stops it at once. A stop or a failure is printed ("Stopped.", "Failed: ...") and comes back in `outcome`.
15
+ */
16
+ export declare function runAppJob<R>(host: CliHost, job: AppJob<R>): Promise<AppJobRun<R>>;
17
+ /** What is said of the result of an approved decision. Shapes are those of the shared file move and its undo. */
18
+ export declare function formatDecisionResult(result: unknown, o?: {
19
+ undoHint?: string;
20
+ }): string[];
21
+ /**
22
+ * Shows a decision as it is now and applies it only on a yes to what was shown. With more than one item the person may leave
23
+ * some out. With nobody to ask (no terminal, or the non-interactive mode `decline`) it is declined. If it changed meanwhile the
24
+ * new one is shown instead, up to three times. Prints the outcome and returns it: `done` also when the person declined.
25
+ */
26
+ export declare function reviewDecision(host: CliHost, session: AppSession, decision: Decision, o?: {
27
+ undoHint?: string;
28
+ }): Promise<AppOutcome<unknown>>;
package/dist/args.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ export interface ArgOption {
2
+ type: 'boolean' | 'string';
3
+ /** One letter. */
4
+ short?: string;
5
+ help: string;
6
+ }
7
+ export interface ArgCommand {
8
+ help: string;
9
+ options?: Record<string, ArgOption>;
10
+ }
11
+ export interface ArgSpec {
12
+ name: string;
13
+ version: string;
14
+ summary?: string;
15
+ /** Accepted with or without a command. `--help` and `--version` are built in. */
16
+ options?: Record<string, ArgOption>;
17
+ /** A command is recognised only as the first argument. */
18
+ commands?: Record<string, ArgCommand>;
19
+ }
20
+ export type ParsedArgs = {
21
+ kind: 'help';
22
+ } | {
23
+ kind: 'version';
24
+ } | {
25
+ kind: 'error';
26
+ message: string;
27
+ } | {
28
+ kind: 'run';
29
+ command?: string;
30
+ options: Record<string, string | boolean | undefined>;
31
+ words: string[];
32
+ };
33
+ /** `argv` is the user's arguments only, i.e. `process.argv.slice(2)`. */
34
+ export declare function parseCommandLine(argv: readonly string[], spec: ArgSpec): ParsedArgs;
35
+ export declare function commandLineHelp(spec: ArgSpec): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Writes `text` to `path` through a temp file in the same folder and a rename, so a crash mid-write never leaves half a file.
3
+ * The folder is made owner-only when it is new, and the file is owner-only (0600): the temp file is new each time, so the mode
4
+ * always applies and the rename carries it over an older, possibly world-readable, file.
5
+ */
6
+ export declare function writeFileAtomic(path: string, text: string): Promise<void>;
@@ -0,0 +1,15 @@
1
+ export interface CommandSpec {
2
+ /** The command and any fixed subcommand words, no arguments: '/memory export'. The first word is the dispatch key. */
3
+ name: string;
4
+ /** What help prints for it, arguments included. */
5
+ usage: string;
6
+ description: string;
7
+ /** Other dispatch keys that run the same command ('/new' for '/clear'). */
8
+ aliases?: string[];
9
+ }
10
+ /** First word of a command name: the key a dispatch table matches on. */
11
+ export declare function commandKey(name: string): string;
12
+ /** One line per command: usage, padded, then the description. */
13
+ export declare function formatCommandHelp(commands: readonly CommandSpec[]): string;
14
+ /** A bare `exit` / `quit` (with or without a leading slash, any case) ends the session; anything longer is an ordinary message. An exact command match, not a reading of the message. */
15
+ export declare function isQuitCommand(message: string): boolean;
@@ -0,0 +1,16 @@
1
+ import { FsBackend } from '@buildaharness/runtime';
2
+ /**
3
+ * node:fs/promises-backed FsBackend.
4
+ *
5
+ * The same backend writes both an app's own state (transcripts, checkpoints, experience, reminders) and
6
+ * the user's workspace files. Only paths under a private root are made owner-only, like the config and
7
+ * activity log; other files keep the permissions they would get anyway.
8
+ */
9
+ export declare function nodeFsBackend(options?: {
10
+ privateRoots?: string[];
11
+ }): FsBackend;
12
+ /**
13
+ * Size and SHA-256 of a file's bytes, `undefined` when it does not exist. For `createFileMover`'s `fingerprint`: unlike a text reading it
14
+ * tells two different binary files (Word, PDF, images) apart. Reads the whole file, so the caller limits what it asks about.
15
+ */
16
+ export declare function nodeFileFingerprint(path: string): Promise<string | undefined>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Keeps an app's own state directories out of version control when the workspace is inside a git repo, by adding them to
3
+ * `<gitdir>/info/exclude` — the per-clone ignore file git itself provides for this. The user's tracked `.gitignore` is never
4
+ * touched, and a non-git directory is left exactly as it was. Idempotent and best-effort: resolves with the entries it added
5
+ * and never rejects (a read-only .git must not stop the app from starting). The file work is done before the call returns.
6
+ */
7
+ export declare function excludeFromGit(workspaceRoot: string, dirs: readonly string[], options?: {
8
+ appName?: string;
9
+ }): Promise<string[]>;
package/dist/host.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { AppIdentity } from '@buildaharness/runtime';
2
+ import { CommandSpec } from './commands.js';
3
+ import { NonInteractiveMode } from './non-interactive.js';
4
+ import { SelectOption, YesNoAnswer } from './prompts.js';
5
+ export interface HostCommand extends CommandSpec {
6
+ /** Without `run`, the entry only appears in help (a subcommand of an entry that has it). */
7
+ run?(args: string[]): void | Promise<void>;
8
+ }
9
+ /** A reason the app refuses to start; show the message to the user as it is. */
10
+ export declare class StartupError extends Error {
11
+ }
12
+ export interface CliHost {
13
+ /** One line or block, control sequences removed. */
14
+ print(text: string): void;
15
+ /** The same filter, without the newline: for streamed output. */
16
+ write(text: string): void;
17
+ /** False when no answer can be read, and always in `decline` mode. */
18
+ confirm(question: string): Promise<boolean>;
19
+ /** `none`: no answer could be read. In `decline` mode the answer is `no`. */
20
+ yesNo(question: string): Promise<YesNoAnswer>;
21
+ /** Free text. Undefined when nothing could be read, and in `decline` mode (input is not touched). */
22
+ ask(question: string): Promise<string | undefined>;
23
+ /** The last option on any answer that names none. Put the safe choice last. */
24
+ select(question: string, options: readonly SelectOption[]): Promise<string>;
25
+ /**
26
+ * Handles input until it closes or the user types `exit`/`quit`: registered commands run, every other non-empty line
27
+ * goes to `handle`, one at a time. Resolves when the queued lines have been handled, and closes the host.
28
+ */
29
+ serve(handle?: (line: string) => Promise<void>): Promise<void>;
30
+ /** Ctrl+C goes to the newest listener instead of ending the session, while the host is reading a terminal. Returns the remover. */
31
+ onInterrupt(listener: () => void): () => void;
32
+ help(): string;
33
+ readonly nonInteractive: NonInteractiveMode | undefined;
34
+ close(): void;
35
+ }
36
+ export interface CliHostOptions {
37
+ identity: AppIdentity;
38
+ commands?: HostCommand[];
39
+ input?: NodeJS.ReadableStream;
40
+ output?: NodeJS.WritableStream;
41
+ /** Default: the `<envPrefix>_NON_INTERACTIVE_APPROVAL` environment variable, if set. */
42
+ nonInteractive?: NonInteractiveMode;
43
+ env?: Record<string, string | undefined>;
44
+ /** Shown by `serve` when input is a terminal. Default `> `. */
45
+ prompt?: string;
46
+ }
47
+ export declare function createCliHost(o: CliHostOptions): CliHost;
@@ -0,0 +1,27 @@
1
+ /** Node.js ports for apps built from Build A Harness parts. */
2
+ export { appPaths, BUILDAHARNESS_DIR } from './paths.js';
3
+ export { nodeFsBackend, nodeFileFingerprint } from './fs-backend.js';
4
+ export { jsonConfigStore } from './json-config-store.js';
5
+ export { fileSettingsStore } from './settings-store.js';
6
+ export { runShellCommand } from './shell.js';
7
+ export type { ShellCommandOptions } from './shell.js';
8
+ export { startNetworkContainment, getNetworkContainmentProxy, resetNetworkContainmentProxiesForTests } from './network-containment.js';
9
+ export type { NetworkContainmentProxy } from './network-containment.js';
10
+ export { excludeFromGit } from './git-exclude.js';
11
+ export { nodeResolver } from './resolver.js';
12
+ export { stripTerminalControls, oneLine, indentBlock } from './terminal.js';
13
+ export { createLineQueue } from './line-queue.js';
14
+ export { createPrompter } from './prompts.js';
15
+ export type { Prompter, PrompterOptions, PromptSource, SelectOption, YesNoAnswer } from './prompts.js';
16
+ export { resolveNonInteractiveMode } from './non-interactive.js';
17
+ export type { NonInteractiveMode } from './non-interactive.js';
18
+ export { commandKey, formatCommandHelp, isQuitCommand } from './commands.js';
19
+ export type { CommandSpec } from './commands.js';
20
+ export { parseCommandLine, commandLineHelp } from './args.js';
21
+ export type { ArgSpec, ArgCommand, ArgOption, ParsedArgs } from './args.js';
22
+ export { createCliHost, StartupError } from './host.js';
23
+ export type { CliHost, CliHostOptions, HostCommand } from './host.js';
24
+ export { formatJobEvent, runJob, formatPending, formatApproveResult, reviewApproval } from './render.js';
25
+ export type { ReviewOutcome } from './render.js';
26
+ export { formatAppEvent, formatDecision, formatDecisionResult, runAppJob, reviewDecision } from './app-render.js';
27
+ export type { AppJobRun } from './app-render.js';