For the complete documentation index, see llms.txt. This page is also available as Markdown.

Instrument Apex

The instrument-apex skill adds Triton logging to an Apex class or trigger — transactions, catch blocks, DML and callout payloads — then optionally deploys it.

The instrument-apex skill reads an Apex class or trigger, works out what kind of code it is, and adds Triton 2.0 instrumentation that follows the same patterns you'd write by hand — the entry-point template, resume-first transactions, logNow in every catch, dmlResults on partial DML, callout payloads, and flush at the right boundaries. Then it shows you the diff and, if you want, deploys.

Invocation is agent-specific. In agents that support slash commands (Claude Code, Cursor) you invoke it as /pharos:instrument-apex <path>; in others you point the agent at this skill and give it the path. See Running these in your agent. The /pharos: form below is a readable stand-in for "run this skill."

Usage

/pharos:instrument-apex <path> [--area <Area>] [--type <Type>] [--deploy]
Argument
Required
Meaning

<path>

✅

The .cls or .trigger file to instrument. If omitted, the skill asks.

--area

The business/functional Area for these logs. If omitted, the skill infers it from the code and matches related code. Accepts any String — enum values are just examples.

--type

Technical classification. Defaults to Backend; inferred per method (e.g. BackendCall for callouts). Accepts any String.

--deploy

Deploy the file after you accept the diff. Without it, the skill asks at the end.

Logs are created by passing a fluent TritonBuilder to Triton.log(...) (buffered) or Triton.logNow(...) (immediate). Builders come from Triton.t — the template-derived builder that auto-stamps the current transaction id — or from Triton.makeBuilder() for a fresh one.

What it instruments

The skill classifies the file's structure first, because the transaction and flush pattern depends on it — a trigger handler, an @AuraEnabled controller, a batch, a queueable, an @future, and a plain service class each get treated differently. Test classes (@isTest) are left alone entirely.

From there it applies:

  • Entry-point template. At the start of the entry-point method (never a shared static {} block — the template is a shared static and the last writer wins), it sets the template with the Category, Type, and Area, then creates each log from Triton.t.

  • Resume-first transactions. It always resumes an existing transaction before starting a new one, per the structure (see below).

  • Try/catch logging. Every catch gets a Triton.logNow(...) with .exception(e) before the original throw — the flow is preserved, only logging is added.

  • DML result logging. After partial-success DML (allOrNone = false), it adds .dmlResults(results). A failure becomes a DMLResult log at ERROR with an issue; when every row succeeded, nothing is emitted, so the skill never wraps it in an if.

  • Callout / integration logging. HTTP calls get .integrationPayload(req, res) and Type.BackendCall — the category stays the default Apex.

  • System.debug → Triton. Every System.debug(...) in non-test code becomes a buffered Triton.debug(...). (Debug lines are safe in production — verbosity is governed by Log Level metadata.)

  • Performance marks, where a span earns its place. For a stretch of work that should show up as a span rather than as one timed log, the skill wraps it in Triton.startMark() / Triton.endMark() (closed in a finally), using Triton.startTriggerMark() in trigger handlers. Everything logged inside nests under the mark, and names are derived from the stack trace, so no strings are hand-typed. Marks are not added to every entry point; a one-off duration on a single log is still timed by hand. See Performance Marks in Apex.

  • Flush at boundaries. Triton.flush() is added at the end of triggers, each batch execute chunk and finish, Queueable/Schedulable execute, @future, @AuraEnabled, and REST/invocable methods — never inside a loop.

  • Related objects. Record ids flowing through a method are attached with .relatedObject(id) / .relatedObjects(ids), prioritized on error and DML logs.

Template initialization

The entry-point method opens with the template:

Transaction management (resume-first, per structure)

The goal is one correlated transaction across the whole call chain, so the skill always resumes first.

Synchronous entry points (@AuraEnabled, REST, @InvocableMethod) resume a caller-supplied id if there is one:

