Skip to content

Typed interface calls for the version, status and telemetry-token commands - #100

Merged
anth-volk merged 7 commits into
mainfrom
claude/typed-command-calls
Nov 26, 2026
Merged

anth-volk merged 7 commits into
mainfrom
claude/typed-command-calls

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #99

microcosm-emitter-interface gains typed calls that run the installed emitter's version, status and telemetry-token commands and return the parsed answer, or a typed failure for each documented exit code, so Microcosm no longer parses their output itself. Tracker task 5a · 4 (PolicyEngine/microcosm#1743) then switches Microcosm's telemetry_emitter.py, tools/modal_us_stage.py and tools/modal_us_stage_plan.py to these calls and deletes its own parsing.

What changes

  • Three calls in microcosm_emitter_interface.commands, exported from the package:

    Call Command Answer
    emitter_version() microcosm-emitter version EmitterVersion(emitter_version, protocol_versions, collector_origin)
    emitter_status(event_queue_file=None) microcosm-emitter status [--event-queue-file=PATH] EmitterStatus(runs), one EventQueueRun(run_id, producer_id, upload_state, reason, undelivered_events) per printed run
    telemetry_token(lifetime_seconds, development_collector_url=None) microcosm-emitter telemetry-token --lifetime-seconds=N TelemetryToken(access_token, expires_at, collector_origin) with an expiry property

    Each takes command, timeout and environment. The command is found as resolve_command finds it: the argument, then MICROCOSM_EMITTER_COMMAND, then PATH, read from the build's own environment. environment is the whole environment the command runs with; without it, the command inherits the build's. The default timeouts are 15 s, 60 s and 120 s, and Microcosm passes its own; a timeout above a day (MAX_TIMEOUT_SECONDS) is refused. Standard input reads nothing (/dev/null).

  • Failures. Every call returns a CommandFailure(command, reason, exit_code, message, error_type, output_problem) instead of raising. The reason is one of:

    • invalid_arguments: nothing was run, for example a timeout outside (0, one day], a lifetime that is not a whole number of 1 or more, a null character in a path, URL or environment variable, or = in an environment variable name;
    • command_not_found;
    • could_not_run: the command was found but could not be started, and error_type names the exception;
    • timed_out: the command was stopped;
    • usage_error: exit 2, which an emitter without the command also gives;
    • for telemetry-token: missing_credential (3), rejected_credential (4), collector_unavailable (5), lifetime_unsupported (6) and unexpected_failure (7);
    • for status: no_event_queue_file (8) and unusable_event_queue_file (9);
    • exited: an exit code the command does not document, or a signal;
    • unusable_output: exit 0, but not the documented answer, and output_problem says what is wrong without quoting it.

    message is the last line the command wrote on standard error, with whitespace collapsed and at most 300 characters. It never comes from standard output, and a telemetry-token failure that printed anything on standard output gets no message at all.

  • Parsing.

    • version accepts any JSON object, and each field is None when it is missing or of another type. Microcosm uses version to name an outdated emitter, and releases before 0.11.0 print no collector_origin.
    • status requires every documented field of every run, with its type.
    • telemetry-token requires an access_token of visible ASCII characters, an expires_at with a time zone and an HTTPS collector_origin, which loses its trailing slash. A loopback HTTP origin is accepted only when development_collector_url was given.
    • All three ignore keys the README does not document. A TelemetryToken's repr leaves the token out.
  • Recorded outputs. fixtures/commands/version.json, status.json and telemetry-token.json record each command's documented output and ship in the wheel. commands.recorded_command_output(name) returns one as the command prints it, for tests that stand in for the emitter. The new rule emitter.protocol.command-outputs-recorded-and-read registers the contract tests, which check that each call parses its recording and that the installed command prints exactly the recorded keys and answers the call.

  • One place starts the command. launcher.start_command_process is now the only subprocess.Popen call in the interface, used by start_emitter for the host and by the three calls. This keeps the number of inline Ruff suppressions unchanged. start_emitter passes the same arguments as before.

  • Exit codes defined once. src/microcosm_emitter/cli.py reads EXIT_MISSING_CREDENTIAL to EXIT_UNUSABLE_EVENT_QUEUE_FILE from the interface's commands. test_hand_over_codes_and_limits_are_defined_once now also covers them, and the rule emitter.boundaries.shared-definitions-written-once says so.

  • Installed check. tools/installed-interface-smoke.py, which tools/check-installed.py runs in an environment holding only the interface wheel, now also checks two things once the started emitter has exited: emitter_version reports the interface's version and protocol version 1, and emitter_status lists the run the client recorded. The statement of emitter.distributions.artifacts-verified says so.

  • Docs. The interface's README has a new "Emitter commands" section, and the root README points to it from the version description. A changelog fragment is included, and AGENTS.md and docs/generated/rules.md are re-rendered.

Decisions

Anthony chose option A for all three on 2026-11-26, which is what the PR implements.

  1. Oldest installed emitter. Option A: version reads any JSON object, so every emitter with the command (0.4.0 onward) gives an answer. status (from 0.13.0) and telemetry-token (from 0.11.0) need no version check of their own: an older emitter rejects the unknown command with exit 2, reported as usage_error. Microcosm refuses emitters below 0.17.0 through version anyway. Other options: B, each call first runs version and returns unsupported_emitter; or C, all three require this release.
  2. Shape of a failure. Option A: an answer or a CommandFailure, both frozen dataclasses, and no exceptions. This matches RequestFailure and the package's rule that no call raises errors into the build. Other options: B, one generic result type; or C, exceptions.
  3. Fields. Option A: a typed field for every key the README documents, which for these commands is exactly what Microcosm needs, and development_collector_url only for tests. Other options: B, also keep the whole parsed object; or C, only Microcosm's fields and no development URL.

Not covered

tools/route_a/check_emitter.py in Microcosm keeps its own command-line version call. It runs under a standalone python3 -I before the build's environment exists, so it cannot import this package.

Release

Merging publishes 0.18.0 of microcosm-emitter and microcosm-emitter-interface to PyPI through push.yaml, because the fragment is added and main is at 0.17.0. The merge waits only for Anthony marking the PR ready and merging it. Tracker task 5a · 4 then raises Microcosm's microcosm-emitter-interface minimum to 0.18.0.

Deployment

No workflow, deploy script, Dockerfile, Terraform or cloud configuration changes. tools/installed-interface-smoke.py, a script CI runs, changes. It reads no environment variable of its own:

Variable Read by Provider and wiring
MICROCOSM_EMITTER_COMMAND resolve_command, which existed already, when a call gets no command Not set in CI. tools/check-installed.py removes it before running the check, and the check passes command explicitly.

No other variable is added or read.

Tests

Tests were run locally only to see them fail. CI runs them on this PR.

  • Failing tests, a38d077: tests/interface/test_commands.py (stand-in commands for each exit code and output shape), tests/interface/test_command_contracts.py (the real installed command), the extended tests/test_boundaries.py::test_hand_over_codes_and_limits_are_defined_once, and a commands.py holding only the types, the exit codes and the fixture reader. The 125 node IDs were run once: 120 failed, each on an AssertionError. The 5 that passed check behaviour that already exists, namely that today's commands print the recorded keys and that the fixture reader works.
  • Change, 49efc94: not re-run locally. CI run 45705969222 on it failed on one test only, test_output_that_is_not_utf_8_is_read_with_replacement_characters, in all four Tests jobs; 1,237 tests passed on Ubuntu and 1,238 on macOS. The test's stand-in wrote a backslash and xff instead of the byte 0xFF, which review round 1 also found.
  • Round 1's failing tests, ffc7576: 30 node IDs run once. 9 failed: seven on an assertion, and two because the call itself raised (OverflowError for a timeout of 107 s, ValueError for a lifetime of 105000), which is the defect they test. The other 21 passed: the fixed non-UTF-8 test, the token contract test with its new bounds, and the two new tests that a stopped command is gone, which check existing behaviour.
  • Round 1's fixes, 46cac83: not re-run locally. CI on it, read during round 2: the Tests jobs passed on Ubuntu with Python 3.13 and 3.14 and on macOS with Python 3.14, and the quality, build-packages, collector, Rules registry and changelog jobs passed. macOS with Python 3.13 was still running.
  • Round 2's failing test, 5fd4aaa: 16 node IDs run once. The new timeout of 10**400 failed because the call raised OverflowError, which is the defect it tests; the other 15 passed, including the interruption test with its new time bound, which checks existing behaviour.
  • Round 2's fix, 88cbda0: not re-run locally. CI on it, read after it finished: every check passed, including the four Tests jobs (Ubuntu and macOS, Python 3.13 and 3.14), quality, build-packages, collector, Rules registry, the changelog check and Required checks.
  • Round 3's documentation fixes, 4bad0ef: docstrings, comments and the rule's wording only; its CI had not started when read.

