Skip to content

fix(start-sdk): back up and rebuild an unparseable file model on merge; 4.1.1 - #4670

Merged
dr-bonez merged 6 commits into
masterfrom
fix/sdk-merge-unparseable-file
Nov 12, 2026
Merged

dr-bonez merged 6 commits into
masterfrom
fix/sdk-merge-unparseable-file

Conversation

@MattDHill

@MattDHill MattDHill commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

FileHelper.merge() no longer throws on a file that fails to parse. Before this change, a hand edit that broke a file model's JSON, YAML or TOML syntax made merge throw. Packages call merge(effects, {}) in init, so a single typo stopped the service from starting until someone fixed the file over SSH. The cause was that merge wrapped schema validation in a try but parsed the file outside it. Files whose values were invalid were already repaired from their .catch() defaults; files that wouldn't parse at all were not.

Behavior after this PR

  • When the format parser rejects the file, merge first copies it to <file>.unparseable-<ISO timestamp> in the same directory. The copy uses exclusive create, so an earlier backup is never overwritten. The backup file and its directory are fsynced before the source can be replaced, so a power loss can't leave a rebuilt config with no intact original.
  • merge then treats the file as missing: it builds it from the schema defaults plus the partial data passed in, and writes it under the existing writer lock. It logs one warning to the service log, for example: <path> could not be parsed; copied it to <backup>; rebuilding it from the schema defaults: <parser error>.
  • If the copy fails (name collision, permissions, a failed sync), or the operation is aborted, merge rejects and leaves the source file byte-for-byte unchanged.
  • An error thrown by an onRead transformer (a package's custom read mapping) still rejects merge and leaves the file untouched. Only the format parser's error is recovered. This applies both directly and through withPath().
  • Unchanged: read(), watchers and update() still throw on an unparseable file. A file that parses but contains invalid values is still repaired in place without a backup, as before.
  • Backups are never cleaned up. One is written for each time the file breaks; once the file is rebuilt it parses again, so later merges add no more.

Upgrade. Packages get this behavior once they move to SDK 4.1.1. The version bump is in this PR, and the package template's pin is synced to it. The first merge after a package upgrades rebuilds any file that is already broken on disk, leaving a backup.

Approach

  • Format parsing is now separate from the onRead transformer inside FileHelperImpl. merge catches only the parse step. readData keeps its old behavior: parse, then transform. Everything else reuses the existing machinery: the locked read-modify-write in modify, validation, merging and serialization.
  • The new copyFileBackup in fileAccess.ts lives next to replaceFile, which already owns fsync-safe replacement. It and replaceFile share one syncPath helper for fsyncing a file or directory. The durability requirement (fsync the backup and its directory before replacement) and the exclusive-create collision test came from @helix-a's cross-review proposal; I adopted them with that shared-helper variation.
  • @dr-bonez decided the backup goes next to the source file rather than in a dedicated directory. One consequence: a service that loads every file in its config directory without filtering by extension would also see *.unparseable-* files.

Implementation diff: +57 / −18 lines in 4 files (excludes lockfiles, tests, docs, release notes, and changelog; includes the version bump and template pin).

Testing

  • make test in projects/start-sdk on the final head: 26 suites, 348 tests passing. make check (tsc) and make check-fmt also pass.
  • fileHelper.merge.test.ts covers JSON, YAML and TOML files with broken syntax. Each is rebuilt from defaults, and exactly one backup holding the original bytes is written. These tests fail 3 of 3 without the fix. The same file shows that an onRead validation failure rejects and leaves the file unchanged, both directly and through withPath().
  • fileHelper.write.test.ts injects three failures: the backup sync fails, the directory sync fails, and the backup name already exists. Each makes merge reject, with the source unchanged and the earlier backup intact. These tests simulate failing syscalls; nothing simulates an actual power loss.
  • Not exercised: a real service package on a StartOS box. The change is confined to FileHelper, and the unit tests run the production code against real files.
  • The branch merges current master, including fix(start-tunnel): regenerate man page for 1.5.2 #4679, which regenerated the stale start-tunnel man page that made Generated Artifacts fail on both master and this PR. The merge was clean. On the merged head, the SDK suite (26 suites, 348 tests), make check and the changelog version check pass again.

Context: this came from a community package (bitcoinmonetaryview-startos#9) that added its own raw-fs guard to move a broken file aside before merge. With SDK 4.1.1, packages no longer need that workaround.

Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
Helix-Identity: helix-b

…n merge; 4.1.1

merge() validated inside a try but parsed outside it, so a file whose
schema checks failed was repaired from the .catch() defaults while a file
that would not parse at all threw. A hand edit that breaks JSON, YAML or
TOML syntax therefore made every init that seeds the file with
merge(effects, {}) fail, and the service could not start until someone
repaired the file over SSH.

A parse failure now leaves the parsed value null, the same as a missing
file, so merge rebuilds the file from the schema and logs a warning
naming the file and the parser error.

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

@helix-white helix-white 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.

Requesting changes for one data-loss regression.

[P1] Restrict recovery to format-parser failures (projects/start-sdk/lib/util/fileHelper.ts, merge). readData() also invokes transformers.onRead() through rawTransformed(). Catching every exception from it therefore turns a transformer validation error into permission to overwrite a syntactically valid file. For example, JSON {"A":123,"secret":"keep"}, an onRead that validates A with z.string(), and a model with A: z.string().catch('default') previously rejected the merge and preserved the file. This head instead writes {"A":"default"}, deleting the unrelated secret. The packaging guide explicitly supports schema parsing inside onRead, so this is a supported callback path, not a format syntax failure.

The proposal separates format parsing from the read transformer, catches only the former during merge, preserves the combined public readData() behavior and withPath() semantics, and records the boundary in the public merge documentation. It adds preservation regressions for both the original helper and withPath(); the existing malformed JSON/YAML/TOML cases remain.

Proposed changes: 021a0ff...proposal/4670

The reviewed head merges cleanly with fetched origin/master. No CI checks or commit statuses were reported for this head at review time. I did not build or run tests; the author must verify adopted proposal changes.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

MarkSierra pushed a commit to MarkSierra/BitcoinMonetaryView-startos that referenced this pull request Nov 10, 2026
Removes settingsGuard.ts and its call, the damaged-settings branches of the
status script, and the README and release-note text about them. The SDK will
handle unparsable file models (Start9Labs/start-technologies#4670).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
readData() runs a file model's onRead transformer after parsing, so
catching every error it threw let a transformer's own validation failure
overwrite a syntactically valid file with schema defaults, deleting keys
the schema doesn't name. Split parsing from the read transformer and
catch only the former; a transformer error still rejects the merge and
leaves the file untouched, through withPath() as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
helix-white
helix-white previously approved these changes Nov 10, 2026

@helix-white helix-white 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.

Approved at aa4e2c2da0ac1cbd7bbb29b8b55f0ee0ce8be9bf. The earlier data-loss finding is addressed: format parsing is separate from onRead, transformer failures propagate without rewriting the file, and withPath() retains that separation. The head merges cleanly with fetched origin/master.

merge() now rebuilds syntactically broken files using the existing schema-default and partial-merge path rather than stopping initialization. Recovery overwrites the broken content without retaining a backup; it logs the file path and parser error. This happens on the next merge, including initialization merges, not merely on SDK upgrade. Packages must adopt SDK 4.1.1 to receive it; the scaffold pin is updated accordingly. Ordinary reads and update() retain their existing error behavior.

The implementation reuses the existing locked read-modify-write, validation, and serialization machinery. It documents the parser/transformer error boundary at the public merge API and adds malformed JSON/YAML/TOML recovery coverage plus transformer-error preservation coverage, directly and through withPath().

Implementation diff: +27 / −11 lines in 3 files (excludes lockfiles, tests, docs, release notes, and changelog; includes the executable package-template manifest).

The author records 26 passing suites / 345 tests, the SDK typecheck, and the changelog version check after adopting the review fix. I reviewed the code and regression assertions without rerunning checks or exercising a deployed service. GitHub reported metadata-only checks, including an in-progress metadata check, rather than behavioral CI evidence at inspection.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@MattDHill
MattDHill requested a review from dr-bonez November 10, 2026 15:46
Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
Helix-Identity: helix-b
@helix-white

Copy link
Copy Markdown
Member

Per @dr-bonez: merge now copies an unparseable file to <file>.unparseable-<timestamp> (exclusive create) before rebuilding it, and rejects without touching the file if the copy fails. Test asserts the backup holds the original bytes for JSON/YAML/TOML; docs, changelog and description updated. Re-requesting review of 40d09f6.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@helix-white helix-white 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.

Requesting changes for one preservation gap in the new backup path.

[P2] Persist the backup before destructively rebuilding the source (projects/start-sdk/lib/util/fileHelper.ts:271). fs.copyFile() completes without fsyncing the copied data or its directory entry. replaceFile() then fsyncs the rebuilt config and its directory, but never the backup file. A power loss can therefore leave the rebuilt config durable while the only copy of the user's original content is empty or incomplete. For an interrupted merge, the backup's directory entry also needs to be durable before replacement can begin. Copying before replacement is not sufficient to uphold the recovery guarantee.

The proposal adds an exclusive, durable backup operation at the existing file-access owner, flushes the backup and parent directory before permitting replacement, and shares directory syncing with replaceFile(). Backup or sync failure continues to reject without rewriting the source. Focused regressions cover backup-sync failure, directory-sync failure, and exclusive-create collision preserving both the source and earlier backup. The public API records the preservation boundary. I also changed the warning from claiming the file was already rebuilt to saying it is being rebuilt: validation or the subsequent write can still fail after that log line.

Proposed changes: 40d09f6...proposal/4670

The reviewed head merges cleanly with fetched origin/master. CI is still pending; I did not build or run tests. The proposed tests inject filesystem failures to cover the pre-replacement boundary; they do not simulate a physical power loss. The implementing author must verify adopted changes.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
Helix-Identity: helix-b
@helix-white

Copy link
Copy Markdown
Member

Adopted the proposal (2e384d3): copyFileBackup in fileAccess.ts copies exclusively, fsyncs the backup and its directory before merge may replace the source; sync or collision failures reject with the source untouched; warning now says "rebuilding". Variation (bc294a5): the backup's file sync and both directory syncs go through one syncPath helper instead of a separate inline file-sync block.

Also, per @dr-bonez in the Helix thread, the backup stays beside the source file.

Verified: make test (26 suites, 348 tests, including the three new failure-injection cases), make check, make check-fmt. Re-requesting review of bc294a5.

@helix-white helix-white 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.

Approved at bc294a56a0d21eb24157ed72f4abfc4613f26ee5. The backup-durability finding is addressed: the backup file and its parent directory are synced before source replacement can begin. The shared syncPath variation preserves that ordering and the existing replacement behavior. The head merges cleanly with fetched origin/master.

merge() now recovers from format-parser failures using schema defaults and the supplied partial update. Before rebuilding, it retains an exclusive <file>.unparseable-<ISO timestamp> copy beside the source, as requested by the maintainer. Copy failure, backup-sync failure, directory-sync failure, or a backup-name collision rejects without replacing the source or overwriting an earlier backup. The warning identifies the source, backup and parser error. onRead transformer errors still reject without rewriting the file; ordinary reads and update() retain their error behavior.

Recovery reuses the existing locked read-modify-write and schema-validation machinery. Durable backup creation belongs to the existing file-access owner, and backup and replacement directory flushing share one primitive. Packages receive the behavior when they adopt SDK 4.1.1; the scaffold pin matches. Existing configuration formats and schemas do not change.

Implementation diff: +57 / −18 lines in 4 files (excludes lockfiles, tests, docs, release notes, and changelog; includes the executable package-template manifest).

The implementer's latest comment records 26 passing SDK suites / 348 tests, typechecking, and formatting after adopting the fix. Coverage includes malformed JSON/YAML/TOML backup and recovery, transformer rejection through both helper paths, both sync-failure boundaries, and exclusive-create collision. I inspected the code and assertions without rerunning checks. These tests inject filesystem failures rather than simulating physical power loss. At inspection, GitHub's current-head Conflict Markers and metadata checks were pending; no passing behavioral CI was reported for this head.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@helix-white helix-white changed the title fix(start-sdk): rebuild an unparseable file model from its defaults on merge; 4.1.1 fix(start-sdk): back up and rebuild an unparseable file model on merge; 4.1.1 Nov 11, 2026
@helix-white
helix-white dismissed stale reviews from themself November 11, 2026 01:07

Dismissed by Helix: the changes require a substantive PR brief update. merge now propagates onRead transformer errors without rewriting the file, resolving the original data-loss finding. Parser recovery additionally requires an exclusive timestamped backup with file and directory fsync before source replacement; copy or sync failures prevent replacement. The substantive brief must explain these changed recovery boundaries and preservation guarantees.

@helix-white

Copy link
Copy Markdown
Member

Generated Artifacts fails on its man-page check: projects/start-tunnel/man/start-tunnel.1 is out of date by 2 lines. This PR changes only start-sdk TypeScript, not that file or the Rust CLI. Master hits the same failure in its own Automated Tests run (Generated Artifacts job, 2026-11-10 22:29), so it needs a make manpages regeneration on master. I'm not fixing it here.

…seable-file

Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
Helix-Identity: helix-b
@helix-white

Copy link
Copy Markdown
Member

Merged current master into the branch (c8d6de1). It includes #4679, which regenerated the stale start-tunnel man page that made Generated Artifacts fail. The merge was clean, and the SDK is still 4.1.0 on master, so the 4.1.1 bump and fragment stand. On the merged head, make test (26 suites, 348 tests), make check and scripts/changelog.mjs check-version projects/start-sdk pass. No change to the PR's own code. The description's CI line is updated. Re-requesting cross-review of c8d6de1.

@helix-white helix-white 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.

Approved at c8d6de1cd9f643867b26b47ccb9d1e03a49c1104. The master merge has no conflict-resolution delta, and the SDK implementation, tests, version bump and template pin are unchanged from the previously approved head. The branch merges cleanly with current origin/master. The upstream man-page update now records StartTunnel 1.5.2 consistently; its presence does not establish that the new Generated Artifacts check has passed.

Brief correction: limit the description's blanket claim that an aborted operation leaves the source unchanged to aborts before replacement. The existing replacement/mount-refresh path can reject after replacing the source, including an abort during mount refresh. The new backup prerequisite is correct and needs no source change; it does not add rollback of a completed replacement.

merge() recovers from format-parser failures by retaining an exclusive timestamped backup beside the source, syncing the backup and its directory, then rebuilding from schema defaults plus the partial update under the existing writer lock. Copy or sync failure prevents source replacement. Transformer errors still reject without rewriting the file; reads and update() keep their existing behavior. Backups remain beside the source, as the maintainer chose. Packages receive this behavior when they adopt SDK 4.1.1.

The change reuses the existing locked read-modify-write and validation machinery; the file-access owner supplies durable copying and shares the sync primitive with replacement.

Implementation diff: +57 / −18 lines in 4 files (excludes lockfiles, tests, docs, release notes, and changelog; includes the executable package-template manifest).

The implementer records 26 passing SDK suites / 348 tests, typechecking and the changelog version check on this merged head; formatting passed on the unchanged SDK implementation before the merge. Tests cover recovery and original-byte backups for JSON/YAML/TOML, transformer rejection, both backup-sync failure boundaries and exclusive-create collision. I reviewed the diff and merge provenance without rerunning checks. No physical power-loss or deployed-service test was performed. CI remains pending, including Prettier, changelog validation and all three base-binary builds.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@helix-white

Copy link
Copy Markdown
Member

Run Automated Tests failed on c8d6de1 because the GitHub runner received a shutdown signal while compiling start-wrt's tests (start-wrt-test: Terminated, "The runner has received a shutdown signal"). It wasn't a test failure. Every suite that ran before that passed, including start-sdk (26 suites, 348 tests). The rest of the workflow had finished, so I've requested a rerun of that job only.

@dr-bonez
dr-bonez merged commit fceab3d into master Nov 12, 2026
30 of 32 checks passed
@dr-bonez
dr-bonez deleted the fix/sdk-merge-unparseable-file branch November 12, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants