9 Commits
Author SHA1 Message Date
Vlad Lasky a6c67f8cb2 history.py: keep every line and byte of the file when adding a row
An earlier commit on this branch made a corrupt history readable by
decoding with errors="replace" and skipping lines that are not JSON
objects. But add() rewrote the file from the rows it had parsed, so the
next add destroyed whatever it could not read, where upstream crashed
and left the file alone:

- Windows PowerShell 5 writes UTF-16 LE by default (`$rows | Out-File`).
  The whole file failed to parse, `moves.py` said "no moves saved yet",
  and the next add left a file with only the new row, with no warning.
- A cp1252 file from `Set-Content` ("Łódź" saved as ANSI) was listed as
  "L�dz", and the next add wrote U+FFFD into the file for good.
- Any line that was not a JSON object was dropped at the next add.

Now:
- A file that starts with a UTF-16 BOM (FF FE or FE FF) is decoded as
  UTF-16, and the next add writes it back as UTF-8.
- Anything else is decoded as UTF-8 with errors="surrogateescape" and
  written back the same way, so bytes that are not UTF-8 survive byte
  for byte. The listing shows them as "?", since stdout already uses
  errors="replace".
- add() keeps the existing lines as text and appends the new row,
  instead of re-serialising parsed rows. Lines that are not JSON objects
  stay in the file. They count toward the 300-row cap and are trimmed
  oldest first like the rest. Blank lines are dropped, and a "\r" at the
  end of a line is removed, so a CRLF file comes back with one line
  ending per row.
- load() still returns only the lines that parse as JSON objects.

On Linux: the bytes of a cp1252 file were unchanged after an add, UTF-16
LE and BE files with CRLF kept all 3 rows and became UTF-8, junk lines
("not json at all", 5, null) were still there after an add, and a
300-line file with a junk first line dropped exactly that line when one
row was added.
2026-11-26 14:56:06 +11:00
Vlad Lasky 95ee5116fc history.py: retry a read that Windows refuses during a rename
Readers don't take the lock. On Windows, opening history.jsonl while a
writer's os.replace is swapping it in fails with PermissionError for a
moment. load() turned that into "could not read the history" and an
empty list, so `moves.py` printed "no moves saved yet" and seed.py
stopped avoiding recent values, even though nothing was wrong with the
file.

Opening and reading now go through the same short retry as the rename
in add(): PermissionError is retried every 20 ms, up to 50 times (about
1 s). On Linux and macOS a file that really can't be read now warns
after that second instead of at once.

On Windows (Python 3.14, NTFS), one process looping read() while 4
others did 40 adds each got PermissionError on 104, 90 and 78 of about
4400 to 5200 reads in three runs before, and on 0 of about 3500 to 4500
reads after.
2026-11-26 14:56:06 +11:00
Vlad Lasky 3801e69d03 history.py, seed.py, moves.py: warn instead of crashing when history fails
With an unwritable history location (ANTIALGORITHM_HISTORY under a
read-only folder), seed.py printed the seed and then exited 1 with a
PermissionError traceback, and `moves.py add` failed the same way. An
agent that sees a failed command may throw the seed away and pick a
start point from its own taste, which is what the skill forbids, so the
seed has to succeed whatever happens to the history.

Now:
- load() turns a read error (no permission, a folder in the way) into
  one line on stderr and an empty history, so seeds are still rolled,
  only without avoiding recent values.
- seed.py prints the seed, and if saving fails it prints one line on
  stderr and still exits 0.
- `moves.py add` prints "could not save the move: <reason>" and exits 1,
  since saving is its whole job, but without a traceback.

add() still reads the file itself and fails on a read error rather than
treating the history as empty, so a file it cannot read is never
overwritten with a single row. This is one finding across three files:
the history module decides what a failure is, and each script decides
what it means for its own exit status.
2026-11-26 14:56:06 +11:00
Vlad Lasky 67d79fd095 history.py: lock the history file while adding a row
Agents can run several sessions at once, and each one rolls seeds and
saves moves. add() read the whole file, appended a row and wrote it back
through a shared PATH + ".tmp", so parallel writers broke it two ways:

- 40 parallel `seed.py idea` runs gave a FileNotFoundError traceback
  when one writer renamed the shared tmp file away from under another,
  and only 38 of 40 rows were saved;
- even with distinct tmp names, two writers that read the same file each
  wrote back their own copy without the other's row: 20 parallel
  `moves.py add` runs left 18 rows.

add() now holds a lock file (history.jsonl.lock) across its read and
write. The lock is taken with os.open(O_CREAT | O_EXCL), which is atomic
on Linux, macOS and Windows and needs only the standard library; fcntl
is Unix-only and msvcrt is Windows-only. A writer that finds the lock
retries every 20 ms for up to 15 s. A lock older than 10 s is taken to be
left by a crashed writer and removed, since a real holder keeps it for
milliseconds; so a leftover lock costs one wait at most, never a
permanently broken history. The tmp file name includes the pid, so a
writer that breaks a stale lock still can't clobber another's tmp file.

On Windows, os.open on a lock file that another process is deleting
fails with PermissionError (ERROR_ACCESS_DENIED), not FileExistsError,
so on Windows PermissionError is retried the same way. A first version
that did not saved 196 and 193 of 200 rows in two runs of 20 processes
x 10 adds on Windows (Python 3.14, NTFS), with every failure at
os.open; with the retry 7 runs saved 1400 of 1400. The cost is that on
Windows a lock that can never be created (a read-only folder) fails
after the 15 s wait, with "could not create the lock file <path>",
instead of at once. On Linux and macOS PermissionError there always
means the folder is not writable, so it still fails at once.

On Windows, os.replace fails with PermissionError while another process
has the target open, which a reader (seed.py, moves.py) may have for a
moment, so the rename is retried for up to 1 s. migrate() takes the same
lock and re-checks before copying, so two first runs can't both copy the
old repo-root file over a row that one of them has just added. The
300-row cap is unchanged.

After: 40 parallel seeds saved 40 rows and 20 parallel adds saved 20
distinct moves, with empty stderr, in each of 4 runs; 320 more seeds
from 8 parallel loops left exactly 300 rows.
2026-11-26 14:56:06 +11:00
Vlad Lasky 9411daa610 history.py, seed.py, moves.py: skip malformed history rows
One bad row in history.jsonl broke both scripts until the file was
deleted by hand:

- a byte that is not UTF-8 (`{"type":"move","move":"x \xff"}`) raised
  UnicodeDecodeError in load();
- a line that is valid JSON but not an object (`5`, `null`) raised
  AttributeError on entry.get();
- `"seed": null` or `"seed": ["a"]` raised AttributeError in
  load_recent(), and `"seed": {"color": ["a"]}` raised an unhashable
  TypeError in pick();
- a move row without "move" raised KeyError in the listing.

load() now decodes with errors="replace", so a stray byte costs one
character instead of the whole file, and returns only JSON objects.
load_recent() skips seeds that are not objects and values that are not
strings, and moves.py lists only rows whose move is a string. Bad shapes
are filtered out before the recent-seed and --last slices, so they don't
take a slot from a good row.

This is one finding across three files: the reader and both consumers
each crashed on a different shape of the same corrupt file. Rows that
are JSON objects are written back unchanged by the next add, so no good
row is lost; only lines that are not JSON objects drop out.
2026-11-26 13:57:42 +11:00
Vlad Lasky 3caf74aa65 history.py: split history on newlines only and skip a BOM
load() used str.splitlines(), which also splits on U+2028, U+2029,
U+0085, form feed and the other Unicode line breaks. json.dumps with
ensure_ascii=False writes those raw, so `moves.py add "a<U+2028>b"`
printed "saved:" but the row came back as two broken halves: the next
listing said "no moves saved yet", and the next add rewrote the file
without it.

A UTF-8 BOM, as left by Notepad, made the first line fail to parse, so
that row was dropped silently and erased at the next write.

Rows are written one per "\n", so split on "\n" alone, and read with
utf-8-sig so a leading BOM is skipped. newline="" keeps a "\r" from a
CRLF edit at the end of its line, where json.loads ignores it.
2026-11-26 13:57:42 +11:00
Vlad Lasky a2519e6216 history.py: resolve the history path before using it
ANTIALGORITHM_HISTORY=rel.jsonl made every write fail, because
os.makedirs(os.path.dirname("rel.jsonl")) is os.makedirs("") and raises
FileNotFoundError. A directory in ANTIALGORITHM_HISTORY raised
IsADirectoryError, "~/x.jsonl" created a folder literally named "~" in
the current directory, and a relative XDG_STATE_HOME was used as given,
although the XDG Base Directory spec says relative paths are invalid and
must be ignored.

Now ANTIALGORITHM_HISTORY is tilde-expanded and made absolute, a
directory there means history.jsonl inside it, and XDG_STATE_HOME is used
only when it is absolute, falling back to ~/.local/state.
2026-11-26 13:57:42 +11:00
mankidev 0ce7f88ab8 Added plugin install and moved seed history out of the skill folder 2026-11-20 19:09:12 +03:00
mankidev ff7c3c4a46 Added interface rules, seed history and move log 2026-11-18 16:34:43 +03:00