Repository navigation
Commit da50177
feat(ai): a context pack's related list takes where / orderBy / limit (ADR-0160 row 11) (#37354)
Fixes #37203
Clause-②: yes
Metadata-protocol: added
Skills-docs: follow-up #37177
## 维护者速读
- 用户能看到什么变化:AI
读取记录资料包时,关联列表可以按作者写的条件筛选、排序并限制条数;条件或排序用到提问人不能查询的字段时,这一段被省略并附一句「部分资料对你不可见」,不再整次报错。
- 为什么做:记录范围 AI 问询专题的第 11 行(关联列表的筛选和排序),按维护者已同意的设计修订实现。
- 出错会怎样:最坏是某个关联段落被省略或条数不对,AI 回答少了资料;不会多给提问人无权看的数据;撤回此 PR 即恢复原行为。
- 要你做什么:无。
## What changes
**Spec (`packages/spec/src/ai/ai-context.zod.ts`).** A `related` entry
gains three optional keys, per ADR-0160 D1 (second amendment):
- `where`: the object filter form on the child's own fields, held to
`AI_CONTEXT_WHERE_OPERATORS` (`$eq $ne $in $nin $gt $gte $lt $lte $null
$exists`) joined by `$and` / `$or`. A bare value is `$eq`. Refused at
parse: any other operator, a dotted path, a condition on a lookup's
target, `{ $field }`, a `$now` anchor, a formula/object comparand, and a
comparand that is one whole brace placeholder (matched by
`FILTER_TOKEN_WRAPPED_RE`). Built as `FilterConditionSchema` plus one
refinement walk, so the base form's own comparand refusals still apply.
- `orderBy`: `SortNodeSchema[]` (`{ field, order }`, `direction` refused
by name), with the field held to a snake_case own-field name.
- `limit`: positive integer up to `AI_CONTEXT_MAX_RELATED_ROWS` (20).
This is the one number. `RECORD_CONTEXT_RELATED_MAX_ROWS` in the service
now reads it (ADR-0160 row 11: "the schema that bounds `limit` and the
service that cuts the list read one number").
- `aiContextWhereFields(where)`: lists the fields a `where` names. The
service and the lint rule both use it.
- Near-miss aliases: `filter` → `where`, `sort` / `sortBy` → `orderBy`.
The schema-level `guidance.filter` now points at the entry's `where`.
**Render service (`packages/services/service-record-context`).**
- The one capped child query is `where: { $and: [linkFilter,
entry.where] }`, with `orderBy` and `limit: (entry.limit ?? cap) + 1`. A
cut at `limit` is marked `truncated` and stated (`related_truncated`).
Still one query.
- Before that query, each `where` / `orderBy` field is judged. A field
the child's published definition lacks leaves the section out with
nothing stated (ADR point 20). A field the caller cannot query on drops
the section with the one `not_visible` statement, and the field is never
named (D3, point 19). Neither case refuses the call.
- "Cannot query on" is the door's new optional `queryableFieldsOf` (the
security service's `getQueryableFields`). Without that answer, the
contract's fallback applies (`ISecurityService.getQueryableFields`): the
field must be served, must not be withheld (`fieldVisibilityOf`), and
must declare no `maskingRule`. A probe that throws drops the section
with the statement.
- Triage row 7154563681 on the card (a related child that *this caller*
is not served is left out, not refused) is **not** in this PR. It was
implemented and then reverted in `aa76b901b`, because turning it on
flips the real-kernel parity pin in `packages/runtime` (landed in
#37350, case titled "[today; ADR-0160 as amended says left out and
stated]"), and `packages/runtime` is fenced here. Today's behaviour (the
whole call is refused) is unchanged. It moves to a follow-up card.
- The read set (`recordContextReadSet` / `planObjectsOf`) does not
change (D4), and a test pins it with and without the keys.
**Author surfaces (`packages/lint/src/validate-ai-context.ts`).** This
extends the #37176 rule rather than adding a validator. A `where` or
`orderBy` field the child does not define is reported as
`ai-context-field-unknown` (warning) at save and by `os validate`, and
the message says the list is left out.
**Review delta S1/S2 (contract record 7177225249, on bb0107c).**
- S1: `AI_CONTEXT_WHERE_MAX_DEPTH` (32) bounds `$and` / `$or` nesting in
`where` at parse, refused at the list that would carry a condition past
it. `aiContextWhereFields` walks to that same constant, so every field
of a `where` that parsed is judged by the service and the lint rule. A
test pins the bound: 32 parses and names the innermost field, 33 is
refused once.
- S2: an `orderBy` field the child's sortability projection calls
unsortable (`resolveObjectSortability`: a formula, or `sortable: false`)
is an author defect. The executor leaves the section out and states
nothing, matching the "field the definition lacks" arm and ADR D1,
instead of letting the door refuse the whole call with `INVALID_SORT`.
`validateAiContext` reports it at save and by `os validate` as the new
warning `ai-context-sort-field-unsortable`, with an `os explain` entry.
HTTP door, read only (runtime is fenced): `recordContextMembers` →
`bridge.query` → `callData('query')` → `protocol.findData` →
`assertSortFieldsExist` refuses it with `400 INVALID_SORT` too (formula
by the unmaterialized-type verdict, `sortable: false` by
`assertSortFieldsAreOffered`), so before this fix both transports
refused the call. After it, neither door is asked.
**Liveness, example, generated.** `liveness/ai_context.json` gains
`related.where`, `related.orderBy.{field,order}` and `related.limit`.
The showcase `KIND_COVERAGE` pack `showcase_project_brief` now carries
open tasks filtered by `status $ne done`, ordered by `due_date`, `limit:
10`. Regenerated: `dropped-refinements.baseline.json` (the `where` walk
and the `orderBy` field check are refinements JSON Schema cannot carry;
the build printed these rows), authorable-surface, api-surface,
export-origins, declaration-map, reference docs, state-counts.
## Tests
<details><summary>Local runs (final head bb0107c; the derived gate
sweep re-run on bb0107c)</summary>
- `pnpm --filter @objectstack/service-record-context test`: 2 files, 99
tests passed. The new describe block "a related list's where, orderBy
and limit" has 18 cases.
- `pnpm --filter @objectstack/spec exec vitest run
src/ai/ai-context.zod.test.ts`: 23 passed (parse admits and refuses
every bullet of What 1, and the nesting bound).
- `pnpm --filter @objectstack/lint exec vitest run
src/validate-ai-context.test.ts`: 18 passed. Whole package `pnpm
--filter @objectstack/lint test`: 235 files, 9633 tests passed.
- `pnpm --filter @objectstack/mcp exec vitest run
src/mcp-record-context-tools.test.ts` (read-only run of the #37140
parity harness against the rebuilt service, at 16b86ef): 14 passed. CI
Test Core at bb0107c runs it and the runtime real-kernel harness:
green.
- Typecheck: `@objectstack/spec`, `@objectstack/service-record-context`
and `@objectstack/lint` all exit 0.
- `pnpm --filter @objectstack/spec check:generated`: all 14 artifacts up
to date.
Ablations: five, each through `scripts/ablation-replace.mjs` with the
blob restored to HEAD. Each suite imports `src`, so there is no dist
step.
1. Skip `queryableAmong`: 6 cases red (the four unqueryable cases, the
masking fallback, the throwing probe).
2. Drop the `$and` join: 4 cases red, including "no where reaches
another root's rows".
3. S2, skip the executor's sortability check: 2 cases red (`sortable:
false`, formula), the door's `INVALID_SORT` reached.
4. S1, drop the parse depth bound: 1 case red (the bound test).
5. S2, skip the lint verdict: 2 cases red (the rule test and its
save-door family). A first attempt was refused by the tool as a no-op
(ambiguous replacement) and re-run with a unique one.
</details>
## Acceptance notes
- **Parity on both transports with a `where` pack is not pinned here.**
The #37140 harness is
`packages/mcp/src/mcp-record-context-tools.test.ts`, which was fenced
for this PR while sibling #37175 was in flight in `packages/runtime/**`
and `packages/mcp/**`. Both doors hand the one executor a door whose
`query` passes the options through (`recordContextMembers`, the stdio
bridge's `query` already takes `orderBy`), and the existing harness runs
green against this build. The pin is a follow-up for the report.
- **Neither MCP door hands `queryableFieldsOf` yet**, so on real doors
the contract fallback decides (masking-declared, hidden and withheld
fields count as not queryable). A field that the security service's
answer would exclude but the fallback admits (an agent acting on
someone's behalf, or a computed field reading a hidden one) still
reaches the engine guard, and the guard refuses the call. This is
reported as a finding for the door lane.
- Studio's `ContextPackRelatedEntries` does not offer the three keys
yet; it preserves them on edit (note 7168171718). That goes to #37143.
- `RecordContextNoteKind`'s doc line still says `related_truncated` is
"cut at the row cap". It now also fires at `limit`. This is spec prose
outside the claim's one spec file and is left as is.
- A door whose own page cap is below `limit + 1` cuts silently. That is
pre-existing for the cap too, and left as is.
- `where` nesting is bounded (S1). No bound on the number of conditions
or `$in` members was set; ADR row 11 allows one, but none was needed.
## Verification
Two-minute check: `pnpm --filter @objectstack/service-record-context
exec vitest run src/__tests__/record-context-tools.test.ts`. Expect it
green, with the "a related list's where, orderBy and limit" block green.
Read first: (1) `record-context-tools.ts`, the 4b block that judges
`queried` fields before `relatedPlans.push`, and the `$and` query; (2)
`ai-context.zod.ts`, `checkRelatedWhere`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01Utp6CbeF2saT6wUBV4E9uc)_
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6CbeF2saT6wUBV4E9uc -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent db77cc4 commit da50177
23 files changed
Lines changed: 1092 additions & 52 deletions
File tree
- .changeset
- content/docs/references
- ai
- examples/app-showcase/src
- ai
- packages
- lint/src
- services/service-record-context/src
- __tests__
- spec
- api-surface
- authorable-surface
- declaration-map
- export-origins
- json-schema.manifest
- liveness
- state-counts
- src/ai
| 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 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
| |||
79 | 82 | | |
80 | 83 | | |
81 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
82 | 95 | | |
83 | 96 | | |
84 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | | - | |
17 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
26 | 36 | | |
27 | 37 | | |
28 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
| 220 | + | |
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
| 387 | + | |
387 | 388 | | |
388 | 389 | | |
389 | 390 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6060 | 6060 | | |
6061 | 6061 | | |
6062 | 6062 | | |
| 6063 | + | |
| 6064 | + | |
| 6065 | + | |
| 6066 | + | |
| 6067 | + | |
| 6068 | + | |
| 6069 | + | |
| 6070 | + | |
| 6071 | + | |
| 6072 | + | |
| 6073 | + | |
| 6074 | + | |
| 6075 | + | |
6063 | 6076 | | |
6064 | 6077 | | |
6065 | 6078 | | |
| |||
7049 | 7062 | | |
7050 | 7063 | | |
7051 | 7064 | | |
| 7065 | + | |
7052 | 7066 | | |
7053 | 7067 | | |
7054 | 7068 | | |
| |||
0 commit comments