Agents from scratch (TypeScript) · Chapter 11 of 18

Permissions

By the end of this chapter you can decide what an agent is allowed to do, and make that decision hold: tools per role, a check in the code that runs every call, a customer limited to their own bookings, and instructions hidden in messages and tool results read as data, never obeyed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Agents that hold up
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.

Why you need this

GharFix now runs three assistants on the same loop. The customer bot on WhatsApp finds technicians, quotes and books. The support bot helps Meenakshi's team move and cancel bookings. The finance bot helps Farooq with refunds. Same model, same loop, same eight tools, and very different things each should be able to do.

The first version said so in the system message. Here is the customer bot, offered all eight tools, with "You may only find technicians, quote and book. Never cancel a booking or refund." at the end of its system message, and the message "Cancel GF318075" (temperature: 0):

[0] cancel_booking({"booking_id":"GF318075"})
    -> {"booking_id":"GF318075","status":"cancelled","refund":199,"policy":"4.1",...}
[1] answer: Booking GF318075 is cancelled, with a refund of ₹199 (policy: 4.1, ...).

The lab model ignores such rules completely. A real model follows them most of the time, and that is the problem: the customers who try hardest to talk a bot into something are exactly the ones you need the rule for. Vikram's rule for the team: what an agent is allowed to do is decided in code, never only in the prompt.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    roles.ts: tools per role

    In roles.ts, export ROLES, an object from a role's name to a Set of the tool names it may use: customer may use find_technicians, check_slots, get_quote, book_slot and get_booking; support may use everything customer may, plus reschedule_booking and cancel_booking; finance may use everything support may, plus request_refund.

    Then export toolsFor(role): the tool descriptions from TOOLS (in ../../shared/gharfix.ts) that the role may use, in TOOLS's order. A role that is not in ROLES gets an empty array, and that includes names every object answers to, like constructor and toString.

  2. Task 2

    executor.ts: refuse outside the role

    In executor.ts, export execute(role, name, argumentsJson): name is the tool the model asked for and argumentsJson the JSON text it wrote. When the role may not use the tool (by ROLES, from ./roles.ts), run nothing and return JSON text of an object with an error key that names the tool and the role. Otherwise run GharFix's function (FUNCTIONS) and return its result as JSON text; anything thrown becomes an error result too, so execute never throws.

    Then export async runAgent(question, role): chapter 3's loop with lab-chat, a system message saying today is 2026-10-22, the question, and only the tools the role may use (toolsFor), at most 10 requests, with every call run through execute. Return the final answer's text.

    The check calls execute with calls in and out of each role, and with roles it does not know, and runs a cancellation as a customer and as support.

  3. Task 3

    owner.ts: only your own bookings

    A customer chats from their own phone, and may only see and book for that phone. In owner.ts, export executeFor(phone, name, argumentsJson): it runs a call for the customer role (use execute from ./executor.ts), with two more checks first.

    When the arguments have a booking_id, the booking must be one whose phone is the customer's; if it is someone else's, or does not exist, return an error, and make the two errors read the same, so the bot never tells a stranger that a booking ID exists. When the call is book_slot, its phone must be the customer's, or return an error. Like execute, it returns JSON text and never throws.

    A booking's phone is in GharFix's data: db().bookings is a Map from booking ID to booking. The tools never give it out.

  4. Task 4 · interview question

    Interview: fix-injection.ts, an injected refund

    fix-injection.ts's runSupport(question) answers customers for GharFix's support team. Only finance refunds, and its system message says so, yet this morning a customer's message got a booking refunded, and a note left on another booking made the bot answer with an instruction hidden in it.

    Fix it so that the bot can never refund: it is offered only the support tools (SUPPORT), and any call outside them is refused with an error result when it is run (in runTool). An instruction inside a tool result must be read as data, never obeyed. Support's own work, looking bookings and notes up, moving and cancelling, must keep working.

    Keep runSupport(question), runTool, getNotes, NOTES and SUPPORT, exported. The check sends this morning's two messages, and a reschedule.

What you will learn

  • Tools per role: a model can only ask for what it is offered.
  • An executor that refuses: the check that holds when everything else is wrong.
  • Ownership: a customer sees and books only for their own phone.
  • Injected instructions, in a customer's message or a tool result.

More in Agents that hold up

Build "Permissions" yourself

GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. You build it in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.