Skip to content

Commit 1441a46

Browse files
committed
fix(gateway): stamp creation provenance on sessions.patch-created sessions
The sessions.patch create path skipped createdVia and createdActor, making the creator own session invisible under createSessionListEntryFilter when gateway.roles sets sessions.others=none. Thread resolveOperatorSessionCreation through sessions-patch-engine into projectSessionsPatchEntry and stamp only genuinely new rows, mirroring session-create-service. Preserve existing rows and placeholder aliases without restamping their provenance. Add an owner-boundary regression proving the identified creator can see and update a patch-created session. Follow-up of #128548.
1 parent a6a9f55 commit 1441a46

3 files changed

Lines changed: 82 additions & 14 deletions

File tree

‎src/gateway/server-methods/sessions-mutations.owner.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ensureProfileForEmail } from "../../state/user-profiles.js";
99
import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js";
1010
import { dispatchGatewayMethodInProcess } from "../server-plugins.js";
1111
import {
12+
createSessionListEntryFilter,
1213
resolveSessionMutationAuthorization,
1314
resolveSessionSharingRole,
1415
resolveSessionSharingTarget,
@@ -85,6 +86,69 @@ async function invoke(params: {
8586
return { authorization, requestContext, responses };
8687
}
8788

89+
describe("sessions.patch", () => {
90+
it("keeps a newly created session visible to its identified non-admin creator", async () => {
91+
await withOpenClawTestState({ scenario: "minimal" }, async (state) => {
92+
const profileId = ensureProfileForEmail("[email protected]").id;
93+
const sessionKey = "agent:main:patch-created";
94+
const requestClient = client(profileId);
95+
const cfg: OpenClawConfig = {
96+
gateway: {
97+
roles: {
98+
default: "member",
99+
definitions: {
100+
member: {
101+
sessions: { others: "none" },
102+
agents: "*",
103+
scopes: ["operator.write"],
104+
},
105+
},
106+
},
107+
},
108+
};
109+
const requestContext = context(cfg);
110+
const patch = async (pinned: boolean) => {
111+
const request = { key: sessionKey, pinned };
112+
const authorization = resolveSessionMutationAuthorization({
113+
client: requestClient,
114+
method: "sessions.patch",
115+
requestParams: request,
116+
context: requestContext,
117+
});
118+
expect(authorization.error).toBeNull();
119+
const respond = vi.fn();
120+
await sessionMutationHandlers["sessions.patch"]?.({
121+
params: request,
122+
client: requestClient,
123+
context: requestContext,
124+
sessionMutationAuthorization: authorization.authorization,
125+
respond,
126+
} as never);
127+
expect(respond).toHaveBeenCalledWith(true, expect.any(Object), undefined);
128+
};
129+
130+
await patch(true);
131+
const entry = loadSessionEntry({ agentId: "main", env: state.env, sessionKey });
132+
expect(entry).toMatchObject({
133+
createdVia: "operator",
134+
createdActor: { type: "human", id: profileId },
135+
createdAt: expect.any(Number),
136+
});
137+
if (!entry) {
138+
throw new Error("expected patch-created session entry");
139+
}
140+
expect(
141+
createSessionListEntryFilter({ client: requestClient, cfg })?.(sessionKey, entry),
142+
).toBe(true);
143+
144+
await patch(false);
145+
expect(
146+
loadSessionEntry({ agentId: "main", env: state.env, sessionKey })?.pinnedAt,
147+
).toBeUndefined();
148+
});
149+
});
150+
});
151+
88152
describe("sessions.assignOwner", () => {
89153
it("records the trusted in-process agent tool caller as the assigning agent", async () => {
90154
await withOpenClawTestState({ scenario: "minimal" }, async (state) => {

‎src/gateway/server-methods/sessions-patch-engine.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import { projectSessionsPatchEntry } from "../sessions-patch.js";
3636
import { gatewayClientSessionCreator } from "./gateway-client-identity.js";
3737
import { emitSessionsChanged } from "./session-change-event.js";
38+
import { resolveOperatorSessionCreation } from "./session-creation-provenance.js";
3839
import {
3940
prepareSessionPatchArchive,
4041
type SessionPatchArchivePreparation,
@@ -132,6 +133,7 @@ async function executeSessionPatchMutations(params: {
132133
}): Promise<MutationCoreResult> {
133134
const { client } = params;
134135
const cfg = params.context.getRuntimeConfig();
136+
const creation = resolveOperatorSessionCreation(client);
135137
const archiveActor = gatewayClientSessionCreator(client);
136138
const callerScopes = Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [];
137139
const callerCanManageCron = client === null || callerScopes.includes(ADMIN_SCOPE);
@@ -432,6 +434,7 @@ async function executeSessionPatchMutations(params: {
432434
}
433435
const projected = await projectSessionsPatchEntry({
434436
cfg,
437+
creation,
435438
existingEntry,
436439
isLabelInUse: (label) => labelOwners.isLabelInUse(label, candidateKeys),
437440
storeKey: primaryKey,

‎src/gateway/sessions-patch.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import type {
4040
InternalSessionEntry as SessionEntry,
4141
SessionToolOverrides,
4242
} from "../config/sessions.js";
43+
import {
44+
buildSessionCreationStamp,
45+
type SessionCreatedVia,
46+
} from "../config/sessions/session-entry-provenance.js";
4347
import { projectCanonicalSessionEntryShape } from "../config/sessions/store-entry-shape.js";
4448
import type { OpenClawConfig } from "../config/types.openclaw.js";
4549
import { normalizeExecTarget } from "../infra/exec-approvals.js";
@@ -167,6 +171,7 @@ function normalizeSessionToolOverrides(
167171
/** Project a validated gateway session patch for one session entry. */
168172
export async function projectSessionsPatchEntry(params: {
169173
cfg: OpenClawConfig;
174+
creation?: { via: SessionCreatedVia; actor?: SessionCreatedActor };
170175
existingEntry?: SessionEntry;
171176
isLabelInUse: (label: string) => boolean;
172177
storeKey: string;
@@ -178,7 +183,7 @@ export async function projectSessionsPatchEntry(params: {
178183
/** Exact harness owner authorized to project its new reserved session row. */
179184
authorizedAgentHarnessId?: string;
180185
}): Promise<{ ok: true; entry: SessionEntry } | { ok: false; error: ErrorShape }> {
181-
const { cfg, storeKey, patch } = params;
186+
const { cfg, storeKey, patch, creation } = params;
182187
const authorizedHarnessCreation =
183188
params.existingEntry === undefined &&
184189
isAgentHarnessSessionKeyOwnedBy(storeKey, params.authorizedAgentHarnessId);
@@ -245,20 +250,16 @@ export async function projectSessionsPatchEntry(params: {
245250
return loadedModelCatalog;
246251
};
247252

248-
const existing = params.existingEntry
249-
? projectCanonicalSessionEntryShape({ ...params.existingEntry })
250-
: undefined;
253+
const existing =
254+
params.existingEntry && projectCanonicalSessionEntryShape({ ...params.existingEntry });
251255
// Existing entries without session ids are placeholder aliases; assigning an id makes them real.
252-
const next: SessionEntry = existing?.sessionId
253-
? {
254-
...existing,
255-
updatedAt: Math.max(existing.updatedAt ?? 0, now),
256-
}
257-
: {
258-
...existing,
259-
sessionId: randomUUID(),
260-
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
261-
};
256+
const next: SessionEntry = {
257+
...existing,
258+
sessionId: existing?.sessionId || randomUUID(),
259+
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
260+
// Stamp only genuinely new rows; existing placeholder aliases must not be restamped.
261+
...(creation && params.existingEntry === undefined ? buildSessionCreationStamp(creation) : {}),
262+
};
262263
if (existing && !existing.sessionId) {
263264
delete next.label;
264265
delete next.category;

0 commit comments

Comments
 (0)