From c6629f626328f5ca02fd9c69e6197fe3bba6e056 Mon Sep 17 00:00:00 2001 From: martin veillette Date: Sat, 21 Nov 2026 20:09:34 -0500 Subject: [PATCH] fix(challenge): stop leaking a vegas LevelCompletedNode every round vegas LevelCompletedNode (SceneryStack 3.0.0) never disposes its "Level N" DerivedProperty, which stays linked to VegasStrings.label.level; the shared closure context then keeps the whole disposed panel alive. The Challenge view built one per completed round, so the headless soak's retained heap climbed ~0.6 MiB a round (heap-snapshot retainers: StarNode <- ScoreDisplayStars <- vBoxChildren <- derivation closure <- LocalizedStringProperty level). Replace it with a sim-owned RoundCompletedPanel built once and shown or hidden, with the same look and vegas strings, its text derived from the model's level and score. Add a disposal-leak case for the panel and a regression test that toggling roundCompleteProperty adds no listeners to the vegas strings. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RTDmW5KsKz1edFyaGexxua --- src/LewisStructuresColors.ts | 12 +++ src/challenge/view/ChallengeScreenView.ts | 54 +++++----- src/challenge/view/RoundCompletedPanel.ts | 114 ++++++++++++++++++++++ tests/memory-leak.test.ts | 32 +++++- 4 files changed, 181 insertions(+), 31 deletions(-) create mode 100644 src/challenge/view/RoundCompletedPanel.ts diff --git a/src/LewisStructuresColors.ts b/src/LewisStructuresColors.ts index 7efcfc8..6acc028 100644 --- a/src/LewisStructuresColors.ts +++ b/src/LewisStructuresColors.ts @@ -54,6 +54,18 @@ const LewisStructuresColors = { projector: "#f5f5f5", }), + /** End-of-round panel fill (the vegas LevelCompletedNode colour, kept in both profiles). */ + roundCompletedFillColorProperty: new ProfileColorProperty(LewisStructuresNamespace, "roundCompletedFill", { + default: "rgb( 180, 205, 255 )", + projector: "rgb( 180, 205, 255 )", + }), + + /** End-of-round panel border. */ + roundCompletedStrokeColorProperty: new ProfileColorProperty(LewisStructuresNamespace, "roundCompletedStroke", { + default: "black", + projector: "black", + }), + /** * Border/stroke color for control panels and dialogs. * Teal-navy in default mode; medium gray in projector mode. diff --git a/src/challenge/view/ChallengeScreenView.ts b/src/challenge/view/ChallengeScreenView.ts index 8031822..b039e7d 100644 --- a/src/challenge/view/ChallengeScreenView.ts +++ b/src/challenge/view/ChallengeScreenView.ts @@ -6,7 +6,7 @@ import { CHALLENGE_SCREEN_VIEW_SETTINGS } from "../../LewisStructuresConstants.j * bar (level, problem number, score, Start Over). Each problem names a species to draw from scratch * with the palette and board; Hint reveals the next hint, Check grades the drawing, Next moves on. * The electron count stays hidden until the first hint, since counting is part of the task. A - * LevelCompletedNode shows the score at the end of the round. + * RoundCompletedPanel shows the score at the end of the round. * * ┌ status bar ─────────────────────────────────────────────┐ * │ task ┌ hints ─────┐ @@ -23,7 +23,6 @@ import { ScreenView, type ScreenViewOptions } from "scenerystack/sim"; import { Tandem } from "scenerystack/tandem"; import { FiniteStatusBar, - LevelCompletedNode, LevelSelectionButtonGroup, type LevelSelectionButtonGroupItem, ScoreDisplayStars, @@ -62,6 +61,7 @@ import lewisStructuresQueryParameters from "../../preferences/lewisStructuresQue import { type ChallengeModel, HINTS, LEVEL_COUNT, NUMBER_OF_STARS, PERFECT_SCORE } from "../model/ChallengeModel.js"; import { ChallengeScreenSummaryContent } from "./ChallengeScreenSummaryContent.js"; import { hintText } from "./challengeHints.js"; +import { RoundCompletedPanel } from "./RoundCompletedPanel.js"; const STATUS_BAR_HEIGHT: number = CHALLENGE_SCREEN_VIEW_SETTINGS.STATUS_BAR_HEIGHT; const TASK_TOP = STATUS_BAR_HEIGHT + 8; @@ -77,8 +77,6 @@ const LEVEL_ICON_SIZE: number = CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_ICON_SIZE; const LEVEL_ICON_SPECIES = ["water", "carbonDioxide", "sulfurHexafluoride"]; export class ChallengeScreenView extends ScreenView { - private levelCompletedNode: LevelCompletedNode | null = null; - public constructor(model: ChallengeModel, preferences: LewisStructuresPreferencesModel, options?: ScreenViewOptions) { super({ screenSummaryContent: new ChallengeScreenSummaryContent(model), ...options }); @@ -390,31 +388,6 @@ export class ChallengeScreenView extends ScreenView { resetAllButton.visible = level === null; }); - model.roundCompleteProperty.link((complete) => { - this.levelCompletedNode?.dispose(); - this.levelCompletedNode = null; - if (complete) { - this.levelCompletedNode = new LevelCompletedNode( - model.levelNumberProperty.value, - model.scoreProperty.value, - PERFECT_SCORE, - NUMBER_OF_STARS, - false, - 0, - 0, - false, - () => model.leaveLevel(), - { - cornerRadius: PANEL_CORNER_RADIUS, - buttonFont: new PhetFont(CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_COMPLETED_NODE_FONT_SIZE), - center: this.layoutBounds.center, - contentMaxWidth: 400, - }, - ); - this.addChild(this.levelCompletedNode); - } - }); - this.addChild( new Node({ pdomOrder: [ @@ -433,6 +406,29 @@ export class ChallengeScreenView extends ScreenView { ], }), ); + + // One end-of-round panel, shown when the round is complete. Rebuilding vegas' + // LevelCompletedNode each round leaked every panel (see RoundCompletedPanel). + const roundCompletedPanel = new RoundCompletedPanel( + model.levelNumberProperty, + model.scoreProperty, + PERFECT_SCORE, + NUMBER_OF_STARS, + () => model.leaveLevel(), + { + cornerRadius: PANEL_CORNER_RADIUS, + buttonFont: new PhetFont(CHALLENGE_SCREEN_VIEW_SETTINGS.LEVEL_COMPLETED_NODE_FONT_SIZE), + contentMaxWidth: 400, + visible: false, + }, + ); + roundCompletedPanel.localBoundsProperty.link(() => { + roundCompletedPanel.center = this.layoutBounds.center; + }); + model.roundCompleteProperty.link((complete) => { + roundCompletedPanel.visible = complete; + }); + this.addChild(roundCompletedPanel); } } diff --git a/src/challenge/view/RoundCompletedPanel.ts b/src/challenge/view/RoundCompletedPanel.ts new file mode 100644 index 0000000..25b722b --- /dev/null +++ b/src/challenge/view/RoundCompletedPanel.ts @@ -0,0 +1,114 @@ +/** + * RoundCompletedPanel.ts + * + * The end-of-round panel on the Challenge screen: a title that depends on the score, the stars, + * "Level N", "Score: X out of Y" and a Continue button. It looks like vegas' LevelCompletedNode + * and uses the vegas strings, but it is built once and shown or hidden, with its text derived + * from the model's level and score. + * + * Why not LevelCompletedNode: it is built per round, and its "Level N" DerivedProperty is never + * disposed (SceneryStack 3.0.0). That Property stays linked to the vegas level string, and the + * shared closure context keeps every disposed panel's subtree alive, so each completed round + * leaked a whole panel (the headless soak's retained-heap climb). + */ + +import { DerivedStringProperty, type ReadOnlyProperty, type TReadOnlyProperty } from "scenerystack/axon"; +import { optionize } from "scenerystack/phet-core"; +import { StringUtils } from "scenerystack/phetcommon"; +import { Text, VBox } from "scenerystack/scenery"; +import { PhetColorScheme, PhetFont } from "scenerystack/scenery-phet"; +import { Panel, type PanelOptions, TextPushButton } from "scenerystack/sun"; +import { ScoreDisplayStars, VegasStrings } from "scenerystack/vegas"; +import LewisStructuresColors from "../../LewisStructuresColors.js"; + +/** vegas LevelCompletedNode defaults, kept so the panel looks the same. */ +const TITLE_FONT = new PhetFont({ size: 28, weight: "bold" }); +const INFO_FONT = new PhetFont({ size: 22, weight: "bold" }); +const Y_SPACING = 30; +const STAR_DIAMETER = 62; + +type SelfOptions = { + buttonFont?: PhetFont; + contentMaxWidth?: number; +}; + +export type RoundCompletedPanelOptions = SelfOptions & PanelOptions; + +export class RoundCompletedPanel extends Panel { + public constructor( + levelProperty: TReadOnlyProperty, + scoreProperty: ReadOnlyProperty, + perfectScore: number, + numberOfStars: number, + continueFunction: () => void, + providedOptions?: RoundCompletedPanelOptions, + ) { + const options = optionize()( + { + buttonFont: new PhetFont(26), + contentMaxWidth: 400, + fill: LewisStructuresColors.roundCompletedFillColorProperty, + stroke: LewisStructuresColors.roundCompletedStrokeColorProperty, + lineWidth: 2, + cornerRadius: 35, + xMargin: 20, + yMargin: 20, + }, + providedOptions, + ); + const maxWidth = options.contentMaxWidth; + + // Same thresholds as LevelCompletedNode. + const titleStringProperty = new DerivedStringProperty( + [scoreProperty, VegasStrings.excellent, VegasStrings.great, VegasStrings.good, VegasStrings.keepTrying], + (score, excellent, great, good, keepTrying) => { + const proportion = score / perfectScore; + return proportion > 0.95 ? excellent : proportion > 0.75 ? great : proportion >= 0.5 ? good : keepTrying; + }, + ); + const levelStringProperty = new DerivedStringProperty([VegasStrings.label.level, levelProperty], (pattern, level) => + StringUtils.format(pattern, level), + ); + const scoreStringProperty = new DerivedStringProperty( + [VegasStrings.label.score.max, scoreProperty], + (pattern, score) => StringUtils.format(pattern, score, perfectScore), + ); + + const content = new VBox({ + spacing: Y_SPACING, + children: [ + new Text(titleStringProperty, { font: TITLE_FONT, maxWidth }), + new ScoreDisplayStars(scoreProperty, { + numberOfStars, + perfectScore, + starNodeOptions: { starShapeOptions: { innerRadius: STAR_DIAMETER / 4, outerRadius: STAR_DIAMETER / 2 } }, + maxWidth, + }), + new Text(levelStringProperty, { font: INFO_FONT, maxWidth }), + new Text(scoreStringProperty, { font: INFO_FONT, maxWidth }), + new TextPushButton(VegasStrings.continue, { + listener: continueFunction, + font: options.buttonFont, + baseColor: PhetColorScheme.BUTTON_YELLOW, + maxWidth, + }), + ], + }); + + super(content, options); + + this.disposeRoundCompletedPanel = () => { + content.disposeSubtree(); + titleStringProperty.dispose(); + levelStringProperty.dispose(); + scoreStringProperty.dispose(); + }; + } + + private readonly disposeRoundCompletedPanel: () => void; + + public override dispose(): void { + this.disposeRoundCompletedPanel(); + super.dispose(); + } +} diff --git a/tests/memory-leak.test.ts b/tests/memory-leak.test.ts index e606841..14f3716 100644 --- a/tests/memory-leak.test.ts +++ b/tests/memory-leak.test.ts @@ -9,9 +9,15 @@ * tests below using forceGC(). */ -import { Property } from "scenerystack/axon"; +import { NumberProperty, Property } from "scenerystack/axon"; import { Vector2 } from "scenerystack/dot"; import { Node } from "scenerystack/scenery"; +import { Tandem } from "scenerystack/tandem"; +import { VegasStrings } from "scenerystack/vegas"; +import { describe, it } from "vitest"; +import { ChallengeModel } from "../src/challenge/model/ChallengeModel.js"; +import { ChallengeScreenView } from "../src/challenge/view/ChallengeScreenView.js"; +import { RoundCompletedPanel } from "../src/challenge/view/RoundCompletedPanel.js"; import { boardBounds, DrawingModel } from "../src/common/model/DrawingModel.js"; import { generateStructures } from "../src/common/model/generateStructures.js"; import { LewisAtom, LewisStructure } from "../src/common/model/LewisStructure.js"; @@ -29,8 +35,9 @@ import { ShapePreviewNode } from "../src/common/view/ShapePreviewNode.js"; import { ShapeViewNode } from "../src/common/view/ShapeViewNode.js"; import { SpeciesChooserNode } from "../src/common/view/SpeciesChooserNode.js"; import { StructureNode } from "../src/common/view/StructureNode.js"; +import { LewisStructuresPreferencesModel } from "../src/preferences/LewisStructuresPreferencesModel.js"; import { FormCardNode } from "../src/resonance/view/FormCardNode.js"; -import { describeDisposalLeaks } from "./helpers/memoryLeak.js"; +import { describeDisposalLeaks, expectNoListenerGrowth } from "./helpers/memoryLeak.js"; const nitrate = (): StructureData => generateStructures(getSpecies("nitrate").skeleton, OctetPolicy.ACCEPT_BOTH).best[0]?.data ?? @@ -64,6 +71,8 @@ const speciesProperty = new Property(getSpecies("nitrate")); const chargeProperty = new Property(0); const listParent = new Node(); const speaker = new Node(); +const levelNumberProperty = new NumberProperty(2); +const scoreProperty = new NumberProperty(7); describeDisposalLeaks([ { @@ -131,4 +140,23 @@ describeDisposalLeaks([ return new FormCardNode(form, 1, 136, 118, () => undefined); }, }, + { + name: "RoundCompletedPanel", + create: () => new RoundCompletedPanel(levelNumberProperty, scoreProperty, 10, 3, () => undefined), + keepAlive: () => [levelNumberProperty, scoreProperty], + }, ]); + +describe("Challenge round end", () => { + // Regression: the view used to build a vegas LevelCompletedNode per completed round, whose + // undisposed "Level N" DerivedProperty kept each panel linked to the vegas level string. + it("does not link new listeners to the vegas strings each round", async () => { + const preferences = new LewisStructuresPreferencesModel(Tandem.OPT_OUT); + const model = new ChallengeModel(preferences.octetPolicyProperty); + new ChallengeScreenView(model, preferences, { tandem: Tandem.OPT_OUT }); + await expectNoListenerGrowth([VegasStrings.label.level, VegasStrings.label.score.max, model.scoreProperty], () => { + model.roundCompleteProperty.value = true; + model.roundCompleteProperty.value = false; + }); + }); +});