Review

  • Round 1, on 49efc94: one high-priority bug and two low ones, all fixed.
    • High: the non-UTF-8 test's stand-in escaped its byte twice, so the test failed in CI. Fixed in ffc7576.
    • Low: a timeout above what select.poll can wait (about 24.8 days), or a lifetime with more than 4,300 digits, made the call raise. Tests are in ffc7576 and the fix in 46cac83: a timeout above a day, and a lifetime that cannot be converted to text, are now invalid_arguments.
    • Low: the token contract test's upper bound assumed the command starts within about a second. It is now bounded by the times just before and after the call (ffc7576).
    • Code-quality points fixed: a null character in a path, URL or environment variable, or = or nothing as an environment variable name, is now invalid_arguments instead of could_not_run; new tests check that a command stopped for its timeout, or after an interruption, is gone; _Output's repr leaves out standard output; the docs say standard input reads /dev/null; the cli comment was moved; and the new rule lists the contract tests of the documented failures.
  • Round 2, on 46cac83: two low-priority bugs; round 1's findings confirmed fixed.
    • Low, fixed: an integer timeout above the largest float, such as 10**400, still made math.isfinite raise. Test in 5fd4aaa, fix in 88cbda0: the timeout is now checked by comparison only.
    • Low, left open: Popen.communicate holds all of a command's output in memory until the timeout, so a faulty emitter that prints without end could make a call raise MemoryError or exhaust the build's memory. Bounding it needs a reader of its own in place of communicate, and the emitter's real commands print one short line, so it is not changed here.
    • Code-quality points: the interruption test now bounds its time, so it fails if the command is not stopped (5fd4aaa). Text holding a lone surrogate, which cannot be encoded, is still reported as could_not_run rather than invalid_arguments; it is left as is.
  • Round 3, on 88cbda0: no bugs. Its documentation points are fixed in 4bad0ef: exit_code is negative when a signal ended the command; the usage_error note no longer claims version before 0.4.0 exits 2 (those releases have no microcosm-emitter command, so the call gives command_not_found); telemetry_token says how to compare expiry with the lifetime asked for; the launcher's docstring describes env=None; and the rule names exactly the failures tested with the real command and lists the default-path contract test.
  • Left as they are: CommandFailure.command holds the command's name (version) while each call's command argument is the program's path; the recorded outputs are not pinned by SHA-256 like the protocol fixtures; and exit 7 of telemetry-token, and exit 2 of version and status, are tested only with stand-in commands.
  • Notes for 5a · 4 (Microcosm), from round 3: telemetry_token captures the command's standard error, so the caller prints message itself where the emitter's line used to reach the operator directly. The command is found from the build's own environment, not from the environment passed. And status on a file that an emitter still running holds for a commit longer than 5 s returns unusable_event_queue_file. The Modal runner calls it only after the build's emitter has exited.

Review stopped after round 3.

🤖 Generated with Claude Code

anth-volk and others added 7 commits November 26, 2026 01:30
Add failing tests for microcosm_emitter_interface.commands: calls that run
the installed emitter's `version`, `status` and `telemetry-token` commands
and return the parsed answer or a CommandFailure for each documented exit
code, a missing or unrunnable command, a timeout, unusable arguments and
unusable output, without raising and without the token in any error text
or repr. Contract tests run the real installed command, and recorded
outputs in fixtures/commands/ must match what it prints. The boundary test
now requires the commands' exit codes to be defined once across both
packages.

commands.py holds only the types, the exit codes and the fixture reader;
its calls return a placeholder could_not_run failure, so the tests fail on
their assertions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
microcosm_emitter_interface.commands runs the installed emitter's
`version`, `status` and `telemetry-token` commands and returns an
EmitterVersion, an EmitterStatus or a TelemetryToken, or a CommandFailure
for each documented exit code, an undocumented one, a missing or
unrunnable command, a timeout, unusable arguments or unusable output. No
call raises into the build. `version` accepts any JSON object so an
emitter too old for a build can still be named; a failure's message comes
only from standard error, and a TelemetryToken's repr leaves the token out.

The launcher's start_command_process is now the one place the package
starts the emitter command, for the host and for these calls. The
emitter's cli reads its exit codes from the interface, so they are defined
once. The installed-interface check runs the version and status calls
against the installed emitter. The new rule
emitter.protocol.command-outputs-recorded-and-read registers the contract
tests; both READMEs, a changelog fragment and the rendered rules follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 of #100 found that a timeout above what select.poll can
wait (about 24.8 days) raises OverflowError and a lifetime with more digits
than int-to-text conversion allows raises ValueError, though no call may
raise. New cases of test_unusable_arguments_are_a_failure_and_run_nothing
expect invalid_arguments for those, for a timeout above the new
MAX_TIMEOUT_SECONDS (a day, not yet checked), and for a null character or
"=" in a path, URL or environment variable name, which reached Popen.

Two tests check that a command stopped for its timeout, or after an
interruption while waiting, is gone. The non-UTF-8 test now has its stand-in
write the byte 0xFF rather than a backslash and "xff", and the token
contract test bounds the expiry by the times just before and after the
call instead of assuming the command starts within a second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A timeout above MAX_TIMEOUT_SECONDS (a day), a lifetime with more digits
than int-to-text conversion allows, and a null character or "=" in a path,
URL or environment variable name are now invalid_arguments, checked before
anything runs, so no call raises for them (review round 1 of #100).

Also from that review: _Output's repr leaves out standard output; the docs
say standard input reads /dev/null rather than "closed"; the cli's note on
the exit codes no longer reads as the description of
TELEMETRY_TOKEN_FAILURES; the rule command-outputs-recorded-and-read lists
the contract tests of the documented failures; the changelog fragment names
the timeout limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 of #100 found that math.isfinite raises OverflowError for an
integer timeout above the largest float, such as 10**400, so the call
raises instead of answering invalid_arguments. The interruption test now
also bounds its time: without the kill, Popen's exit would wait out the
stand-in's 30 seconds and the test would still pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_usable_timeout no longer calls math.isfinite, which raises OverflowError
for an integer above the largest float. The range check alone refuses a
NaN, an infinity and such an integer (review round 2 of #100).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wording only, from review round 3 of #100, which found no bugs: exit_code
is negative when a signal ended the command; a release before 0.4.0 has no
microcosm-emitter command at all, so the usage_error note no longer names
it; telemetry_token says how to compare expiry with the lifetime asked for;
the launcher's docstring describes env=None; the rule
command-outputs-recorded-and-read names the failures the real command is
tested for and lists the default-path contract test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 17:49
@anth-volk
anth-volk merged commit a776fb6 into main Nov 26, 2026
14 checks passed
@anth-volk
anth-volk deleted the claude/typed-command-calls branch November 26, 2026 17:50
anth-volk added a commit to PolicyEngine/microcosm that referenced this pull request Nov 26, 2026
Interface 0.18.0 (PolicyEngine/microcosm-emitter#100) adds typed calls for
the emitter's version, status and telemetry-token commands. microcosm-build
requires it; uv.lock moves only that package, APPROVED_UV_LOCK_SHA256 follows
the lock, and the installed-emitter job tests emitter 0.18.0 beside the
minimum, 0.17.0, which does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Typed interface calls for the emitter's version, status and telemetry-token commands

1 participant