
# The edge gate (shape 1)

Set `[auth] provider = "edge"` in `agentcloud.toml` and a gate in front of the app signs people in before any request reaches it. Requests that get through carry `x-agc-user-email` and `x-agc-user-name`; the app keeps no session and has no sign-in page.

Success: after `agc ship`, a fresh browser signs in, then lands in the app.

For an internal tool, pair the gate with an [audience](#audiences-who-can-open-an-app): `agc audience set org` limits the app to your organization's domains and members. New apps start at `anyone`, so the gate alone admits any Google account or email address.

## Execution contract

- **Before starting:** Use a target with an edge gate; for a team tool, check `agc audience` (default `anyone`).
- **Do:** Set `[auth] provider = "edge"`, read the identity headers, test with a signed-in smoke flow.
- **Success:** `agc status` reports `data.sign_in.mode` as `edge` and `data.audience` names the intended people.
- **If blocked:** For `ship.edge_unavailable`, use a supported sign-in shape or have the target owner configure its gate.
- **Human handoff:** Only an owner, or an agent working for one, sets the audience. Changing the provider on a live app waits for approval.

## Ask your agent

```text
Make this a private tool behind Agent Cloud's sign-in gate, open only to our
organization. No login pages in the app; show the signed-in person's name in the header.
```

The agent ships and runs `agc audience set org`, or, if you aren't an owner, gives you the console link for one.

## Steps

```toml
[auth]
provider = "edge"
```

Trust the headers only behind the gate, which is what `AGC_AUTH_PROVIDER=edge` tells you:

```ts
const behindGate = process.env.AGC_AUTH_PROVIDER === 'edge';

app.use('*', async (c, next) => {
  if (!behindGate) return c.text('This app runs behind the edge gate.', 500);
  c.set('user', { email: c.req.header('x-agc-user-email')!, name: c.req.header('x-agc-user-name')!,
    role: c.req.header('x-agc-user-role') ?? null }); // "owner" | "member" | null
  await next();
});

// Sign-out: fetch('/auth/sign-out', { method: 'POST' }); the gate's 401 {"signed_out":true} is success.
```

Key your rows on the email the gate sends (lowercased); it passes no user id. A smoke flow that runs signed in:

```toml
[[check.smoke]]
name  = "a teammate sees their own requests"
steps = ["SIGN IN pat@example.test", "GET /api/me -> 200"]
```

```bash
AGC_EDGE_DEV_USER=pat@example.test agc up --detach   # the mirror's test identity
agc check
agc ship
agc audience set org
```

### Webhooks behind the gate

A sender with no person behind it (Slack, a GitHub webhook) can't sign in, so list the paths it posts to beside the provider:

```toml
[auth]
provider = "edge"
public = ["/api/slack/*", "/hooks/github"]   # a trailing /* is a prefix; anything else is exact; at most 20
```

Those paths get no sign-in and no identity headers, so the app checks each sender's own signature (kept in `[secrets] required`) before acting. A release that opens a new path asks a person.

## How it works

**On Agent Cloud's own cluster**, a page load with no app session is sent to `https://agent.cloud/auth/apps/<app>/authorize?…&edge=1`. Agent Cloud signs the person in, applies the [audience](#audiences-who-can-open-an-app), and returns a one-time code to `https://<app host>/__agc/sso`; the gate checks `state`, sets its session `__Host-agc_edge` and rechecks the person each minute ([cookies, code and recheck](/docs/security/web-security-for-the-console-and-apps.md#edge-sign-in)).

**On an organization's target with its own sign-in**, a broker inside the identity provider's cookie domain reads the provider's session and hands the gate a one-time code. The provider's cookie and token never reach the app ([Your IdP at the edge](/docs/targets/your-idp-at-the-edge.md)).

| What the app sees | |
|---|---|
| Identity headers | The gate replaces any client-sent copies. App pods are reachable only through the ingress, so the headers can't be forged |
| `x-agc-user-role` | On Agent Cloud's own sign-in only: `owner` or `member` for a person in the app's organization, current within a minute. Everyone else gets none: treat that as an outsider |
| Health path | Set `health = "/health"` in `[service.web]`. It's ungated, so releases reach it |
| A `fetch` with no session | 401 `auth.required` ("Sign in first."), not a redirect: reload to sign in |
| Sign-out | `POST /auth/sign-out` and `/__agc/sign-out` end only the app session at the gate and never reach the app |
| Mirrors, rehearsals | No gate: on a mirror a local proxy sets the email and name to a [test identity](/docs/reference/app-runtime-contract.md#environment-variables) (none on `[auth] public` paths), and in a smoke flow [`SIGN IN`](/docs/checks/smoke-flows.md#sign-in) plays the gate |

## Audiences: who can open an app

Each app in an organization has an audience: `anyone`, `org`, `people` or `group`. `agc audience` reads it; `agc audience set` changes it, only for an agent working for an owner of the app or its organization (or an owner, in the app's settings, "Who can use it"). Agent Cloud decides who gets in; the app decides what each person may do.

```bash
agc audience                                          # data.audience: kind, label; may_change
agc audience set org                                  # the organization's verified domains and its members
agc audience set people ana@example.test bob@example.test
agc audience set group Data                           # needs the organization's provider
agc audience set anyone                               # public
```

Success: `data.audience.label` says who gets in, such as "people at @example.test, and Acme's members". A person outside it gets a 403 page ("camp-bookings is for its organization, and pat@example.test isn't in it. Ask the organization.") and never reaches the app.

| Audience | Console label | Who gets in |
|---|---|---|
| `anyone` | Anyone with Google or an email link | Anyone who signs in |
| `org` | `<Org>`: people at `@<domain>`, and its members | A verified address at an organization's verified domain, or a member at any address |
| `people` | Named people | The listed addresses, after Google, an email link or the provider verifies them |
| `group` | Named groups | People in one of the listed provider groups; needs a [connected provider](/docs/owners/company-sign-in-and-scim.md) |

| When | How the audience applies |
|---|---|
| At sign-in | Without a provider, people sign in with Google or an email link and are checked on return. With the organization's provider connected, any audience but `anyone` signs in through it |
| After a change | At the next sign-in, and within the minute recheck for an edge session |
| Each change | Recorded in the organization's Activity as `identity.audience` |

## Reference

`agc status` always shows `data.audience` (`kind`, `label`, `public: true` for `anyone`); `data.signin` names the provider unless it's `anyone`.
## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md).

| Code | Exit | Cause | Fix |
|---|---|---|---|
| `audience.not_owner` | 12 | The agent's person isn't an owner of the app or its organization | Give the person the `console_url` for an owner |
| `audience.needs_provider` | 1 | `group` with no provider connected | `agc audience set org` or `people`, or connect a provider |
| `audience.no_organization` | 1 | The app isn't in an organization | Move it to one in its settings |
| `cli.usage` | 2 | Names after `anyone` or `org`, or none after `people` or `group` | Fix the command; nothing was sent |
| `ship.public_needs_edge` | 1 | `[auth] public` without `provider = "edge"` | Set the provider, or remove `public` |
| `ship.bad_public` | 1 | A path that isn't plain, or over 20 | Prefixes end in `/*`; nothing under `/__agc` |
| `ship.edge_unavailable` | 1 | The app's target has no gate | Use Agent Cloud sign-in, or ask the target's owner |
| `ship.release_failed` (cause `health_missing`) | 1 | An edge app with no `health` path: the release saw only the sign-in. The previous release keeps serving | Set `health` in `[service.web]`, ship again |
| `ship.edge_domains` | 1 | The app has [custom domains](/docs/build/custom-domains.md) | Keep Agent Cloud sign-in, or remove the domains |

```json
{
  "ok": false, "product": "agent.cloud", "command": "audience set",
  "error": { "retryable": false, "code": "cli.usage",
    "message": "agc audience set org takes no names, and got \"ana@example.test\". Nothing was changed.",
    "fix": "Run `agc audience set org` alone, or to open the app to named addresses only, `agc audience set people <address> [<address>…]` (named groups: `agc audience set group <group> [<group>…]`).",
    "docs": "agc help audience" },
  "next": [{ "cmd": "agc audience", "why": "who can open the app now" }]
}
```

## Limits

Gate session: 8 hours on Agent Cloud's cluster; on a target, [its operator's setting](/docs/targets/your-idp-at-the-edge.md). Groups or people per app: 200. Public paths: 20.

## Agent notes

- Set `health` in `[service.web]` for an edge app, or the release fails with `ship.release_failed`.
- `401 {"signed_out":true}` after a sign-out is success, not an error.
- Don't add a session cookie or a login page to an edge app.
- `data.audience.public: true` means anyone can sign in. Don't filter by email domain in code; run `agc audience set org`, or relay `console_url` to an owner.
- Make admins from `x-agc-user-role`, never the first person to sign in.
- On a target with its own sign-in, the target's provider decides who gets in, not the audience.

## Related

- [Choosing a sign-in shape](/docs/build/choosing-a-sign-in-shape.md)
- [Same-site neighbours, CSRF and webhooks](/docs/security/web-security-for-the-console-and-apps.md#same-site-neighbours-and-webhooks)
- [Your IdP at the edge](/docs/targets/your-idp-at-the-edge.md)
- [Company sign-in (SSO) and SCIM](/docs/owners/company-sign-in-and-scim.md)
- [Organizations](/docs/owners/setting-up-an-organization.md#organizations-and-roles)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/the-edge-gate: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
