Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions deploy/org/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,24 @@ right to a private chat than anyone else (the policy's `operator.sees_private` i
into their own My space. The refusals read `Only the person who owns this chat can move it, and they need permission to manage it.`
(the default) and `Only the person who owns this chat, or someone with permission to manage it, can move it.` Any other word is
refused with a 422.
- **Who approves a tool call.** `PUT /v1/org/policy` takes `"chats": {"approvals_by": "owner"}` or `"chats": {"approvals_by": "answer"}`.
Absent or empty means `owner`: a command a shared chat wants to run, which needs approval under its owner's approval mode,
waits for the owner whatever role anyone else holds, on both of the chat's approval doors (`ResolveConsent`, and an answer to
the chat's question of kind `consent` sent through `ResolveQuestion`); a guest who tries either reads `This chat is shared with
you. Only its owner can do that.` `answer` lets a person who holds the `answer` verb (the `edit` and `full` roles) allow one
call once, or refuse it, by either door. Allowing a tool for the rest of the chat, putting the approval away, revising an
answer and the chat's own approval setting stay with the owner, and a person who cannot answer is refused as always. The
guest's approval is recorded as the chat's latest asker. The setting is re-read on each heartbeat, so it reaches a window
that is already open. Any other word is refused with a 422 that names the two.
- **Who may start a chat that is not running.** `PUT /v1/org/policy` takes `"chats": {"wake_by": "view"}`, `"run"` or `"owner"`.
Absent or empty means `view`: opening a shared chat that is not running starts it for its owner, in its owner's place among
the chats they may have running, for anyone who can see it (a `view` guest's `--once` included), which is what the server always
did. `run` lets only a person who holds `run` start it, and `owner` only its owner; anyone else reads `This chat is not running.
Starting it takes permission to send messages in it. Ask its owner.` or `This chat is not running. Only its owner can start it.
Ask them to open it.`, and the chats' host is never asked to start it. A chat that is already running opens for everyone who can
see it under every setting, and the owner's own window is not affected. The rule is checked when the window opens; if the
chat's state cannot be read, `run` and `owner` refuse with the "not answering" sentence. Any other word is refused with a 422
that names the three.
- **The hosting policy.** `PUT /v1/org/policy` takes `"hosting": {"local_sessions": "allow|warn|refuse",
"max_session_cost_usd": 0}`. Empty `local_sessions` means `refuse` for an organization that has chats' machine and
`allow` for one that does not; the list reports the value that applies, with `words` to show for `refuse` (that
Expand All @@ -438,9 +456,10 @@ right to a private chat than anyone else (the policy's `operator.sees_private` i
Known limit of this version: a guest's `Steer` or prompt is refused by the chat itself, in the chat's own words about
the keyboard, while the owner's window holds the keyboard, because the guest's window does not take it on arrival; it
goes through when the owner has no window open, or after the guest sends `Take` (allowed with `run`, and the owner's
window is told). `Interrupt` and `ResolveQuestion` are not affected. A tool-use approval stays the owner's, so a
command that needs one waits for the owner. The proper fix for both is a guest class inside the engine itself, which
is a change to the wire's own protocol and not made here.
window is told). `Interrupt` and `ResolveQuestion` are not affected, except that an answer to a tool approval is the
owner's unless `chats.approvals_by` says `answer`. A tool-use approval stays the owner's by default, so a command that
needs one waits for the owner. The proper fix for the keyboard is a guest class inside the engine itself, which is a
change to the wire's own protocol and not made here.

### Turning it on

Expand Down
15 changes: 15 additions & 0 deletions docs/changes/unreleased/2927-configurable-choices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
kind: fixed
title: A shared hosted chat's tool approvals stay with its owner, and two policy settings loosen that
pr: 2927
surface: [factory, chat, docs]
invalidates:
- "A guest of a shared hosted chat who held the answer permission, which the `edit` role does, could approve a tool call by sending the chat's approval question an answer, and the chat then ran the command as its owner, although the documentation said a command that needs approval waits for the owner. Now both doors an approval has stay with the owner by default, and the guest reads the same sentence as before: `This chat is shared with you. Only its owner can do that.` Answering the chat's other questions is unchanged."
- "Who approves a tool call in a shared hosted chat could not be set. Now the organization's policy has `chats.approvals_by`: `owner`, which an absent value also means and which is the behaviour above, or `answer`, which lets a person who holds the answer permission allow one tool call once or refuse it. Allowing a tool for the rest of the chat, putting the approval away and the chat's own settings stay with the owner."
- "Anyone who could see a shared hosted chat that was not running started it for its owner by opening it, and no setting could change that. Now the policy has `chats.wake_by`: `view`, which an absent value also means and which is the behaviour from before, `run`, which lets only a person who may send messages in the chat start it, or `owner`. Under the last two a guest is told the chat is not running and to ask its owner, and nothing is started."
---

Both settings are the operator's to set with `PUT /v1/org/policy`, are refused with a sentence naming the allowed words
when they are not one of them, and show in `GET /v1/org/policy` once set. An organization that sets neither sees no
difference except the first line above. The keyboard rule for a guest's message while the owner's window is open, and
how a new share reaches an open desktop window, are not settings here.
6 changes: 4 additions & 2 deletions docs/design/app-v3/PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,10 @@ An engine whose hello carries `org_access` serves an organization's access polic
grants on its things. `factory.org_policy` `GET /v1/org/policy` answers the policy to any
member: `{rev, roles: [{name, verbs}], owner_role, org_role, defaults: {<kind>: {visibility}},
sharing: {who}, operator: {role, sees_private, may_govern}, agents: {may_create, may_share,
may_grant, default_scope, scopes?}, chats?: {move_by}}` (`chats` is present only when set, and
`move_by` is `owner`, the default, or `manage`: who may move a chat), and the default policy when none was ever saved, which
may_grant, default_scope, scopes?}, chats?: {move_by?, approvals_by?, wake_by?}}` (`chats` is present only when set, and
each field only when set: `move_by` is `owner`, the default, or `manage`, who may move a chat; `approvals_by` is `owner`, the
default, or `answer`, who approves a tool call in a shared hosted chat; `wake_by` is `view`, the default, `run` or `owner`, who
may start a hosted chat that is not running), and the default policy when none was ever saved, which
is how the server behaved before policies existed. A role is a named bundle of verbs from a
closed list (`view`, `comment`, `edit`, `run`, `steer`, `answer`, `share`, `manage`), every
role holds `view`, and the four names `view`, `comment`, `edit` and `full` are kept. Only
Expand Down
13 changes: 9 additions & 4 deletions docs/organizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ organization starts with (the operator can change their bundles; the names stay)
What a guest can do in a hosted chat follows from the verbs: `view` watches and reads; `steer` also interrupts a turn
and redirects it; `answer` also answers a question the chat is waiting on; `run` also sends messages, files and
commands. `manage` also stops or deletes the chat. Everything else (approving a tool use, settings, opening the
chat's files) is the owner's whatever the role.
chat's files) is the owner's whatever the role, with one setting: `chats.approvals_by` set to `answer` lets a person who
holds `answer` allow one tool use once, or refuse it (never allow a tool for the rest of the chat).

**The owner owns the chat.** It runs on the owner's credentials, spending limits and GitHub identity. A person who may
send messages acts with the owner's GitHub identity, the owner's files and the owner's limits, and the server records
Expand Down Expand Up @@ -146,7 +147,9 @@ The organization's policy (`GET` and `PUT /v1/org/policy`; only the operator cha
`CODEAF_HOSTED_SESSION_CAP_USD` (five dollars). The monthly limit every person's spend is held to is set by the operator in Settings (twenty dollars by default).

The same policy holds the roles, whether new chats start private (the default for chats and hosted chats), who may share,
who may move a chat (`chats.move_by`: `owner`, the default, or `manage`) and what bots and agents may do.
who may move a chat (`chats.move_by`: `owner`, the default, or `manage`), who approves a tool call in a shared chat
(`chats.approvals_by`: `owner`, the default, or `answer`), who may start a chat that is not running (`chats.wake_by`:
`view`, the default, `run` or `owner`) and what bots and agents may do.

A **team server does not trust its own computer.** A request from the server's own machine with no sign-in is refused
like any other (a solo server, a desktop window's own, still trusts it). `loopback_trust` is off for a team server; the
Expand Down Expand Up @@ -312,11 +315,13 @@ It answers with an `access_token` the same way.
- A guest's message or steer waits while the owner's window holds the keyboard: the chat itself refuses it in its own words
about the keyboard. It goes through when the owner has no window open, or after the guest takes the keyboard (allowed
with `run`). Interrupting and answering are not affected. A tool-use approval stays with the owner, so a command that
needs one waits for the owner.
needs one waits for the owner, unless the policy's `chats.approvals_by` is `answer`.
- A guest who runs `codeaf chat --org ... --session ... --once` on a chat that is not running starts it for its owner in that
one-shot mode, even when the guest's own message is refused. The owner's window then reads `this conversation is already
open in a different interactive or headless mode; close it before retrying` until the chat is stopped (the owner's stop, or
`POST /v1/hosted/sessions/<chat>/stop`) or stops itself when idle.
`POST /v1/hosted/sessions/<chat>/stop`) or stops itself when idle. The policy's `chats.wake_by` set to `run` or `owner`
keeps a person who cannot send messages in the chat (or anyone but its owner) from starting it at all: they are told the
chat is not running and to ask its owner, and nothing starts. The default, `view`, is the behaviour described here.
- By default only the owner moves a chat between My space and a workstream; the policy's `chats.move_by` set to `manage` lets
people with the `manage` verb on the chat move it too (never into their own My space, never a bot).
- `local_sessions` `allow` and `warn` are reported and stored; nothing in a client acts on them yet.
Expand Down
85 changes: 85 additions & 0 deletions internal/factory/acl/chats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,88 @@ func TestChatMoveRuleTakesOnlyItsTwoWords(t *testing.T) {
t.Fatalf("the setting is not written under chats.move_by: %s", data)
}
}

// Contract: who approves a tool call in a shared chat, and who starts a chat that is not running, are policy settings
// whose defaults are what the server did before they existed: the owner approves, and anyone who can see the chat opens
// it. A policy with no `chats` section, whether the default policy or one stored before the settings existed, reads that
// way, is still valid, and writes back without them.
func TestChatApprovalAndWakeRulesAreTodaysBehaviourUntilTheOrganizationChangesThem(t *testing.T) {
if got := DefaultPolicy().Chats.ApprovalsByFor(); got != ApprovalsByOwner {
t.Fatalf("default policy: who approves a tool call = %q, want %q", got, ApprovalsByOwner)
}
if got := DefaultPolicy().Chats.WakeByFor(); got != WakeByView {
t.Fatalf("default policy: who starts a chat = %q, want %q", got, WakeByView)
}
var old Policy
if err := json.Unmarshal([]byte(organizationPolicyBeforeChatRules), &old); err != nil {
t.Fatal(err)
}
if old.Chats.ApprovalsByFor() != ApprovalsByOwner || old.Chats.WakeByFor() != WakeByView {
t.Fatalf("a policy stored before the settings existed reads approvals %q, wake %q", old.Chats.ApprovalsByFor(), old.Chats.WakeByFor())
}
if err := old.Validate(Policy{}, nil); err != nil {
t.Fatalf("a policy stored before the settings existed is no longer valid: %v", err)
}
// Saying the default out loud is the same behaviour, and is written down as said.
p := DefaultPolicy()
p.Chats = Chats{ApprovalsBy: ApprovalsByOwner, WakeBy: WakeByView}
if p.Chats.ApprovalsByFor() != ApprovalsByOwner || p.Chats.WakeByFor() != WakeByView {
t.Fatalf("the defaults said out loud read %q and %q", p.Chats.ApprovalsByFor(), p.Chats.WakeByFor())
}
}

// Contract: approvals_by takes `owner` and `answer`, wake_by takes `view`, `run` and `owner`, any other word is refused
// with a sentence that names the words that are allowed, and a setting is written under `chats` by its own name.
func TestChatApprovalAndWakeRulesTakeOnlyTheirWords(t *testing.T) {
for _, tc := range []struct {
set Chats
wantApproval, wantWake string
}{
{Chats{}, ApprovalsByOwner, WakeByView},
{Chats{ApprovalsBy: "owner", WakeBy: "owner"}, ApprovalsByOwner, WakeByOwner},
{Chats{ApprovalsBy: "answer", WakeBy: "run"}, ApprovalsByAnswer, WakeByRun},
{Chats{WakeBy: "view"}, ApprovalsByOwner, WakeByView},
} {
p := DefaultPolicy()
p.Chats = tc.set
if err := p.Validate(Policy{}, nil); err != nil {
t.Fatalf("%+v is refused: %v", tc.set, err)
}
if p.Chats.ApprovalsByFor() != tc.wantApproval || p.Chats.WakeByFor() != tc.wantWake {
t.Errorf("%+v resolves to %q and %q, want %q and %q", tc.set, p.Chats.ApprovalsByFor(), p.Chats.WakeByFor(), tc.wantApproval, tc.wantWake)
}
}
var validation *ValidationError
for _, tc := range []struct {
name string
set func(string) Chats
words []string
}{
{"approvals_by", func(w string) Chats { return Chats{ApprovalsBy: w} }, []string{"owner", "answer"}},
{"wake_by", func(w string) Chats { return Chats{WakeBy: w} }, []string{"view", "run", "owner"}},
} {
for _, word := range []string{"anyone", "manage", "Answer", "edit"} {
p := DefaultPolicy()
p.Chats = tc.set(word)
err := p.Validate(Policy{}, nil)
if !errors.As(err, &validation) {
t.Errorf("%s %q: %v", tc.name, word, err)
continue
}
for _, allowed := range tc.words {
if !strings.Contains(err.Error(), allowed) {
t.Errorf("%s %q is refused with %q, which does not name %q", tc.name, word, err, allowed)
}
}
}
}
p := DefaultPolicy()
p.Chats = Chats{ApprovalsBy: ApprovalsByAnswer, WakeBy: WakeByRun}
data, err := json.Marshal(p)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), `"chats":{"approvals_by":"answer","wake_by":"run"}`) {
t.Fatalf("the settings are not written under chats by their own names: %s", data)
}
}
58 changes: 56 additions & 2 deletions internal/factory/acl/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,20 @@ func (h Hosting) Words(hasChatHost bool) string {
}
}

// Chats holds the organization's rules for the chats its people keep. There is one so far: who may move a chat to another
// workstream or into My space.
// Chats holds the organization's rules for the chats its people keep: who may move a chat to another workstream or into
// My space, and, for a chat that runs on the organization's own server and is shared with other people, who may approve
// one of its tool calls and who may start it when it is not running. Every field is optional, and an empty one means the
// rule the server had before the field existed, so a policy that never mentions a rule is unchanged by it.
type Chats struct {
// MoveBy is MoveByOwner or MoveByManage. Empty means MoveByOwner, so a policy stored before this field existed, and
// a policy that never mentions it, keep the rule the server always had.
MoveBy string `json:"move_by,omitempty"`
// ApprovalsBy is ApprovalsByOwner or ApprovalsByAnswer. Empty means ApprovalsByOwner: a tool call a shared chat wants
// to make waits for its owner, whatever role anyone else holds.
ApprovalsBy string `json:"approvals_by,omitempty"`
// WakeBy is WakeByView, WakeByRun or WakeByOwner. Empty means WakeByView: anyone who can see a shared chat opens it
// when it is not running, and it starts for its owner.
WakeBy string `json:"wake_by,omitempty"`
}

const (
Expand All @@ -140,6 +148,26 @@ const (
MoveByManage = "manage"
)

const (
// ApprovalsByOwner keeps every approval of a tool call with the chat's owner. It is the default.
ApprovalsByOwner = "owner"
// ApprovalsByAnswer lets anyone who holds the answer verb on a shared chat allow one tool call once, or refuse it.
// Allowing a tool for the rest of the chat, and every other setting of the chat, stays with the owner. Under the
// starting roles that is whoever the chat is shared with at `edit` or `full`.
ApprovalsByAnswer = "answer"
)

const (
// WakeByView lets anyone who can see a shared chat open it when it is not running, which starts it for its owner.
// It is the default, and what the server did before the rule was a setting.
WakeByView = "view"
// WakeByRun lets only a person who may send messages in the chat (the run verb) open it when it is not running.
// Someone who can only watch it sees a running chat, and is told to ask its owner for one that is not.
WakeByRun = "run"
// WakeByOwner lets only the chat's owner start it. Everyone else sees it while it is running.
WakeByOwner = "owner"
)

// MoveByFor is the rule that applies, with an empty setting read as the default.
func (c Chats) MoveByFor() string {
if c.MoveBy == "" {
Expand All @@ -148,6 +176,22 @@ func (c Chats) MoveByFor() string {
return c.MoveBy
}

// ApprovalsByFor is the rule that applies, with an empty setting read as the default.
func (c Chats) ApprovalsByFor() string {
if c.ApprovalsBy == "" {
return ApprovalsByOwner
}
return c.ApprovalsBy
}

// WakeByFor is the rule that applies, with an empty setting read as the default.
func (c Chats) WakeByFor() string {
if c.WakeBy == "" {
return WakeByView
}
return c.WakeBy
}

// Policy is the organization's access document: the roles, what each kind of
// resource starts as, who may share, what the operator and agents may do. It
// is stored as one row with a revision and changes only through a PUT by the
Expand Down Expand Up @@ -287,6 +331,16 @@ func (p Policy) Validate(prev Policy, inUse func(role string) bool) error {
default:
return invalid("who may move a chat must be the owner or people with manage permission")
}
switch p.Chats.ApprovalsBy {
case "", ApprovalsByOwner, ApprovalsByAnswer:
default:
return invalid("who may approve a tool call in a shared chat must be owner (only its owner) or answer (anyone who may answer its questions)")
}
switch p.Chats.WakeBy {
case "", WakeByView, WakeByRun, WakeByOwner:
default:
return invalid("who may start a chat that is not running must be view (anyone who can see it), run (anyone who may send messages in it) or owner (only its owner)")
}
if cost := p.Hosting.MaxSessionCostUSD; math.IsNaN(cost) || cost < 0 || cost > 1_000_000 {
return invalid("hosting chat cost must be between 0 and 1,000,000 dollars")
}
Expand Down
4 changes: 4 additions & 0 deletions internal/factory/serve/hosted_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (h *Handler) lookupHostedChat(w http.ResponseWriter, r *http.Request, id st
return chat, true
}

// hostedStateRunning is the state the chats' host reports for a chat with a live process. Anything else it says (it says
// suspended) means opening the chat starts it.
const hostedStateRunning = "running"

type hostedStatusFields struct {
ID string `json:"id"`
State string `json:"state"`
Expand Down
Loading
Loading