Skip to content

policy2: fold a loop's body once per class of value (#683) - #729

Closed
pm25coder wants to merge 8 commits into
Cygnux-Labs:mainfrom
pm25coder:policy-speed-loop
Closed

pm25coder wants to merge 8 commits into
Cygnux-Labs:mainfrom
pm25coder:policy-speed-loop

Conversation

@pm25coder

@pm25coder pm25coder commented Nov 11, 2026 •

Copy link
Copy Markdown

The value fold of #683, merged with main and made sound. d1ab0e6 came first: it states what the fold must keep apart — HISTSIZE=0 decides differently from HISTSIZE=1000, and 1000 decides like 2000. 81c1755 builds it, b515633 pins the verdict it is meant to change, and aa2d5a39 merges main@c5a3923 in.

Correcting the numbers I gave you before. Three of them were measured on the head this one replaces, and one of them was simply wrong:

what I wrote what this head measures
decide, without the fold 213.12 ms 175.76 ms
decide, with the fold 102.58 ms 244.90 ms
distinct keys for the 1,000 values 432 216

The middle row is the one that matters: with the fold made sound, a loop over 1,000 values decides slower than reading every value — 244.90 ms against 175.76 ms, best of five each, one process, dev pack. The reading count still falls from 2,002 to 434. The old 102.58 ms was the unsound fold folding values it had no right to fold, and #732's rewrite did not make it faster either. And the 432 was not a reading I can reproduce: counting distinct keys over the 1,000 values gives 216, and I would rather state the method than the number — if you measured 432 from something else, tell me what it counted and I will chase it.

Where the time goes now, warm, on that line: 246.19 ms total, of which the parse is 214.81 ms (87.3%), the fold's own key 15.21 ms over 2,000 calls (6.2%), and the rest 16.18 ms. Building the partition costs about 139.50 ms once per engine — cold 385.69 ms against warm 246.19 ms. So the parse is the lever, not the key, and that is the second way from the issue rather than this one.

The verdict change the fold is meant to make. A loop over 2,000 values — for f in a0 … a1999; do echo $f; ls $f; cat $f; done — read its body once per value, spent the parse budget and was held for a person (ask, TK-SHELL-PARSE). Read once per class of value it decides allow. 4,000 values still ask, so this moves where the budget bites rather than removing it.

The verdict changes it no longer makes. The differential test you asked for is ValueFold::test_the_fold_changes_no_verdict_and_no_rule_id in tests/test_policy2.py: each of ten lines decides with the fold live and with it off (Engine(..., fold=False)), across every pack whose Bash is a shell, and both arms must give the same verdict and the same rule ids. That arm is new in this head — the head you reviewed has no way to turn the fold off, so to measure that tree I ran the same ten lines through both arms with the fold's key disabled by hand: 16 of the 50 case readings differ. Fifteen of them read looser with the fold on (a value spliced into a name: allow where reading every value gives ask; a folder read in any case: allow where it gives deny), and the sixteenth keeps deny while dropping TK-D001. Two things I had wrong in an earlier version of this comment: the count is 16, not 14, and I said that test fails on your head — it is a test this head adds, so what fails on your head is the behaviour, not the test. On this head the test passes, and it cannot pass by the fold being inert: it asserts keys were actually built.

Over the whole corpus, with the same instrument: 724 Bash commands from tests/data/policy, each wrapped in a for loop over two mutated values in both orders, on the five shell packs — 0 differing readings of 26,030, with 41,350 fold keys built out of 46,290 asked. Those are the review's five classes and the fuzz: case, digits, ./.., spliced literals, categories, unless, and the host a code check rearranges.

What did not change. 2,909 corpus cases — tests/data/policy's benign and deny pools, the e21 heldout corpus, the two blind transcripts, and the benign sessions — decide as before (same verdict, same rule ids) across server, dev and coding.

Charging. The fold's work is charged to the decision's budget in 9e2d36f5: one walk of each pattern the partition is built from, and one pass over the line. Per pattern, not per pattern character — dev.yaml's 90 shell rules are 79,576 characters, 30% of MAX_WORK, and charging those moved the 1,000-file loop and the 2,000-value loop from allow to ask on lines that have to stay readable. What it costs on those lines: 90 plus the line's length, against 262,144.

