Repository navigation
feat(query): default session-query results to the top-level unit - #3495
Conversation
Problem: subagent/branch children are 45.6% of the archive (8,614 of
18,871 sessions; 8,824 session_links rows with link_type='subagent',
measured 2026-07-29) and previously ranked equal to real top-level
sessions in every default result list. `polylogue find repo:polylogue`
returned rows like one parent session repeated 3x under different
agent-suffixed child ids, making the default result unreadable.
Solution: the `root` field/DSL clause/`--root`/`--no-root` flag already
existed (spec.root: bool | None, None = both). This change resolves the
unset case to root-only (top-level sessions) at the SQL-filter-kwargs
boundary via the new `resolve_default_root_filter()` helper
(polylogue/archive/query/spec.py), applied consistently across every
session-list/count/search entry point that was missing `root` pushdown
entirely or relying on an in-memory post-filter after LIMIT/OFFSET had
already truncated the page (pagination/count dishonesty):
- polylogue/cli/archive_query.py: the CLI find/read hot path (its
`_ArchiveFilterKwargs["root"]` already existed but was never
defaulted); `_emit_list`/`_emit_search` gain a `total_unit` envelope
field naming the counted unit (AC2); `_emit_daemon_list_payload`/
`_emit_daemon_search_payload` propagate the daemon's `total_unit`.
- polylogue/mcp/archive_support.py: `ArchiveQueryFilters` was missing
`root` entirely -- MCP query/list never filtered by session
structure at all, not even on an explicit `root:` clause.
- polylogue/archive/query/archive_execution.py: the Python-API/
SessionFilter path's `_ArchiveFilterKwargs` was also missing `root`
(post-filter only, after SQL LIMIT).
- polylogue/daemon/http.py: the daemon HTTP session-list route
(`_archive_filter_kwargs_from_spec`) was a fourth parallel filter-kwargs
builder, also missing `root` -- the deployed daemon (the normal
runtime path per CLAUDE.md) would have silently kept the old
unfiltered default even after the CLI/MCP fix landed.
The default is deliberately NOT written into `SessionQuerySpec.root`/
`SessionQueryPlan.root` themselves (which stay `None`-by-default,
reflecting genuine unset user intent) -- those fields feed
`has_filters()`, which many surfaces treat as a safety/routing signal
(unscoped-delete guards in query_actions.py/query_contracts.py, "bare
invocation shows stats" detection in root_request.py, `latest`
resolution). Silently defaulting `spec.root` itself to `True` would have
made every unfiltered query look "filtered" to those consumers,
bypassing an unscoped-mutation guard. `resolve_default_root_filter()` is
applied only at the point each surface builds its SQL filter-kwargs dict.
A `lineage:id:<ref>` predicate is itself an explicit request for one
session's whole family (root plus descendants), so
`resolve_default_root_filter()` exempts it via the new
`lineage_seed_from_predicate()` helper (polylogue/archive/query/predicate.py)
-- otherwise the root-only default would silently drop the children a
lineage-seeded query explicitly asked for (caught by an existing
regression test, test_lineage_id_cli_query_materializes_parent_child_refs).
Non-goals (per scope): no schema change, no storage/lineage semantic
change (polylogue-4ts territory) -- this is read-side query
planning/filtering/rendering only. fcyf's fanout-as-first-class-view
stays untouched.
Verification:
- mypy --strict on every touched file: clean.
- devtools test tests/unit/cli/ tests/unit/archive/ tests/unit/mcp/
tests/unit/daemon/ tests/unit/api/ tests/unit/storage/test_topology_edges.py
tests/unit/storage/test_session_topology.py: only pre-existing
failures remain (confirmed against master e8a23cc via an isolated
detached worktree -- title-fallback drift in
test_cli_output_schemas.py/test_plain_cli_snapshots.py/
test_assertion_candidate_evidence_disclosure.py, origin-enum
completion drift in test_command_aux_runtime.py, unrelated
facade-route/PTY/clock/materialization-bootstrap failures). All
genuinely new failures from the default-flip were fixed: two
intentional test updates (test_bounded_pages_report_indexed_total's
stale total==2 assertion, now root-only total==1, plus a new
companion test_no_root_flag_recovers_the_subagent_child_and_honest_total)
and a real daemon/CLI parity gap (test_daemon_golden_parity.py) that
surfaced the missing daemon-side root pushdown above.
- devtools render all --check: sync OK (docs/cli-reference.md
regenerated for the changed --root/--no-root help text; a
__snapshots__/test_terminal_snapshots.ambr update was needed and
included).
Ref polylogue-j8u2
Co-Authored-By: Claude <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 23 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
polylogue find/read/MCP query/daemon-proxied query now default to thetop-level session as the result unit. Subagent/branch children stay
fully queryable via the pre-existing explicit opt-in (
--no-root/root:falseDSL field) — no evidence is hidden or deleted.Problem
Measured 2026-07-29 (bead body): 8,614 of 18,871 sessions (45.6%) are
subagent children (8,824
session_linksrows withlink_type='subagent'),and they ranked equal to real top-level sessions in every default result
list. Dogfood example from the bead:
Three rows, one parent session, differing only by an agent suffix — this
shape filled the default result list.
Re-measured today (2026-07-31) against the live archive,
repo:polyloguematches 5,161 sessions total when root is unfiltered — 1,906 top-level,
3,255 subagent/branch children (1906 + 3255 = 5161, confirms honest,
mutually-exclusive SQL partitioning).
Solution
The
rootfield/DSL clause/--root/--no-rootCLI flag already existed(
SessionQuerySpec.root: bool | None,None= both, unfiltered). Thischange resolves the unset case to root-only via a new
resolve_default_root_filter()helper(
polylogue/archive/query/spec.py), applied at the point each surfacebuilds its SQL filter-kwargs — not by mutating
spec.root/plan.rootthemselves, which stay
None-by-default. Those fields feedhas_filters(),which several surfaces treat as a safety/routing signal (unscoped-delete
guards in
query_actions.py/query_contracts.py, "bare invocation showsstats" detection in
root_request.py,latestresolution) — defaultingspec.rootitself would have made every unfiltered query look "filtered"to those consumers and silently bypassed an unscoped-mutation guard.
Four parallel filter-kwargs builders needed the fix, three of which were
missing
rootentirely (not just missing the default — the fieldwasn't SQL-pushed at all, or was post-filtered after
LIMIT/OFFSEThadalready truncated the page):
polylogue/cli/archive_query.py— the CLIfind/readhot path. Its_ArchiveFilterKwargs["root"]already existed but was never defaulted._emit_list/_emit_searchgain atotal_unitenvelope field namingthe counted unit (AC2);
_emit_daemon_list_payload/_emit_daemon_search_payloadpropagate the daemon'stotal_unit.polylogue/mcp/archive_support.py—ArchiveQueryFilterswas missingrootentirely. MCP query/list never filtered by session structure atall, not even on an explicit
root:clause.polylogue/archive/query/archive_execution.py— the Python-API/SessionFilterpath's_ArchiveFilterKwargswas also missingroot(post-filter only, after SQL
LIMIT/OFFSET— a pagination/counthonesty bug for any consumer of
SessionFilter.list()/.count()).polylogue/daemon/http.py— the daemon HTTP session-list route(
_archive_filter_kwargs_from_spec) was a fourth parallel filter-kwargsbuilder, also missing
root. The deployed daemon (the normal runtimepath per this repo's CLAUDE.md) would have silently kept the old
unfiltered default even after the CLI/MCP fix landed — caught by the
existing
test_daemon_golden_parity.py::test_find_list_json_parity_between_direct_and_daemonregression test, which failed until this route got the same fix.
A
lineage:id:<ref>predicate is itself an explicit request for onesession's whole family (root plus descendants), so
resolve_default_root_filter()exempts it via a newlineage_seed_from_predicate()helper(
polylogue/archive/query/predicate.py) — otherwise the root-only defaultwould silently drop the children a lineage-seeded query explicitly asked
for. Caught by the existing
test_lineage_id_cli_query_materializes_parent_child_refsregression test.AC matrix
explicit projection — satisfied.
resolve_default_root_filterresolves the SQL
rootfilter to top-level-only whenever the queryleft
rootunset, across CLI find/read, MCP query, the Python-APISessionFilter, and the daemon HTTP session-list route.--no-root/root:false(pre-existing) remains the explicit opt-in back ontochildren, and stays a real SQL predicate (not a post-fetch slice), so
a bounded page still reports an honest total.
surfaces this lane touches (CLI find/read
total_unitenvelope field;MCP/daemon inherit the same resolution feeding their
total).--root/--no-roothelp text and theroot:DSL field descriptionwere updated to state the new default explicitly. A full count-label
audit across every surface (e.g.
polylogue analyze/stats-onlysurfaces untouched by this lane) is out of scope per the coordinator's
non-goals and can be a follow-up if it balloons.
--no-root/root:falseis unchanged plumbing, now confirmed to be areal SQL predicate rather than a post-filter, and MCP's
root:DSLfield now actually filters (previously a no-op). Verified live:
find repo:polylogue --no-rootreturns 3,255 sessions, allagent-*-suffixed children.see below.
Verification
mypy --stricton every touched file: clean.devtools test tests/unit/cli/ tests/unit/archive/ tests/unit/mcp/ tests/unit/daemon/ tests/unit/api/ tests/unit/storage/test_topology_edges.py tests/unit/storage/test_session_topology.py: only pre-existingfailures remain, confirmed against master
e8a23cc31via an isolateddetached worktree (title-fallback drift in
test_cli_output_schemas.py/test_plain_cli_snapshots.py/test_assertion_candidate_evidence_disclosure.py, origin-enumcompletion drift in
test_command_aux_runtime.py, and unrelatedfacade-route/PTY/clock/materialization-bootstrap failures — none touch
session listing/query defaults). Genuinely new failures from the
default flip were all fixed:
test_bounded_pages_report_indexed_total'sstale
total==2assertion updated to the now-correct root-onlytotal==1, a new companiontest_no_root_flag_recovers_the_subagent_child_and_honest_total, andthe
test_daemon_golden_parity.pyparity gap described above.devtools render all --check: sync OK (docs/cli-reference.mdregenerated for the changed
--root/--no-roothelp text; a__snapshots__/test_terminal_snapshots.ambrupdate was needed and isincluded).
git pushran the pre-pushdevtools verify --quickhook(format+lint+mypy+render-all-check): exit 0.
AC4 live dogfood (read-only against
/realm/db/polylogue, run via eachworktree's own interpreter to avoid the documented shared-venv-.pth
hazard —
python3 -c "...; from polylogue.cli import main; main()"from a detached worktree pinned at
e8a23cc31for "before" and thisbranch for "after"):
Before (
e8a23cc31, unpatched) —find repo:polylogue --limit 5 -f json:After (this branch) — same command:
After, explicit children projection —
find repo:polylogue --no-root --limit 5 -f json:1906 (root) + 3255 (children) = 5161 (unfiltered before-total) — exact,
confirming SQL-level, mutually-exclusive partitioning.
Before —
read --all --limit 5 -f json:After — same command:
Anti-vacuity statement
resolve_default_root_filter()'s production caller ispolylogue/cli/archive_query.py::_execute_archive_query_stdout(thelive
polylogue find/readimplementation exercised above against thereal archive), plus
polylogue/mcp/archive_support.py::archive_query_filters(MCP query/list tools),
polylogue/archive/query/archive_execution.py::_plan_filter_kwargs(
SessionFilter/Python API), andpolylogue/daemon/http.py::_archive_filter_kwargs_from_spec(daemon HTTP session-list route, exercised by
test_daemon_golden_parity.pyagainst a real UDS server).rootkey from any of those four filter-kwargsdictionaries (or reverting
resolve_default_root_filtertoreturn root, dropping the default) makestest_no_root_flag_recovers_the_subagent_child_and_honest_totalandtest_bounded_pages_report_indexed_totalfail (total reverts to theunfiltered structural count) and reintroduces the
test_daemon_golden_parity.pyparity gap.lineage_seed_from_predicateexemption makestest_lineage_id_cli_query_materializes_parent_child_refsfail (theseeded family's child row disappears from the result set).
Ref polylogue-j8u2