Repository navigation
Commit ed906d5
feat(spec, objectql): working-calendar service, arithmetic and refusal codes (ADR-0157, part 1 of 2) (#35641)
repo:objectstack#5 · os-bill · session_01Utp6Ca1jJvsA1dHsPwMiQb
Part of #35355
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
用户能看到什么变化:系统管理员和普通用户在界面上都看不到变化(用户无感);之前平台不会按上班日算期限,之后平台内部能算,但要等配套的日历设置页一起上线才用得上。
为什么做:属于「工作日历」,替公式作者和审批流程计算“几个工作日后”“几个工作小时后”,让节假日和调休上班日都算对。
出错会怎样:若算错,按工作日计算的到期日会差一天或落在休息日,审批提醒会提前或推迟;没指明组织的计算会被直接拒绝,不会借用别的组织的日历;这一半只新增、不改旧功能,可以直接撤回。
要你做什么:无。
## 席位核对
### Pair and release (ADR-0157 D4)
**Not released alone; pair #35641/#35642.** This is the first of two
pull requests for #35355, split by layer; its pair is #35642 (the two
calendar objects, Setup, import and translations), which lands right
after this one. Both ship in the same release as #35356 (the working-day
formula functions) and #35357 (the approval escalation's working-time
clock), per ADR-0157 D4. On its own this half registers a service that
refuses every resolution, because no calendar object exists until #35642
lands.
### Fix round 1 (contract review 7023325199, ruling record 7023403692)
- **M1, an absent organization.** Outside the `single` tenancy posture
(the engine's own reading, `resolveEnginePosture`), a computation whose
`organizationId` is absent is refused `ERR_WORK_CALENDAR_UNRESOLVED`
with reason `no_organization` before any read, the cache's included;
`single` is unchanged. The rule is stated in the `WorkCalendarRef`
jsdoc, the service header and the ADR anchor. The same rule binds the
hooks' elevated reads (the one-default check and the time-zone fill),
and the hooks take the organization from the session. When no posture is
passed, the service and the hooks read the posture of the engine they
run on, so SecurityPlugin's injected posture is the one in force.
- **S1, where the arithmetic lives.** The period parser,
`buildWorkCalendar`, the four counts, `isWorkingDay`,
`workingPeriodsOn`, `exceptionDayOf`, `calendarDayOf` and the three
error classes move to `@objectstack/formula`
(`packages/formula/src/work-calendar.ts`). The edge already existed:
`@objectstack/objectql` and `plugin-approvals` depend on formula, and
formula depends on spec, so no dependency edge is new. Spec keeps
`zonedWallClockToUtcMs`, the types, the constants and the ledger codes,
and its module header now states Prime Directive #2 as schemas, types
and constants only. The gantt's two ALLOW entries in
`ui/scripts/check-spec-symbol-derivation.mjs` are dropped, since spec no
longer exports those names; that file is back to its `main` bytes.
- **S2, the contract.** `IWorkCalendarService`, `WorkCalendarRef` and
`WORK_CALENDAR_SERVICE` are in
`packages/spec/src/contracts/work-calendar-service.ts`, beside the other
service contracts. The objectql class keeps its name and `implements
IWorkCalendarService`.
- **N1.** The 60-day lead's jsdoc says the next year's holidays are
usually published around or after November, and makes no claim about the
date.
- **N2.** #35642 is named by number here and in the changeset, both with
"Not released alone; pair #35641/#35642".
- **N3.** `buildWorkCalendar` returns a frozen calendar (the calendar,
the weekly lists, every period, the exceptions and each day), so a
cached calendar answers every caller alike.
- **The ledger entry.** It stays under `@objectstack/spec`. The three
code constants stay in spec, and `check:error-code-provenance`
attributes a code to the package whose constant spells it. Formula's
error classes stamp it by reference.
- **Semver.** Spec stays `patch` (`Metadata-protocol: none`);
`@objectstack/formula` is added to the changeset as `patch`.
### Main merge and fix round 2 (contract delta 7026479156, PASS with S3;
seat ruling: refuse)
- `28fca759a9` merges `main` (`03865ae995`, K=25); the merge regenerates
`api-surface/data.json` and `export-origins/data.json` and carries no
hand edit (contract delta 7026479156).
- `f502d9238c` (S3): the calendar insert hook refuses a create whose row
names an organization other than the session's and sets `is_default:
true`, before any read, with `400 VALIDATION_FAILED` (`is_default`,
`invalid_value`) and a reason that says how to proceed. A mismatched
create without a default passes as before. The module header says a
future seed that sets a default loads pinned to its organization or sets
it by an update. Pins: the member and system-writer cases now assert the
refusal, with a CONTROL for a plain create and a new pin for the update
route; ablation 3 red / 21 green, restored. `@objectstack/objectql`
(`--project local`) at `f502d9238c`: 617 files, 13947 tests passed. 2204
lines against `main`.
### Comments read (through 7023325199)
- **7021287976, the docs drift check.** No hand-written page under
`content/docs`, `skills` or `docs` (ADRs aside) names the moved names:
`git grep` for the arithmetic, the service, the contract constant and
`zonedWallClockToUtcMs` finds none. The reference pages are generated,
and `check:docs` passes.
- **7022981176, L1-a.**
- R1, the claim line for the ui script, is moot: that file is back to
its `main` bytes.
- Observations 1 to 3 were ruled in 7023403692 and are carried out
above: semver stays `patch`, M1, and the pair sentence.
- **7023325199, the contract review.** M1, S1, S2 and N1 to N3 are
answered item by item in the fix-round section above.
### What changes
- **Arithmetic**: `packages/formula/src/work-calendar.ts` (new).
`isWorkingTime`, `addWorkingDays`, `workingDaysBetween` and
`addWorkingHours` are pure functions over a resolved calendar, so #35356
can call them synchronously. `parseWorkPeriods` enforces D7 and D10: a
period crossing midnight is refused, `24:00` is a legal end, and
overlapping or malformed text is refused. `buildWorkCalendar` validates
a stored row and its exceptions, and builds frozen.
- **Types, constants and the time-zone rule**:
`packages/spec/src/data/work-calendar.ts` and `wall-clock.ts`.
`zonedWallClockToUtcMs`, `wallClockToUtcMs` and `WallClockParts` move
from `@objectstack/core` byte-identical, and core re-exports all three
(D11).
- **Ledger codes**: `ERR_WORK_CALENDAR_UNRESOLVED`,
`ERR_WORK_CALENDAR_NOT_MAINTAINED` and `ERR_WORK_CALENDAR_INVALID`, each
a 400.
- **Service**: `packages/objectql/src/work-calendar-service.ts`,
registered as `work-calendar` from the ObjectQL plugin. Under D2 it
takes the named code, else the organization default, else refuses, and
never falls back to wall-clock time. It reads elevated in the named
organization, refuses an absent one outside `single`, caches with a 60 s
TTL, drops the cache on any calendar or exception write, and never
caches a refusal.
- **Write door**: hooks on both objects, bound by name and inert until
#35642 registers the objects. Each of the following is refused `400
VALIDATION_FAILED` with an ADR-0114 field code: a period crossing
midnight, a malformed or overlapping period, `off` with periods, `on`
without, `maintained_through` before `maintained_from`, and a second
default. A create placed in an organization other than the session's
that sets Default is refused before any read; set the default by an
update, which is judged in the stored row's organization. A create
without `timezone` takes the organization's Localization zone. The hooks
take the organization from the session.
### Service API for #35356 and #35357
- **Contract**, `@objectstack/spec/contracts`:
- `IWorkCalendarService`: `resolve(ref)`, `isWorkingTime(ref, at)`,
`addWorkingDays(ref, day, days)`, `workingDaysBetween(ref, start, end)`
(start excluded, end included), `addWorkingHours(ref, start, hours)`
(returns epoch ms) and `invalidate()`.
- `ref` is `WorkCalendarRef`, `{ organizationId, calendar? }`.
- The service registers under `WORK_CALENDAR_SERVICE` (`work-calendar`);
the implementation is `WorkCalendarService` in `@objectstack/objectql`.
- **Sync**, `@objectstack/formula`: the same four computations taking a
resolved `WorkCalendar` first, plus `buildWorkCalendar`,
`parseWorkPeriods`, `isWorkingDay` and `workingPeriodsOn`. The
conversion is `zonedWallClockToUtcMs` from `@objectstack/spec/data`.
- **Refusals**, `@objectstack/formula`:
- `WorkCalendarUnresolvedError`, with reason `not_found`, `no_default`,
`ambiguous` or `no_organization`.
- `WorkCalendarNotMaintainedError` and `WorkCalendarInvalidError`.
- `isWorkCalendarRefusal`.
- The codes are `@objectstack/spec/data` constants.
### Tests
Head `e8af402891`. Each run below went through `os-verify-lock` and read
VERDICT `command-exit 0`. Commands are `--maxWorkers=2`. `--project
local` is the package's own `test` script.
- Builds: `@objectstack/objectql` plus its dependency closure; spec,
formula and core among them.
- Typecheck, exit 0: spec, formula, objectql and core.
- `@objectstack/formula`: 69 test files and 1902 tests passed.
- `@objectstack/objectql` (`--project local`): 617 test files and 13941
tests passed.
- `@objectstack/spec` (`--project local`): 857 test files and 27394
tests passed, with 1 todo. This was after `check:generated --fix`
rewrote and re-checked `api-surface/` and `export-origins/`.
- `pnpm --dir ui run check:spec-symbols`: exit 0, with 0 untriaged
collisions after the ALLOW entries were dropped.
New pins this round
(`packages/objectql/src/work-calendar-service.test.ts`, 23 tests;
`packages/formula/src/work-calendar.test.ts`, 14 tests):
- **The service, under `isolated` and under `group`.** A named code held
only by another organization is refused `no_organization`, and so is a
default held only by another organization. An empty-string organization
is refused the same way, and so is `addWorkingDays`. Each refusal
reaches HTTP as 400 `ERR_WORK_CALENDAR_UNRESOLVED`, with zero engine
reads. CONTROL: naming the holding organization resolves it.
- **The service, under `single`.** A computation without an organization
still reads, unscoped.
- **The posture source.** When none is passed, the service reads the
engine's injected posture.
- **The hooks take the organization from the session.** This runs under
`single` and under `isolated`, with a CONTROL showing the session's own
organization is read and judged. A system writer reads in its session's
organization too.
- **No organization, outside `single`.** Under `isolated`, an insert and
an update that name no organization read nothing. CONTROL: under
`single` the same writes are judged.
- **The frozen calendar.** The built calendar is frozen down to its
periods.
The double scopes each read by its context's tenant, as the engine does.
Ablations, one per new mechanism, through
`scripts/ablation-replace.mjs`. Every anchor went from 1 hit to 0 and
every blob moved. Each restore is proven by the blob equalling HEAD and
by an empty `git diff HEAD`. Subjects are imported by relative source
path, so there is no dist leg.
- Resolve guard off: 3 red (isolated, group, engine posture).
- Session rule off: 3 red (the three session pins).
- Insert hook guard off: 1 red.
- Update hook guard off: 1 red.
- Outer freeze off: 1 red.
- Period freeze off: 1 red.
### Gates
- **Derived families.** `node scripts/pm/dispatch-gates.mjs --commands`
at tree `e8af402891` derives 132 families. Each ran with its exit code
captured before any pipe, and all 132 exited 0.
- **Reconciliation** (`--ran`, each family recorded with its exit code):
`Run reconciliation — 132 derived, 132 run, 0 NOT-MEASURED, 0 UNRUN`.
The zero is derived, since every family carries an exit code.
- **Among them:**
- Error codes: `check:error-code-provenance`, `check:error-code-casing`,
`check:dispatcher-error-vocabulary`.
- Generated artifacts and docs: `check:api-surface`,
`check:export-origins`, `check:docs`, `check:generated`.
- `check:authorable-surface`: no authorable key moved, hence
`Metadata-protocol: none`.
- Test doubles: `check:where-matcher`, `check:objectql-double-limit`,
`check:engine-double-contract`, `check:query-options-erasure`.
- `check:system-context-census`, `check:dual-build-cjs-loads`,
`check:adr-anchors`, `check:nul-bytes`.
- **Outside the derived list.** `node
scripts/check-platform-checklist.mjs --changed-from origin/main`
(exported symbols moved) exits 0, and `overlap-check` gives `VERDICT
clean exit 0`.
- **Two reds on the first pass at `743be6587f`, both fixed in
`e8af402891`:**
- `check:system-context-census` flagged a new caller-keyed branch in the
insert hook. The hooks now take every writer's organization from the
session, so no branch is left.
- `check:where-matcher` could not lift the test double's matcher once
tenant scoping shared its callback. The scoping is now a step of its
own.
- **CI at `e8af402891`, read once:** 58 success, 10 skipped and 1 in
progress (Test Core 10/10), with no failure. Every Lint and Type Check
lane, Type Check UI, Build Core, Check Changeset and the PR-body guards
passed.
### Acceptance notes
- The service and hooks are inert until #35642 registers the two
objects. Until then, `resolve` refuses `ERR_WORK_CALENDAR_UNRESOLVED`
(`not_found` or `no_default`), and the hooks bind by object name.
- `main` is merged at `28fca759a9` (K=25); #35581 and #35583 shared
three files with this PR (`packages/spec/api-surface/data.json`,
`packages/spec/export-origins/data.json`,
`packages/spec/src/data/index.ts`), and the two generated files were
regenerated in the merge.
- #35657 (the ADR-0157 D1 note for `maintained_from`) stays outside both
PRs.
### Verification
- Read first:
- `packages/objectql/src/work-calendar-service.ts`: `resolve` (the
no-organization guard) and `insertReadOrganization` (the hooks'
organization).
- `packages/formula/src/work-calendar.ts`: `addWorkingHours` and
`requireMaintained`, the D8 and D11 core.
- `packages/spec/src/contracts/work-calendar-service.ts`: the contract.
- Two-minute check: `pnpm --filter @objectstack/objectql exec vitest run
src/work-calendar-service.test.ts`. Expect 24 passed, including the
`isolated` and `group` no-organization pins and the session pins.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01Utp6Ca1jJvsA1dHsPwMiQb)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 7c122ff commit ed906d5
25 files changed
Lines changed: 2065 additions & 139 deletions
File tree
- .changeset
- content/docs/references/api
- packages
- core/src/utils
- formula/src
- objectql/src
- spec
- api-surface
- export-origins
- src
- api
- contracts
- data
- scripts/adr-anchors
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
427 | | - | |
| 427 | + | |
428 | 428 | | |
429 | 429 | | |
430 | 430 | | |
| |||
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
578 | 581 | | |
579 | 582 | | |
580 | 583 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
189 | 192 | | |
190 | 193 | | |
191 | 194 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
353 | 353 | | |
354 | 354 | | |
355 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
356 | 359 | | |
357 | 360 | | |
358 | 361 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
19 | 32 | | |
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
23 | 36 | | |
24 | 37 | | |
25 | 38 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | 39 | | |
68 | 40 | | |
69 | 41 | | |
| |||
126 | 98 | | |
127 | 99 | | |
128 | 100 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | 101 | | |
222 | 102 | | |
223 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
198 | 218 | | |
199 | 219 | | |
200 | 220 | | |
| |||
0 commit comments