Repository navigation
Commit 3d6dea2
feat(formula): working-day functions addWorkingDays, workingDaysBetween and addWorkingHours over the organization calendar (#36796)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} -->
_Requested by **Yibo Wang** · [project
thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwBEaJzgBWzjWCNjBjkVoeRto)_
Fixes #35356
repo:objectstack#3 · os-support-ai · session_01WGufzi4y7vb1rP9MERYbw2
Clause-②: yes (widening)
Metadata-protocol: added — a formula field's or a stored field's
`expression` may now call `addWorkingDays(...)`,
`workingDaysBetween(...)` and `addWorkingHours(...)`
Skills-docs: updated here — skills/objectstack-formula/SKILL.md teaches
the three functions and eval 8 of
skills/objectstack-project/evals/project-process.json exercises them
(the card's order names both)
## 维护者速读
- 用户能看到什么变化:管理员和建模人员在公式字段里可以按组织的工作日历算到期日和剩余工作日,节假日、周末和调休上班日都会被正确跳过或计入。
- 为什么做:工作日历系列的第三步,让服务时限和到期提醒不必再由各个应用自己写日历计算。
- 出错会怎样:日历缺失或维护期不够时该公式读为空并在服务端记一行日志,记录照常保存,不会按自然日乱算;要撤回只需回滚本次改动。
- 要你做什么:无。
## Review round 2 (L1-b 7118804738, contract 7118785761)
- **M1.** `Metadata-protocol: added` with `@objectstack/formula` at
minor (the three names are a new accepted value of an authored
`expression`, as with `rmbUpper`), and the `Skills-docs:` line, in this
body and the changeset.
- **S1.** `os build` now refuses the three in a field's expression
`defaultValue` (the default step has no calendar, so every insert would
land blank) and in a seed record's expression values at any depth (the
seed loader has none either), with the stored due-time prescription. New
module `packages/lint/src/validate-working-day-sites.ts`; a bare-string
`defaultValue` is a literal and draws nothing (CONTROL).
- **S2.** Already held: `planFormulaProjection` widens a named
projection to every stored column, `organization_id` included, and cuts
it back after evaluation. Now pinned: the stub driver honours `fields`,
and "a projection that leaves organization_id out still counts each row
in its own organization" asserts both the per-organization answer and
that the driver was asked for `organization_id`. Ablation: skipping
`organization_id` in the widening turns it red. Documented in the header
of `formula-work-calendars.ts`.
- **S3.** A kernel boot test: `ObjectKernel` + `ObjectQLPlugin({ ql:
engine })` serves `objectql` as the same engine and its formula counts
with the plugin-installed calendar service. Ablation: removing the
plugin's `setFormulaWorkCalendarService` call turns it red.
- **Open point 1 (option A stands).** The docs page and the skill each
say a blank stored due time stays blank until the record's next write;
the docs and changeset add that a write while the calendar refuses
blanks a previously valid value.
- **Nits.** N1 the skill's `timestamp(s)` line and `PRIOR()` note
reworded; N3 eval 8 says `{now}` for a filter and `now()` for a CEL
condition; N4 the catalog's `sys_work_calendar` row names all three
functions; N5 a blank start answers blank before any calendar is read
(test added).
- **#36673.** Still open at this head; this branch merges `main` once it
lands, per the seat.
## What changes
Three formula functions over the organization's working calendar
(ADR-0157 D4), with the evaluation contract this card had to settle.
- `addWorkingDays(day, n[, code])`: the calendar day `n` working days
after `day` (before it, for a negative `n`). `day` itself never counts,
and `0` answers `day`. The answer is a calendar day at UTC midnight, as
`today()` and a `date` field are.
- `workingDaysBetween(start, end[, code])`: the working days after
`start` up to and including `end`, negative when `end` is earlier. It is
the inverse: `workingDaysBetween(d, addWorkingDays(d, n)) == n`.
- `addWorkingHours(start, hours[, code])`: the instant `hours` of
working time after `start`, counted as elapsed time in the calendar's
time zone (D11, open point 3). A start outside working time begins at
the next working period's start.
All three count through `work-calendar.ts`, the arithmetic the calendar
service and the approval clock already use, so there is one walk and one
daylight-saving rule.
**Packages.**
- `@objectstack/formula`: `working-day.ts` registers the functions as
CEL stdlib over `EvalContext.workCalendars` (pure data, the existing
binding pattern). `working-day-needs.ts` reads, from an expression's
AST, which calendars it names: the default for a two-argument call, and
each quoted-literal code. `cel-to-filter.ts` refuses the three as a
filter (`CompileError('unsupported')`), prescribing the stored due-time
field. They join `CEL_STDLIB_FUNCTIONS`.
- `@objectstack/objectql`: `formula-work-calendars.ts` resolves exactly
the calendars a formula plan names, per row organization
(`organization_id`, else the read's tenant), through
`WorkCalendarService.resolve`, so its cache and invalidation are reused.
A refusal is handed over as data. Wired into find / findOne, the insert
and update responses, the summary-fold dependents and stored expressions
(`computeStoredExpressions`). The plugin installs the kernel's calendar
service beside the engine (`setFormulaWorkCalendarService`), and each
formula path reaches it through its host's `facade`, so `engine.ts`
stays at its facade line ceiling (6857) with no new member.
- `@objectstack/lint`: refuses the three at every expression site except
a formula field's and a stored field's `expression` (`check()`, the
declared flow predicate and value slots, a field's expression
`defaultValue`, seed values), and refuses a calendar code that is not a
quoted literal (`judgeFieldFormula`).
- `@object-ui/core`: a form treats a formula that calls one as
unservable, because the calendar is loaded for the server's read and
never handed to the form.
- Docs and skills: the formula docs page gains a Working days section;
objectstack-formula documents the three functions inside its existing
ceiling; the platform catalog drops the "Working-day formulas" Planned
row and names the formulas on `sys_work_calendar`'s row; project-process
eval 8 now expects `addWorkingHours(` and a stored due time, and refuses
`PLATFORM-CANDIDATE`.
**Contract choices (D4: the card's to settle, pinned by tests).**
- The calendar code is a quoted literal. The engine loads the calendars
an expression names before it evaluates it, so a computed code names a
calendar nobody loaded. To choose a calendar per record, branch between
literal calls. The lint refuses a computed code with that prescription.
- A blank start day or start instant answers blank (a due date with no
start has none), before any calendar is read. A blank count, or a blank
end of `workingDaysBetween`, refuses.
- Hook sandbox: no host function is added. A hook reads the stored
due-time field.
## Decision for review
**Open point 1: a stored field's `expression` that meets a D8 refusal
stores blank, and the write goes through, with one log line naming the
object, the field and the refusal.**
Why this option and not "the write fails":
- It matches D8's read behaviour: a formula field that meets the same
refusal reads `null` and logs once. A stored due time and a computed one
then answer the same way for the same calendar state.
- A failing write would block every record whose span crosses the end of
the maintained period, for example every case opened in late December
before an administrator enters next year's holidays. That is the
non-blocking stance ADR-0157 D9 takes for the approval clock.
- The column is not silently wrong: it is blank, never a weekly-pattern
or wall-clock guess, and the next write after the calendar is fixed
counts. The author-facing cost is said in the docs and the skill: a
blank stored due time stays blank until the record's next write, and a
write while the calendar refuses blanks a value that was valid before.
Pinned by `engine-formula-working-day.test.ts` › "a refused count stores
blank and the write goes through, with one line naming the field". The
alternative is one branch in `computeStoredExpressions`: rethrow instead
of storing blank.
## Tests
- `packages/formula/src/working-day.test.ts`: a `cn_mainland` calendar
(Asia/Shanghai, 09:00-12:00 and 13:00-18:00, National Day 10-01..07 off,
Saturday 10-10 on) against a weekly-only CONTROL calendar. Covers a
weekend, the National Day week, the 调休 Saturday, negative and zero
counts, date, `today()` and timestamp inputs, blank handling, the
inverse property, DST spring-forward and fall-back, every refusal (no
default, not maintained, not loaded, code not loaded), the needs
analysis, and the pushdown refusal with an `addDays` CONTROL.
- `packages/objectql/src/engine-formula-working-day.test.ts`: the real
`WorkCalendarService` over a stub driver. A read over the National Day
week onto the 调休 Saturday, a weekend on findOne, two organizations each
on its own default, the write response, D8 past `maintained_through` and
with no default (null, one log line, never a wall-clock count), no
service, D6 stored count and stored blank on refusal, the cache serving
until `invalidate()`, no calendar read for a projection whose formulas
call none, a projection without `organization_id` (S2), and a kernel
boot through `ObjectQLPlugin` (S3).
- `packages/lint/src/validate-expressions.working-day-35356.test.ts`:
accepted at a formula and a stored `expression` (including the branch
between literal codes); refused at a validation rule, an action's
`visible`, a flow condition, a sharing rule (both the expression lint
and the enforceability pass), a field's expression `defaultValue` and a
seed value; a computed code refused; `addDays` and bare-string CONTROLs.
- `authored-site-expression.test.ts` classifies the new gate passes and
the value-site walk; `validate-expressions.test.ts` excuses the new
import tail `sites`.
- `ui/packages/core/.../formulaRecord-35033.test.ts`: a working-day
formula and a formula reading it are unservable; `addDays` CONTROL
computes.
## Acceptance notes
- Release pairing (D4, same release as the objects): `npm view
@objectstack/objectql version` and `@objectstack/formula` read `18.6.0`,
which carries no calendar code. The changesets of #35355 and #35357 are
still pending on `main`, so this PR ships in the same release as them.
- Contract-review tier: `packages/spec` carries no function list or
schema that changes (its only diff is the catalog generator's row list,
`packages/spec/scripts/**`, unpublished). The published list that widens
is `@objectstack/formula`'s `CEL_STDLIB_FUNCTIONS` (three names).
Measured: not CONTRACT_REVIEW_TIER by the order's spec criterion; the
seat decides.
- Metadata-protocol `added`: no authorable key or
`authorable-surface/**` shard moves, but an authored `expression`
accepts three new function names (corrected in round 2 from `none`).
- File surface beyond the order's list, each declared: objectql engine
facets (`engine-find.ts`, `engine-insert.ts`, `engine-update.ts`,
`engine-security-resolvers.ts`, `engine-top-level.ts`, `plugin.ts`) and
the new `formula-work-calendars.ts`, because the formula read and write
paths are where the calendars are loaded; `engine.ts` and
`work-calendar-service.ts` are unchanged. `packages/lint/**` is the
measured expression-lint location. `ui/packages/core/**` keeps the form
from computing a formula the server's write-path check already calls
unservable.
- Skills readings (`skills/**`, published):
`skills/objectstack-formula/SKILL.md` 491 → 485 lines, 6090 tokens. Its
ceiling rises 6008 → 6090 (+82) under the standing ruling
「A:先削重复,余额按实测提高」 (record 6513306068): restatement was deleted first in
the same file (changed lines remove 789 priced bytes and add 1130), and
the balance the round-2 sentences need is raised to the measured value,
as the comment in `scripts/check-skills-token-ratchet.mjs` records.
`skills/**` makes this PR governed.
## Verification
Two-minute check: `pnpm --filter @objectstack/formula exec vitest run
src/working-day.test.ts` shows the National Day and 调休 Saturday cases
passing.
Read first (by risk): `packages/objectql/src/formula-work-calendars.ts`
(which organization a row counts in),
`packages/objectql/src/engine-top-level.ts` (`computeStoredExpressions`,
the open point 1 branch), `packages/formula/src/working-day.ts` (the
public contract).
<details><summary>Local gates (dispatch-gates --ran: 122 derived
families accounted for, 118 run, 4 NOT-MEASURED)</summary>
- Round 2 at 5e11588: formula `working-day.test.ts` 23 passed; objectql
`engine-formula-working-day.test.ts` 13 passed; lint
`validate-expressions.working-day-35356`, `validate-expressions`,
`authored-site-expression` 606 passed; `platform-catalog.test.ts` 20
passed; `tsc --noEmit` formula, objectql, lint green;
`check-skills-token-ratchet`, `check-facade-delegation-only`,
`check-nul-bytes`, `check-skill-links`,
`check-skill-identifier-liveness`, `check-skill-top-level-keys`,
`check-skill-frame-sync`, `check-changeset-fixed`,
`check-empty-changeset`, `check-changeset-no-major` green.
Round 1:
- All 139 derived commands run at the final source; green except the
following.
- NOT MEASURED (exit 3, need a built closure; CI builds it):
`check:skill-examples`, `check:dual-build-cjs-loads`,
`check:lean-entry-closure`, `check-plugin-teardown-shape --self-test`
(its positive control is pinned to a commit this shallow clone cannot
reach).
- `pnpm --dir ui exec vitest run scripts/`: 1 red,
`unit-registry-absence-collision` cannot import modules that resolve
`@objectstack/client`, which is unbuilt here. Environment, not this
diff; CI runs it built.
- `check:facade-delegation-only` was red on the first run (`engine.ts`
32 lines over its ceiling); fixed by moving the calendar wiring into
`formula-work-calendars.ts`, re-run green.
- Typecheck (`tsc --noEmit` and `check:test-typecheck`):
`@objectstack/formula`, `@objectstack/objectql`, `@objectstack/lint`
green.
- Suites: `@objectstack/formula` 2133 passed; `@objectstack/objectql`
14363 passed; `@objectstack/lint` 9475 of 9479 passed, the 4 reds were
two ledgers naming the new gate passes, fixed and both files re-run
green (592 passed); `ui/packages/core` 4769 passed, 27 skipped.
</details>
<!-- signed: repo:objectstack#3 · os-support-ai ·
session_01WGufzi4y7vb1rP9MERYbw2 -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent d9c799c commit 3d6dea2
32 files changed
Lines changed: 1690 additions & 44 deletions
File tree
- .changeset
- content/docs/data-modeling
- packages
- formula/src
- lint/src
- objectql/src
- spec/scripts/lib
- scripts
- skills
- objectstack-formula
- objectstack-project
- evals
- references
- ui/packages/core/src/evaluator
- __tests__
| 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 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
203 | 206 | | |
204 | 207 | | |
205 | 208 | | |
| |||
573 | 576 | | |
574 | 577 | | |
575 | 578 | | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
576 | 637 | | |
577 | 638 | | |
578 | 639 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
512 | 513 | | |
513 | 514 | | |
514 | 515 | | |
| 516 | + | |
| 517 | + | |
515 | 518 | | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
516 | 527 | | |
517 | 528 | | |
518 | 529 | | |
519 | 530 | | |
520 | 531 | | |
521 | 532 | | |
522 | | - | |
| 533 | + | |
523 | 534 | | |
524 | 535 | | |
525 | 536 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2810 | 2810 | | |
2811 | 2811 | | |
2812 | 2812 | | |
| 2813 | + | |
2813 | 2814 | | |
2814 | 2815 | | |
2815 | 2816 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
331 | 332 | | |
332 | 333 | | |
333 | 334 | | |
| 335 | + | |
334 | 336 | | |
335 | 337 | | |
336 | 338 | | |
337 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
338 | 359 | | |
339 | 360 | | |
340 | 361 | | |
| |||
717 | 738 | | |
718 | 739 | | |
719 | 740 | | |
| 741 | + | |
720 | 742 | | |
721 | 743 | | |
722 | 744 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
218 | 225 | | |
219 | 226 | | |
220 | 227 | | |
| |||
230 | 237 | | |
231 | 238 | | |
232 | 239 | | |
233 | | - | |
| 240 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
641 | 642 | | |
642 | 643 | | |
643 | 644 | | |
| 645 | + | |
| 646 | + | |
644 | 647 | | |
645 | 648 | | |
646 | 649 | | |
| |||
661 | 664 | | |
662 | 665 | | |
663 | 666 | | |
| 667 | + | |
664 | 668 | | |
665 | 669 | | |
666 | 670 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
75 | 106 | | |
76 | 107 | | |
77 | 108 | | |
| |||
212 | 243 | | |
213 | 244 | | |
214 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
215 | 261 | | |
216 | 262 | | |
217 | 263 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2222 | 2222 | | |
2223 | 2223 | | |
2224 | 2224 | | |
| 2225 | + | |
| 2226 | + | |
2225 | 2227 | | |
2226 | 2228 | | |
2227 | 2229 | | |
0 commit comments