Skip to content

fix(sharing): let a non-owner admin add themselves to an agent's access - #4762

Open
mayuriphad wants to merge 2 commits into
Abilityai:devfrom
mayuriphad:fix/4666-admin-self-share
Open

mayuriphad wants to merge 2 commits into
Abilityai:devfrom
mayuriphad:fix/4666-admin-self-share

Conversation

@mayuriphad

Copy link
Copy Markdown

Description

A non-owner admin could not add themselves to an agent's access. share_agent_endpoint and SharingMixin.share_agent both refused any share whose target matched the caller's email. The refusal is meant to stop an owner sharing an agent that is already on their roster. It also blocked a non-owner admin, and for them a self-share is the only way an agent reaches their Workspace, because the admin role does not widen the roster.

Both layers now compare the target with the agent owner's email:

  • db/agent_settings/sharing.py: a new get_agent_owner_email(agent_name), also exposed on database.db. share_agent refuses a share with the owner, whoever calls it. A related problem is gone too: on dev, an admin could share an agent with its own owner, which writes a redundant row.
  • routers/sharing.py: the owner gets 400 Cannot share an agent with yourself: you own it, so it is already on your Workspace. An admin targeting the owner gets 400 Cannot share an agent with its owner: .... A self-add skips invite_on_share, so it sends no email and returns invitation: "not_needed". It is still audited like any other share, and still behind require_interactive / reject_non_interactive_principal.

This PR is backend only. I left the two UI items in the acceptance criteria for a follow-up: the "Add myself" action on the access UI and the admin empty-state copy in Portal.vue. With this change, an admin can already add themselves by entering their own email in the existing share field.

Written with an AI assistant. I have reviewed the change and can explain every line.

Related Issue

Fixes #4666

Journey Impact

Journey Impact: none: corrects which email the existing share refusal compares against; no new or changed user promise in the journey catalog

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Testing

  • I have tested this locally
  • New tests added (if applicable)
  • All existing tests pass
  • Every new test executes the changed path

tests/unit/test_4666_admin_self_share.py drives the real sharing router and DB layer over the unit DB, using the same harness as test_ent459_operator_invite.py. It covers:

  • a non-owner admin self-add succeeds, sends no invitation and is audited;
  • an admin still can't share with the owner;
  • the owner's self-share is refused with the new message;
  • the owner can still share with an admin;
  • a non-admin non-owner still gets the existing 404 from the owner gate;
  • the DB layer gets the same checks.

test_837_workspace_only_floor.py: its fake DB gains get_agent_owner_email, which the route now calls.

I ran these on a Linux GitHub Actions job (Python 3.13, tests/requirements-test.txt). The job ran the new file, the touched test and the other share and roster tests: test_ent459_operator_invite.py, test_ent719_inline_layer_matches_route_rule.py, test_ent357_workspace_owned_roster.py and test_4579_credential_primary_reach.py. Result: 210 passed.

Mutation: with src/ reverted to dev, test_4666_admin_self_share.py gives 6 failed, 2 passed. The 2 that still pass are the owner sharing with an admin and the non-owner 404, which are the controls.

Checklist

  • My code follows the project's style guidelines
  • I have updated the documentation (if applicable)
  • I have not committed any sensitive data (API keys, credentials, etc.)
  • My commits are signed off (git commit -s) — required for contributors outside the core team
  • If this PR was written with an AI assistant, I say so above, and I have read and can explain every line
  • I have added appropriate logging for new functionality

The share route and SharingMixin.share_agent refused any share whose
target was the caller's email. That was meant to stop an owner sharing an
agent that is already on their roster, but it also blocked a non-owner
admin from adding themselves, which is the only way an agent reaches
their Workspace (the admin role does not widen the roster).

Compare the target against the agent owner's email instead, in both
layers. The owner is still refused, now with a message that says why. A
self-add sends no invitation email and is audited like any other share.

Fixes Abilityai#4666

Signed-off-by: mayuriphad <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@vybe

vybe commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

merge-train note: validated at 61d1df673. The backend fix is sound and mutation-proven. Fixes #4666 would have closed an issue with two UI criteria unbuilt, so by operator ruling at Gate 1 those two criteria moved to #4768 and this PR rides as the backend half. Two small things recorded on #4768 rather than asked of this PR: the roster half of criterion 1 has no test, and the is_self_add invite-skip branch is not separated by any test. docs/memory/feature-flows/agent-sharing.md (lines ~199 and ~438) still shows the old check and message.

@vybe

vybe commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

merge-train: not riding this train after all; rides the next one once fixed. Tier 1 ran for the first time on this head (61d1df673) and regression diff is red on one test the PR owns:

test_4674_sync_on_loop_ratchet.TestTheLiveTree::test_the_ratchet — src/backend/routers/sharing.py: 22 blocking calls in async functions against a baseline of 20. The two new sync DB reads in share_agent_endpoint (db.get_agent_owner_email and the caller-email lookup) run on the event loop. The ratchet (#4674) only shrinks, so the baseline is not to be regenerated upward: hop the new reads to a thread (await asyncio.to_thread(...)) or fold them into a call the handler already makes. Check locally with python3 scripts/ci/check_sync_on_loop.py.

The Gate 1 ruling stands: this is accepted as the backend half of #4666, with the UI criteria in #4768.

@vybe vybe added the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Nov 24, 2026
The two DB reads added to share_agent_endpoint ran on the event loop and
grew routers/sharing.py past the Abilityai#4674 sync-on-loop baseline. Hop both to
a thread and read the caller's email once; the file now has 19 blocking
calls, so the baseline drops from 20 to 19.

Signed-off-by: mayuriphad <[email protected]>
@mayuriphad

Copy link
Copy Markdown
Author

Fixed in 9ce1b7c. Both new reads in share_agent_endpoint (db.get_agent_owner_email and the caller-email lookup) now run through await asyncio.to_thread(...), and the caller email is read once instead of twice. routers/sharing.py now has 19 blocking calls in async functions, below the baseline of 20, so I lowered its baseline entry to 19 to keep the ratchet tight.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants