Documentation menu

Write a protected function

Turn required calculations and business checks into an accepted Wasm function, test its behavior, and verify its permitted result.

Customers define the logic the protected operation must follow. Agents request the work. Standard executes the accepted function on protected inputs, enforces authority and release, and provides Standard Proof.

Technical authoring example · Sales calculation

The introductory Conversation analytics walkthrough explains the operation flow. This separate guide demonstrates the low-level Wasm interface with a sales calculation: select completed transactions, reject duplicate IDs and total the amounts using an approved rule. Planning and orchestration remain in the calling application; the calculation and required checks live in the function.

The CLI is the primary interface for every step below. The SDK runs the same CLI commands from your code, and the optional standard-mcp server lets a coding agent run the local preparation steps. Publication is always a step the person runs.

1. Define the rule and result

Choose the business behavior before writing the adapter. For this example:

  • Each transaction has an ID, a status code and an unsigned amount in integer minor units for one agreed currency.
  • A separate rule input supplies the status code that means completed.
  • IDs must be unique across the whole input, including transactions excluded from the total.
  • The function sums completed transactions with checked integer arithmetic.
  • A duplicate ID or an overflowing total produces a failure report with no usable total. A malformed input refuses computation.

A recipient expecting a successful calculation requires outcome 0, the accepted rule and workload references, and matching input/result evidence before using the total.

2. Use the supported function interface

During onboarding, agree the workload interface, input formats, resource limits and execution path for your function.

The restricted interface

The function runs on protected inputs, so what it can reach is part of the security boundary. It can read the inputs the operation declares, write the outputs the operation declares and observe cancellation. It has no general shell, filesystem, subprocess or network interface, and it does not inherit the requesting agent’s environment or credentials. Memory, fuel, duration and input and output sizes are bounded by the accepted limits.

This keeps the function’s reach to what the owners accepted. Protected inputs can leave only as declared output candidates, which the release rules then evaluate. A function cannot fetch other data, call an external service or run a different program during the operation. Logic that needs those facilities must be adapted to this interface: supply the data as declared inputs and compute the result inside the module.

Interface mechanics

The core Wasm interface below reads named input buffers and writes named output candidates. The accepted operation binds those names and byte limits to the workload.

A core module exports one 32-bit linear memory named memory and an entry point standard_run_v1() -> i32. Its host imports come from standard_core_v1:

  • input_open — (i32, i32) -> i32. Open a declared input using a UTF-8 name pointer and byte length.
  • input_len — (i32) -> i64. Read a handle’s total input size.
  • input_read — (i32, i64, i32, i32) -> i32. Copy bytes from a handle and offset into guest memory at a destination pointer and length.
  • output_open — (i32, i32) -> i32. Open a declared output using its name pointer and byte length.
  • output_write — (i32, i32, i32) -> i32. Append bytes from guest memory to the named output candidate.
  • cancelled — () -> i32. Observe cancellation; a nonzero value signals cancellation.

Pointers and lengths refer to the module’s exported memory. Reads and writes return the transferred byte count; invalid handles, ranges or bounds trap. Resource access is supplied through these declared buffers. Source credentials are used by the appropriate confidential retrieval component, separately from the function’s input/output interface.

The entry point returns 0 when it has produced its output candidates. Standard then applies the operation’s result-release rules. A nonzero return or a trap refuses the computation’s candidates. A business validation failure can instead be an explicit output report with a zero entry-point return.

3. Implement the sales calculation

Download the complete Wasm text example. The example implements the interface above with a fixed 64 KiB memory and at most 256 transactions. The client bundle's starter Rust project implements the same calculation in src/lib.rs, with the named-buffer bindings in src/bindings.rs; start from it to have the CLI compile the workload for you, or compile the Wasm text to a binary core module with your own WebAssembly toolchain and test it as precompiled bytes.

The example’s byte formats are deliberately small and explicit. All integers use little-endian encoding:

  • transactions: repeated 16-byte records containing ID (u32), status (u32) and amount in minor units (u64). Maximum: 4,096 bytes.
  • rules: the completed-status code (u32). Exactly 4 bytes.
  • report: outcome (u32) followed by total in minor units (u64). Exactly 12 bytes.

The business logic is:

text
validate the rule and transaction byte formats
reject duplicate transaction IDs across the full input
select transactions whose status matches the approved rule
sum their amounts using checked integer arithmetic
produce a total report, or a duplicate/overflow failure report

For completed-status code 1, transactions (101, 1, 1250), (102, 0, 999) and (103, 1, 2750) produce outcome 0 and total 4000. The incomplete transaction is excluded. Repeating ID 101 produces outcome 1 and total 0; a total exceeding the unsigned 64-bit range produces outcome 2 and total 0.

The downloaded source contains the calculation and failure handling. Adapt the rule, formats and limits to your own operation, then test and accept that resulting workload revision.

4. Write the operation profile and fixtures

The function defines its logic; the operation profile defines its accepted execution boundaries. Configure the named inputs, their formats and byte limits, execution limits, output format and permitted recipient roles. The profile is the work contract owners authorize; writing one grants nothing.

Treat rule configuration as an input requiring its own accepted reference. The program implements how rules are interpreted; the approved rules bytes determine which status is included in this example. Accept the code and configuration revisions outside the requesting agent’s authority. A change to either follows the affected owners’ approval process.

Keep the fixtures beside the project but versioned independently of the code. A fixture directory holds manifest.json and the adapter contract it names. Each case gives named inputs and either the expected outputs, marked as a success or a business failure, or the expected refusal. For this example, cover at least:

  • Completed and incomplete transactions: only the approved status contributes to the total.
  • Empty input: outcome 0, total 0.
  • Duplicate IDs, including excluded transactions: outcome 1, total 0.
  • Sum overflow: outcome 2, total 0.
  • Malformed rule or partial transaction: a refusal, not a report.
  • Maximum input size and configured memory, fuel and output limits.

Changing an expected output to match new code is a change to the requirements, not a fix. Review it with the people who own the rule before using it to judge a pass.

5. Prepare the workload locally

Local preparation uses fixture data only: no account, key, cloud credential, connected source or customer data. The CLI runs every dependency, compile and fixture step in the preparation image, pinned by image ID, on your local Docker engine. No command, SDK option or MCP input names an image. The client bundle is built for Linux on x86_64, and the CLI uses only a preparation image your Docker engine already holds under the pinned ID; Standard does not distribute that image, so agree how you obtain it during onboarding.

sh
standard dependencies project --workspace . --out deps
standard build project --workspace . --profile profile.json --tests tests \
  --out dist/function --harden --seed 000000000000002a --iterations 100
standard test dist/function/program.wasm --workspace . \
  --profile dist/function/profile.json --tests tests --out test-report.json

standard dependencies retrieves only the crates locked in Cargo.lock and checks each against its lockfile checksum; compilation itself runs offline. standard build compiles the project, runs every declared fixture against the final bytes and, with --harden, runs bounded hardening: seeded mutations of the declared fixtures, bounded by the seed, the iteration count and a time budget. Reaching that budget is not proof for every input. standard test runs the same checks against precompiled Wasm and profile bytes.

standard build writes the authoring bundle: a new output directory holding program.wasm, the emitted profile.json, artifacts/ and build-report.json. Keep the emitted profile exactly as written. The build report is a consistency-checked local record of what was tested: the module, profile, fixtures, preparation image and results. It is unsigned. It detects drift between what was tested and what is published; it is not an approval, publisher authority or evidence of protected execution.

The installed client bundle carries the preparation image pin, and its signed manifest covers that pin’s bytes. The pin a client bundle ships is written when the preparation image is built from the bundle’s source revision, and the pin records that revision. The preparation image itself has no authenticated distribution channel, so the pin is only as trustworthy as the client bundle that ships it. A CLI built from source outside a client bundle reads an unchecked pin beside its executable; its reports say so, and they are refused for production publication.

A missing or fixture pin, an image your engine does not hold, an unreachable Docker engine or an uncertain container cleanup is a setup result, not a finding about your code and not a pass. The CLI names the stage and class; follow its recovery instruction before retrying.

Working with an agent

The client bundle ships the standard-function agent skill beside the CLI, with the starter project. The skill is instructions a coding agent reads to write the function, the profile and the fixtures, and to run standard build and standard test. It is not an operation profile and grants no authority. Through the optional standard-mcp server, the same steps are the standard_build and standard_test tools. They return only the compact summary and the report path, and they refuse a CLI whose preparation image pin is not from its installed client bundle.

From your own code, the SDK calls are standard.dependencies(), standard.build() and standard.test(). See Application integration and Local MCP server.

6. Publish the function

Publication signs with your publisher key and takes effect without an owner approval of its own, so the person who holds that key runs it. There is no MCP publication tool. Pass the build report and fixtures the bytes were prepared with:

sh
standard deploy dist/function/program.wasm --profile dist/function/profile.json \
  --report dist/function/build-report.json --tests tests --workspace . \
  --test-level hardened --name sales-total

The CLI checks the report before reading any key. It refuses when the report is absent or does not bind these exact bytes, profile and preparation image. A refusal means preparing the changed bytes again; never edit the report. standard.deploy() in the SDK runs the same command with the same checks only when it is given the same report and tests; without them, publication of a profile that runs Wasm is refused with report_required. There is no MCP publication tool.

The result is the published function: the profile revision as profileId@revision, its publication digest and a public handoff in the standard.published-function.v1 format that names the Space, the revision and the module digest. The handoff carries no authority and is not signed; give it to the agents and applications that request the function.

7. Request the function

Use the operation sequence in Run your first task with this published sales-total function and its own transaction/rule inputs. That walkthrough uses Conversation analytics; its function name, input format and result schema must not be copied into this sales example. An agent or application requests an operation of the published profile through the CLI, the SDK or MCP. Each request requires the applicable owner authority; no tool approves on an owner’s behalf. Confirm the available route and the hosted continuation limit before attempting execution.

Use local encrypted inputs or approved source selections for both data and separately supplied rule configuration. Inputs must match the accepted format and required bindings before the function uses them. Before relying on the integration, attempt a changed workload artifact, changed rule digest, substituted candidate and mismatched output or recipient. Each request must satisfy the accepted workload, input, authority and release bindings.

8. Verify and use the result

The receiving integration verifies Standard Proof against its accepted workload, input, authority, execution and release references before it uses the result. It then evaluates the report: in this example, outcome 0 makes the total eligible for the recipient’s next-action policy; duplicate and overflow reports identify a failed business check.

Keep the fixtures, the build report, the published revision and the recipient’s result evaluation connected to the same code and rule revisions. See Verification checks for the supported evidence bindings.