Skip to content

BDSP AutoStory rebuild 1/6: detectors - #455

Draft
dayfine wants to merge 21 commits into
bdsp/rebuild-basefrom
bdsp/rebuild-detectors
Draft

dayfine wants to merge 21 commits into
bdsp/rebuild-basefrom
bdsp/rebuild-detectors

Conversation

@dayfine

@dayfine dayfine commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Depends on #332 (autostory/engine), through base bdsp/rebuild-base: #332 plus the lab core and tooling at rebuild/src, which has no PR of its own, and #464 (the shared leg trace and replay) merged in. The diff shows only this PR.

The BDSP AutoStory rebuild starts here, with one squashed PR per layer cut from rebuild/src. This one holds the detectors only: the menu, name-list and transition watchers in PokemonBDSP/Inference, plus the AutoStory battle menu reader, intro menu, menu cursor, overworld detectors and the Trainer Card reader that 01.04's badge check uses (from lab #449). It replaces the detector parts of #152 and #148.

Built SerialProgramsTests locally (ccache, -j4). The PokemonBDSP, shared Story and LabChannel suites ran 256 passed and 28 failed, all 28 being the detector image tests that need the CommandLineTests checkout. Code health: 0 failures.

🤖 Generated with Claude Code

https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
dayfine and others added 10 commits November 13, 2026 05:26
Adds Detect/PokemonBDSP_AutoStory_TrainerCardReader from rebuild/src
1da5f30 (lab #449).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
…ation#1910)

* Sockets: add a per-connection id to server socket sends

send() can now be given the connection_id() the caller captured when
it started writing. The id increments on every accept, under the
socket's lock, and send() compares it against the current id at
transport selection and on every loop iteration (queue time and run
time in the Qt backend). A write that outlived its connection - the
peer disconnected and a new client was accepted in the meantime - is
refused instead of being delivered into the new client's socket.

Motivated by the in-app HTTP server: its per-connection task may be
inside a long handler call when the client disconnects, and a fast
reconnect can be accepted before the task notices; without the id,
its final response could be written into the new client's stream.

Default argument (0 = no check) keeps existing callers unchanged.

Test: Sockets - A send for a replaced connection is refused (runs on
the Qt, POSIX, and WinSocket backends).

* Sockets: report connection_id() as 0 when no connection is active

The documented contract ("0 when no connection is active") now holds in
all three backends: the accessor reports 0 whenever the socket is not
CONNECTED. The stored counter stays monotonic - resetting it on close
would let a queued write from the previous connection match the id of
the next one - and send() compares against the counter, so the
stale-send refusal is unaffected. Test_Sockets_StaleSend now also
checks the idle state before and after the first connection.

* Sockets: move the connection_id() monotonicity note to the base class

Suggested by review: the note explaining why the counter is never
reset was duplicated in all three backends; it now lives once in the
abstract class next to the connection_id() contract. Comment-only
change.

---------

Co-authored-by: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com>
…w fixes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
…w fixes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
* FRLG SpriteReader fixes and tests

* allow for mirroring sprites left to right in SpriteDatabase
…d leg trace and replay (#464)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
dayfine and others added 10 commits November 13, 2026 17:06
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
…1879)

* HttpServer: add a minimal Qt-free HTTP/1.1 server on AbstractServerSocket

A minimal HTTP/1.1 server for a single local client that issues one
request at a time (e.g. an MCP client), built on the socket library
instead of Qt:

- One connection at a time (the server socket's model); requests are
  processed in order, so pipelined bytes simply wait in the buffer.
- The per-connection task does all parsing and writing; the socket's
  receive callbacks only append data and flip flags. The user handler
  may block on another thread (e.g. the program thread) and can poll
  HandlerContext::connection_alive() to bail out of long-running work;
  responses are discarded if the connection dies meanwhile.
- Content-Length bodies only (chunked -> 411), sizes capped
  (8 KB request line, 64 KB headers, 1 MiB body, 2 MiB buffer),
  "Expect: 100-continue" answered, strict request/header validation
  (400), responses always carry Content-Length.
- start()/stop() are fully synchronous from the caller's point of view
  once on_listen_finished has fired: stop() closes the connection,
  waits for the in-flight handler, and joins the socket's worker
  threads, so the server can be started again afterwards.

No TLS, no upgrades, no method routing or auth: the user handler owns
all of that. 10 loopback tests (raw ClientSocket clients) cover the
happy path, keep-alive, pipelining, Connection: close, 400/411/413,
100-continue, disconnect and stop() during a handler, and
second-client rejection.

Part of the groundwork for the in-app MCP server; stacks on the
ServerSocket factory alias PR.

* HttpServer: fix CI build failures

- Rename ParseState::ERROR to ParseState::ERROR_RESPONSE: ERROR is a
  macro on Windows (winerror.h), which broke the qualified name on
  MSVC and clang-cl (C2589).
- Keep the AsyncTask handle from ThreadPool::dispatch in the
  stop-during-handler test: dispatch is [[nodiscard]] and clang's
  -Wunused-result (CI's -Werror) rejects discarding it. Its destructor
  also waits for the trivial release task to finish.

* HttpServer: inherit from AbstractHttpServer; address review

Split the request/response types, the handler contract, and the
listener events out into AbstractHttpServer (stacked on the
extract-abstract-http-server PR); HttpServer now implements that
interface, with the types re-exported under its own name.

Fixes from the review:

- Deadlock in on_connect(): the connection task handle was moved into
  m_connection_task while holding m_lock, and destroying the old handle
  blocked on the old task, whose final cleanup takes m_lock. Task
  handles now live in m_tasks under a dedicated m_task_lock (the socket
  library's m_task_lock pattern): on_connect() dispatches outside
  m_lock and never waits on an old task, so a new client is no longer
  held up while the previous handler is still running.
- Deadlock in stop(): the socket was destroyed while holding m_lock,
  and ~ServerSocket joins the socket's worker threads, whose callbacks
  take m_lock. The socket is now moved out under m_lock and destroyed
  after releasing it.
- Busy-spin on partial requests: the wait predicate was
  !buffer.empty(), which is permanently true once a partial request is
  buffered. It now requires the buffer to have grown past what the
  parse just saw, plus the dead/stop flags.
- Chunked requests: 501 Not Implemented instead of 411 (RFC 9112).
- The 413 check at parse time was unreachable (an incomplete request
  is always smaller than the buffer cap); the 2 MiB buffer cap is now
  enforced when data is appended instead, closing the connection.
- end_connection() only closes the socket's connection while it still
  belongs to this connection, so a task noticing its own connection
  died late cannot close the next client's connection.
- Documented that start()/stop() are not thread-safe with each other.

Test: new case that a connection flooding past the buffer cap is
closed without a response while the server keeps listening; chunked
case now expects 501.

* HttpServer: close the reconnect write race from the review

The socket's connection thread sets the state back to LISTENING before
it reports the disconnect, so a fast reconnect can be accepted before
the old connection's on_disconnect callback runs. Two consequences:

- The old connection's task could then write its final response through
  socket->send() into the new client's socket (send() has no
  per-connection identity; it writes whatever the socket is connected
  to). on_connect() now marks the connection it replaces dead (and
  notifies it) before the new one takes over, so the old task's
  post-handler check discards its response.
- on_disconnect() marked m_connection dead, which in that window is the
  new client's connection: it would kill the new connection task. It
  now only does so while the socket is not already CONNECTED to the
  next client.

Test: a client disconnects while its handler is still blocked on a
gate; a second client connects (replacing the first in the server)
before the gate is released. The first handler's late response must be
discarded, and the second client's stream must contain only its own
response.

* HttpServer: address the second review round

Five items from the fresh review:

- [P1] stop() could hang waiting for a handler: the socket suppresses
  its disconnect callback for a close_connection() close, so a handler
  polling connection_alive() kept seeing true while shutdown waited
  for it. stop() now marks the active connection dead before it waits.
  The stop-during-handler test now polls connection_alive() after the
  gate, which would spin forever without the mark.
- [P1] A response could reach a different client after reconnection:
  every write now carries the connection's socket id
  (AbstractServerSocket::connection_id(), new), so the socket refuses
  a write whose connection was replaced. This closes the last of the
  'task outlives the connection' windows; the id check and the
  transport selection happen under the socket's lock.
- [P2] Expect: 100-continue stalled clients: the interim response was
  sent only after the complete request (including body) was parsed,
  which a client waiting for it before sending the body never sees.
  Parsing now reports a separate NEED_BODY state (headers complete and
  valid, body pending), and the 100 is answered at that point. The
  test now performs the real two-phase handshake.
- [P2] Complete headers bypassed the 64 KiB limit: the in-progress
  check only fires while the terminator is still missing. The
  completed header boundary is now checked against the limit too, with
  a test sending a ~75 KiB header section in one piece.
- [P2] The test client's receive buffer was a data race: 'received'
  and the error strings are now protected by the waiter's mutex, and
  assertions use synchronized snapshots (same for the server
  listener's listen error).

* Cleanup

---------

Co-authored-by: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com>
Co-authored-by: Alexander Yee <a-yee@u.northwestern.edu>
The SystemKeyboardDetector screenshots are on the fork's CommandLineTests
main, not upstream's, so the image tests could not load them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
# Conflicts:
#	SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss
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.

4 participants