The sanctioned signature exception. Adding an optional transactionId parameter to an entry-point method — so it can stitch into an instrumented LWC's transaction — is the one time the skill will change a method signature. It's flagged explicitly in the diff. Everywhere else, signatures, access modifiers, and return types are left untouched. If you'd rather not touch the signature at all, the skill can use Triton.withCache() at entry instead, which carries the id via platform cache with zero param changes. Pick one mechanism per call chain and keep the LWC side consistent (the LWC skill passes a param named transactionId).

Other structures each get their own handling:

  • Trigger handlers guard the start (if (Triton.TRANSACTION_ID == null) Triton.startTransaction();) so before/after and recursion don't reset the id, and log affected ids with .relatedObjects(...), built null-safely — see Trigger handlers below.

  • Batchable classes implement Database.Stateful and take the caller's id in the constructor, but only store it there: the constructor runs in the caller's transaction, so startTransaction() there would overwrite the caller's id. start resumes that id (or starts one if there's none), resumeTransaction(this.txId) re-establishes it at the top of each execute and in finish, and the class flushes per execute chunk and in finish (each chunk is its own transaction).

  • Queueable / Schedulable take a String txId constructor arg, resume it at the top of execute, and flush before returning.

  • @future takes a String transactionId param (only primitives cross the boundary), resumes it, and flushes before returning.

  • Async dispatch sites thread the current id through the hop:

  • Private / service methods never start, resume, or stop a transaction — they use the ambient one set by the entry point. Triton.stopTransaction() is only ever called at a true end-of-chain boundary (batch finish, top-level entry-point completion).

Try/catch — log and rethrow

.exception(e) sets the type, ERROR level, summary, stack trace, details, and flags issue creation — so no redundant .level(...). Downgrading it — Triton.warning(Triton.t.exception(e)) — keeps the exception detail but withdraws the issue; an explicit .createIssue() keeps it. A custom summary must chain after .exception(e), or it gets overwritten:

DML result logging

Callout / integration logging

Trigger handlers: logging never fails the write

A trigger runs inside someone else's DML. On a managed-package object — or any object a system or integration user writes to — a throw from your handler rolls back a write you don't own and may not be able to test as. Logging must never be the reason that happens.

What Triton guarantees. Triton.log and the level methods, logNow, flush, flushTop, the Flow Log action, and the LWC endpoint catch their own failures. If Pharos refuses a publish, those logs are dropped and the failure is written to the Apex debug log at ERROR; the business write goes ahead.

What the skill still takes care of in your code:

  • Null-safe builder arguments. The builder expression runs in your code before Triton is called, so Triton can't catch what throws there — Trigger.newMap.keySet() in before insert, record.Parent__r.Name on an unqueried relationship, e.getCause().getMessage() when there's no cause. Record ids are built like this:

  • Governor limits. System.LimitException can't be caught by anyone. In a 200-record trigger the skill buffers with Triton.log(...) — never logNow in a loop — flushes once at the end of the handler, and keeps per-record details lean.

  • No new try/catch around business logic. Where a catch already exists, it logs and keeps the existing throw or return.

  • One publish on managed-package objects. Triton.log(...) plus a single Triton.flush() as the handler's last statement, rather than logNow. The audit flags logNow and flush on these paths for review.

The test it adds. The skill adds a test that performs the triggering DML as a low-privilege user and asserts the write landed:

Know what this doesn't prove. System.runAs enforces record sharing only, not object or field permissions, and a user you can create in a test can't reproduce a hidden platform integration user. A passing test shows the handler survives a restricted human user; Triton's never-throw guarantee is what covers the rest. To prove your code survives a refused publish, see TritonTestFactory.failPublishing().

Review the diff

Before writing anything, the skill presents a summary — the structure it classified, the template it set, how it handled transactions, and counts of try/catch, DML, callout, System.debug, and flush changes (plus any sanctioned signature change) — followed by the key changes. You answer:

  • yes — apply and write the file.

  • modify — give free-form corrections; the skill re-applies and asks again.

  • skip — stop without writing.

Deploy

The instrumented content is written back to the original file (never a new one). Then, with --deploy or on confirmation:

A "status": "Succeeded" reports the deployed component; failures show the component-failures table and stop without reverting your file.

Where to go next

Last updated