Skip to content

[AbsoluteValue] PR2 - state management#3349

Open
handeyeco wants to merge 4 commits intoLEMS-3347/absolute-value-pr1from
LEMS-3347/absolute-value-pr2
Open

[AbsoluteValue] PR2 - state management#3349
handeyeco wants to merge 4 commits intoLEMS-3347/absolute-value-pr1from
LEMS-3347/absolute-value-pr2

Conversation

@handeyeco
Copy link
Contributor

@handeyeco handeyeco commented Mar 13, 2026

Everything is AI

Summary:

This is part of a series of PRs as defined in the first PR:

Initial implementation experiment is Perseus#3304.

Issue: LEMS-3347


Implements the full state pipeline for the absolute-value graph:
initialization, reducer transitions, serialization, and test
infrastructure. Depends on PR 1. No rendering yet — the renderGraphElements
stub from PR 1 remains in place.

Changes:

  • initialize-graph-state.ts — Replaces the PR 1 stub with a real
    initializeGraphState case. Adds getAbsoluteValueCoords(graph, range, step):
    returns graph.coords if set, then graph.startCoords, then normalized
    defaults [[0.5, 0.5], [0.75, 0.75]] (same pattern as sinusoid/quadratic).
    Note: currently unexported — PR 5 exports it for editor use.
  • interactive-graph-action.ts — Adds actions.absoluteValue.movePoint by
    reusing the shared movePoint action factory (same approach as sinusoid).
  • interactive-graph-reducer.ts — Adds a case "absolute-value" in
    doMovePoint that snaps/bounds the destination, then rejects the move if the
    two points would share the same x-coordinate (slope would be undefined).
  • interactive-graph-state.ts — Adds a branch in getGradableGraph() that
    serializes AbsoluteValueGraphState back to PerseusGraphTypeAbsoluteValue
    for scoring.
  • mafs-state-to-interactive-graph.ts — Replaces the PR 1 stub with a real
    case that maps state coords back to the graph type for live interaction
    (e.g. equation display).
  • interactive-graph-question-builder.ts — Adds withAbsoluteValue(options?)
    builder method and AbsoluteValueGraphConfig class so test fixtures can
    construct absolute-value questions.

@handeyeco handeyeco self-assigned this Mar 13, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

🗄️ Schema Change: No Changes ✅

@handeyeco handeyeco changed the title Implement PR 2 [AbsoluteValue] PR1 - state management Mar 13, 2026
@handeyeco handeyeco changed the title [AbsoluteValue] PR1 - state management [AbsoluteValue] PR2 - state management Mar 13, 2026
@github-actions github-actions bot added the schema-change Attached to PRs when we detect Perseus Schema changes in it label Mar 13, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (89adf57) and published it to npm. You
can install it using the tag PR3349.

Example:

pnpm add @khanacademy/perseus@PR3349

If you are working in Khan Academy's frontend, you can run the below command.

./dev/tools/bump_perseus_version.ts -t PR3349

If you are working in Khan Academy's webapp, you can run the below command.

./dev/tools/bump_perseus_version.js -t PR3349

@github-actions github-actions bot removed schema-change Attached to PRs when we detect Perseus Schema changes in it item-splitting-change labels Mar 13, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

🛠️ Item Splitting: No Changes ✅

@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

Size Change: +45 B (+0.01%)

Total Size: 486 kB

