Repository navigation
Make JavaScript SDK state writes reliable #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6ee5fb
sdk/js: wait for browser state writes to commit
ersinkoc 76213d5
sdk/js: isolate temporary files for node state writes
ersinkoc d9d8e3a
sdk/js: retry browser state open after failures
ersinkoc 1960c5b
sdk/js: drop leftover CONTROL OK logs from the state tests
ersinkoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import { test } from "node:test"; | ||
| import { openState } from "./state.browser.ts"; | ||
|
|
||
| for (const outcome of ["commit", "abort", "abort without a reason"] as const) { | ||
| test(`browser state write waits for transaction ${outcome}`, { timeout: 5000 }, async (t) => { | ||
| const opened = { result: { transaction: () => transaction }, onsuccess: () => {} }; | ||
| const stored = { onsuccess: () => {} }; | ||
| let signalPut!: () => void; | ||
| const queued = new Promise<void>((resolve) => { signalPut = resolve; }); | ||
| const failure = new DOMException("commit failed", "QuotaExceededError"); | ||
| const transaction = { | ||
| objectStore: () => ({ | ||
| put: () => { | ||
| signalPut(); | ||
| return stored; | ||
| }, | ||
| }), | ||
| error: outcome === "abort" ? failure : null, | ||
| oncomplete: () => {}, | ||
| onabort: () => {}, | ||
| }; | ||
| Object.defineProperty(globalThis, "indexedDB", { | ||
| configurable: true, | ||
| value: { open: () => opened }, | ||
| }); | ||
| t.after(() => Reflect.deleteProperty(globalThis, "indexedDB")); | ||
|
|
||
| let settled = false; | ||
| const result = openState("test").write("credential", Uint8Array.of(1)).then( | ||
| () => { settled = true; }, | ||
| (error: unknown) => { | ||
| settled = true; | ||
| return error; | ||
| }, | ||
| ); | ||
| opened.onsuccess(); | ||
| await queued; | ||
| stored.onsuccess(); | ||
| await new Promise<void>((resolve) => setImmediate(resolve)); | ||
| assert.equal(settled, false, "write settled before the transaction finished"); | ||
| if (outcome === "commit") { | ||
| transaction.oncomplete(); | ||
| } else { | ||
| transaction.onabort(); | ||
| } | ||
| const error = await result; | ||
| if (outcome === "abort without a reason") { | ||
| assert.match(String(error), /IndexedDB write transaction aborted/); | ||
| } else { | ||
| assert.equal(error, outcome === "abort" ? failure : undefined); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test("browser state retries an unsuccessful database open", async (t) => { | ||
| const failure = new DOMException("database temporarily unavailable", "UnknownError"); | ||
| const data = Uint8Array.of(5); | ||
| const db = { | ||
| transaction: () => ({ | ||
| objectStore: () => ({ | ||
| get: () => { | ||
| const request = { result: data, onsuccess: () => {} }; | ||
| queueMicrotask(() => request.onsuccess()); | ||
| return request; | ||
| }, | ||
| }), | ||
| }), | ||
| }; | ||
| let opens = 0; | ||
| Object.defineProperty(globalThis, "indexedDB", { | ||
| configurable: true, | ||
| value: { | ||
| open: () => { | ||
| const request = { result: db, error: failure, onsuccess: () => {}, onerror: () => {} }; | ||
| opens++; | ||
| const fail = opens === 2; | ||
| queueMicrotask(() => { | ||
| if (fail) { | ||
| request.onerror(); | ||
| } else { | ||
| request.onsuccess(); | ||
| } | ||
| }); | ||
| return request; | ||
| }, | ||
| }, | ||
| }); | ||
| t.after(() => Reflect.deleteProperty(globalThis, "indexedDB")); | ||
|
|
||
| const working = openState("working"); | ||
| assert.deepEqual(await working.read("credential"), data); | ||
| assert.deepEqual(await working.read("credential"), data); | ||
| assert.equal(opens, 1); | ||
|
|
||
| const recovering = openState("recovering"); | ||
| await assert.rejects(recovering.read("credential"), failure); | ||
| const retried = await Promise.allSettled([ | ||
| recovering.read("credential"), | ||
| recovering.read("credential"), | ||
| ]); | ||
| assert.equal(opens, 3, "database open was not retried once"); | ||
| assert.deepEqual(retried, [ | ||
| { status: "fulfilled", value: data }, | ||
| { status: "fulfilled", value: data }, | ||
| ]); | ||
| assert.deepEqual(await recovering.read("credential"), data); | ||
| assert.equal(opens, 3); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import fs from "node:fs/promises"; | ||
| import { syncBuiltinESMExports } from "node:module"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { test } from "node:test"; | ||
| import { openState } from "./state.ts"; | ||
|
|
||
| test("overlapping state writes publish their own bytes", { timeout: 5000 }, async (t) => { | ||
| const dir = await fs.mkdtemp(join(tmpdir(), "agentmesh-state-")); | ||
| t.after(() => fs.rm(dir, { recursive: true, force: true })); | ||
| const state = openState(dir); | ||
| await state.write("credential", Uint8Array.of(0)); | ||
| assert.deepEqual(await state.read("credential"), Uint8Array.of(0)); | ||
| const rename = fs.rename; | ||
| let signalReady!: () => void; | ||
| let allowRename!: () => void; | ||
| const ready = new Promise<void>((resolve) => { signalReady = resolve; }); | ||
| const release = new Promise<void>((resolve) => { allowRename = resolve; }); | ||
| let paused = false; | ||
| const intercepted = t.mock.method(fs, "rename", async (...paths: Parameters<typeof rename>) => { | ||
| if (!paused) { | ||
| paused = true; | ||
| signalReady(); | ||
| await release; | ||
| } | ||
| await rename(...paths); | ||
| }); | ||
| syncBuiltinESMExports(); | ||
| t.after(() => { | ||
| allowRename(); | ||
| intercepted.mock.restore(); | ||
| syncBuiltinESMExports(); | ||
| }); | ||
|
|
||
| const first = state.write("credential", Uint8Array.of(1)); | ||
| await ready; | ||
| try { | ||
| await openState(dir).write("credential", Uint8Array.of(2)); | ||
| assert.deepEqual(await state.read("credential"), Uint8Array.of(2)); | ||
| } finally { | ||
| allowRename(); | ||
| await first; | ||
| } | ||
| assert.deepEqual(await state.read("credential"), Uint8Array.of(1)); | ||
| assert.deepEqual(await fs.readdir(dir), ["credential"]); | ||
| }); | ||
|
|
||
| test("state write can replace an existing value with empty bytes", async (t) => { | ||
| const dir = await fs.mkdtemp(join(tmpdir(), "agentmesh-state-")); | ||
| t.after(() => fs.rm(dir, { recursive: true, force: true })); | ||
| const state = openState(dir); | ||
| await state.write("credential", Uint8Array.of(1)); | ||
| await state.write("credential", new Uint8Array()); | ||
| assert.deepEqual(await state.read("credential"), new Uint8Array()); | ||
| assert.deepEqual(await fs.readdir(dir), ["credential"]); | ||
| }); | ||
|
|
||
| test("state write removes its temporary file if publish fails", async (t) => { | ||
| const dir = await fs.mkdtemp(join(tmpdir(), "agentmesh-state-")); | ||
| t.after(() => fs.rm(dir, { recursive: true, force: true })); | ||
| await fs.mkdir(join(dir, "credential")); | ||
| await assert.rejects(openState(dir).write("credential", Uint8Array.of(1))); | ||
| assert.deepEqual(await fs.readdir(dir), ["credential"]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In IndexedDB, if a request inside a transaction fails, the error event bubbles up to the transaction, then to the database, and finally to the global scope (triggering
window.onerroror unhandled rejection handlers) unlesspreventDefault()is called on the event. To prevent transaction failures from polluting the global error logs or causing test runner failures, it is highly recommended to add anonerrorhandler to the transaction that callsevent.preventDefault().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not take this one: calling
preventDefault()on the request's error event changes what the write reports, not only what gets logged.Per the IndexedDB spec, when a request fails the
errorevent is dispatched with the request, the transaction and the connection in its path; if no listener cancels it, the transaction is aborted. Withtx.onerror = (event) => event.preventDefault(), a failedput(for example aQuotaExceededError) no longer aborts the transaction, so it goes on to commit,oncompletefires, andwrite()resolves as if the record had been stored. That is the silent lost write this change exists to remove.As written, a failed
putaborts the transaction andonabortrejectswrite()withtx.error, which is what the caller needs to see. The error event's path also ends at theIDBDatabase; it is not dispatched towindow, so there is no globalonerrornoise to suppress.