feat(p18): a listing is a WINDOW, and an invented path is refused by name

P18 part A (order 20260914T105139Z). P16 measured S7a-3's ladder against a
delivered corpus for the first time and found two things fixture bases
cannot show.

(1) One level is not bounded by being one level. Measured 14.09 on the four
mounted vegnormal bases: okf.directory_listing on krav/N200 is 169 974 chars
over 1 132 documents, krav/N100 69 250 over 445, krav/N500 39 853 over 269,
and R761's own root 110 912 over 2 728 SUBDIRECTORIES -- 27-113x the
1 500-char ceiling S7a-3 set, riding in every later prompt. That last number
is why the window covers BOTH kinds: a pagination over documents only would
have left the largest measured level unpaginated.

read_dir now answers with a window. offset/limit page directories first then
documents as ONE sequence (two independent windows make "the next ten" a
question with two answers); total is the denominator and is always carried;
limit is CLAMPED to 50, never refused. Default 10 chosen against the ceiling:
one entry is 121-209 chars (median 145) over the four bases. After: n100
1 493, n500 1 453, R761 479, n200 1 537 -- 2.5 % over, stated rather than
tuned away, because the ceiling is a character budget and the window is a
count. Largest single call any caller can make: ~7 600 chars.

filter narrows a level instead of paging it: case-insensitive SUBSTRING over
title + req_number/prosessnr and over a directory path, answering with
total_matches beside total. A substring and not a pattern for
_ground_against_input's reason one rung down -- a form the rule does not know
returns nothing, and an empty listing reads as "the base does not have this".
A filter that matches nothing is an ANSWER (total_matches: 0), never a
refusal. ORDER PREMISE FELLED before building on it: the order asks for a
separate top-level reader "like own_frontmatter" because parse_frontmatter
was last-write-wins -- P15 (f13dc64) already made a top-level key win, so
BundleFile.frontmatter IS the concept's own value and a second reader here
would be the second copy ko-(p) forbids.

