Repository navigation
fix(factscribe): find Python on Windows, keep CRLF line endings on save - #160
MyPrototypeWhat merged 4 commits into
Conversation
Two fixes: one for Windows machines, one for manuscripts that use CRLF.
1. Windows interpreter discovery
The candidate list had no Windows shape at all -- no "py" launcher, no
versioned command, no common install directory -- and a failed probe was
collapsed into "not installed". A user with Python 3.9 on the machine was
told to "install Python >=3.10", with nothing to act on.
- new miniapp/node/python_discovery.mjs: candidates are {command, args}
and cover py, py -3, py -3.10 ... py -3.14, py.exe, versioned commands,
python.exe and absolute paths under %LOCALAPPDATA%, %PROGRAMFILES% and
C:\Python3xx -- 36 candidates on Windows. The macOS/Linux list is
unchanged, word for word.
- the probe classifies the outcome: version high enough / too low /
command missing / present but the version cannot be read. The message
therefore names the version it found and the executable it came from.
- discovery and engine start share one "command + prefix arguments" pair,
so "py -3" is used for both, not only for probing.
- the settings page and the engine start failure share one message:
not installed / too old (3.9 and below cannot run this engine) /
installed but not on PATH (point FS_PYTHON at it) / Microsoft Store
placeholder alias that cannot report its version.
The requirement is unchanged: the engine needs Python >= 3.10.
2. Browser-layer CRLF preservation
The engine reads sections with Python's universal-newline mode (CRLF
becomes LF before the text ever reaches the page) and writes the file
back byte for byte, so every save rewrote the whole file with LF
endings -- a CRLF manuscript lost all its CR bytes on the first edit.
- new miniapp/node/eol.mjs: split with the endings kept apart, recognise
crlf / lf / mixed, put the original endings back.
- the Node shell reads the real endings from disk before the save, hands
the engine an LF-only body and restores the endings afterwards: lines
the save did not touch keep their exact endings (a mixed file stays
mixed), the edited lines take the file's dominant ending, and the text
is never altered -- only the endings move. If the file changed
underneath while the save ran, the endings are left alone.
The vendored Python engine is untouched by both changes.
Verified:
- upstream checker: plugins/unclemoc/factscribe OK (0 errors, 0 warnings);
npm run check exits 0 (92/92 checker self-tests).
- package tests: node --test tests/*.test.mjs -- 19 cases, all green
(16 unit cases for the line-ending module, plus 3 end-to-end cases that
start the vendored Python engine and save into a copy of the sample
paper).
- measured on the vendored sample paper: a 678-byte section with 11 CRLF
endings came back with all 11 endings and the same text; without the fix
the engine's own save rewrote it as 667 bytes with 0 CRLF.
Not verified:
- Windows end to end. There is no Windows machine here. The 36 Windows
candidates are generated from the platform string and covered by unit
tests, but no run on a real Windows host has been done.
- Linux hosts, as before.
…ackage The vendored copy of the Python engine in this package predates the newline-fidelity work. It read user files in universal-newline mode, so a CRLF file was already LF by the time the text reached the page, and it wrote several user-owned files in platform default text mode, where a CRLF string is written as \r\r\n on Windows. The Node shell restored the endings after each save, but the engine itself still lost them, and the Node layer can only put a file back when its line contents still match. Re-vendor the engine from the FactScribe source tree, which now carries the newline-fidelity work: - new factscribe/textio.py: read_text_exact / write_text_exact / append_text_exact keep the file's own line endings, and to_lf gives the analysis layer an explicit normalisation point; - the read side uses read_text_exact, the write side write_text_exact; - machine-generated artefacts (JSON reports, audit matrices, manifests) keep using the platform default, so their format stays predictable. The vendored tree is produced by miniapp/build.sh and is byte-identical to the source tree apart from the excluded web/ directory. Measured on the vendored sample paper, saving through this package: a 678-byte section with 11 CRLF endings came back as 678 bytes with 11 CRLF endings and the same sha256; the LF file (667 bytes, 11 LF) and the mixed file (668 bytes, 1 CRLF + 10 LF) came back byte-identical. With the previous engine the Node layer had to repair the file after every CRLF and mixed save; with this engine the engine's own write is already faithful. Not verified: Windows end to end, and Linux.
|
A blocking save issue was reproduced at Please make line-ending handling and fingerprint calculation consistent across both paths while preserving the document's original CRLF line endings on save. Add a regression test covering the full flow: retrieve the paragraph fingerprint from the page response, then use it to successfully save a multiline CRLF body. This issue blocks merging. We'll review the updated commit once the fix is ready. |
…ackage
Two Windows defects that the vendored engine still had, found on a real
Windows host (winbox 10.0.1.9, ACP=936) and fixed in the engine source
tree before re-vendoring it.
D1 - a write-side landing point that cannot be resolved must be refused,
not crash.
The engine resolved write targets with pathlib.Path.resolve(). On a
symlink cycle that raises RuntimeError on Python 3.10-3.12 (and does
not raise at all on 3.13+), so a section file that was a symlink loop
escaped as an uncaught exception - "internal error", exit 4 - instead
of a refusal with a judgement code. A section symlinked outside the
project escaped the same way from the closure builder.
New factscribe/landing.py owns landing resolution: it resolves with
os.path.realpath(strict=True) (same answer on every Python version),
treats "cannot be resolved" as a semantic condition rather than an
errno whitelist (Windows raises winerror=1921 / errno=EINVAL for a
cycle, not ELOOP - errno.ELOOP is 10062 there), and the three write
gates - _safe_section_file, _root_tex_file, write_section_data - now
refuse with their own existing codes (SECTION_PATH_INVALID /
PATH_ESCAPE) instead of crashing. The "outside the project root"
wording is unchanged, and an in-project symlink is still written
through.
D2 - a Chinese Windows console must not break the tool.
The human-readable output contains U+2713 / U+2717 / U+2194, which
cp936 cannot encode. print() raised UnicodeEncodeError, and the
command layer turned that into exit 4. New factscribe/console.py
installs a custom encoding error handler on stdout/stderr at the CLI
entry: characters that cannot be encoded become equivalent ASCII
(checkmark -> v, xmark -> x, arrow -> <->), the command runs to
completion and keeps its own exit code, the stream keeps its own
encoding, and --json payloads are unchanged.
The vendored tree is still generated by miniapp/build.sh and is
byte-identical to the source tree apart from the excluded web/ directory.
Measured on the real Windows host, same machine and same commands before
and after (A/B):
* doctor / fonts --status / mirror-check / review / tide under cp936:
exit 4 -> exit 0; audit --json unchanged (exit 1 both ways);
* a section file that is a symlink cycle: uncaught RuntimeError with
no code -> ConfigError SECTION_PATH_INVALID / PATH_ESCAPE, and
zero bytes written outside the project;
* a section symlinked outside the project: unchanged refusal and
unchanged wording;
* an in-project symlink: still written through;
* the F4 newline fidelity checks (CRLF / LF / mixed): byte-identical
results before and after - no regression;
* the package's own node tests, 19 cases including 3 that start the
vendored Python engine: all green on the Windows host.
Not verified: the Mini App installed and driven through the host UI on
Windows; Linux.
The page and the save path read the same manuscript differently, and the paragraph fingerprint is a sha256 over that text - so a manuscript with CRLF line endings could not be saved with the fingerprint the page had just given. The page side went through the audit closure, which reads in universal newline mode: a CRLF file was already LF by the time the paragraph reached the browser. The save side reads the file byte-exact, so the raw it fingerprints still had its CR bytes in it. The two sides therefore disagreed by exactly the CR bytes, and saving such a paragraph was refused with HTTP 409 BLOCK_STALE. A paragraph whose text is a single line hid it - there are no CR bytes inside it, so both sides happened to agree. Reproduced at 908bdf9 and still present at 3adf295: open a document whose body has several CRLF lines, take the paragraph fingerprint from the page response, save with it - HTTP 409, BLOCK_STALE. Both sides now use the file's own bytes: - workbench/projects.py::_section_files reads each section file with read_text_exact instead of taking the closure's universal-newline text, so the raw the page is rendered from is the raw the save path fingerprints. The cover abstract follows the same rule in both of its layouts (section file and root file), which used to disagree as well. - The fingerprint itself is unchanged (sha256 over the block raw, first 16 hex digits); only the text it is taken over is now the same on both sides. Saving still preserves the manuscript's own line endings, byte for byte: - New factscribe/textio.py::restore_newlines puts the block's original endings back on the text that is about to be written. A contenteditable always hands back LF, so without this a CRLF file would lose its CR bytes on the first save. The endings are restored per line, so a file with mixed endings stays mixed, and endings the block did not have are taken from the file. - The structural work (protection-surface scan, anchor re-insertion, surface comparison) runs on the LF-normalised copy on both sides, so a CRLF manuscript takes exactly the same decisions as an LF one. - New regression test tests/save-fingerprint.test.mjs covers the whole flow the review names: take the paragraph fingerprint from the page response, save a multi-line CRLF body with it, and assert that the save succeeds and that the file on disk is byte-identical to the file before the save. A second case does the same for an LF body (it must not come back as CRLF), and a third one sends the LF text a browser actually sends for a CRLF file (it must come back as CRLF). - tests/save-eol.test.mjs now derives the package root with fileURLToPath, so the package's own test suite also runs on Windows (a URL pathname is /C:/... there). Measured: - macOS 26.6.2, Node 26.7, Python 3.14: the reproduction above returns HTTP 200 and the file is unchanged byte for byte. The new test is red against 3adf295 (409 BLOCK_STALE) and green with this change. - Package tests: node --test tests/*.test.mjs - 22 cases, all green (the three new ones start the vendored Python engine and save into a copy of the sample paper). - Windows NT 10.0.26200.9974 (winbox 10.0.1.9, ACP=936), portable Python 3.12.10, Node 24.22.2: the same 22 cases green, including the three new ones - the CRLF fingerprint save path works on the real host. The newline fidelity checks (CRLF / LF / mixed) are byte identical to the disk, a symlink cycle is still refused with SECTION_PATH_INVALID / PATH_ESCAPE and writes nothing outside the project, and the human-readable commands still exit 0 under cp936. - Upstream checker: plugins/unclemoc/factscribe: OK - 0 errors, 0 warnings. npm run check exits 0 (92/92). - The engine's own Python suite against the vendored copy inside this package: 2399 cases and 397 subtests, all green. Not changed: no judgement code, no exit code, no wording; the golden baseline is untouched.
|
Fixed in 9912594 — the paragraph fingerprint and the save path now use one line-ending basis, and CRLF bodies still save as CRLF. The two sides disagreed by exactly the CR bytes: the page side went through the audit closure, which reads in universal-newline mode (a CRLF file was already LF by the time the paragraph reached the browser), while the save side fingerprinted the file byte-exact. Both now read the file's own bytes. Regression test added in this PR: Happy to rebase or split the commits if that helps review. |
Three fixes for the FactScribe Paper Workbench package. The vendored Python
engine now reads and fingerprints the manuscript on one line-ending basis, so a
manuscript with CRLF line endings can be saved with the paragraph fingerprint
the page just gave it; on Windows the Mini App can find the Python interpreter
the machine already has; and saving a manuscript no longer rewrites its line
endings. The vendored Python engine has been upgraded as well: it keeps the
file's own line endings itself instead of leaving that to the Node shell to put
back afterwards, and it no longer crashes on a Chinese Windows console or on a
write target whose landing point cannot be resolved.
Written by MiniMax Code (MCode)
The paragraph fingerprint and the manuscript's line endings
This was a blocking review on this PR, and it is reproduced and fixed here.
What was broken
The page and the save path read the same manuscript differently, and the
paragraph fingerprint is a sha256 over that text - so a manuscript with CRLF
line endings could not be saved with the fingerprint the page had just given
it.
factscribe/config.py::_read_text->
Path.read_text()), which reads in universal-newline mode: a CRLF filewas already LF by the time the paragraph reached the browser.
(
factscribe/textio.py::read_text_exact,newline=""), so the raw itfingerprints still had its CR bytes in it.
The two sides therefore disagreed by exactly the CR bytes, and the fingerprint
gate refused the save with HTTP 409
BLOCK_STALE("this paragraph waschanged elsewhere, please edit it again"). A paragraph whose text is a single
line hid the defect - there are no CR bytes inside it, so both sides happened
to agree. That is why the earlier end-to-end test passed: it saved the
section heading, a one-line block.
Reproduced at 908bdf9 and still present at 3adf295 (the two earlier commits
on this branch do not touch the fingerprint path): open a document whose body
has several CRLF lines, take the paragraph fingerprint from the page
response, save with it:
The page had answered with sha
501dfe617f20e728(the fingerprint of the LFtext) while the save path computed
145322cc931203af(the fingerprint of thesame paragraph with its CRLF endings).
What changed
Both sides now use the file's own bytes.
factscribe/workbench/projects.py::_section_filesreads each section filewith
read_text_exactinstead of taking the closure's universal-newlinetext, so the raw the page is rendered from is the raw the save path
fingerprints. The cover abstract follows the same rule in both of its
layouts (a section file and the root file), which used to disagree with
each other as well.
hex digits (
factscribe/workbench/projects.py::short_sha). Only the text itis taken over is now the same on both sides.
Saving still preserves the manuscript's own line endings, byte for byte:
factscribe/textio.py::restore_newlinesputs the block's originalendings back on the text that is about to be written. A
contenteditablealways hands back LF (the DOM has no CRLF), so without this a CRLF file
would lose its CR bytes on the first save. The endings are restored per
line, so a file with mixed endings stays mixed, and endings the block did
not have are taken from the file.
comparison - runs on the LF-normalised copy on both sides, so a CRLF
manuscript takes exactly the same decisions as an LF one. The judgement
codes, the wording and the exit codes are untouched.
The regression test the review asked for
New
tests/save-fingerprint.test.mjscovers the whole flow:(
GET /api/disk/<project>/<disk>/block/<id>);(
POST /api/disk/<project>/<disk>/edit);to the file before the save - same bytes, same CRLF endings.
Two more cases: the same flow for an LF body (it must not come back as
CRLF), and the same CRLF file with the LF text a browser actually sends (it
must come back as CRLF).
The new test is red against 3adf295 (409 BLOCK_STALE) and green with this
change.
tests/save-eol.test.mjsnow derives the package root withfileURLToPath, so the package's own test suite also runs on Windows (a URLpathname is
/C:/...there).Windows interpreter discovery
The candidate list had no Windows shape at all — no
pylauncher, no versioned command, no common install directory — and a failed probe was collapsed into "not installed". A user with Python 3.9 on the machine was told to "install Python >=3.10", with nothing they could act on.miniapp/node/python_discovery.mjs. Candidates are now{command, args}pairs and coverpy,py -3,py -3.10…py -3.14,py.exe, the versioned commands,python.exe, and absolute paths under%LOCALAPPDATA%,%PROGRAMFILES%andC:\Python3xx— 36 candidates on Windows. The macOS/Linux list is unchanged, word for word.py -3starts the engine too, not only the probe.FS_PYTHONat it) / a Microsoft Store placeholder alias that cannot report its version.The requirement itself is unchanged: the engine needs Python >= 3.10.
Browser-layer CRLF preservation
The engine used to read sections with Python's universal-newline mode — CRLF became LF before the text ever reached the page — and wrote the file back byte for byte. Every save therefore rewrote the whole file with LF endings, so a CRLF manuscript lost all its CR bytes on the first edit. The browser layer is the first line of defence and stays.
miniapp/node/eol.mjs: split the text with the endings kept apart, recognisecrlf/lf/mixed, and put the original endings back.The vendored Python engine keeps line endings too
The package ships a copy of the Python engine under
miniapp/node/engine/, and that copy was still the older build. It read user files in universal-newline mode, so a CRLF file was already LF by the time the text reached the page, and it wrote several user-owned files in platform default text mode, where a CRLF string is written as\r\r\non Windows. The Node shell puts the endings back after each save, so the file on disk looked right — but the engine had already lost the endings, and the Node layer can only repair a file whose line contents still match.The vendored engine has been re-vendored from the engine source tree:
factscribe/textio.py:read_text_exact/write_text_exact/append_text_exactread and write with the file's own line endings — no universal-newline translation on the way in, no platform translation on the way out — andto_lfgives the analysis layer one explicit place where line endings are normalised in memory. Machine-generated artefacts (JSON reports, audit matrices, manifests) keep the platform default, so their format stays predictable.read_text_exact; the write side useswrite_text_exact.miniapp/build.shand is byte-identical to the source tree apart from the excludedweb/directory.The vendored Python engine also fixes two Windows defects
Two more Windows defects were found on a real Windows host (winbox 10.0.1.9, console code page ACP=936 / gb2312) after the engine above had been vendored. Both are fixed in the engine source tree, and this vendored copy carries the fixes.
D1 - a write-side landing point that cannot be resolved must be refused, not crash
The engine resolved write targets with
pathlib.Path.resolve(). On a symlink cycle that raisesRuntimeErroron Python 3.10-3.12 and does not raise at all on 3.13+, so a section file that was a symlink loop escaped as an uncaught exception - "internal error", exit 4 - instead of a refusal with a judgement code. A section symlinked outside the project escaped the same way from the closure builder.factscribe/landing.pyowns landing resolution. It resolves withos.path.realpath(strict=True), so every Python version gives the same answer, and it judges "cannot be resolved" semantically instead of by an errno whitelist: on Windows a symlink cycle raisesOSError(winerror=1921, errno=22 EINVAL), notELOOP(the Windowserrno.ELOOPconstant is 10062), so a whitelist would miss it on that platform._safe_section_file,_root_tex_file,write_section_data- now refuse with their own existing codes (SECTION_PATH_INVALID/PATH_ESCAPE) instead of crashing. The "outside the project root" wording is unchanged, and an in-project symlink is still written through.D2 - a Chinese Windows console must not break the tool
The human-readable output contains U+2713 / U+2717 / U+2194, which cp936 cannot encode.
print()raisedUnicodeEncodeErrorand the command layer turned that into exit 4.factscribe/console.pyinstalls a custom encoding error handler on stdout/stderr at the CLI entry: characters that cannot be encoded become equivalent ASCII (checkmark tov, xmark tox, arrow to<->). The command runs to completion and keeps its own exit code, the stream keeps its own encoding (Chinese text redirected to a file is unchanged), and--jsonpayloads are untouched.What this does not change
This engine line has no write-side landing gate of its own - the gate is a feature of the main engine repository, not of this package - so there is no
FACT_FILE_UNRESOLVEDcode and no exit-5 contract here. What this PR transplants is the principle: a landing point that cannot be resolved is refused with a judgement code, never a bare exit 4. No other code, exit code or wording was touched.Tested environment and results
plugins/unclemoc/factscribe: OK— 0 errors, 0 warnings.npm run checkexits 0 (92/92 checker self-tests).node --test tests/*.test.mjs— 22 cases, all green (16 unit cases for the line-ending module, 3 end-to-end cases that start the vendored Python engine and save into a copy of the sample paper, and 3 new end-to-end cases for the paragraph fingerprint).501dfe617f20e728) and the save path computed the fingerprint of the same paragraph with its CRLF endings (145322cc931203af) → HTTP 409BLOCK_STALE;145322cc931203af, the same value the save path computes, the save returns HTTP 200, and the file on disk is byte-identical to the file before the save (same bytes, same CRLF endings);repaired: 1for the CRLF file and for the mixed file); with the new engine the engine's own write is already faithful and nothing has to be repaired.line1\nline2\n; the newread_text_exactreturnsline1\r\nline2\r\n.doctor,fonts --status,mirror-check,reviewandtideunder cp936: exit 4 to exit 0;audit --jsonunchanged (exit 1 both ways);RuntimeErrorwith no code, now aConfigErrorwithSECTION_PATH_INVALID/PATH_ESCAPE, and zero bytes written outside the project (the directory snapshot is identical before and after);What was not verified
PATHand the engine started, so Windows discovery is exercised for that candidate shape; thepylauncher, the versioned commands and the install-directory candidates are still only covered by unit tests.miniapp/build.shcopies) needs the same change, otherwise the next re-vendor would undo it. That is a separate change in the engine repository and is deliberately not part of this PR.factscribe/workbench/write.py::_draft_insertionuses a hard-coded"\n"), so inserting an AI draft into a CRLF manuscript leaves LF lines in it. That path is not the fingerprint save path this review is about, and it was left untouched here.BLOCK_STALEuntil the page is reloaded. That is the intended meaning of the gate ("this DOM was rendered from this version of the file"), but it is a behaviour change for line-ending-only edits made outside the app.npm run checkwarningsNone — the package reports 0 errors and 0 warnings, so there is nothing to explain.
Access and minimum client version
No new access is declared, and the minimum client version is unchanged at 3.1.2.
plugin.jsonmoves from 1.1.0 to 1.2.0.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.