Skip to content

Add: docstrings for the remote-buffer API and Worker lifecycle - #1542

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:docs/api-docstrings
Jul 28, 2026
Merged

Add: docstrings for the remote-buffer API and Worker lifecycle#1542
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoWao:docs/api-docstrings

Conversation

@ChaoWao

@ChaoWao ChaoWao commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

docs/user/reference/python-api.md (from #1526) is hand-maintained and states that the source is authoritative when the two disagree. That promise only holds if the source documents itself — and coverage on the public API was 54% in worker.py, 57% in task_interface.py, concentrated in exactly the surface a reader is least able to infer: the L4 remote-buffer calls and the terminal close() contract.

Public non-nested symbols in worker.py, task_interface.py and orchestrator.py are now 100% documented.

File Before After (public, non-nested)
worker.py 54% 36/36
task_interface.py 57% 49/49
orchestrator.py 88% 17/17

Each docstring states read-from-the-implementation behaviour

Not a restatement of the signature. The ones a caller cannot guess:

  • remote_free / remote_release_import are idempotent, reject the opposite handle kind, and defer while a live task slot or an outstanding import still references the buffer — they do not issue the release immediately (_pending_remote_buffer_frees).
  • remote_export can only ever narrow access, never widen it (if flags & ~handle.access_flags: raise), and nbytes=None means "to the end of the buffer".
  • remote_import rejects an export minted by a different Worker, and one whose owner buffer is already freed.
  • add_remote_worker must precede init(), requires level >= 4, and validates the endpoint before any process is forked — its host must be a numeric IP because hostname resolution is unbounded and uncancellable.
  • RemoteBufferHandle distinguishes owner from imported handles (not interchangeable) and is constructible only through Worker (token-guarded).
  • RemoteAddressSpace records that REMOTE_WINDOW / UB_LDST are protocol placeholders today.

Worker.close() is the notable one: its contract already existed as a 26-line comment block, so the reentrancy, init-guard, single-shot-teardown and owner-thread rules were invisible to help() and to any generated reference. The docstring distils that contract; the linearization comments that explain the code below it stay where they are. I did not invent new prose for it.

Eight symbols left undocumented on purpose

worker.py reports 36/44 if nested functions are counted. The eight are handle_task / handle_control / dispatch closures inside private loop functions (_sub_worker_loop, _run_chip_main_loop, _child_worker_loop, _dispatch_control_domain) — internal, not API. The 54% figure was partly a measurement artifact for the same reason.

Why this matters beyond coverage

It is the prerequisite for replacing the hand-written reference with a generated one. With coverage at 54% a generator would have emitted a page full of holes; that was the stated reason for hand-writing it in #1526.

Verification

  • ruff check python/simpler/ and ruff format --check clean
  • All three files compile; test_package_surface.py still 5 passed / 1 skipped
  • Claims spot-checked against code, not assumed: the token guard (task_interface.py:235), the narrow-only access check (worker.py:2901), and the before-init() guard (worker.py:2419)
  • Docstrings verified to land on the intended symbol — an earlier attempt matched by signature text and put RemoteBufferExport's wording onto RemoteBufferHandle's same-named properties; the final insertion is AST-position driven, and same-named properties on the two classes were re-checked individually for correct attribution and no duplicates
  • No behaviour change — docstrings only

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0e2ff365-a71e-4a3d-b471-b1a528c4f921

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Documentation-only updates expand public API docstrings for orchestration scopes, remote task interfaces, worker memory operations, worker registration, and lifecycle methods. No logic or public signatures changed.

Changes

Public API documentation

Layer / File(s) Summary
Orchestration scope lifecycle
python/simpler/orchestrator.py
scope_begin() and scope_end() now document explicit nested-scope usage and required pairing.
Remote interface contracts
python/simpler/task_interface.py
Remote enums, handles, exports, tensor references, domain context, and worker state now document their semantics and constraints.
Worker API constraints
python/simpler/worker.py
Worker registration, remote memory operations, transport selection, and shutdown APIs now document validation and lifecycle requirements.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

Poem

A bunny reads each scope and sign,
“Begin, then end”—the docs now shine.
Remote buffers hop with care,
Worker rules are written there.
No code was changed, just words in line!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding docstrings for the remote-buffer API and Worker lifecycle.
Description check ✅ Passed The description is clearly about the same docstring-only API documentation update and matches the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@python/simpler/task_interface.py`:
- Around line 187-194: Update the RemoteTensorRef documentation to describe
HOST_INLINE handles created by RemoteTensorRef.host_inline(): they contain
inline payload bytes, have no remote allocation, are not released through either
Worker release API, and use nbytes for payload size. Add this distinction
wherever the owner/imported handle behavior is described, including the
referenced additional documentation.

In `@python/simpler/worker.py`:
- Around line 2407-2413: Update the remote worker registration docstring to
state that endpoint hosts must be numeric IPv4 addresses or localhost, matching
the validation performed by _parse_remote_endpoint(). Do not advertise general
numeric IP or IPv6 support unless that parser is explicitly extended.
- Around line 6272-6288: Update the close() docstring to state that any
incomplete accepted-run drain, including interruption, leaves teardown
unattempted and permits retry; do not limit the retryable case to drain
timeouts. Preserve the existing documentation for completed teardown and other
close() behaviors.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: abec49ea-d5da-48fb-9a7b-cfa50b0da3a7

📥 Commits

Reviewing files that changed from the base of the PR and between f293be3 and 3907057.

📒 Files selected for processing (3)
  • python/simpler/orchestrator.py
  • python/simpler/task_interface.py
  • python/simpler/worker.py

Comment thread python/simpler/task_interface.py
Comment thread python/simpler/worker.py
Comment thread python/simpler/worker.py
The user-facing Python reference added in hw-native-sys#1526 is hand-maintained and
says the source is authoritative when the two disagree. That only holds
if the source actually documents itself; docstring coverage on the
public API was 54% in worker.py and 57% in task_interface.py,
concentrated in exactly the surface a reader is least able to infer —
the L4 remote-buffer calls and the terminal close() contract.

Public non-nested symbols in worker.py, task_interface.py and
orchestrator.py now all carry docstrings. Each states behaviour read out
of the implementation rather than restating the signature:

- Worker.remote_free and remote_release_import are idempotent, reject
  the opposite handle kind, and defer while a live task slot or an
  outstanding import still references the buffer, instead of issuing the
  release now.
- Worker.remote_export can only narrow access, never widen it, and
  nbytes=None means "to the end of the buffer".
- Worker.remote_import rejects an export minted by a different Worker
  and one whose owner buffer is already freed.
- Worker.add_remote_worker must precede init(), requires level >= 4, and
  validates the endpoint before any process is forked. Its host must be
  a numeric IPv4 address or localhost: hostname resolution is unbounded
  and so unsupported, and an IPv6 literal is rejected earlier still,
  because the endpoint is parsed as one host:port pair and fails on its
  colon count.
- RemoteBufferHandle distinguishes owner from imported handles, which
  are not interchangeable, and the HOST_INLINE form that
  RemoteTensorRef.host_inline mints — that one names no allocation, so
  neither release call applies to it and its nbytes describes the
  payload rather than an allocation.
- RemoteAddressSpace records that REMOTE_WINDOW and UB_LDST are protocol
  placeholders today.

Worker.close() had its contract in a comment block rather than a
docstring, so the reentrancy, init-guard, single-shot-teardown and
owner-thread rules were invisible to help() and to any generated
reference. The docstring distils that contract; the linearization
comments explaining the code below it stay where they are.

Those comments also under-described the contract, and are corrected so
the file does not contradict its own docstring: they called a drain
timeout "the one retry path", but an asynchronous interruption that
leaves an accepted run fence undrained equally leaves teardown
un-attempted and the tree intact. Both paths are now named, in the
docstring and in the two comments.

Eight symbols in worker.py remain undocumented on purpose: they are
handle_task / handle_control / dispatch closures nested inside private
loop functions, not API.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ChaoWao

ChaoWao commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai All three inline findings addressed and resolved. Each was a real accuracy defect in my docstrings, not a style nit:

  • HOST_INLINE handle form — the class docstring listed only owner and imported handles; RemoteTensorRef.host_inline mints a third form that neither release call applies to, and whose nbytes is a payload size. Both docstrings now say so.
  • IPv6 — corrected to "numeric IPv4 address or localhost". _validate_numeric_endpoint_host would accept IPv6 via AI_NUMERICHOST, but _parse_remote_endpoint rejects the colon count first, so the parser is the binding constraint. Documented rather than adding bracketed-IPv6 parsing, which changes the accepted format and needs a maintainer's call.
  • Second retryable close() path — the async-interruption branch at worker.py:6329-6332 also leaves teardown un-attempted. Now named alongside the lease-budget timeout.

One disclosure: the third fix also required correcting two pre-existing comments in worker.py that called the drain timeout "the one retry path" — I had inherited the error from them, and fixing only the docstring would have left the file contradicting itself. That is the sole change in this PR beyond adding docstrings, and the commit message states it.

Docstring coverage is unchanged at 36/36, 49/49 and 17/17 for public non-nested symbols. ruff check / ruff format --check clean; test_package_surface.py 5 passed / 1 skipped.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

worker_file=$(fd -a '^worker\.py$' | grep -E '(^|/)python/simpler/worker\.py$' | head -n 1)
task_file=$(fd -a '^task_interface\.py$' | grep -E '(^|/)python/simpler/task_interface\.py$' | head -n 1)

printf '%s\n' "== Files =="
printf 'worker: %s\ntask interface: %s\n' "$worker_file" "$task_file"

printf '%s\n' "== Symbol outline =="
ast-grep outline "$worker_file" --items all --match 'Worker|_parse_remote_endpoint|_validate_numeric_endpoint_host' --view expanded
ast-grep outline "$task_file" --items all --match 'RemoteBufferHandle|RemoteTensorRef' --view expanded

printf '%s\n' "== Relevant definitions and documentation =="
rg -n -A22 -B5 \
  'class RemoteBufferHandle\b|class RemoteTensorRef\b|def host_inline\b|def _validate_numeric_endpoint_host\b|def _parse_remote_endpoint\b|def close\b|retry path|retryable|lease budget|drain timeout|HOST_INLINE|IPv4|localhost' \
  "$worker_file" "$task_file"

printf '%s\n' "== Worker.close async interruption range =="
sed -n '6280,6360p' "$worker_file"

Length of output: 77973


@ChaoWao Verified the updates against the implementation. The HOST_INLINE handle form and payload-sized nbytes, the IPv4/localhost endpoint constraint imposed by the single-host:port parser, and both retryable Worker.close() drain-failure paths are now documented accurately. The accompanying pre-existing comments are consistent with that contract as well.

🐇✅


✏️ Learnings added
Learnt from: ChaoWao
URL: https://github.com/hw-native-sys/simpler/pull/1542

Timestamp: 2026-07-28T02:51:35.196Z
Learning: In `python/simpler/task_interface.py`, `RemoteTensorRef.host_inline()` produces a third `RemoteBufferHandle` form with `RemoteAddressSpace.HOST_INLINE`. It contains message-inline payload bytes rather than a remote allocation, its `nbytes` is the payload size, and neither `Worker.remote_free()` nor `Worker.remote_release_import()` applies.

Learnt from: ChaoWao
URL: https://github.com/hw-native-sys/simpler/pull/1542

Timestamp: 2026-07-28T02:51:35.196Z
Learning: In `python/simpler/worker.py`, `Worker.add_remote_worker()` accepts endpoint hosts only as numeric IPv4 addresses or `localhost`: although `_validate_numeric_endpoint_host()` can parse numeric IPv6 literals, `_parse_remote_endpoint()` requires exactly one colon in `host:port` and rejects IPv6 before validation.

Learnt from: ChaoWao
URL: https://github.com/hw-native-sys/simpler/pull/1542

Timestamp: 2026-07-28T02:51:35.196Z
Learning: In `python/simpler/worker.py`, `Worker.close()` remains retryable only when teardown was not attempted and the tree remains intact: either active operation leases exceed the cleanup budget or an asynchronous interruption leaves an accepted run fence undrained.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@ChaoWao
ChaoWao merged commit 29c1488 into hw-native-sys:main Jul 28, 2026
16 checks passed
@ChaoWao
ChaoWao deleted the docs/api-docstrings branch July 28, 2026 04:36
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