Filename Size Change
packages/perseus/dist/es/index.js 187 kB +45 B (+0.02%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 20.8 kB
packages/keypad-context/dist/es/index.js 1 kB
packages/kmath/dist/es/index.js 5.96 kB
packages/math-input/dist/es/index.js 98.5 kB
packages/math-input/dist/es/strings.js 1.61 kB
packages/perseus-core/dist/es/index.item-splitting.js 11.9 kB
packages/perseus-core/dist/es/index.js 25 kB
packages/perseus-editor/dist/es/index.js 100 kB
packages/perseus-linter/dist/es/index.js 8.82 kB
packages/perseus-score/dist/es/index.js 9.26 kB
packages/perseus-utils/dist/es/index.js 403 B
packages/perseus/dist/es/strings.js 7.47 kB
packages/pure-markdown/dist/es/index.js 1.39 kB
packages/simple-markdown/dist/es/index.js 6.71 kB

compressed-size-action

Copy link

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — straightforward, pattern-following implementation of the absolute-value graph type across existing switch statements and builders.

Extended reasoning...

Overview

This PR fills in the absolute-value graph type implementation across 7 files in the interactive-graphs widget. Every change follows the exact same pattern as existing graph types (sinusoid, quadratic, angle, etc.): replacing throw new Error("Not implemented") stubs with actual case handlers that mirror sibling graph types. The new getAbsoluteValueCoords function, AbsoluteValueGraphConfig builder class, doMovePoint reducer case, getGradableGraph handler, and mafsStateToInteractiveGraph case all follow established conventions precisely.

Security risks

None. The changes are entirely internal graph state management — no user input parsing, no network calls, no auth code, no data exposure vectors.

Level of scrutiny

Low scrutiny is appropriate. This is mechanical, pattern-following code in a widget subsystem. Each new code block is structurally identical to existing cases for other graph types (e.g., the sinusoid case). The feature is also behind a feature flag (per PR #3344). The only filed bug is a nit about a missing package-level re-export, which is explicitly deferred to a later PR in this multi-PR series and has no functional impact (the graph initializes correctly via fallback defaults).

Other factors

The PR is part of a planned multi-PR series (LEMS-3347). PR 1 set up the scaffolding and editor support; this PR 2 implements the core graph reducer/state logic. Test data is included. The PR description is sparse but the code changes are self-explanatory given the established patterns.

Comment on lines +375 to +394
export function getAbsoluteValueCoords(
graph: PerseusGraphTypeAbsoluteValue,
range: [x: Interval, y: Interval],
step: [x: number, y: number],
): [Coord, Coord] {
if (graph.coords) {
return graph.coords;
}

if (graph.startCoords) {
return graph.startCoords;
}

const defaultCoords: [Coord, Coord] = [
[0.5, 0.5],
[0.75, 0.75],
];

return normalizePoints(range, step, defaultCoords, true);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Nit: getAbsoluteValueCoords is exported from initialize-graph-state.ts but not re-exported from packages/perseus/src/index.ts, unlike every other coord initialization function (getCircleCoords, getLineCoords, etc.). This means the editor's getDefaultGraphStartCoords in util.ts has no absolute-value case and returns undefined. Since this is clearly a multi-PR effort and the editor integration was set up in PR 1, this is likely planned for a later PR — but worth tracking to ensure the export and editor case are added before the feature ships.

Extended reasoning...

What the bug is

getAbsoluteValueCoords is a new function introduced in this PR, exported from initialize-graph-state.ts. However, it is NOT re-exported from packages/perseus/src/index.ts (line 111-121), where all 9 other coord initialization functions (getCircleCoords, getLineCoords, getLinearSystemCoords, getPointCoords, getPolygonCoords, getSegmentCoords, getSinusoidCoords, getQuadraticCoords, getAngleCoords) are exported. This breaks the established pattern.

Downstream impact

The editor utility getDefaultGraphStartCoords in packages/perseus-editor/src/widgets/interactive-graph-editor/start-coords/util.ts computes default start coordinates for each graph type by calling the corresponding coord function. Without the getAbsoluteValueCoords export, there is no "absolute-value" case in the switch statement, so it falls through to default: return undefined (line 91-92). Meanwhile, shouldShowStartCoordsUI at line 195 returns true for absolute-value, so the start coords editor UI IS shown.

Step-by-step proof

  1. User opens the editor for an absolute-value graph type
  2. shouldShowStartCoordsUI returns true (line 195 of util.ts, added in PR 1)
  3. The start coords UI is displayed with a "Use default start coordinates" button
  4. User clicks the button, which calls getDefaultGraphStartCoords
  5. The switch statement has no "absolute-value" case, falls through to default: return undefined
  6. undefined is set as startCoords on the graph config
  7. During graph initialization, getAbsoluteValueCoords receives graph.startCoords === undefined, skips the startCoords branch, and computes defaults from scratch
  8. The graph still initializes correctly — the bug is silent

Addressing refutations

Multiple verifiers noted this is intentionally deferred work in a multi-PR series. I agree. The git history confirms: shouldShowStartCoordsUI for absolute-value was added in PR 1 (commit aae4026), not this PR. This PR does not modify util.ts at all. The practical impact is negligible since undefined startCoords triggers the correct default fallback in getAbsoluteValueCoords. The feature is also behind a feature flag (PR #3344).

Why this is a nit, not a bug

This is a pattern inconsistency in an incremental implementation, not a functional bug. The graph initializes correctly either way. However, the export should be added to the package index and the editor case should be completed before the feature flag is removed, to ensure the "Use default start coordinates" button returns explicit computed defaults rather than relying on the accidental undefined fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant