Skip to content

fix: tasks, event streams and child processes (review P1-2) - #201

Merged
Derrors merged 2 commits into
mainfrom
fix/p1-tasks-streams
Nov 25, 2026
Merged

Derrors merged 2 commits into
mainfrom
fix/p1-tasks-streams

Conversation

@Derrors

@Derrors Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What and why

P1, second batch, from the full review of main after #195: tasks, event streams and child processes.

  • Takeover while a step waits for the user (Task::control): waiting refused takeover, so the user had to answer before looking or changing anything. Now:

    • takeover is allowed while a step waits for an answer, and the question stays;
    • it can be answered under takeover, and the task stays with the user until handback;
    • a handback with the question still open goes back to waiting.

    The page offers "Take over" while waiting and "Review" while in control with a question open. The demo mode follows the same rules.

  • Nothing after done: events kept coming after a task ended (a waiting question woken up, a request finishing). Now:

    • a step under way is recorded as stopped before done, and emit drops anything later;
    • a subscriber that Lagged could miss done, and the broadcast never closes, so the connection stayed open for good; task_stream now catches up from the task's log and subscribes again.
  • The page's task stream (tasks.js):

    • a stream the browser closed for good (404/401) left the task "running"; now the page asks for the task list and follows the task again if it is still there, or marks it stopped (5 tries when the list can't be read);
    • each reconnection replays the task and redrew every gesture mark; marks are now drawn once per step (not those known before a reload);
    • an unknown state no longer replaces a known one, and taskStatus gives one it doesn't know a plain label.
  • Process groups:

    • procgroup::stop sent SIGKILL blindly 3 s after SIGTERM, possibly to a reused group number; now it looks every 100 ms and leaves a group that is gone alone;
    • task run exited right after its command, taking the delayed SIGKILL with it, so what ignored SIGTERM stayed; it now stop_and_waits any group members left;
    • doctor's and ffmpeg's timed commands now kill_on_drop.
  • Streams:

    • the MJPEG relay ends when the daemon no longer controls its phone (controlled_changes_from);
    • Viewer::drop uses spawn_local: its task outlived the LocalSet and could close HID again after shutdown;
    • HEVC to H.264: a Closed ffmpeg input counts as a failure (it restarted for ever). Repeated failures without frames move to the next encoder, as MJPEG does. Not when ffmpeg only stands in for the built-in encoder: its failure isn't that encoder's.
  • task run --url: stop() times out after 10 s; an event stream ends after 60 s of silence (the viewer pings every 15 s) or an event over 1 MB.

  • One daemon per socket:

    • ctl.lock beside the socket is held with flock; a second daemon waits up to 15 s for one that is stopping, and bails at once if one answers;
    • the socket is bound under umask 077, so it is never open to others before the chmod;
    • a socket directory it creates is 0700.

Tests

1. Unit tests

  • cargo test --workspace --all-features --locked: 584 passed; clippy -D warnings; cargo fmt --check for all 11 packages; node --test tests/viewer/*.test.mjs: 40 passed; tests/check_regression_cases.py: 0 missing
  • New:
    • Rust: the_user_can_take_over_while_a_step_waits_for_them, nothing_follows_done, a_follower_that_fell_behind_catches_up_and_ends (fails with the old Lagged arm), a_switch_to_another_device_ends_the_stream, a_group_is_seen_until_it_is_gone, an_event_without_end_ends_the_stream, only_one_daemon_holds_a_socket;
    • page: a step waiting for the user can be answered after taking over, a step mark is drawn once however often the stream replays it, an unknown task state is shown as it is and does not replace a known one, a stream the browser gave up on is followed again if the task is still there, else marked stopped
  • No test removed, ignored or loosened (summary.test.mjs's harness gets taskStatus)

2. Real-device regression

  • iPhone 15 Pro Max (iPhone16,2) iOS 27.2.1, native; iPad mini 5 (iPad11,1) iPadOS 26.7.3, WDA; Debian 12
  • tests/device/run.py on the iPhone: 16 calls, 0 failed

3. Regression cases

4. Real-device proof

  • A task's task_confirm waiting; through the viewer's API: takeover, a tap by the user, handback, back to waiting; takeover again, approve, and the task's request got "approved" while the state stayed takeover; handback, running
  • task run -- sh -c 'trap "" TERM; sleep 301 & exit 0': ended in 3.03 s, and no sleep 301 was left
  • iPad: /stream through the viewer, then the iPhone selected: the stream ended 0.17 s later
  • The deployed daemon: ctl.sock and ctl.lock are 0600; a second iphone-control daemon exits with "A daemon is already running"
  • Not tried: a lagging follower on the device (unit-tested, including with the old code); an ffmpeg that crashes (no way to make it here); task run --url against an unresponsive viewer; macOS

Checklist

  • MCP tool names, parameters and default outputs stay compatible (D10)
  • No new dependencies
  • CHANGELOG.md and CHANGELOG.zh-CN.md (Unreleased)
  • No private data in the outputs above

🤖 Generated with Claude Code

- a step waiting for the user can be taken over; the question stays, answered
  under takeover or after handing back (daemon, page, demo)
- nothing is recorded after a task's done (a step under way is stopped
  first); a follower that lagged catches up from the log instead of missing
  done and staying open
- the page: a stream the browser closed for good is followed again or the
  task marked stopped; marks drawn once per step; unknown states shown as is
- process groups: watched during the grace, no SIGKILL once gone; task run
  waits for what its command left before exiting; timed commands killed on
  drop (doctor, ffmpeg)
- the MJPEG relay ends when the daemon switches devices; Viewer::drop on the
  LocalSet
- HEVC to H.264: a closed ffmpeg input is a failure; repeated failures move to
  the next encoder
- task run --url: stop() times out; silent or endless event streams end
- one daemon per socket (flock), the socket private from creation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eload, process groups)

- A task's end stops every step still under way (`in_flight`), not only the
  latest: a tool call asking to leave the task's scope and its question are two.
  The page also closes steps left open when `done` arrives (older daemons).
- Steps saved before a reload keep their gesture, so the replayed stream
  doesn't draw their marks again.
- `stop_and_wait` drops a group as soon as it is gone, instead of keeping it
  for the SIGKILL after the grace another group took.
- `task run` looks for what the command left right after reaping it and stops
  it at once, alongside telling the daemon, instead of after two calls that
  may take seconds.
- `single_instance` reports a flock error other than contention instead of
  waiting 15 s and calling it another daemon.
- Approving a step while taken over says the AI goes on once handed back.
- The SSE test installs the rustls provider itself (it depended on test order).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Derrors
Derrors force-pushed the fix/p1-tasks-streams branch from 65dc1e2 to 817eaa0 Compare November 25, 2026 16:25
@Derrors
Derrors merged commit d61d137 into main Nov 25, 2026
7 checks passed
@Derrors
Derrors deleted the fix/p1-tasks-streams branch November 25, 2026 16:32
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