aiml

Lesson 3 of 21Module 1

Set task acceptance criteria and request resource budgets

Define task acceptance and request resource limits. Reuse application graders and distinguish admission decisions from post-execution checks.

Prerequisites

The architecture and evaluation-set pilots. The existing RAG capacity gate supplies admission; this lesson connects its behavior to application acceptance instead of rebuilding a queue or agent runtime.

By the end, you can

  • Require task correctness and resource compliance independently.
  • Place each limit before the work it can prevent, or identify it as a post-execution check.
  • Distinguish queue waiting, application execution, output size and provider cost.
Runtime
Python 3.12+
Dependencies
Standard library only

Lab code, tests and fixtures (ZIP, 21 KB)Quiz check your understanding

Progress is saved in this browser.

Accept a result only when the task is handled correctly and the request stays within stated resource limits. Connect the existing application checks to byte limits and capacity admission, then inspect which limits prevent execution and which withhold its output.

Problem

When you need this. You need explicit success criteria and request limits before comparing implementations.

A response uses 147 bytes but gives the staging command for a production request. It fits a resource budget and fails the application task. Conversely, the correct 167-byte result can violate a policy that permits only one output byte. Neither dimension can replace the other.

You will combine the existing application checks with source-byte, output-byte and capacity limits. Seven local scenarios exercise successful handling, wrong results and refusal before invocation. The numbers below come from a local Python function and capacity gate. The lab makes no model call and measures no latency.

Concepts

Task acceptance asks whether the returned result satisfies the user’s contract. Resource compliance asks whether the wrapper stayed within a declared limit. Both must pass; a small wrong answer is still wrong.

A capacity lease temporarily reserves an active worker slot until its work finishes. Admission grants that lease before application work starts. A queue-wait limit controls waiting for that lease. The wrapper can check source bytes before it calls the application; it knows the serialized output bytes only afterwards. None of these limits is a provider-token allowance, a price ceiling or an execution deadline.

Architecture

Local function resultafter admissionExisting task graderssource, command and action policyOutput byte capcount canonical UTF-8 JSONBothpass?Return the responsemeets_contract: trueWithhold the responsefailed task checkor output limityesnoBefore this view: oversized source, full queue or expired queue wait prevents invocation.Local function resultafter request admissionExisting task graderssource, command and action policyOutput byte capcount canonical UTF-8 JSONBothpass?Return the responsemeets_contract: trueWithhold the responsefailed task check or output limitbytesyesnoA small result can still be wrong.
Task acceptance and resource compliance are independent. This view shows checks after an admitted function call; source and capacity checks can stop the request before that call.

The wrapper reads and checks source bytes, acquires a lease from one shared capacity gate, and invokes the existing application. Graders inspect the result, then the wrapper serializes it and checks output size. A refusal before the call leaves the function uncalled. An oversized result after the call is withheld in full; the wrapper does not shorten or alter the command.

Working code

Declare what each limit actually covers

The fixture policy is small enough to inspect:

policy.json — declared scenario limitsSource · jsonDownload
{
  "max_source_bytes": 1024,
  "max_output_bytes": 512,
  "max_active": 1,
  "max_waiting": 1,
  "wait_seconds": 0.02
}

max_source_bytes covers the exact source snapshot read for the lookup, including whitespace. max_output_bytes covers the canonical UTF-8 JSON produced by this wrapper. Neither counts model tokens, HTTP headers or all process memory. The wrapper reads the source before it checks the size, so the cap prevents the application call but not the memory used to read the file.

max_active and max_waiting configure one shared capacity gate. wait_seconds limits how long a request waits for a lease, not how long the application runs. A provider timeout, cancellation policy or spending limit must be built separately, where that dependency is called.

These values are scenario inputs, not measured service capacity. Measure your own workload and capacity before choosing values for a service.

Download the complete lab and enter ai-system-acceptance/:

Exercise task checks and request limits togetherTerminal · bash
python3.12 -m venv .venv
source .venv/bin/activate
python inspect_acceptance.py
python -m unittest -v test_acceptance.py

The ZIP includes the existing architecture, cases and graders, plus the unchanged RAG capacity gate. It uses the standard library, local threads and an asyncio event loop. No package, model or data download is required.

Refuse work before invoking the application when possible

The wrapper checks source size before parsing and invocation. It then acquires a capacity lease before calling the function. An oversized source, full queue or expired queue wait leaves called: false and the task verdict not_run.

acceptance.pySource · pythonDownload
"""Join existing task checks and capacity admission at an application boundary."""
from dataclasses import asdict, dataclass
import json
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent / 'vendor'))
from gate import Gate, Refused as CapacityRefused, finish_worker
import harness
from architecture import Request, answer
import task


@dataclass(frozen=True)
class Policy:
    max_source_bytes: int = 1024
    max_output_bytes: int = 512
    max_active: int = 1
    max_waiting: int = 1
    wait_seconds: float = 0.02

    def __post_init__(self):
        for field, maximum in [('max_source_bytes', 1_048_576), ('max_output_bytes', 65_536)]:
            if type(getattr(self, field)) is not int or not 1 <= getattr(self, field) <= maximum:
                raise ValueError('invalid byte budget: ' + field)
        Gate(self.max_active, self.max_waiting, self.wait_seconds)

    def gate(self):
        return Gate(self.max_active, self.max_waiting, self.wait_seconds)


def encode(value):
    return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(',', ':'), allow_nan=False).encode('utf-8')


def invoke(case, pages):
    return answer(Request(**case['input']['request']), pages)


async def handle(case, raw_source, policy, gate, application=invoke):
    if not isinstance(policy, Policy) or not isinstance(gate, Gate) or any(getattr(gate, field) != getattr(policy, field) for field in ('max_active', 'max_waiting', 'wait_seconds')):
        raise ValueError('capacity gate must match the declared policy')
    if not isinstance(raw_source, bytes):
        raise ValueError('source snapshot must be bytes')
    result = {'called': False, 'source_bytes': len(raw_source), 'output_bytes': 0,
              'quality': {'verdict': 'not_run'}, 'resource_status': 'not_admitted',
              'response': None, 'meets_contract': False, 'policy': asdict(policy)}
    if len(raw_source) > policy.max_source_bytes:
        return dict(result, resource_status='source_too_large')
    try:
        pages = json.loads(raw_source)
        if not isinstance(pages, list):
            raise ValueError('expected a source list')
    except (ValueError, UnicodeError):
        return dict(result, resource_status='source_invalid')

    def call_application():
        result['called'] = True
        return application(case, pages)

    try:
        async with gate.lease():
            try:
                output = await finish_worker(call_application)
            except Exception as error:
                quality = harness.judge_error(case, error)
                response = {'refused': str(error)} if quality['verdict'] == 'pass' else {'error': type(error).__name__}
            else:
                quality = ({'verdict': 'incomplete', 'reason': 'no output'} if output is None
                           else harness.grade(case, output, tuple(task.GRADERS) + harness.CORE_GRADERS))
                response = output
            try:
                produced = encode(response)
            except (TypeError, ValueError):
                return dict(result, quality=quality, resource_status='output_not_serializable')
            resource_status = 'within_limits' if len(produced) <= policy.max_output_bytes else 'output_too_large'
            meets = resource_status == 'within_limits' and quality['verdict'] == 'pass'
            return dict(result, output_bytes=len(produced), quality=quality, resource_status=resource_status,
                        response=response if meets else None, meets_contract=meets)
    except CapacityRefused as error:
        return dict(result, resource_status=str(error))

Create one gate for the shared worker capacity and pass it to requests using that capacity. Creating a fresh gate for every request would give each request its own allowance. The demonstration resets the gate between independent scenarios, then deliberately holds a lease within the saturation probes.

The wrapper also checks that the supplied gate matches the policy it reports. Without that check, a record could report a one-worker limit while a different gate admitted more work.

The reused finish_worker() runs this trusted local function in a thread and retains the lease while it finishes. If the awaiting coroutine is canceled, the thread can still be running, so the lease stays held until the thread returns. The existing gate lesson covers that lifecycle; this wrapper adds no way to stop arbitrary code.

Keep task checks separate from output accounting

After invocation, the existing graders check the response contract, source, exact command and action policy. A required refusal can be the correct task outcome. meets_contract means correct request handling under the declared limits; it never authorizes executing a rollback.

The task verdict compares the result with a case whose expected result is known. A production handler can reuse permission and schema checks, but an arbitrary user request has no known exact answer to compare against.

The wrapper encodes the result and counts bytes after the function returns. If the result exceeds the output cap, it withholds the response. It does not cut a command in half to fit. It also withholds a response that fails the task checks, even when its byte count is small.

The output check runs after the work, so it cannot undo that work or recover money already spent. A model adapter needs its own generation limit before the provider call, and it must account for the provider’s actual usage.

If the wrapper cannot encode a result as JSON, it withholds the result with output_not_serializable and keeps the task verdict it already has, instead of raising an error. The regression test returns a Python set to exercise this path.

Inspect the two independent verdicts

Recorded local acceptance scenariosRecorded output · textDownload
Declared request policy; actual local calls, byte counts and capacity outcomes
normal: called=True; quality=pass; resource=within_limits; bytes=167; meets_contract=True
wrong_command: called=True; quality=fail; resource=within_limits; bytes=147; meets_contract=False
oversized_source: called=False; quality=not_run; resource=source_too_large; bytes=0; meets_contract=False
output_limit: called=True; quality=pass; resource=output_too_large; bytes=167; meets_contract=False
queue_full: called=False; quality=not_run; resource=queue_full; bytes=0; meets_contract=False
wait_timeout: called=False; quality=not_run; resource=wait_timeout; bytes=0; meets_contract=False
required_refusal: called=True; quality=pass; resource=within_limits; bytes=61; meets_contract=True
Queue wait is bounded; execution time, memory and provider charges are not bounded by this policy.

The wrong-command probe passes resource checks and fails task checks. The output-limit probe does the reverse. The source and capacity probes do not invoke the function, so there is no task result to grade. When meets_contract is false, read both fields: a task verdict of fail or incomplete points to the application result, and a resource status other than within_limits names what refused or withheld the request, such as a byte limit, a capacity refusal, an invalid source or an output that cannot be encoded.

ScenarioFunction calledTask verdictResource status
normaltruepasswithin_limits
wrong_commandtruefailwithin_limits
output_limittruepassoutput_too_large
required_refusaltruepasswithin_limits

The queue probes use actual local admission. One holds the only active lease and allows no waiting slot, producing queue_full. The other permits one waiter whose declared wait expires, producing wait_timeout. Both finish with no active or waiting work. These outcomes test admission behavior, not throughput or response time.

The source and output endpoint tests allow an exact byte-limit match and refuse one byte over. A separate test uses a non-ASCII value to distinguish encoded bytes from character count. Reporting the unit precisely matters more than choosing a familiar round number. Keep both endpoint cases when you change a limit check; the equality case fails if the comparison stops accepting the maximum.

Decide what the application may promise

Check or save the complete policy experimentTerminal · bash
python inspect_acceptance.py --check
python inspect_acceptance.py --save my-policy-result.json

The result record keeps the policy, actual byte counts, invocation flag, task verdict and gate events for each scenario. It also records source hashes for the application, graders and reused gate, and --check fails if they change.

Inspect how the independent scenarios hold and release capacity
inspect_acceptance.pySource · pythonDownload
"""Exercise declared request limits with local calls and a real local queue wait."""
import argparse
import asyncio
from dataclasses import replace
import hashlib
import json
from pathlib import Path
from acceptance import Policy, handle, invoke, harness
import task

ROOT = Path(__file__).resolve().parent


async def experiment():
    policy = Policy(**json.loads((ROOT / 'policy.json').read_text()))
    cases = harness.load_cases(ROOT / 'cases.json', harness.load_task('task'))
    task.validate_inputs(cases)
    by_id = {case['id']: case for case in cases['cases']}
    case = by_id['v3-production-lookup']
    source = (ROOT / 'pages.json').read_bytes()
    results = {}

    async def run(name, chosen=policy, raw=source, selected=case, application=invoke, saturated=False):
        gate = chosen.gate()
        if saturated:
            async with gate.lease():
                result = await handle(selected, raw, chosen, gate, application)
        else:
            result = await handle(selected, raw, chosen, gate, application)
        result['capacity_after'] = gate.snapshot()
        results[name] = result

    await run('normal')

    def wrong_command(case, pages):
        output = invoke(case, pages)
        output['command_template'] = 'docuctl rollback RELEASE --env staging'
        return output

    await run('wrong_command', application=wrong_command)
    await run('oversized_source', raw=source + b' ' * policy.max_source_bytes)
    await run('output_limit', chosen=replace(policy, max_output_bytes=1))
    await run('queue_full', chosen=replace(policy, max_waiting=0), saturated=True)
    await run('wait_timeout', saturated=True)
    await run('required_refusal', selected=by_id['reader-proposal'])
    return {'mode': 'Local function calls and local capacity admission; no model, tokenizer or paid API',
            'assumption': 'Byte, capacity and queue-wait limits are declared scenario policy, not measured latency or monetary caps.',
            'files': {name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() for name in
                      ('architecture.py', 'pages.json', 'cases.json', 'task.py', 'acceptance.py', 'inspect_acceptance.py', 'policy.json', 'vendor/harness.py', 'vendor/gate.py')},
            'scenarios': results}


def record():
    return asyncio.run(experiment())


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--save', type=Path)
    parser.add_argument('--check', action='store_true')
    args = parser.parse_args()
    result = record()
    if args.check and result != json.loads((ROOT / 'result.json').read_text()):
        raise SystemExit('acceptance results or source bindings differ')
    if args.save:
        args.save.write_text(json.dumps(result, indent=2) + '\n')
    print('Declared request policy; actual local calls, byte counts and capacity outcomes')
    for name, row in result['scenarios'].items():
        print(f"{name}: called={row['called']}; quality={row['quality']['verdict']}; "
              f"resource={row['resource_status']}; bytes={row['output_bytes']}; meets_contract={row['meets_contract']}")
    print('Queue wait is bounded; execution time, memory and provider charges are not bounded by this policy.')


if __name__ == '__main__':
    main()

This local boundary can promise source admission, bounded active/waiting counts, queue-wait handling and withheld invalid or oversized results. It cannot promise an execution deadline, total heap bound, provider-charge cap or correctness outside the supplied task checks. Those are separate requirements to implement and measure.

Design exercise: a product asks for “a two-second response.” Break that requirement into queue wait, application work, dependency calls and response delivery. Mark which parts this lab controls and where cancellation or an in-flight dependency could keep using capacity after the client leaves.

Failure cases

The wrong-command scenario fits the byte allowance and still fails acceptance. The oversized-output scenario produces the right command but withholds it. Saturation and expired queue waiting stop before invocation, and canceling an await does not stop a worker thread that is already running.

Check the input and failure boundaries
Trigger Required behavior
Bounded output has the wrong task result Reject task acceptance even when resource checks pass.
Source limit before invocation Refuse the oversized source before calling the application.
Oversized output cannot be truncated into a command Withhold the complete result rather than truncating operational instructions.
Full queue prevents invocation Return admission refusal with called false.
Expired waiting budget Refuse lease acquisition without calling it an application execution timeout.
Source byte endpoint Accept equality at the declared maximum and refuse a larger source.
Output byte endpoint Accept equality at the declared maximum and withhold a larger serialization.
Invalid policy values Refuse malformed limits before admitting work.
Malformed source bytes Refuse parsing without invoking the application.
Gate differs from reported policy Reject a supplied gate whose capacity differs from the recorded policy.
Nonserializable output Withhold serialization failure while retaining the task verdict already obtained.

Deployment

A service integration must create one gate per shared worker capacity and pass it to the requests using those workers. Record the effective limits with each outcome, and release leases according to actual worker completion. Add dependency timeouts and client-disconnection handling where those dependencies are invoked.

Scaling

Measure source reading, queue waiting, application work and response delivery separately. Increasing a queue limit can increase waiting without increasing throughput. The byte checks describe retained source and serialized output; measure actual process memory and downstream capacity before choosing service limits.

Key takeaways

  • Task correctness and resource compliance are independent verdicts.
  • Place a limit before the work it should prevent whenever the needed information is available.
  • A queue-wait deadline does not terminate application execution.
A request returns a task-correct answer whose canonical UTF-8 output exceeds the configured byte limit. What should this lab return?

Choose one answer, then check your reasoning.

Read the explanation

Task acceptance and resource acceptance are independent. Output size is checked after the function returns; the lab does not truncate a command.

Best practices

  • Reuse one capacity gate for the resources it represents.
  • Withhold oversized command output instead of truncating it.
  • Record whether application work ran when a request is refused.

References

Sources checked October 24, 2026. The byte counts and admission outcomes are from the bundled local execution; the policy values are scenario inputs.

Written by Arjun Prabhulal.

Module 1

Define the task and its acceptance contract