Skip to content

Treat a blank providers.json as empty - #185

Merged
wch merged 3 commits into
mainfrom
fix/blank-providers-json
Nov 12, 2026
Merged

wch merged 3 commits into
mainfrom
fix/blank-providers-json

Conversation

@costrouc

@costrouc costrouc commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #184.

This fixes a race condition that we caught in our emulator tests in Posit Team. This actually occurred quite frequently. Granted our playwright tests are written to go really fast to encourage race conditions.

Problem

The first mutation creates providers.json with an exclusive open, then writes the seed through that descriptor. A process stopped between the two leaves an empty file. From then on:

  • loading reports invalid JSONC, which Posit Assistant shows as an error toast
  • every mutation aborts "until the file is fixed"
  • nothing reseeds the file

Change

  • New isBlankConfigText() in src/node/parse-jsonc.ts: the text is empty or whitespace only. A file holding only comments is still invalid, since a user wrote it.
  • readFileConfig (load and watch): a blank file is the empty layer, as {} is, with no issue.
  • readUserCustomProviderEntries: a blank file gives an empty map.
  • mutateProvidersConfig: a blank file reads as {}, and the mutation seeds it as if this call had created it. The seed is $schema and version, and the whole file is written through atomicWrite. Repairing a blank file reports true, even when the mutation changes nothing.
  • The doc comments on readCurrentConfig and raceSafeEnsureFile say so.

This makes the interrupted creation harmless and repairs files that are already blank, with nothing deleted by hand.

Not changed: creating the file atomically. This change also repairs files that are already blank, which atomic creation alone would not. A follow-up could drop raceSafeEnsureFile and take the lock on the path before the file exists (proper-lockfile with realpath: false), so the first writer goes straight through atomicWrite and no blank file is ever visible.

Testing

New tests:

  • mutate-config.test.ts: an empty file and a whitespace-only file are seeded, and the mutation is applied. A blank file is repaired when the mutation changes nothing.
  • load-config.test.ts: loading a blank file raises no issue and logs no warning, and the file reads as empty.
  • read-user-custom-provider-entries.test.ts: a blank file gives an empty map.

Results:

  • The five new tests fail without the change.
  • vitest run in packages/ai-config passes 800 of 800, after tsc -p . so the CJS tests find dist.
  • oxfmt --check is clean.

The first mutation creates providers.json with an exclusive open and then
writes the seed through that descriptor. A process stopped between the
two, such as an extension host killed by a window reload, leaves an empty
file. Every reader then reported it as invalid JSONC, mutations aborted
until the file was fixed, and nothing ever reseeded it, because the
exclusive open sees the file exists.

An empty or whitespace-only file holds no configuration, so readers now
treat it as missing and the next mutation seeds it as if it had created
it. This also repairs files that are already blank.
@costrouc
costrouc marked this pull request as ready for review November 8, 2026 19:00
Label a blank file as empty rather than missing, since it exists, which is
what a settings report shows. Note that a comments-only file stays
invalid, document that repairing a blank file reports true, and test that
loading a blank file raises no issue or warning and that repairing one
with an unchanged config still writes the seed.
@costrouc costrouc changed the title fix(ai-config): treat a blank providers.json as missing Treat a blank providers.json as empty Nov 9, 2026
@costrouc
costrouc requested review from sharon-wang and wch November 9, 2026 16:16
@sharon-wang
sharon-wang removed their request for review November 11, 2026 15:29
parseProvidersConfig and parseProvidersConfigTolerant now read blank text
as {}, so the loader, the custom-entry reader, and the mutation no longer
each repeat the check. The mutation still checks blank text to decide
whether to seed.

Also: correct the raceSafeEnsureFile comment (readers report a blank file
as empty, and other processes can see it blank during a normal creation),
test that a comment-only file stays invalid, and update the ai-config
memory bank, which still said a zero-byte file is invalid.
@wch
wch merged commit 5e4e96e into main Nov 12, 2026
4 checks passed
@wch
wch deleted the fix/blank-providers-json branch November 12, 2026 19:58
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.

An interrupted first write leaves providers.json empty, and Posit Assistant stays broken

2 participants