Skip to content

forcedotcom/sf-skills / plugins/builder/agentforce-adlc/skills/agentforce-d360-analyze

agentforce-d360-analyze

Verified

Data Cloud 360° view of a single Agentforce session. TRIGGER when user asks to trace, inspect, summarize, or describe a specific Agentforce session by session id (Agent Session UUID `019d…` or MessagingSession id `0Mw…`). Also triggers on session discovery — find/list/search sessions by time, agent, channel, outcome, or conversation text — when the user has no session id yet. DO NOT TRIGGER for design-time architecture questions (use agentforce-architecture-analyze instead) or for runtime perf/latency/SLO questions that require platform telemetry beyond Data Cloud.

FileKindSize
README.mdmarkdown6.2 KB
SKILL.mdskill md13.2 KB
assets/dc/app_generation.sqlother1.9 KB
assets/dc/content_category.sqlother1.5 KB
assets/dc/content_quality.sqlother1.3 KB
assets/dc/discover_sessions.sqlother2.3 KB
assets/dc/feedback.sqlother1.5 KB
assets/dc/feedback_details.sqlother1.3 KB
assets/dc/gateway_records.sqlother1.6 KB
assets/dc/gateway_request_llm.sqlother1.6 KB
assets/dc/gateway_request_metadata.sqlother1.5 KB
assets/dc/gateway_request_tags.sqlother1.4 KB
assets/dc/gateway_requests.sqlother3.4 KB
assets/dc/gateway_responses.sqlother1.7 KB
assets/dc/generations.sqlother1.9 KB
assets/dc/interactions.sqlother2.0 KB
assets/dc/messages.sqlother2.1 KB
assets/dc/messaging_session.sqlother1.7 KB
assets/dc/moment_interactions.sqlother1.4 KB
assets/dc/moments.sqlother1.5 KB
assets/dc/participants.sqlother1.8 KB
assets/dc/sessions.sqlother3.0 KB
assets/dc/steps.sqlother2.6 KB
assets/dc/tag_associations.sqlother1.7 KB
assets/dc/tag_definition_associations.sqlother1.4 KB
assets/dc/tag_definitions.sqlother1.8 KB
assets/dc/tags.sqlother1.3 KB
assets/dc/telemetry_spans.sqlother2.1 KB
references/artifacts.mdmarkdown2.7 KB
references/dc_dmo_fields.mdmarkdown42.1 KB
references/dc_pipeline_contract.mdmarkdown26.3 KB
scripts/_shared/__init__.pyscript122 B
scripts/_shared/cli_override.pyscript3.3 KB
scripts/_shared/fs_guard.pyexecutablescript13.2 KB
scripts/_shared/paths.pyscript5.8 KB
scripts/_shared/runtime.pyscript2.0 KB
scripts/_shared/sql.pyscript548 B
scripts/assemble_dc.pyscript69.6 KB
scripts/config.pyscript1.8 KB
scripts/dc.pyscript13.8 KB
scripts/discover_sessions.pyscript21.1 KB
scripts/fetch_dc.pyscript45.1 KB
scripts/render_dc.pyscript69.7 KB
scripts/resolve_session.pyscript10.8 KB
scripts/storage.pyscript3.1 KB
scripts/tests/__init__.pyscript0 B
scripts/tests/_bootstrap.pyscript483 B
scripts/tests/fixtures/__init__.pyscript0 B
scripts/tests/fixtures/synthetic_session.pyscript14.6 KB
scripts/tests/test_assemble_dc_bootstrap_and_mode.pyscript4.2 KB
scripts/tests/test_assemble_dc_gateway_direct.pyscript8.3 KB
scripts/tests/test_assemble_dc_gateway_direct_integration.pyscript6.4 KB
scripts/tests/test_assemble_dc_helpers.pyscript10.0 KB
scripts/tests/test_assemble_dc_integration.pyscript9.3 KB
scripts/tests/test_dc_and_resolve_session.pyscript26.7 KB
scripts/tests/test_default_target_org.pyscript18.8 KB
scripts/tests/test_discover_sessions.pyscript19.0 KB
scripts/tests/test_discover_sessions_grep_ci.pyscript8.2 KB
scripts/tests/test_fetch_dc_helpers.pyscript10.6 KB
scripts/tests/test_fetch_dc_identity.pyscript22.0 KB
scripts/tests/test_fetch_dc_main.pyscript9.4 KB
scripts/tests/test_fetch_dc_waterfall.pyscript8.6 KB
scripts/tests/test_fetch_dc_waterfall_full.pyscript11.2 KB
scripts/tests/test_identity_coherence.pyscript13.8 KB
scripts/tests/test_render_dc_branches.pyscript9.9 KB
scripts/tests/test_render_dc_gateway_direct.pyscript5.1 KB
scripts/tests/test_render_dc_helpers.pyscript10.5 KB
scripts/tests/test_render_dc_integration.pyscript7.7 KB
scripts/tests/test_render_dc_planner_llm_calls.pyscript11.8 KB
scripts/tests/test_render_dc_show_prompts_gating.pyscript8.7 KB
scripts/tests/test_resolve_from_disk.pyscript4.1 KB
scripts/tests/test_resolve_session_main.pyscript5.2 KB
scripts/tests/test_runtime_override.pyscript4.2 KB
scripts/tests/test_session_shape.pyscript3.5 KB
scripts/tests/test_session_shape_dropped_by_stdm.pyscript3.5 KB
scripts/tests/test_skill_md_prefix_block.pyscript6.8 KB

scripts/tests/test_fetch_dc_identity.py

View on GitHub
"""Tests for ``fetch_dc._resolve_identity`` + ``sf org display`` fallback.

Closes a real-world failure mode: ``sessions[0].ssot__InternalOrganizationId__c``
is occasionally null in SSOT materialization despite every other field
being populated (e.g. session ``019dface-0000-7000-8000-000000000001`` in
``my-org-alias``). The authenticated ``sf`` alias knows the org id
regardless, so we fall back to ``sf org display --target-org <alias> --json``
and parse ``.result.id``. If that also fails, a unified diagnostic
surfaces both failure modes.

Every test patches ``subprocess.run`` at the ``fetch_dc`` module level so
no real CLI is ever invoked.
"""
from __future__ import annotations

import json
import subprocess
import unittest
from types import SimpleNamespace
from unittest import mock

from . import _bootstrap  # noqa: F401  — sys.path setup

import fetch_dc  # type: ignore


_ORG_ALIAS = "my-org-alias"
_FULL_ORG_ID_18 = "00D0000000000000EA"
_EXPECTED_ORG_ID_15 = "00D000000000000"
_AGENT_API = "DemoAgent"
_AGENT_VER = "v5"


def _agent_participant() -> dict:
    return {
        "ssot__AiAgentSessionParticipantRole__c": "AGENT",
        "ssot__AiAgentApiName__c": _AGENT_API,
        "ssot__AiAgentVersionApiName__c": _AGENT_VER,
    }


def _sf_cli_success_stdout(org_id: str = _FULL_ORG_ID_18) -> str:
    """Shape mirrors the real ``sf org display --json`` payload."""
    return json.dumps({
        "status": 0,
        "result": {
            "id": org_id,
            "instanceUrl": "https://example.my.salesforce.com",
            "accessToken": "REDACTED",
        },
    })


class ResolveIdentityDmoPopulatedTests(unittest.TestCase):
    """Happy path — DMO field carries a valid 18-char id, no subprocess spawn."""

    def test_resolve_identity_uses_dmo_when_populated(self):
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [_agent_participant()]

        with mock.patch.object(fetch_dc.subprocess, "run") as m_run:
            org_id_15, agent, ver = fetch_dc._resolve_identity(
                sessions, participants, _ORG_ALIAS,
            )

        self.assertEqual(org_id_15, _EXPECTED_ORG_ID_15)
        self.assertEqual(agent, _AGENT_API)
        self.assertEqual(ver, _AGENT_VER)
        m_run.assert_not_called()


class ResolveIdentityDmoFallbackTests(unittest.TestCase):
    """DMO field null/empty/short — fall back to ``sf org display``."""

    def _patch_sf_cli_ok(self, org_id: str = _FULL_ORG_ID_18):
        return mock.patch.object(
            fetch_dc.subprocess, "run",
            return_value=SimpleNamespace(
                stdout=_sf_cli_success_stdout(org_id),
                stderr="",
                returncode=0,
            ),
        )

    def test_resolve_identity_falls_back_to_sf_cli_when_dmo_null(self):
        sessions = [{"ssot__InternalOrganizationId__c": None}]
        participants = [_agent_participant()]

        with self._patch_sf_cli_ok() as m_run:
            org_id_15, agent, ver = fetch_dc._resolve_identity(
                sessions, participants, _ORG_ALIAS,
            )

        self.assertEqual(org_id_15, _EXPECTED_ORG_ID_15)
        self.assertEqual(agent, _AGENT_API)
        self.assertEqual(ver, _AGENT_VER)
        # Exact argv shape matters — it's the public contract with the sf CLI.
        args, kwargs = m_run.call_args
        self.assertEqual(
            args[0],
            ["sf", "org", "display", "--target-org", _ORG_ALIAS, "--json"],
        )
        self.assertTrue(kwargs.get("check"))
        self.assertTrue(kwargs.get("capture_output"))
        self.assertTrue(kwargs.get("text"))

    def test_resolve_identity_falls_back_when_dmo_empty_string(self):
        sessions = [{"ssot__InternalOrganizationId__c": ""}]
        participants = [_agent_participant()]

        with self._patch_sf_cli_ok():
            org_id_15, _, _ = fetch_dc._resolve_identity(
                sessions, participants, _ORG_ALIAS,
            )
        self.assertEqual(org_id_15, _EXPECTED_ORG_ID_15)

    def test_resolve_identity_falls_back_when_dmo_shorter_than_15(self):
        sessions = [{"ssot__InternalOrganizationId__c": "shortid"}]
        participants = [_agent_participant()]

        with self._patch_sf_cli_ok():
            org_id_15, _, _ = fetch_dc._resolve_identity(
                sessions, participants, _ORG_ALIAS,
            )
        self.assertEqual(org_id_15, _EXPECTED_ORG_ID_15)


class ResolveIdentityBothSourcesFailTests(unittest.TestCase):
    """When both the DMO field AND the CLI fallback fail, surface both."""

    def test_resolve_identity_raises_when_both_sources_fail(self):
        sessions = [{"ssot__InternalOrganizationId__c": None}]
        participants = [_agent_participant()]

        err = subprocess.CalledProcessError(
            returncode=1,
            cmd=["sf", "org", "display", "--target-org", _ORG_ALIAS, "--json"],
            stderr="No AuthInfo found for name " + _ORG_ALIAS,
        )
        with mock.patch.object(fetch_dc.subprocess, "run", side_effect=err):
            with self.assertRaises(SystemExit) as ctx:
                fetch_dc._resolve_identity(
                    sessions, participants, _ORG_ALIAS,
                )

        msg = str(ctx.exception)
        # Must mention both failure modes so users know which to fix.
        self.assertIn("both DMO field and sf org display failed", msg)
        self.assertIn(_ORG_ALIAS, msg)
        self.assertIn("CalledProcessError", msg)


class ResolveIdentityNoAgentParticipantsTests(unittest.TestCase):
    """Recently-created sessions can land here while STDM is still
    materializing — the AGENT participant rows haven't been written yet.
    The error must hint at retry rather than implying a malformed session.
    """

    def test_resolve_identity_raises_with_retry_hint_when_no_agent_participants(self):
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        # No AGENT-role participants; only a USER row.
        participants = [{
            "ssot__AiAgentSessionParticipantRole__c": "USER",
            "ssot__AiAgentApiName__c": "",
            "ssot__AiAgentVersionApiName__c": "",
        }]

        with self.assertRaises(SystemExit) as ctx:
            fetch_dc._resolve_identity(sessions, participants, _ORG_ALIAS)

        msg = str(ctx.exception)
        # Identifying string for the error class.
        self.assertIn("no AGENT participants", msg)
        # New (Fix #6) wording — leads with retry, then falls back to malformed.
        self.assertIn("retry in a few minutes", msg)
        self.assertIn("STDM materialization", msg)

    def test_resolve_identity_raises_when_only_empty_field_participants(self):
        # AGENT-role row exists but agent_api_name + agent_version are blank.
        # Same outcome — no usable identity, same retry-hint error.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [{
            "ssot__AiAgentSessionParticipantRole__c": "AGENT",
            "ssot__AiAgentApiName__c": "",
            "ssot__AiAgentVersionApiName__c": "",
        }]

        with self.assertRaises(SystemExit) as ctx:
            fetch_dc._resolve_identity(sessions, participants, _ORG_ALIAS)

        self.assertIn("no AGENT participants", str(ctx.exception))


class ResolveIdentityUserRowFallbackTests(unittest.TestCase):
    """Some agent shapes (e.g. the OOTB ``MyAgent`` template) leave
    AGENT-row ``ssot__AiAgentApiName__c`` / ``ssot__AiAgentVersionApiName__c``
    as ``NOT_SET`` indefinitely, while USER-role rows correctly carry the
    api_name. The downstream session is otherwise complete (interactions,
    steps, generations all materialize), so refusing to run wastes signal.

    The fallback promotes the api_name from any participant row, stamps
    version=``v0`` (placeholder satisfying ``^v[0-9]+$`` per fs_guard), and
    proceeds. Tests guard against regression — a future "tighten this back
    up" refactor would re-break the MyAgent case silently.
    """

    def test_falls_back_to_user_row_api_name_when_agent_rows_not_set(self):
        # Mirrors a real session shape observed in production:
        # USER rows carry the api_name, AGENT rows are 'NOT_SET'.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "NOT_SET",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]

        org_id_15, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )

        self.assertEqual(org_id_15, _EXPECTED_ORG_ID_15)
        self.assertEqual(agent, "MyAgent")
        # Placeholder must satisfy the AGENT_VERSION_RE used by paths.session_dir.
        self.assertEqual(ver, "v0")

    def test_fallback_handles_blank_api_name_on_agent_row(self):
        # AGENT row api_name is empty string (rather than the literal
        # 'NOT_SET'). Both shapes mean the same thing per _NOT_SET.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",
                "ssot__AiAgentVersionApiName__c": "",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "",
                "ssot__AiAgentVersionApiName__c": "",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "MyAgent")
        self.assertEqual(ver, "v0")

    def test_fallback_picks_lexicographic_first_when_multiple_user_names(self):
        # Multi-agent handoff session where USER rows mention both agents.
        # The fallback uses sorted({...})[0] to match the dominant-agent
        # policy used elsewhere in the pipeline.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "ZAgent",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "AAgent",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "NOT_SET",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "AAgent")
        self.assertEqual(ver, "v0")

    def test_agent_row_with_only_version_not_set_still_uses_strict_path(self):
        # Defensive: AGENT row has a real api_name but version=NOT_SET. The
        # strict candidate filter rejects it, but the fallback should still
        # promote the api_name (avoiding a needless raise just because version
        # was missing on the AGENT row).
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [{
            "ssot__AiAgentSessionParticipantRole__c": "AGENT",
            "ssot__AiAgentApiName__c": "RealAgent",
            "ssot__AiAgentVersionApiName__c": "NOT_SET",
        }]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "RealAgent")
        self.assertEqual(ver, "v0")

    def test_fallback_rejects_api_name_with_invalid_shape(self):
        # Even though a non-empty api_name is present on a USER row, if it
        # doesn't satisfy ^[A-Za-z0-9_]+$ (fs_guard.API_NAME_RE) the fallback
        # MUST fall through to the original SystemExit. Without this guard,
        # paths.session_dir would later reject the dir with an opaque
        # ValidationError far from where the bad value originated.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        for bad_name in ("Service Agent", "agent-v1", "agent.v1", "héllo"):
            with self.subTest(bad_name=bad_name):
                participants = [{
                    "ssot__AiAgentSessionParticipantRole__c": "USER",
                    "ssot__AiAgentApiName__c": bad_name,
                    "ssot__AiAgentVersionApiName__c": "NOT_SET",
                }]
                with self.assertRaises(SystemExit) as ctx:
                    fetch_dc._resolve_identity(
                        sessions, participants, _ORG_ALIAS,
                    )
                # Falls through to the strict-path SystemExit, NOT a path-
                # validation traceback from fs_guard.
                self.assertIn("no AGENT participants", str(ctx.exception))

    def test_fallback_skips_invalid_picks_valid_when_both_present(self):
        # If multiple api_names are seen, the regex filter should drop the
        # invalid ones and the fallback should still succeed with a valid one.
        # Defense against a real session that mixes a malformed display-name
        # row (e.g. legacy data) with a clean USER row.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "Service Agent",  # invalid
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",  # valid
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]
        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "MyAgent")
        self.assertEqual(ver, "v0")

    def test_fallback_returned_version_matches_agent_version_regex(self):
        # Belt-and-braces: the placeholder version we return MUST match
        # the ^v[0-9]+$ shape enforced by paths.session_dir / fs_guard
        # (see scripts/_shared/paths.py:_validate_agent_triple). If a
        # future change picks a different placeholder ('NOT_SET',
        # 'unknown', 'v0.0', etc.) directory creation downstream will
        # blow up. Test the exact contract here without taking a
        # cross-skill import dependency.
        import re

        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [{
            "ssot__AiAgentSessionParticipantRole__c": "USER",
            "ssot__AiAgentApiName__c": "MyAgent",
            "ssot__AiAgentVersionApiName__c": "NOT_SET",
        }]
        _, _, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertRegex(ver, r"^v[0-9]+$")


