Skip to content
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
2 changes: 2 additions & 0 deletions .agents/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@
[0] CI `validate` workflow currently runs `format:check`, `lint`, `test`, and `apps/web` build. Before pushing, run all four locally to avoid fast-follow CI fix commits.
[0] `stash db doctor` does not create parent directories for a missing DB path; if `STASH_DB_PATH` points to a non-existent directory, command can fail with `unable to open database file`.
[0] Prefer explicit named types over `ReturnType<typeof ...>` in production code/tests for readability and easier review comments.
[0] `tithe web` parity means flag-based daemon controls (`--daemon`, `--status`, `--stop`), not `start`/`stop` subcommands; mirror that CLI shape when porting its web supervisor flow into `stash`.
[0] In Commander, a root/global option like `--db-path` is not reliable as a duplicate command-local option on hidden subcommands. For internal commands, pass the global flag before the subcommand (`stash --db-path ... web-supervisor`) and read it from `program.opts()` inside the action.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
STASH_DB_PATH=~/.stash/stash.db
# Optional:
# STASH_AUDIO_DIR=~/.stash/audio
# STASH_WEB_HOST=127.0.0.1
# STASH_WEB_HOST=0.0.0.0
# STASH_API_PORT=4173
# STASH_PWA_PORT=5173
# STASH_AUTO_TAGS_ENABLED=true
Expand Down
16 changes: 13 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ pnpm exec playwright install chromium
```

Web server defaults (overridable in `.env` or CLI flags):
- `STASH_WEB_HOST=127.0.0.1`
- `STASH_WEB_HOST=0.0.0.0` is the recommended daemon/Tailnet-friendly override.
- When `STASH_WEB_HOST` is unset, foreground `stash web` defaults to `127.0.0.1` and `stash web --daemon` defaults to `0.0.0.0`.
- `STASH_API_PORT=4173`
- `STASH_PWA_PORT=5173`

Expand Down Expand Up @@ -341,8 +342,17 @@ Updates should include:
- Auto-tags env controls:
- `STASH_AUTO_TAGS_ENABLED`, `STASH_AUTO_TAGS_MAX`, `STASH_AUTO_TAGS_MIN_SCORE`
- `STASH_AUTO_TAGS_MODEL`, `STASH_AUTO_TAGS_BACKEND`, `STASH_AUTO_TAGS_PYTHON`, `STASH_AUTO_TAGS_HELPER`
- `stash web` now accepts `--host`, `--api-port`, `--pwa-port` (`--port` removed).
- `stash web` runs split listeners (API + PWA) and fails fast on port conflicts or identical API/PWA ports.
- `stash web` now supports foreground and daemon control flags:
- `stash web` / `stash web --foreground` run attached in the current terminal.
- `stash web --daemon` starts a detached supervisor that restarts the combined web runner on unexpected exit.
- `stash web --status` reads daemon state/log metadata.
- `stash web --stop` sends `SIGTERM` to the daemon supervisor and waits for shutdown.
- `stash web` accepts `--host`, `--api-port`, `--pwa-port`; `--status`/`--stop` reject host/port overrides.
- `stash web --daemon` is idempotent for agent callers: a second start returns the existing daemon state instead of failing.
- `stash web` keeps API + PWA in one runtime process; the daemon supervises that single runner rather than separate API/PWA children.
- `stash web` persists daemon files under `~/.stash/` with workspace fallback `.stash/` (`web-daemon.pid`, `web-daemon.log`, `web-daemon.state.json`).
- `stash web` startup/status output includes local URLs and best-effort Tailnet URLs from `tailscale status --json`; loopback binds warn that Tailnet access is unavailable.
- `stash web` still fails fast on identical API/PWA ports.
- Web dev (`apps/web` Vite) reads the same root `.env` port variables and uses strict port binding.
- `pnpm run dev:stack` starts API (`pnpm run dev:api`, default API port `4173`) and Vite HMR (`pnpm run dev:web`, default `5173`) together for frontend hot reload.
- Web UI uses a single mobile-first, single-column layout path across all viewport sizes; desktop split-pane rendering paths were intentionally removed.
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Web UI behavior:
You can configure these via `.env`:

```bash
STASH_WEB_HOST=127.0.0.1
STASH_WEB_HOST=0.0.0.0
STASH_API_PORT=4173
STASH_PWA_PORT=5173
```
Expand All @@ -63,8 +63,19 @@ Or override at runtime:

```bash
stash web --host 127.0.0.1 --api-port 4173 --pwa-port 5173
stash web --daemon --host 0.0.0.0 --api-port 4173 --pwa-port 5173
stash web --status --json
stash web --stop --json
```

Runtime notes:
- `stash web` runs in the foreground by default; `stash web --foreground` is an explicit alias.
- `stash web --daemon` starts a detached supervisor that restarts the combined web runner if it crashes or is killed unexpectedly.
- `stash web --status` and `stash web --stop` mirror the `tithe` control flow and cannot be combined with host/port overrides.
- daemon state lives under `~/.stash/` with workspace fallback `.stash/` (`web-daemon.pid`, `web-daemon.log`, `web-daemon.state.json`).
- daemon startup/status output includes localhost URLs plus best-effort Tailnet URLs from `tailscale status --json`.
- when `STASH_WEB_HOST` is unset, foreground `stash web` defaults to `127.0.0.1`; daemon mode defaults to `0.0.0.0` for remote/Tailnet access.

### Web Development (Vite HMR)

Use this for frontend hot reloading:
Expand Down
182 changes: 130 additions & 52 deletions apps/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
type CoreServices,
type StashItem as CoreStashItem,
} from "@stash/core"
import { CliError } from "./error.js"
import { runWebCommand, runWebRunnerCommand, runWebSupervisorCommand } from "./web.js"

const CLI_DIR = path.dirname(fileURLToPath(import.meta.url))
const DEFAULT_MIGRATIONS_DIR = resolveExistingPath([
Expand All @@ -29,7 +31,6 @@ const DEFAULT_WEB_DIST_DIR = resolveExistingPath([
path.resolve(CLI_DIR, "../../../../apps/web/dist"),
path.resolve(process.cwd(), "apps/web/dist"),
])
const DEFAULT_WEB_HOST = process.env.STASH_WEB_HOST ?? "127.0.0.1"
const DEFAULT_API_PORT = process.env.STASH_API_PORT ? parsePort(process.env.STASH_API_PORT) : 4173
const DEFAULT_PWA_PORT = process.env.STASH_PWA_PORT ? parsePort(process.env.STASH_PWA_PORT) : 5173
const DEFAULT_TTS_VOICE = "tts_models/en/vctk/vits|p241" // Coqui male voice
Expand Down Expand Up @@ -69,17 +70,6 @@ type StartedWebStack = {

type StartWebStackFn = (options: StartWebStackOptions) => Promise<StartedWebStack>

class CliError extends Error {
code: string
exitCode: number

constructor(message: string, code: string, exitCode: number) {
super(message)
this.code = code
this.exitCode = exitCode
}
}

function parsePositiveInt(value: string): number {
const parsed = Number.parseInt(value, 10)
if (!Number.isInteger(parsed) || parsed < 1) {
Expand Down Expand Up @@ -353,7 +343,7 @@ Quick Reference:
stash tts status <jobId> [--json]
stash tts doctor [--json]
stash jobs worker [--poll-ms <n>] [--once] [--json]
stash web [--host <host>] [--api-port <n>] [--pwa-port <n>]
stash web [--daemon|--foreground|--status|--stop] [--host <host>] [--api-port <n>] [--pwa-port <n>] [--json]
stash db migrate [--json] [--migrations-dir <path>]
stash db doctor [--json] [--migrations-dir <path>] [--limit <n>]

Expand Down Expand Up @@ -1061,51 +1051,139 @@ jobsCommand
program
.command("web")
.description("Run local web frontend (PWA) + REST API servers")
.option("--host <host>", "Host to bind API and PWA servers", DEFAULT_WEB_HOST)
.option("--api-port <n>", "Port to bind API server", parsePort, DEFAULT_API_PORT)
.option("--pwa-port <n>", "Port to bind PWA server", parsePort, DEFAULT_PWA_PORT)
.action(async (options: { host: string; apiPort: number; pwaPort: number }) => {
return runDbAction(false, async () => {
if (options.apiPort === options.pwaPort) {
throw new CliError("API and PWA ports must be different.", "VALIDATION_ERROR", 2)
}

const dbPath = resolveDbPath(program.opts().dbPath as string)
const migrationsDir = resolveMigrationsDir()
const startWebStack = await loadStartWebStack()
const web = await startWebStack({
host: options.host,
apiPort: options.apiPort,
pwaPort: options.pwaPort,
dbPath,
migrationsDir,
webDistDir: DEFAULT_WEB_DIST_DIR,
audioDir: resolveAudioDir(process.env.STASH_AUDIO_DIR ?? DEFAULT_AUDIO_DIR),
.option("--host <host>", "Host to bind API and PWA servers")
.option("--api-port <n>", "Port to bind API server", parsePort)
.option("--pwa-port <n>", "Port to bind PWA server", parsePort)
.option("--daemon", "Run in background daemon mode with auto-restart")
.option("--status", "Show background web daemon status")
.option("--stop", "Stop background web daemon")
.option("--foreground", "Run in the foreground even when daemon controls are available")
.option("--json", "Print machine-readable JSON output")
.action(
async (options: {
host?: string
apiPort?: number
pwaPort?: number
daemon?: boolean
status?: boolean
stop?: boolean
foreground?: boolean
json?: boolean
}) => {
const jsonMode = Boolean(options.json)
return runDbAction(jsonMode, async () => {
await runWebCommand(
{
host: options.host,
apiPort: options.apiPort,
pwaPort: options.pwaPort,
daemon: options.daemon,
status: options.status,
stop: options.stop,
foreground: options.foreground,
},
{
json: jsonMode,
dbPath: resolveDbPath(program.opts().dbPath as string),
migrationsDir: resolveMigrationsDir(),
webDistDir: DEFAULT_WEB_DIST_DIR,
audioDir: resolveAudioDir(process.env.STASH_AUDIO_DIR ?? DEFAULT_AUDIO_DIR),
defaultApiPort: DEFAULT_API_PORT,
defaultPwaPort: DEFAULT_PWA_PORT,
envHost: process.env.STASH_WEB_HOST,
loadStartWebStack,
},
)
})
},
)

process.stdout.write(`stash web running\n`)
process.stdout.write(`API: http://${web.api.host}:${web.api.port}\n`)
process.stdout.write(`PWA: http://${web.pwa.host}:${web.pwa.port}\n`)
program
.command("web-supervisor")
.description("Internal daemon supervisor for stash web --daemon")
.option("--host <host>", "Host to bind API and PWA servers")
.option("--api-port <n>", "Port to bind API server", parsePort)
.option("--pwa-port <n>", "Port to bind PWA server", parsePort)
.requiredOption("--migrations-dir <path>", "Path to migrations directory")
.requiredOption("--web-dist-dir <path>", "Path to built web assets")
.requiredOption("--audio-dir <path>", "Path to generated audio directory")
.option("--run-id <runId>", "Daemon run identifier")
.action(
async (options: {
host?: string
apiPort?: number
pwaPort?: number
migrationsDir: string
webDistDir: string
audioDir: string
runId?: string
}) => {
return runDbAction(false, async () => {
if (!options.host || options.apiPort === undefined || options.pwaPort === undefined) {
throw new CliError(
"web-supervisor requires --host, --api-port, and --pwa-port.",
"VALIDATION_ERROR",
2,
)
}

await new Promise<void>((resolve) => {
const stop = (): void => {
web
.close()
.catch(() => {
// Ignore shutdown errors during signal handling.
})
.finally(() => {
process.off("SIGINT", stop)
process.off("SIGTERM", stop)
resolve()
})
await runWebSupervisorCommand({
host: options.host,
apiPort: options.apiPort,
pwaPort: options.pwaPort,
dbPath: resolveDbPath(program.opts().dbPath as string),
migrationsDir: options.migrationsDir,
webDistDir: options.webDistDir,
audioDir: options.audioDir,
runId: options.runId,
})
})
},
)

program
.command("web-runner")
.description("Internal daemon worker for stash web --daemon")
.option("--host <host>", "Host to bind API and PWA servers")
.option("--api-port <n>", "Port to bind API server", parsePort)
.option("--pwa-port <n>", "Port to bind PWA server", parsePort)
.requiredOption("--migrations-dir <path>", "Path to migrations directory")
.requiredOption("--web-dist-dir <path>", "Path to built web assets")
.requiredOption("--audio-dir <path>", "Path to generated audio directory")
.action(
async (options: {
host?: string
apiPort?: number
pwaPort?: number
migrationsDir: string
webDistDir: string
audioDir: string
}) => {
return runDbAction(false, async () => {
if (!options.host || options.apiPort === undefined || options.pwaPort === undefined) {
throw new CliError(
"web-runner requires --host, --api-port, and --pwa-port.",
"VALIDATION_ERROR",
2,
)
}

process.on("SIGINT", stop)
process.on("SIGTERM", stop)
await runWebRunnerCommand(
{
host: options.host,
apiPort: options.apiPort,
pwaPort: options.pwaPort,
dbPath: resolveDbPath(program.opts().dbPath as string),
migrationsDir: options.migrationsDir,
webDistDir: options.webDistDir,
audioDir: options.audioDir,
runId: undefined,
},
loadStartWebStack,
)
})
})
})
},
)

program
.command("read <id>")
Expand Down
10 changes: 10 additions & 0 deletions apps/cli/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class CliError extends Error {
code: string
exitCode: number

constructor(message: string, code: string, exitCode: number) {
super(message)
this.code = code
this.exitCode = exitCode
}
}
Loading