Commit Graph
5 Commits
Author SHA1 Message Date
Jaayden Halko 9c53f3d7ee feat: add per-user chat project pins (#32025)
Adds per-user chat project pins, the first backend layer of the Projects
page and pinned projects design. Nothing in the UI reads pins yet; the
sidebar and Projects page PRs stacked on top do.

- **Schema:** migration `000647_chat_project_pins` adds the
`chat_project_pins` table (`user_id`, `project_id`, `pin_order`,
`source` of `migration`/`default`/`user`, `created_at`). Unpinning
deletes the row. The migration pins every project to its owner in
today's sidebar order (`lower(name), id`); shared projects are not
pinned.
- **Shared rule for which projects a user's list matches:** the
migration also adds `chat_project_ids_for_user(viewer_id,
include_grants)`: owned projects, plus projects whose ACL names the user
or one of their groups (including Everyone) in an organization they
belong to. The project list and `GetChats` call it instead of each
keeping a copy of that predicate, and so do #32026's queries. Postgres
inlines it, so the ACL indexes still apply (same results and timings as
before on a 10,000-user, 305-project test database).
- **Endpoints:** `PUT` and `DELETE
/api/experimental/organizations/{organization}/chats/projects/{project}/pin`
pin and unpin. `PUT` takes no body and pins at the end; a project that
is already pinned keeps its place. `DELETE` removes the pin. The caller
needs read access to a project they can use (anything else is a 404) and
`update_personal` on themselves, because a pin is their personal data;
an API key scoped without `user:update_personal` gets a 403. Pins are
personal preferences, so they are not audited.
- **Reordering:** `PUT /api/experimental/chats/projects/pins/order`
takes `{"project_ids": [...]}`, the pinned projects in their new order.
The server saves the listed pins in that order and keeps every other pin
after them, in its current order. The table also holds pins the caller
can no longer see (lost access, sharing disabled), and this way no write
has to work out which pins those are. IDs the caller has not pinned are
ignored, so a project unpinned in another tab does not fail a reorder.
The route sits outside `/organizations/{organization}` because one pin
list spans organizations. It needs only `update_personal` on the caller.
Pins and reorders take a per-user advisory lock. The stored `pin_order`
only sorts the pins: it can have gaps after an unpin or a deleted
project, until the next reorder.
- **New projects** get a `default` pin for their creator inside the
create transaction, so an API key scoped to create projects needs the
pin's two permissions as well, and gets a 403 without them.
- **Chat list:** `exclude_own_project_chats=true` leaves out the
caller's own unpinned chats in projects the caller can use; clients list
those per project with `project_id`. Project pins don't change the
result, so pinning or unpinning never changes the chat list. Shared
chats and the caller's pinned chats are still returned, and so are chats
in projects the caller can no longer use, because no project list
reaches them. The filter sits in the same `WHERE` as the cursor, so
pages stay full.

User-facing docs land with the UI PR, since these routes stay under
`/api/experimental` with `x-apidocgen skip` like the other project
routes.

<details>
<summary>Verification</summary>

`make gen` (clean diff), `go build ./...`, `make lint/go`, and the pin,
chat list, migration (`TestMigration000647PinsOwnedProjects`,
`TestMigrateUpWithFixtures`), dbauthz and codersdk tests pass. `make
pre-commit` passes except `lint/actions/actionlint`, which needs a newer
local mise and fails the same way on `main`.

The dogfood UAT below ran when a pin took a `pin_order` position. It was
not repeated after reordering became its own call; the Go tests cover
that call, including overlapping reorders and pins.

Dogfood UAT ran against a dev deployment with a premium license (groups
and a second org created through the licensed APIs): upgrade from `main`
with case-sensitive names, pin/reorder/unpin/re-pin, 400 and 404 paths,
an 84-request concurrent pin burst with no 5xx, the chat list filter in
each sharing mode (as `pinned_projects_only`, before the rename to
`exclude_own_project_chats`), and cascade on project delete. Upgrade
output:

```text
 username |  name   | pin_order |  source
----------+---------+-----------+-----------
 usera    | Alpha   |         1 | migration
 usera    | beta    |         2 | migration
 usera    | gamma   |         3 | migration
 userb    | bown    |         1 | migration
 userb    | Bshared |         2 | migration
```

Creating a project afterwards appends a `default` pin at position 4, and
usera has no pin on userb's shared `Bshared`.

</details>

> [!NOTE]
> Most of this PR was written by AI agents (Xum, with UAT run through
Coder Agents). The verification above is from those runs.
2026-12-02 13:57:14 +07:00
Jaayden Halko 1f1aa6ae3c feat: add chat project template schema and queries (#32309)
Adds the database layer for attaching workspace templates to chat
projects: the schema, the queries, their authorization, and the audit
type. Nothing calls the new queries yet, and the existing create and
update handlers pass the new parameters unset. The API, the limit's
enforcement, and the agent tools follow in the PRs above.

- Attachments live in a `chat_project_templates` table (`project_id`,
`template_id`, `created_by`, `created_at`), deleted with the project or
the template. `chat_projects` gains `limit_templates` (default `false`),
`default_template_id`, `default_template_version_preset_id`, and
`default_parameters jsonb`. A project stores a default preset or default
parameter values, not both.
- `InsertChatProjectTemplates` needs project `update` and `use` on every
template. It skips templates that are already attached, and templates
outside the project's organization, so an attachment never crosses
organizations. `DeleteChatProjectTemplate` needs project `update` and
template `read`, and clears the project's default when it detaches the
default template. Both checks are in dbauthz.
- `GetChatProjectTemplatesByProjectIDs` is authorized through the
project, and leaves out deleted, deprecated, and agent-disabled
templates. It does not check the caller's access to each template, so
callers do that before they expose one.
- `UpdateChatProjectByID` changes the template settings only when the
request sets them, so an update that omits them cannot write back a
stale snapshot.
- `AuditableChatProjectTemplates` is the audit form of a project's
attached templates, logged as a write to the project. The new
`chat_projects` columns are tracked in the project's own audit diff,
with the default parameter values masked because they may hold
credentials.

**NOTE**: the migration adds one table and four columns and changes no
existing rows. Existing projects keep unlimited template access.

Stack (merge top to bottom):
- #32309 👈
- #30754
- #32310
- #30755
- #30756
- #30898
2026-11-17 21:59:01 +07:00
Garrett Delfosse 6ebefb8325 feat: add chat project sharing API (#30439)
API for chat project sharing (experiment `chat-projects`). Builds on
#30479 (merged).

Owners share a project with users, groups, or the whole organization
(the Everyone group, ID = organization ID). Chats in a shared project
stay private.

| Role    | Project and instructions | Memories   | ACL  | Delete |
|---------|--------------------------|------------|------|--------|
| `use`   | read                     | read/write | read | no     |
| `admin` | read/write               | read/write | edit | no     |

Only the owner, and administrators whose role grants delete, can delete
a project.

- **API:** `GET`/`PATCH .../chats/projects/{project}/acl`, same shapes
as `/chats/{chat}/acl`, plus SDK methods. Project responses include the
caller's `permissions` (`update`, `delete`, `share`); the UI hides edit
and delete accordingly.
- **Listing:** `GET /chats/projects` returns owned and shared projects.
Removes `GetChatProjectsByOwnerID`.
- **Chat access:** creating a chat in a project, and loading its memory
and instructions on each turn, requires the chat owner to own the
project or have an ACL grant (`chatd.ChatProjectUsableBy`). Role grants
don't count. If the grant is removed, existing chats lose memory and
instructions on their next turn.
- **Validation:** users and groups must be in the project's
organization; keys must be canonical UUIDs; callers can't change their
own role; the owner can't be added. Migration `000624` drops a user's
grants when they leave the organization, so rejoining doesn't restore
access.
- **Instructions:** readers who can't edit get 403 instead of 404, and
the UI hides editing.
- **Agent prompt:** memories are visible to and editable by sharees, so
the agent is told not to save secrets or personal details.

Known gap: project deletion still detaches chats, and a sharee's open
sidebar keeps showing them until reload. #30683 replaces this.

Not included: share notifications, `/acl/available`, sharing UI.

<details>
<summary>Decision log</summary>

- `use` grants memory read/write over HTTP, because agents in a sharee's
chats can already write memories. Making the agent read-only for sharees
was the alternative (@f0ssel chose sharees changing memories through
use).
- An administrator with `share` can grant themselves access and then use
the project. Accepted to keep access control simple (@f0ssel).
</details>

> Generated by Coder Agents.
2026-10-19 11:32:26 +00:00
Garrett Delfosse 2b9fad8bba feat: add chat project sharing database layer (#30479)
Database and authorization layer for chat project sharing (experiment
`chat-projects`), part 1 of 2. #30439 adds the API on top of it. Nothing
here changes behavior: projects start with empty ACLs, no endpoint
writes them yet, and deleting a project still detaches its chats.
Project deletion is in #30683.

- **Schema:** migration `000621` adds `user_acl` and `group_acl` jsonb
columns to `chat_projects`, typed as `ChatACL` like chats, with GIN
indexes. It also adds the `chat_project:share` API key scope.
- **Queries:**
- `GetChatProjectsOwnedOrSharedWithUserID`: owned projects plus projects
with an ACL entry for the user directly, through a group, or through the
Everyone group (whose ID is the organization ID). Entries count only for
members of the project's organization. Keys match regardless of actions,
so dbauthz filters the rows.
- `IsChatProjectAccessibleByUserID`: whether a user owns a project or
holds a `read` or `*` grant on it, for code running under a system
subject. With sharing disabled, dbauthz answers owner-only.
- `GetChatProjectByIDForUpdate`: lets the ACL update read, modify, and
write in one transaction.
  - `UpdateChatProjectACLByID`.
- **RBAC:**
- New `share` action on `chat_project`. dbauthz requires it to update
the ACL.
- `ChatProject.RBACObject` attaches the project ACL. Memory objects get
a derived ACL: project `read` grants memory read, and `read` with
`update` grants memory create and delete. Wildcard grants count. Read
sharees change memories through their chats' agents, which #30439 gates.
Project instructions authorize through the same object, so a `read`
grant reads them and an `update` grant edits them.
- `--disable-chat-sharing` also covers projects: ACLs are ignored, the
ACL update is rejected, and roles deny `chat_project:share`. The help
text and chat sharing docs say so.
- **Audit:** `user_acl` and `group_acl` changes are tracked.
- **Tests:**
- Querier tests run the access queries both raw and through dbauthz
(owner, direct user, group, Everyone, wildcard grants, a member of
another org, grants without read).
- RBAC object and memory ACL tests, with an authorizer check, in both
sharing modes.
- dbauthz cases including the disabled guards, and the
`chat_project:share` negation in the roles test.

The generated TypeScript changes only add the new scope value.

> Generated by Coder Agents.
2026-10-14 10:42:00 -04:00
Garrett DelfosseandClaude Opus 5.5 7cd1b2040b feat: add chat projects API (#29242)
- Experiment, behind `chat-projects` (off by default). First of three
stacked PRs: #29301 (memory), #29583 (UI). This PR is API only; the
screenshots below are from the UI in #29583.
- CRUD under
`/api/experimental/organizations/{organization}/chats/projects/{project}`.
`GET /api/experimental/chats/projects` lists the caller's projects
across all organizations.
- `project_id` on chat create and as a chat list filter. A chat joins a
project only at creation; there is no move.
- Projects are identified by ID. Names are display labels (64
characters, description 1024) with no uniqueness constraint. An optional
`icon` URL (usually an emoji image under `/emojis`) is capped at 256
characters and audited.
- A user can own at most 100 projects across all organizations; creating
another answers 409. Creates count and insert under a per-owner advisory
lock so concurrent requests cannot overshoot.
- Projects are private to their `owner_id`. Members may only create
their own, and a chat can only be bound to a project its owner owns. An
unreadable or foreign project on chat create answers 404 so IDs cannot
be probed; a project from another organization answers 400.
- Permissions ride on the `agents-access` role next to chats (after
#30004), so whoever can use Coder Agents can use projects. Owners and
org admins reach every project.

### Examples

Create a project in an organization:

```http
POST /api/experimental/organizations/{organization}/chats/projects
{"name": "Platform", "description": "Infra and deploy work", "icon": "/emojis/1f680.png"}
```

```json
{
  "id": "7c0e4a3e-5b1f-4f7d-9a51-2f3c8f6d2b10",
  "organization_id": "bb640d07-ca8a-4869-b6bc-ae61ebb2fda1",
  "owner_id": "0ed9befc-4911-4ccf-a8e2-559bf72daa94",
  "name": "Platform",
  "description": "Infra and deploy work",
  "icon": "/emojis/1f680.png",
  "created_at": "2026-09-30T15:04:05Z",
  "updated_at": "2026-09-30T15:04:05Z"
}
```

List the caller's projects across organizations with `GET
/api/experimental/chats/projects` (an array of the object above). Update
with `PATCH .../chats/projects/{project}` and any of `name`,
`description`, `icon`; delete with `DELETE`, which detaches its chats.

Create a chat in the project, and list only its chats:

```http
POST /api/v2/chats
{"organization_id": "bb640d07-…", "project_id": "7c0e4a3e-…", "content": [{"type": "text", "text": "Plan the deploy"}]}

GET /api/v2/chats?project_id=7c0e4a3e-…
```

At the limit:

```json
409 {"message": "You can have at most 100 chat projects. Delete a project to create another."}
```

<details>
<summary>Screenshots (UI from #29583)</summary>

![Create
project](https://raw.githubusercontent.com/coder/coder/f0ssel/chat-projects-assets/v3/pr1-01-create-project-dialog.png)
![Sidebar
folders](https://raw.githubusercontent.com/coder/coder/f0ssel/chat-projects-assets/v3/pr1-02-sidebar-folders.png)
![Project
page](https://raw.githubusercontent.com/coder/coder/f0ssel/chat-projects-assets/v3/pr1-03-project-page.png)
![Folder
menu](https://raw.githubusercontent.com/coder/coder/f0ssel/chat-projects-assets/v3/pr1-05-folder-menu.png)
![Another member sees no
projects](https://raw.githubusercontent.com/coder/coder/f0ssel/chat-projects-assets/v3/pr5-member-sees-no-projects.png)

</details>

---
Generated with Coder Agents on behalf of @f0ssel.

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 11:49:21 -04:00