fix(plugins): merge disk writes into a custom editor's unsaved edits (#37343)

* fix(plugins): merge disk writes into a custom editor's unsaved edits

A write to the file on disk while a custom editor tab had unsaved edits
never reached the editor: the buffer kept the edits, Orca showed its
Reload/Overwrite banner, and the session's merge never ran. Now a frame
whose document session has merge announces it (editor.merges), and Orca
sends it the new disk text with the text the edits started from
(editor.document with ancestor). The session merges, posts the result,
and the tab stays unsaved with it; the banner is skipped. Without merge
the banner decides as before.

* test(editor): an explicit save under the changed-on-disk banner keeps the edits

* fix(plugins): make the outside merge exact, and narrow it to where it is

- The host settles a merge only on the session's edit made against the
  merge's own document version, and always commits that result to the
  buffer (even when it equals the disk text); silence past the 1 s flush
  deadline, a remount or a throwing merge leave the banner and the
  suspended autosave in place.
- Orca stamps exactly the disk snapshot the acknowledged merge used and
  records it as the next ancestor; a write that landed meanwhile puts the
  conflict back for its own merge.
- The ancestor is the tab's disk text recorded while clean and advanced on
  acknowledged merges, and must match the tab's disk baseline.
- Only a local file in one merging editor view, with no other tab on its
  buffer, takes this path; anything else gets the banner.
- Frame events keep reaching their handlers after the merge capability.

* fix(plugins): move nothing on a failed disk merge; advance the saved text on success

A merge that throws now leaves the session's document, baseline and
version untouched, so the user's next edit goes out against the old
version: Orca refuses it and keeps the banner instead of taking it as the
merge's acknowledgement. After an acknowledged merge the tab's saved text
moves to the disk text it used, so typing back to the old ancestor no
longer looks clean.

* chore(plugins): regenerate the testing bundle on main
This commit is contained in:
Neil
2026-11-02 06:35:01 +00:00
committed by GitHub
parent d712485fe2
commit cb031c4517
20 changed files with 689 additions and 21 deletions
@@ -32,6 +32,9 @@ export type CustomEditorDocument = {
content: CustomEditorContent
/** Bytes documents: the manifest suffix that matched, e.g. `xlsx`; never a path or file name. */
fileType?: string
/** Sent only to a session that merges: the file changed on disk while the tab held unsaved
* edits. `content` is the new disk text and `ancestor` the disk text those edits started from. */
ancestor?: CustomEditorContent
}
const customEditorEditSchema = z.strictObject({
@@ -27,6 +27,8 @@ export const FRAME_EVENTS = {
editorDocument: 'editor.document',
/** frame → host, once: the editor asked for its document, so `plugin shot` knows it is wired. */
editorSubscribed: 'editor.subscribed',
/** frame → host, once: its document session merges outside changes into unsaved work. */
editorMerges: 'editor.merges',
/** host → frame, payload `PluginPanelPush`: a worker post, an event or a data change. */
push: 'plugin.push'
} as const
@@ -422,4 +422,67 @@ describe('orca.document.session', () => {
expect(replaced).not.toHaveBeenCalled()
expect(posted().at(-1)).toEqual({ orca: 1, kind: 'res', id: 4, result: null })
})
it('merges a disk change made under unsaved edits, and tells Orca it does', async () => {
const merge = vi.fn(
(base: string, outside: string, mine: string) => `${outside}|${mine}|${base}`
)
const { session, fromHost, replaced, edits, posted } = await open({ debounceMs: 0, merge })
expect(posted()).toContainEqual(
expect.objectContaining({ kind: 'evt', event: 'editor.merges' })
)
session.change('a+mine')
await settle()
fromHost(accept(0))
await settle()
// Accepted into the tab's buffer, still unsaved: the agent writes the file on disk.
fromHost({
...doc(2, 'a+agent'),
payload: {
version: 2,
readOnly: false,
content: { kind: 'text', text: 'a+agent' },
ancestor: { kind: 'text', text: 'a' }
}
})
await settle()
expect(merge).toHaveBeenCalledWith('a', 'a+agent', 'a+mine')
expect(replaced).toHaveBeenLastCalledWith('a+agent|a+mine|a')
expect(edits().at(-1)).toMatchObject({
params: { baseVersion: 2, content: { kind: 'text', text: 'a+agent|a+mine|a' } }
})
})
it('never claims to merge without a merge function', async () => {
const { posted } = await open({ debounceMs: 0 })
expect(posted().some((message) => message.event === 'editor.merges')).toBe(false)
})
it('moves nothing when a disk merge throws, so the next edit cannot acknowledge it', async () => {
const merge = vi.fn(() => {
throw new Error('cannot merge')
})
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
try {
const { session, fromHost, edits, replaced } = await open({ debounceMs: 0, merge })
fromHost({
...doc(2, 'a+agent'),
payload: {
version: 2,
readOnly: false,
content: { kind: 'text', text: 'a+agent' },
ancestor: { kind: 'text', text: 'a' }
}
})
await settle()
expect(replaced).not.toHaveBeenCalled()
expect(session.value).toBe('a')
session.change('a+typed')
await settle()
// Still against the document before the merge: Orca refuses it and keeps its banner.
expect(edits().at(-1)).toMatchObject({ params: { baseVersion: 1 } })
} finally {
error.mockRestore()
}
})
})
@@ -168,6 +168,24 @@ export const PLUGIN_FRAME_DOCUMENT_SCRIPT = `
}
var documents = on(E.editorDocument, function (next) {
if (disposed || next.version === doc.version) return
// A disk change under the tab's unsaved edits: merge it with them and send the result.
// Nothing moves unless the merge succeeds, so a later edit never acknowledges a failed one.
if (next.ancestor && merge) {
var merged
try {
merged = merge(decode(next.ancestor), decode(next.content), value)
} catch (error) {
console.error(error)
return
}
rebase(next)
value = merged
dirty = true
generation += 1
emitReplace(value)
if (!inFlight) send().catch(function () {})
return
}
doc = next
// Unaccepted work waits for its edit's conflict, where merge runs.
if (dirty || inFlight) return
@@ -179,6 +197,8 @@ export const PLUGIN_FRAME_DOCUMENT_SCRIPT = `
var flushes = on(C.editor.flush, function () {
return disposed || doc.readOnly ? undefined : flush()
})
// Orca then hands this session disk changes made under unsaved edits, instead of a banner.
if (merge) post({ kind: 'evt', event: E.editorMerges, payload: {} })
return Object.freeze({
get value() { return value },
get readOnly() { return doc.readOnly },