LearnNewsExamplesServices
Frontmatter
id19851
titleA beforeSet hook that throws leaves the value it rejected installed
stateClosed
labels
bugaicore
assigneesneo-opus-ada
createdAtOct 18, 2026, 3:38 AM
updatedAtOct 18, 2026, 5:10 AM
githubUrlhttps://github.com/neomjs/neo/issues/19851
authorneo-opus-ada
commentsCount0
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
contentTrust
projected
quarantined0
signals[]
blockedBy[]
blocking[]
closedAtOct 18, 2026, 5:10 AM

A beforeSet hook that throws leaves the value it rejected installed

neo-opus-ada
neo-opus-ada commented on Oct 18, 2026, 3:38 AM

Context

Found while reviewing neomjs/neo-agent-brain#1086. TextEmbeddingService.configSnapshot needs a setter that validates and throws on a live config. A reactive configSnapshot_ could not host that setter: after the throw, the rejected value is the config's value. Brain therefore carries plain accessors in a shared seam (neomjs/neo-agent-brain#1092), partly for this reason. It was first captured as a defect-note on 2026-10-17.

The Problem

A reactive config whose beforeSet hook rejects by throwing keeps the rejected value. Probe on Neo 13.1.0 in Node:

class Probe extends Neo.core.Base {
    static config = {className: 'Probe.ThrowingBeforeSet', foo_: null}
    beforeSetFoo(value) { if (value === 'bad') throw new TypeError('rejected'); return value }
}
Neo.setupClass(Probe);
const p = Neo.create(Probe);
p.foo = 'good';
try { p.foo = 'bad' } catch {}
p.foo // 'bad'

Any validation-by-throw on a reactive config is unsafe today, so consumers route around reactive configs. That is the opposite of what the class system is for.

The Architectural Reality

  • The generated setter in src/Neo.mjs (autoGenerateGetSet, dev e0704c93bb) works in three steps:
    • Step 2 writes the candidate to the private backing property ("Create a temporary state for beforeSet hooks"), which reaches Config#setRaw.
    • Step 3 reverts it ("Restore state for change detection").
    • The revert runs after the hook returns, and on the undefined cancel path.
  • The surrounding try has only a finally, which resumes the EffectManager. A throw from beforeSet<Key> therefore skips both reverts, and the getter (me[_key] → config.get()) returns the candidate.
  • Design authority, src/core/Base.mjs config-hook JSDoc: "beforeSetMyConfig(newValue, oldValue): Executed before a new value is set. Can be used for validation or transformation. Returning undefined from this hook will cancel the update." The setter's own comments call the step-2 write a temporary state.

The Fix

In the generated setter, when beforeSet<Key> throws, restore the backing property to oldValue and rethrow the same error. No afterSet runs and no subscriber is notified, matching the cancel path except that the caller sees the error.

Acceptance Criteria

  • AC-1 A beforeSet that throws leaves the previous value readable, rethrows the same error, and runs no afterSet.
  • AC-2 The cancel path (undefined) and the transform path are unchanged, and the existing core class-system suites stay green.
  • AC-3 A unit arm in test/playwright/unit/core/ClassSystem.spec.mjs is red on dev and green with the fix.

Out of Scope

  • Transactional set({a, b}), where configs applied before a later throw stay applied. That is a separate question.
  • Brain's plain accessors (neomjs/neo-agent-brain#1092). They stay valid; this fix makes a reactive slot possible again, not required.

Related

neomjs/neo-agent-brain#1086 · neomjs/neo-agent-brain#1092

Live latest-open sweep: checked the latest 20 open neo issues at 2026-10-18T01:37:47Z; no equivalent ("beforeSet throw", "beforeSet validation", "setter restore throw": no hits). A2A in-flight sweep: the last 12 messages, all statuses; no overlapping claim. Memory Core rationale sweep: no prior decision. Own assignments: none on this surface.

Origin Session ID: 71ff10c1-2fda-4f34-ad65-7d53bf3d2ef8 Retrieval Hint: "reactive config beforeSet throws rejected value installed setRaw restore"

tobiu referenced in commit bdaacd9 - "fix(core): a beforeSet that throws leaves the previous value (#19851) (#19852) on Oct 18, 2026, 5:10 AM
tobiu closed this issue on Oct 18, 2026, 5:10 AM