Plain Reckoner
English
← The blog

Agent workflows

Your AI Agent Returned Valid JSON. Is the Action Actually Allowed?

Make AI agent actions more predictable with output validation, trusted authorization and approval. Run a local example and inspect what each check proves.

By Plain Reckoner Team · Release date: 2026-11-14

Selected source checked:

An action passes through application controls
  1. Model proposal
  2. Validate format
  3. Check permission
  4. Check approval
  5. Ready for execution

Run the local checks

Save both files in the same folder, then run this command with Node.js. The screenshot shows our actual test output using synthetic record IDs.

node --test decision.test.mjs
Run the local checks
This checks the local decision function only. It does not call a model, verify a real identity provider, or perform a database update.

Tested November 15, 2026: five Node.js tests covered malformed input, unexpected fields, unauthorized records, missing approval and an allowed action.

Download decision.mjs · Download decision.test.mjs

Source checked November 15, 2026: OWASP recommends enforcing permissions and parameter validation at tool boundaries. These controls reduce exposure; they do not guarantee that prompt injection is impossible. OWASP ↗

A coding agent returns a neatly formatted request: update record-2. The JSON parses, the fields are present, and the action is spelled correctly. None of that tells you whether the current user may update record-2, whether the user approved this particular change, or whether an earlier request already performed it. The gap between a valid proposal and an authorized action is where a useful AI reliability stack begins.

This tutorial uses a small local Node.js example. It accepts a proposed action as text, validates its shape, then checks permissions and approval against separate trusted application state. It does not call a model, contact a service, or perform an update. Its purpose is to demonstrate the boundary with executable tests, not to measure a model or prove a production system secure.

Start with a task and an observable result

Imagine an assistant that helps a user update a record. The user may work with record-1, but not record-2. The application has already authenticated the user and recorded approval for record-1. The assistant can propose an action; application code decides whether that proposal reaches the operation that changes data.

Write the acceptance checks before generating the integration: malformed output is rejected; unknown fields are rejected; a valid request for an unauthorized record is rejected; an authorized but unapproved request stops; and a request that satisfies the local policy is returned as eligible for the next step. These checks describe this example. A production system also needs real identity, access control, request binding, auditability, and the actual update operation.

A reliable shape does not establish authority

Structured output can make a model's answer easier to parse. A schema can require fields, reject unexpected properties, and restrict values. It cannot prove that a user has access to a named record. It also cannot prove that a reason in a model response matches the user's request or that an approval is genuine. Those facts must come from the application and its trusted state.

In the example, the proposed JSON contains only action and recordId. The trusted allowed-record and approval lists arrive through a separate function argument. An extra field such as approved is rejected rather than treated as a permission. This is intentionally small: in a real system, the trusted data must come from authenticated and authorized server-side mechanisms, not a caller-controlled request body.

Try the local example

Download the source and test files, place them in one folder, and run the Node.js test command provided with the example. Inspect both the passing cases and the negative cases. The tests use synthetic record IDs. They do not test identity providers, databases, network failures, or the behavior of a model.

A useful failure is not merely a nonzero exit code. Check that the unauthorized case reaches the policy check and returns the expected reason. A missing file or syntax error means the demonstration did not run correctly. Keep those outcomes separate when you adapt this pattern to a real application.

What a passing check does and does not establish

In this demonstration, successful tests show that a small function returns the expected decisions for the supplied examples. They do not prove that every possible input was tested, that the trusted lists are populated safely, or that the eventual update would be harmless. Add cases for the actual application's data model and enforce permissions at the side-effect boundary as well.

For example, check that an approval belongs to the same user, record, action, and request that will be executed. Decide what happens if the record changes after review or approval expires. A production design may need a durable operation identifier, a transaction, and explicit handling of retries. Those are application-specific requirements; they are not features supplied by the tiny example.

Keep uncertain actions visible

A timeout after a write does not establish whether the write happened. Before retrying, use the target service's documented status or idempotency mechanism where available. If there is no reliable way to determine the outcome, show it as unknown and provide a safe recovery path. Retrying a model call and repeating an external update are different operations.

Record enough evidence to investigate: an internal operation identifier, the policy decision, timestamps and final state. Do not log secrets or entire private documents merely because a trace is convenient. Test the failure path with harmless fixtures before relying on it in a production workflow.

Build the stack one boundary at a time

Start with a clear contract. Validate the format. Enforce permissions outside the model. Protect consequential actions with the appropriate review and operation identity. Test both allowed and rejected cases. Finally, retain enough sanitized evidence to understand failures and resume safely. Each layer answers a different question; no single layer makes model behavior deterministic.

The OWASP prompt-injection guidance recommends validating tool arguments and enforcing permissions in application code. It also notes that prompt-injection defenses have limits. Use that guidance alongside the documentation for your model, tools and data system, and verify the design under the conditions of your own application.

Read the code as two different inputs

The proposed action is the JSON string supplied to the decision function. Its contents are treated as untrusted even if the model produced valid JSON. The trusted object represents permissions and approvals supplied by application code. In a production service those decisions must be derived from authenticated server-side state; accepting a caller-provided list as authoritative would defeat the example's boundary.

The example checks the exact two allowed keys, requires the action to be update, and requires a nonempty string for the record ID. It then checks whether that ID is permitted and approved. The function returns a decision; it never opens a database connection or changes a record. These are deliberately narrow checks, not a complete security framework.

Try a proposal containing an extra approved field. The check rejects it even if the field says true. Then remove the extra field and request record-2. The format is valid, but the local policy still rejects the request. These two cases illustrate why a schema and a permission decision answer different questions.

Write the smallest useful contract

Before selecting a library, name the operation your application will actually support. Define its inputs, types, permitted values, maximum sizes, and treatment of missing or extra fields. If an identifier is generated outside your application, decide how it maps to a real record. Do not allow the model to turn a display name into authority over an arbitrary internal object.

A good error contract helps the caller recover without exposing private records. Distinguish malformed JSON from invalid shape and an unavailable dependency. For authorization failures, decide how much detail is safe to return to a particular audience. The example returns short reason codes to make the tests understandable; production error disclosure is a separate design decision.

If your model provider supports constrained or structured output, use the provider's documented schema support where appropriate. Still validate at your own application boundary. A provider's schema feature does not establish your business rules, the current user's rights, or the correctness of a factual claim. Also handle refusals, incomplete responses and transport errors as distinct outcomes rather than assuming every response contains the requested object.

Make approval specific to the action

An approval screen should show the action and destination the user is actually authorizing. If the proposal changes after approval, the application needs to decide whether the earlier approval still applies. A review of “update a record” is too vague if the later call changes a different record or a different set of fields.

For a real update, bind approval to the authenticated principal, action type, target identifier, relevant parameters and an expiry appropriate to your workflow. Keep that binding in trusted storage. The model should not be able to grant itself approval by adding a field or writing a persuasive explanation. Enforce the decision in code immediately before the operation, not only in a prompt or a user-interface label.

The local example uses a list of approved record IDs to keep the tests short. It does not model approval expiry, different users, changing parameters or concurrent requests. Do not copy that simplification into a real approval system without designing and testing those missing properties.

Recheck at the execution boundary

Validation performed earlier in the conversation can become stale. A user may lose access, a record may change ownership, or an approval may expire between planning and execution. Decide which facts must be rechecked immediately before a write. If the operation depends on a specific record version, use the storage system's supported concurrency controls rather than relying on the model to notice that another user made a change.

A returned decision is not the same as a completed update. In the local example, an allowed result means only that the illustrated checks passed. In an application, the write can still fail, time out, or affect a different state than expected. Keep the proposed action, authorization result, operation identifier and confirmed outcome distinct in the state machine.

If you add a tool adapter, test it with a harmless substitute before connecting a production service. Verify that rejected proposals do not reach the adapter, that allowed proposals carry only approved parameters, and that failures are surfaced. A clean function result alone does not test the transport or the actual external service.

Design recovery before adding retries

Suppose an approved update is sent and the network times out. The client may not know whether the destination performed it. Starting the entire agent task again can generate another proposal and another write. Preserve the operation identifier and use the destination's supported lookup or idempotency contract to reconcile the result before repeating a side effect.

Retry policy should name its owner and its boundaries: which failures qualify, how many attempts are allowed, what deadline applies, and whether an SDK has retries enabled already. Authentication errors and malformed input usually require correction rather than another identical request. A network error can have a different meaning depending on whether it happened before or after submission.

If the destination cannot establish the result, surface an unknown state. A user can then review the situation rather than being told the update definitely failed. The local decision function contains no retry loop, storage or deduplication mechanism; those must be implemented and tested in the system that performs the action.

Test both rejection and legitimate work

A gate that blocks every update will pass an unauthorized-action test while breaking the product. Include a positive case for a permitted, approved request and negative cases for malformed output, missing fields, extra fields, forbidden records and missing approval. Then add tests for the real application's authorization and approval rules, including the correct user and actual update parameters.

Run the same checks on the broken and corrected versions when reproducing a known defect. Verify that a failure reaches the intended assertion instead of stopping at an unrelated import error. Record the command, environment and final exit status. When model behavior is part of the task, evaluate representative model outputs separately from the deterministic code tests; do not report the latter as a model benchmark.

Keep the test cases small and reviewable. A known forbidden record ID and a known allowed record ID can expose a boundary that a large, vague end-to-end script misses. At the same time, small unit tests do not replace an integration check that exercises your real identity and storage layers in a controlled environment.

Capture evidence without capturing everything

For each attempt, preserve a request identifier, the action type, a policy outcome, timing and a final state where appropriate. Separate the proposal from the action that was actually executed. A log line saying “approved” should point to the trusted decision, not merely repeat a string from the model's response.

Avoid putting access tokens, private prompts or complete customer records into a general log. Use synthetic data in shareable examples and set retention and access controls for real traces. A sanitized trace can show that a request was rejected because it lacked approval without exposing the private contents of the record.

When a review cannot determine the outcome, leave it marked unknown. Missing telemetry is a reason to improve the evidence path, not a reason to invent a successful run. This makes the next investigation more useful and makes recovery less likely to repeat an action accidentally.

Choose tools after the boundary is clear

Schema validators, evaluation frameworks and observability tools can make the process easier to implement, but each covers a different layer. A validator checks a format or specified constraints. An evaluation tool runs cases and reports results. A trace can help locate where a workflow diverged. None automatically decides your application's access policy or guarantees that a tool invocation is appropriate.

Start with the simplest maintained tools compatible with your stack, and verify their current documentation and configuration. Keep a small known-good and known-bad fixture even when you add a framework. If a change causes a formerly rejected case to pass, the test should make that visible before the system is released.

A reliable AI application is built by combining flexible model behavior with clear application contracts and observable outcomes. The exact libraries will change; the questions remain useful: what was proposed, who was allowed to do it, what actually happened, and how can another person verify the answer?