feat(atproto): resolve DID to handle and display name via enrichment UDFs - #418
feat(atproto): resolve DID to handle and display name via enrichment UDFs#418julietshen wants to merge 30 commits into
Conversation
Mirrors the existing example_plugins/example_rules pattern with a custom register_input_stream hook that subscribes to Bluesky's JetStream WebSocket firehose. Lets contributors exercise Osprey against real production-rate event volume without the synthetic 1-event/sec generator. Stack docker-compose.atproto.yaml on top of the main compose file (or run ./run-atproto.sh) to use it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Action data shape now mirrors haileyok/atproto-ruleset: $.did,
$.operation.{action,collection,path,cid,record}, $.eventMetadata.
Identity events become action_name='identity'; commit events become
'operation#<create|update|delete>'.
- Rules tree restructured to match the index.sml routing pattern:
main.sml → rules/index.sml → rules/record/index.sml →
rules/record/post/index.sml → individual rule files. Models split
into models/{base,record/base,record/post}.sml.
- Use required=False on entities so non-applicable events don't emit
extraction errors (eliminates __error_count noise on non-post events).
- Snowflake-generated action_id via batched generate_snowflake_batch
(250 IDs per call) instead of time_us, since collisions are possible
at sustained throughput.
- Add unit tests for _event_to_action covering posts, likes, deletes,
identity, account skip, missing time_us, unknown kinds.
- Add app.bsky.actor.profile to default subscribed collections.
- Tighten per-event try/except so a malformed event no longer tears
down the WebSocket connection.
- Drop the Discord-monorepo-specific port-reset block from
docker-compose.atproto.yaml.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
mypy couldn't narrow `ws: Optional[WebSocket]` inside the recv loop because the variable also had to survive into the `finally` block. Moved the inner streaming + close lifecycle into _stream_one_connection, leaving _gen as just the reconnect loop. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Earlier review feedback pointed to haileyok/atproto-ruleset for project
*structure* — but that ruleset is fed by a separate enrichment pipeline,
not JetStream directly, and uses paths like \$.eventMetadata.* and
\$.operation.path that JetStream doesn't emit. Mapping JetStream events
into that shape was misleading: it pretended to expose enrichment
fields that are always null and synthesised paths that don't exist.
Reverted the data shape so Action.data is the JetStream JSON event
passed through unchanged. Rules now read JetStream-native paths:
\$.did, \$.kind, \$.commit.{operation,collection,rkey,cid,record},
\$.identity.handle, etc. action_name is the event's kind ('commit'
or 'identity'). The file hierarchy from atproto-ruleset (main.sml,
models/{base,record/...}, rules/index.sml routing) is preserved.
Verified end-to-end on the live worker:
- 7/7 unit tests pass in container
- 230,392 events processed post-restart, every one with __error_count: 0
- 253,820 commits + 142 identity events emitted with correct paths
- 70 PostContainsTestRule positive matches with label mutation
UserId/test-poster/1 emitted on posts containing "test"
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- IMP-1: Add 30s socket read timeout to detect stalled connections - IMP-2: Track in-process cursor (time_us) to resume from last event on reconnect - IMP-3: Add test coverage for _build_url wantedCollections format, cursor param presence, and malformed message handling - IMP-4: Drop snowflake-id-worker dependency; pass action_id=0 to rely on RulesSink fallback minting - MIN-5: Drop coerce_type=True from string-typed JsonData fields (Collection, Rkey, Cid, PostText) - MIN-7: Stricter time_us validation: reject 0 and negative values, add unit tests - MIN-8: Change debug log to info for socket-close errors - MIN-9: Clarify docker-compose stack startup time in README - MIN-6: Update atproto-ruleset caveat to note enrichment pipeline shape mismatch Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The new test from the previous commit never actually ran (pytest can't import the workspace package outside Docker). Inside Docker the test revealed two issues: - WebSocketConnectionClosedException propagated out of _stream_one_connection, breaking list(gen). Catch it inside the loop and return cleanly — a closed connection is the expected end of a session, not an error worth reraising. _gen still reconnects since the generator just finishes. - NoopAckingContext exposes _item, not item; tests now use the actual attribute name. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
JetStream's contract is time_us: int (microseconds). The previous isinstance(time_us, (int, float)) was over-permissive and would have allowed _event_to_action to yield while the cursor-update narrowing at the call site (isinstance(time_us, int)) silently rejected the same value — leaving the cursor stale on reconnect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The two earlier commits on this branch that touched uv.lock (469219b, c2c0746) were generated with the older host uv, which downgraded the lockfile from rev 3 (main's format, with editable workspace sources) to rev 2 (with virtual workspace sources). Docker builds use a newer uv that requires rev 3, so `uv sync --locked` was failing in the worker image build. Regenerated with the uv shipped in the worker image so the lockfile again matches main's format. Source-only changes: revision bump, workspace members marked editable, and two unused linux_armv7l grpcio wheels dropped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Plugin emits action_name = '<operation>_<short>' for commit events (create_post, delete_like, update_profile, ...) using the COLLECTION_NAMES map next to DEFAULT_COLLECTIONS, or 'identity' for identity events. Commits for unmapped collections or unexpected operations are skipped. Rules side adds three small JsonData feature models — IdentityHandle, LikeSubjectUri, FollowSubject — and imports them from main.sml so they evaluate for every action (None when the path doesn't resolve). example_atproto_rules/config/ui_config.yaml registers default_summary_features mapping action globs to the features the Osprey UI surfaces in the event stream — so each event type shows contextually relevant fields without rule-code changes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The cycle-1 IMP-4 review change passed action_id=0 expecting RulesSink.run's fallback to mint a snowflake. The fallback's condition is `if not action.action_id and action.action_id != 0`, which short- circuits to False when action_id is exactly 0 — so action_id stays 0 forever. Every event landed in MinIO storage keyed action_id=0, overwriting the previous one. Druid retained N distinct rows but the event-scan UI fetched by action_id, returning the same MinIO object N times — hence the duplicate-event rendering. Restoring the local snowflake batch (250 ids per snowflake-id-worker call) so each event gets a distinct id. README updated to surface the SNOWFLAKE_API_ENDPOINT dependency. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Old shape evaluated FollowSubject ($.commit.record.subject as str) for
every event. For like/repost records, the path resolves to a {uri, cid}
dict, the str type check fails, and the engine increments
__error_count by 1 on every like/repost — likely also what was
breaking the timeseries chart, since errored events get aggregated
differently.
Replace with a single Subject feature in models/record/base.sml that
prefers $.commit.record.subject.uri (the dict-shape) and falls back to
$.commit.record.subject coerced to str (the follow DID-string shape)
via ResolveOptional. Drop the per-collection follow.sml / like.sml
files and update ui_config.yaml so likes / reposts / follows all
reference the unified Subject.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
websocket.create_connection returns the low-level socket which doesn't auto-PING; the only thing keeping the connection alive was JetStream's volume plus the read timeout. Use WebSocketApp.run_forever with ping_interval=20 and ping_timeout=10 instead, and bridge the callback API into _gen via a gevent.queue.Queue. greenlet.link pushes a 'done' sentinel as a safety net so the generator never blocks if run_forever exits without firing on_close. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Previously every reconnect waited a fixed reconnect_seconds (default 2s), so a dead JetStream host or a bad URL would just be hammered every two seconds forever. Add max_reconnect_seconds (default 60s) and double the sleep on each session that produced no events; reset to the base on any session that yielded at least one event. Extracted the state update into _advance_backoff so it can be tested without spinning up the full _gen loop. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Examples shouldn't pull in observability infra. Removed sentry_sdk import + the two capture_exception calls from the plugin entirely. Replaced the catch-all `except Exception` around `json.loads` plus `_event_to_action` with three narrower paths: - json.JSONDecodeError logs the parser message and the first 200 bytes of the raw payload so a non-JSON message is identifiable in logs. - A non-dict JSON payload (list, scalar, etc.) is logged with the parsed type name. - snowflake_id_worker minting failures (the only realistic raise from _next_action_id) are logged distinctly so they don't get conflated with payload-shape problems. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The repo already has a Backoff utility used by the etcd watcher and the bulk-label sink. Drop the hand-rolled _advance_backoff and _next_reconnect_seconds in favour of it; succeed() resets and fail() returns the next delay. The Backoff class also includes jitter, which the hand-rolled version did not. The dedicated _advance_backoff tests go away with the helper — the Backoff class is exercised elsewhere in the worker. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Resolve conflicts: - pyproject.toml: keep both osprey_async_worker (main) and example_atproto_plugins (this branch) across workspace members, isort, and fawltydeps. - CHANGELOG.md: adopt main's Keep a Changelog format; re-add the #236 entry under Added. - uv.lock: regenerated with `uv lock` (picks up websocket-client and example-atproto-plugins).
The atproto plugin ships 24 unit tests for the JetStream event mapping, but example_atproto_plugins was not in testpaths, so pytest never collected them. Add it alongside example_plugins. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
The atproto plugin registers only the input stream; the sample rules use TextContains, a labels service, and an output sink from the sibling example_plugins package. Document that the two run together so it isn't a surprise for anyone lifting the sample on its own. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
…check The license check (added since this branch was cut) flags the local example_atproto_plugins package as UNKNOWN license. Add it to the first-party ignore list alongside example_plugins, and trigger the check on its pyproject. websocket-client (Apache-2.0) is already covered by the allow-list. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
python-quality (added since this branch was cut) flagged two things: the atproto_plugin package lacked a py.typed marker, so mypy treated imports from it as untyped; and jetstream_input_stream.py needed reformatting under main's ruff line length. Add the marker and apply ruff format. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
- Snowflake mint failures now drop the connection instead of re-minting per message, so _gen()'s reconnect backoff throttles retries during a snowflake-id-worker outage (was a retry-storm risk). - Guard datetime.fromtimestamp against out-of-range time_us so one malformed event is skipped rather than propagating an exception that forces a full reconnect; add a regression test. - Use an absolute OSPREY_RULES_PATH for the worker to match the UI API. websocket-client (Apache-2.0) already passes the license check and has no known CVEs, so no dependency change was needed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
…UDFs JetStream events identify the actor only by DID, which isn't searchable the way a handle or display name is (closes #417). Add AtprotoHandle and AtprotoDisplayName UDFs that resolve a DID against Bluesky's public getProfile AppView, cached per DID and failing soft when the API rate-limits at firehose volume. Wire them into base.sml as Handle/DisplayName on every action and surface them in ui_config. This replaces the dead IdentityHandle model (JetStream identity events carry no handle) and corrects the README's identity-event shape. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (1)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
The enrichment does a per-event external API call, which is great for demos but undermines #236's load-testing purpose. Move Handle/DisplayName out of base.sml into an opt-in models/enrichment.sml that main.sml does not import by default, so the firehose runs dependency-free unless enrichment is explicitly enabled. Keep the two shipped UDFs (AtprotoHandle, AtprotoDisplayName) over a shared full-profile cache, and document in the README both how to enable enrichment and how to extend it with more profile fields (account age, follower counts, labels) rather than shipping them all wired-in. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
| _ATPROTO_CATEGORY = 'ATProto' | ||
| _GET_PROFILE_URL = 'https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile' | ||
| _REQUEST_TIMEOUT_SECONDS = 5 | ||
| _CACHE_MAX_SIZE = 10_000 |
There was a problem hiding this comment.
i think i'd suggest a larger cache size, maybe around 100k?
|
|
||
| _session = requests.Session() | ||
| # did -> profile dict (the raw getProfile response). | ||
| _profile_cache: 'OrderedDict[str, dict[str, Any]]' = OrderedDict() |
There was a problem hiding this comment.
im not super familiar with how to best do this in python and particularly with gevent and locks... i might suggeset to claude to try and use functools's lru_cache decorator for this though.
https://docs.python.org/3/library/functools.html#functools.lru_cache
There was a problem hiding this comment.
tbh we probably also want this to be an expiry cache, since both handles are display names are subject to change
a preferred approach to having the cached items expire though is to have the cache get bust for the given key whenever an identity event (for handles) comes through jetstream or whenever a profile update events comes through. that way we are immediately responding to those things.
this adds some complexity that might not be worth fully ironing out here, but that would be the canonical example of how to do this i think
| def execute(self, execution_context: ExecutionContext, arguments: DidArguments) -> str: | ||
| handle = _profile_or_skip(arguments.did).get('handle') | ||
| if not handle: | ||
| raise ExpectedUdfException() | ||
| return handle |
There was a problem hiding this comment.
hmm. this is interesting and im not exactly sure how to best do this with SML.
it's probably most likely that we're going to want both the handle and the DID in whatever model deals with user events. in that case, especially since these are going to get executed async, i think we'll end up with two requests getting made for the handle. the current locking won't actually stop a second request for being made for a user. this, i think, will double the amount of requests we are making to the api.
there's a couple of things that we could probably do, but the easiest might just be to do something like:
- Have some UDF called
AtprotoProfilethat returns the profile object - Use
JsonValueon the result ofAtprotoProfileto pull out the handle and display name from theAtprotoProfileUDF's result
i think this will work with SML? but im not sure...
|
left some comments. i think this code would work as is, but most of these are suggestions on how we can reduce the number of requests we have to make... i'd be less hesitant to make sure we get this right if i didn't think that people would actually end up using these in real world use cases, but i figure there's a good chance that they will, so i'd definitely like to at least get right stuff like handling display name/handle updates |
|
Thanks @haileyok, this was really helpful! I ended up keeping the two DID-based UDFs but reworked the cache underneath them to incorporate all your feedback.
I seriously considered your AtprotoProfile-returns-the-object idea and i like it but JsonData only reads the raw event data, not another feature's output, so there's no JsonValue-to-pull-from-a-feature to lean on, and a plain dict isn't a valid feature type. To make it work you'd need a custom language type (like we have for |
Addresses Hailey's review: async UDFs run concurrently in the gevent pool, so a rule reading both handle and display name fired AtprotoHandle and AtprotoDisplayName at once, each missing a cold cache and making its own getProfile call. Coalesce concurrent misses so the second greenlet waits on the first fetch instead of duplicating it. - Share an in-flight fetch per DID (one request, not one per field). - Expire cached profiles after an hour, since handles and display names change; note the event-driven busting alternative in code and README. - Raise the cache ceiling to 100k. Keeps the two DID-based UDFs unchanged, so rules and the README's extend-with-more-fields pattern are unaffected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TEcfpqhza3dPepZUVWw36X
Closes #417. Stacked on #236 (
hailey/atproto-jetstream-sample) — review after #236 lands.What this adds
JetStream events identify the actor only by DID, so the event stream and rules can't search by handle or display name. This adds two enrichment UDFs in the atproto plugin —
AtprotoHandle(did)andAtprotoDisplayName(did)— that resolve a DID against Bluesky's public, unauthenticated AppView (app.bsky.actor.getProfile), over a shared per-DID profile cache, failing soft when the API errors or rate-limits.Enrichment is opt-in (off by default). Each unique DID costs an external API call, which is exactly the dependency you don't want in #236's load-testing scenario. So the default rules run against the raw firehose with no outbound calls; the enrichment lives in
models/enrichment.sml, whichmain.smldoes not import by default. The README documents the one-step opt-in (plus the lexicographically-sorted import) and how to extend it with more profile fields (account age, follower/post counts, existing moderation labels) rather than shipping them all wired-in.It also removes the dead
IdentityHandlemodel and corrects the README's identity-event shape — JetStream identity events carry onlydid/seq/time, never a handle (verified against live traffic).Verified live
Handle/DisplayNamein output,__error_count: 0.__error_count: 0, rate-limited lookups fail soft.🤖 Generated with Claude Code