Skip to main content
A scenario holds the situation a run was in: the trigger that started it and the data available when it arrived. Bitfab can read the trigger from a trace, but the records your agent could see (messages, contacts, memories, calendar events) live in your systems. The scenario callback lets your code send them. Each time a trace is added to a dataset, or its scenario is generated with generate_scenario on the MCP or generateScenario in the SDKs, Bitfab sends the trace to one URL you choose. Your code looks up what it holds for that run and answers with it, and Bitfab saves the answer onto the trace’s scenario.

Setup

  1. Go to Integrations in the Bitfab portal.
  2. Under Scenarios, click Connect on the Scenario callback card.
  3. Enter the URL Bitfab should send to (https only) and a Signing secret of at least 16 characters.
  4. Click Save, then Test Connection. Bitfab sends a ping, and any 2xx answer passes.
There is one callback per organization. Every request names its trace function, so one endpoint can handle several functions.

The request

Bitfab sends a POST with a JSON body:
type is "scenario.added_to_dataset" for a trace added to a dataset, and "scenario.generation_requested" for a trace whose scenario was generated on its own, where datasetId is null. scenario.trigger and scenario.data are null when the scenario has not been generated yet. rootSpan is null when the trace has no root span. environment is the environment the trace was recorded in, or null. callerMetadata is the metadata your code attached to the trace, {} when there is none.

When Bitfab does not send

Bitfab sends once per trace added to a dataset or generated, in the background, so adding the trace never waits for your answer. It skips the trace in these cases.
  • The trace already has a scenario. Adding it to another dataset, or generating it again, sends nothing. Generating it with regenerate: true is the exception: the scenario is cleared and your callback is asked again.
  • The trace is a replay. Its scenario belongs on the trace it replays.
  • A request for the trace is still waiting to run, or your code answered it with 202 less than a day ago. A request that failed, came back empty, or was skipped does not hold back a new one.
  • Bitfab builds the trigger for this trace function and it came out empty. Nothing is saved, so the scenario keeps showing the trace’s input and output.
The test ping has a smaller body: { "type": "ping", "version": 1, "organizationId": "..." }. Bitfab may add fields to the request, and new type values, without raising version, so ignore the ones you do not know. version goes up only when a field you already read changes shape or meaning.

Verifying the signature

Every request carries a Bitfab-Signature header:
v1 is the hex HMAC-SHA256 of <t>.<raw body>, keyed with your signing secret. Compute it over the raw body before parsing it, compare it in constant time, and reject requests whose t is more than a few minutes old.

The answer

Answer 200 with a JSON object, 204 when you have nothing to add, or 202 when you need longer than Bitfab waits and will save the scenario yourself later (see Saving later). Every field is optional, and a field you leave out is left as it is. A field Bitfab does not know is refused, so a misspelled field fails loudly instead of being dropped. A record looks like this:
kind is any name for the kind of record your systems hold, such as memory, order, or ticket. ref names the record in your systems. at is when it was written, or null. asOf is run when the record is as it was when the run started, and now when it is as it is today. quote is the exact text Bitfab looks for in the run’s spans, falling back to text when it is null.

The trigger

scenario.trigger is null until a trigger is saved, and records are refused until one is. Send it in the same answer as the records. History, missing items, and the actual outcome can be saved without a trigger, and the trigger stays unset until an answer sends one. Every field is required:

History and sources

A history entry is one earlier fact, in time order:
role is thread start for the message that opened the thread and fact for anything else. sources describes how a captured copy of your systems was read. Its shape is specific to captures Bitfab already reads, so leave it out unless Bitfab has asked you to send it.

Merge or replace

Leave mode out, or send merge, to add to what the scenario holds. Send replace when your answer is the full picture: each of records, history, and missing that you send replaces the saved one whole, so an empty list clears it. A list you leave out is kept in either mode, so { "mode": "replace", "records": [] } clears the records and nothing else. Repeats inside one answer are dropped in both modes: identical records, history entries with the same kind and quote, and the same missing text. In merge mode, sending the same answer twice leaves the scenario as it was after the first. Describe the situation only. A scenario is read by the person who labels the run, so never say what the run should have done.

Saving later

Bitfab waits 60 seconds for an answer. When your code needs longer, answer 202 straight away and save the scenario once you are done with the SDK’s saveScenario (save_scenario in Python and Ruby, SaveScenario in Go), passing the request’s traceId. Bitfab ignores the body of a 202, saves nothing, does not retry, and records the run as accepted for a later save rather than as having nothing to add.
A later save goes through the same endpoint as any other scenario save, so it does not combine with what is saved the way an answer does:
  • No merge mode. data replaces the scenario’s data whole. To add to it instead, send appendData (append_data), which appends history and missing and leaves the rest as it is.
  • Records replace the saved records whole. Each record is still matched to the span it appears in, as in an answer, but nothing replaces records by kind and ref, and repeats are not dropped, from records, history, or missing items.
  • Records go in a save of their own. records can be sent only with sources beside it, and is refused while the scenario’s trigger is empty. When your answer would have set the trigger, save it first, then the records. An empty list of records is always accepted and clears them.
  • A trace with no scenario returns 404. The save updates a scenario that already exists.
  • Errors come back to your code. A refused save fails your SDK call instead of showing up on the run in Bitfab, so log or retry it on your side.

Failures

Bitfab waits up to 60 seconds for an answer. What it does next depends on the status you answer with: Bitfab follows redirects. A 307 or 308 sends the same signed POST to the new URL, but a 301, 302, or 303 turns it into a GET with no body, which your endpoint will not be able to read. Register the URL your endpoint answers at, without a redirect. To refuse a trace you will not build, answer 422 with the reason in the body. It shows up on the scenario as its error, and Bitfab does not send that trace again until someone generates the scenario again.

What the scenario shows while it waits

Every scenario shows how far building it has come, on its page in Bitfab, in get_scenario on the MCP, and as creationStatus and creationError from the SDKs’ getScenario: A save from your code always marks the scenario created, even when it lands after Bitfab recorded an error.