(2) 0 of 26 fasit concepts were opened in 32 read_file calls (the order's
"24" is the four runs' DISTINCT paths, re-measured 14.09), and 10 of those
calls named a path the base does not hold. Each reached the model as MAF's
opaque "Error: Function failed." while counting toward the three consecutive
tool errors that end a request. read_file now refuses such a path by name
(BundlePathNotFound, funn-99 returned form) and names the nearest directory
that actually HOLDS documents -- chosen off context_files, never the
filesystem, because a directory can exist on disk and hold no navigated
concept (read_dir would then refuse the very path the refusal handed back)
and because context_files is what drops the type: verdict layer, so a refusal
can never advertise by name the one layer no listing mentions. Narrow by
construction: only an ABSENT path is translated; any other OSError propagates
untouched.

Two existing arms REWRITTEN, neither weakened:
- test_a_nonexistent_sibling_is_still_an_os_error was a tripwire whose own
  docstring said "when it goes red, someone has closed it, and that is a
  decision to be recorded". This is the record. Its narrowness half survives
  as a new arm driving a real PermissionError on a file that IS there.
- test_every_document_is_still_reachable_and_the_counts_add_up became
  STRONGER: the accounting must now page, so the same assertion also proves
  the window is complete and non-overlapping.

tests/test_navigation_window_loadbearing.py: 13 arms. Arms needing the
delivered bases SKIP with the root named (PORTFOLIO_VEGNORMAL_ROOT), as
MAJOR-3's ceiling arm does; the window algebra, the filter negative and the
refusal run over a synthetic base UNCONDITIONALLY, so the file can never be
silently absent in full.

Verification: uv run pytest -q 1672 passed / 5 skipped before the new file
(1670 on cfd9079). ruff check + format clean, mypy clean (38 files). Golden
demo-transcript.stdout BYTE-UNCHANGED, shasum -a 1 of the CONTENT =
ea8c534773acdbe41ae68f2c55724d69aaf8be4f. No version bump, no push.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-14 22:10:43 +02:00
commit 9b47e5aa62
7 changed files with 634 additions and 42 deletions

View file

@ -45,6 +45,7 @@ from typing import Any
import pytest
from portfolio_optimiser import okf
from portfolio_optimiser.explore import DirectoryPathRefused, navigator_tools
_EXAMPLES = Path(__file__).resolve().parents[1] / "shared" / "examples"
@ -134,14 +135,59 @@ def test_a_document_is_still_returned_whole() -> None:
assert body.strip(), "reading a real document must be untouched by the directory branch"
def test_a_nonexistent_sibling_is_still_an_os_error() -> None:
"""(5) MEASURED, REPORTED, NOT FIXED: the live call's ACTUAL shape.
def test_a_nonexistent_path_is_refused_by_name_and_the_nearest_directory_is_given() -> None:
"""(5) P18/A3 CLOSES the gap this arm used to record as open.
The model appended ``.md`` to a directory name, which is not a directory -- it is a path that
does not exist. This order fixes the directory case; this arm records that the adjacent case is
unchanged, so the gap cannot be mistaken for covered. Written as an assertion on the CURRENT
behaviour: when it goes red, someone has closed it, and that is a decision to be recorded.
It used to assert ``pytest.raises(FileNotFoundError)`` and said in its own docstring: "when it
goes red, someone has closed it, and that is a decision to be recorded." This is the record.
MEASURED over P16's four paid runs before the change: 10 of 24 ``read_file`` calls named a path
the base does not hold (8 distinct paths, one of them a single-character UUID slip), and every
one of them reached the model as MAF's opaque ``"Error: Function failed."`` while counting
toward the three consecutive tool errors that end a request.
REWRITTEN, NEVER WEAKENED. The half this file exists for is still asserted below: only an
ABSENT path is translated, and the refusal is not an ``OSError`` wearing a better message.
"""
path = _a_directory(_NESTED) + ".md"
with pytest.raises(FileNotFoundError):
_tools(_NESTED)["read_file"].func(bundle_id=_NESTED.name, path=path)
answer = _tools(_NESTED)["read_file"].func(bundle_id=_NESTED.name, path=path)
assert answer.startswith(f"REFUSED ({okf.BundlePathNotFound.__name__})")
assert path in answer, "a refusal that does not quote the path the caller sent is unactionable"
assert "read_dir" in answer, "the one thing the caller can act on is the rung that lists names"
assert issubclass(okf.BundlePathNotFound, ValueError)
assert not issubclass(okf.BundlePathNotFound, OSError)
def test_the_nearest_existing_directory_is_a_path_that_resolves() -> None:
"""(6) the anti-vacuity half of (5): a refusal that named a directory which does not exist
would be the ``_index_excerpt`` failure one rung up -- a path that never was is worse than no
path. The name the refusal hands back is fed straight into ``read_dir`` and must answer."""
deep = _a_directory(_NESTED) + "/undermappe-som-ikke-finnes/dokument.md"
answer = _tools(_NESTED)["read_file"].func(bundle_id=_NESTED.name, path=deep)
quoted = answer.rsplit("documents: ", 1)[1].split("'")[1]
listing = _tools(_NESTED)["read_dir"].func(bundle_id=_NESTED.name, path=quoted)
assert "refused" not in listing, f"the refusal named {quoted!r}, which does not resolve"
assert int(listing["total"]) > 0
def test_a_real_os_error_inside_the_base_is_not_masked() -> None:
"""(7) the NARROWNESS arm -- the half of the old tripwire that must survive the rewrite.
Only a path that is ABSENT becomes a refusal. A file that IS there and cannot be read is a
different fact: the caller's path was right and the machine failed, so translating it into
"this base has no such document" would tell the model a lie it would then act on. Driven with a
real, unreadable file rather than a mock, because the branch under test is ``Path.exists()``.
"""
document = next(
f for f in okf.navigate_bundle(str(_NESTED)).context_files if f.name.endswith(".md")
)
target = _NESTED / document.name
mode = target.stat().st_mode
target.chmod(0o000)
try:
with pytest.raises(PermissionError):
_tools(_NESTED)["read_file"].func(bundle_id=_NESTED.name, path=document.name)
finally:
target.chmod(mode)