From agentforce-adlc
Analyzes production Agentforce agent behavior using STDM session traces and Data Cloud queries. Investigates failures, regressions, performance issues, and reproduces problems in preview.
npx claudepluginhub salesforceairesearch/agentforce-adlc --plugin agentforce-adlcThis skill is limited to using the following tools:
Improve Agentforce agents using session trace data and live preview testing.
Analyzes production Agentforce agent behavior using STDM session traces and Data Cloud. Triggers for querying sessions, investigating failures, regressions, performance issues, or reproducing in preview.
Builds, modifies, debugs, and deploys Salesforce Agentforce AI agents using Agent Script, .agent files, aiAuthoringBundle metadata, and sf CLI commands like generate/preview/publish/test.
Provides guidance on Salesforce Agentforce AI agents, Atlas Reasoning Engine, components, and 2025 features for autonomous automation.
Share bugs, ideas, or general feedback.
Improve Agentforce agents using session trace data and live preview testing.
Three-phase workflow:
sf agent preview to simulate problematic conversations live.agent file directly, validate, publish, verifypython3 with python on Windows./tmp/ with $env:TEMP\ (PowerShell) or %TEMP%\ (cmd).jq with python -c "import json,sys; ..." if jq is not installed.Gather these inputs before starting:
.agent file, typically force-app/main/default/aiAuthoringBundles/<AgentName>/<AgentName>.agent. Auto-detect if not provided.Determine intent from user input:
Before any STDM query, resolve the user-provided agent name against the org to get the exact MasterLabel and DeveloperName:
sf data query --json \
--query "SELECT Id, MasterLabel, DeveloperName FROM GenAiPlannerDefinition WHERE MasterLabel LIKE '%<user-provided-name>%' OR DeveloperName LIKE '%<user-provided-name>%'" \
-o <org>
MasterLabel = display name used by STDM findSessions and Agent Builder UI (e.g. "Order Service")DeveloperName = API name with version suffix used in metadata (e.g. "OrderService_v9")--api-name flag for sf agent preview/activate/publish uses DeveloperName without the _vN suffix (e.g. "OrderService")Store these values:
AGENT_MASTER_LABEL -- for findSessions() agent filterAGENT_API_NAME -- DeveloperName without _vN suffix, for sf agent CLI commandsPLANNER_ID -- the Salesforce record ID for this agentStep 1 -- Search locally:
find <project-root>/force-app/main/default/aiAuthoringBundles -name "*.agent" 2>/dev/null
If the user provided an agent file path, use that directly. Otherwise, search for files matching AGENT_API_NAME.
Step 2 -- If not found locally, retrieve from the org:
sf project retrieve start --json --metadata "AiAuthoringBundle:<AGENT_API_NAME>" -o <org>
Known bug:
sf project retrieve startcreates a double-nested path:force-app/main/default/main/default/aiAuthoringBundles/.... Fix it immediately after retrieve:
if [ -d "force-app/main/default/main/default/aiAuthoringBundles" ]; then
mkdir -p force-app/main/default/aiAuthoringBundles
cp -r force-app/main/default/main/default/aiAuthoringBundles/* \
force-app/main/default/aiAuthoringBundles/
rm -rf force-app/main/default/main
fi
Step 3 -- Validate the retrieved file:
Read the .agent file and verify it has proper Agent Script structure:
system: block with instructions:config: block with developer_name:start_agent or subagent blocks with reasoning: instructions:instructions: content (not identical across subagents)Store the resolved path as AGENT_FILE for Phase 3.
Before running any STDM query, determine the correct Data Cloud Data Space API name.
sf api request rest "/services/data/v63.0/ssot/data-spaces" -o <org>
Note: sf api request rest is a beta command -- do not add --json (that flag is unsupported and causes an error).
The response shape is:
{
"dataSpaces": [
{
"id": "0vhKh000000g3DjIAI",
"label": "default",
"name": "default",
"status": "Active",
"description": "Your org's default data space."
}
],
"totalSize": 1
}
The name field is the API name to pass to AgentforceOptimizeService.
Decision logic:
'default' and note it as an assumption.status: "Active" entries.<name>".Store the selected name value as DATA_SPACE for all subsequent steps.
After deploying the helper class (step 1.0), run a quick probe to verify the STDM Data Model Objects exist in Data Cloud:
sf apex run -o <org> -f /dev/stdin << 'APEX'
ConnectApi.CdpQueryInput qi = new ConnectApi.CdpQueryInput();
qi.sql = 'SELECT ssot__Id__c FROM "ssot__AiAgentSession__dlm" LIMIT 1';
try {
ConnectApi.CdpQueryOutputV2 out = ConnectApi.CdpQuery.queryAnsiSqlV2(qi, '<DATA_SPACE>');
System.debug('STDM_CHECK:OK rows=' + (out.data != null ? out.data.size() : 0));
} catch (Exception e) {
System.debug('STDM_CHECK:FAIL ' + e.getMessage());
}
APEX
If STDM_CHECK:FAIL: STDM is not activated. Inform the user and switch to Phase 1-ALT:
STDM (Session Trace Data Model) is not available in this org. To enable: Setup -> Data Cloud -> Data Streams and verify "Agentforce Activity" is active. Proceeding with fallback: test suites + local traces.
If STDM_CHECK:OK, proceed to Phase 1 (STDM path).
When STDM is not available, use test suites and sf agent preview --authoring-bundle with local trace analysis.
| Data source | When to use | Pros | Cons |
|---|---|---|---|
| STDM (Phase 1) | Historical production analysis | Real user data, volume | Requires Data Cloud, 15-min lag |
| Test suites + local traces (Phase 1-ALT) | Dev iteration, orgs without STDM | Instant, full LLM prompt, variable state | Preview only, no real user data |
sf agent test list --json -o <org>
sf agent test run --json --api-name <TestSuiteName> --wait 10 --result-format json -o <org> | tee /tmp/test_run.json
JOB_ID=$(python3 -c "import json; print(json.load(open('/tmp/test_run.json'))['result']['runId'])")
sf agent test results --json --job-id "$JOB_ID" --result-format json -o <org>
If no test suite exists, derive utterances: one per non-entry subagent (from description: keywords), one per key action, one guardrail test, one multi-turn test.
--authoring-bundle (local traces)Run each test utterance through preview to generate local trace files:
sf agent preview start --json --authoring-bundle <BundleName> -o <org> | tee /tmp/preview_start.json
SESSION_ID=$(python3 -c "import json; print(json.load(open('/tmp/preview_start.json'))['result']['sessionId'])")
sf agent preview send --json --session-id "$SESSION_ID" --authoring-bundle <BundleName> \
--utterance "$UTT" -o <org> | tee /tmp/preview_response.json
sf agent preview end --json --session-id "$SESSION_ID" --authoring-bundle <BundleName> -o <org>
Trace file location: .sfdx/agents/{BundleName}/sessions/{sessionId}/traces/{planId}.json
| Issue type | Trace command |
|---|---|
| Subagent misroute | jq -r '.plan[] | select(.type=="NodeEntryStateStep") | .data.agent_name' "$TRACE" |
| Action not called | jq -r '.plan[] | select(.type=="EnabledToolsStep") | .data.enabled_tools[]' "$TRACE" |
| LOW adherence | jq -r '.plan[] | select(.type=="ReasoningStep") | {category, reason}' "$TRACE" |
| Variable capture fail | jq -r '.plan[] | select(.type=="VariableUpdateStep") | .data.variable_updates[]' "$TRACE" |
| Vague instructions | jq -r '.plan[] | select(.type=="LLMStep") | .data.messages_sent[0].content' "$TRACE" |
DefaultTopic trace quirk: With --authoring-bundle, the root .topic field often shows "DefaultTopic" even when routing works. Always use NodeEntryStateStep.data.agent_name for the real subagent chain.
Entry answering directly (SMALL_TALK pattern): If start_agent trace shows SMALL_TALK grounding and transition tools visible but none invoked, add "You are a router only. Do NOT answer questions directly." to start_agent instructions.
Classify issues using the categories in references/issue-classification.md. After presenting findings, automatically proceed to agent config evidence analysis.
Full STDM query details, Apex service deployment, and response parsing: see
references/stdm-queries.md
Deploy AgentforceOptimizeService Apex class to the org. Check if already deployed first:
sf data query --json --query "SELECT Id, Name FROM ApexClass WHERE Name = 'AgentforceOptimizeService'" -o <org>
If not deployed, copy from skill directory and deploy. See references/stdm-queries.md for full steps.
Query recent sessions using findSessions(). Parse DEBUG|STDM_RESULT: from the Apex debug log. If findSessions returns empty, switch to Phase 1-ALT.
Use getMultipleConversationDetails() for up to 5 sessions (most recent first). Returns turn-by-turn data with messages, steps, topics, and action results.
When LOW adherence detected, use getLlmStepDetails() to get the actual LLM prompt and response.
Use getAggregatedMetrics() for high-level health dashboard: session rates, top intents, quality distribution, RAG averages.
Use getMomentInsights() for intent summaries, quality scores (1-5), and retriever metrics per session.
Use runObservabilityQuery() for targeted RAG analysis: KnowledgeGap, Hallucination, RetrievalQuality, AnswerRelevancy, Leaderboard.
Render turn-by-turn timeline from ConversationData JSON for each session.
Full issue pattern table and classification categories: see
references/issue-classification.md
Check each session for: action errors, subagent misroutes, missing actions, wrong inputs, variable capture failures, no transitions, slow actions, LOW adherence, abandoned sessions, dead subagents, publish drift, dead hub anti-pattern, entry answering directly, and safety issues.
Priority: P1 = action errors, misroutes, LOW adherence; P2 = missing actions, variable bugs, knowledge gaps; P3 = performance, abandoned sessions.
Present sessions analyzed, issues grouped by root cause category, and uplift estimate. Then automatically proceed to analyze the .agent file to confirm root causes.
Full structural analysis checks, cross-reference procedures, and publish drift detection: see
references/issue-classification.md
Retrieve the .agent file from the org, run automated checks (subagent count vs action blocks, dead hub detection, orphan actions, cross-subagent variable dependencies), and cross-reference STDM symptoms against the file structure.
Full preview procedures, trace diagnosis commands, and classification criteria: see
references/reproduce-reference.md
Build one test scenario per confirmed issue from Phase 1. Run each through sf agent preview with --authoring-bundle (generates local traces). Run each scenario 3 times and classify:
| Verdict | Criteria |
|---|---|
[CONFIRMED] | Same failure in 3/3 runs |
[INTERMITTENT] | Failure in 1-2 of 3 runs |
[NOT REPRODUCED] | Passes in 3/3 runs |
Only [CONFIRMED] and [INTERMITTENT] issues proceed to Phase 3.
Key commands:
sf agent preview start --json --authoring-bundle <Name> -o <org>
sf agent preview send --json --session-id "$SID" --utterance "<text>" --authoring-bundle <Name> -o <org>
sf agent preview end --json --session-id "$SID" --authoring-bundle <Name> -o <org>
Trace location: .sfdx/agents/{Name}/sessions/{sessionId}/traces/{planId}.json
Full procedures for pre-flight checks, fix mapping, instruction principles, regression prevention, deployment chain, verification, safety re-verification, and test case creation: see
references/improve-reference.md
Verify all action targets exist and are registered in the org before editing. If targets are missing, present options: deploy stubs, remove actions, register via UI, or proceed with routing-only fixes.
Map each confirmed issue to a fix location in the .agent file (description, instructions, actions, bindings, transitions). Use the Edit tool for targeted changes. Follow instruction principles: name actions explicitly, state pre-conditions, scope tightly, keep persona in system: only.
Establish baseline before editing. Make minimal edits. Test immediately after each edit. One fix per publish cycle. Check cross-subagent dependencies. Test adjacent subagents.
Read the .agent file, edit with the Edit tool (tabs for indentation), show the diff.
# Validate (dry run)
sf agent validate authoring-bundle --json --api-name <AGENT_API_NAME> -o <org>
# Publish (compile + deploy + activate)
sf agent publish authoring-bundle --json --api-name <AGENT_API_NAME> -o <org>
If publish fails, use deploy + activate fallback (note: incomplete -- does not propagate reasoning: actions: to live metadata).
Run Phase 2 scenarios post-fix. Check trace for correct routing, grounding, tools, and variables. After 24-48 hours, re-run Phase 1 to compare against baseline.
Re-run safety review (Section 15 of /developing-agentforce) on the modified .agent file. Revert any changes that introduce BLOCK findings.
Create regression test cases from confirmed issues in Testing Center YAML format. Deploy with sf agent test create and verify all previously-broken scenarios pass.
| Reference | Contents |
|---|---|
references/stdm-queries.md | STDM query procedures, Apex service deployment, response parsing |
references/issue-classification.md | Issue pattern table, root cause categories, structural analysis checks |
references/reproduce-reference.md | Phase 2 preview procedures, trace diagnosis, classification criteria |
references/improve-reference.md | Phase 3 editing, deployment chain, verification, safety, test cases |
references/stdm-schema.md | DMO field schemas, data hierarchy, quality notes, agent name resolution |