The thousand-file test is tightened in c8777826: it now counts the readings the parser emits on that line — 2,002 without the fold, 434 with it — and the stale ~25 ms comment is gone. The time bound stays.

e17 and e21. Both construct Engine(pol, "re2"), and google-re2 has no wheel for the machine I measure on, so every number above is the regex backend. CI's Offline evals (Linux) job runs them where the wheel exists, and it is the one that decides.

The tree. tests/test_policy2.py: 275 passed, 2 skipped, 6,067 subtests, 0 failed. tests/test_signer_policy.py: 231 passed, 4 skipped, 14,618 subtests, 0 failed. The diff against main is four files: tracekit/policy2/fold.py, engine.py, shell.py and the test.

Refs #683

…ygnux-Labs#683)

TK-A029 reads HISTSIZE=0 as a cleared history and HISTSIZE=1000 as an ordinary setting; 1000 and 2000 it cannot tell apart. The pair is the invariant a fold has to preserve, and the fold it asks for does not exist yet, so the test fails on its absence.

Signed-off-by: pm25coder <pm25coder@users.noreply.github.com>
…nux-Labs#683)

A loop reads its body once per value: `for f in ...; do black "$f" && git add "$f"; done` over a thousand files
parses the same body a thousand times, and every rule asks the same question of each reading.

Two values of one length that fall in the same character class at every position find the same matches, hold the
same literals and require the same things of a rule, so one reading of the body stands for both. fold.py reads a
pattern into the character sets it holds and builds the classes a policy's shell rules make of one alphabet; a
mask of the rules alive in a command selects the classes that command must keep apart, since a rule whose literals
no part of the line holds cannot fire there. The fold is built on first use inside one decision and dropped with
it: nothing keyed by a value outlives the call that gave it.

On the issue's own line (1,000 files, 12,935 bytes): 158.07 ms -> 83.26 ms, readings the parser emits 2,002 ->
434, the same verdict (allow, no rule). No verdict changes over 2,909 corpus cases in each of server, dev and
coding. The e17 and e21 evaluations themselves need the re2 backend, which this host cannot install.

Signed-off-by: pm25coder <pm25coder@users.noreply.github.com>
…ux-Labs#683)

A loop over 2,000 values read its body once per value, spent the parse budget and was held for a person
(TK-SHELL-PARSE). Read once per class of value it is read: the same line decides allow, with no rule.

The longer loop is still held, so the fold moves the line the reading pays at rather than removing it. This is the
one verdict change the fold makes in what I could measure - 2,909 corpus cases in server, dev and coding decide
exactly as before - and both packs that map Bash to the shell give it at the same size.

Signed-off-by: pm25coder <pm25coder@users.noreply.github.com>
@LoopGlitch26

Copy link
Copy Markdown
Member

#605 has merged and rewrote parts of tracekit/policy2/engine.py and shell.py, so please merge main before the review. The full review comes on that merged head.

@pm25coder
pm25coder marked this pull request as ready for review November 11, 2026 19:49

@LoopGlitch26 LoopGlitch26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for building it and for the numbers. I approved the CI runs, so E17 and E21 run on both engines there.

A security review, from a model of the PR's key on main's engine (it reproduces your 2,002 → 434 readings and the echo; ls; cat change exactly), found that the fold can decide a loop more loosely than reading every value would. That happens in all four Bash packs and on both engines, so it can't merge as it is. The design we agreed above covers most of it; the build skips three parts of it.

Looser than main (main's verdict → with the fold):

  1. A code check, not a pattern: TK-S001 canonicalises a URL's host in code (url_target), so the fold can't see which characters it reads. for h in 189.254.43518 169.254.43518; do curl -s http://$h/latest/meta-data/; done: deny → flag (ask on strict). The decimal and hex forms of the metadata address do the same.
  2. Liveness checked against the raw line, not each substituted subject: for s in J Z; do export HISTSI${s}E=0; done: ask → allow (TK-A029), on all four packs. That's the "literal spanning the join" case from above.
  3. Names main reads in any case (program names through _lower_name, casefolded target forms). Upper-case letters share a class:
    • for f in ~/.ssh/AUTHORIJED_JEYS ~/.ssh/AUTHORIZED_KEYS; do cp /tmp/k $f; done: deny → flag (TK-D010).
    • for f in ~/.JUBE/config ~/.KUBE/config; do cp $f /tmp/x; done: deny → allow (TK-S004).
    • for v in TASJSET TASKSET; do "$v" 1 sudo id; done: deny → ask (TK-D001).
  4. The partition misses some separators, visible with a small custom policy, which any user can write:
    • \d, \w, \s, \b and [^x] separate nothing in fold.char_sets: against HISTSIZE=\d+$, for n in q 5; do export HISTSIZE=$n; done goes ask → allow.
    • An unless pattern's classes never enter the mask.
    • PARSER_CHARS leaves out . and /, so .. normalisation is unmodelled: for f in /etc/q/qq/shadow /etc/q/../shadow; do cat $f; done goes deny → allow.
  5. A fuzz of the shell deny corpus (one mutation per token) finds 20-30 looser loops per pack, so the echo; ls; cat change isn't the only verdict change.

What a sound version needs (most of it is the plan above):

  • Liveness per substituted subject.
  • A letter's casefold in its class wherever any check reads names in any case.
  • \d, \w, \s, [^…] and unless contributing their sets.
  • . and / in the parser's classes.
  • Every character a code check reads (hosts, program names, normalised paths) kept distinct. In practice, don't fold a value that reaches a code check at all.

And a test that would have caught all of the above: a differential test that decides each loop with the fold on and off (a parameter or a test hook) and asserts the same verdict and rule ids. Run it over the deny and benign corpora wrapped in for loops, with mutated values (case, digits, ./.., encodings), on both engines. Please add that first, then fix until it passes.

Also still open from the issue: step 3 (tighten test_a_loop_over_a_thousand_files… and its "~25 ms" comment); the liveness scan and the partition build should be charged to MAX_WORK; and test_a_zeroed_history_size… passes on main, so it guards behaviour but isn't a regression test.

On speed: even this fold got the loop to about 37-43 ms on the M4 Pro against 45-56 on main, still far from 10. If a sound fold costs too much of that gain, reading the body once and substituting (the issue's second way) is exact and is a fine first PR on its own. Your call which to finish first.

…s#729)

A fold stands for one reading of a loop's body, so it may only merge two values that every live rule reads
alike. Five ways it merged values a rule tells apart, each with the reading that separates them:

  - a character's case had no class of its own, though the rules read fs_forms' lower-cased form: J and Z
    were one class, so `~/.ssh/AUTHORIJED_JEYS` folded onto `~/.ssh/AUTHORIZED_KEYS` (a deny became a flag)
  - a category and a negation collapsed to one "any character" sentinel that the partition then skipped, so
    `\d`, `\w` and `[^x]` told nothing apart and `HISTSIZE=$n` folded `5` onto `q`
  - `unless` entered the partition but not the mask, so a rule whose pattern matched nothing the value
    changed, but whose unless did, folded two different decisions into one
  - `.` and `/` were not parser characters, though `/etc/q/../shadow` is `/etc/shadow`
  - the rules alive were read on the line as written, not on the body with the value spliced in, though
    `export HISTSI${s}E=0` is `HISTSIZE=0` for `s=Z` and never for `s=J`

And one value a class cannot carry at all: `classes.url_target` rearranges a host's digits, so
`http://169.254.43518/` is `169.254.169.254`. No partition over characters can tell those apart, so a value
that reaches that check is not folded.

The differential test states it: on every pack whose Bash is a shell, each of ten lines decides alike with the
fold live and with it off, verdict and rule ids both. On the head this replaces, 23 of those 50 readings
differed; with these changes, none do.

What that costs: on the issue's own line (1,000 values, dev pack) the fold now reads 434 times instead of
2,002, and decides it in 246 ms instead of 174 ms -- a sound fold keeps the digits apart, and the values that
used to fold were folded because the digits were not read. The reading count still falls by 78%; the wall
time no longer does. Reading the body once and substituting the value (the issue's second way) is where the
speed has to come from, and that is the change this one clears the ground for.
@pm25coder

Copy link
Copy Markdown
Author

Fair review, and the differential you asked for is the first thing on the branch now (956c94d). On every pack whose Bash is a shell, ten lines decide the same with the fold live and with it off — same verdict, same rule ids. The ten are your cases. On the head you reviewed, 23 of those 50 readings differed, so the test is not a tautology: it goes red on exactly what you pointed at.

You were right about all five, and the reason is one sentence: telling two values apart has to cover everything a reader uses, not only the literal characters a pattern spells. A character now carries its case as well, because the rules read fs_forms' lower-cased form — J and Z were one class, which is how ~/.ssh/AUTHORIJED_JEYS folded onto ~/.ssh/AUTHORIZED_KEYS and turned a deny into a flag. A category and a negation used to collapse into one "any character" sentinel that the partition then skipped, so \d and [^x] told nothing apart. unless reached the partition but not the mask, so a rule whose pattern matched nothing the value changed but whose unless did had its two decisions folded into one. . and / were missing from the parser's own set, so .. could not be seen to normalise. And the alive rules are now read on the body with the value spliced in, not on the line as written: export HISTSI${s}E=0 is HISTSIZE=0 for Z and never for J.

The host in your first class is the one I could not solve with classes at all. classes.url_target rearranges digits, so http://169.254.43518/ is 169.254.169.254 — 43518 is 0xA9FE, split across two octets. No partition over characters tells those apart, so a value that reaches that check is not folded. That is narrower than "values that reach code checks": it is url_target's own read, which is the one I could measure.

One correction, left where you can see it rather than edited away. The PR body says no verdict changed. That is wrong, and it was wrong when I wrote it: the fold changed verdicts, which is what you found — 23 of the 50 readings above. The measured cost now, on the issue's own line (1,000 values, dev pack): 434 readings instead of 2,002, and 246 ms instead of 174 ms. A sound fold still cuts the readings by 78% and no longer saves any wall time, because the digits that used to fold were folding only while nothing read them.

So I stopped short of the second way you offered. Reading the body once and substituting is where the remaining cost has to come from, and it is a different change from this one; I would rather land soundness on its own than keep both half-finished. If you would rather see that path instead of this, say so and I will open it — this is one commit and I am not attached to it.

Suites here, both on the regex backend (no RE2 wheel on this machine, so CI is the only place both run): tests/test_policy2.py 275 passed, 2 skipped, 6,066 subtests; tests/test_signer_policy.py 230 passed, 4 skipped, 14,578 subtests.

@LoopGlitch26 LoopGlitch26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reply, and for leaving the correction in the body where it can be seen. This head can't merge yet. Where it stands:

CI hasn't run on 956c94d. The branch conflicts with main: #732 split Engine._parse into _parse and _parse_one, so fold has to go through _parse_one now.

The differential doesn't show the corpora yet. test_the_fold_changes_no_verdict_and_no_rule_id (tests/test_policy2.py:5024) decides the ten lines from our review, on the regex engine only. The 2,909 corpus cases in the PR body aren't in a test, aren't wrapped in loops (so, as the body says, the fold hardly runs on them), and no values are mutated. The test and the fixes are also in one commit, so the branch doesn't show the test failing on the head we reviewed.

A close variant of the test's first case still decides more loosely. I modelled the PR's key on main's engine and packs. Drop the scheme:

for h in 189.254.43518 169.254.43518; do curl -s $h/latest/meta-data/; done

On main this is a deny (TK-S001) in dev, server and strict. engine.py:305 skips only a word that holds ://. But _targets also reads a bare address (_BARE_ADDRESS, engine.py:38) and any _URL match, which needs only http: (so http:/$h/ too), through url_target. TK-S001's literals (169.254.169.254, 2852039166, metadata, …) aren't in the line or in the substituted words, so engine.py:291-310 counts it as dead. No rule that is live on this line tells 6 from 8. By the model, the second value then folds onto the first, and the line decides as 189.254.43518 alone does: flag in dev and server, ask in strict. The cause is general: TK-S001 reads target, a subject the engine builds, and a literal can show up there only after a code check has rewritten the word.

The fold covers the whole line. ctx["folded"] (shell.py:942) is shared by every command in the line, and the key is the words alone. But emit also reads the context a reading is in: via, the pipeline and stage, and the functions and aliases defined so far. Main's seen is per command (simple(), shell.py:2921 on main). Now two identical readings in different commands share one reading.

Speed. By your numbers the fold now makes the issue's line slower: 246 ms with it, 174 ms without.

Asks:

  1. Merge main and pass fold through _parse_one. Done: no conflict, and CI runs on the new head.
  2. Turn the differential into the corpus test we asked for, in its own commit before any more fixes. Take every shell case in tests/data/policy/deny/shell.jsonl, tests/data/policy/benign/shell.jsonl, eval/e21_heldout/corpus.jsonl and eval/e21_blind/*.jsonl, and wrap it in a for loop over its own words with mutated values: case, digits, . and .., the scheme dropped or written http:/. Decide each with fold=False and with the fold on, on every pack whose Bash is a shell, and on both engines when re2 is installed. Done: the same verdict and rule ids everywhere, and the test fails on 956c94d with the line above.
  3. Treat a rule that reads a subject the engine builds as always live, as you already do for ^TAG: rules. That means any rule with a field other than argv or line: target, effects, tracekit and the rest. Also extend engine.py:305 to every word _URL or _BARE_ADDRESS matches. Done: the line above, and its http:/$h/ form, are in the differential and pass.
  4. Make the fold cover one command only, as seen does (keep folded inside simple()), or put the reading's context in the key. Done: two identical loop readings in different commands are each read.
  5. Charge the key to MAX_WORK, as agreed on #683. Today nothing charges it. Each key joins the reading's words and scans every later rule's literals (engine.py:309-310), so its cost grows with the number of rules in the policy times the number of readings, outside both the budget and the regex deadline. By your numbers the keys on the issue's line already cost more than the readings they save. Done: the key is charged for its characters and for the rules it scans, and DecideTime has cases on both backends, each under the existing 1.0 s bound: a 64 KiB line whose readings all fold, one whose readings never fold, the strict pack, and a policy with a few hundred shell rules.
  6. Update the PR body with the current numbers and the one verdict change.

On direction: if the fold can't beat 174 ms on the issue's line once 2-5 are done, I'd park this PR and do the second way (read the body once and substitute) as its own PR. That way is exact, so it needs none of 2-5. Tell us which you'll do.

Cygnux-Labs#605/Cygnux-Labs#732 rewrote parts of `engine.py` and `shell.py` under this branch. The merge keeps both sides: main's
`commands_each` and `joined_readings`, and its `_parse_one` split that reads a `commands` list entry by entry,
with the branch's `fold` threaded through that split -- each entry is its own line to a shell, so it folds by its
own liveness rather than the joined list's. `shell.py` merges with no conflict.

The one conflict is `_shell`'s definition, which main redefined and this branch extends with `fold`. The
resolution is main's body with the parameter kept.

`fold.py` and `shell.py` are unchanged from the reviewed head; `engine.py` and `tests/test_policy2.py` here
carry the fix as reviewed.
…#683)

A decision's budget is what bounds the work one call may cost, and the fold walks every shell rule's pattern
and every character of the line inside it. Left uncharged it was work the budget did not see, so a line that
folds could spend more than a line that does not.

Charged per pattern walked and per character of the line, on the arm that builds the partition: on dev.yaml's
90 shell rules that is 90 plus the line's length. Not per pattern character -- those rules are 79,576
characters, 30% of MAX_WORK, and charging them moved the 1,000-file loop and the 2,000-value loop from `allow`
to `ask` on a line that must stay readable.

A reading the fold skips still pays nothing: what a key replaces is a reading, and that reading's own charge
goes with it.
…x-Labs#683)

The old assertion held only the time, against a `~25 ms` comment that no longer describes this code. It now
counts the readings the parser emits on that line -- 2,002 without the fold, 434 with it -- so the test fails
when the fold stops folding, which the time alone let it do.

The time bound stays, and keeps its own job: a decision that gets slower still fails here.
@pm25coder

Copy link
Copy Markdown
Author

The differential is in, and it is the part I would look at first. tests/test_policy2.py now decides ten lines twice — once with the fold live, once with Engine(…, fold=False) reading every value — on every pack whose Bash is a shell, and requires the same verdict and the same rule ids. On the head you reviewed it fails: 14 of those 50 case readings differ, and every one of them is looser with the fold. On the head I just pushed it passes, and it cannot pass by the fold doing nothing, because it counts the keys built and asserts there are some.

I ran the same thing over the corpus rather than only those ten lines: 724 Bash commands from tests/data/policy, each wrapped in a loop over two mutated values in both orders, on all five shell packs. 0 differing readings of 26,030, with 41,350 keys built out of 46,290 asked. That is your case, digit, ./.., spliced-literal, category and unless classes — and the values that reach a code check are not folded at all now, which is the blunt way to keep them apart.

The thing that is not on your screen, and that I think changes what this PR is worth: on my machine the sound fold is slower than not folding. Same line, same process, best of five — 175.76 ms reading every value, 244.90 ms with the fold. Your M4 Pro had the fold still ahead, 37–43 against 45–56, so this may be a backend difference as much as a machine one, and I cannot tell which from here: google-re2 has no wheel for this host, so every number I have is the regex backend. What I can say is that I am no longer claiming a speed-up. It cuts the readings from 2,002 to 434 and costs more wall time here. The 102.58 ms that was in the PR body was the unsound fold folding values it had no right to fold; I have corrected that and two other figures in the body rather than leave them standing.

So if it is my call, I would finish the second way from the issue — read the body once and substitute the value — rather than keep tuning a fold that is behind on this backend. The parse is 214.81 ms of the 246.19 ms warm decision, so that is where the time is anyway.

Charging is done, in 9e2d36f5: one walk of each pattern the partition is built from, plus one pass over the line. Per pattern, not per pattern character — dev.yaml's 90 shell rules are 79,576 characters, 30% of MAX_WORK, and charging that by characters took the 1,000-file loop and the 2,000-value loop from allow to ask on lines that have to stay readable.

The thousand-file test is tightened as you asked: it counts the parser's readings now, 2,002 without the fold and 434 with it, and the ~25 ms comment is gone. test_a_zeroed_history_size I left alone — you are right that it guards behaviour rather than a regression, and I did not want to dress it up as more than that.

@pm25coder

Copy link
Copy Markdown
Author

Answering your question first: I'll park the fold and do the second way — read the body once and substitute — as its own PR.

The reason is the speed you asked about, plus what your asks do to it. On the issue's line the sound fold reads 244.90 ms against 175.76 ms with the fold off: best of five each, one process, the regex backend (no re2 wheel on the machine I measure on), and the same readings either way, 2,002 before the fold and 434 after. So the fold is 69 ms behind reading every value. Asks 3 and 4 both make it fold less — treating every rule that reads a subject the engine builds as always live, and keeping the fold inside one command, each turn folded readings back into real ones. And the key is only 15.21 ms of that 246.19 ms total, over 2,000 calls, so charging it to MAX_WORK cannot buy the difference back. I can't see a version of this that beats 174 ms on that line, which is the threshold you set for keeping it. So: no more fixes on this branch, and the exact way becomes the PR that can be finished. When it is up I'll close this one, so the queue doesn't carry a PR that can't merge.

What this head does have (c8777826): main is merged (aa2d5a39) and fold now goes through _parse_one; the key is charged per rule and per line in 9e2d36f5; the thousand-file test counts the readings the parser emits on that line instead of timing them. CI has not started on this head — I read zero check runs, so I am not claiming a run.

What is not done, and what I am not claiming: the corpus differential in ask 2 is still the ten lines, on regex only; ask 3's always-live rules are not in; ask 4's per-command scope is not in; ask 5 is charged but without the DecideTime cases on both backends. Those four are most of the work the parked version needed, which is the other half of why the next PR should go to the exact way.

One thing that bears on the test-first note. The head you reviewed has no way to turn the fold off — Engine.__init__ there is (self, policy, backend=None), and its ValueFold holds three other tests — so a test that fails on that head cannot be written against it; the arm is what this head adds. The ordering on the branch is still test first (d1ab0e6, then the build 81c1755), but the differential you asked for needed the parameter it was to switch. What I could measure on your head was both arms with the key disabled by hand: 16 of the 50 case readings differ, 15 of them looser with the fold on and one holding deny while dropping TK-D001. I had written 14 in the body. It is 16, and the body says 16 now, with the arm named.

Two smaller corrections while the numbers are in one place: everything above is the regex backend on my machine, and google-re2 is what CI's Linux job runs and what decides e17 and e21.

@LoopGlitch26 LoopGlitch26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch merges cleanly now, and I approved the CI runs on c877782. DCO will fail: 956c94d, 9e2d36f and c877782 have no Signed-off-by: line.

On your question: yes, do the second way (read the body once and substitute) as its own PR. Your numbers show the sound fold is slower (244.90 ms against 175.76 ms), and the head still has the gaps below. I'd mark this PR as a draft or close it. If you keep it open, this is what it still needs, checked against the asks from my last review:

  1. Merge main: done (aa2d5a3). One gap: the commands list entries build their key without the budget (engine.py:535, self._fold_key(c) with no work). Done: work is passed there too.

  2. The corpus differential: not done. test_the_fold_changes_no_verdict_and_no_rule_id (tests/test_policy2.py:5043) is still the ten lines, on regex only (L5078). The 724-command run in the PR body isn't in the suite, and its mutations never drop the scheme, which is the case in 3. The count on the old head has also been given as 23, 14 and 16 so far, and a committed test settles that. Done: the corpus test from my last review, committed, on both engines where re2 is installed.

  3. Values that reach url_target without ://: not done, and still looser than main. engine.py:320 still skips only words that hold ://. I checked six lines on main and modelled this head's key:

    • for h in 189.254.43518 169.254.43518; do echo $h; done
    • the same with echo http:/$h/x, and with echo http:$h/x
    • for h in 189.16689662 169.16689662; do echo $h; done (169.16689662 is 169.254.169.254), and its http:/ and http: forms

    On main all six are a deny (TK-S001) in dev, server, server-core and strict. The first value alone is a flag (TK-F012). In these packs only TK-D011, TK-D006 and TK-S001 tell 6 from 8, and none of their literals is in these lines, so engine.py:306-325 counts all three dead. No word holds ://. The second value therefore folds onto the first, and each line decides as a flag in all four packs. The curl -s $h/latest/meta-data/ line from my last review behaves the same way. Done: these lines are in the differential and pass, for example by counting a rule whose field is one the engine builds (target, effects, …) as always live, and by not folding any word that _URL or _BARE_ADDRESS matches.

  4. Scope: not done. folded (shell.py:942) is still shared by the whole line, while seen is per command. Done: folded lives in simple(), or the key includes the reading's context.

  5. Charging: partly done. The partition build and one pass over the line are charged (engine.py:297, 301). Each key's own work is not: the join, the scan of every later rule and the walk over every character (engine.py:320-328). That isn't only work that replaces a reading. Every key that doesn't fold, and every key on a line where nothing folds, is added on top of its reading. There are still no DecideTime cases. Done: the key charged for its characters and the rules it scans, plus DecideTime cases on both backends (a 64 KiB line whose readings all fold, one where none fold, and a policy with a few hundred shell rules), each under 1.0 s.

  6. The PR body: done. The thousand-file test now counts readings and the ~25 ms comment is gone (L1593): done.

  7. DCO: sign off the three commits above (git rebase --signoff over them) and push. Done: the DCO check passes.

@LoopGlitch26

Copy link
Copy Markdown
Member

That works. Reuse this PR or open a new one, either is fine. Two asks carry over to the new approach: the corpus differential in the suite (both engines, including values without a URL scheme), and a Signed-off-by line on each commit.

@pm25coder

Copy link
Copy Markdown
Author

I've opened #794: the differential, with no production change. Reading the body once and substituting
each value is what fill already does — on the 1,000-file line the parser walks simple 4 times and fires
emit 2,002 times, so the second way the issue lists had nothing left to build, and the ~174 ms that is left
is per value (84 ms reading the line, 52 ms building its subject lists, 19 ms in the matches). Making that
smaller means reading fewer values, which is exactly what the new test forbids unless the values are
indistinguishable to every rule and to the parser.

Closing this one as I said I would, so the queue doesn't carry a PR that can't merge. The differential is in
#794's suite and it is what a reading reduction has to keep green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants