Repository navigation
feat(mcp): aggregate_records orders, cuts and counts groups in the database - #35193
Conversation
…iver's statement (wip) Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…ver HTTP and stdio; changeset Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…gregate-order-cut
…literal refusal codes; alias-clean test imports Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…e order into the cut An AggregateOrderKey may carry rank, a list of the column's values: the SQL driver orders by each listed value's position (case ... when ? then i ... end), every other value after them, before the cut. The bridge typings carry it so a declared option order read off the served schema can reach the statement. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8
📓 Docs Drift CheckThis PR changes 5 package(s): 43 hand-written doc(s) name something this change touched — list omitted above 15 rows. Re-derive on the tree named below: ⛔ 12 release-owned page(s) also affected — read-only, see AGENTS.md Documentation Guardrails. What this run could not see
Coarse fallback — 56 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 32ac78af08ebaa367bd2ebbf97c9536948275160 && git checkout 32ac78af08ebaa367bd2ebbf97c9536948275160
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin b02484e13a2e43f12d2c942e9a8966ece9cfa31e 5839005b034da9ceec1329a3e9c288625ca8e717 && git checkout -B drift-repro b02484e13a2e43f12d2c942e9a8966ece9cfa31e && git merge --no-ff 5839005b034da9ceec1329a3e9c288625ca8e717
node scripts/docs-audit/affected-docs.mjs --json b02484e13a2e43f12d2c942e9a8966ece9cfa31e
|
…ate order and cut Resolves the two conflicts in packages/mcp (mcp-http-tools.ts and the aggregation-entry test) and carries the agreed items as the second PR to merge main: - A limit with no orderBy hands the database the declared option order of the leading select/radio group keys, each as an order key's rank, derived by the uncut path's own declaredOptionRank (the leading-key reading is now one helper, declaredOptionOrderKeys, used by both paths). - The cut result carries currencies (fixedAmountCurrencies) like the uncut one, and keeps groups on the uncut path. - Seat ruling: the result keeps totalGroups and adds no groupCount; on the cut path totalGroups is the statement's count of the groups before the cut. The engine outcome, the bridge member and the driver directive spell it totalGroups too. - Pins: the limit-only push-down (tool and stdio engine), the explicit-order CONTROL, currencies on the cut result, and no groupCount in a served result. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…ds call sends, over a real SQLite database Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
… the aggregate order and cut Five adjacent-insertion conflicts, each resolved keeping both sides: the engine exports and the runtime imports of AGGREGATE_ORDER_CUT beside LATEST_PER (objectql index, runtime action-execution and domains/mcp), the two Symbol.for read options in the stdio bridge, and the aggregation-entry test, where the order-and-cut describe block and #35202's lookup-label block both land after the served-schema block. mcp-http-tools.ts merged without conflict. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…side its id An aggregate_records call with orderBy or limit now answers each lookup or master-detail group key with KEY__label exactly as the default path does: both paths call one local labelServed closure over withLookupGroupLabels, applied to the rows served, after the cut, so the name read never covers the probe row past the cap. Pin: an orderBy+limit call grouped by a lookup returns the labelled rows, one read for the served ids only. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
…totalGroups, #35179 withheld fields) into the aggregate order and cut Three adjacent-insertion conflicts, each resolved keeping both sides: the McpDataBridge members aggregateGroups and #35198's userTextLocales (the interface in mcp-http-tools.ts and the runtime HTTP bridge in domains/mcp.ts), and both members in the write-response tripwire roster. #35179's withheld-field naming applies to query_records and get_record only and merged without conflict; #35201 touches service-analytics only. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Head for review · 2026-11-18T16:39Z: |
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Contract-review-claim: session_01JQwxbWDsbbJizuYnDwBxT8 · head |
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Contract reviewServed-tier: Read: card #35144 (its body and all 11 comments, including the claim, the hand-on, seat ruling 6978086158, carry note 6980343159 and the four dev reports), the PR body, the 17-file list, the net diff ① Derived judgments
② Semver level
③ Boundary flagsFindings
SQL push-down (AGENTS.md Prime Directive 5)
Answers to the dev reports
Gates (check-runs at the head, read once at 16:47Z without waiting)
No must-fix. Should-fix: S1, S2, S3. Implemented-by: VERDICT: PASS |
… should-fixes Contract review S1: a column named by a rank key now stays among the directive's trailing ascending keys (NULL last), so the values the rank does not list, which share one rank, keep a deterministic order and a cut among them answers the same groups on every run. Pinned over real SQLite in runtime (two unlisted values and a NULL at a cut boundary, same answer twice, the tie-break in the statement); the engine and stdio directive pins now show the ranked column among the trailing keys. S2: the aggregate_records description states that a per-record-currency amount ranks by bare numbers across currencies and says to rank inside one currency by filtering on the currency code field; pinned in tools/list. S3: a runtime pin with a registered read middleware narrowing ast.where: the cut rows and totalGroups count only the admitted groups, the uncut aggregate agreeing. N1: the changeset, the engine docblocks and the driver comments say what a rank does in each direction and how the unlisted values order. N3: a pin measures the tool's rank maximum against the engine's at the boundary (500 sent and served, 501 not sent and refused). N4: the stdio read-option constants sit above unwrapRows' docblock. The changeset also names KEY__label on the cut answer. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
The engine call in the boundary pin passes a typed query and spells the symbol read option as a deliberate off-contract cast, so the query-options-erasure test surface stays at its ceiling (236). Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Head for delta review · 2026-11-18T18:24Z: |
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Contract-review-claim: session_01JQwxbWDsbbJizuYnDwBxT8 · head |
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Contract reviewServed-tier: This is a DELTA review over PASS record 6985265817 at Read: record 6985265817, head note 6987666833 on this PR, dev report 6987634040 on card #35144, the PR body at this head, the delta ① Derived judgments
② Semver level
③ Boundary flagsFindings
SQL push-down (AGENTS.md Prime Directive 5)
Answers to the dev report (6987634040)
Gates (check-runs at the head, read once at 18:27Z without waiting)
No must-fix, no should-fix. Implemented-by: VERDICT: PASS |
|
repo:objectstack#1 · os-warren · session_01JQwxbXr9wLAH24MEgxaLud 席位核对L1 full · ACCEPT · Head-sha: Net diff at Against the card
Read at the head
Round 5 delta (
|
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Landing · 2026-11-18T19:03Z · head
Status: ready + auto-merge. |
…e order and cut One conflict, in packages/mcp/src/stdio-data-bridge.ts, resolved keeping both sides: the two Symbol.for read-option constants (this branch's STDIO_AGGREGATE_ORDER_CUT and #35199's STDIO_LATEST_PER), each under its own docblock, then #35225's stdioExpandMap byte-identical to main, then unwrapRows under its own docblock. mcp-http-tools.ts and runtime domains/mcp.ts merged without conflict. Claude-Session: https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8 Co-authored-by: Claude <noreply@anthropic.com>
|
repo:objectstack#1 · os-warren · session_01JQwxbXr9wLAH24MEgxaLud 席位核对L1 delta · ACCEPT · Head-sha: What the delta changes
Read at the head
Not mine, still owed
|
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Contract reviewServed-tier: This is a DELTA review over PASS record 6987793676 at ① Derived judgments
② Semver level
③ Boundary flags
No must-fix, no should-fix. Implemented-by: VERDICT: PASS |
|
repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 Landing · 2026-11-18T21:15Z · head
Status: ready + auto-merge. |
Fixes #35144
Clause-②: yes (widening:
orderByandlimitarguments onaggregate_records)Metadata-protocol: none
The card's
query_datasethalf is split to #35194 (seat ruling 6978086158, Q1), which carries the group total onQueryDatasetOutputSchema; this PR closes the card on theaggregate_recordshalf. Per the same ruling (Q2 = C) the answer keeps the publishedtotalGroups(the groups before the cut) and adds no second key.维护者速读
aggregate_records新增orderBy与limit。排名类问题(如“收入前五的客户”)由数据库一条语句完成排序、截断和分组计数;结果沿用已发布的totalGroups(截断前的分组总数),并在有截断时给出truncated: true。只给limit不给orderBy时,按单选/radio 字段声明的选项顺序在数据库里排序后再截断(与 feat(mcp): aggregate_records returns select groups in declared option order and names a fixed amount's currency #35186 的无序默认路径同一条规则);固定币种金额在截断结果上同样给出currencies;按 lookup/主从字段分组时,截断结果与默认路径一样在键旁给出KEY__label(以调用者权限读取的记录名称,feat(mcp): aggregate_records names a lookup group's record beside its id, read as the caller #35202 的同一个withLookupGroupLabels)。having的聚合一律以 INVALID_QUERY/400 拒绝,不会退化成内存排序。不带新参数的调用行为与 main 完全一致。回滚即 revert 本 PR。窗口函数count(*) over ()要求 MySQL 8 及以上。packages/drivers/driver-sql/src/sql-driver-aggregation.ts(派生表加 ORDER BY/LIMIT/窗口计数、rank 编译成 CASE 的那段)、packages/mcp/src/mcp-http-tools.ts(截断路径:未给orderBy时由declaredOptionOrderBy推导声明顺序、探测组与truncated、currencies)、packages/objectql/src/aggregate-order-cut.ts(判定谁能执行、哪些情况拒绝)。pnpm --filter @objectstack/runtime exec vitest run src/mcp-aggregate-order-cut.test.ts,预期 6 个用例全绿;其中一条断言真实 SQLite 语句里带order by case ... end asc与limit,声明顺序在前、NULL 在最后、totalGroups为 4。What changes
@objectstack/objectql).engine.aggregatetakes an engine-internal read option,AGGREGATE_ORDER_CUT. It is aSymbol.forkey, so no wire request or JSON query can set it.orderBy(aggregation aliases and group keys), an optionallimit, and an outcome slot that the engine writes once the driver has served the request:{ kind: 'served', totalGroups }.INVALID_SORT/400, and the message names the columns.INVALID_QUERY/400: any driver not on the compiling list, an aggregate the engine computes in memory, ahaving, and a relation group.INTERNAL_ERROR/500 and are never served.rank: a list of the column's values in declared order. This is how a declared option order reaches the statement.@objectstack/driver-sql). The grouped statement becomes a derived table, withcount(*) over ()andORDER BY/LIMITover its columns. It is one statement and one round trip.rank, a NULL ranks last in either direction.sumor count with no value ranks as the0it is served as.rankkey compiles to acase ... when ? then i ... else n endexpression: every unlisted value (a NULL among them) shares rankn, so the unlisted values come after the listed ones ascending and before them descending.rank, ascending, NULL last (engine,aggregateOrderCutDirective). So the unlisted values of a ranked column keep their own order, and a cut among them answers the same groups on every run (contract review S1).INVALID_QUERY/400). The local and replica faces inherit the SQL driver.McpDataBridge.aggregateGroups, answering{ rows, totalGroups }. It runs through the same gates asaggregate, carries the request, and reads the engine's outcome back.aggregate_records.orderByandlimit(at most the row cap).sort,sortBy,orderandorder_bymap toorderBy;top,takeandpageSizemap tolimit.truncatedis a fact rather than a guess.totalGroupsis the statement's own count of the groups before the cut.limitwith noorderBy: the leading select/radio group keys' declared option order is handed to the database asrankkeys, so it is taken before the cut. The leading-key reading is one helper (declaredOptionOrderKeys) that both paths call: the uncut path sorts by it, the cut path sends it. A declared list longer than the engine's rank maximum (500) ends the reading at its key rather than send a rank the engine refuses. AnorderBythe caller sends is passed unchanged and overrides the option order.currencies(a fixed-currency amount's ISO code, by alias) is on the cut result as on the uncut one.KEY__labelon the cut result as on the uncut one: both paths call one local closure over feat(mcp): aggregate_records names a lookup group's record beside its id, read as the caller #35202'swithLookupGroupLabels, applied to the served rows after the cut, so the name read never covers the probe group past the cap.whereon its currency code field (contract review S2, option 1; no refusal added).aggregateGroupsanswersNOT_IMPLEMENTED/501.main.Tests
packages/objectql/src/aggregate-order-cut.test.ts(14): the directive the driver receives, the count taken off the rows,INVALID_SORTnaming the columns, malformed requests and ranks,having, a timezone bucket computed in memory, a missing count, refusal on every driver that does not compile the cut, and the Turso local face and WASM driver served.packages/drivers/driver-sql/src/sql-driver-aggregate-order-cut.test.ts: live dialect cells, where SQLite always runs and Postgres/MySQL run where they are provisioned. It covers the top five of 62 groups; NULL and the 0 identity ranking; rank order; tenant wall pluswhere; a query spy showing ORDER BY, LIMIT and the window in the statement; CONTROL with no directive; a malformed or unprojected directive refused.packages/runtime/src/mcp-aggregate-order-cut.test.ts(6): HTTP bridgebuildMcpBridge, over a realObjectQL, a realSqlDriverand SQLite in memory. It covers the top five of 70 customers in order withtotalGroups70 and the statement regex; a group key ascending; a declared rank; the declared option order a limit-only call sends (declared groups first, NULL last, the cut after the CASE,totalGroups4);INVALID_SORT; CONTROL.packages/mcp/src/stdio-data-bridge.aggregate-order-cut.test.ts(6): stdio, through a real MCP client against a realObjectQL. It covers the top five of 80 groups (exactly 5, in order,totalGroups80,truncated);INVALID_SORT; a memory driver refused; a missing count never answered; a limit-only call over a select key reaching the driver directive as arankbefore the cut; CONTROL with no order and no limit (totalGroups, nogroupCount).packages/mcp/src/mcp-http-tools.aggregation-entry.test.ts: the tool half (probe group,truncated, aliases, 501 without the member, CONTROL), feat(mcp): aggregate_records returns select groups in declared option order and names a fixed amount's currency #35186's option-order and currency block unchanged, and a new block for the carried items: the limit-only push-down (the bridge receives{ field, order: 'asc', rank }and the cut keeps the first declared groups,totalGroups5), the rank read by alias for each leading select key and ending at the first that is not one, the over-long declared list, the explicit-order CONTROL (anorderByreaches the bridge unchanged, with no rank),currencieson the cut result, and a bridge without the served definition cutting as before. Every served result in that block is pinned to carry nogroupCount.orderBy+limitcall grouped by a lookup answers the served groups withaccount__label,totalGroups3 andtruncated, from one read of the referenced object for the served ids only (never the probe group).runtimereal SQLite: one declared value and three unlisted (lost,open, NULL), cut at 3: the answer iswon,lost,opentwice over, the statement carries the column's null-last ascending tie-break after the CASE, and the uncut order ends with NULL. The engine directive pin and the stdio directive pin show the ranked column among the trailing keys.tools/listpin asserts the record-mode ranking sentence.runtimereal SQLite: a registered read middleware that narrowsast.whereto one stage leaves the cut andtotalGroupswith only the 18 admitted groups (top five c69, c65, c61, c57, c53), and the uncut aggregate through the same middleware agrees.mcp-write-response-internal-fields.tripwire.test.ts,stdio-data-bridge.stored-metadata-body.test.ts) classifyaggregateGroups.Acceptance notes
main(rule 6977042103): the limit-only push-down of the declared option order throughrank, derived from feat(mcp): aggregate_records returns select groups in declared option order and names a fixed amount's currency #35186'sdeclaredOptionRank(the leading-key loop moved intodeclaredOptionOrderKeys, whichinDeclaredOptionOrdernow calls; the rule is not restated);groupskept on the default path;totalGroupskept; the explicit-order CONTROL pin, which feat(mcp): aggregate_records returns select groups in declared option order and names a fixed amount's currency #35186 recorded as NOT MEASURED until both were in;currencieson the cut result.main(carry note 6980343159): the order-and-cut path labels its served rows through the samewithLookupGroupLabels, after the cut; the default path keepsgroups,totalGroupsand the labels; feat(mcp): aggregate_records names a lookup group's record beside its id, read as the caller #35202's pins stay green.groupCountis gone from the result, the tool text, the pins and the changeset; the engine outcome, the bridge member's answer and the driver directive's column field spell ittotalGroupstoo (totalGroupsColumn, column__os_total_groups). None of these was released.@objectstack/mcpdoes not depend on@objectstack/objectqlat runtime, so the tool carriesORDER_RANK_MAX = 500beside a comment namingAGGREGATE_ORDER_RANK_MAX.metadata-protocol/src/protocol-data-crud.ts, the grouped arm) does not applyorderByon a grouped query and sliceslimitin memory.count(*) over ()needs MySQL 8 or later. MySQL 5.7 would fail the statement rather than return a wrong answer.Symbol.forread-option constants instdio-data-bridge.tsnow sit aboveunwrapRows' docblock. feat(mcp, objectql, driver-sql): query_records latestPer, each X with its latest Y in one call #35199'sSTDIO_LATEST_PERmoved with ours, because it held the same position. N2 skipped by order.5839005b034(overlap-check --branch=HEAD, VERDICT clean exit 0):origin/main(b02484e13a2, feat(spec): declare the AI answer follow-up routes and carry turn_id on conversation messages #35240 landed after the merge and touches none of this PR's packages) and fix(audit): a search the server widens never names a column the caller may not query #34817 merge cleanly. feat(mcp): query_records expand and get_record related, every reached object judged by the read guard #35225 landed first and is merged in (dbb2f6805b0). Its one conflict, inpackages/mcp/src/stdio-data-bridge.ts, was resolved keeping both sides: the twoSymbol.forread-option constants each under their own docblock, then feat(mcp): query_records expand and get_record related, every reached object judged by the read guard #35225'sstdioExpandMap, byte-identical to main, thenunwrapRowsunder its docblock.Verification
Readings at head
5839005b034(origin/maindbb2f6805b0merged, with #35225). The round-5 ablation and theobjectqlanddriver-sqlfull suites were read at87429381cbc; that merge changes no code they exercise. The gate union and the reconciliation are quoted in the dev report on #35144.Round 5 ablation: the ranked column in the trailing keys (S1)
The engine line
const named = new Set(request.orderBy.filter((k) => k.rank === undefined).map((k) => k.field));was replaced withconst named = new Set(request.orderBy.map((k) => k.field));(node scripts/ablation-replace.mjs, blob 0760cdd6255f to the mutation). Then@objectstack/objectqlwas rebuilt, andablation-dist-preflightfound the marker in 4 built files.won, NULL,lostinstead ofwon,lost,open, so SQLite really did keep NULL at the boundary. Also red: objectql 1 of 14 (the directive pin) and stdio 1 of 7 (the directive pin).git diff HEADis empty. After the rebuild, preflight--absentexited 0.Round 3 ablation: the label call on the order-and-cut path
node scripts/ablation-replace.mjsreplacedrows: await labelServed(cut ? groups.slice(0, cap) : groups, fields),withrows: cut ? groups.slice(0, cap) : groups,inpackages/mcp/src/mcp-http-tools.ts(anchor 1 to 0, blob 84c75cdaef65 to 529eafa55eaf; the test imports the source, notdist/).git diff HEADempty.Round 2 ablation (unchanged code): the declared-order derivation on the limit-only path
Replacing the
declaredOptionOrderByfallback withorderBy ?? []reddened 4 of 40: the three limit-only pins and the stdio rank pin; restored, blob equals HEAD.Round 1 ablations (unchanged code): the order push-down and the rank arm
for (const key of orderCut.orderBy) {replaced with an empty list, rebuilt, preflighted againstdist/): three driver pins red; restored, blob equals HEAD.rankarm made unreachable (rebuilt, marker indist/): the driver rank pin and the runtime rank pin red; restored, marker absent, tree clean.Suites and typecheck
pnpm build(120/120):objectqlsrc/aggregate-order-cut.test.ts14/14;driver-sqlsrc/sql-driver-aggregate-order-cut.test.ts7 passed, 2 skipped (live Postgres/MySQL cells, unprovisioned);runtimesrc/mcp-aggregate-order-cut.test.ts9/9;mcpstdio order-cut + aggregation-entry + both tripwires: 4 files, 92 passed. Lock VERDICT 0.mcpfull suite at5839005b034: Test Files 77 passed (77), Tests 1400 passed (1400). At87429381cbc:objectqlfull suite: Test Files 615 passed (615), Tests 13881 passed (13881).driver-sqlfull suite: Test Files 263 passed, 13 skipped (276), Tests 4387 passed, 257 skipped (4644). Each VERDICT command-exit 0.objectql,driver-sql,driver-turso,mcp,runtime(incl.check:test-typecheckledgers): VERDICT command-exit 0.dispatch-gates --commandsat5839005b034: 75 families, all exit 0.--ranwith exit codes: 75 derived, 75 run, 0 NOT-MEASURED (a derived zero).check:query-options-erasuretest surface is 236, at its ceiling.check-governed-merges --branch HEAD: NOT governed, 0 of 17 paths, +2005/-43.check-platform-checklist --changed-from origin/main: OK, 29 anchors judged, 0 lost.Generated by Claude Code