Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: mark pages in someone's personal source as above a shared room
garrytan36c08e024fdc
diff --git a/changelog.d/gbra498-owner-room-marks.md b/changelog.d/gbra498-owner-room-marks.mdindex 244e824d92..29efb1d4d2 100644--- a/changelog.d/gbra498-owner-room-marks.md+++ b/changelog.d/gbra498-owner-room-marks.md@@ -3,9 +3,9 @@**A harness can now tell gbrain which group chat an answer goes into, and gbrain marks every page and fact with whether the people in that chat could have read it.** The owner of a brain who asks their agent something in a team chat should get the same work as in a direct message, but the agent also needs to know what not to repeat in front of the room. Declaring the room does exactly that and nothing more: the read is never refused, shortened, reordered or failed because of it, and a call with no room returns what it always did.- Declare the room with the `X-Gbrain-Room` header (`gbrain serve --http`) or request-level `_meta.room`; a harness that can set neither may pass a `room` tool argument once `whoami` shows `features.room: true`. Entries are member ids, `group:<id>` or `guest`.-- Each item in `get_page`, `search`, `query`, `list_pages`, `find_pages`, `get_chunks`, `get_timeline`, `get_backlinks`, `recall` and `entity` answers, each hot-memory fact and each saved fact a search returns (also shown on its line of the saved-facts block) carries `room_access`: `{readable: true}`, `{readable: false, reason}` (`readers`, `folder_rule`, `labelled_only`, `private_label`, `derived`) or `{readable: "unknown", reason}`. `_meta.room` sums it up. A link, a timeline row or an entity-card fact is readable only when the room can read every page it depends on.+- Each item in `get_page`, `search`, `query`, `list_pages`, `find_pages`, `get_chunks`, `get_timeline`, `get_backlinks`, `recall` and `entity` answers, each hot-memory fact and each saved fact a search returns (also shown on its line of the saved-facts block) carries `room_access`: `{readable: true}`, `{readable: false, reason}` (`readers`, `folder_rule`, `labelled_only`, `private_label`, `derived`, `personal_source`) or `{readable: "unknown", reason}`. `_meta.room` sums it up. A link, a timeline row or an entity-card fact is readable only when the room can read every page it depends on.- The `room_above_level` notice tells the model to use those items for the work, keep them out of the reply unless the ask needs them and offer them in a direct message. `room_hints_unavailable` says when the room could not be judged within 3 seconds or at all (the answer still goes out whole and on time, every item marked unknown, with the degraded stamp), and `room_conflict` says when a `room` argument disagreed with the harness; an argument can only add people.-- The owner's own credential needs no `may_act_for` to declare a room, because a room only adds marks; any other member's credential needs `may_act_for` covering the room. Each marked call writes a count-only `room_marked` row to the audience audit log.+- The owner's own credential needs no `may_act_for` to declare a room, because a room only adds marks; any other member's credential needs `may_act_for` covering the room. Each marked call writes a `room_marked` row to the audience audit log with the room's principals and the counts, never page names or content.- No setting, no migration, and it works the same on a repo-less brain. Guide: [docs/guides/rooms.md](docs/guides/rooms.md).<!-- behavior-change: owner_room_marks_gst2639 -->diff --git a/docs/guides/rooms.md b/docs/guides/rooms.mdindex 255ee7c5e3..d41ce27173 100644--- a/docs/guides/rooms.md+++ b/docs/guides/rooms.md@@ -62,6 +62,7 @@ page is marked `private_label` for any room. The reasons for `false`, checked in| `labelled_only` | someone in the room reads only pages labelled for them, and this one is not || `private_label` | the page is `visibility: private` || `derived` | it was built from a page the room cannot read |+| `personal_source` | it sits in someone's personal source (`gbrain access member source`) and the room holds anyone else, a group or a guest |When any item is not `true`, the answer carries the `room_above_level`safety notice, so a model that never sees `_meta` still sees the warning.@@ -142,7 +143,8 @@ the room can read, and store only the reply it sent.Each marked call appends a count-only `room_marked` row to`~/.gbrain/audit/access-audience-YYYY-Www.jsonl`: how many items were-readable, above or unknown, by reason. It never records the room's names, slugs-or content. Rooms add no setting and no migration, and they work the same on a+readable, above or unknown, by reason, and the room's principals as the harness+declared them, the same way an `on_behalf_of` row records its audience. It+never records page names, slugs or content. Rooms add no setting and no migration, and they work the same on arepo-less brain, where the readers, folder rules and labels live in thedatabase.diff --git a/src/core/access/room.ts b/src/core/access/room.tsindex 7f76d8b784..54259e65ed 100644--- a/src/core/access/room.ts+++ b/src/core/access/room.ts@@ -17,6 +17,7 @@ import type { Action, Notice } from '../agent-output.ts';import { logAudienceAssertion, normalizePrincipals, type AudiencePrincipal } from './audience.ts';import { audienceCallerAccess, resolveAudienceIdentities } from './caller.ts';import { factVisibilitySql } from './member-facts.ts';+import { hiddenPersonalSources } from './member-sources.ts';import {callerIdentities, currentCallerAccess, folderRulesAllowedSql, labelledOnlyForSql, privacyForCaller, privacyPartSql,privatePagesFilterForCaller, privateProvenanceFilterUnderPolicy, readersAllowedSql, withCallerAccess, worldOnlyFacts,@@ -33,7 +34,7 @@ export const ROOM_DEADLINE_MS = 3_000;export const ROOM_MAX_REFS = 2_000;/** Why the room could not read an item. */-export type RoomReason = 'readers' | 'folder_rule' | 'labelled_only' | 'private_label' | 'derived';+export type RoomReason = 'readers' | 'folder_rule' | 'labelled_only' | 'private_label' | 'derived' | 'personal_source';/** Why an item, or the whole answer, could not be judged. */export type RoomUnknownReason = 'invalid' | 'not_permitted' | 'unknown_principal' | 'unbound' | 'mark_failed' | 'deadline' | 'response_cap'| 'op_not_covered' | 'not_found' | 'not_identified' | 'over_limit';@@ -175,7 +176,7 @@ function pageReason(r: PageRow): RoomAccess {}/** The room's decisions for the refs, judged under the room's own access record (the same predicates its reads use). */-async function decide(engine: BrainEngine, access: Partial<CallerAccess>, refs: Ref[]): Promise<Map<string, RoomAccess>> {+async function decide(engine: BrainEngine, access: Partial<CallerAccess>, principals: AudiencePrincipal[], refs: Ref[]): Promise<Map<string, RoomAccess>> {const pages = refs.filter((r): r is Extract<Ref, { kind: 'page' }> => r.kind === 'page');const ids = refs.filter((r): r is Extract<Ref, { kind: 'page_id' }> => r.kind === 'page_id').map(r => r.id);const facts = refs.filter((r): r is FactRef => r.kind === 'fact').map(r => r.id);@@ -194,8 +195,14 @@ async function decide(engine: BrainEngine, access: Partial<CallerAccess>, refs:});const out = new Map<string, RoomAccess>();if (pages.length || ids.length) {- for (const row of await engine.executeRaw<PageRow>(pageSql, [pages.map(p => p.source_id), pages.map(p => p.slug), ids])) {- const access = pageReason(row);+ const rows = await engine.executeRaw<PageRow>(pageSql, [pages.map(p => p.source_id), pages.map(p => p.slug), ids]);+ const sources = [...new Set(rows.filter(r => r.ok).map(r => r.source_id))];+ const personal = new Set<string>();+ for (const p of principals) {+ for (const s of await hiddenPersonalSources(engine, sources, p.startsWith('member:') ? p.slice('member:'.length) : '', false)) personal.add(s);+ }+ for (const row of rows) {+ const access: RoomAccess = row.ok && personal.has(row.source_id) ? { readable: false, reason: 'personal_source' } : pageReason(row);out.set(`p:${row.source_id}:${row.slug}`, access);out.set(`i:${row.id}`, access);}@@ -339,7 +346,7 @@ async function assessRoomUnguarded(engine: BrainEngine, input: { op: string; resdecisions = await withDeadline((async () => {await deadlineForTests?.delay();const access = late ? null : await roomAccessFor(engine, caller, d.principals);- return access && !late ? decide(engine, access, [...refs.values()]) : new Map<string, RoomAccess>();+ return access && !late ? decide(engine, access, d.principals, [...refs.values()]) : new Map<string, RoomAccess>();})(), deadlineForTests?.ms ?? input.deadlineMs ?? ROOM_DEADLINE_MS, () => { late = true; });} catch (e) {unavailable = e instanceof RoomUnavailable ? e : new RoomUnavailable('mark_failed');diff --git a/test/access-room.test.ts b/test/access-room.test.tsindex 9fc122d7db..4d1bfdaffc 100644--- a/test/access-room.test.ts+++ b/test/access-room.test.ts@@ -31,6 +31,7 @@ import { operations } from '../src/core/operations.ts';import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';import { addGroup, addMember, bindCredential } from '../src/core/access/members.ts';import { setOwner } from '../src/core/access/owner.ts';+import { bindMemberSource, unbindMemberSource } from '../src/core/access/member-sources.ts';import { setMemberLabelledOnly } from '../src/core/access/labelled-only.ts';import { proposeRevision } from '../src/core/access/policy.ts';import { __setRoomDeadlineForTests, assessRoom, readRoomDeclaration, ROOM_COVERED_OPS } from '../src/core/access/room.ts';@@ -387,6 +388,20 @@ for (const kind of testBackends()) {expect(cardBody.entity.room_access).toEqual({ readable: true });}, 60_000);+ test("a page in the owner's personal source is above any room holding someone else, and readable to the owner alone", async () => {+ await bindMemberSource(engine, 'owner-example', SRC);+ try {+ const group = await call('owner', 'get_page', { slug: PAGES.open.slug }, { room: { header: 'group:team-a' } });+ const alone = await call('owner', 'get_page', { slug: PAGES.open.slug }, { room: { header: 'owner-example' } });+ expect(group.isError).toBe(false);+ expect(unmarked(group.json)).toEqual(unmarked(alone.json));+ expect((group.json as { room_access?: unknown }).room_access).toEqual({ readable: false, reason: 'personal_source' });+ expect((alone.json as { room_access?: unknown }).room_access).toEqual({ readable: true });+ } finally {+ await unbindMemberSource(engine, 'owner-example', SRC, { apply: true });+ }+ }, 60_000);+test('if marking itself throws, the read still goes out whole, flagged mark_failed', async () => {const { declaration } = readRoomDeclaration({ header: 'group:team-a' }, {});const result = { slug: PAGES.open.slug, keep: () => 1 };