Repository navigation
Commit f6836ca
fix(cli): run an app's onEnable and its handlers from one module evaluation (#34297)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbTGhxJSYAmQXU7efVdNqrpw)_
Fixes #34179
Clause-②: no
Metadata-protocol: none (boot composition; no authorable key changes;
the dev confirms against the diff)
## 维护者速读
- 改了什么:从编译产物启动时,应用的 job/hook/action 处理函数改为和 `onEnable`
取自同一次加载的配置模块,不再一部分取自产物里打包的 runtime 模块(那是同一份源码的第二次求值)。
- 为什么改:`onEnable` 在模块级保存的状态(例如 duly 的数据引擎)在 job 里读不到,`os dev` 下 job 每次都报
"no data engine";实测非 host 配置的 `os start` 生产启动也一样。
- 风险与回滚:只影响从产物启动且配置带 `onEnable`
的启动;元数据仍取自产物。产物里有、源码里已没有的处理函数保持原样,并打印一次警告。回滚即 revert 本 PR。
-
先看:`packages/cli/src/utils/graft-runtime-hooks.ts`(`graftAuthoredRuntimeMembers`
的改指向逻辑),然后是 `packages/cli/src/commands/serve.ts` 的两处调用和警告文案。
- 两分钟验证:`pnpm --filter @objectstack/cli exec vitest run --project unit
src/utils/graft-runtime-hooks.test.ts`,预期 21 个用例全绿,其中 "a job handler and
a hook see the state onEnable bound" 通过。
- 需要你做的:无,席位落地。
## What changes
A boot that serves the compiled artifact gets the app's callables from
the artifact's runtime module (`dist/objectstack-runtime.*.mjs`).
`loadArtifactBundle` imports that module and `mergeRuntimeModule` merges
its `functions`. The module is an esbuild bundle of the same config
source, so it is a second evaluation of it, with its own module state.
serve then grafted the loaded config module's `onEnable` beside those
callables. So `onEnable` and the handlers ran from two evaluations, and
state that `onEnable` binds at module scope never reached a handler.
`graftAuthoredRuntimeMembers` (now async, still the one graft both serve
routes call) closes the split. The re-point runs only when the target
bundle has a `runtimeModule` and runs the config module's `onEnable`. In
that case every callable the runtime module supplied is replaced, ref
for ref, with the config module's own callable. The ref map is
`lowerCallables(normalizeStackInput(config)).functions`, the same
reading `os compile` writes the runtime module from. Each entry keeps
its declaration (`effect`, and so on), and no entry without a callable
gains one. The lowering is loaded lazily, so a boot without the split
does not load the TypeScript parser.
- **Doors fixed:** `os dev` on a host config (the `devArtifactDoor`
wrap) and on a non-host config, and `os start` / `os serve` on a
non-host config (the standalone stack plus the `hasAppPluginAlready`
graft). The production path was measured double-evaluating too.
- **Doors unchanged:** a boot from an artifact alone (no config module,
so no `onEnable` at all), and host `os start` (it already wraps the
module, one evaluation).
- **Stale artifact:** if the artifact names a ref the config no longer
serves (for example `os dev --no-compile` over an older build), that
callable stays on the runtime module. The boot then warns once and names
it.
Files:
- `packages/cli/src/utils/graft-runtime-hooks.ts`
- `packages/cli/src/commands/serve.ts` (both call sites, the warning,
the #31761 docblock)
- `packages/cli/src/utils/graft-runtime-hooks.test.ts` (pins; existing
cases now `await`)
- `packages/cli/test/normalized-call-sites.test.ts` (one `top-level` row
for the deliberate `bundle.functions` read, granted by the area)
- `.changeset/34179-dev-one-module-evaluation.md` (patch)
## Tests
The new pin composes the boot from the real producer to the real reader,
in the cli unit tier. It runs an in-process `os compile` on a fixture
whose `onEnable` binds a module-scope engine. A `functions` entry (the
shape a job `handler` names) and a hook read that engine. The pin loads
the artifact with the runtime's own `loadArtifactBundle`, loads the
config with serve's `bundleConfigModule`, and wraps it in a real
`AppPlugin`. It then reads handlers through `collectBundleFunctions`,
the map job and string-named hook handlers resolve against. It asserts
that both handlers see the engine. Further cases cover a stale ref
(reported, left alone), the array form, a bundle with no runtime module,
and a bundle whose own `onEnable` won (both untouched).
## Acceptance notes
- **Artifact-only boot:** it runs no `onEnable` because the runtime
module exports only `functions`. This is a single evaluation and the
documented behaviour (the job context hands `ql` for that reason), so it
is not this defect.
- **`--no-compile` with matching refs:** handlers now run the edited
source's code beside the artifact's metadata. That is the rule the
#31761 docblock states: the module contributes the members a JSON
artifact cannot carry. `onEnable` already came from the module there.
## Verification
Head e367244 (`origin/main` merged). Session
`session_01QsR2jTEivi2wogPpeo3xd5`.
<details><summary>Ablation: put the two-evaluation composition
back</summary>
`node scripts/ablation-replace.mjs` mutated the re-point line (anchor x1
to x0; blob 4ae519b to 250315e758f2), then ran the pin file:
```
× a job handler and a hook see the state onEnable bound
× re-points the refs the module serves and reports the ones it does not (an artifact older than its source)
× re-points the array form too, and never gives an entry without a callable one
Caused by: Error: Job 'one_dispatch' has no data engine
Tests 3 failed | 18 passed (21)
ablation-replace: ok restored: blob == HEAD (4ae519b) and `git diff HEAD` is empty
```
</details>
<details><summary>Real boots: a 1 s interval job writes the module state
onEnable set</summary>
Built `bin/run.js`, `OS_AUTOMATION_SCHEDULED_WORK_ENABLED=true`. The
"main" column is this build with the re-point line removed from `dist/`
(restored by sha256 afterwards). It is the same composition as `main`,
not a separate build.
| door | main | this PR |
|:--|:--|:--|
| host config, `os dev` | `unset` | `bound` |
| host config, `os start` | `bound` | `bound` |
| non-host config, `os dev` | `unset` | `bound` |
| non-host config, `os start` | `unset` | `bound` |
| artifact only, `os start` | `unset` (no onEnable runs) | `unset`
(unchanged) |
Host `os dev --no-compile` with refs matching: `bound`. With the job's
function renamed in source after compiling: `unset`, plus this warning:
```
⚠ 1 handler of the compiled artifact (mm_job) has no counterpart in objectstack.config.ts as this boot evaluated it, so it runs from ./objectstack-runtime.433af1218610613d.mjs, a second evaluation of
the same source: module state its onEnable sets is not visible there. Recompile (os compile) so the
artifact matches the config.
```
</details>
<details><summary>Local gates on e367244</summary>
- `pnpm --filter @objectstack/cli typecheck`
(`NODE_OPTIONS=--max-old-space-size=8192`): exit 0.
- cli unit project (`vitest run --project unit`): Test Files 423 passed
(423), Tests 6017 passed (6017).
- `pnpm lint`: exit 0.
- `dispatch-gates --commands` on the merged tree: 74 families, all exit
0, reconciled with `--ran` ("74 run, 0 NOT-MEASURED (a DERIVED zero)").
- `check:dual-build-cjs-loads` and `check:i18n-coverage` first exited 3
(workspace not built). Both read exit 0 after a whole-workspace build.
- `check:type-check-debt` first answered 99 (lock queue). It read exit 0
on the re-take.
- cli `integration` tier: declared to CI (no spawn entry or integration
file touched).
- Direct dependents of `@objectstack/cli`: declared to CI.
`graft-runtime-hooks` is not in the package's `exports`, so the
published surface is unchanged.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent e53e02e commit f6836ca
5 files changed
Lines changed: 424 additions & 53 deletions
File tree
- .changeset
- packages/cli
- src
- commands
- utils
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| |||
3876 | 3876 | | |
3877 | 3877 | | |
3878 | 3878 | | |
3879 | | - | |
| 3879 | + | |
| 3880 | + | |
| 3881 | + | |
| 3882 | + | |
| 3883 | + | |
| 3884 | + | |
| 3885 | + | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
| 3889 | + | |
| 3890 | + | |
| 3891 | + | |
| 3892 | + | |
| 3893 | + | |
| 3894 | + | |
| 3895 | + | |
3880 | 3896 | | |
3881 | 3897 | | |
3882 | 3898 | | |
| |||
3909 | 3925 | | |
3910 | 3926 | | |
3911 | 3927 | | |
| 3928 | + | |
| 3929 | + | |
| 3930 | + | |
| 3931 | + | |
| 3932 | + | |
| 3933 | + | |
| 3934 | + | |
| 3935 | + | |
| 3936 | + | |
| 3937 | + | |
3912 | 3938 | | |
3913 | 3939 | | |
3914 | 3940 | | |
| |||
3932 | 3958 | | |
3933 | 3959 | | |
3934 | 3960 | | |
3935 | | - | |
| 3961 | + | |
3936 | 3962 | | |
3937 | 3963 | | |
3938 | 3964 | | |
| |||
3960 | 3986 | | |
3961 | 3987 | | |
3962 | 3988 | | |
3963 | | - | |
3964 | | - | |
| 3989 | + | |
| 3990 | + | |
| 3991 | + | |
| 3992 | + | |
| 3993 | + | |
| 3994 | + | |
3965 | 3995 | | |
3966 | 3996 | | |
3967 | 3997 | | |
| |||
0 commit comments