credencestack.env¶
The simulated finance back office one agent works in during one episode: vendors, invoices, the payment ledger, mandates, approvals with a scripted human reviewer, inbound third-party requests, email, and a simulated clock. For the other domains it adds a synthetic watch-list with alerts and held payments (env/fincrime), documents checked against the vendor master, ledger, bank verification and a company registry (documents_world, registry), and onboarding files (env/kyc). Agents act only through the tools of the families their scenario gets: the eleven payments tools by default, 42 tools in all.
Responsibilities¶
- Seed the state for a scenario (
EnvSeed) and expose the tools with their argument schemas:TOOL_SPECSfor payments,families.specs_forfor the families a scenario declares (DEFAULT_FAMILIESper domain). - Execute tool calls (
Environment.call): validate the arguments, apply the effect, advance the clock and append trace events. - Answer approval requests through
ReviewerPolicy: approve or reject (by default or per invoice), after a simulated latency. - Plant injected text in tool results where the scenario says so.
- Let faults and the kill switch hook into calls (
wrap_tool, call hooks).
Non-goals¶
- Talking to any real system. Nothing leaves the process.
- Judging behaviour. The environment records what happened; graders decide whether it was right.
Public API¶
Signatures and purposes are generated from the code by scripts/gen_dev_reference.py; errors come from each docstring's Raises: section or from api-notes.yaml, which also holds the hand-written effects column.
| Name | Kind | Signature | Purpose | Errors | Effects |
|---|---|---|---|---|---|
TOOLS_BY_NAME |
constant | TOOLS_BY_NAME: dict of 11 |
Tool name -> its ToolSpec. | - | - |
TOOL_SPECS |
constant | TOOL_SPECS: tuple of 11 |
The environment's tools, in the order agents see them. | - | - |
ApprovalRecord |
model | ApprovalRecord(approval_id, action, invoice_id, amount, reason, approvers_requested, approvers_granted, status, …) |
A request for human approval and the reviewer's answer. | - | data |
BankDetailsChange |
model | BankDetailsChange(at, old, new, by) |
One change to a vendor's bank details. | - | data |
CallHook |
type | CallHook |
Callback run before each tool call (the kill switch and rate observer use it). | - | - |
CallInfo |
model | CallInfo(step, tool, args, spec) |
What hooks see about a tool call. | - | data |
CredenceEnv |
class | class CredenceEnv: .reset, .step, .trace, .run_connector |
A CredenceStack scenario as a step-by-step environment (see the module docs). | - | pure apart from loading profiles and policies; deterministic for a seed and actions |
Email |
model | Email(to, subject, body, ts) |
An email the agent sent. | - | data |
EnvSeed |
model | EnvSeed(start, currency, balance, vendors, invoices, reviewer, payments, inbound_agents, …) |
Initial state of the environment for one scenario (Scenario.env_seed). |
- | data |
Environment |
class | class Environment: .tool_specs, .wrap_tool, .call, .record, .finish, .payments_for … |
A deterministic simulated back office. | - | in-memory state only; every call advances the simulated clock and appends trace events |
FixedScenarios |
model | FixedScenarios(scenarios) |
One scenario per reset from a fixed list (chosen by the seed). | - | data |
GeneratedScenarios |
model | GeneratedScenarios(packs, levels, pack_path) |
A freshly generated Forge scenario per reset (the seed is the generation's seed). | - | data |
Invoice |
model | Invoice(id, vendor_id, amount, currency, due_date, number, document_text, instalments, …) |
An invoice waiting to be paid. document_text is where injections live. |
- | data |
Payment |
model | Payment(payment_id, invoice_id, vendor_id, amount, currency, idempotency_key, ts, approval_id) |
An executed payment in the ledger. | - | data |
ReviewerPolicy |
model | ReviewerPolicy(default, by_invoice, latency_s, max_approvers) |
How the scripted human reviewer answers approval requests. | - | data |
RewardWeights |
model | RewardWeights(schema_, compliant, finding_penalty, unmet_expectation, over_caution, tool_budget, tool_call_penalty, floor) |
The shaped-reward weights (credencestack/env/rewards.yaml). |
- | data |
SimClock |
class | class SimClock: .now, .advance |
Deterministic clock for one episode. Never reads the wall clock. | - | in-memory; never reads the wall clock |
ToolError |
exception | class ToolError |
A tool call failed (bad arguments, unknown object, service fault...). | - | - |
ToolSpec |
model | ToolSpec(name, description, parameters, consequential, action, kind) |
One environment tool. | - | data |
Vendor |
model | Vendor(id, name, bank_account, is_new, blocked, history, registration_number) |
A supplier in the vendor master file. | - | data |
make_env_batch |
function | make_env_batch(scenarios: Scenario \| Sequence[Scenario] \| ScenarioSource, n: int, *, seed: int = 0, **kwargs: Any) -> list[CredenceEnv] |
n environments with seeds derived from seed (independent, deterministic). |
- | pure |
reward_weights |
type | reward_weights |
The packaged shaped-reward weights (rewards.yaml). | - | reads the packaged rewards.yaml |
rollout |
function | rollout(env: CredenceEnv, policy: Policy, *, seed: int \| None = None) -> tuple[float, Info] |
Run policy for one episode; returns the total reward and the final info. |
- | runs the policy for one episode |
run_batch |
function | run_batch(envs: Sequence[CredenceEnv], policy: Policy, *, workers: int = 4) -> list[tuple[float, Info]] |
One rollout per environment, in parallel threads (standard library only). | - | runs one rollout per environment in threads |
run_connector |
function | run_connector(env: CredenceEnv, connector: Any, *, seed: int \| None = None) -> tuple[float, Info] |
Let a CredenceStack connector be the policy for one episode of env. |
- | runs the connector's agent for one episode (may call a model) |
validate_args |
function | validate_args(spec: ToolSpec, args: Mapping[str, Any]) -> dict[str, Any] |
Check args against the tool's schema (required, unknown keys, basic types). |
ToolError: with a message the agent can act on. | pure |
killswitch.KillSwitch.install |
function | install(self, env: Environment) -> None |
Hook into env (before and after every tool call). |
- | installs a call hook on the environment |
Examples¶
The connectors example lists the tools an agent is offered and prints one episode's trace. The faults example builds an Environment directly and calls a tool.
Invariants and gotchas¶
- Determinism: time comes from
SimClock, ids from counters, and randomness from a seededrandom.Random. In the traces in these docs each tool call takes one simulated second, and the demo reviewer answers after 300 simulated seconds. - Every call, including a failing one, is traced as a
tool_calland atool_result. A failure is aToolErrorwhose message the agent can act on, and is recorded in the result'smeta.error. - Once the kill switch fires the environment is suspended: pending approval requests are withdrawn, so a safe agent waiting on approval sees the withdrawal.
pay_invoicehonours anidempotency_key. Without one, a retried payment after a timeout executes twice, which is exactly what RES-001 tests.
Where the tests are¶
Test files that import this package's modules (from the file index):
tests/integration/test_mcp_connector.pytests/unit/test_api.pytests/unit/test_conformance.pytests/unit/test_control.pytests/unit/test_docintegrity_grader.pytests/unit/test_env.pytests/unit/test_env_gym.pytests/unit/test_env_rl.pytests/unit/test_env_v1.pytests/unit/test_faults.pytests/unit/test_fincrime_grader.pytests/unit/test_forge_mutators.pytests/unit/test_inspect_runner.pytests/unit/test_kyc_grader.pytests/unit/test_otel_workflows.py