Files
hapi/e2e
f6d7238607 fix(shared): hide and report near-miss AGENT_NOTIFY_SUMMARY footers (#2481)
* fix(shared): hide and report near-miss AGENT_NOTIFY_SUMMARY footers

A turn whose final line attempts the machine footer but does not parse -
most often truncated before the closing brace - fails twice over: the
status is dropped, and because the strict matcher found nothing to strip,
the raw `AGENT_NOTIFY_SUMMARY {...` line is rendered to the reader. The
second half contradicts the product intent `e2e/notify-summary.spec.ts`
already asserts, and the first half is silent: `extractNotifySummary`
returns null with no log, so the only detector is a human noticing.

Introduce the near-miss notion: the last non-empty line is positionally
eligible to be a footer (the existing glued/indented rules, unchanged) but
yields no summary. On that condition:

- `stripNotifySummaryFooter` removes it, keeping any glued prose prefix
  exactly as it does for a valid footer. The machine line is addressed to
  the harness, not the reader, and a truncated one is no more readable
  than a valid one. `NotifySummaryText` now falls back to the stripped
  body instead of re-rendering raw text when there is no summary to show.
- `detectNotifySummaryNearMiss` classifies why it failed - unterminated
  JSON, trailing content, invalid JSON, not an object - and the work-graph
  ingest logs one warn naming the reason, so a broken emitter is greppable
  instead of invisible. Mid-stream snapshots are excluded: a footer still
  being written is incomplete by construction, not malformed. The log
  carries the payload *length*, not the payload, to keep agent output out
  of hub logs.

What is accepted as a *valid* footer is unchanged, and so is what is left
alone: whitespace-delimited prose mentions (`Example: AGENT_NOTIFY_SUMMARY
{...`), mid-body quotes, non-final footers, and a bare token with no
payload all still render as written.

Deliberately not included: repairing a truncated payload by appending the
missing brace. Truncation cuts content, not only syntax, so a repaired
footer would write a silently shortened summary into the append-only
work-graph ledger as though the agent had meant it. The near-miss reason
makes repair a small follow-up if a maintainer wants it.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* fix(shared): keep quoted footer examples out of near-miss matching

Review catch: the near-miss path inherited the strict matcher's "glued"
eligibility, which treats any non-whitespace character before the token as
glue - including a backtick or quote. A final line such as

    Use `AGENT_NOTIFY_SUMMARY {"status":"done"}` at the end.

therefore matched, the closing backtick read as trailing content, and
stripping left only `Use ` + the opening backtick, deleting the example and
the explanation after it. The Codex commentary path strips before storage
rather than before display, so that text was lost, not merely hidden.

Near-miss matching now skips occurrences inside an inline-code span and
occurrences immediately preceded by a quoting or opening delimiter. The
strict matcher is deliberately left as it was: a payload that parses as an
object through end of line is its own evidence that the line is a real
footer, so a backtick-glued valid footer is almost certainly an agent that
forgot the newline. A near miss has no such evidence, so it has to be more
conservative about what it will delete.

Genuine glued footers are unaffected, including after an earlier inline-code
span on the same line (``Ran `bun test`.AGENT_NOTIFY_SUMMARY {...``), which
is covered by its own regression test alongside the preserved examples.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* fix(shared): treat near-miss quoting as a span, not one character

Second review catch, same class as the first: the guard added in 3fbda1e7e
inspected the single character before the token, but quoting is a property
of the *span*. A final line such as

    The agent wrote "Done.AGENT_NOTIFY_SUMMARY {"status":"done"}" verbatim.

has ordinary prose punctuation immediately before the token, and the
backtick-parity check only counted backticks, so a double-quoted span read
as unquoted. The line was stripped to `The agent wrote "Done.`, and the same
variant existed for every quote style the local check did not enumerate -
`('Done.AGENT_NOTIFY_SUMMARY {...}')` among them.

Replace both checks with one span test: is the token inside an open quoting
or code span on this line? Symmetric delimiters (backtick, double quote,
single quote) are counted by parity, so a *closed* pair earlier on the line
(`He said "hello".AGENT_NOTIFY_SUMMARY {...`) does not grant immunity;
bracketing delimiters are counted by depth. An apostrophe inside a word is
not treated as a quote, or `didn't` would hide a real footer.

Angle brackets are deliberately absent: `a < b` is far more common in agent
prose than an angle-bracketed footer example, and treating `<` as an opener
would hide real footers. That narrows the previous commit's delimiter set by
one character, by choice.

The strict matcher remains untouched, for the same reason as before: a
payload that parses as an object through end of line is its own evidence of
a real footer.

Regression coverage for the quoted-glue variants, plus three over-correction
guards that pass on both sides of the change - a closed inline-code span, a
closed quote or bracket pair, and an in-word apostrophe must all still strip
a genuine glued footer. The Codex commentary path gets the same cases, since
that path strips before storage rather than before display.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* fix(shared): strip a near miss only when its payload runs to end of line

Three review rounds found three quoting constructs the near-miss guard did
not know about - a single-backtick span, a double-quoted span with glued
prose inside it, a double-backtick run - and each fix enumerated one more
piece of syntax. That was not converging, and the reason is visible in the
classifier: *every* reported false positive is `trailing-content`.

    The agent wrote "Done.AGENT_NOTIFY_SUMMARY {…}" verbatim.   → trailing-content
    ('Done.AGENT_NOTIFY_SUMMARY {…}')                           → trailing-content
    Use `AGENT_NOTIFY_SUMMARY {…}` at the end.                  → trailing-content
    Use ``Done.AGENT_NOTIFY_SUMMARY {…}`` at the end.           → trailing-content
    See [Done.AGENT_NOTIFY_SUMMARY {…}] above.                  → trailing-content

while every shape this feature exists to catch is not:

    …,"summary":"ok     → unterminated-json      {bogus}    → invalid-json
    {"ver               → unterminated-json      not-json   → not-an-object

So stop enumerating quoting syntax and gate on the payload region instead:
strip a near miss only when it runs to the end of the line. Truncation
terminates the line by definition, so it always qualifies; a quoted example
always closes its delimiter *after* the payload, so it never does. One rule
replaces the whole guard - no quote characters, no backtick runs, no in-word
apostrophe case - and `shared/src/messages.ts` is ~40 lines shorter for it.

`trailing-content` stays classified and logged, so a genuinely stray-text
emitter is still greppable; it simply is not deleted. The cost is one odd
line on screen when an emitter really does append text after a complete
object - and the status was dropped for that shape anyway, since the strict
matcher rejects it too, so the change is purely cosmetic. The alternative is
deleting a reader's example and the prose around it, which the Codex
commentary path does before storage rather than before display. Showing a
strange line beats destroying text.

Every preservation test from the previous two rounds still passes, now for
this one reason rather than per-syntax guards, and the Codex commentary
suite keeps the quoted and double-backtick cases the review asked for.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* fix(shared): only delete a near miss when no prose shares its line

Two review points, both about not destroying text.

**The log wording was wrong for the one reason that is no longer stripped.**
`[work-graph] ignored a malformed AGENT_NOTIFY_SUMMARY footer` is untrue of a
`trailing-content` line: a quoted example is correct prose, and it is kept and
shown rather than ignored. Someone grepping that string for lost statuses
would read an example as a dropped one. The message is now
`no AGENT_NOTIFY_SUMMARY status recorded from the final line`, which holds for
every reason, and `removedFromDisplay` carries which of the two outcomes
happened. The reason is still logged - an emitter that appends stray text
after a complete object is genuine signal, and suppressing it to quieten the
noise would trade the wrong way.

**A quoted example of an *incomplete* payload was still being deleted.**
``Use `AGENT_NOTIFY_SUMMARY {"summary":` to reproduce.`` classifies as
`unterminated-json`, so the end-of-line rule could not protect it: an
unterminated payload has no object end to measure against. This was a known
residual of that rule and should have been on the record when it landed.

The fix is a second structural condition rather than a return to parsing
quote syntax: **the token must own the line.** Nothing but whitespace may
precede it. That covers every quoting construct at once, including the ones
earlier rounds enumerated and the ones nobody has thought of, because what it
tests is the presence of prose rather than the shape of quotes.

A *valid* footer still tolerates glued prose - a payload that parses as an
object through end of line is its own evidence that the line is a real footer.
A near miss has no evidence, so it must be sure the line is machine output
before deleting it.

The cost, stated plainly: a truncated footer glued to prose on the same line
now stays visible. That is a real sub-case of the defect this PR fixes, and it
is given up deliberately. The status is lost either way - the strict matcher
rejects that shape too - so the cost is one odd line on screen, against the
risk of deleting a reader's example and the prose around it, which the Codex
commentary path does before storage rather than before display.

The four syntax guards from the previous rounds are replaced by this one rule
and their tests collapse into it, alongside a contrast test that a valid glued
footer is still stripped.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* test(shared): pin why line ownership, not payload shape, decides the strip

A payload-shape predicate - "the remainder must be a well-formed JSON prefix
that runs to end of line" - looks like it should separate a truncated footer
from a quoted example, and it does for most shapes. It fails on an open
string, because an unterminated string swallows everything after it:

    Use `AGENT_NOTIFY_SUMMARY {"summary":"` to reproduce the failure.

the payload of which is byte-for-byte the shape of a real truncation
(`"summary":"ok`). The signature of the defect and the signature of the
false positive are the same, so no test of the payload can tell them apart -
which is why the decision is made from the line instead.

Pins that case so the next person to reach for a payload-shape rule finds the
counterexample already failing.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

---------

Co-authored-by: HAPI <noreply@hapi.run>
2026-11-18 08:08:40 +08:00
..