Skip to content

Reload .autocp when modified externally#170

Open
znzryb wants to merge 2 commits into
Pushpavel:mainfrom
znzryb:watch-autocp-external-edits
Open

Reload .autocp when modified externally#170
znzryb wants to merge 2 commits into
Pushpavel:mainfrom
znzryb:watch-autocp-external-edits

Conversation

@znzryb

@znzryb znzryb commented May 13, 2026

Copy link
Copy Markdown
Contributor

Motivation

.autocp is loaded once via by lazy and only ever written to, so any external edit (a script, another editor, an AI tool) gets silently overwritten by AutoCpStorageSaver on the next saveAllDocuments using stale in-memory state.

Change

Register a BulkFileListener (application level) that calls a new AutoCpStorage.reloadFromDisk() whenever a file named .autocp under any open project's basePath reports a content change. It re-parses through the existing Migrations path and resets the two MutableStateFlows.

The saver also short-circuits with if (document.text == newText) return before setText, which prevents a write→fire→reload loop and avoids spurious modify events.

The IDE-internal warning panel in AutoCpFileEditor is unchanged — direct editing inside the IDE remains blocked via HIDE_DEFAULT_EDITOR.

Failure mode

If the new disk content fails to parse (invalid JSON / schema mismatch), reloadFromDisk catches and log.warns, leaving in-memory state untouched — the next saveAllDocuments will overwrite the broken file with the previous valid state, equivalent to the current "external edit silently lost" behaviour. No data loss compared to the status quo.

znzryb added 2 commits May 13, 2026 17:55
Previously .autocp was loaded once via `by lazy` and only written to,
so any external edit (e.g. by an AI tool) would be silently overwritten
by AutoCpStorageSaver on the next saveAllDocuments using stale in-memory
state.

Register a BulkFileListener that calls AutoCpStorage.reloadFromDisk()
when the file's content changes on disk, re-parsing through the existing
Migrations path and resetting the two MutableStateFlows. The saver also
short-circuits when document text already matches the encoded db,
preventing a write-fire-reload loop and avoiding spurious modify events.

The IDE-internal warning panel (AutoCpFileEditor) is unchanged --
direct edits inside the IDE editor remain blocked.
When an external tool (e.g. an AI script generating problem sets)
writes a fresh .autocp into a project directory that previously had
none, the user's data disappears within seconds: the file shows up
with only a stub SolutionFile entry, all problems gone.

Mechanism: AutoCpStorage's databaseDelegate is `by lazy`, so on first
access (tool window opening, a cpp file getting focus) it sees no
.autocp on disk and initializes to AutoCpDB() default. The external
write then arrives as VFileCreateEvent, but AutoCpExternalReloader
filtered only VFileContentChangeEvent, so reloadFromDisk() never ran
and in-memory stayed at the empty default. Any subsequent
beforeAllDocumentsSaving (triggered by focus changes / doc saves)
serialized that empty default and overwrote the disk content. If the
user clicked an unassociated .cpp in between, ToolFactory's
AssociateFilePanel injected a stub SolutionFile, which is what
ended up on disk in place of the original data.

Widen the listener filter to also accept VFileCreateEvent so the
created file is parsed into memory before any save fires. Add a
staleness guard in AutoCpStorageSaver: if in-memory equals
DEFAULT_AUTO_CP_DB but the disk document is non-blank, reload from
disk instead of overwriting it -- covers the theoretical race where
beforeAllDocumentsSaving lands ahead of the BulkFileListener
dispatch.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../github/pushpavel/autocp/database/AutoCpStorage.kt 0.00% 16 Missing ⚠️
...ushpavel/autocp/database/AutoCpExternalReloader.kt 0.00% 13 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Files with missing lines Coverage Δ
...ushpavel/autocp/database/AutoCpExternalReloader.kt 0.00% <0.00%> (ø)
.../github/pushpavel/autocp/database/AutoCpStorage.kt 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants