#1697·OpenMAIC

[Bug]: Generated publishState() calls without an observation abort interactive initialization

Author: lianglaibin116-cloudCreated Sep 27, 2026Updated Sep 28, 2026
Labelsbugarea:generation

Bug Description

A generated interactive reveal diagram called publishState() with no argument during its first updateReveal(). With the publication helper supplied by OpenMAIC, this throws Error: Invalid state publication and aborts the remainder of the initialization script. The page still renders, but its interaction setup can be incomplete.

This is a specific generation-contract failure, related to the broader runtime-recovery issue #1622. I did not find an existing issue or PR covering the empty-argument publication / late-wrapper pattern.

Evidence and scope

  • Observed in a customized local deployment based on OpenMAIC v1.1.1, in an existing generated course. This was not a clean-install or fresh official hosted-demo generation test.
  • Checked upstream main at f2875426ae5712d26f0a76e54e9de763e86027a6. The same publication helper is still in interactive-observation.md.
  • A minimal Chromium reproduction using that verbatim upstream helper reproduces the error deterministically, without private course content, model requests, or external dependencies in the HTML.
  • Upstream's classic inline script validator accepts both failing examples below, since their JavaScript is syntactically valid. generateWidgetContent currently uses that syntax preflight.
  • I am not claiming that every model or every new course generates these defects, or that the official hosted demo was tested. The failure is conditional on generated/saved HTML containing the bad call pattern.

Steps to Reproduce

  1. Create a standalone HTML document with a root <main id="experiment"> containing <span id="step">0</span> and <button id="next-btn">Next</button>.
  2. Add the current upstream publication helper below, followed by the reveal script.
  3. Open it in a browser and click Next.

The upstream helper, copied unchanged:

javascript
function publishState(observation) {
  const root = document.getElementById('experiment');
  if (!root) throw new Error('Missing interaction scope');
  try {
    const raw = JSON.stringify(observation);
    if (typeof raw !== 'string' || new TextEncoder().encode(raw).length > 32768)
      throw new Error('Invalid state publication');
    let node = root.querySelector('script[data-maic-observation]');
    if (!node) {
      node = document.createElement('script');
      node.type = 'application/json';
      node.setAttribute('data-maic-observation', '');
      root.appendChild(node);
    }
    node.textContent = raw;
  } catch (error) {
    root.querySelectorAll('script[data-maic-observation]').forEach(node => node.remove());
    throw error;
  }
}

Minimized generated caller pattern:

javascript
let step = 1;

function publishCurrentState() {
  publishState({ summary: 'Step ' + step, state: { step } });
}

function updateReveal() {
  document.getElementById('step').textContent = step;
  publishState(); // no observation argument
}

updateReveal(); // throws before later setup can execute

document.getElementById('next-btn').addEventListener('click', () => {
  step++;
  updateReveal();
});

// A late wrapper was also present in the observed generated HTML.
// It cannot repair the initial failure, and still invokes the bad call.
const original = updateReveal;
updateReveal = function () {
  original();
  publishCurrentState();
};

Expected Behavior

The initial observation should be constructed and published correctly, and initialization / controls should complete. Every state-changing path should call one canonical projection helper with a valid observation.

Actual Behavior

JSON.stringify(undefined) returns undefined; the upstream helper rejects it. The first render throws, subsequent statements in the same script do not run, and a late wrapper cannot recover it. The DOM can look complete despite partial initialization.

In the local classroom this surfaced as:

Uncaught Error: Invalid state publication (about:srcdoc:980)

Related state-initialization failure in another generated game

A second saved scene had this separate, syntactically valid pattern:

javascript
let state = null;
function init() {
  state = { phase: 'planning', gameOver: false };
}
function startGame() {
  document.getElementById('start-screen').hidden = true;
  state.phase = 'planning';
  state.gameOver = false;
  init();
}

Clicking Start throws:

TypeError: Cannot set properties of null (setting 'phase')

Calling init() first fixes this minimal case. Upstream's game start examples still show state setup before initLevel(). Those examples are not themselves a proof of a null-state crash, but do not explicitly distinguish constructing the state object from transitioning already-initialized state. Strengthening that invariant would help avoid this related generated-code failure.

Local Validation

Isolated Chromium tests used the upstream publication helper and the actual syntax validator (verified identical to the upstream file at the SHA above):

Example Upstream syntax preflight Browser result
Empty publishState() during first reveal Accepted Invalid state publication; Next leaves step at 1
Use publishCurrentState() directly; remove late wrapper Accepted Next advances to step 2; no page errors
Write state.phase before init() Accepted Null-state TypeError on Start
Initialize state before field access Accepted Reaches planning phase; no page errors

The two affected saved scenes were also repaired locally in place: the reveal's five steps / previous button, and the game's Start → submit → next quarter flow worked after repair. Updating prompts alone did not change the existing saved HTML.

Suggested Narrow Fix

  1. Explicitly prohibit no-argument publishState() calls in the generation snippet; demonstrate a canonical publishCurrentState() projection used directly from initial render, updates, reset, and programmatic changes.
  2. Avoid late monkey-patching as a substitute for fixing the original update path.
  3. Require state construction before first render / Start / reset accesses its fields; align both game-start examples with that invariant.
  4. Add regressions for these valid-syntax runtime cases. Prompt hardening alone reduces risk but is not a runtime validity guarantee.
  5. Track user-initiated repair of already-saved failing HTML under #1622; prompt updates do not retroactively repair saved courses.

Please preserve the fail-closed observation behavior: do not fabricate a valid observation, silently publish stale values, or drop the size cap just to hide this exception.

Deployment Method

Local development.

OpenMAIC Version

Observed: v1.1.1-based customized deployment. Upstream code checked: f2875426ae5712d26f0a76e54e9de763e86027a6 (main, package.json version 1.1.1).

Affected Area

Classroom generation / Playback.

Browser

Google Chrome on the affected local classroom; isolated Chromium for the deterministic minimal reproductions.

Operating System

macOS; Node.js v24.18.0 for the test harness.

Additional Context

Related: #1622 (broader interactive runtime failure/recovery), #1508 (declared interactive state publication).

This report and minimal reproduction were prepared with Codex assistance. No private course archive, account details, API keys, or configuration are attached.