From ffe71849b570a6e83ace257cc75964099a66e329 Mon Sep 17 00:00:00 2001 From: Charles Teague Date: Sat, 21 Sep 2024 11:08:40 -0400 Subject: [PATCH] =?UTF-8?q?Diffs=20don=E2=80=99t=20fail=20the=20viewer=20(?= =?UTF-8?q?#472)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make the viewer robust against failures to apply diffs properly. Instead, display an ‘Unable to render diff’ message and log some details in the console. This will prevent state bugs from blowing up the viewer. Co-authored-by: jjallaire-aisi --- src/inspect_ai/_view/www/dist/assets/index.js | 26 ++++++++++++++----- .../samples/transcript/TranscriptState.mjs | 22 ++++++++++++---- .../transcript/state/StateDiffView.mjs | 2 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/inspect_ai/_view/www/dist/assets/index.js b/src/inspect_ai/_view/www/dist/assets/index.js index 9260ca941..f0b1c6808 100644 --- a/src/inspect_ai/_view/www/dist/assets/index.js +++ b/src/inspect_ai/_view/www/dist/assets/index.js @@ -15666,7 +15666,7 @@ function format(delta, left2) { } const StateDiffView = ({ starting, ending, style }) => { const changes = diff(starting, ending); - const html_result = format(changes); + const html_result = format(changes) || "Unable to render differences"; return m$1`
{ * @param {import("../../types/log").Changes} changes - The new state object to update with. */ applyChanges: (changes) => { - state = applyPatch( - structuredClone(state), - structuredClone(changes).map(ensureValidChange), - true - ).newDocument; + try { + state = applyPatch( + structuredClone(state), + structuredClone(changes).map(ensureValidChange), + true + ).newDocument; + } catch (ex) { + const ops = changes.reduce((prev, change) => { + if (!Object.keys(prev).includes(change.op)) { + prev[change.op] = []; + } + prev[change.op].push(change.path); + return prev; + }, {}); + const message = `${ex.name} +Failed to apply patch: +${JSON.stringify(ops, void 0, 2)}`; + console.error(message); + } } }; }; diff --git a/src/inspect_ai/_view/www/src/samples/transcript/TranscriptState.mjs b/src/inspect_ai/_view/www/src/samples/transcript/TranscriptState.mjs index d43032df6..06e45c15f 100644 --- a/src/inspect_ai/_view/www/src/samples/transcript/TranscriptState.mjs +++ b/src/inspect_ai/_view/www/src/samples/transcript/TranscriptState.mjs @@ -34,11 +34,23 @@ export const initStateManager = (scope) => { * @param {import("../../types/log").Changes} changes - The new state object to update with. */ applyChanges: (changes) => { - state = applyPatch( - structuredClone(state), - structuredClone(changes).map(ensureValidChange), - true, - ).newDocument; + try { + state = applyPatch( + structuredClone(state), + structuredClone(changes).map(ensureValidChange), + true, + ).newDocument; + } catch (ex) { + const ops = changes.reduce((prev, change) => { + if (!Object.keys(prev).includes(change.op)) { + prev[change.op] = []; + } + prev[change.op].push(change.path); + return prev; + }, {}); + const message = `${ex.name}\nFailed to apply patch:\n${JSON.stringify(ops, undefined, 2)}`; + console.error(message); + } }, }; }; diff --git a/src/inspect_ai/_view/www/src/samples/transcript/state/StateDiffView.mjs b/src/inspect_ai/_view/www/src/samples/transcript/state/StateDiffView.mjs index 60a8f9d6d..cf204e177 100644 --- a/src/inspect_ai/_view/www/src/samples/transcript/state/StateDiffView.mjs +++ b/src/inspect_ai/_view/www/src/samples/transcript/state/StateDiffView.mjs @@ -16,7 +16,7 @@ import { format } from "jsondiffpatch/formatters/html"; */ export const StateDiffView = ({ starting, ending, style }) => { const changes = diff(starting, ending); - const html_result = format(changes); + const html_result = format(changes) || "Unable to render differences"; return html`