Skip to content

Say how NEMAR cuts an analysis plan into steps (Refs #714) - #716

Merged
neuromechanist merged 8 commits into
developfrom
feature/issue-714-plan-step-granularity
Nov 11, 2026
Merged

neuromechanist merged 8 commits into
developfrom
feature/issue-714-plan-step-granularity

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

NEMAR's analysis model proposes plans of different granularity for the same request (read, preprocess and epoch as one step or three; plot folded into compute; an extra inspection step).
This adds a short block to plan_first_instructions in src/assistants/nemar/config.yaml that says how to cut a plan:

  • one step per kind of work, in a fixed order: read the recording; inspect (only when the request depends on something unknown, such as channel or event names); preprocess (ONE step for all filtering, re-referencing and rejection); epoch; compute; plot, leaving out kinds the request does not need;
  • plotting is never folded into computing, preparation is never folded into the next step, one kind of work is never split;
  • titles open with the plain verb of the kind. Each example title was checked with scripts/plan_consistency.py normalize_step: "Read the recording" and "Inspect the event names" are load, "Filter and clean the recording" is filter, "Epoch around the events" is epoch, "Compute the band power" is spectrum, "Plot the result" is plot. RULES and the script are untouched.

The block is a new numbered item before the propose_plan item, so the later items are renumbered (4, 5, 6); their wording is unchanged. Docs (docs/community-widget.md) and CHANGELOG.md each get one entry.

Refs #714

Not measured before merge

The plan consistency check cannot run before merge: it makes paid analysis-model calls against a deployment and needs a model key, which this branch does not have.
It will be run once on the settled develop deployment after merge and reported on this PR and on #714.
Baseline top-sequence shares from #714 on develop: ERP by condition (xx099904) 1/4, band power (xx099906) 2/4, epoch counts 4/4, raw segment 4/4, topography 4/4.

Tests

  • uv run pytest tests/test_agents/test_plan_first.py tests/test_api/test_propose_plan.py tests/test_scripts/test_plan_consistency.py tests/test_core/test_config -q: 710 passed, 1 skipped
  • New TestNemarPlanFirstText cases read the real config.yaml through plan_first_section(config, "analysis", DECLARED); new router-level class in test_propose_plan.py loads the real NEMAR text and checks it reaches the analysis prompt and not the quick one
  • uv run ruff check . and uv run ruff format --check .: clean
  • uv run pytest tests/ -q --cov (ignoring tests/test_core/test_llm_service.py, which fails to collect on the local Python 3.14 with a pydantic.v1 error unrelated to this change): 6145 passed, 92 skipped, 1 failed. The failure is tests/test_tools/test_hed_suggest.py::test_real_program_suggests_a_schema_tag, which needs the local hed-validator node module that is absent in this checkout; it does not touch NEMAR.

… plan-first tests pass, consistency check not run: needs a model key)
…lization (tested)

Short requests keep their own kinds (3 to 6 applies after kinds are chosen), the preprocess title is pinned, inspect means what the nemar_* calls could not answer, titles are examples. Tests run each example title through plan_consistency.normalize_step and check the earlier rules survived.
Constants at the top, a plain fixture instead of autouse, exact-text check only, clearer class name.
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcome for each finding. Fixes are on this branch (3 commits, pushed). uv run pytest on test_plan_first.py, test_propose_plan.py and test_plan_consistency.py: 101 passed; ruff check . clean and the changed files are ruff format clean.

  • Important, 3-to-6 versus "leave out every kind": fixed. The block now says the 3 to 6 of item 4 applies after the kinds are chosen, that a request needing only two kinds (read and plot) gets two steps, and that no kind is split to reach 3. Item 4's sentence is unchanged word for word (propose_plan accepts 1 to 6 steps, so two steps is valid). Phrase test added. The raw-segment request (baseline 4/4) is named as a regression risk for the post-merge report.
  • Important, preprocess title not pinned: fixed. The preprocess bullet now says: whatever it contains, title it "Filter and clean the recording" and put re-referencing and rejection in detail. RULES untouched. Phrase test added.
  • Suggestion, request-specific example titles: fixed. The title bullet now says the quoted titles are examples and each step is named for what it actually does.
  • Suggestion, "Inspect" means two things: fixed. The Inspect bullet says it concerns what the nemar_* calls in item 1 could not answer; names they already returned need no inspect step. Phrase test added. The ERP request (event names) remains the case to watch after merge.
  • Suggestion, title test only checks presence: fixed. The test now parses the quoted titles out of the text and requires them to equal the table, and a parametrized test runs each title through plan_consistency.normalize_step (Read the recording and Inspect the event names to load, Filter and clean the recording to filter, Epoch around the events to epoch, Compute the band power to spectrum, Plot the result to plot).
  • Suggestion, test structure in test_propose_plan.py: fixed. NEMAR_YAML moved to the top with the other constants, the autouse fixture is now a plain fixture that returns the text and is requested by both tests, the redundant flattened CUT_RULE check is gone, and the class is TestThePlanFirstPromptWithNemarText.
  • Suggestion, nothing tests that the old rules survived: fixed. One test asserts the one-fix-retry sentence, "Never run code that is not a step of the approved plan." and "Calling it ends your turn; write nothing after the call."
  • Suggestion, long changelog line and commit subjects: changelog entry updated to match the new rules; its length stays consistent with the neighbouring entries, so not shortened further (intentional). Use a short squash subject on merge, for example "NEMAR plan-first text: cut plans into steps by kind of work (Refs NEMAR plans vary across repeats for ERP and band power requests (E2.5 follow-up) #714)". The post-merge report will also flag xx-prefixed staging ids if they cause extra steps. The three round 1 commits carry no AI attribution.

The consistency check itself still needs a model key and runs against develop after merge.

@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2 outcomes (fixes in 3e3dcd7 and 66b6fb6):

  • Important, never-split rule vs item 4 memory rule: fixed. The bullet now reads "Never split one kind of work across steps, except as item 4 requires to fit the memory budget." Item 4 is unchanged. The phrase test, the changelog entry and the docs/community-widget.md sentence are updated to match, and a new test asserts the exception and that item 4's "split it into steps that read the data in pieces, so each fits" is still there. The "never split a kind of work to reach 3" wording is kept: it concerns padding to the minimum, not memory. A memory split is expected to vary the step sequence for large requests, so the post-merge consistency check should read those runs separately.
  • Suggestion, block length: partly fixed, the rest rejected as intentional. The inspect clarification is folded into the inspect bullet and the fold and split rules share one bullet, which takes item 3 from 21 to 20 lines. It stays over the 10 to 15 line target because each remaining line carries a rule a round 1 finding or an observed inconsistency asked for (title pin, 3-to-6 reconciliation, inspect scope, example titles); cutting further would reopen those findings.
  • Suggestion, repeated test setup: fixed. A flat_nemar_text fixture in TestNemarPlanFirstText returns the whitespace-collapsed plan_first_section(config, "analysis", DECLARED) output, and every new cutting-rule test uses it, so each now checks the text that reaches the prompt path.

Tested: tests/test_agents and tests/test_api/test_propose_plan.py pass (47 in the two plan files), tests/test_frontend/test_embed_snippet_hygiene.py passes, ruff check and ruff format --check pass on src, tests and scripts. tests/test_core/test_llm_service.py fails at collection with a pydantic v1 config error in this environment, a file this PR does not touch.

@neuromechanist
neuromechanist merged commit 89ed2c0 into develop Nov 11, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-714-plan-step-granularity branch November 11, 2026 12:39
@neuromechanist

Copy link
Copy Markdown
Member Author

PM verdict: merged into develop (squash). All acceptance criteria checked against the diff and tests; both review rounds have outcomes; all checks passed (Lint, Test 3.11, Test 3.12, Frontend Tests, Docker build, notebook site). The PR says Refs #714, so #714 stays open until the post-merge consistency report (5 requests x 4 repeats on the settled develop deployment) is posted here and on #714. The block is about 20 lines against the 10 to 15 target, which the round 2 comment justifies.

