Esta página fue traducida por una máquina y aún no ha sido revisada por una persona. El inglés es la referencia; las correcciones son bienvenidas en GitHub. GitHub

Cookwala / SDK y escenarios / 048

An agent whose mandate lacks the start_cooking scope is refused

Agents act under a mandate from a person. A request that carries a mandate without the start_cooking scope is refused with mandate_scope; the same request with the scope is accepted.

Audiencia: agent builder · Objetivo: Robots para las personas · Nivel: beginner

Leer primero: /docs/CORE/ /docs/HUB/

Pasos

  1. Send a request whose mandate allows planning meals and ordering groceries only. startExecution

  2. The same request with start_cooking in the scopes is accepted. startExecution

Resultado

You know a device checks the mandate before the dry run. Next: scenario 049 shows malformed requests, which are Problems rather than refusals.

Línea de comandos

cookwala hub --recipes examples --speed 200
jq '. + {"x-hub-human-present": true}' examples/core/execute-request.json | curl -s -X POST http://localhost:7878/v1/executions -H 'Content-Type: application/json' -H 'Idempotency-Key: ex-2026-10-04-001-a' -d @-

Salida grabada

Estado: ok

[1] startExecution: {"core": "0.2.0", "kind": "ExecutionStatus", "id": "ex-048-mandate", "seq": 0, "state": "refused", "request": "ex-048-mandate", "updatedAt": "2026-10-07T00:54:31Z", "refusal": {"reason": "mandate_scope", "detail": "the agent mandate lacks start_cooking"}}
[2] startExecution: {"core": "0.2.0", "kind": "ExecutionStatus", "id": "ex-048-ok", "seq": 0, "state": "accepted", "request": "ex-048-ok", "updatedAt": "2026-10-07T00:54:31Z", "x-hub-plan": [{"node": "n1", "op": "cw.op.cut", "by": "device", "verifiedBy": "sensor", "rung": "cw.sense.vision"}, {"node": "n2", "op": "cw.op.cut", "by": "device", "verifiedBy": "sensor", "rung": "cw.sense.vision"}, {"node": "n3", "op": "cw.op.cut", "by": "device", "verifiedBy": "sensor", "rung": "cw.sense.vision"}, {"node": "n4", "op": "cw.op.heat", "by": "human", "verifiedBy": "human"}, {"node": "n5", "op": "cw.op.saute", "by": "human"…

Código

Ejecutar: python scenarios/out/048-refused-when-the-mandate-lacks-start-cooking/python.py

# Scenario 048: An agent whose mandate lacks the start_cooking scope is refused
# Agents act under a mandate from a person. A request that carries a mandate without the start_cooking scope is refused with mandate_scope; the same request with the scope is accepted.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem

def load(path):
    with open(path, encoding="utf-8") as f:
        return json.load(f)

def pick(obj, path):
    for part in path.replace("]", "").replace("[", ".").split("."):
        obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
    return obj

c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))


# Step 1: Send a request whose mandate allows planning meals and ordering groceries only.
try:
    refused = c.start_execution({"core": "0.2.0", "kind": "ExecuteRequest", "id": "ex-048-mandate", "recipe": "cw:cookwala.ai:example-shakshuka", "recipeHash": "sha256:2ea6fab93cc673fc6784f9bc9fe401d2387bb7779dc537df97286b73807c8841", "servings": 4, "requestedBy": "agent:planner-1", "idempotencyKey": "ex-048-mandate-a", "mandate": {"principal": "household:h-42/person:p-1", "agent": "agent:planner-1", "scopes": ["plan_meals", "order_groceries"], "confirmBefore": ["irreversible", "safety_override"], "expires": "2026-10-04T23:59:00+03:00"}}, "ex-048-mandate-key", True)
except CookwalaProblem as p:
    refused = p.body  # a refusal is a result, not a crash
    print("refused:", p.refusal)
assert pick(refused, "state") == "refused", pick(refused, "state")
print("ok", "state", "=", "refused")
assert pick(refused, "refusal.reason") == "mandate_scope", pick(refused, "refusal.reason")
print("ok", "refusal.reason", "=", "mandate_scope")
assert pick(refused, "refusal.detail") == "the agent mandate lacks start_cooking", pick(refused, "refusal.detail")
print("ok", "refusal.detail", "=", "the agent mandate lacks start_cooking")

# Step 2: The same request with start_cooking in the scopes is accepted.
try:
    accepted = c.start_execution({"core": "0.2.0", "kind": "ExecuteRequest", "id": "ex-048-ok", "recipe": "cw:cookwala.ai:example-shakshuka", "recipeHash": "sha256:2ea6fab93cc673fc6784f9bc9fe401d2387bb7779dc537df97286b73807c8841", "servings": 4, "requestedBy": "agent:planner-1", "idempotencyKey": "ex-048-ok-a", "mandate": {"principal": "household:h-42/person:p-1", "agent": "agent:planner-1", "scopes": ["plan_meals", "start_cooking", "stop_cooking"], "confirmBefore": ["irreversible", "safety_override"], "expires": "2026-10-04T23:59:00+03:00"}}, "ex-048-ok-key", True)
except CookwalaProblem as p:
    accepted = p.body  # a refusal is a result, not a crash
    print("refused:", p.refusal)
assert pick(accepted, "state") == "accepted", pick(accepted, "state")
print("ok", "state", "=", "accepted")

print("scenario complete")

scenarios/048-refused-when-the-mandate-lacks-start-cooking.json

Anterior: 047 Siguiente: 049