Repository navigation
Commit 97ca257
feat(runtime): a sandboxed body can do time-zone date maths, through Intl.DateTimeFormat (#29031)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"}
-->
_Requested by **Leehom** · [project
thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X5pePkMPaas25B8dVLe6RiN)_
Fixes #28898
Clause-②: no
No published schema, type, export or error code moves. The one
`packages/spec` change is the job `handler` key's `.describe()` text
(and the reference page regenerated from it); the accept set is
identical. The sandbox's globals are not declared by any spec schema:
`ScriptBodySchema.source` is a string, and what it may call is the
runtime's answer, which is what changes here.
Metadata-protocol: none
What a metadata author can write is unchanged: no key, value, type or
default moves. A body `source` that calls `Intl.DateTimeFormat` was
already accepted at every door; it now runs instead of throwing
`ReferenceError`.
## 维护者速读
- 改了什么:沙箱(QuickJS)里有了
`Intl.DateTimeFormat`,完整的一整个构造器(`format`/`formatToParts`/`formatRange`/`formatRangeToParts`/`resolvedOptions`/`supportedLocalesOf`),结果由服务器自己的
`Intl` 算出,所以 IANA 时区和夏令时边界与服务器一致。`Intl` 的其它成员都不存在。钩子、动作、作业三种 body 共用同一个
runner,所以三者同时获得。`os build` 的降级判断随之按成员判:只用 `Intl.DateTimeFormat` 的
handler 现在能降级成 body,用 `Intl.NumberFormat` 等仍打包。作业 `handler`
的弃用说明补了一句"时区日期计算可用 `Intl.DateTimeFormat` 迁到 `body`"。
- 为什么改:duly#147 的派单作业要按每个职责自己的时区算当前周期,沙箱没有 `Intl`,作业就离不开已弃用的
`handler`。席位裁定走纯运行时路线,不给 `ctx` 加日期助手,作业上下文契约不动。
- 风险与回滚:跨边界只有纯值(一个 JSON 字符串 + 最多两个毫秒数进,一个 JSON
字符串出,在沙箱内解析),桥是闭包参数而非全局,"现在"取沙箱自己的 `Date.now()`,无新 I/O。回滚即 revert,无数据迁移。
-
先看:`packages/runtime/src/sandbox/sandbox-intl.ts`(垫片与宿主侧的值清洗、错误转译);`packages/cli/src/utils/detect-free-identifiers.ts`
的 `MEMBER_JUDGED_GLOBALS` 与 `collectReferences`
的成员分支;`packages/cli/src/utils/sandbox-globals-probe.test.ts` 的点号探针。
- 两分钟验证:`pnpm --filter @objectstack/runtime exec vitest run
src/sandbox/sandbox-intl.test.ts`,预期 `Tests 12 passed (12)`。
- 需要你做的:无,席位落地。
## What changes
- **`packages/runtime/src/sandbox/sandbox-intl.ts`** (new):
`installSandboxIntl(vm)` installs `globalThis.Intl` (non-enumerable,
like the built-in) with one member, `DateTimeFormat`: callable with or
without `new`, `supportedLocalesOf`, and the whole prototype (`format`
as a bound getter, `formatToParts`, `formatRange`, `formatRangeToParts`,
`resolvedOptions`, `Symbol.toStringTag`). The VM-side constructor keeps
only a plain-value snapshot of `locales` and of the option keys
`DateTimeFormat` reads; every call asks the host to answer with a host
`Intl.DateTimeFormat` built from that snapshot (a per-invocation cache
of at most 64 formatters, dropped with the VM).
- In: an op name, ONE JSON string, at most two numbers. The host
re-reads the JSON and keeps only the known option keys with primitive
values.
- Out: one JSON string, parsed inside the VM, so `resolvedOptions()` and
the parts arrays are VM objects. A host error becomes a VM `RangeError`
/ `TypeError` with the host's message: the body's own throw, never a
host refusal relayed.
- The bridge is a closure argument of the shim (no global), and the shim
captures `JSON`, `Date.now` and friends at install, so a body replacing
them cannot steer the host. `format()` with no argument takes "now" from
the VM's own clock.
- **`packages/runtime/src/sandbox/quickjs-runner.ts`**: `execute()`
calls `installSandboxIntl(vm)` right after `installCtx`. Every body kind
(hook, action, job, and the L1 expression path) goes through
`execute()`, so this is one seam and one global set.
- **`packages/cli/src/utils/detect-free-identifiers.ts`**: the measured
allowlist learns member granularity. New `MEMBER_JUDGED_GLOBALS =
{'Intl'}`; `collectReferences` records a non-computed `Intl.NAME` read
as `Intl.NAME`; `SANDBOX_GLOBALS` gains `Intl.DateTimeFormat`;
`NODE_ONLY_GLOBALS` swaps `Intl` for the ten other `Intl` members Node
22 has. A bare `Intl` use (`const I = Intl`, `Intl[k]`) or an unlisted
member is reported host-only, so that handler stays bundled (the safe
direction). A local or parameter named `Intl` still binds it.
- **`packages/cli/src/utils/extract-hook-body.ts`**: the host-only
refusal sentence says "of `Intl` it provides `Intl.DateTimeFormat` only"
when an `Intl` name is refused, and "would throw" where it said "would
throw ReferenceError" (a missing member throws `TypeError`).
- **`packages/spec/src/system/job.zod.ts`**: the `handler` docblock and
`.describe()` (the deprecation message) say time-zone date maths moves
to `body` through `Intl.DateTimeFormat`.
`content/docs/references/system/job.mdx` regenerated (`gen:schema &&
gen:docs`).
- **`content/docs/automation/jobs.mdx`**: one bullet under "What running
in the sandbox means": `Intl.DateTimeFormat` is there whole, nothing
else of `Intl` is, the feature test, and that hook and action bodies
share it.
- **`.changeset/28898-sandbox-intl-date-time-format.md`**:
`@objectstack/runtime`, `@objectstack/cli`, `@objectstack/spec` patch;
both declaration lines.
## Tests
- New `packages/runtime/src/sandbox/sandbox-intl.test.ts` (12, real
QuickJS). Every expected value is computed with the host's `Intl` in the
test, never a literal (CI's ICU differs from a container's).
- The card reproduction: `formatToParts` of two instants either side of
New York's 2026-03-08 spring-forward, in `America/New_York` and
`Asia/Shanghai`, equals the host's (anti-vacuity: the zone name changes
in New York and not in Shanghai).
- `format`, `resolvedOptions`, `formatRange`, `formatRangeToParts` equal
the host's; the period-start shape (calendar date of one instant in two
zones: `2026-03-08` vs `2026-03-09`); callable without `new`, bound
`format` getter, `instanceof`, both `toStringTag`s,
`supportedLocalesOf`; options read at construction.
- Errors: an invalid `timeZone` throws the host's `RangeError` text as a
VM `RangeError` (and uncaught, fails the run with that text);
`format(NaN)`, `formatRange(0)`, `formatToParts('not a date')` throw
what the host throws.
- Absent, not half-working: `Object.getOwnPropertyNames(Intl)` is
`['DateTimeFormat']`, `typeof Intl.NumberFormat` is `'undefined'`,
`Intl` is not enumerable.
- Isolation: answers are VM-made (`Object.prototype` /
`Array.prototype`), fresh per call; no global named like the bridge; a
foreign receiver throws `TypeError`; replacing `JSON.stringify` /
`Date.now` steers nothing.
- One seam: a hook body and an action body answer the same. Through the
job door: a `jobBodyRunnerFactory` job reports `period 2026-03-09`.
- New `packages/qa/dogfood/test/job-body-time-zone-date.dogfood.test.ts`
(1): a booted stack declares a job whose `body` inserts each zone's
calendar date of one instant; the job service triggers it, and `GET
/data/tzj_period` reads both zones' own dates (anti-vacuity: the two
dates differ; the job is listed as scheduled first).
- cli: `detect-free-identifiers.test.ts` (+3: `Intl.DateTimeFormat` has
no free name; the bare namespace is reported; an unlisted member is
reported), `extract-hook-body.test.ts` (+1: a handler using
`Intl.DateTimeFormat` lowers), `hook-body-lowering.test.ts` (+1: no
finding for it), `sandbox-globals-probe.test.ts` (dotted names probed as
namespace-then-member; +1: every member-judged namespace measures
present and owns every dotted member). The #14301 refusal pins move from
`Intl.DateTimeFormat` to `Intl.NumberFormat`, a member the sandbox still
lacks, so the class stays pinned.
<details><summary>Red first, green, ablations</summary>
- Red on base `a3640933` (new runtime test, unchanged source): `Tests 12
failed (12)`, each `ReferenceError: 'Intl' is not defined` (the hook
case: `hook 't' threw: ReferenceError: 'Intl' is not defined`).
- Green: `src/sandbox/` `Test Files 35 passed (35)`, `Tests 341 passed
(341)`.
- Ablation, runtime (`scripts/ablation-replace.mjs`, anchor
`installSandboxIntl(vm);` hit once, blob `f779c2c2` to `8af3c789`; the
test imports `src`): `Test Files 1 failed | 34 passed (35)`, `Tests 12
failed | 329 passed (341)`: exactly the 12 Intl cases. Restored: blob
`f779c2c2` == HEAD, `git diff HEAD` empty.
- Ablation, cli (anchor `'Intl.DateTimeFormat',` deleted from
`SANDBOX_GLOBALS`, the four files, all `src`): `Tests 4 failed | 136
passed (140)`: the three new lower-it pins and the named-cases pin. The
partition pin stays green, as it must: a name removed from a set is no
longer probed, which is why the named-cases pin exists. Restored: blob
`7df744bf` == HEAD, `git diff HEAD` empty.
- Dogfood, red leg on a rebuilt dist (call replaced by `void
installSandboxIntl;`, blob `f779c2c2` to `a8815ee9`; `pnpm --filter
@objectstack/runtime build`; `ablation-dist-preflight` "marker absent
from all 12 built files"): `Tests 1 failed (1)`, the job logged `job
'tzj_period_stamp' threw: ReferenceError: 'Intl' is not defined` and the
read was `{}`. Restored: blob == HEAD, `git diff HEAD` empty; restore
leg rebuilt, preflight "marker present in 2 built files", "working tree
clean against HEAD", `Tests 1 passed (1)`.
</details>
## Acceptance notes
- **Hook and action bodies share the runner** (measured: every
production call site of `hookBodyRunnerFactory`,
`actionBodyRunnerFactory` and `jobBodyRunnerFactory`, in `app-plugin.ts`
and `app-artifact-handlers.ts`, hands the body to a
`QuickJSScriptRunner`, one class in separate instances, and
`QuickJSScriptRunner.execute()` is the only place a VM is made; `git
grep "new QuickJSScriptRunner"` outside tests: 5 hits, all those
factories), so they get the same `Intl.DateTimeFormat`, as the order
rules. The L1 expression path goes through `execute()` too.
- **Why the cli moved in this PR.** `sandbox-globals-probe.test.ts`
measures the sandbox's globals against the lowering allowlist, so the
runtime change alone reds it (`Intl` measures present). Moving `Intl`
wholesale to the sandbox set would have lowered a handler using
`Intl.NumberFormat` into a body that throws, the #14301 failure class;
leaving it host-only would keep printing "Intl is not provided by the
sandbox", which this change makes false. Judging per member is the
smallest honest change, and it is why `DateTimeFormat` is whole: a
lowered handler calling `formatRange` must not throw where the
in-process one works.
- **Published value change (cli `hook-body` subpath).**
`HookBodyExtractionError.freeIdentifiers` / `nodeOnlyIdentifiers` now
name the member (`Intl.NumberFormat`) where they named `Intl`; the
error's shape and members are unchanged
(`published-subpath-hook-body.pin.test.ts` green). The refusal
sentence's host-only branch changed as listed above; the module-scope
sentence the docs quote is byte-identical (its pin is green).
- **What a body can learn.** `new
Intl.DateTimeFormat().resolvedOptions()` reports the server's default
locale and time zone, which a body could not read before. Values, not
objects; noted for the seat, not treated as a leak.
- **Not done, out of scope.** QuickJS's own
`Date.prototype.toLocaleString(locale, { timeZone })` exists and ignores
its arguments; a handler using it lowers today and formats in the server
zone. Not changed here (not class a/b/c with a measured reach). Dedupe
words: toLocaleString timeZone sandbox ignored.
- **Overlap.** Before opening: none of the 31 open PRs touches
`packages/runtime/src/sandbox/**`, the four cli files,
`system/job.zod.ts` or `automation/jobs.mdx` (file lists read per PR);
search for `quickjs`, `sandbox`, `detect-free-identifiers`, `Intl`, `job
body`, `hook body` among open PRs found no related one.
## Verification
Run on `aed5e295` (base `a3640933`). Each test command under
`os-verify-lock` (`VERDICT command-exit 0` unless stated).
<details><summary>Package suites, type checks, lint</summary>
- Builds: `pnpm --filter '@objectstack/cli^...' build` exit 0; `pnpm
--filter '@objectstack/dogfood^...' build` exit 0; `pnpm --filter
@objectstack/spec build` exit 0.
- `pnpm --filter @objectstack/runtime test`: `Test Files 514 passed
(514)`, `Tests 8279 passed | 19 skipped (8298)`.
- `@objectstack/cli` unit tier (`vitest run --project unit`): `Test
Files 366 passed`, 2 failed for `packages/cli is not built` (the two
`published-subpath-*.pin` files); after `pnpm --filter @objectstack/cli
build` those two: `Test Files 2 passed (2)`, `Tests 29 passed (29)`. So
368 of 368 files green. The integration tier is declared to CI (no
integration-tier file or spawn entry touched).
- `pnpm --filter @objectstack/runtime typecheck` and `pnpm --filter
@objectstack/cli typecheck`: exit 0, both `check:test-typecheck: OK`.
- Dogfood: the new file `Tests 1 passed (1)` on the built dist. The full
battery is not owed (no `!` in the title, no narrowing arm).
- `eslint --no-inline-config` over the eleven changed source and test
files: exit 0.
</details>
<details><summary>Derived gates</summary>
`node scripts/pm/dispatch-gates.mjs --commands` (derived on `aed5e295`
against merge base `a3640933`): 108 families, each run with `timeout 900
bash -c CMD` from `/dev/null`, exit captured before any pipe; record
`pm-orders/server/28898-gates-ran.txt` (`CMD :: exit N`). Reconciled:
`✓ dispatch-gates --ran: 108 derived famil(ies) accounted for — 108 run,
0 NOT-MEASURED (a DERIVED zero — all 108 recorded an exit code and none
of them is 3).`
All 108 exit 0, among them `check:docs` (`235 generated files in sync`),
`check:generated` (`All 14 generated artifacts are up to date`),
`check:authorable-surface`, `check:api-surface`,
`check:type-check-debt`, `check:nul-bytes`, `check:test-source-alias`,
`check:i18n-coverage`, `check:dual-build-cjs-loads` and
`check:skill-examples`. A first pass on `b4099da2` (before the dogfood
file) read 104 at 0 and 4 at 3 (prerequisite not met: the
plugin-teardown self-test needed its pinned commit fetched; the other
three needed builds of packages outside this diff); the final pass above
ran after `git fetch --depth=1` of that commit and the dogfood closure
build. Families CI owns (`Test Core`, the type-check lanes, `pnpm lint`)
are read on this PR's check-runs.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5paXMpX75J4HCKYU6Z)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 5d3667e commit 97ca257
14 files changed
Lines changed: 860 additions & 33 deletions
File tree
- .changeset
- content/docs
- automation
- references/system
- packages
- cli
- src
- lint
- utils
- test
- qa/dogfood/test
- runtime/src/sandbox
- spec/src/system
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
194 | 202 | | |
195 | 203 | | |
196 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | | - | |
56 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
57 | 70 | | |
58 | 71 | | |
59 | 72 | | |
| |||
119 | 132 | | |
120 | 133 | | |
121 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
122 | 139 | | |
123 | 140 | | |
124 | 141 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
201 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
202 | 205 | | |
203 | | - | |
| 206 | + | |
204 | 207 | | |
205 | 208 | | |
206 | | - | |
207 | | - | |
| 209 | + | |
| 210 | + | |
208 | 211 | | |
209 | 212 | | |
210 | 213 | | |
211 | 214 | | |
212 | 215 | | |
213 | 216 | | |
214 | 217 | | |
215 | | - | |
216 | | - | |
| 218 | + | |
| 219 | + | |
217 | 220 | | |
218 | 221 | | |
219 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
220 | 249 | | |
221 | 250 | | |
222 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
| |||
92 | 97 | | |
93 | 98 | | |
94 | 99 | | |
95 | | - | |
96 | | - | |
97 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
98 | 106 | | |
99 | 107 | | |
100 | 108 | | |
| |||
113 | 121 | | |
114 | 122 | | |
115 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
116 | 140 | | |
117 | 141 | | |
118 | 142 | | |
| |||
268 | 292 | | |
269 | 293 | | |
270 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
271 | 305 | | |
272 | 306 | | |
273 | 307 | | |
| |||
346 | 380 | | |
347 | 381 | | |
348 | 382 | | |
349 | | - | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
350 | 387 | | |
351 | 388 | | |
352 | | - | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
353 | 392 | | |
354 | 393 | | |
355 | | - | |
| 394 | + | |
356 | 395 | | |
357 | 396 | | |
358 | 397 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
711 | 711 | | |
712 | 712 | | |
713 | 713 | | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
714 | 719 | | |
715 | 720 | | |
716 | 721 | | |
717 | 722 | | |
718 | | - | |
| 723 | + | |
719 | 724 | | |
720 | 725 | | |
721 | 726 | | |
| |||
0 commit comments