Skip to content

Add lifecycle hooks for the session, services and infra - #31

Merged
rhyek merged 4 commits into
mainfrom
feat/lifecycle-hooks
Nov 26, 2026
Merged

rhyek merged 4 commits into
mainfrom
feat/lifecycle-hooks

Conversation

@rhyek

@rhyek rhyek commented Nov 26, 2026

Copy link
Copy Markdown
Owner

What this adds

Lifecycle hooks: async functions in stackctl.config.ts that stackctl runs around its own steps. Four things take them, each under its own lifecycle:

Level Under Hooks
The session session.lifecycle beforeStart, afterStart, afterReady, beforeStop, afterStop
An application service services.<name>.lifecycle the same five
The Compose project infra['docker-compose'].lifecycle the same five, and beforeDestroy, afterDestroy
An infra service infra['docker-compose'].services.<name>.lifecycle the same seven

The page for it is docs/config/lifecycle.md: when each hook runs, the order for four common commands, and what a hook receives.

How it behaves

  • One context shape at every level: exec, log, data, envs, tokens, profile, config, workspaceDir, plus what the level adds (trigger, service, buildAddress, mode).
  • One data per operation, shared by every hook the operation runs.
  • Ready includes the hooks. A service is ready once its healthcheck passes and its afterStart and afterReady have finished. The dev reverse proxy forwards as soon as the healthcheck passes, so a hook can call its own service.
  • A failed start hook is reported. An application service reads failed (or stopped, after beforeStart), with the reason in stackctl status, the control API, the proxy's page and every wait. A session whose start hook fails ends its start and leaves the reason for status and wait.
  • A newer request abandons the hooks of the one it replaces. The stop hooks at a shutdown have 5 seconds a step.
  • A hook's commands end with stackctl, however it ends. Each exec command leads its own process group, held by a small shell that kills what is left once the stackctl process is gone, a kill -9 included. This covers a standalone stackctl infra command too.

Also in this change

  • A compose file's top-level name: names the Compose project as it is, with no profile suffix.
  • --wait-timeout bounds the whole of stackctl service start; stackctl service stop waits for a slow beforeStop.
  • stackctl status and GET /query/status name the hook a service still runs (serviceHooks).
  • The installed skill and rules file name hooks in their scope.
  • A docs rule, .claude/rules/docs-concise.md: keep a page to what a developer needs, and put detail only an agent needs in <llm-only>, which the site leaves out and the Markdown copies keep.
  • The demo, picker, run screen and getting started recordings are re-recorded.

Breaking

Each has an entry in the changelog with what to do:

  • An infra service's callbacks move under lifecycle.
  • data is shared across services within an operation.
  • A service's status can be failed.
  • A stop during a restart stops the service.
  • An afterDestroy failure reports the service destroyed, with the error.
  • A restart a stop cancels ends with outcome: 'cancelled'.
  • A wait's timeout reads not ready after <n>s.

For the reviewer

  • Merging this releases 0.12.0 (changelog.ts check says so).
  • Verified locally on top of Retry the control API's bind on another port when it is taken #30: build, lint and type-check; pnpm test 3266 passed, 1 skipped, six runs in a row; pnpm test:e2e 40 passed; the Docker integration spec 20 passed.
  • Where the risk is: src/session/supervisor.ts (the per-start run and what supersedes it), src/session/session.ts (the shutdown steps and their budgets) and src/spawn.ts (process groups, the guard, the standalone command's signals).
  • Not on Windows: there are no process groups there, so nothing guards a hook's command against a killed stackctl.
  • Left for later: a label showing which hook printed a log line, file:line on a hook's error, a failing stop hook reported by service stop, and a type for data.

NestJS's ConsoleLogger in JSON mode, and winston, write the log's text
under `message`. The default formatter only looked for `msg`, so their
logs showed a bare `[LEVEL]` header with the text listed as an ordinary
field. With no `fields.message` configured, the formatter now reads
`msg`, or `message` on a log that has no `msg`; a configured key still
wins.

Everything else in NestJS 12's JSON output already rendered: its level
names map onto the canonical levels, `params` (merged or flattened) is
listed field by field, and stack traces keep their line breaks.
A hook is an async function stackctl runs around one of its own steps.
The session, each application service, the Compose project and each
infra service take `beforeStart`, `afterStart`, `afterReady`,
`beforeStop` and `afterStop` under `lifecycle`; the Compose project and
infra services also take `beforeDestroy` and `afterDestroy`. Until now
only infra services had callbacks, four of them, at the service's top
level.

What a hook gets and what it can count on:

- One context shape at every level: `exec`, `log`, `data`, `envs`,
  `tokens`, `profile`, `config` and `workspaceDir`, plus what the level
  adds (`trigger`, `service`, `buildAddress`, `mode`).
- One `data` object per operation, shared by every hook the operation
  runs, at every level.
- A service is ready once its healthcheck passes and its `afterStart`
  and `afterReady` have finished. The dev reverse proxy forwards to it
  as soon as the healthcheck passes, so a hook can call its own service.
- A start hook that fails is reported, not swallowed: an application
  service reads `failed` (or `stopped`, after `beforeStart`), with the
  reason in `stackctl status`, the control API, the proxy's page and
  every wait. A session whose start hook fails ends its start, and
  leaves the reason behind for `status` and `wait`.
- A newer request abandons the hooks of the one it replaces, and the
  stop hooks at a shutdown have five seconds a step.
- A command a hook runs with `exec` leads a process group of its own,
  and ends with the stackctl process that ran it however that process
  ends. A small shell holds each group and kills what is left once
  stackctl is gone, since nothing ties a child's life to its parent's
  on macOS. This holds for `stackctl infra` run by itself too, where a
  `kill` or a Ctrl+C now reaches a hook's command and what it started.

Also in this change:

- A compose file's top-level `name:` names the Compose project as it
  is, like `COMPOSE_PROJECT_NAME`, with no profile added.
- `--wait-timeout` bounds the whole of `stackctl service start`, and
  `stackctl service stop` waits for a slow `beforeStop`.
- `stackctl status` and `GET /query/status` name the hook a service
  still runs (`serviceHooks`).

Breaking: an infra service's callbacks move under `lifecycle`; `data` is
shared across services; a service's status can be `failed`; a wait's
timeout reads `not ready after`. The changelog has what to do for each.

The docs get a page of their own, docs/config/lifecycle.md, with the
detail only an agent needs kept out of the site and in the Markdown
copies, and a rule for keeping pages short
(.claude/rules/docs-concise.md). The installed skill and rules file name
hooks in their scope. The demo, picker, run screen and getting started
recordings are re-recorded.
@rhyek
rhyek enabled auto-merge (squash) November 26, 2026 17:38
The test starts two commands that each print 200 lines and checks that
both appear among the first 200, to know their lines did interleave.
Each printed as fast as it could, in a few milliseconds, so on a busy
CI runner the first could finish before the second had started, and
the check failed with only `a` in the first half.

Both now start printing at one agreed instant and take a few hundred
milliseconds over it, so they overlap however unevenly their processes
come up.
`start --detach` and `startStack()` learn why a session's start failed
in one of two ways: from the session's events as they arrive, or from
its event log once it is gone. The first worded it "<reason>. See
<log>", the second "<reason>" alone, so which one a developer got
depended on timing. On CI the second came first, and the test that
pins the message failed.

Both give "<reason>. See <log>" now.
@rhyek
rhyek merged commit b70d2ad into main Nov 26, 2026
5 checks passed
@rhyek
rhyek deleted the feat/lifecycle-hooks branch November 26, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant