Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(onenote): skip protected sections in the breaker instead of resetting the streak
Elian Doran22a3c68d269a
diff --git a/apps/server/src/services/import/onenote/importer.spec.ts b/apps/server/src/services/import/onenote/importer.spec.tsindex 45ba63ea41..c080c692a0 100644--- a/apps/server/src/services/import/onenote/importer.spec.ts+++ b/apps/server/src/services/import/onenote/importer.spec.ts@@ -404,6 +404,41 @@ describe("importSelection (real DB)", () => {expect(parent.getChildNotes()).toHaveLength(0);});+ it("still trips the section breaker when a systemic failure run is interleaved with protected sections", async () => {+ // A protected section between systemic failures must not mask the outage: it is skipped, not a+ // streak reset, so the non-protected failures keep accumulating across it and still trip.+ graphMock.isEncryptedSectionError.mockImplementation((e) => e instanceof Error && e.message.includes("20185"));+ graphMock.listPages.mockClear();+ graphMock.listPages.mockImplementation((_token, sectionId) =>+ sectionId === "sec-locked-mid"+ ? Promise.reject(new Error("Microsoft Graph request failed (HTTP 403: 20185: Encrypted sections are not accessible.)"))+ : Promise.reject(new Error("Microsoft Graph request failed (HTTP 401: 40001: Access token has expired.)")));++ const parent = cls.init(() => noteService.createNewNote({+ parentNoteId: "root",+ title: "interleaved parent",+ content: "",+ type: "text",+ mime: "text/html"+ }).note);++ // Three failures, a protected section, then more failures: the streak survives the protected one+ // (1,2,3, skip, 4,5,6) and trips on the sixth non-protected failure — the seventh section listed.+ await cls.init(() => importSelection({+ getAccessToken: () => Promise.resolve("token"),+ parentNoteId: parent.noteId,+ sections: [+ ...Array.from({ length: 3 }, (_, i) => ({ id: `sec-a${i}`, title: `A${i}`, groupPath: [], notebookId: "nb-il", notebookTitle: "IL Notebook" })),+ { id: "sec-locked-mid", title: "Locked Mid", groupPath: [], notebookId: "nb-il", notebookTitle: "IL Notebook" },+ ...Array.from({ length: 4 }, (_, i) => ({ id: `sec-b${i}`, title: `B${i}`, groupPath: [], notebookId: "nb-il", notebookTitle: "IL Notebook" }))+ ],+ taskId: "task-interleaved"+ }));++ expect(graphMock.listPages).toHaveBeenCalledTimes(7);+ expect(parent.getChildNotes()).toHaveLength(0);+ });+it("does not trip the section breaker on a notebook full of password-protected sections", async () => {// Password-protected sections are an expected, healthy outcome (Graph answers with 403/20185), so// even a long run of them must not abort — every one becomes a placeholder.diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.tsindex 7a23109dd6..b9b63d7230 100644--- a/apps/server/src/services/import/onenote/importer.ts+++ b/apps/server/src/services/import/onenote/importer.ts@@ -66,12 +66,13 @@ interface FetchedPage {const MAX_CONSECUTIVE_PAGE_FAILURES = 5;/**- * Aborts the import once this many sections in a row fail to enumerate for a non-protected reason.- * A section whose page list can't be listed normally becomes a placeholder folder, but a streak of- * such failures means a systemic problem (expired token, Graph outage) rather than individual bad- * sections — failing fast beats marking a placeholder-only tree "successful". Password-protected- * sections don't count: Graph answered cleanly (a structured 403/20185), so auth and connectivity- * are healthy and a placeholder is the correct, expected outcome.+ * Aborts the import once this many non-protected sections fail to enumerate without a successful one+ * in between. A section whose page list can't be listed normally becomes a placeholder folder, but a+ * streak of such failures means a systemic problem (expired token, Graph outage) rather than+ * individual bad sections — failing fast beats marking a placeholder-only tree "successful".+ * Password-protected sections are skipped, not counted: a structured 403/20185 is a known per-section+ * condition, so it neither trips the breaker nor resets the streak (so a systemic run interleaved with+ * locked sections still trips). Only a genuine success resets the streak.*/const MAX_CONSECUTIVE_SECTION_FAILURES = 5;@@ -147,13 +148,14 @@ export async function importSelection({ getAccessToken, parentNoteId, sections,getLog().error(`OneNote import: could not list the pages of section '${section.title}' (${section.id}); it will be imported as a placeholder: ${rawMessage}`);sectionPages.push({ section, pages: [], error, passwordProtected });- // Only unexpected failures count toward the circuit breaker: a protected section means- // Graph is healthy (it returned a structured 403/20185), so it doesn't signal a systemic- // problem and mustn't trip the breaker on a notebook full of locked sections.- if (passwordProtected) {- consecutiveSectionFailures = 0;- } else if (++consecutiveSectionFailures > MAX_CONSECUTIVE_SECTION_FAILURES) {- throw new Error(`Aborting the OneNote import: ${consecutiveSectionFailures} sections in a row failed to list their pages, which points to a systemic problem (expired authentication, Graph outage) rather than individual unreadable sections. Last error: ${rawMessage}`);+ // Only unexpected failures count toward the circuit breaker. A password-protected section+ // is a known per-section condition, not a failure signal, so it is skipped entirely — it+ // neither increments the streak nor resets it. Skipping (rather than resetting) is what+ // keeps a systemic run of failures *interleaved* with locked sections tripping the breaker,+ // instead of each protected response masking the outage. Only a genuine success (above)+ // resets, since it proves real pages came back and the pipeline is healthy.+ if (!passwordProtected && ++consecutiveSectionFailures > MAX_CONSECUTIVE_SECTION_FAILURES) {+ throw new Error(`Aborting the OneNote import: ${consecutiveSectionFailures} sections failed to list their pages without a successful one in between, which points to a systemic problem (expired authentication, Graph outage) rather than individual unreadable sections. Last error: ${rawMessage}`);}}}