@neuromechanist

Copy link
Copy Markdown
Member Author

After-merge run of the consistency check (develop 0.8.20.dev34, with #716)

scripts/plan_consistency.py --repeats 4 against develop, rules as merged in #715 (not retuned). 4 of 5 requests pass; band power does not, so #714 stays open.

Request Before #716 After #716
ERP by condition 1/4 3/4
Band power 2/4 2/4
Epoch counts 4/4 3/4
Raw segment plot 4/4 4/4
Topography 4/4 3/4

What still varies:

  • Band power: whether the whole-recording Welch spectrum and the band integration are one step or two, and whether reading the recording is its own step. The chunked Welch recipe (NEMAR prompt: whole-recording Welch spectra by averaging over chunks #669) reads the recording itself, so "read" and "compute" overlap for this request, and the new rule does not say which wins.
  • Epoch counts: one run folded counting into the plot step, against the new "never fold plotting into computing" rule.
  • ERP: "Inspect the channel labels" sometimes comes before "Read the recording" (both normalize to load, so the share is unaffected).

With four repeats a share moves by one run, so 3/4 against 2/4 is within noise for any single request.

Full report

NEMAR plan consistency (E2.5, #663)

Deployment: https://develop-widget.osc.earth/osa; date: 2026-11-11; repeats per request: 4; pass: top sequence in at least 3 of 4 repeats (3 of 4, rounded up).

Request Dataset Sequences (count) Top share Pass
ERP by condition xx099904 load > load > filter > epoch > average > plot (x3)
load > filter > epoch > average > plot (x1)
3/4 pass
Band power xx099906 spectrum > spectrum > plot (x2)
load > spectrum > plot (x1)
spectrum > plot (x1)
2/4 FAIL
Epoch counts xx099904 stats > plot (x3)
plot (x1)
3/4 pass
Raw segment plot xx099905 load > plot (x4) 4/4 pass
Topography xx099905 load > spectrum > plot (x3)
spectrum > plot (x1)
3/4 pass

Caveats

  • The normalization rules were tuned against the first live run and then widened in review for titles audited from it, so shares measured with the table are post hoc, not an independent measurement.
  • A step no rule matches is other, and two other steps compare equal.

Plans, run by run

ERP by condition (xx099904)

Plot the average ERP at Pz for target versus non-target stimuli in dataset xx099904.

Run Outcome Step Operation Decided by Raw title
1 plan 1 load title Read the recording
1 plan 2 load title Inspect the channel labels
1 plan 3 filter title Filter and clean the recording
1 plan 4 epoch title Epoch around the events
1 plan 5 average title Compute the average ERPs
1 plan 6 plot title Plot the ERPs at Pz
2 plan 1 load title Inspect the channel labels
2 plan 2 filter title Read the recording and filter it
2 plan 3 epoch title Epoch around the stimuli
2 plan 4 average title Compute the average ERPs
2 plan 5 plot title Plot Target vs NonTarget ERP at Pz
3 plan 1 load title Read the recording
3 plan 2 load title Inspect the channel labels
3 plan 3 filter title Filter and clean the recording
3 plan 4 epoch title Epoch around the events
3 plan 5 average title Compute the average ERPs at Pz
3 plan 6 plot title Plot the Pz ERP
4 plan 1 load title Inspect the channel labels
4 plan 2 load title Read the recording
4 plan 3 filter title Filter and clean the recording
4 plan 4 epoch title Epoch around the events
4 plan 5 average title Compute the average ERPs at Pz
4 plan 6 plot title Plot the Pz ERP

Band power (xx099906)

Compute the delta, theta, alpha and beta band power for the first recording of dataset xx099906 and plot it as a bar chart.

Run Outcome Step Operation Decided by Raw title
1 plan 1 spectrum title Compute the Welch spectrum of the whole recording
1 plan 2 spectrum title Compute the band power
1 plan 3 plot title Plot the band power as a bar chart
2 plan 1 spectrum title Compute the Welch spectrum of the whole recording
2 plan 2 spectrum title Compute the band power
2 plan 3 plot title Plot the band power as a bar chart
3 plan 1 load title Read the recording
3 plan 2 spectrum title Compute the band power
3 plan 3 plot title Plot the band power as a bar chart
4 plan 1 spectrum title Compute the band power with Welch's method
4 plan 2 plot title Plot the band power as a bar chart

Epoch counts (xx099904)

Analyze dataset xx099904: compute how many epochs there are per event type across all its subjects and plot the counts.

Run Outcome Step Operation Decided by Raw title
1 plan 1 plot title Plot the epoch counts per event type
2 plan 1 stats title Compute the epoch counts per event type
2 plan 2 plot title Plot the epoch counts per event type
3 plan 1 stats title Compute epoch counts per event type
3 plan 2 plot title Plot the epoch counts
4 plan 1 stats title Compute the epoch counts per event type
4 plan 2 plot title Plot the epoch counts

Raw segment plot (xx099905)

Plot a 10 second segment of the raw EEG from the first recording of dataset xx099905.

Run Outcome Step Operation Decided by Raw title
1 plan 1 load title Read the recording window
1 plan 2 plot title Plot the 10 s segment
2 plan 1 load title Read the first 10 s of the recording
2 plan 2 plot title Plot the 10 s segment
3 plan 1 load title Read the 10 s window from the first recording
3 plan 2 plot title Plot the raw EEG segment
4 plan 1 load title Read a 10 s window of the first recording
4 plan 2 plot title Plot the raw EEG window

Topography (xx099905)

Plot the scalp topography of mu band (8 to 12 Hz) power for the first recording of dataset xx099905.

Run Outcome Step Operation Decided by Raw title
1 plan 1 spectrum title Read the recording and compute mu-band power
1 plan 2 plot title Plot the mu-band scalp topography
2 plan 1 load title Read the recording and check channel labels
2 plan 2 spectrum title Compute mu-band power per channel
2 plan 3 plot title Plot the mu-power topography
3 plan 1 load title Read the recording
3 plan 2 spectrum title Compute the mu band power
3 plan 3 plot title Plot the scalp topography
4 plan 1 load title Inspect the channel labels
4 plan 2 spectrum title Compute the mu band power per channel
4 plan 3 plot title Plot the scalp topography

Normalization rules

First match wins, top to bottom; title first, then detail.

Priority Operation Pattern
1 plot plot|\bfigures?\b(?! out)|visuali[sz]|topograph|topomap|scalp map|chart|graph|display|\bdraw
2 export export|\bsave\b|\bcsv\b|write (?:out|to)|store (?:the )?results?
3 stats statistic|t-?test|anova|signific|correlat|compar|summari[sz]|tally|tabulat|\bcounts?\b|number of|(?:epochs?|trials?|events?) per (?:event|condition|subject|type|class)|peak (?:latenc|amplitude)|latenc(?:y|ies)|mean (?:amplitude|value)s? per
4 spectrum spectr|\bpsd\b|welch|\bfft\b|wavelet|time-frequency|band[- ]?power|\bpower\b(?![- ]line)
5 rereference re-?referenc|(?:average|common)[- ]referenc|referenc\w*(?: \w+){0,3} to (?:the )?(?:average|mastoids?)|common average|\bcar\b|linked mastoid
6 average averag|grand mean|\bmean\b|\berps?\b|evoked|contrast
7 reject reject|artifact|artefact|bad[- ](?:channels?|epochs?|segments?|trials?)|drop|exclud|outlier|\bica\b|peak-to-peak|amplitude threshold
8 epoch epoch|trial|baseline|time-locked|event-locked|\bcut\b
9 filter filter|band-?pass|high-?pass|low-?pass|notch|resampl|downsampl|detrend|smooth|power[- ]line|line[- ]noise|mains
10 load \bload|\bread\b|fetch|\bopen\b|import|inspect|retriev|access|check|confirm|\bfind\b|identify|\blist\b|describe|look up
11 other (nothing matched)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant