Repository navigation
Typed interface calls for the version, status and telemetry-token commands - #100
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 26, 2026 17:49
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #99
microcosm-emitter-interfacegains typed calls that run the installed emitter'sversion,statusandtelemetry-tokencommands 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'stelemetry_emitter.py,tools/modal_us_stage.pyandtools/modal_us_stage_plan.pyto these calls and deletes its own parsing.What changes
Three calls in
microcosm_emitter_interface.commands, exported from the package:emitter_version()microcosm-emitter versionEmitterVersion(emitter_version, protocol_versions, collector_origin)emitter_status(event_queue_file=None)microcosm-emitter status [--event-queue-file=PATH]EmitterStatus(runs), oneEventQueueRun(run_id, producer_id, upload_state, reason, undelivered_events)per printed runtelemetry_token(lifetime_seconds, development_collector_url=None)microcosm-emitter telemetry-token --lifetime-seconds=NTelemetryToken(access_token, expires_at, collector_origin)with anexpirypropertyEach takes
command,timeoutandenvironment. The command is found asresolve_commandfinds it: the argument, thenMICROCOSM_EMITTER_COMMAND, thenPATH, read from the build's own environment.environmentis 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. Thereasonis 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, anderror_typenames the exception;timed_out: the command was stopped;usage_error: exit 2, which an emitter without the command also gives;telemetry-token:missing_credential(3),rejected_credential(4),collector_unavailable(5),lifetime_unsupported(6) andunexpected_failure(7);status:no_event_queue_file(8) andunusable_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, andoutput_problemsays what is wrong without quoting it.messageis the last line the command wrote on standard error, with whitespace collapsed and at most 300 characters. It never comes from standard output, and atelemetry-tokenfailure that printed anything on standard output gets no message at all.Parsing.
versionaccepts any JSON object, and each field is None when it is missing or of another type. Microcosm usesversionto name an outdated emitter, and releases before 0.11.0 print nocollector_origin.statusrequires every documented field of every run, with its type.telemetry-tokenrequires anaccess_tokenof visible ASCII characters, anexpires_atwith a time zone and an HTTPScollector_origin, which loses its trailing slash. A loopback HTTP origin is accepted only whendevelopment_collector_urlwas given.TelemetryToken's repr leaves the token out.Recorded outputs.
fixtures/commands/version.json,status.jsonandtelemetry-token.jsonrecord 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 ruleemitter.protocol.command-outputs-recorded-and-readregisters 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_processis now the onlysubprocess.Popencall in the interface, used bystart_emitterfor the host and by the three calls. This keeps the number of inline Ruff suppressions unchanged.start_emitterpasses the same arguments as before.Exit codes defined once.
src/microcosm_emitter/cli.pyreadsEXIT_MISSING_CREDENTIALtoEXIT_UNUSABLE_EVENT_QUEUE_FILEfrom the interface'scommands.test_hand_over_codes_and_limits_are_defined_oncenow also covers them, and the ruleemitter.boundaries.shared-definitions-written-oncesays so.Installed check.
tools/installed-interface-smoke.py, whichtools/check-installed.pyruns in an environment holding only the interface wheel, now also checks two things once the started emitter has exited:emitter_versionreports the interface's version and protocol version 1, andemitter_statuslists the run the client recorded. The statement ofemitter.distributions.artifacts-verifiedsays so.Docs. The interface's README has a new "Emitter commands" section, and the root README points to it from the
versiondescription. A changelog fragment is included, andAGENTS.mdanddocs/generated/rules.mdare re-rendered.Decisions
Anthony chose option A for all three on 2026-11-26, which is what the PR implements.
versionreads any JSON object, so every emitter with the command (0.4.0 onward) gives an answer.status(from 0.13.0) andtelemetry-token(from 0.11.0) need no version check of their own: an older emitter rejects the unknown command with exit 2, reported asusage_error. Microcosm refuses emitters below 0.17.0 throughversionanyway. Other options: B, each call first runsversionand returnsunsupported_emitter; or C, all three require this release.CommandFailure, both frozen dataclasses, and no exceptions. This matchesRequestFailureand the package's rule that no call raises errors into the build. Other options: B, one generic result type; or C, exceptions.development_collector_urlonly 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.pyin Microcosm keeps its own command-lineversioncall. It runs under a standalonepython3 -Ibefore the build's environment exists, so it cannot import this package.Release
Merging publishes 0.18.0 of
microcosm-emitterandmicrocosm-emitter-interfaceto PyPI throughpush.yaml, because the fragment isaddedandmainis at 0.17.0. The merge waits only for Anthony marking the PR ready and merging it. Tracker task 5a · 4 then raises Microcosm'smicrocosm-emitter-interfaceminimum 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:MICROCOSM_EMITTER_COMMANDresolve_command, which existed already, when a call gets nocommandtools/check-installed.pyremoves it before running the check, and the check passescommandexplicitly.No other variable is added or read.
Tests
Tests were run locally only to see them fail. CI runs them on this PR.
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 extendedtests/test_boundaries.py::test_hand_over_codes_and_limits_are_defined_once, and acommands.pyholding only the types, the exit codes and the fixture reader. The 125 node IDs were run once: 120 failed, each on anAssertionError. The 5 that passed check behaviour that already exists, namely that today's commands print the recorded keys and that the fixture reader works.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 andxffinstead of the byte 0xFF, which review round 1 also found.ffc7576: 30 node IDs run once. 9 failed: seven on an assertion, and two because the call itself raised (OverflowErrorfor a timeout of 107 s,ValueErrorfor 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.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.5fd4aaa: 16 node IDs run once. The new timeout of 10**400 failed because the call raisedOverflowError, which is the defect it tests; the other 15 passed, including the interruption test with its new time bound, which checks existing behaviour.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.4bad0ef: docstrings, comments and the rule's wording only; its CI had not started when read.Review
49efc94: one high-priority bug and two low ones, all fixed.ffc7576.select.pollcan wait (about 24.8 days), or a lifetime with more than 4,300 digits, made the call raise. Tests are inffc7576and the fix in46cac83: a timeout above a day, and a lifetime that cannot be converted to text, are nowinvalid_arguments.ffc7576).=or nothing as an environment variable name, is nowinvalid_argumentsinstead ofcould_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; theclicomment was moved; and the new rule lists the contract tests of the documented failures.46cac83: two low-priority bugs; round 1's findings confirmed fixed.math.isfiniteraise. Test in5fd4aaa, fix in88cbda0: the timeout is now checked by comparison only.Popen.communicateholds all of a command's output in memory until the timeout, so a faulty emitter that prints without end could make a call raiseMemoryErroror exhaust the build's memory. Bounding it needs a reader of its own in place ofcommunicate, and the emitter's real commands print one short line, so it is not changed here.5fd4aaa). Text holding a lone surrogate, which cannot be encoded, is still reported ascould_not_runrather thaninvalid_arguments; it is left as is.88cbda0: no bugs. Its documentation points are fixed in4bad0ef:exit_codeis negative when a signal ended the command; theusage_errornote no longer claimsversionbefore 0.4.0 exits 2 (those releases have nomicrocosm-emittercommand, so the call givescommand_not_found);telemetry_tokensays how to compareexpirywith the lifetime asked for; the launcher's docstring describesenv=None; and the rule names exactly the failures tested with the real command and lists the default-path contract test.CommandFailure.commandholds the command's name (version) while each call'scommandargument is the program's path; the recorded outputs are not pinned by SHA-256 like the protocol fixtures; and exit 7 oftelemetry-token, and exit 2 ofversionandstatus, are tested only with stand-in commands.telemetry_tokencaptures the command's standard error, so the caller printsmessageitself where the emitter's line used to reach the operator directly. The command is found from the build's own environment, not from theenvironmentpassed. Andstatuson a file that an emitter still running holds for a commit longer than 5 s returnsunusable_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