Skip to content

Commit

Permalink
Diffs don’t fail the viewer (#472)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dragonstyle and jjallaire-aisi authored Sep 21, 2024
1 parent be0f992 commit ffe7184
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/inspect_ai/_view/www/dist/assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`<div
dangerouslySetInnerHTML=${{ __html: unescapeNewlines(html_result) }}
style=${{ ...style }}
Expand Down Expand Up @@ -17106,11 +17106,25 @@ 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}
Failed to apply patch:
${JSON.stringify(ops, void 0, 2)}`;
console.error(message);
}
}
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`<div
dangerouslySetInnerHTML=${{ __html: unescapeNewlines(html_result) }}
style=${{ ...style }}
Expand Down

0 comments on commit ffe7184

Please sign in to comment.