Course menuForge Cloud

Level 2 · Lesson 5

Send repairs to the expert

Goal of this lesson

Add a second trigger that watches for repairs marked for the expert and moved to Approved for expert, checks four conditions (the lesson calls them gates), and sends each one on with a comment, exactly once.

About 25 min4 steps

Behind, or starting here? Download the code as it should be before this lesson, or in the course workspace run course-catch-up level-2/after-04-repair-report in the folder above your app. It offers to bring your own app folder to this point and keeps its app id (the id: line in manifest.yml, which links the code to your app on the site), so it stays the same app. Your old files are kept in a copy next to it.

Some repairs need a diagnosis before anyone touches the car, such as the battery repair whose notes ask for one before any electrical work. Workshops send that kind of repair to an expert, such as someone who knows car electrics. Forge LLM already labels those repairs needs-expert. A person still decides when one is ready: they move it to Approved for expert, and the app sends it on. In this lesson, sending it on means one comment on the repair task saying it is ready for the expert; nobody answers yet. In lesson 6 you add the expert, an AI agent, who picks it up.

See this on the Level 2 chart, in the handoff step.

Step 1: Four gates, then send it on once

Runs in the Atlassian cloud

Agent handoffin the workshop: sending a repair to the expert

The handoff is a trigger that runs on work item updates and acts only when every gate passes:

  • The repair task is in the Workshop space.
  • It has the needs-expert label.
  • Its status is Approved for expert.
  • It has not been sent on before.

Then it comments once and records that it did, so moving the repair task back and forth never sends it twice.

In the workshop Approved work goes to an expert, such as someone who knows car electrics, once each.

An updatefrom JiraWorkshopspace?needs-expert label?Approvedfor expert?Not sentbefore?Comment oncethen record itany gate that says no stops the update there:nothing happens, and nothing is recorded

A new file holds the gates:

New filesrc/handoff.js

A new file: read the repair task, check four gates, comment, and remember.

Added: import api, { route } from '@forge/api';
Added: import { kvs } from '@forge/kvs';
Added: import { LABELS, STORE, isIssueKey, settings } from './lib/config';
Added: import { paragraphs } from './lib/adf';
Added: import { comment } from './repairs/comment';
Added:
Added: // Events carry only some fields, so read the ones the gates need from Jira itself.
Added: async function readRepairTask(issueKey) {

Read the task’s labels, status and space, as the app.

Added: const response = await api.asApp().requestJira(route`/rest/api/3/issue/${issueKey}?fields=labels,status,project`);
Added: if (!response.ok) throw new Error(`Jira answered ${response.status} for ${issueKey}.`);
Added: const issue = await response.json();
Added: return issue.fields;
Added: }
Added:
Added: /** Sends a repair approved for the expert on, once. Runs on updated and assigned events. */
Added: export async function run(event) {

What runs each time a work item is updated or assigned.

Added: const issueKey = event.issue?.key;
Added: if (!isIssueKey(issueKey)) return;
Added: const { projectKey, handoffStatus } = settings();
Added: const fields = await readRepairTask(issueKey);
Added: if (fields.project?.key !== projectKey) return; // 1. a repair task in our space

Stop unless all four checks pass. This one: is it in our space?

Added: if (!fields.labels?.includes(LABELS.EXPERT)) return; // 2. marked for the expert
Added: if (fields.status?.name !== handoffStatus) return; // 3. approved for the expert
Added: if (await kvs.get(`${STORE.HANDOFF}${issueKey}`)) return; // 4. not sent before

Has it been handed over before? Storage remembers, so it never goes twice.

Added: await comment(issueKey, paragraphs('This repair is ready for the expert.'));

Hand it over: a comment on the task.

Added: await kvs.set(`${STORE.HANDOFF}${issueKey}`, { at: new Date().toISOString(), eventType: event.eventType });

Remember that it was sent, and when.

Added: console.log(`Handoff: sent ${issueKey} to the expert.`);
Added: }

Why: Agent handoff · Key-Value Store (KVS) · asApp()

Jira’s update events carry only some of a work item’s fields, and labels are not among them. So the function reads the labels, status and space from Jira itself instead of trusting the event. The record in the Key-Value Store is the fourth gate: it is written only after the comment succeeds.

Stuck?One common problem, and its fix
  • Moving a repair task to “Approved for expert” does nothing

    The handoff checks four things before it sends a repair on: the repair task is in your Workshop space, it has the needs-expert label, its status is exactly the one named by HANDOFF_STATUS in src/lib/config.js (by default “Approved for expert”), and it was not sent on before. The log says which check stopped it.

    Open in the FAQ

Step 2: A second trigger, for the handoff

See this on the Level 2 chart, in the step where the update event reaches the handoff trigger.

The new trigger listens for updates and assignments, and, like the bell since lesson 2, ignores what the app did itself. Its function is the second change:

Editmanifest.ymlChange 1 of 2

Listen for updates and assignments, but not the app’s own.

- avi:jira:created:issue
filter:
ignoreSelf: true
Added: - key: expert-handoff

A second trigger, for the handoff.

Added: function: handoff
Added: events:
Added: - avi:jira:updated:issue

Listen for changed work items, such as a status change.

Added: - avi:jira:assigned:issue

And for assigned ones.

Added: filter:
Added: ignoreSelf: true
llm:
- key: split-notes
model:
Editmanifest.ymlChange 2 of 2

The function it runs.

- key: checkin-worker
handler: worker.run
timeoutSeconds: 120
Added: - key: handoff
Added: handler: handoff.run
resources:
- key: main
path: src/frontend/index.jsx

Why: Product trigger · ignoreSelf · Agent handoff

Without ignoreSelf, the app’s own comment and edits could wake the trigger again. The fourth gate would still stop a second comment, but every edit would cost a run.

Stuck?2 common problems, and their fixes
  • I changed manifest.yml

    Check it, then deploy:

    RunTerminal
    forge lint
    forge deploy

    If the change adds a scope or an outside host, forge deploy stops and asks for approval. See Deploy stops after a permission change.

    Open in the FAQ
  • Moving a repair task to “Approved for expert” does nothing

    The handoff checks four things before it sends a repair on: the repair task is in your Workshop space, it has the needs-expert label, its status is exactly the one named by HANDOFF_STATUS in src/lib/config.js (by default “Approved for expert”), and it was not sent on before. The log says which check stopped it.

    Open in the FAQ

Step 3: Deploy

The app already has every scope this needs, so this is a minor version:

RunTerminal
npm run build:ui
forge deploy
Stuck?2 common problems, and their fixes
  • Deploy stops after a permission change

    That is expected, not an error. A new scope or outside host makes a new major version, which you approve. Then move each installation onto it. On your own developer site you are the admin, so you accept it yourself:

    RunTerminal
    forge deploy --approve MAJOR_VERSION_RULE
    forge install --upgrade
    Open in the FAQ
  • The tunnel is running while I deploy or upgrade

    Stop it with Ctrl+C first, then deploy or upgrade, then start it again. While the tunnel runs, Jira runs the code on your laptop instead of your last deploy, so if you deploy with it running you cannot tell which version you are testing. Changes to the manifest only reach the site through a deploy, never through the tunnel.

    Open in the FAQ

Step 4: Approve a repair for the expert

  1. Move the battery repair task to Approved for expert. Within a few seconds it gets a comment: “This repair is ready for the expert.”
  2. Move it to another column and back again. No second comment.
  3. Move a repair task without the needs-expert label to Approved for expert. Nothing happens.

forge logs shows one Handoff: line for it.

Stuck?One common problem, and its fix
  • Moving a repair task to “Approved for expert” does nothing

    The handoff checks four things before it sends a repair on: the repair task is in your Workshop space, it has the needs-expert label, its status is exactly the one named by HANDOFF_STATUS in src/lib/config.js (by default “Approved for expert”), and it was not sent on before. The log says which check stopped it.

    Open in the FAQ

Compare with yours: download the code after this lesson.

Type at least two letters.