class ResolveIdentityCrossRoleVersionHarvestTests(unittest.TestCase):
    """Real-data shape: AGENT participant rows had api_name + version both
    NOT_SET, but the USER rows carried the real (api_name, version) pair.
    The prior fallback only harvested api_name from non-AGENT rows and
    stamped version=``v0``, so the dir landed at ``<agent>__v0/`` while
    ``assemble_dc`` reconciled the real version into the tree from
    ``session.identity`` — dir + tree out of sync.

    The cross-role harvest closes that gap: when the strict AGENT-only
    candidate list is empty, scan ALL participant rows for a valid
    (api_name, version) pair before falling back to the v0 placeholder.
    Real version wins; placeholder is genuinely a last resort.
    """

    def test_user_row_carries_real_version_dir_lands_correctly(self):
        # The exact shape found on a live session. AGENT rows: NOT_SET on
        # both fields. USER rows: real pair.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",
                "ssot__AiAgentVersionApiName__c": "v24",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",
                "ssot__AiAgentVersionApiName__c": "v24",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "NOT_SET",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "MyAgent")
        # MUST be the real version, NOT the v0 placeholder — that's the
        # whole point of the cross-role harvest.
        self.assertEqual(ver, "v24")

    def test_falls_through_to_v0_placeholder_when_no_role_carries_version(self):
        # Builder Previewer shape: api_name lands on a USER row but no
        # participant row carries a real version. Must drop through to
        # the v0 placeholder branch (which keeps the pipeline runnable
        # for that legitimate session shape).
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "MyAgent",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "NOT_SET",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "MyAgent")
        self.assertEqual(ver, "v0")

    def test_cross_role_rejects_malformed_version(self):
        # Someone wrote a malformed version into a USER row (e.g.
        # 'v1.0', '1', '24'). Must NOT adopt it — fs_guard will reject
        # the dir later. Fall through to v0 placeholder so the pipeline
        # still runs and the assemble_dc reconcile can promote later
        # if a real version shows up elsewhere in the tree.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        for bad_version in ("v1.0", "1", "24", "version-24", ""):
            with self.subTest(bad_version=bad_version):
                participants = [
                    {
                        "ssot__AiAgentSessionParticipantRole__c": "USER",
                        "ssot__AiAgentApiName__c": "MyAgent",
                        "ssot__AiAgentVersionApiName__c": bad_version,
                    },
                    {
                        "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                        "ssot__AiAgentApiName__c": "NOT_SET",
                        "ssot__AiAgentVersionApiName__c": "NOT_SET",
                    },
                ]
                _, agent, ver = fetch_dc._resolve_identity(
                    sessions, participants, _ORG_ALIAS,
                )
                self.assertEqual(agent, "MyAgent")
                # Bad version rejected; placeholder kicks in.
                self.assertEqual(ver, "v0")

    def test_strict_agent_row_precedence_beats_user_row(self):
        # When BOTH AGENT and USER rows carry valid (api_name, version)
        # pairs, the strict-AGENT path runs first and the cross-role
        # branch is unreachable. Pin this precedence so a future
        # refactor can't accidentally reorder the resolution. If the
        # AGENT row's pair were ever shadowed by a USER row's pair, the
        # dominant-agent invariant on handoff sessions breaks.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "AAgent",  # lex-first across both rows
                "ssot__AiAgentVersionApiName__c": "v1",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "ZAgent",  # lex-LAST overall
                "ssot__AiAgentVersionApiName__c": "v9",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        # AGENT-row precedence: ZAgent/v9 wins, NOT AAgent/v1.
        self.assertEqual(agent, "ZAgent")
        self.assertEqual(ver, "v9")

    def test_cross_role_picks_lex_first_when_multiple_valid_pairs(self):
        # Handoff with two valid (api_name, version) pairs across USER
        # rows. The strict-path dominant-agent rule applies: lex-first
        # wins. This keeps the cross-role harvest aligned with the
        # AGENT-only path's selection policy.
        sessions = [{"ssot__InternalOrganizationId__c": _FULL_ORG_ID_18}]
        participants = [
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "ZAgent",
                "ssot__AiAgentVersionApiName__c": "v9",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "USER",
                "ssot__AiAgentApiName__c": "AAgent",
                "ssot__AiAgentVersionApiName__c": "v3",
            },
            {
                "ssot__AiAgentSessionParticipantRole__c": "AGENT",
                "ssot__AiAgentApiName__c": "NOT_SET",
                "ssot__AiAgentVersionApiName__c": "NOT_SET",
            },
        ]

        _, agent, ver = fetch_dc._resolve_identity(
            sessions, participants, _ORG_ALIAS,
        )
        self.assertEqual(agent, "AAgent")
        self.assertEqual(ver, "v3")


if __name__ == "__main__":
    unittest.main()
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall