Repository navigation
Add lifecycle hooks for the session, services and infra - #31
Merged
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
Lifecycle hooks: async functions in
stackctl.config.tsthat stackctl runs around its own steps. Four things take them, each under its ownlifecycle:session.lifecyclebeforeStart,afterStart,afterReady,beforeStop,afterStopservices.<name>.lifecycleinfra['docker-compose'].lifecyclebeforeDestroy,afterDestroyinfra['docker-compose'].services.<name>.lifecycleThe 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
exec,log,data,envs,tokens,profile,config,workspaceDir, plus what the level adds (trigger,service,buildAddress,mode).dataper operation, shared by every hook the operation runs.afterStartandafterReadyhave finished. The dev reverse proxy forwards as soon as the healthcheck passes, so a hook can call its own service.failed(orstopped, afterbeforeStart), with the reason instackctl status, the control API, the proxy's page and every wait. A session whose start hook fails ends its start and leaves the reason forstatusandwait.execcommand leads its own process group, held by a small shell that kills what is left once the stackctl process is gone, akill -9included. This covers a standalonestackctl infracommand too.Also in this change
name:names the Compose project as it is, with no profile suffix.--wait-timeoutbounds the whole ofstackctl service start;stackctl service stopwaits for a slowbeforeStop.stackctl statusandGET /query/statusname the hook a service still runs (serviceHooks)..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.Breaking
Each has an entry in the changelog with what to do:
lifecycle.datais shared across services within an operation.failed.afterDestroyfailure reports the servicedestroyed, with the error.outcome: 'cancelled'.not ready after <n>s.For the reviewer
changelog.ts checksays so).pnpm test3266 passed, 1 skipped, six runs in a row;pnpm test:e2e40 passed; the Docker integration spec 20 passed.src/session/supervisor.ts(the per-start run and what supersedes it),src/session/session.ts(the shutdown steps and their budgets) andsrc/spawn.ts(process groups, the guard, the standalone command's signals).file:lineon a hook's error, a failing stop hook reported byservice stop, and a type fordata.