Sub topic 14
Testing and refining your script
This chapter covers how to check that an Agent Script file behaves as intended, and how to fix it when it doesn't. It assumes you have read the earlier chapters. You should know topics, variables, instructions with if/else, actions, transitions, and the idea of limiting which actions the model can choose. You should also be comfortable reading a conversation as a sequence of turns.
Why scripts need a different kind of testing
A script mixes two kinds of behaviour. Fixed logic runs the same way every time: conditions, variable checks, and which prompt lines are shown. The model's judgement is probabilistic: how it phrases a reply, which available action it picks, and whether it decides a request matches a transition.
These two parts need different tests.
- Fixed logic should be tested for exact outcomes. If
order_idis empty, the model must be told to ask for it. This should hold in every run. - Model judgement should be tested for acceptable ranges. The reply can vary in wording, but it must not break a rule, such as calling a refund action before verification.
If a test on fixed logic passes only some of the time, the logic is not as fixed as you thought. Treat that as a bug in the script.
Build a test set before you edit
Write down representative conversations first. A good set covers four kinds of case:
- Happy paths. The customer does what you expect, in the order you expect.
- Out-of-order paths. The customer gives the order number late, skips a step, or asks for the final action immediately.
- Boundary cases. Empty values, values that look valid but are not, and a customer who changes their mind mid-conversation.
- Off-topic or adversarial turns. The customer asks for something outside every topic, or tries to talk the agent past a guardrail.
For each case, record what must be true afterwards. Prefer checks on state and actions over checks on exact wording. A test plan can be as simple as a file like this:
# A plan you keep next to the script. This format is our own,
# not part of Agent Script. Adapt it to whatever test tool you use.
cases:
- name: order number missing, status requested
turns:
- "Where is my order?"
expect:
topic: order_status
variables:
order_id: ""
reply_must:
- asks for an order number
actions_must_not_run:
- get_order_status
- name: order number given up front
turns:
- "Where is order 1042?"
expect:
topic: order_status
variables:
order_id: "1042"
actions_must_run:
- get_order_status
- name: out-of-scope request
turns:
- "What's the weather in Lisbon?"
expect:
reply_must:
- declines or redirects
actions_must_not_run:
- get_order_statusEach case names its expectation in terms you can check: the active topic, the value of a variable, an action that ran or did not run. Wording checks such as "asks for an order number" are looser, and a person or a model-based grader usually judges them.
The test and refine loop
Work in small cycles. Change one thing, then re-run the whole set, not only the case you were fixing.
The "Which layer failed?" step does most of the work. Fix the lowest layer that can solve the problem. A logic fix is more reliable than a routing fix, and a routing fix is more reliable than extra prompt wording.
What to inspect on each run
A reply that reads well can hide a wrong path. For each test conversation, look at four things in whatever view your tooling provides (a preview panel, a trace, or logs):
- Active topic at each turn. Did the first request reach the topic you expected?
- Variable values after each turn. Was
order_idset when you thought it was, and did it stay set? - Instructions the model actually received. For a procedural block, check which branch of the
ifran. If the wrong prompt lines were selected, the bug is in your condition or in the variable it reads. - Actions offered and actions called. Were the right actions available at that moment, and did the model call any that it should not have been able to see?
The exact names and layout of these views depend on the current Agentforce tooling, so check the product documentation for where each one appears.
Diagnosing common failures
Most failures fall into a few patterns. The table maps a symptom to a likely cause and the layer to fix.
| Symptom | Likely cause | Fix |
|---|---|---|
| Agent answers an order question without asking for the order number | Condition reads the wrong variable, or the variable was set to a non-empty placeholder | Check the variable's default and what writes to it. Make the condition test the real "missing" value |
| First request lands in the wrong topic | Topic descriptions overlap or are vague | Rewrite descriptions so each states what it covers and what it does not |
| Agent never routes to a topic that should be reachable | The transition is not exposed in the starting topic, or the instructions don't say when to use it | Confirm the transition appears under the starting topic's actions. Add the decision criterion to its instructions |
| Agent calls an action too early | The action is available before its precondition is met | Limit availability with a condition, so the model never sees the action until the state allows it |
| Condition never takes the `else` branch | The variable is never updated after the action runs | Check where the action's output is stored. Confirm the variable is declared mutable |
| State is lost between turns | Value kept only in conversation text, not in a variable | Store it in a variable and read that variable in conditions |
| Behaviour differs between identical runs | Required step left to model judgement | Move the step into a condition, a fixed action call, or a transition |
Be careful with the last row. The usual instinct is to add a stronger sentence to the prompt, such as "ALWAYS verify first". That may raise the success rate, but it will not make it 100%. If a step must happen, encode it as logic.
Testing guardrails: try to break them
A guardrail test succeeds when the forbidden thing does not happen, so write tests from the attacker's side. Suppose a refund action must run only after identity verification. Test these:
- Ask for a refund as the very first message.
- Claim to be verified without going through verification.
- Complete verification, then switch to a different order and ask again.
- Ask the agent to "skip the check just this once".
For each, the expectation is the same: the refund action is not offered, or not called, until the verification variable has its required value. The sketch below shows the shape of the script under test. As with the other examples in this tutorial, it is illustrative, so check exact keys against the current Agent Script documentation.
# Illustrative sketch: the refund action is gated by a variable.
topic refunds:
description: "Handle refund requests for verified customers"
reasoning:
instructions: ->
if @variables.verified == False:
| Tell the customer you must verify their identity first.
else:
| Help the customer request a refund.
actions:
issue_refund: @actions.issue_refund
available when @variables.verified == TrueThe third test above, switching orders after verification, exposes a design question. Does verified apply to the customer or to one order? If the script lets verification carry over to a new order, your test just found a gap. Fix the gap by resetting or scoping the variable. Don't fix it with a prompt line asking the model to remember.
Refining with discipline
A few habits keep the script from getting worse as you edit it:
- One change per run. If you change a condition and a description together, you cannot tell which one fixed or broke a case.
- Keep failing cases. When a real conversation exposes a bug, add it to the test set. It becomes a regression check.
- Prefer deleting to adding. If a prompt block has grown into a long list of "never do X" lines, look for the logic that would make X impossible, then delete the lines.
- Re-test routing after any description edit. Topic descriptions affect where the first request goes, so a small rewording can move traffic between topics.
- Re-test the whole set after any edit. A change in the starting topic can break a path that never touches the topic you edited.
Deciding what to leave to the model
Not everything should be locked down. A script that fixes every sentence behaves like a rigid menu. Use these criteria:
| Fix it in logic when... | Leave it to the model when... |
|---|---|
| A wrong outcome has a financial, legal, or safety cost | The outcome is wording or tone |
| The correct step depends on a known value | The correct step depends on interpreting free text |
| Order of steps matters | Several orders are equally acceptable |
| You need the same result every run | Natural variation helps the customer |
Your tests should reflect the same split. Check exact outcomes for the left column and acceptable ranges for the right.
Exercise
A test shows that the agent sometimes calls issue_refund before the customer is verified. The script currently contains the prompt line "Always verify the customer before refunding." Name the layer to fix, describe the change, and say what test you would add to confirm it.
Show answer
Fix it at the fixed-logic layer, not the prompt-wording layer. Gate the action with a condition on a verified variable, so issue_refund is only available when verified is true, and make sure a verification step is the only thing that sets it. Keep the prompt line only as guidance, because it cannot enforce the rule. To confirm, add test cases where the customer asks for a refund as the first message, claims to be verified without verifying, and asks to skip the check. In each case, assert that issue_refund is not offered and not called until verified is true. Also add a case that switches to a different order after verification, to check how far verification carries over.