Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions evidence/indexer-255-limit-sizing-20261030/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Issue #255 — avoid redundant size serialization after the item limit

[Result table and limits](results/report.md) · [Frozen protocol](plan/protocol.md) · [Independent audit](results/independent-review.json)

This distinct five-line runtime optimization passes its bounded CPU-retrieval campaign. It is not acceptance of the earlier resource-cache candidate, a retrieval-accuracy gain, or closure of #255. Historical FAIL/INCONCLUSIVE evidence remains unchanged.

## Replay and inspection

The candidate snapshot and tests are retained under [patch/](patch/README.md). The [frozen runner](validator/evaluate.py) and its adjacent [freeze receipt](validator/candidate-runner-freeze.json) describe exact baseline/candidate pins. `plan/protocol.md` preserves the original `proposed-protocol.md` bytes; that original filename appears in the freeze receipt.

Inspect the archived evidence without executing retrieval:

```bash
python evidence/indexer-255-limit-sizing-20261030/validator/verify_raw.py
```

The deterministic gzip/tar archive retains every original raw file, including240 completed-arm receipts,120 paired groups, original output captures, source identities, run order, environment and summary. [Raw-file manifest](results/raw-files.json) records decompressed exact byte counts and hashes. The verifier does not extract or execute archive members. The independent audit is a post-run supplement, not the prospective validator; its original local helper was not portable and is not represented as a reusable production tool.

To reproduce the experiment only with appropriate CPU-execution authorization, reconstruct the two pinned worktrees, use the same recorded Python/dependencies, and invoke:

```bash
python evidence/indexer-255-limit-sizing-20261030/validator/evaluate.py \
--baseline "$BASELINE_ROOT" --candidate "$CANDIDATE_ROOT" --output "$NEW_OUTPUT_DIR"
```

`NEW_OUTPUT_DIR` must not exist. Keep the frozen receipt adjacent to the runner. Worktrees must be at the exact recorded commits; the runner asserts pins, source/corpus equality, output equality and unchanged source. The original run is complete; this command is reproducibility documentation, not permission to repeat until PASS. No source/control defaults carry campaign thresholds.

The runner's generic exception wrapper records `INCONCLUSIVE`; an output-parity assertion failure must instead be interpreted as a correctness **FAIL/blocker**, not measurement noise. No exception or mismatch occurred in the retained run.
6 changes: 6 additions & 0 deletions evidence/indexer-255-limit-sizing-20261030/patch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Frozen candidate

Baseline: `f93df0548a26714f6514337a8525c7c14108acad`.
Candidate: `9f7862a9f8619dd68ac59f12736f6b50da10d9e8`.

`candidate.patch` is the exact baseline-to-candidate source/test diff. `candidate.bundle` retains that one commit with baseline prerequisite. No ranking, corpus or default interface change is included. Publication commits additionally package evidence and inventory registration; they are not substituted for the frozen measured source revision.
Binary file not shown.
121 changes: 121 additions & 0 deletions evidence/indexer-255-limit-sizing-20261030/patch/candidate.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
diff --git a/src/inferrsi/knowledge_operations.py b/src/inferrsi/knowledge_operations.py
index 1c78418f..d5748c46 100644
--- a/src/inferrsi/knowledge_operations.py
+++ b/src/inferrsi/knowledge_operations.py
@@ -566,6 +566,11 @@ class KnowledgeReader:

def select(reference: str, bundle: Bundle, view: dict) -> None:
nonlocal used, context_used
+ # Normal Bundle views were serialized while computing their revision.
+ # Preserve custom subclasses' serialization/error behavior below.
+ if type(bundle) is Bundle and len(selected) >= limit:
+ omitted.append({"id": reference, "reason": "output_limit"})
+ return
size = len(_json(view))
if len(selected) >= limit or (max_bytes is not None and used + size > max_bytes):
omitted.append({"id": reference, "reason": "output_limit"})
diff --git a/tests/test_collect_limit_serialization.py b/tests/test_collect_limit_serialization.py
new file mode 100644
index 00000000..00153e5f
--- /dev/null
+++ b/tests/test_collect_limit_serialization.py
@@ -0,0 +1,99 @@
+"""Count saturation skips only redundant normal-view payload sizing."""
+import pytest
+
+from inferrsi import knowledge_operations as operations
+from inferrsi.knowledge_operations import Bundle, KnowledgeReader
+
+
+class CustomBundle(Bundle):
+ pass
+
+
+class InvalidViewBundle(Bundle):
+ def view(self, *, material=False):
+ return {**super().view(material=material), "custom": object()}
+
+
+def bundle(identity, cls=Bundle):
+ return cls({
+ "id": identity, "kind": "knowledge", "entrypoint": "entry.md",
+ "sources": [{"type": "unknown"}], "applicability": {}, "state": "test",
+ "limitations": [], "attribution": "test", "required_resources": ["entry.md"],
+ }, {"entry.md": identity.encode()}, {})
+
+
+class FixtureReader(KnowledgeReader):
+ def __init__(self, bundles):
+ super().__init__()
+ self.bundles = {item.metadata["id"]: item for item in bundles}
+ self.reads = []
+
+ def records(self):
+ return {identity: {} for identity in self.bundles}
+
+ def _read(self, qualified, revision):
+ self.reads.append(qualified)
+ return self.bundles[qualified.partition(":")[2]]
+
+
+def size_calls(monkeypatch):
+ original, seen = operations._json, []
+
+ def observe(value):
+ if isinstance(value, dict) and "authority" in value and "ref" in value:
+ seen.append(value["id"])
+ return original(value)
+
+ monkeypatch.setattr(operations, "_json", observe)
+ return seen
+
+
+@pytest.mark.parametrize("material", [False, True])
+def test_saturated_count_omits_without_sizing_but_reads_every_bundle(monkeypatch, material):
+ reader = FixtureReader([bundle(name) for name in ("a", "b", "c")])
+ seen = size_calls(monkeypatch)
+ result = reader.collect(limit=1, material=material)
+ assert seen == ["a"]
+ assert reader.reads == ["package:a", "package:b", "package:c"]
+ assert result["omissions"] == [
+ {"id": "package:b", "reason": "output_limit"},
+ {"id": "package:c", "reason": "output_limit"},
+ ]
+ assert result["material_bytes"] == len(operations._json(result["items"][0]))
+
+
+@pytest.mark.parametrize("max_bytes", [None, 1, 100000])
+@pytest.mark.parametrize("material", [False, True])
+def test_custom_subclass_fallback_is_identical_to_normal_views(max_bytes, material):
+ normal = FixtureReader([bundle(n) for n in ("a", "b", "c")])
+ legacy = FixtureReader([bundle(n, CustomBundle) for n in ("a", "b", "c")])
+ options = dict(limit=1, max_bytes=max_bytes, material=material, context_bytes=100)
+ assert normal.collect(**options) == legacy.collect(**options)
+
+
+def test_byte_budget_rejections_still_size_every_unsaturated_item(monkeypatch):
+ reader = FixtureReader([bundle(n) for n in ("a", "b", "c")])
+ seen = size_calls(monkeypatch)
+ result = reader.collect(limit=1, max_bytes=1)
+ assert seen == ["a", "b", "c"]
+ assert result["no_match"] and len(result["omissions"]) == 3
+
+
+def test_invalid_custom_view_still_raises_after_count_limit():
+ reader = FixtureReader([bundle("a"), bundle("b", InvalidViewBundle)])
+ with pytest.raises(TypeError, match="not JSON serializable"):
+ reader.collect(limit=1)
+
+
+def test_invalid_normal_resource_is_reported_even_after_count_limit():
+ invalid = bundle("b")
+ invalid.content["../escape"] = b"unsafe"
+ result = FixtureReader([bundle("a"), invalid]).collect(limit=1)
+ assert result["omissions"][0]["reason"] == "invalid"
+ assert "Unsafe bundle path" in result["omissions"][0]["detail"]
+
+
+@pytest.mark.parametrize("limit", [0, None, -1, True])
+def test_invalid_limits_still_rejected(limit):
+ with pytest.raises(ValueError, match="positive integer"):
+ FixtureReader([bundle("a")]).collect(limit=limit)
44 changes: 44 additions & 0 deletions evidence/indexer-255-limit-sizing-20261030/plan/protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Issue #255: defer serialization of already-limit-omitted views (proposal)

Status: prospectively accepted by coordinator before candidate/measurements, with independent reviewer conservative subclass guard. Baseline f93df0548a26714f6514337a8525c7c14108acad. Plane: harness. Historical cache v1 FAIL and confirmation uncertainty remain unchanged; this is a distinct, smaller optimization, not another attempt on that cache.

## Hypothesis and scope

`KnowledgeReader.collect.select` calls `len(_json(view))` before checking `len(selected) >= limit`. Once the item count is full, that byte size cannot alter the `output_limit` omission. Move only the full-count check before serialization for exact type(bundle) is Bundle; subclasses retain the original serialization/error path. Leave byte-budget checks after serialization. Do not skip `_read`, `Bundle.view`, identity/revision/resource validation, filters, ranking, query processing, omitted-item ordering, or context selection. No memo/cache, source/corpus edits or ranking changes.

For N calls to select and no max_bytes, view-size JSON serialization reduces from N to min(N,limit). With max_bytes, only calls after count saturation are removed; byte-budget-excluded candidates before saturation still undergo exact size calculation. Manifest and complete-resource hashing remain O(N*resource_bytes); no claim of removing that dominant work.

## Before performance execution

Independent source/protocol review; synthetic call-count and full JSON parity tests, including default, relevance, explicit order, aliases/revisions, material, context, exact/undersized byte budgets, invalid resources after count saturation, mutable inputs, filters and no match. Invalid limit values (0, None, negative, bool) retain their existing rejection. Supported normal Bundle outputs already serialize their metadata/resources/dependencies into the revision before select; custom overridden view behavior must be considered explicitly rather than hidden.

Freeze candidate commit, runner hash, exact cases and installed/import-path proof before timing. Run existing relevant software tests. No semantic heldout evaluation or new labels. Any correctness discrepancy stops before performance. Independent resource/output audit uses saved captures only.

## Fixed workload

All arms use same Python, source imports pinned to each worktree, identical packaged corpus, no filters unless stated, limit=5, max_bytes=None, material=False.

1. Primary: reused-reader default Collect(query=''); one warmup then one call per sample. This broad, bounded enumeration is the direct targeted operation, not a claim about every query.
2. Secondary: reused-reader Collect(query='', relevance='memory', context_bytes=262144); one warmup then one call.
3. Secondary: fresh-process CLI default Collect(query='', limit=5), full parent-observed startup/stdout capture.
4. Untouched guard: fresh-process CLI catalog search(query='cgroupv2', limit=5), same exact command/API mapping as old study.

Exact CLI argv: [PYTHON, '-m', 'inferrsi', 'collect', '', '--limit', '5', '--json'] and [PYTHON, '-m', 'inferrsi', 'knowledge', 'search', 'cgroupv2', '--limit', '5', '--json']. Each child gets only PATH=os.defpath, PYTHONPATH=<arm>/src, PYTHONDONTWRITEBYTECODE=1; cwd is its matching arm root. Freeze runner manifest before candidate measurements. Both arms serialize identical results; retain complete correctness output once per path plus every timing sample digest/bytes.

## One study only

120 paired groups, balanced AB/BA shuffled with random.Random(25530), path order fixed 1→4. Sequential isolated arm subprocesses, no concurrent heavy work. Capture wall nanoseconds and process CPU nanoseconds for API; child CPU user/system from os.wait4/rusage-compatible subprocess accounting for CLI plus wall time. Record monotonic chronology, arm order, source/corpus hashes before/after, load averages as contextual metadata. CPU and load evidence can narrow attribution but cannot waive a wall-time failure. Fresh process is not cold filesystem cache.

No trimming, pooling with old studies, retries to obtain PASS or tuning after outcomes. Exactly one execution after freeze. Missing/changed-source samples → INCONCLUSIVE; preserve partial data, no replacement.

## Prospective gates (proposal requiring coordinator acceptance)

- All output/resource/contract parity gates must pass.
- Primary median within-pair latency reduction >=5%, bootstrap95% lower bound >0. This practical threshold is specific to this smaller eliminated-serialization hypothesis, not relaxation of historical cache gates.
- Secondary each p50 regression <=10%, p95 regression <=20%; nearest-rank p95. Additionally upper95% bootstrap bound on each secondary p50 regression <=10% to exclude the median uncertainty that prevented previous acceptance. Point-limit failure → FAIL; point gates pass but confidence bound exceeds limit → INCONCLUSIVE (not PASS).
- 10,000 paired-group bootstrap resamples, seed25531, percentile2.5/97.5; no arm-independent resampling. Ratios of resampled medians for secondary p50. p95 confidence intervals descriptive, never SLO claim.
- Any source/corpus/parity/correctness failure blocks promotion regardless of speed. No ranking accuracy gain inferred; #255 accuracy remains independently open.

## Deliverable / stopping condition

One table with primary/secondary wall and CPU metrics, exact counts, bootstrap uncertainty, output parity and deterministic serialization counts. If practical gain is absent or any gate fails/inconclusive, retain evidence and stop this candidate without another confirmation. If all pass, seek current-head independent review and publication approval; no automatic tracker closure. No model/service/hardware calls or remote writes in this preparation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"README.md": "dfcb9cc72c7ed9e06b94cbc848fb3870697cd35860e66add1b9dd8d75bef188d",
"patch/README.md": "a229382547bd8da466b069c332614df34f077bb5f0fcfe9ab6a9d634f0a64844",
"patch/candidate.bundle": "bebda54d45d8a33fd1bf61a72025f57f7fbb28de0e25185c4711046b91423b83",
"patch/candidate.patch": "f9fbd04c05742e4e927df44344a87eb56b5af82a37627ba1ea8099f4b7f3aaef",
"plan/protocol.md": "d577712b231be4bf33e66d401da22d50a38b01951707dae7a65b8d9c2dfc6a44",
"results/affected-tests.txt": "b446d144969edc0fcfad2fec5a9766269f4423b8a1587b18ca27baf67617c82b",
"results/independent-review.json": "66b17170a7d968f950ef2ad969734ccfae4c55f78bd7705930e1b9f350f97e76",
"results/lint.txt": "82b3e6a6c090a57601d22943bd23fca9218d1031dbe5a7b754092f9a156b4f18",
"results/raw-files.json": "5895b8827f4db31aaa887c294942f0107338d528388857e5b9983b95e60ee637",
"results/raw-study.tar.gz": "50f1055d01a6f1305ef34c798a68fdf5334fc1b0033b0fc7808f11b443f89dea",
"results/report.md": "c70e5e2cb216a49467306859044d9ee9dfe3522a8e60c9919378cc6e87fa1c69",
"results/summary.json": "45888c28f57685aa91315811a04facf743a2e111a329cf62467b85f61528c3d8",
"validator/candidate-runner-freeze.json": "c118d40b587195b1a4a471dd2c75b63321b0c89e6661b40855d9b906f06a926d",
"validator/evaluate.py": "fe46d6fdfecccd5ce3fe8ac517aa8db39dc7c7dacf1aae775b62a7f3b5cc9760",
"validator/verify_raw.py": "ca9951697caf851a0c9facc49d3f4bd560f376647fcd2c976270ad2002dcd5f6"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
............................................................ss.......... [ 62%]
........................................... [100%]
113 passed, 2 skipped in 53.73s
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"reviewed_at_utc": "2026-10-30T08:30:47.957157+00:00",
"scope": "Independent saved-output/source/arithmetic audit only; no retrieval or timing rerun. Exact reader.read/indexed_skill required-resource verification.",
"baseline": "f93df0548a26714f6514337a8525c7c14108acad",
"candidate": "9f7862a9f8619dd68ac59f12736f6b50da10d9e8",
"runner_sha256": "fe46d6fdfecccd5ce3fe8ac517aa8db39dc7c7dacf1aae775b62a7f3b5cc9760",
"paired_groups": 120,
"arm_receipts": 240,
"mode_comparisons": 480,
"output_parity": true,
"source_before_after_and_current_identical": true,
"bootstrap_recomputed": true,
"metrics": {
"context_api": {
"paired_median_gain_percent": 7.098877743926381,
"paired_gain_95ci": [
4.048262810162958,
10.614829211904599
],
"p50_change_percent": -5.763618073452181,
"p95_change_percent": -32.9411077361231,
"p50_change_95ci": [
-8.949749849290267,
-2.725503643668059
],
"p95_change_95ci": [
-50.601304184113104,
-12.990828764875516
]
},
"default_api": {
"paired_median_gain_percent": 9.43424477752091,
"paired_gain_95ci": [
4.603267611296492,
13.337351437091183
],
"p50_change_percent": -6.205209666429812,
"p95_change_percent": -23.826233930620834,
"p50_change_95ci": [
-9.786019053699235,
-3.0363831683991815
],
"p95_change_95ci": [
-43.36007076233995,
1.860914709383743
]
},
"default_cli": {
"paired_median_gain_percent": 3.974159412973369,
"paired_gain_95ci": [
1.4083114467975126,
8.746270620946916
],
"p50_change_percent": -3.563052589953841,
"p95_change_percent": 2.957726056732457,
"p50_change_95ci": [
-7.671345176007338,
2.268486090834343
],
"p95_change_95ci": [
-16.703171909592108,
17.294299031876182
]
},
"search_cli": {
"paired_median_gain_percent": -0.5137363838687043,
"paired_gain_95ci": [
-5.567556545513452,
3.5983360196163208
],
"p50_change_percent": 0.5069227477990879,
"p95_change_percent": 8.991532975478789,
"p50_change_95ci": [
-5.60419094777459,
4.633352077175856
],
"p95_change_95ci": [
-42.024943749883306,
32.84556993923775
]
}
},
"required_resource_checks": [
{
"ref": "package:inferrsi.ascend.skills.torchnpu_pinned_host_memory_lifecycle",
"required_count": 2,
"emitted_count": 2,
"bytes": 19652,
"revision_match": true
},
{
"ref": "package:inferrsi.ascend.skills.torchnpu_device_memory_lifecycle",
"required_count": 2,
"emitted_count": 2,
"bytes": 17459,
"revision_match": true
},
{
"ref": "package:inferrsi.common.skills.host_memory_management",
"required_count": 10,
"emitted_count": 10,
"bytes": 69776,
"revision_match": true
},
{
"ref": "package:inferrsi.common.skills.audit_llama_runtime_state",
"required_count": 2,
"emitted_count": 2,
"bytes": 24572,
"revision_match": true
},
{
"ref": "package:inferrsi.common.skills.cgroup_resource_controls",
"required_count": 1,
"emitted_count": 1,
"bytes": 7348,
"revision_match": true
}
],
"context_bytes": 138807,
"timing_gate": "PASS",
"limits": [
"One host and frozen workloads, not a universal speed claim.",
"Search CLI p95 point regression about 8.99%; its 95% CI extends above 20%, but preregistered secondary uncertainty gate covered p50 only.",
"No ranking or semantic accuracy improvement established; #255 accuracy objective remains open.",
"Old failed experiments remain distinct and unchanged."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All checks passed!
Loading
Loading