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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p
of each conversation's own. An admin's assistant searches and reads the mail, calendars and files connected to Lens
(Gmail, Google Calendar, Google Drive, S3 and the rest), cites messages and events, and brings them in once you
approve; where an admin allows it, it sends mail, adds events and writes files, each only after you read all of it
and say yes. Regenerating an answer or editing a question makes a new version beside the old one, and
and say yes yourself (an API key, an app or an agent can only decline). Regenerating an answer or editing a question makes a new version beside the old one, and
nothing is lost. Each answer keeps a record of what produced it (the model, its tools, how long it took and what it
cost), opened from its header. It reads answers aloud with a speech provider, macOS `say` or StyleTTS2 on this machine
(opt-in), any message in one click. Long conversations are summarised before they fill the model's context, and
Expand Down
5 changes: 5 additions & 0 deletions changelog.d/agent-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- **Only the person approves what the assistant asks.** Sending mail, adding an event, writing a file and the
assistant's other proposals are approved only by the person signed in to Lens, or in the chat they were asked in.
An API key or an app given access can now only decline them (`POST /api/v1/approvals/{id}` answers 403 to an
approve or a sample), as MCP and the assistant already could, and each approval's `can` says so
([Approvals](docs/automations.md#approvals)).
5 changes: 3 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,9 @@ automations' actions ([Approvals](automations.md#approvals)); filter by `status`
`automation` or `chat_id`, and page with `before` (the last id of the page before) and `limit`. `/approvals/count`
gives `{waiting, automations}`. `POST /approvals/{aid} {decision, params?}` decides one: `approve`, `sample` (a batch
run on 3 assets first), `decline`, `edit` (an automation's action with `params` changed) or `always` (approved, and
that node runs without asking from now on). Approving, editing and `always` on an automation's action need a signed-in
session (403 for an API key or an app); each approval's `can` says what you may do with it.
that node runs without asking from now on). Approving (and a sample), editing and `always` need a signed-in session,
for the assistant's proposals as for an automation's actions (403 for an API key or an app, which can only decline);
each approval's `can` says what you may do with it.

`GET /chats/capabilities` tells anyone signed in whether a language model is set up (`configured`), which (`model`),
whether it uses tools (`tools`, `max_steps`, `max_tool_calls`), whether answers can be checked (`check`) and the `models` people may pick
Expand Down
3 changes: 2 additions & 1 deletion docs/assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ When the configured model supports function calling, chat becomes an agent.
and its other names, hiding one, or adding one to a namespace's fixed list) and changing topics (merging,
renaming, describing or deleting one; [Topics](topics.md)) don't happen directly.
They become approval cards (with the batch estimate) that the person approves, approves on a sample, or declines
(`POST /api/v1/approvals/<id>`). Viewers only get the read tools. When an [automation](automations.md#the-model-and-the-agent)
(`POST /api/v1/approvals/<id>`). Only the person approves one, signed in to Lens or in the chat: an API key, an app,
MCP and the assistant can only decline it. Viewers only get the read tools. When an [automation](automations.md#the-model-and-the-agent)
uses the assistant, every change it would make is an approval of yours, notes included, with the run as its origin.
- **Check sources** (`POST /api/v1/chats/<id>/messages/<id>/check`) re-checks every cited claim against the lines it
cites, and lists sentences that cite nothing.
Expand Down
5 changes: 4 additions & 1 deletion docs/automations.md
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,10 @@ own chat app (below). An API key, an app, MCP and the assistant can list and dec
since approving is the person's pick; each approval's `can` says what the caller may do with it. This is the one
place where an agent can't do what you can: agents read them with `approvals` and decline with `decline_approval`
(over [MCP](mcp.md#tools) and in the [assistant](assistant.md#the-assistants-tools)), and there is no tool to approve.
The assistant's own approvals in a conversation are decided as before.
The assistant's own approvals (sending mail, adding an event, writing a file, changing entities and the rest) follow
the same rule: you approve them signed in to Lens or in the chat they were asked in, and an API key, an app, MCP and
the assistant can only decline them (403). A research plan is the one exception, and only when an admin turns on
`research.agents_skip_review` ([Deep research](assistant.md#deep-research)).

**In your chat app.** A reply of "yes" in the private chat you were told in approves an automation's action, under
the rules a chat room's approvals follow ([Chat rooms](chat-rooms.md#what-it-does)), and only when all of these hold:
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ request, to another check that proves the same thing; loosening a requirement or
| It acts with tools, and changes wait for approval | [test_chats.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chats.py) `test_tools_approvals_and_checking`; [test_approvals.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_approvals.py) `test_the_assistant_lists_and_declines_but_never_approves` |
| Web search and reading pages, cited like the archive, off until an admin turns it on | [test_web_search.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_web_search.py) `test_an_answer_cites_the_web_and_the_conversation_keeps_it`, `test_reading_pages_and_pdfs`; [chat-web-citations.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-web-citations.test.tsx) |
| An admin's assistant searches and reads connected mail, calendars and files, cites messages and events, and brings them in only once approved | [test_connection_tools.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_connection_tools.py) `test_only_admins_get_them_and_only_for_what_is_connected`, `test_searching_and_reading_gmail`, `test_calendar_events_between_two_times`, `test_finding_and_reading_files_in_storage`, `test_bringing_things_in_waits_for_the_persons_yes`; [chat-web-citations.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-web-citations.test.tsx) `a message from a connected mailbox` |
| Where an admin allows it, the assistant sends mail, adds events and writes files, each only after the person reads all of it and says yes | [test_connection_tools.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_connection_tools.py) `test_sending_mail_waits_for_the_persons_yes`, `test_adding_an_event_waits_for_the_persons_yes`, `test_writing_a_file_waits_for_the_persons_yes`; [approvals-page.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/approvals-page.test.tsx) `shows the assistant's whole message before it is sent`; [add-connection.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/add-connection.test.tsx) `connects Gmail the same way, and asks a service account whose mail it reads` |
| Where an admin allows it, the assistant sends mail, adds events and writes files, each only after the person reads all of it and says yes, signed in or in the chat; an API key, an app or an agent only declines | [test_connection_tools.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_connection_tools.py) `test_sending_mail_waits_for_the_persons_yes`, `test_adding_an_event_waits_for_the_persons_yes`, `test_writing_a_file_waits_for_the_persons_yes`; [test_approvals.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_approvals.py) `test_only_the_person_approves_what_the_assistant_asks`; [approvals-page.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/approvals-page.test.tsx) `shows the assistant's whole message before it is sent`; [add-connection.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/add-connection.test.tsx) `connects Gmail the same way, and asks a service account whose mail it reads` |
| Python and bash in a sandbox of each conversation's own, off until an admin turns it on | [test_sandbox.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_sandbox.py) `test_python_and_bash_share_one_sandbox_per_conversation`, `test_the_sandbox_is_off_until_an_admin_turns_it_on_for_the_people_it_names` |
| Deep research: a goal gets a plan to approve, runs in the background and comes back as a cited report, saved as a note or a podcast episode | [test_research.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_research.py) `test_a_goal_gets_a_plan_that_waits_for_approval_and_can_be_revised`, `test_an_approved_plan_runs_and_its_report_comes_back_checked`, `test_a_report_becomes_a_note_and_a_podcast`; [chat-research.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-research.test.tsx) `approves the plan with Enter, sending the edits`, `saves it as a note, asking where when it could go in several namespaces`, `turns the next message into a research goal, and says when it can't`; [journeys.mjs](https://github.com/adeelahmad/lense/blob/main/ci/clicks/journeys.mjs) `chat-research`; [baseline.json](https://github.com/adeelahmad/lense/blob/main/ci/clicks/baseline.json) `chat-research` ≤ 4 clicks |
| Regenerating an answer or editing a question makes a new version beside the old one | [test_chat_versions.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_chat_versions.py) `test_regenerate_edit_switch_and_go_on`; [chat-versions.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/chat-versions.test.tsx) `regenerating an answer`, `the version switch`; [journeys.mjs](https://github.com/adeelahmad/lense/blob/main/ci/clicks/journeys.mjs) `chat-regenerate`; [baseline.json](https://github.com/adeelahmad/lense/blob/main/ci/clicks/baseline.json) `chat-regenerate` ≤ 4 clicks |
Expand Down
4 changes: 2 additions & 2 deletions fastapi_backend/app/api/v1/routes/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ def get_approval(aid: int, user: CurrentUser, db: Db, cfg: Cfg) -> Approval:
def decide_approval(aid: int, body: ApprovalDecision, user: Writer, acl: Acl, db: Db, cfg: Cfg, request: Request) -> ApprovalOutcome:
"""Approve (or run a sample of, or decline) something the assistant proposed or an automation's action. Each
approval is decided once. An automation's action can also be edited (`params`) and then carried out, or allowed
from now on for that automation (`always`); approving one needs you signed in, since it's your pick: an API key
or an app can only decline it."""
from now on for that automation (`always`). Approving is your pick, so it needs you signed in: an API key or an
app can only decline (403 otherwise)."""
a = db.one("SELECT account FROM $r", r=R("approval", aid))
if not a or a["account"] != user.id:
raise HTTPException(404, "not found")
Expand Down
10 changes: 7 additions & 3 deletions fastapi_backend/app/domain/ai_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,9 +2378,9 @@ def approve(db, cfg, aid, user, editable, decision="approve", base=None, admin=F
"""Carry out an approved action: a batch run (or a sample of it), an entity change, a note an automation's agent
proposed, or a change to the server (ops_tools.py: settings, a namespace, importing attached files or web pages,
following a news feed).
An automation's action (an approval with an intent) is decided by approvals.decide: `params` are an edit's, `via`
where the pick was made (web, chat, token, oauth or agent; only the person, signed in or in their own chat,
approves it)."""
An automation's action (an approval with an intent) is decided by approvals.decide: `params` are an edit's. `via`
is where the pick was made (web, chat, token, oauth or agent): only the person, signed in or in their own chat,
approves; an API key, an app, MCP and the assistant can only decline (PermissionError)."""
from . import approvals # it carries actions out, and actions load the assistant's tools

a = approvals.get(db, aid)
Expand All @@ -2399,6 +2399,10 @@ def approve(db, cfg, aid, user, editable, decision="approve", base=None, admin=F
"UPDATE $r SET status = 'declined', decided_at = $t, decided_by = $u", r=R("approval", a["id"]), t=store.now(), u=user["email"]
)
return {"status": "declined"}
if not approvals.can(a, via)["approve"]:
raise PermissionError(
"approve what the assistant asks yourself, signed in to Lens or in the chat: an API key or an app can only decline it"
)
with graph_history.acting(actor=user["email"], via="assistant", approval=a["id"]):
result = _carry_out(db, cfg, a, user, editable, decision, base, admin, readable, via)
db.q(
Expand Down
17 changes: 9 additions & 8 deletions fastapi_backend/app/domain/approvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
first, which the engine hands to `ask`. Approving it performs the intent through `actions.perform`, as the
automation and for its person, and records the outcome on the run (`automations.update_intent`).

An automation's approval is the person's pick: approving it (or editing its params, or allowing that node from now on)
needs them signed in to Lens, or a yes in the chat it was told in (bridge.decide_in_room). An API key, an app, MCP and
the assistant can list and decline approvals, never approve them. Undecided ones expire after
Every approval is the person's pick: approving one (or editing an automation's params, or allowing that node from now
on) needs them signed in to Lens, or a yes in their own chat (the assistant's chat, or the chat app it was told in:
bridge.decide_in_room). An API key, an app, MCP and the assistant can list and decline approvals, never approve them;
a research plan is the one exception, and only with research.agents_skip_review on. Undecided ones expire after
`automations.approval_days` (default 7) and nothing runs; the assistant's own approvals never expire, as before.

Statuses: pending (waiting), running (being carried out), done, failed, declined, expired.
Expand Down Expand Up @@ -262,9 +263,9 @@ def edited(intent, new):

def can(row, via="web", cfg=None):
"""What the person can do with a waiting approval from where they are (`via`: web for a signed-in session, chat for
their own chat app, token or oauth for an API key or an app, agent for MCP and the assistant, which only decline).
A research plan is approved by the person only (edited in the web app), unless research.agents_skip_review is on
in `cfg`."""
their own chat app, token or oauth for an API key or an app, agent for MCP and the assistant). Only the person
approves, signed in or in their chat; the others only decline. A research plan is approved by the person only
(edited in the web app), unless research.agents_skip_review is on in `cfg`."""
pending = row.get("status") == "pending"
intent = row.get("intent")
if row.get("tool") == "research":
Expand All @@ -275,9 +276,9 @@ def can(row, via="web", cfg=None):
"edit": pending and via == "web",
"always": False,
}
if not intent:
return {"approve": pending and via != "agent", "decline": pending, "edit": False, "always": False}
person = via in ("web", "chat")
if not intent: # the assistant's proposal: the person's pick, as an automation's is
return {"approve": pending and person, "decline": pending, "edit": False, "always": False}
return {
"approve": pending and person,
"decline": pending,
Expand Down
51 changes: 51 additions & 0 deletions fastapi_backend/tests/api/test_approvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,54 @@ def test_the_assistant_lists_and_declines_but_never_approves(db, cfg, people, po
assert json.loads(fresh.call("decline_approval", {"id": second})[0]) == {"approval": second, "status": "declined"}
assert "not waiting" in fresh.call("decline_approval", {"id": second})[0]
assert "decline_approval" in actions_ai.LEFT_OUT # an automation's agent doesn't get to decline its person's picks


def test_only_the_person_approves_what_the_assistant_asks(client, db, cfg, people, monkeypatch):
"""Sending mail, adding an event, writing a file: an API key, an app and MCP only decline them; signed in, the
person approves."""
from app.domain import connection_tools
from tests.api.test_mcp import tool, tool_error
from tests.api.test_oauth import _bearer, _grant

done = []
monkeypatch.setattr(connection_tools, "carry_out", lambda db, cfg, name, args, user, admin: done.append(name) or {"sent": name})

def asked(name):
aid = db.next_id("approval")
db.q(
"CREATE $r CONTENT $d",
r=store.R("approval", aid),
d={"chat": 5, "account": people["me"], "tool": name, "args": {"to": ["a@x.io"]}, "summary": name, "status": "pending"},
)
return aid

h = login(client, "me@x.io", "my password 1")
_, key = auth.create_token(db, people["me"], "script", scope="write")
hk = {"Authorization": f"Bearer {key}"}
ho = _bearer(_grant(client, h, scope="write")[1])
mail, event, file = asked("send_mail"), asked("add_event"), asked("write_file")
for hdr in (hk, ho): # an API key and an app see they can only decline, and can't approve or sample
assert client.get(f"/api/v1/approvals/{mail}", headers=hdr).json()["can"] == {
"approve": False,
"decline": True,
"edit": False,
"always": False,
}
for aid in (mail, event, file):
for decision in ("approve", "sample"):
r = client.post(f"/api/v1/approvals/{aid}", headers=hdr, json={"decision": decision})
assert r.status_code == 403 and "only decline" in r.json()["detail"], (aid, decision, r.text)
with pytest.raises(PermissionError): # nor MCP or the assistant, however they reach it
ai_tools.approve(db, cfg, mail, {"id": people["me"], "email": "me@x.io"}, {people["pods"]}, "approve", via="agent")
assert not done and approvals.get(db, mail)["status"] == "pending"
assert client.get(f"/api/v1/approvals/{mail}", headers=h).json()["can"]["approve"] is True
# declining works from each of them
assert client.post(f"/api/v1/approvals/{event}", headers=hk, json={"decision": "decline"}).json()["status"] == "declined"
assert client.post(f"/api/v1/approvals/{file}", headers=ho, json={"decision": "decline"}).json()["status"] == "declined"
later = asked("send_mail")
assert tool(client, ho, "decline_approval", id=later) == {"approval": later, "status": "declined"}
assert "not waiting" in tool_error(client, ho, "decline_approval", id=later)
# and the person, signed in, approves
out = client.post(f"/api/v1/approvals/{mail}", headers=h, json={"decision": "approve"})
assert out.status_code == 200 and out.json()["status"] == "done" and done == ["send_mail"]
assert approvals.get(db, mail)["decided_by"] == "me@x.io"
4 changes: 2 additions & 2 deletions nextjs-frontend/app/openapi-client/sdk.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10967,8 +10967,8 @@ export class Chats {
*
* Approve (or run a sample of, or decline) something the assistant proposed or an automation's action. Each
* approval is decided once. An automation's action can also be edited (`params`) and then carried out, or allowed
* from now on for that automation (`always`); approving one needs you signed in, since it's your pick: an API key
* or an app can only decline it.
* from now on for that automation (`always`). Approving is your pick, so it needs you signed in: an API key or an
* app can only decline (403 otherwise).
*/
public static decideApproval<ThrowOnError extends boolean = false>(
options: Options<DecideApprovalData, ThrowOnError>,
Expand Down
Loading
Loading