Skip to content

placement: decode forwarded facts, here fallback, needs reader, factsCommand runner, facts read limits (#794, #805, #821) - #970

Merged
scuttletoby merged 3 commits into
mainfrom
fork/placement-followups-batch10
Nov 22, 2026
Merged

scuttletoby merged 3 commits into
mainfrom
fork/placement-followups-batch10

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Fixes #794, Fixes #805, Fixes #821

Placement follow-ups from the P2, P3 and P5 reviews (umbrella #883). This PR changes only fork-named files (fork/, fork*, FORK_*, fork docs), so there are no new seams and no list change. Placement stays off; turning it on is #902, which this PR does not touch.

#794 (rule engine input)

Point Done Evidence
1. An answer with neither field Yes factsFromAnswer (apps/server/src/fork/environmentFacts.ts) maps it to null, meaning "no facts", as P3 documents for an older build. A named machine then meets only the rules that need no facts. Test: environmentFacts.answer.test.ts, "neither field".
2. "here" without host facts under enforce Yes When this machine has no CPU and memory sample (no sampler, a failed read, or CPU not measured), it passes H2 and H8 and goes last in the order, after every ranked machine. The why text says it was picked only because nothing ranks. Upstream's own limits still hold for it: manual or an unknown weight word keeps it out, and so does 5 % free memory or less when memory was measured. With agent slots on, its gate checks again at launch; with slots off nothing does, as in upstream's own launch on this machine. A linked machine without a sample is still refused. Tests in forkPlacement.test.ts: "a sampler failure here does not stop all delegation", plus new H2 and H8 rows and the weight and memory cases.
3. Decode the forwarded answer with P3's schema Yes factsFromAnswer decodes with ResultFields. When the schema refuses an answer (wrong type, negative count, CPU above 1, no factsAt, not an object), the facts carry problem. H2 then refuses the machine with "The machine's facts are not valid: …", even when it is named. A bad answer never reads as busy or free.
4. Labels in stored form, weight words decoded Already done in #798; finished here PlacementLabel already decoded to the canonical form, and PlacementWeight is a literal union (packages/contracts/src/placement.ts). The last part was the second label function, merged under #805 item 2.

#805 (P2 review)

Point Done Evidence
1. P5 reads needs through readPlacementNeeds Yes PlacementTask.needs is now PlacementNeeds, and H6 and choosePlacement read it through readPlacementNeeds. A broken value (or anything that is not a list) fits no machine, including a named machine and this machine, with the reason. Test: "a broken needs value fits no machine".
2. Merge canonicalPlacementLabels and canonicalLabels Yes The engine's own copy is gone. Its canonicalLabels now calls contracts' canonicalPlacementLabels, the one P2 stores with, after dropping any value that is not a string (from another machine's answer). The contracts file is unchanged; see the seam note below. The test checks that both give the same result.
3. The P4 runner checks the path again on its own host; no shell, no argument splitting Yes (runner only) New apps/server/src/fork/factsCommand.ts. It checks the path against HostProcess.Platform before running it. It runs the program as one path with args: [] through ProcessRunner (shell: false). On Windows only an .exe runs, because ProcessRunner would use a shell for .cmd and .bat. Limits: 5 s timeout, output truncated at 16 KB, at most 64 labels. A run cut at the output limit drops its last line, which may be part of a label. A program that a signal ends reads as "stopped by a signal". Tests run a real program in a folder named with space;echo hacked: it prints args-0 (no arguments) and whole-path ($0 is the full path), and both come back as labels. Wrong-OS paths are never run. Row P4 wires the runner into the facts read; nothing calls it yet.
4. Threat notes for P4 and later Yes docs/fork/README.md, server.placement. Placement guards against mistakes, not against a determined agent with a shell. factsCommand runs with the server's rights. A linked machine's own report is taken as that machine's word, and a refused answer is refused.
5. A wrong-OS factsCommand kept in its read form Already safe (no change) Only the placing view (readPlacementForThisOs) turns such a value into off with a reason, and that view is never saved. The settings file keeps the person's text byte for byte; placementSettingsFile.test.ts asserts file.placement equals what was written.
Owner comment: placement reads through readPlacementForThisOs; the runner checks again Yes The README now says both. The runner's check again is item 3.

#821 (P3 review)

Point Done Evidence
1. The FORK_CHANGES row links #779 Yes It now links #820.
2. "Nothing is stored" and the cost of the read Yes The README now says facts are never stored, but the gate's status read first clears spent wait holds (the same step wait_list runs) and gets at most 6 s. A cheaper read was considered. The facts read now runs the host read and the slot count at the same time. With slots off, placement no longer waits for the host. With slots on, the gate's own count reads the same sample (see item 3). The gate's usage read is kept because it is the one source wait_list shows, and its clean-up is idempotent.
3. The host read needs its own timeout Yes HOST_READ_TIMEOUT (2 s). A hung sampler leaves out hostResources, and placement still comes. Tests: "a host read that hangs is given up at its own limit". With slots on, the gate's count reads the same sample for its memory floor, so placement is "not available (the gate could not count)" after 6 s, never a guessed count. That is the gate's own answer at launch. Test: "a hung sampler with slots on" in environmentFacts.realGate.test.ts. The README says this.
4. available ignores per-account limits Yes (docs) The README says available and slotsFree count only the machine limit and the memory floor; the account is checked at launch by the gate.
5. No test runs the real slot gate Yes New environmentFacts.realGate.test.ts runs SlotGate.layerOver with only its inputs replaced (settings, running threads, host sample). It covers counts, full, below the floor, slots off, and a second run giving the same result.

Tests that fail without the fix

Each fix was reverted in place, the new tests were run against it, and the files were then restored from the commit:

  • Engine (removed the problem check in H2, removed the here exemption in H2, read needs raw instead of through readPlacementNeeds): 7 of 127 fail. These are the H2 "did not decode" and "here without a host sample" tests, H6 "broken needs", "a broken needs value fits no machine", "a sampler failure here does not stop all delegation", "missing, stale or partial facts never fit", and the labels test.
  • factsFromAnswer (no decode): 7 fail. These are "a good answer … newer build's field ignored" and all six "reported as not valid" cases.
  • Host read timeout (removed): "a host read that hangs is given up at its own limit" fails, by timing out.
  • factsCommand (removed the path check again and the .exe rule): 4 fail. On linux, darwin and win32 a wrong-OS path "is never run", and "on Windows only an .exe runs".
  • Review fixes, here fallback (fallback without the weight and memory limits): 1 of 127 fails, "a sampler failure here does not stop all delegation", at the new manual case.
  • Review fixes, factsCommand (kept the cut last line, and read a signal as "could not run"): 3 of 9 fail. These are "output cut at its limit", the stand-in signal case, and the real kill -9 program.
  • The real-gate tests (Placement P3 minors from the #820 lens #821.5, plus the hung sampler with slots on) and the label merge (Placement P2 follow-ups for P4/P5 (from the #798 review) #805.2) pin existing behaviour; they are coverage, not fixes.

Checks run

All of these ran locally behind the machine's capacity gate:

  • vitest packages/shared forkPlacement.test.ts: 127 passed
  • vitest apps/server src/fork/ plus SlotGate.test.ts: 61 files, 1320 passed
  • typecheck of apps/server and packages/shared
  • vp lint on the changed files
  • knip
  • vp fmt --check
  • the seam check as PR CI runs it (--config-ref main): "only the pinned seams"

Review

One code lens (Opus, high effort) reviewed the first commit. It found six points, and the second commit fixes all six:

  1. The here fallback ignored a manual or unknown weight.
  2. The fallback ignored upstream's 5 % free-memory limit when CPU was not measured.
  3. The docs said a hung sampler never delays placement. That is false with slots on; the docs are fixed and a test is added.
  4. Output cut at the limit could turn its partial last line into a label.
  5. A signal-killed program read as "could not run".
  6. The shell-sign test proved nothing. It now checks $# and $0.

It also noted that with slots off nothing checks again at launch; the comment and README now say so.

State question

  • Runs again: factsFromAnswer, choosePlacement and the facts read are pure, or re-measure each time. Tests show that the same input gives the same output (the answer decode, the real gate read, two runs of factsCommand). factsCommand is run again on each read and leaves nothing behind.
  • Stops half-way: nothing here writes. When a read is cut off (host past 2 s, slot count past 6 s, factsCommand past 5 s), the result is "no host facts" or "not available" with a reason, never a guessed count. The factsCommand process is stopped at its timeout (timedOutResult). Output cut at its limit gives only the whole lines, and a program a signal ends is "not available".
  • Old or empty state: an older linked build (neither field) means no facts. A newer build's extra field is ignored. A broken stored placementNeeds fits no machine. An empty factsCommand is available with no labels and nothing runs. This machine with no sampler is still a last-place fallback, so a sampler failure does not stop all delegation.

General vs project

All of this is general fork code. Machine facts come from each machine's own settings (placement.labels, placement.factsCommand, agentSlots) and from its live readings. Nothing machine- or project-specific is hard-coded. The only numbers are generic limits: 2 s, 5 s, 16 KB, 64 labels.

Seam note

packages/contracts/src/placement.ts and its test are the fork's own files (P2, #798), but .github/fork-seams.json forkFiles does not list them. As a result, the seam check reads any change to them as an upstream edit. This PR leaves them alone. A future change to them needs a list-only pull request first that adds packages/contracts/src/placement* to forkFiles; upstream has no such file.

Left for later rows (not in this PR)

🤖 Generated with Claude Code

scuttletoby and others added 2 commits November 22, 2026 14:52
…r, facts read limits (#794, #805, #821)

- factsFromAnswer decodes a linked machine's t3_environment_read answer with
  P3's schema: neither field is no facts (null); a refused answer carries a
  problem and the engine refuses that machine, named too.
- Rule engine: here without a host sample passes H2/H8 and is the last
  fallback; needs are read through readPlacementNeeds (a broken value fits
  no machine); labels use contracts' one canonical form.
- factsCommand runner (not wired yet): re-checks the path on its own OS,
  no shell, no args, .exe only on Windows, 5 s limit, capped output.
- Facts read: the host read has its own 2 s limit and runs beside the slot
  count; a test runs the real slot gate.
- Docs: threat notes, clean-up wording, per-account limits; FORK_CHANGES
  links the P3 PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The here fallback keeps upstream's weight rule (manual or an unknown word
  stays out) and its 5 % free-memory limit when memory was measured.
- factsCommand: a run cut at the output limit drops its last, maybe
  partial, line; a program a signal ends reads as "stopped by a signal".
- The real-program test now proves the path ran whole with no arguments.
- A real-gate test for a hung sampler with slots on; the README now says
  that case gives "not available" after 6 s, and that with slots off
  nothing checks again at launch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: lens agent (Opus) · read at: 13bfe5d
verdict: safe to merge
covered: the whole PR at the head (both commits): packages/shared/src/forkPlacement.ts and its test, apps/server/src/fork/factsCommand.ts and its test, apps/server/src/fork/environmentFacts.ts and its three tests (answer, realGate, existing), docs/fork/README.md, FORK_CHANGES.md; every point of #794, #805 and #821 with their comments, and the umbrella #883; the PR body; contracts' placement.ts, playtest.ts, upstream's chooseLoadBalancedEnvironment, ProcessRunner and resolveSpawnCommand for what the new code calls.
checks: ran each changed test file alone: forkPlacement 127/127, environmentFacts.answer 10/10, environmentFacts.realGate 4/4, environmentFacts 9/9, factsCommand 9/9. Undid three fixes in place, then restored: (1) H2's problem check removed: 2 engine tests and the 6 "reported as not valid" answer tests fail; (2) the fallback without its weight and memory limits: "a sampler failure here does not stop all delegation" fails, and it fails again with only the 5 % memory limit removed, so both limits are pinned; (3) the runner's path check again removed: the 3 wrong-OS "never run" tests fail; the .exe rule removed: "on Windows only an .exe runs" fails. Ran the seam check as PR CI runs it (--config-ref main): only the pinned seams. A throwaway one-line edit to packages/contracts/src/placement.ts then fails it ("upstream file with 1 fork line but no entry"). PR CI is green at this head.
state: runs again: factsFromAnswer and choosePlacement are pure and the facts read measures again, so the same input gives the same answer (tested for the decode, the here fallback, the real gate and two factsCommand runs); nothing is stored. Stops half-way: nothing here writes; a host read past 2 s leaves out hostResources (here becomes the last-place fallback with slots off; with slots on the gate cannot count within 6 s, so available is false and H4 refuses here, the same answer the gate gives at launch); a factsCommand past 5 s is stopped and reads as not available; output cut at 16 KB drops its partial last line. Old or empty state: an older peer's answer with neither field decodes to null (unnamed: refused; named: only H1, H3, H7, H9); an answer the schema refuses (wrong type, negative count, CPU above 1, no factsAt, a string, null) carries only problem, and H2 refuses it, named too, and here's no-host exemption comes after that check; a broken saved placementNeeds returns none before the named path, so no machine fits, here and a named machine without facts included; an empty factsCommand is available with no labels and runs nothing.
focus: a machine whose facts are invalid is never picked: yes (H2 checks problem first; without that check a problem-only answer is still refused, as "no placement facts"). The last-resort rule keeps weight and free memory: yes (fitsAsFallback = here without a CPU sample AND a usable weight AND not 5 % or less free when memory was sampled; H2 still checks the sampled numbers and the receipt time; H8 still checks slots and the floor). The factsCommand runner: empty runs nothing; the path is checked again against HostProcess.Platform; it runs as one path with args: [], and on Linux and macOS resolveSpawnCommand passes it with shell: false (the real-program test proves $# is 0 and $0 is the whole path with a space and ;); on Windows only a path ending in .exe is passed on; timeout, output and label caps are set. One label function: yes, the engine's canonicalLabels now calls contracts' canonicalPlacementLabels, with non-strings dropped first.
seams: packages/contracts/src/placement.ts (and its test) need a .github/fork-seams.json forkFiles entry (packages/contracts/src/placement*, like playtest*) before any PR edits them: upstream has no such file, and the seam check reads an edit to it as an upstream edit (checked above). This PR does not touch it, so no entry is needed for this PR; a list-only PR first, as the PR body says.
blocker: none
major: none
minor: packages/shared/src/forkPlacement.ts:439 the doc comment "Why upstream's score left out a machine that passed every hard rule." is now orphaned above isLowOnMemory's own comment; it belongs on unrankedReason (line 453).
minor: packages/contracts/src/placement.ts:40 the comment still says canonicalPlacementLabels is "Same as placement row P5's canonicalLabels ... merge the two into one later"; after this PR the engine calls it. It cannot change here (seam rule above); fix it in the PR that adds the forkFiles entry.
minor: apps/server/src/fork/factsCommand.ts:52 the .exe rule checks the configured text, not the path resolveSpawnCommand resolves. If the server's PATHEXT ever lacks .EXE, packages/shared/src/shell.ts:541 tries <path>.exe plus each PATHEXT ending, so a facts.exe.CMD next to it would run with shell: true (shell.ts:1022). This needs an odd server env and a planted file, and nothing calls the runner yet; P4 could refuse unless PATHEXT has .EXE, or check the resolved command.
minor: apps/server/src/fork/factsCommand.ts:82 on a non-zero exit the reason takes the first line of stdout before stderr, so a label line hides the program's error ("exited with code 2: gpu" in the test, not "all slots used"). Prefer stderr's first line, then stdout's.
minor: apps/server/src/fork/environmentFacts.ts:133 factsFromAnswer(null) or (undefined) gives problem, not null. A named machine that did not answer then fails H2 instead of meeting only the no-facts rules. That fails closed; P6 should pass facts: null for "no answer" and call factsFromAnswer only on an answer it received. One line in its comment would say so.

🤖 Generated with Claude Code

@scuttletoby
scuttletoby merged commit dec9bc0 into main Nov 22, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant