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
Original file line number Diff line number Diff line change
Expand Up @@ -7636,6 +7636,7 @@ public struct SessionsSendParams: Codable, Sendable {
public let deliver: Bool?
public let suppresscommandinterpretation: Bool?
public let senderattribution: [String: AnyCodable]?
public let personalautomations: Bool?
public let attachments: [[String: AnyCodable]]?
public let timeoutms: Int?
public let idempotencykey: String?
Expand All @@ -7648,6 +7649,7 @@ public struct SessionsSendParams: Codable, Sendable {
deliver: Bool? = nil,
suppresscommandinterpretation: Bool? = nil,
senderattribution: [String: AnyCodable]? = nil,
personalautomations: Bool? = nil,
attachments: [[String: AnyCodable]]? = nil,
timeoutms: Int? = nil,
idempotencykey: String? = nil)
Expand All @@ -7659,6 +7661,7 @@ public struct SessionsSendParams: Codable, Sendable {
self.deliver = deliver
self.suppresscommandinterpretation = suppresscommandinterpretation
self.senderattribution = senderattribution
self.personalautomations = personalautomations
self.attachments = attachments
self.timeoutms = timeoutms
self.idempotencykey = idempotencykey
Expand All @@ -7672,6 +7675,7 @@ public struct SessionsSendParams: Codable, Sendable {
case deliver
case suppresscommandinterpretation = "suppressCommandInterpretation"
case senderattribution = "senderAttribution"
case personalautomations = "personalAutomations"
case attachments
case timeoutms = "timeoutMs"
case idempotencykey = "idempotencyKey"
Expand Down Expand Up @@ -17686,6 +17690,7 @@ public struct ChatSendParams: Codable, Sendable {
public let systemprovenancereceipt: String?
public let suppresscommandinterpretation: Bool?
public let senderattribution: [String: AnyCodable]?
public let personalautomations: Bool?
public let expectedleafentryid: AnyCodable?
public let requireexactleaf: Bool?
public let expectedsessionroutingcontract: String?
Expand Down Expand Up @@ -17714,6 +17719,7 @@ public struct ChatSendParams: Codable, Sendable {
systemprovenancereceipt: String? = nil,
suppresscommandinterpretation: Bool? = nil,
senderattribution: [String: AnyCodable]? = nil,
personalautomations: Bool? = nil,
expectedleafentryid: AnyCodable? = nil,
requireexactleaf: Bool? = nil,
expectedsessionroutingcontract: String? = nil,
Expand Down Expand Up @@ -17741,6 +17747,7 @@ public struct ChatSendParams: Codable, Sendable {
self.systemprovenancereceipt = systemprovenancereceipt
self.suppresscommandinterpretation = suppresscommandinterpretation
self.senderattribution = senderattribution
self.personalautomations = personalautomations
self.expectedleafentryid = expectedleafentryid
self.requireexactleaf = requireexactleaf
self.expectedsessionroutingcontract = expectedsessionroutingcontract
Expand Down Expand Up @@ -17769,6 +17776,7 @@ public struct ChatSendParams: Codable, Sendable {
systemprovenancereceipt: String? = nil,
suppresscommandinterpretation: Bool? = nil,
senderattribution: [String: AnyCodable]? = nil,
personalautomations: Bool? = nil,
expectedleafentryid: AnyCodable? = nil,
requireexactleaf: Bool? = nil,
expectedsessionroutingcontract: String? = nil,
Expand Down Expand Up @@ -17797,6 +17805,7 @@ public struct ChatSendParams: Codable, Sendable {
systemprovenancereceipt: systemprovenancereceipt,
suppresscommandinterpretation: suppresscommandinterpretation,
senderattribution: senderattribution,
personalautomations: personalautomations,
expectedleafentryid: expectedleafentryid,
requireexactleaf: requireexactleaf,
expectedsessionroutingcontract: expectedsessionroutingcontract,
Expand Down Expand Up @@ -17826,6 +17835,7 @@ public struct ChatSendParams: Codable, Sendable {
case systemprovenancereceipt = "systemProvenanceReceipt"
case suppresscommandinterpretation = "suppressCommandInterpretation"
case senderattribution = "senderAttribution"
case personalautomations = "personalAutomations"
case expectedleafentryid = "expectedLeafEntryId"
case requireexactleaf = "requireExactLeaf"
case expectedsessionroutingcontract = "expectedSessionRoutingContract"
Expand Down
2 changes: 2 additions & 0 deletions packages/gateway-protocol/src/schema/logs-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export const ChatSendParamsSchema = closedObject({
systemProvenanceReceipt: Type.Optional(Type.String()),
suppressCommandInterpretation: Type.Optional(Type.Boolean()),
senderAttribution: Type.Optional(ChatSenderAttributionSchema),
/** Trusted relay proof for a private conversation in the attributed Agent. */
personalAutomations: Type.Optional(Type.Literal(true)),
// Client's believed active-branch leaf entry id. A mismatch with the
// session's current active leaf rejects the send so stale views cannot post elsewhere.
expectedLeafEntryId: Type.Optional(Type.Union([NonEmptyString, Type.Null()])),
Expand Down
1 change: 1 addition & 0 deletions packages/gateway-protocol/src/schema/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ export const SessionsSendParamsSchema = closedObject({
/** Trusted relays may preserve the same provenance controls as chat.send. */
suppressCommandInterpretation: Type.Optional(Type.Boolean()),
senderAttribution: Type.Optional(ChatSenderAttributionSchema),
personalAutomations: Type.Optional(Type.Literal(true)),
attachments: Type.Optional(ChatAttachmentsSchema),
timeoutMs: Type.Optional(Type.Integer({ minimum: 0 })),
idempotencyKey: Type.Optional(NonEmptyString),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ export class BrowserGatewayAssertions {
}
}

ownedSessionKey(agentId: string, rawSessionKey: unknown, label: string): void {
ownedSessionKey(agentId: string, rawSessionKey: unknown, label: string): string {
const sessionKey = optionalString(rawSessionKey);
if (!sessionKey) {
this.fail("invalid-params", `${label} is required`);
}
if (this.resolveAgentIdFromSessionKey(sessionKey) !== agentId) {
this.fail("cross-agent-denied", `browser access denied for ${label}`);
}
return sessionKey;
}

sessionKeyArray(agentId: string, value: unknown, label: string, required: boolean): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function assertConfiguredCronModel(
}
}

function assertEffectiveCronDelivery(
function prepareEffectiveCronUpdate(
context: BrowserCronContext,
currentJob: JsonObject,
prepared: JsonObject,
Expand All @@ -69,7 +69,20 @@ function assertEffectiveCronDelivery(
prepared.patch && typeof prepared.patch === "object" && !Array.isArray(prepared.patch)
? (prepared.patch as JsonObject)
: {};
const delivery = patch.delivery === undefined ? currentJob.delivery : patch.delivery;
const currentDelivery = currentJob.delivery as JsonObject | undefined;
if (currentJob.sessionTarget === "current" && currentDelivery?.channel === "webchat") {
const sessionKey = optionalString(currentJob.sessionKey);
if (!sessionKey || (patch.sessionTarget !== undefined && patch.sessionTarget !== "current")) {
context.fail("method-not-allowed", "this cron job must stay in its original conversation");
}
// The browser editor sends last for announce and omits the channel for
// none. Keep this job's exact transcript destination across both edits.
patch.sessionKey = sessionKey;
if (patch.delivery !== undefined) {
patch.delivery = { ...(patch.delivery as JsonObject), channel: "webchat" };
}
}
const delivery = patch.delivery === undefined ? currentDelivery : patch.delivery;
const sessionTarget =
patch.sessionTarget === undefined ? currentJob.sessionTarget : patch.sessionTarget;
if (patch.sessionTarget === "current" && currentJob.sessionTarget !== "current") {
Expand Down Expand Up @@ -165,7 +178,7 @@ export async function preflightCronMutation(
return;
}
if (method === "cron.update") {
assertEffectiveCronDelivery(context, job, prepared);
prepareEffectiveCronUpdate(context, job, prepared);
}
await assertConfiguredCronModel(context, method, prepared, job);
const expectedConfigRevision = optionalString(prepared.expectedConfigRevision);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe("BrowserGatewayProxy ACP commands", () => {
idempotencyKey: `request-${message}`,
agentId: binding.agentId,
deliver: false,
personalAutomations: true,
senderAttribution: {
id: user.accountId,
name: user.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,56 @@ describe("BrowserGatewayProxy cron", () => {
});
});

it("keeps a private conversation job's delivery and session through browser management", async () => {
const { binding, proxy, request, token } = await setup();
const sessionKey = `agent:${binding.agentId}:dashboard:private`;
const owner = { agentId: binding.agentId, sessionKey, accountId: "first.user" };
let job = safeCronJob(binding.agentId, {
sessionTarget: "current",
sessionKey,
owner,
delivery: { mode: "announce", channel: "webchat" },
});
const jobRef = { id: job.id, expectedConfigRevision: "revision-1" };
request.mockResolvedValueOnce({ jobs: [job], total: 1, hasMore: false });
await expect(proxy.request(token, "cron.list", {})).resolves.toMatchObject({ jobs: [job] });

for (const delivery of [{ mode: "none" }, { mode: "announce", channel: "last" }]) {
const updated = { ...job, delivery: { mode: delivery.mode, channel: "webchat" } };
request.mockResolvedValueOnce(job).mockResolvedValueOnce(updated);
await expect(
proxy.request(token, "cron.update", { ...jobRef, patch: { delivery } }),
).resolves.toEqual(updated);
expect(request).toHaveBeenLastCalledWith("cron.update", {
...jobRef,
patch: { agentId: binding.agentId, sessionKey, delivery: updated.delivery },
});
job = updated;
}

request.mockClear();
request.mockResolvedValueOnce(job);
await expect(
proxy.request(token, "cron.update", { ...jobRef, patch: { sessionTarget: "isolated" } }),
).rejects.toMatchObject({ code: "method-not-allowed" });
expect(request).toHaveBeenCalledExactlyOnceWith("cron.get", { id: job.id });

request.mockClear();
await expect(
proxy.request(token, "cron.update", {
...jobRef,
patch: { delivery: { mode: "announce", channel: "knox", to: "dm:foreign" } },
}),
).rejects.toMatchObject({ code: "method-not-allowed" });
expect(request).not.toHaveBeenCalled();

for (const method of ["cron.run", "cron.remove"]) {
request.mockResolvedValueOnce(job).mockResolvedValueOnce({ ok: true });
await expect(proxy.request(token, method, jobRef)).resolves.toEqual({ ok: true });
expect(request).toHaveBeenLastCalledWith(method, jobRef);
}
});

it("returns an empty run-history page when the owned registry has no jobs", async () => {
const { proxy, request, token } = await setup();
request.mockResolvedValueOnce({ jobs: [], total: 0, hasMore: false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe("BrowserGatewayProxy personal session messaging", () => {
timeoutMs: 10_000,
idempotencyKey: "run-1",
deliver: false,
personalAutomations: true,
suppressCommandInterpretation: true,
senderAttribution: {
id: user.accountId,
Expand All @@ -72,7 +73,12 @@ describe("BrowserGatewayProxy personal session messaging", () => {
await expect(
proxy.request(token, method, { key: "agent:other:main", message: "hello" }),
).rejects.toMatchObject({ code: "cross-agent-denied" });
for (const field of ["senderAttribution", "suppressCommandInterpretation", "deliver"]) {
for (const field of [
"senderAttribution",
"suppressCommandInterpretation",
"deliver",
"personalAutomations",
]) {
await expect(
proxy.request(token, method, { key, message: "hello", [field]: true }),
).rejects.toMatchObject({ code: "method-not-allowed" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe("BrowserGatewayProxy model selection", () => {
message: "hello",
idempotencyKey: expect.any(String),
deliver: false,
personalAutomations: true,
senderAttribution: expect.objectContaining({ id: user.accountId, profileId: user.id }),
suppressCommandInterpretation: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ describe("BrowserGatewayProxy session lifecycle", () => {
agentId: binding.agentId,
deliver: false,
suppressCommandInterpretation: true,
personalAutomations: true,
senderAttribution: {
id: user.accountId,
name: user.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ describe("BrowserGatewayProxy", () => {
deliver: false,
queueMode: "steer",
expectedLeafEntryId: "leaf-1",
personalAutomations: true,
replyToId: "message-1",
senderAttribution: expect.objectContaining({
id: user.accountId,
Expand Down Expand Up @@ -760,6 +761,7 @@ describe("BrowserGatewayProxy", () => {
sessionKey: createdKey,
agentId: binding.agentId,
message: "hello",
personalAutomations: true,
idempotencyKey: expect.any(String),
deliver: false,
senderAttribution: expect.objectContaining({
Expand Down Expand Up @@ -806,6 +808,7 @@ describe("BrowserGatewayProxy", () => {
sessionKey: createdKey,
agentId: binding.agentId,
message: "",
personalAutomations: true,
attachments: [attachment],
idempotencyKey: expect.any(String),
deliver: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,11 @@ export class BrowserGatewayProxy {
const keyField = PLATFORMCLAW_WEB_SESSION_KEY_METHODS.get(method);
if (keyField) {
this.assertions.optionalAgentId(access.binding.agentId, params.agentId, method);
this.assertions.ownedSessionKey(access.binding.agentId, params[keyField], keyField);
const sessionKey = this.assertions.ownedSessionKey(
access.binding.agentId,
params[keyField],
keyField,
);
if (method === "chat.send" || method === "sessions.send" || method === "sessions.steer") {
const gatewayParams = { ...params };
delete gatewayParams["__controlUiReconnectResume"];
Expand All @@ -647,6 +651,7 @@ export class BrowserGatewayProxy {
deliver: false,
// Browser parameters are validated before this server-owned identity is added.
senderAttribution: createBrowserSenderAttribution(access),
...(this.spaceAccess.isPersonalSession(sessionKey) ? { personalAutomations: true } : {}),
};
}
// Never let a browser-selected run id cross the shared Gateway client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class BrowserGatewaySpaceAccess {
this.options.spaceService?.conversations.assertNativeRequest(access.user.id, method, params);
}

private isPersonalSession(sessionKey: string): boolean {
isPersonalSession(sessionKey: string): boolean {
return (
!isSpaceConversationSession(sessionKey) &&
!this.options.spaceService?.spaces.registeredConversation(sessionKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ describe("Space-created personal conversations", () => {
expect.objectContaining({
agentId: f.alice.binding.agentId,
sessionKey: conversation.sessionKey,
senderAttribution: expect.objectContaining({ profileId: f.alice.user.id }),
senderAttribution: expect.objectContaining({
profileId: f.alice.user.id,
isOwner: false,
}),
deliver: false,
}),
);
expect(f.request.mock.lastCall?.[1]).not.toHaveProperty("personalAutomations");
expect(await create(f)).toEqual(conversation);
expect(f.request.mock.calls.filter(([method]) => method === "sessions.create")).toHaveLength(1);
await expect(
Expand Down
29 changes: 29 additions & 0 deletions src/agents/agent-tools.personal-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createOpenClawCodingTools } from "./agent-tools.js";
import { createAgentToolsSandboxContext } from "./test-helpers/agent-tools-sandbox-context.js";
import { createHostSandboxFsBridge } from "./test-helpers/host-sandbox-fs-bridge.js";
import { AGENT_WORKSPACE_TOOL_NAMES } from "./tools/agent-workspace-tools.js";
import { AUTOMATIONS_TOOL_NAME } from "./tools/automations-tool-name.js";
import { readWorkspaceStateSnapshot } from "./workspace-state-store.js";

const roots: string[] = [];
Expand Down Expand Up @@ -73,6 +74,34 @@ function requireTool(tools: ReturnType<typeof createOpenClawCodingTools>, name:
}

describe("personal Agent workspace authority", () => {
it("exposes only the scoped automations tool for a matching browser grant", async () => {
const ctx = await fixture();
const ownerToolNames = (
senderAgentId: string,
personalAutomations: string | undefined,
sessionKey = "agent:main:main",
) =>
createOpenClawCodingTools({
agentId: "main",
workspaceDir: ctx.agentWorkspaceDir,
config: { tools: { profile: "full" } },
sessionKey,
senderIsOwner: false,
senderAgentId,
toolBindings: { personalAutomations },
wrapBeforeToolCallHook: false,
})
.filter((tool) => GATEWAY_OWNER_ONLY_CORE_TOOLS.some((name) => name === tool.name))
.map((tool) => tool.name);

expect(ownerToolNames("main", "main")).toEqual([AUTOMATIONS_TOOL_NAME]);
expect(ownerToolNames("main", undefined)).toEqual([]);
expect(ownerToolNames("foreign", "main")).toEqual([]);
expect(ownerToolNames("main", "foreign")).toEqual([]);
expect(ownerToolNames("main", "main", "agent:foreign:main")).toEqual([]);
expect(ownerToolNames("main", "main", "agent:main:cron:job")).toEqual([]);
});

it("allows the bound Agent profile and bootstrap without Gateway owner tools", async () => {
const ctx = await fixture();
const configPath = path.join(ctx.root, "openclaw.json");
Expand Down
Loading