@clawos-dev/clawd 0.2.77-beta.145.522509b → 0.2.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.cjs +2349 -601
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -87,15 +87,16 @@ var init_methods = __esm({
87
87
  "git:branches",
88
88
  "capabilities:get",
89
89
  // ---- persona:* (persona 管理) ----
90
+ // persona:list 响应 shape 不分流(统一返 PersonaFile[]),但 daemon handler 按
91
+ // ConnectionContext 投影不同子集:
92
+ // owner ctx → 返全量 personas(含 private)
93
+ // guest ctx → persona.public === true ∧ assertGrant(ctx.grants, {persona,p.id}, 'read')
94
+ // method-grants.ts: persona:list 是 CAPABILITY_SCOPED;其它 persona:* 仍 ADMIN_ANY。
90
95
  "persona:create",
91
96
  "persona:list",
92
97
  "persona:get",
93
98
  "persona:update",
94
99
  "persona:delete",
95
- // token 颁发 / 吊销:file-sharing HTTP Bearer 鉴权用(PersonaRegistry.findByToken
96
- // 反查 personaId / label)。owner UI 在 PersonaSettingsDrawer "分享 Token" section 管理。
97
- "persona:issueToken",
98
- "persona:revokeToken",
99
100
  // ---- session:pty 双向透传(仅 CLAWD_CC_MODE=tui 才生效,对应 session:pty push 帧的上行) ----
100
101
  // session:pty:input — UI 把用户键盘字节(base64 UTF-8)发给 daemon,daemon 直写 pty stdin。
101
102
  // 高频帧(每次按键 1 帧),不入 reducer / 不广播。response 是 fire-and-forget 风格 ack。
@@ -106,14 +107,45 @@ var init_methods = __esm({
106
107
  // ---- attachment.* file-sharing(详见 attachment-schemas.ts) ----
107
108
  // 命名警告:这里的 `attachment.*` RPC 与 CC v2.x 上行 `type:"attachment"` 系统行
108
109
  // (attachment-skills / attachment-deferred-tools / attachment_memories)是不同概念。
109
- // 全部管理类 RPC handler 入口 requireOwnerpersonal token 调任意一个都 403);
110
- // 实际文件传输走 HTTP 路由(GET /files?p=&e=&s=,签名验证),不在白名单内。
110
+ // 全部管理类 RPC dispatcher grant 限 ownercapability admin-only);
111
+ // 实际文件传输走 HTTP 路由(GET /files?p=&e=&s=,HMAC 签名自包含,不在白名单内)。
111
112
  "attachment.signUrl",
112
113
  "attachment.groupAdd",
113
114
  "attachment.groupRemove",
114
115
  "attachment.groupList",
115
- // v2:跨 session 聚合(本期 UI 不调,保留槽位用于 HTTP ACL 内部判定 / 未来 "All files" tab)
116
- "attachment.groupListPersona",
116
+ // ---- capability:* (capability platform 鉴权底座) ----
117
+ // owner 颁发 / 列出 / 删除给 guest 的 capability。三者均需 admin 权限(METHOD_GRANT_MAP
118
+ // 在 daemon 端固定为 `{ resource: '*', action: 'admin' }`,owner 自动满足)。
119
+ // 颁发后 daemon 推 'capability:tokenIssued' 帧;删除推 'capability:tokenDeleted' 帧。
120
+ // 删除是 hard delete:CapabilityStore 物理移除 + 关该 cap 的所有活跃 ws + rm
121
+ // personas/<pid>/.clawd/sessions/guests/<capId>/ guest sessions 目录。
122
+ "capability:issue",
123
+ "capability:list",
124
+ "capability:delete",
125
+ // bidirectional cap 视角 B 2026-05-23: owner 主动告诉 daemon "我颁的某 cap 给了谁"
126
+ // (UI 在 accept dialog 4 步流程里调; 等价于 recordPeerHello 但不经 ws auth path).
127
+ // 否则 B 端颁回的 cap 因为 A 不连 B → peerOwnerId 永不回写 → useContacts 派生 pending.
128
+ "capability:bindPeer",
129
+ // ---- inbox:* (capability platform Phase 3 跨用户通知 + Phase 4 DM) ----
130
+ // owner 接 guest 的 cross-principal 消息事件 + DM 双向私聊.
131
+ // inbox:list / markRead: admin-only (Phase 3); inbox:postMessage: DM 自带能力,
132
+ // 任何 principal (owner/guest) 可调 (Phase 4).
133
+ "inbox:list",
134
+ "inbox:markRead",
135
+ "inbox:postMessage",
136
+ // ---- received-capability:* (对方颁给我的 cap, 视角 B 双向授权模型, 2026-05-23) ----
137
+ // owner 入口: list / add / remove
138
+ // guest 端: autoAttach (B 接 A 的 token 后通过 cap channel ws 推 cap 给 A)
139
+ // 详见 received-capability.ts JSDoc + spec 2026-05-23-bidirectional-cap-design.md
140
+ "received-capability:list",
141
+ "received-capability:add",
142
+ "received-capability:remove",
143
+ "received-capability:autoAttach",
144
+ // ---- whoami (v2 capability platform) ----
145
+ // 任何 authed connection 都能调;返回 owner 显示名 + 当前 capability wire 形态 +
146
+ // grants 对应的 persona 元数据 (id + displayName)。UI 端 AddRemotePersonaDialog
147
+ // 的 preview 一次性临时 client 用它判定身份和可用 persona。
148
+ "whoami",
117
149
  "info",
118
150
  "ping"
119
151
  ];
@@ -609,8 +641,8 @@ var init_parseUtil = __esm({
609
641
  init_errors2();
610
642
  init_en();
611
643
  makeIssue = (params) => {
612
- const { data, path: path32, errorMaps, issueData } = params;
613
- const fullPath = [...path32, ...issueData.path || []];
644
+ const { data, path: path39, errorMaps, issueData } = params;
645
+ const fullPath = [...path39, ...issueData.path || []];
614
646
  const fullIssue = {
615
647
  ...issueData,
616
648
  path: fullPath
@@ -921,11 +953,11 @@ var init_types = __esm({
921
953
  init_parseUtil();
922
954
  init_util();
923
955
  ParseInputLazyPath = class {
924
- constructor(parent, value, path32, key) {
956
+ constructor(parent, value, path39, key) {
925
957
  this._cachedPath = [];
926
958
  this.parent = parent;
927
959
  this.data = value;
928
- this._path = path32;
960
+ this._path = path39;
929
961
  this._key = key;
930
962
  }
931
963
  get path() {
@@ -4309,12 +4341,12 @@ var init_zod = __esm({
4309
4341
  });
4310
4342
 
4311
4343
  // ../protocol/src/attachment-schemas.ts
4312
- var TOKEN_ROLES, GROUP_FILE_SOURCES, GroupFileEntrySchema, AttachmentSignUrlArgs, AttachmentSignUrlResponseSchema, AttachmentGroupAddArgs, AttachmentGroupAddResponseSchema, AttachmentGroupRemoveArgs, AttachmentGroupRemoveResponseSchema, AttachmentGroupListArgs, AttachmentGroupListResponseSchema, AttachmentGroupListPersonaArgs, AttachmentGroupListPersonaResponseSchema;
4344
+ var TOKEN_ROLES, GROUP_FILE_SOURCES, GroupFileEntrySchema, AttachmentSignUrlArgs, AttachmentSignUrlResponseSchema, AttachmentGroupAddArgs, AttachmentGroupAddResponseSchema, AttachmentGroupRemoveArgs, AttachmentGroupRemoveResponseSchema, AttachmentGroupListArgs, AttachmentGroupListResponseSchema;
4313
4345
  var init_attachment_schemas = __esm({
4314
4346
  "../protocol/src/attachment-schemas.ts"() {
4315
4347
  "use strict";
4316
4348
  init_zod();
4317
- TOKEN_ROLES = ["owner", "personal"];
4349
+ TOKEN_ROLES = ["owner"];
4318
4350
  GROUP_FILE_SOURCES = ["agent", "owner"];
4319
4351
  GroupFileEntrySchema = external_exports.object({
4320
4352
  /** daemon 派发的稳定 id(用于 RPC remove / UI key) */
@@ -4370,31 +4402,15 @@ var init_attachment_schemas = __esm({
4370
4402
  AttachmentGroupListResponseSchema = external_exports.object({
4371
4403
  entries: external_exports.array(GroupFileEntrySchema)
4372
4404
  });
4373
- AttachmentGroupListPersonaArgs = external_exports.object({
4374
- personaId: external_exports.string().min(1)
4375
- });
4376
- AttachmentGroupListPersonaResponseSchema = external_exports.object({
4377
- perSession: external_exports.array(
4378
- external_exports.object({
4379
- sessionId: external_exports.string().min(1),
4380
- entries: external_exports.array(GroupFileEntrySchema)
4381
- })
4382
- )
4383
- });
4384
4405
  }
4385
4406
  });
4386
4407
 
4387
4408
  // ../protocol/src/persona-schemas.ts
4388
- var PersonaTokenEntrySchema, PersonaFileSchema, PersonaSkillSummarySchema, PersonaPluginSummarySchema, PersonaSandboxSettingsSchema, PersonaInfoResponseSchema, PersonaCreateArgsSchema, PersonaIdArgsSchema, PersonaUpdateArgsSchema, PersonaIssueTokenArgsSchema, PersonaRevokeTokenArgsSchema;
4409
+ var PersonaFileSchema, PersonaSkillSummarySchema, PersonaPluginSummarySchema, PersonaSandboxSettingsSchema, PersonaInfoResponseSchema, PersonaCreateArgsSchema, PersonaIdArgsSchema, PersonaUpdateArgsSchema;
4389
4410
  var init_persona_schemas = __esm({
4390
4411
  "../protocol/src/persona-schemas.ts"() {
4391
4412
  "use strict";
4392
4413
  init_zod();
4393
- PersonaTokenEntrySchema = external_exports.object({
4394
- label: external_exports.string(),
4395
- issuedAt: external_exports.number(),
4396
- revoked: external_exports.boolean()
4397
- });
4398
4414
  PersonaFileSchema = external_exports.object({
4399
4415
  personaId: external_exports.string(),
4400
4416
  label: external_exports.string(),
@@ -4403,13 +4419,6 @@ var init_persona_schemas = __esm({
4403
4419
  // 8-key set defined UI-side in `lib/session-icons.ts`; protocol stays plain string
4404
4420
  // for forward compatibility, consumer fallbacks to default when key not found.
4405
4421
  iconKey: external_exports.string().optional(),
4406
- /**
4407
- * Persona token 列表:file-sharing HTTP Bearer 鉴权用。
4408
- * - 写:`PersonaManager.issueToken` / `revokeToken`
4409
- * - 读:`PersonaRegistry.findByToken` 反向查表(需 persona.public === true)
4410
- * - optional:兼容历史 persona.json 不含此字段的情形(旧 daemon 写文件时省略)
4411
- */
4412
- tokenMap: external_exports.record(external_exports.string(), PersonaTokenEntrySchema).optional(),
4413
4422
  createdAt: external_exports.number(),
4414
4423
  updatedAt: external_exports.number()
4415
4424
  }).strict();
@@ -4468,14 +4477,6 @@ var init_persona_schemas = __esm({
4468
4477
  iconKey: external_exports.string().nullable().optional()
4469
4478
  }).strict()
4470
4479
  }).strict();
4471
- PersonaIssueTokenArgsSchema = external_exports.object({
4472
- personaId: external_exports.string().min(1),
4473
- label: external_exports.string().min(1)
4474
- });
4475
- PersonaRevokeTokenArgsSchema = external_exports.object({
4476
- personaId: external_exports.string().min(1),
4477
- token: external_exports.string().min(1)
4478
- });
4479
4480
  }
4480
4481
  });
4481
4482
 
@@ -4582,6 +4583,15 @@ var init_schemas = __esm({
4582
4583
  // 才能让 alice 重连同一 sub-session(持久化在 alice localStorage 之外,由 daemon push 同步)。
4583
4584
  // optional 是因为 owner-mode session 不带;只要写入就是 listener-mode 必填三元组。
4584
4585
  chatId: external_exports.string().min(1).optional(),
4586
+ /**
4587
+ * 创建该 session 的 principal id(owner uuid 或 guest cap.id),从 session:create handler
4588
+ * 的 ctx.principal.id 派生。Person identity Q2「按对端人聚合 sessions」需要它:
4589
+ * UI 端按 `creatorPrincipalId === ownerPrincipalId` 区分「我自己」vs「别人接入」,
4590
+ * 后者再通过 PersonAlias 表反查 personId 二级分组。
4591
+ *
4592
+ * optional:兼容 2026-05-21 之前的老 session 数据,新建一定有值。
4593
+ */
4594
+ creatorPrincipalId: external_exports.string().min(1).optional(),
4585
4595
  createdAt: external_exports.string().min(1),
4586
4596
  updatedAt: external_exports.string().min(1)
4587
4597
  });
@@ -4991,7 +5001,20 @@ var init_schemas = __esm({
4991
5001
  AuthRequestFrameSchema = external_exports.object({
4992
5002
  type: external_exports.literal("auth"),
4993
5003
  token: external_exports.string().min(1),
4994
- scheme: external_exports.literal("bearer").optional()
5004
+ scheme: external_exports.literal("bearer").optional(),
5005
+ /**
5006
+ * guest 端 ws 接入时自报对端稳定 ownerPrincipalId。AuthGate.authenticate 收到
5007
+ * 后调 capabilityManager.recordPeerHello(capId, ownerPrincipalId, displayName)
5008
+ * 把 cap.peerOwnerId / firstUsedByPeerAt 字段回写,让 owner 端 UI 在 People
5009
+ * tab 顶层 PeerOwner 视图 + Personas tab 二级分组拿到对端身份。
5010
+ */
5011
+ selfPrincipalId: external_exports.string().min(1).optional(),
5012
+ /**
5013
+ * 同 selfPrincipalId:自报 owner displayName(人类可读),让对端 daemon 把
5014
+ * 显示名写入 cap.peerOwnerDisplayName(cap.displayName 本来就是 owner 颁时填
5015
+ * 的"颁给谁/用途",hello 收到的 displayName 是辅助)。
5016
+ */
5017
+ selfDisplayName: external_exports.string().optional()
4995
5018
  });
4996
5019
  AuthOkFrameSchema = external_exports.object({
4997
5020
  type: external_exports.literal("auth:ok"),
@@ -5029,10 +5052,9 @@ var init_schemas = __esm({
5029
5052
  // PR 2 daemon 实现 single HTTP server + auth-context 后,daemon 必返这五个字段,
5030
5053
  // 届时可考虑改成 required。spec §11 第 3 条:"tokenRole 是协议字段,daemon 查 token
5031
5054
  // 表后显式下发,UI 不自行推导"——所以 UI 一律读这里,禁止本地推导。
5032
- /** 'owner' = 拿到 owner-token 的连接(不限来源 IP);'personal' = persona 派发的访客 token。来源 TOKEN_ROLES(spec §11 #1 中央真理源) */
5055
+ /** 'owner' = 拿到 owner-token 的连接(不限来源 IP)。来源 TOKEN_ROLES(spec §11 #1 中央真理源)。
5056
+ * 历史 'personal' role(persona file-sharing token)在 2026-05-21 删除;tokenPersonaId 一同移除。 */
5033
5057
  tokenRole: external_exports.enum(TOKEN_ROLES).optional(),
5034
- /** tokenRole='personal' 时携带(绑定到具体 persona);owner 不携带 */
5035
- tokenPersonaId: external_exports.string().min(1).optional(),
5036
5058
  /** socket.remoteAddress 是否落在 127.0.0.1 / ::1;本期无 RPC 消费,保留协议槽位给未来 "Reveal in Finder" 等本机动作 */
5037
5059
  isLoopback: external_exports.boolean().optional(),
5038
5060
  /** UI 拼 file-sharing HTTP 路由的前缀(http(s)://host:port),不含尾斜杠;frpc 反代时返反代地址 */
@@ -5059,6 +5081,194 @@ var init_persona_mode = __esm({
5059
5081
  }
5060
5082
  });
5061
5083
 
5084
+ // ../protocol/src/principal.ts
5085
+ function makeOwnerPrincipal(id, displayName) {
5086
+ return PrincipalSchema.parse({ id, kind: "owner", displayName });
5087
+ }
5088
+ var PrincipalKindSchema, PrincipalSchema;
5089
+ var init_principal = __esm({
5090
+ "../protocol/src/principal.ts"() {
5091
+ "use strict";
5092
+ init_zod();
5093
+ PrincipalKindSchema = external_exports.enum(["owner", "guest"]);
5094
+ PrincipalSchema = external_exports.object({
5095
+ id: external_exports.string().min(1),
5096
+ kind: PrincipalKindSchema,
5097
+ displayName: external_exports.string()
5098
+ }).strict();
5099
+ }
5100
+ });
5101
+
5102
+ // ../protocol/src/capability.ts
5103
+ function stripSecretHash(cap) {
5104
+ const { secretHash: _hash, ...wire } = cap;
5105
+ return wire;
5106
+ }
5107
+ var ResourceSchema, ActionSchema, GrantSchema, CapabilitySchema, CapabilityWireSchema, CapabilityErrorCodeSchema, WhoamiResponseSchema, CapabilityIssueArgsSchema, CapabilityBindPeerArgsSchema;
5108
+ var init_capability = __esm({
5109
+ "../protocol/src/capability.ts"() {
5110
+ "use strict";
5111
+ init_zod();
5112
+ ResourceSchema = external_exports.discriminatedUnion("type", [
5113
+ external_exports.object({ type: external_exports.literal("persona"), id: external_exports.string().min(1) }).strict(),
5114
+ external_exports.object({ type: external_exports.literal("chat"), id: external_exports.string().min(1) }).strict(),
5115
+ external_exports.object({ type: external_exports.literal("*") }).strict()
5116
+ ]);
5117
+ ActionSchema = external_exports.enum(["read", "send", "admin"]);
5118
+ GrantSchema = external_exports.object({
5119
+ resource: ResourceSchema,
5120
+ actions: external_exports.array(ActionSchema).min(1)
5121
+ }).strict();
5122
+ CapabilitySchema = external_exports.object({
5123
+ id: external_exports.string().min(1),
5124
+ // sha256(token) hex 64
5125
+ secretHash: external_exports.string().regex(/^[a-f0-9]{64}$/),
5126
+ displayName: external_exports.string(),
5127
+ // 空数组合法 = 纯访客(只能 DM)
5128
+ grants: external_exports.array(GrantSchema),
5129
+ issuedAt: external_exports.number().int().nonnegative(),
5130
+ expiresAt: external_exports.number().int().positive().optional(),
5131
+ maxUses: external_exports.number().int().positive().optional(),
5132
+ usedCount: external_exports.number().int().nonnegative(),
5133
+ /**
5134
+ * guest 在 auth 帧捎带 selfPrincipalId 时由 AuthGate.authenticate 回写;
5135
+ * 未被消费的 cap 此字段缺失。
5136
+ */
5137
+ peerOwnerId: external_exports.string().min(1).optional(),
5138
+ /** 首次 client hello 时间戳(与 peerOwnerId 同步写入) */
5139
+ firstUsedByPeerAt: external_exports.number().int().positive().optional(),
5140
+ /**
5141
+ * 对方 daemon owner 的 displayName,guest auth 帧 selfDisplayName 触发
5142
+ * recordPeerHello 时同步回写。视角 B (2026-05-23) UI 派生联系人列表用此字段
5143
+ * 显示对方名字(避免 fallback 到我颁 cap 时填的 displayName)。
5144
+ */
5145
+ peerDisplayName: external_exports.string().optional()
5146
+ }).strict();
5147
+ CapabilityWireSchema = CapabilitySchema.omit({ secretHash: true });
5148
+ CapabilityErrorCodeSchema = external_exports.enum([
5149
+ "TOKEN_INVALID",
5150
+ "TOKEN_REVOKED",
5151
+ "TOKEN_EXPIRED",
5152
+ "TOKEN_EXHAUSTED"
5153
+ ]);
5154
+ WhoamiResponseSchema = external_exports.object({
5155
+ type: external_exports.literal("whoami:ok"),
5156
+ owner: external_exports.object({
5157
+ id: external_exports.string(),
5158
+ kind: external_exports.enum(["owner", "guest"]),
5159
+ displayName: external_exports.string()
5160
+ }).strict(),
5161
+ capability: CapabilityWireSchema,
5162
+ grantedPersonas: external_exports.array(
5163
+ external_exports.object({
5164
+ id: external_exports.string().min(1),
5165
+ displayName: external_exports.string()
5166
+ }).strict()
5167
+ )
5168
+ }).strict();
5169
+ CapabilityIssueArgsSchema = external_exports.object({
5170
+ displayName: external_exports.string().min(1),
5171
+ grants: external_exports.array(GrantSchema),
5172
+ expiresAt: external_exports.number().int().positive().optional(),
5173
+ maxUses: external_exports.number().int().positive().optional()
5174
+ }).strict();
5175
+ CapabilityBindPeerArgsSchema = external_exports.object({
5176
+ capabilityId: external_exports.string().min(1),
5177
+ peerOwnerId: external_exports.string().min(1),
5178
+ peerDisplayName: external_exports.string()
5179
+ }).strict();
5180
+ }
5181
+ });
5182
+
5183
+ // ../protocol/src/inbox.ts
5184
+ var InboxMessageSchema, InboxPostMessageArgsSchema, InboxListArgsSchema, InboxMarkReadArgsSchema;
5185
+ var init_inbox = __esm({
5186
+ "../protocol/src/inbox.ts"() {
5187
+ "use strict";
5188
+ init_zod();
5189
+ InboxMessageSchema = external_exports.object({
5190
+ id: external_exports.string().min(1),
5191
+ peerOwnerId: external_exports.string().min(1),
5192
+ senderPrincipalId: external_exports.string().min(1),
5193
+ text: external_exports.string().min(1),
5194
+ createdAt: external_exports.number().int().nonnegative(),
5195
+ readBy: external_exports.record(external_exports.string().min(1), external_exports.number().int().positive()).default({})
5196
+ }).strict();
5197
+ InboxPostMessageArgsSchema = external_exports.object({
5198
+ peerOwnerId: external_exports.string().min(1).optional(),
5199
+ id: external_exports.string().min(1),
5200
+ text: external_exports.string().min(1),
5201
+ createdAt: external_exports.number().int().nonnegative()
5202
+ }).strict();
5203
+ InboxListArgsSchema = external_exports.object({
5204
+ peerOwnerId: external_exports.string().min(1),
5205
+ sinceCreatedAt: external_exports.number().int().nonnegative().optional()
5206
+ }).strict();
5207
+ InboxMarkReadArgsSchema = external_exports.object({
5208
+ peerOwnerId: external_exports.string().min(1),
5209
+ upToCreatedAt: external_exports.number().int().nonnegative()
5210
+ }).strict();
5211
+ }
5212
+ });
5213
+
5214
+ // ../protocol/src/received-capability.ts
5215
+ var ReceivedCapabilitySchema, ReceivedCapabilityWireSchema, ReceivedCapabilityAddArgsSchema, ReceivedCapabilityAddOkSchema, ReceivedCapabilityRemoveArgsSchema, ReceivedCapabilityRemoveOkSchema, ReceivedCapabilityListOkSchema, ReceivedCapabilityAutoAttachArgsSchema, ReceivedCapabilityAutoAttachOkSchema, ReceivedCapabilityAddedFrameSchema, ReceivedCapabilityRemovedFrameSchema;
5216
+ var init_received_capability = __esm({
5217
+ "../protocol/src/received-capability.ts"() {
5218
+ "use strict";
5219
+ init_zod();
5220
+ init_capability();
5221
+ ReceivedCapabilitySchema = external_exports.object({
5222
+ ownerPrincipalId: external_exports.string().min(1),
5223
+ ownerDisplayName: external_exports.string(),
5224
+ remoteUrl: external_exports.string().min(1),
5225
+ capabilityId: external_exports.string().min(1),
5226
+ capabilityToken: external_exports.string().min(1),
5227
+ grants: external_exports.array(GrantSchema),
5228
+ receivedAt: external_exports.number().int().nonnegative()
5229
+ }).strict();
5230
+ ReceivedCapabilityWireSchema = ReceivedCapabilitySchema;
5231
+ ReceivedCapabilityAddArgsSchema = external_exports.object({
5232
+ remoteUrl: external_exports.string().min(1),
5233
+ token: external_exports.string().min(1)
5234
+ }).strict();
5235
+ ReceivedCapabilityAddOkSchema = external_exports.object({
5236
+ type: external_exports.literal("received-capability:add:ok"),
5237
+ receivedCap: ReceivedCapabilityWireSchema
5238
+ }).strict();
5239
+ ReceivedCapabilityRemoveArgsSchema = external_exports.object({
5240
+ ownerPrincipalId: external_exports.string().min(1)
5241
+ }).strict();
5242
+ ReceivedCapabilityRemoveOkSchema = external_exports.object({
5243
+ type: external_exports.literal("received-capability:remove:ok"),
5244
+ ownerPrincipalId: external_exports.string().min(1)
5245
+ }).strict();
5246
+ ReceivedCapabilityListOkSchema = external_exports.object({
5247
+ type: external_exports.literal("received-capability:list:ok"),
5248
+ receivedCaps: external_exports.array(ReceivedCapabilityWireSchema)
5249
+ }).strict();
5250
+ ReceivedCapabilityAutoAttachArgsSchema = external_exports.object({
5251
+ ownerPrincipalId: external_exports.string().min(1),
5252
+ ownerDisplayName: external_exports.string(),
5253
+ remoteUrl: external_exports.string().min(1),
5254
+ capabilityId: external_exports.string().min(1),
5255
+ token: external_exports.string().min(1),
5256
+ grants: external_exports.array(GrantSchema)
5257
+ }).strict();
5258
+ ReceivedCapabilityAutoAttachOkSchema = external_exports.object({
5259
+ type: external_exports.literal("received-capability:autoAttach:ok")
5260
+ }).strict();
5261
+ ReceivedCapabilityAddedFrameSchema = external_exports.object({
5262
+ type: external_exports.literal("received-capability:added"),
5263
+ receivedCap: ReceivedCapabilityWireSchema
5264
+ }).strict();
5265
+ ReceivedCapabilityRemovedFrameSchema = external_exports.object({
5266
+ type: external_exports.literal("received-capability:removed"),
5267
+ ownerPrincipalId: external_exports.string().min(1)
5268
+ }).strict();
5269
+ }
5270
+ });
5271
+
5062
5272
  // ../protocol/src/runtime.ts
5063
5273
  var init_runtime = __esm({
5064
5274
  "../protocol/src/runtime.ts"() {
@@ -5071,6 +5281,10 @@ var init_runtime = __esm({
5071
5281
  init_persona_schemas();
5072
5282
  init_persona_mode();
5073
5283
  init_attachment_schemas();
5284
+ init_principal();
5285
+ init_capability();
5286
+ init_inbox();
5287
+ init_received_capability();
5074
5288
  }
5075
5289
  });
5076
5290
 
@@ -5345,8 +5559,8 @@ var require_req = __commonJS({
5345
5559
  if (req.originalUrl) {
5346
5560
  _req.url = req.originalUrl;
5347
5561
  } else {
5348
- const path32 = req.path;
5349
- _req.url = typeof path32 === "string" ? path32 : req.url ? req.url.path || req.url : void 0;
5562
+ const path39 = req.path;
5563
+ _req.url = typeof path39 === "string" ? path39 : req.url ? req.url.path || req.url : void 0;
5350
5564
  }
5351
5565
  if (req.query) {
5352
5566
  _req.query = req.query;
@@ -5511,14 +5725,14 @@ var require_redact = __commonJS({
5511
5725
  }
5512
5726
  return obj;
5513
5727
  }
5514
- function parsePath(path32) {
5728
+ function parsePath(path39) {
5515
5729
  const parts = [];
5516
5730
  let current = "";
5517
5731
  let inBrackets = false;
5518
5732
  let inQuotes = false;
5519
5733
  let quoteChar = "";
5520
- for (let i = 0; i < path32.length; i++) {
5521
- const char = path32[i];
5734
+ for (let i = 0; i < path39.length; i++) {
5735
+ const char = path39[i];
5522
5736
  if (!inBrackets && char === ".") {
5523
5737
  if (current) {
5524
5738
  parts.push(current);
@@ -5649,10 +5863,10 @@ var require_redact = __commonJS({
5649
5863
  return current;
5650
5864
  }
5651
5865
  function redactPaths(obj, paths, censor, remove = false) {
5652
- for (const path32 of paths) {
5653
- const parts = parsePath(path32);
5866
+ for (const path39 of paths) {
5867
+ const parts = parsePath(path39);
5654
5868
  if (parts.includes("*")) {
5655
- redactWildcardPath(obj, parts, censor, path32, remove);
5869
+ redactWildcardPath(obj, parts, censor, path39, remove);
5656
5870
  } else {
5657
5871
  if (remove) {
5658
5872
  removeKey(obj, parts);
@@ -5737,8 +5951,8 @@ var require_redact = __commonJS({
5737
5951
  }
5738
5952
  } else {
5739
5953
  if (afterWildcard.includes("*")) {
5740
- const wrappedCensor = typeof censor === "function" ? (value, path32) => {
5741
- const fullPath = [...pathArray.slice(0, pathLength), ...path32];
5954
+ const wrappedCensor = typeof censor === "function" ? (value, path39) => {
5955
+ const fullPath = [...pathArray.slice(0, pathLength), ...path39];
5742
5956
  return censor(value, fullPath);
5743
5957
  } : censor;
5744
5958
  redactWildcardPath(current, afterWildcard, wrappedCensor, originalPath, remove);
@@ -5773,8 +5987,8 @@ var require_redact = __commonJS({
5773
5987
  return null;
5774
5988
  }
5775
5989
  const pathStructure = /* @__PURE__ */ new Map();
5776
- for (const path32 of pathsToClone) {
5777
- const parts = parsePath(path32);
5990
+ for (const path39 of pathsToClone) {
5991
+ const parts = parsePath(path39);
5778
5992
  let current = pathStructure;
5779
5993
  for (let i = 0; i < parts.length; i++) {
5780
5994
  const part = parts[i];
@@ -5826,24 +6040,24 @@ var require_redact = __commonJS({
5826
6040
  }
5827
6041
  return cloneSelectively(obj, pathStructure);
5828
6042
  }
5829
- function validatePath(path32) {
5830
- if (typeof path32 !== "string") {
6043
+ function validatePath(path39) {
6044
+ if (typeof path39 !== "string") {
5831
6045
  throw new Error("Paths must be (non-empty) strings");
5832
6046
  }
5833
- if (path32 === "") {
6047
+ if (path39 === "") {
5834
6048
  throw new Error("Invalid redaction path ()");
5835
6049
  }
5836
- if (path32.includes("..")) {
5837
- throw new Error(`Invalid redaction path (${path32})`);
6050
+ if (path39.includes("..")) {
6051
+ throw new Error(`Invalid redaction path (${path39})`);
5838
6052
  }
5839
- if (path32.includes(",")) {
5840
- throw new Error(`Invalid redaction path (${path32})`);
6053
+ if (path39.includes(",")) {
6054
+ throw new Error(`Invalid redaction path (${path39})`);
5841
6055
  }
5842
6056
  let bracketCount = 0;
5843
6057
  let inQuotes = false;
5844
6058
  let quoteChar = "";
5845
- for (let i = 0; i < path32.length; i++) {
5846
- const char = path32[i];
6059
+ for (let i = 0; i < path39.length; i++) {
6060
+ const char = path39[i];
5847
6061
  if ((char === '"' || char === "'") && bracketCount > 0) {
5848
6062
  if (!inQuotes) {
5849
6063
  inQuotes = true;
@@ -5857,20 +6071,20 @@ var require_redact = __commonJS({
5857
6071
  } else if (char === "]" && !inQuotes) {
5858
6072
  bracketCount--;
5859
6073
  if (bracketCount < 0) {
5860
- throw new Error(`Invalid redaction path (${path32})`);
6074
+ throw new Error(`Invalid redaction path (${path39})`);
5861
6075
  }
5862
6076
  }
5863
6077
  }
5864
6078
  if (bracketCount !== 0) {
5865
- throw new Error(`Invalid redaction path (${path32})`);
6079
+ throw new Error(`Invalid redaction path (${path39})`);
5866
6080
  }
5867
6081
  }
5868
6082
  function validatePaths(paths) {
5869
6083
  if (!Array.isArray(paths)) {
5870
6084
  throw new TypeError("paths must be an array");
5871
6085
  }
5872
- for (const path32 of paths) {
5873
- validatePath(path32);
6086
+ for (const path39 of paths) {
6087
+ validatePath(path39);
5874
6088
  }
5875
6089
  }
5876
6090
  function slowRedact(options = {}) {
@@ -6038,8 +6252,8 @@ var require_redaction = __commonJS({
6038
6252
  if (shape[k2] === null) {
6039
6253
  o[k2] = (value) => topCensor(value, [k2]);
6040
6254
  } else {
6041
- const wrappedCensor = typeof censor === "function" ? (value, path32) => {
6042
- return censor(value, [k2, ...path32]);
6255
+ const wrappedCensor = typeof censor === "function" ? (value, path39) => {
6256
+ return censor(value, [k2, ...path39]);
6043
6257
  } : censor;
6044
6258
  o[k2] = Redact({
6045
6259
  paths: shape[k2],
@@ -6257,10 +6471,10 @@ var require_atomic_sleep = __commonJS({
6257
6471
  var require_sonic_boom = __commonJS({
6258
6472
  "../node_modules/.pnpm/sonic-boom@4.2.1/node_modules/sonic-boom/index.js"(exports2, module2) {
6259
6473
  "use strict";
6260
- var fs28 = require("fs");
6474
+ var fs34 = require("fs");
6261
6475
  var EventEmitter2 = require("events");
6262
6476
  var inherits = require("util").inherits;
6263
- var path32 = require("path");
6477
+ var path39 = require("path");
6264
6478
  var sleep = require_atomic_sleep();
6265
6479
  var assert = require("assert");
6266
6480
  var BUSY_WRITE_TIMEOUT = 100;
@@ -6314,20 +6528,20 @@ var require_sonic_boom = __commonJS({
6314
6528
  const mode = sonic.mode;
6315
6529
  if (sonic.sync) {
6316
6530
  try {
6317
- if (sonic.mkdir) fs28.mkdirSync(path32.dirname(file), { recursive: true });
6318
- const fd = fs28.openSync(file, flags, mode);
6531
+ if (sonic.mkdir) fs34.mkdirSync(path39.dirname(file), { recursive: true });
6532
+ const fd = fs34.openSync(file, flags, mode);
6319
6533
  fileOpened(null, fd);
6320
6534
  } catch (err) {
6321
6535
  fileOpened(err);
6322
6536
  throw err;
6323
6537
  }
6324
6538
  } else if (sonic.mkdir) {
6325
- fs28.mkdir(path32.dirname(file), { recursive: true }, (err) => {
6539
+ fs34.mkdir(path39.dirname(file), { recursive: true }, (err) => {
6326
6540
  if (err) return fileOpened(err);
6327
- fs28.open(file, flags, mode, fileOpened);
6541
+ fs34.open(file, flags, mode, fileOpened);
6328
6542
  });
6329
6543
  } else {
6330
- fs28.open(file, flags, mode, fileOpened);
6544
+ fs34.open(file, flags, mode, fileOpened);
6331
6545
  }
6332
6546
  }
6333
6547
  function SonicBoom(opts) {
@@ -6368,8 +6582,8 @@ var require_sonic_boom = __commonJS({
6368
6582
  this.flush = flushBuffer;
6369
6583
  this.flushSync = flushBufferSync;
6370
6584
  this._actualWrite = actualWriteBuffer;
6371
- fsWriteSync = () => fs28.writeSync(this.fd, this._writingBuf);
6372
- fsWrite = () => fs28.write(this.fd, this._writingBuf, this.release);
6585
+ fsWriteSync = () => fs34.writeSync(this.fd, this._writingBuf);
6586
+ fsWrite = () => fs34.write(this.fd, this._writingBuf, this.release);
6373
6587
  } else if (contentMode === void 0 || contentMode === kContentModeUtf8) {
6374
6588
  this._writingBuf = "";
6375
6589
  this.write = write;
@@ -6378,15 +6592,15 @@ var require_sonic_boom = __commonJS({
6378
6592
  this._actualWrite = actualWrite;
6379
6593
  fsWriteSync = () => {
6380
6594
  if (Buffer.isBuffer(this._writingBuf)) {
6381
- return fs28.writeSync(this.fd, this._writingBuf);
6595
+ return fs34.writeSync(this.fd, this._writingBuf);
6382
6596
  }
6383
- return fs28.writeSync(this.fd, this._writingBuf, "utf8");
6597
+ return fs34.writeSync(this.fd, this._writingBuf, "utf8");
6384
6598
  };
6385
6599
  fsWrite = () => {
6386
6600
  if (Buffer.isBuffer(this._writingBuf)) {
6387
- return fs28.write(this.fd, this._writingBuf, this.release);
6601
+ return fs34.write(this.fd, this._writingBuf, this.release);
6388
6602
  }
6389
- return fs28.write(this.fd, this._writingBuf, "utf8", this.release);
6603
+ return fs34.write(this.fd, this._writingBuf, "utf8", this.release);
6390
6604
  };
6391
6605
  } else {
6392
6606
  throw new Error(`SonicBoom supports "${kContentModeUtf8}" and "${kContentModeBuffer}", but passed ${contentMode}`);
@@ -6443,7 +6657,7 @@ var require_sonic_boom = __commonJS({
6443
6657
  }
6444
6658
  }
6445
6659
  if (this._fsync) {
6446
- fs28.fsyncSync(this.fd);
6660
+ fs34.fsyncSync(this.fd);
6447
6661
  }
6448
6662
  const len = this._len;
6449
6663
  if (this._reopening) {
@@ -6557,7 +6771,7 @@ var require_sonic_boom = __commonJS({
6557
6771
  const onDrain = () => {
6558
6772
  if (!this._fsync) {
6559
6773
  try {
6560
- fs28.fsync(this.fd, (err) => {
6774
+ fs34.fsync(this.fd, (err) => {
6561
6775
  this._flushPending = false;
6562
6776
  cb(err);
6563
6777
  });
@@ -6659,7 +6873,7 @@ var require_sonic_boom = __commonJS({
6659
6873
  const fd = this.fd;
6660
6874
  this.once("ready", () => {
6661
6875
  if (fd !== this.fd) {
6662
- fs28.close(fd, (err) => {
6876
+ fs34.close(fd, (err) => {
6663
6877
  if (err) {
6664
6878
  return this.emit("error", err);
6665
6879
  }
@@ -6708,7 +6922,7 @@ var require_sonic_boom = __commonJS({
6708
6922
  buf = this._bufs[0];
6709
6923
  }
6710
6924
  try {
6711
- const n = Buffer.isBuffer(buf) ? fs28.writeSync(this.fd, buf) : fs28.writeSync(this.fd, buf, "utf8");
6925
+ const n = Buffer.isBuffer(buf) ? fs34.writeSync(this.fd, buf) : fs34.writeSync(this.fd, buf, "utf8");
6712
6926
  const releasedBufObj = releaseWritingBuf(buf, this._len, n);
6713
6927
  buf = releasedBufObj.writingBuf;
6714
6928
  this._len = releasedBufObj.len;
@@ -6724,7 +6938,7 @@ var require_sonic_boom = __commonJS({
6724
6938
  }
6725
6939
  }
6726
6940
  try {
6727
- fs28.fsyncSync(this.fd);
6941
+ fs34.fsyncSync(this.fd);
6728
6942
  } catch {
6729
6943
  }
6730
6944
  }
@@ -6745,7 +6959,7 @@ var require_sonic_boom = __commonJS({
6745
6959
  buf = mergeBuf(this._bufs[0], this._lens[0]);
6746
6960
  }
6747
6961
  try {
6748
- const n = fs28.writeSync(this.fd, buf);
6962
+ const n = fs34.writeSync(this.fd, buf);
6749
6963
  buf = buf.subarray(n);
6750
6964
  this._len = Math.max(this._len - n, 0);
6751
6965
  if (buf.length <= 0) {
@@ -6773,13 +6987,13 @@ var require_sonic_boom = __commonJS({
6773
6987
  this._writingBuf = this._writingBuf.length ? this._writingBuf : this._bufs.shift() || "";
6774
6988
  if (this.sync) {
6775
6989
  try {
6776
- const written = Buffer.isBuffer(this._writingBuf) ? fs28.writeSync(this.fd, this._writingBuf) : fs28.writeSync(this.fd, this._writingBuf, "utf8");
6990
+ const written = Buffer.isBuffer(this._writingBuf) ? fs34.writeSync(this.fd, this._writingBuf) : fs34.writeSync(this.fd, this._writingBuf, "utf8");
6777
6991
  release(null, written);
6778
6992
  } catch (err) {
6779
6993
  release(err);
6780
6994
  }
6781
6995
  } else {
6782
- fs28.write(this.fd, this._writingBuf, release);
6996
+ fs34.write(this.fd, this._writingBuf, release);
6783
6997
  }
6784
6998
  }
6785
6999
  function actualWriteBuffer() {
@@ -6788,7 +7002,7 @@ var require_sonic_boom = __commonJS({
6788
7002
  this._writingBuf = this._writingBuf.length ? this._writingBuf : mergeBuf(this._bufs.shift(), this._lens.shift());
6789
7003
  if (this.sync) {
6790
7004
  try {
6791
- const written = fs28.writeSync(this.fd, this._writingBuf);
7005
+ const written = fs34.writeSync(this.fd, this._writingBuf);
6792
7006
  release(null, written);
6793
7007
  } catch (err) {
6794
7008
  release(err);
@@ -6797,7 +7011,7 @@ var require_sonic_boom = __commonJS({
6797
7011
  if (kCopyBuffer) {
6798
7012
  this._writingBuf = Buffer.from(this._writingBuf);
6799
7013
  }
6800
- fs28.write(this.fd, this._writingBuf, release);
7014
+ fs34.write(this.fd, this._writingBuf, release);
6801
7015
  }
6802
7016
  }
6803
7017
  function actualClose(sonic) {
@@ -6813,12 +7027,12 @@ var require_sonic_boom = __commonJS({
6813
7027
  sonic._lens = [];
6814
7028
  assert(typeof sonic.fd === "number", `sonic.fd must be a number, got ${typeof sonic.fd}`);
6815
7029
  try {
6816
- fs28.fsync(sonic.fd, closeWrapped);
7030
+ fs34.fsync(sonic.fd, closeWrapped);
6817
7031
  } catch {
6818
7032
  }
6819
7033
  function closeWrapped() {
6820
7034
  if (sonic.fd !== 1 && sonic.fd !== 2) {
6821
- fs28.close(sonic.fd, done);
7035
+ fs34.close(sonic.fd, done);
6822
7036
  } else {
6823
7037
  done();
6824
7038
  }
@@ -7075,7 +7289,7 @@ var require_thread_stream = __commonJS({
7075
7289
  var { version: version2 } = require_package();
7076
7290
  var { EventEmitter: EventEmitter2 } = require("events");
7077
7291
  var { Worker } = require("worker_threads");
7078
- var { join: join4 } = require("path");
7292
+ var { join: join11 } = require("path");
7079
7293
  var { pathToFileURL } = require("url");
7080
7294
  var { wait } = require_wait();
7081
7295
  var {
@@ -7111,7 +7325,7 @@ var require_thread_stream = __commonJS({
7111
7325
  function createWorker(stream, opts) {
7112
7326
  const { filename, workerData } = opts;
7113
7327
  const bundlerOverrides = "__bundlerPathsOverrides" in globalThis ? globalThis.__bundlerPathsOverrides : {};
7114
- const toExecute = bundlerOverrides["thread-stream-worker"] || join4(__dirname, "lib", "worker.js");
7328
+ const toExecute = bundlerOverrides["thread-stream-worker"] || join11(__dirname, "lib", "worker.js");
7115
7329
  const worker = new Worker(toExecute, {
7116
7330
  ...opts.workerOpts,
7117
7331
  trackUnmanagedFds: false,
@@ -7497,7 +7711,7 @@ var require_transport = __commonJS({
7497
7711
  "use strict";
7498
7712
  var { createRequire } = require("module");
7499
7713
  var getCallers = require_caller();
7500
- var { join: join4, isAbsolute, sep } = require("path");
7714
+ var { join: join11, isAbsolute, sep: sep2 } = require("path");
7501
7715
  var sleep = require_atomic_sleep();
7502
7716
  var onExit = require_on_exit_leak_free();
7503
7717
  var ThreadStream = require_thread_stream();
@@ -7560,7 +7774,7 @@ var require_transport = __commonJS({
7560
7774
  throw new Error("only one of target or targets can be specified");
7561
7775
  }
7562
7776
  if (targets) {
7563
- target = bundlerOverrides["pino-worker"] || join4(__dirname, "worker.js");
7777
+ target = bundlerOverrides["pino-worker"] || join11(__dirname, "worker.js");
7564
7778
  options.targets = targets.filter((dest) => dest.target).map((dest) => {
7565
7779
  return {
7566
7780
  ...dest,
@@ -7578,7 +7792,7 @@ var require_transport = __commonJS({
7578
7792
  });
7579
7793
  });
7580
7794
  } else if (pipeline2) {
7581
- target = bundlerOverrides["pino-worker"] || join4(__dirname, "worker.js");
7795
+ target = bundlerOverrides["pino-worker"] || join11(__dirname, "worker.js");
7582
7796
  options.pipelines = [pipeline2.map((dest) => {
7583
7797
  return {
7584
7798
  ...dest,
@@ -7600,12 +7814,12 @@ var require_transport = __commonJS({
7600
7814
  return origin;
7601
7815
  }
7602
7816
  if (origin === "pino/file") {
7603
- return join4(__dirname, "..", "file.js");
7817
+ return join11(__dirname, "..", "file.js");
7604
7818
  }
7605
7819
  let fixTarget2;
7606
7820
  for (const filePath of callers) {
7607
7821
  try {
7608
- const context = filePath === "node:repl" ? process.cwd() + sep : filePath;
7822
+ const context = filePath === "node:repl" ? process.cwd() + sep2 : filePath;
7609
7823
  fixTarget2 = createRequire(context).resolve(origin);
7610
7824
  break;
7611
7825
  } catch (err) {
@@ -8590,7 +8804,7 @@ var require_safe_stable_stringify = __commonJS({
8590
8804
  return circularValue;
8591
8805
  }
8592
8806
  let res = "";
8593
- let join4 = ",";
8807
+ let join11 = ",";
8594
8808
  const originalIndentation = indentation;
8595
8809
  if (Array.isArray(value)) {
8596
8810
  if (value.length === 0) {
@@ -8604,7 +8818,7 @@ var require_safe_stable_stringify = __commonJS({
8604
8818
  indentation += spacer;
8605
8819
  res += `
8606
8820
  ${indentation}`;
8607
- join4 = `,
8821
+ join11 = `,
8608
8822
  ${indentation}`;
8609
8823
  }
8610
8824
  const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
@@ -8612,13 +8826,13 @@ ${indentation}`;
8612
8826
  for (; i < maximumValuesToStringify - 1; i++) {
8613
8827
  const tmp2 = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation);
8614
8828
  res += tmp2 !== void 0 ? tmp2 : "null";
8615
- res += join4;
8829
+ res += join11;
8616
8830
  }
8617
8831
  const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation);
8618
8832
  res += tmp !== void 0 ? tmp : "null";
8619
8833
  if (value.length - 1 > maximumBreadth) {
8620
8834
  const removedKeys = value.length - maximumBreadth - 1;
8621
- res += `${join4}"... ${getItemCount(removedKeys)} not stringified"`;
8835
+ res += `${join11}"... ${getItemCount(removedKeys)} not stringified"`;
8622
8836
  }
8623
8837
  if (spacer !== "") {
8624
8838
  res += `
@@ -8639,7 +8853,7 @@ ${originalIndentation}`;
8639
8853
  let separator = "";
8640
8854
  if (spacer !== "") {
8641
8855
  indentation += spacer;
8642
- join4 = `,
8856
+ join11 = `,
8643
8857
  ${indentation}`;
8644
8858
  whitespace = " ";
8645
8859
  }
@@ -8653,13 +8867,13 @@ ${indentation}`;
8653
8867
  const tmp = stringifyFnReplacer(key2, value, stack, replacer, spacer, indentation);
8654
8868
  if (tmp !== void 0) {
8655
8869
  res += `${separator}${strEscape(key2)}:${whitespace}${tmp}`;
8656
- separator = join4;
8870
+ separator = join11;
8657
8871
  }
8658
8872
  }
8659
8873
  if (keyLength > maximumBreadth) {
8660
8874
  const removedKeys = keyLength - maximumBreadth;
8661
8875
  res += `${separator}"...":${whitespace}"${getItemCount(removedKeys)} not stringified"`;
8662
- separator = join4;
8876
+ separator = join11;
8663
8877
  }
8664
8878
  if (spacer !== "" && separator.length > 1) {
8665
8879
  res = `
@@ -8700,7 +8914,7 @@ ${originalIndentation}`;
8700
8914
  }
8701
8915
  const originalIndentation = indentation;
8702
8916
  let res = "";
8703
- let join4 = ",";
8917
+ let join11 = ",";
8704
8918
  if (Array.isArray(value)) {
8705
8919
  if (value.length === 0) {
8706
8920
  return "[]";
@@ -8713,7 +8927,7 @@ ${originalIndentation}`;
8713
8927
  indentation += spacer;
8714
8928
  res += `
8715
8929
  ${indentation}`;
8716
- join4 = `,
8930
+ join11 = `,
8717
8931
  ${indentation}`;
8718
8932
  }
8719
8933
  const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
@@ -8721,13 +8935,13 @@ ${indentation}`;
8721
8935
  for (; i < maximumValuesToStringify - 1; i++) {
8722
8936
  const tmp2 = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation);
8723
8937
  res += tmp2 !== void 0 ? tmp2 : "null";
8724
- res += join4;
8938
+ res += join11;
8725
8939
  }
8726
8940
  const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation);
8727
8941
  res += tmp !== void 0 ? tmp : "null";
8728
8942
  if (value.length - 1 > maximumBreadth) {
8729
8943
  const removedKeys = value.length - maximumBreadth - 1;
8730
- res += `${join4}"... ${getItemCount(removedKeys)} not stringified"`;
8944
+ res += `${join11}"... ${getItemCount(removedKeys)} not stringified"`;
8731
8945
  }
8732
8946
  if (spacer !== "") {
8733
8947
  res += `
@@ -8740,7 +8954,7 @@ ${originalIndentation}`;
8740
8954
  let whitespace = "";
8741
8955
  if (spacer !== "") {
8742
8956
  indentation += spacer;
8743
- join4 = `,
8957
+ join11 = `,
8744
8958
  ${indentation}`;
8745
8959
  whitespace = " ";
8746
8960
  }
@@ -8749,7 +8963,7 @@ ${indentation}`;
8749
8963
  const tmp = stringifyArrayReplacer(key2, value[key2], stack, replacer, spacer, indentation);
8750
8964
  if (tmp !== void 0) {
8751
8965
  res += `${separator}${strEscape(key2)}:${whitespace}${tmp}`;
8752
- separator = join4;
8966
+ separator = join11;
8753
8967
  }
8754
8968
  }
8755
8969
  if (spacer !== "" && separator.length > 1) {
@@ -8807,20 +9021,20 @@ ${originalIndentation}`;
8807
9021
  indentation += spacer;
8808
9022
  let res2 = `
8809
9023
  ${indentation}`;
8810
- const join5 = `,
9024
+ const join12 = `,
8811
9025
  ${indentation}`;
8812
9026
  const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
8813
9027
  let i = 0;
8814
9028
  for (; i < maximumValuesToStringify - 1; i++) {
8815
9029
  const tmp2 = stringifyIndent(String(i), value[i], stack, spacer, indentation);
8816
9030
  res2 += tmp2 !== void 0 ? tmp2 : "null";
8817
- res2 += join5;
9031
+ res2 += join12;
8818
9032
  }
8819
9033
  const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation);
8820
9034
  res2 += tmp !== void 0 ? tmp : "null";
8821
9035
  if (value.length - 1 > maximumBreadth) {
8822
9036
  const removedKeys = value.length - maximumBreadth - 1;
8823
- res2 += `${join5}"... ${getItemCount(removedKeys)} not stringified"`;
9037
+ res2 += `${join12}"... ${getItemCount(removedKeys)} not stringified"`;
8824
9038
  }
8825
9039
  res2 += `
8826
9040
  ${originalIndentation}`;
@@ -8836,16 +9050,16 @@ ${originalIndentation}`;
8836
9050
  return '"[Object]"';
8837
9051
  }
8838
9052
  indentation += spacer;
8839
- const join4 = `,
9053
+ const join11 = `,
8840
9054
  ${indentation}`;
8841
9055
  let res = "";
8842
9056
  let separator = "";
8843
9057
  let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth);
8844
9058
  if (isTypedArrayWithEntries(value)) {
8845
- res += stringifyTypedArray(value, join4, maximumBreadth);
9059
+ res += stringifyTypedArray(value, join11, maximumBreadth);
8846
9060
  keys = keys.slice(value.length);
8847
9061
  maximumPropertiesToStringify -= value.length;
8848
- separator = join4;
9062
+ separator = join11;
8849
9063
  }
8850
9064
  if (deterministic) {
8851
9065
  keys = sort(keys, comparator);
@@ -8856,13 +9070,13 @@ ${indentation}`;
8856
9070
  const tmp = stringifyIndent(key2, value[key2], stack, spacer, indentation);
8857
9071
  if (tmp !== void 0) {
8858
9072
  res += `${separator}${strEscape(key2)}: ${tmp}`;
8859
- separator = join4;
9073
+ separator = join11;
8860
9074
  }
8861
9075
  }
8862
9076
  if (keyLength > maximumBreadth) {
8863
9077
  const removedKeys = keyLength - maximumBreadth;
8864
9078
  res += `${separator}"...": "${getItemCount(removedKeys)} not stringified"`;
8865
- separator = join4;
9079
+ separator = join11;
8866
9080
  }
8867
9081
  if (separator !== "") {
8868
9082
  res = `
@@ -9953,11 +10167,11 @@ var init_lib = __esm({
9953
10167
  }
9954
10168
  }
9955
10169
  },
9956
- addToPath: function addToPath(path32, added, removed, oldPosInc, options) {
9957
- var last = path32.lastComponent;
10170
+ addToPath: function addToPath(path39, added, removed, oldPosInc, options) {
10171
+ var last = path39.lastComponent;
9958
10172
  if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) {
9959
10173
  return {
9960
- oldPos: path32.oldPos + oldPosInc,
10174
+ oldPos: path39.oldPos + oldPosInc,
9961
10175
  lastComponent: {
9962
10176
  count: last.count + 1,
9963
10177
  added,
@@ -9967,7 +10181,7 @@ var init_lib = __esm({
9967
10181
  };
9968
10182
  } else {
9969
10183
  return {
9970
- oldPos: path32.oldPos + oldPosInc,
10184
+ oldPos: path39.oldPos + oldPosInc,
9971
10185
  lastComponent: {
9972
10186
  count: 1,
9973
10187
  added,
@@ -10025,7 +10239,7 @@ var init_lib = __esm({
10025
10239
  tokenize: function tokenize(value) {
10026
10240
  return Array.from(value);
10027
10241
  },
10028
- join: function join3(chars) {
10242
+ join: function join4(chars) {
10029
10243
  return chars.join("");
10030
10244
  },
10031
10245
  postProcess: function postProcess(changeObjects) {
@@ -10398,10 +10612,10 @@ function attachmentToHistoryMessage(o, ts) {
10398
10612
  const memories = raw.map((m2) => {
10399
10613
  if (!m2 || typeof m2 !== "object") return null;
10400
10614
  const rec = m2;
10401
- const path32 = typeof rec.path === "string" ? rec.path : null;
10615
+ const path39 = typeof rec.path === "string" ? rec.path : null;
10402
10616
  const content = typeof rec.content === "string" ? rec.content : null;
10403
- if (!path32 || content == null) return null;
10404
- const entry = { path: path32, content };
10617
+ if (!path39 || content == null) return null;
10618
+ const entry = { path: path39, content };
10405
10619
  if (typeof rec.mtimeMs === "number") entry.mtimeMs = rec.mtimeMs;
10406
10620
  return entry;
10407
10621
  }).filter((m2) => m2 !== null);
@@ -10861,6 +11075,27 @@ var init_claude_history = __esm({
10861
11075
  }
10862
11076
  });
10863
11077
 
11078
+ // src/tools/sandbox.ts
11079
+ function shouldSandbox(cwd, personaRoot) {
11080
+ if (!personaRoot) return false;
11081
+ const sep2 = personaRoot.endsWith(path12.sep) ? "" : path12.sep;
11082
+ return cwd.startsWith(personaRoot + sep2) && cwd !== personaRoot;
11083
+ }
11084
+ function inferSandboxSettingsPath(cwd, personaRoot) {
11085
+ if (!shouldSandbox(cwd, personaRoot)) return null;
11086
+ const rel = path12.relative(personaRoot, cwd);
11087
+ const personaId = rel.split(path12.sep)[0];
11088
+ if (!personaId) return null;
11089
+ return path12.join(personaRoot, personaId, ".clawd", "sandbox-settings.json");
11090
+ }
11091
+ var path12;
11092
+ var init_sandbox = __esm({
11093
+ "src/tools/sandbox.ts"() {
11094
+ "use strict";
11095
+ path12 = __toESM(require("path"), 1);
11096
+ }
11097
+ });
11098
+
10864
11099
  // src/tools/claude.ts
10865
11100
  function macOSDesktopCandidates(home) {
10866
11101
  return [
@@ -10910,7 +11145,7 @@ function buildSpawnArgs(ctx) {
10910
11145
  ];
10911
11146
  if (ctx.model) args.push("--model", ctx.model);
10912
11147
  switch (ctx.personaMode) {
10913
- case "listener":
11148
+ case "guest":
10914
11149
  args.push("--setting-sources", "project,local");
10915
11150
  break;
10916
11151
  case "owner":
@@ -10925,7 +11160,8 @@ function buildSpawnArgs(ctx) {
10925
11160
  throw new Error(`unexpected personaMode: ${String(_exhaustive)}`);
10926
11161
  }
10927
11162
  }
10928
- if (ctx.extraSettings) args.push("--settings", ctx.extraSettings);
11163
+ const sandboxSettings = ctx.extraSettings ?? inferSandboxSettingsPath(ctx.cwd, ctx.personaRoot);
11164
+ if (sandboxSettings) args.push("--settings", sandboxSettings);
10929
11165
  if (ctx.extraSystemPrompt) args.push("--append-system-prompt", ctx.extraSystemPrompt);
10930
11166
  if (ctx.effort) args.push("--effort", ctx.effort);
10931
11167
  if (ctx.toolSessionId) args.push("--resume", ctx.toolSessionId);
@@ -11205,10 +11441,10 @@ function parseAttachment(obj) {
11205
11441
  const memories = raw.map((m2) => {
11206
11442
  if (!m2 || typeof m2 !== "object") return null;
11207
11443
  const rec = m2;
11208
- const path32 = typeof rec.path === "string" ? rec.path : null;
11444
+ const path39 = typeof rec.path === "string" ? rec.path : null;
11209
11445
  const content = typeof rec.content === "string" ? rec.content : null;
11210
- if (!path32 || content == null) return null;
11211
- const out = { path: path32, content };
11446
+ if (!path39 || content == null) return null;
11447
+ const out = { path: path39, content };
11212
11448
  if (typeof rec.mtimeMs === "number") out.mtimeMs = rec.mtimeMs;
11213
11449
  return out;
11214
11450
  }).filter((m2) => m2 !== null);
@@ -11324,6 +11560,7 @@ var init_claude = __esm({
11324
11560
  init_protocol();
11325
11561
  init_claude_history();
11326
11562
  init_tool_result_extra();
11563
+ init_sandbox();
11327
11564
  ATTACHMENT_SILENT_SUBTYPES2 = /* @__PURE__ */ new Set([
11328
11565
  "hook_additional_context",
11329
11566
  "hook_success",
@@ -18706,7 +18943,7 @@ var require_websocket = __commonJS({
18706
18943
  var http2 = require("http");
18707
18944
  var net = require("net");
18708
18945
  var tls = require("tls");
18709
- var { randomBytes, createHash } = require("crypto");
18946
+ var { randomBytes: randomBytes2, createHash: createHash3 } = require("crypto");
18710
18947
  var { Duplex, Readable: Readable3 } = require("stream");
18711
18948
  var { URL: URL2 } = require("url");
18712
18949
  var PerMessageDeflate2 = require_permessage_deflate();
@@ -19236,7 +19473,7 @@ var require_websocket = __commonJS({
19236
19473
  }
19237
19474
  }
19238
19475
  const defaultPort = isSecure ? 443 : 80;
19239
- const key = randomBytes(16).toString("base64");
19476
+ const key = randomBytes2(16).toString("base64");
19240
19477
  const request = isSecure ? https.request : http2.request;
19241
19478
  const protocolSet = /* @__PURE__ */ new Set();
19242
19479
  let perMessageDeflate;
@@ -19366,7 +19603,7 @@ var require_websocket = __commonJS({
19366
19603
  abortHandshake(websocket, socket, "Invalid Upgrade header");
19367
19604
  return;
19368
19605
  }
19369
- const digest = createHash("sha1").update(key + GUID).digest("base64");
19606
+ const digest = createHash3("sha1").update(key + GUID).digest("base64");
19370
19607
  if (res.headers["sec-websocket-accept"] !== digest) {
19371
19608
  abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
19372
19609
  return;
@@ -19733,7 +19970,7 @@ var require_websocket_server = __commonJS({
19733
19970
  var EventEmitter2 = require("events");
19734
19971
  var http2 = require("http");
19735
19972
  var { Duplex } = require("stream");
19736
- var { createHash } = require("crypto");
19973
+ var { createHash: createHash3 } = require("crypto");
19737
19974
  var extension2 = require_extension();
19738
19975
  var PerMessageDeflate2 = require_permessage_deflate();
19739
19976
  var subprotocol2 = require_subprotocol();
@@ -20034,7 +20271,7 @@ var require_websocket_server = __commonJS({
20034
20271
  );
20035
20272
  }
20036
20273
  if (this._state > RUNNING) return abortHandshake(socket, 503);
20037
- const digest = createHash("sha1").update(key + GUID).digest("base64");
20274
+ const digest = createHash3("sha1").update(key + GUID).digest("base64");
20038
20275
  const headers = [
20039
20276
  "HTTP/1.1 101 Switching Protocols",
20040
20277
  "Upgrade: websocket",
@@ -20653,6 +20890,9 @@ function createLogger(opts = {}) {
20653
20890
  return wrap(base);
20654
20891
  }
20655
20892
 
20893
+ // src/session/store-factory.ts
20894
+ var path5 = __toESM(require("path"), 1);
20895
+
20656
20896
  // src/session/store.ts
20657
20897
  var import_node_fs3 = __toESM(require("fs"), 1);
20658
20898
  var import_node_path4 = __toESM(require("path"), 1);
@@ -20661,10 +20901,14 @@ init_protocol();
20661
20901
  // src/session/scope.ts
20662
20902
  var import_node_path3 = __toESM(require("path"), 1);
20663
20903
  function scopeKey(scope) {
20664
- return scope.kind === "default" ? "default" : `persona:${scope.personaId}:${scope.mode}`;
20904
+ if (scope.kind === "default") return "default";
20905
+ if (scope.mode === "guest") return `persona:${scope.personaId}:guest:${scope.capId}`;
20906
+ return `persona:${scope.personaId}:${scope.mode}`;
20665
20907
  }
20666
20908
  function scopeSubPath(scope) {
20667
- return scope.kind === "default" ? ["default"] : [scope.personaId, scope.mode];
20909
+ if (scope.kind === "default") return ["default"];
20910
+ if (scope.mode === "guest") return [scope.personaId, "guests", scope.capId];
20911
+ return [scope.personaId, scope.mode];
20668
20912
  }
20669
20913
  function metaFromScope(scope, personaRoot) {
20670
20914
  if (scope.kind === "default") return void 0;
@@ -20673,10 +20917,18 @@ function metaFromScope(scope, personaRoot) {
20673
20917
  }
20674
20918
  return {
20675
20919
  idleKillEnabled: true,
20676
- personaMode: "listener",
20920
+ personaMode: "guest",
20677
20921
  extraSettings: import_node_path3.default.join(personaRoot, scope.personaId, ".clawd", "sandbox-settings.json")
20678
20922
  };
20679
20923
  }
20924
+ function scopeForFile(file, ownerPrincipalId) {
20925
+ if (!file.ownerPersonaId) return { kind: "default" };
20926
+ const creator = file.creatorPrincipalId;
20927
+ if (ownerPrincipalId && creator && creator !== ownerPrincipalId) {
20928
+ return { kind: "persona", personaId: file.ownerPersonaId, mode: "guest", capId: creator };
20929
+ }
20930
+ return { kind: "persona", personaId: file.ownerPersonaId, mode: "owner" };
20931
+ }
20680
20932
 
20681
20933
  // src/session/store.ts
20682
20934
  function safeFileName(sessionId) {
@@ -20690,12 +20942,16 @@ function safeFileName(sessionId) {
20690
20942
  var SessionStore = class {
20691
20943
  root;
20692
20944
  constructor(opts) {
20693
- const scope = opts.scope ?? { kind: "default" };
20694
- this.root = import_node_path4.default.join(
20695
- opts.dataDir,
20696
- "sessions",
20697
- ...scopeSubPath(scope).map(safeFileName)
20698
- );
20945
+ if ("root" in opts) {
20946
+ this.root = opts.root;
20947
+ } else {
20948
+ const scope = opts.scope ?? { kind: "default" };
20949
+ this.root = import_node_path4.default.join(
20950
+ opts.dataDir,
20951
+ "sessions",
20952
+ ...scopeSubPath(scope).map(safeFileName)
20953
+ );
20954
+ }
20699
20955
  }
20700
20956
  filePath(sessionId) {
20701
20957
  return import_node_path4.default.join(this.root, `${safeFileName(sessionId)}.json`);
@@ -20760,6 +21016,66 @@ var SessionStore = class {
20760
21016
  }
20761
21017
  };
20762
21018
 
21019
+ // src/session/store-factory.ts
21020
+ var SessionStoreFactory = class {
21021
+ dataDir;
21022
+ bareStore = null;
21023
+ vmOwnerStores = /* @__PURE__ */ new Map();
21024
+ // vmGuest 索引 key = `${pid}::${capId}`(按 cap.id 隔离,一张 cap 一个目录)
21025
+ vmGuestStores = /* @__PURE__ */ new Map();
21026
+ constructor(opts) {
21027
+ this.dataDir = opts.dataDir;
21028
+ }
21029
+ // ---- root path 派生(暴露给 migration / revoke cascade 等外部消费方) ----
21030
+ bareRoot() {
21031
+ return path5.join(this.dataDir, "sessions");
21032
+ }
21033
+ vmOwnerRoot(personaId) {
21034
+ return path5.join(
21035
+ this.dataDir,
21036
+ "personas",
21037
+ safeFileName(personaId),
21038
+ ".clawd",
21039
+ "sessions",
21040
+ "owner"
21041
+ );
21042
+ }
21043
+ vmGuestRoot(personaId, capId) {
21044
+ return path5.join(
21045
+ this.dataDir,
21046
+ "personas",
21047
+ safeFileName(personaId),
21048
+ ".clawd",
21049
+ "sessions",
21050
+ "guests",
21051
+ safeFileName(capId)
21052
+ );
21053
+ }
21054
+ // ---- SessionStore 工厂(缓存) ----
21055
+ forBare() {
21056
+ if (!this.bareStore) {
21057
+ this.bareStore = new SessionStore({ root: this.bareRoot() });
21058
+ }
21059
+ return this.bareStore;
21060
+ }
21061
+ forVmOwner(personaId) {
21062
+ const key = personaId;
21063
+ const cached = this.vmOwnerStores.get(key);
21064
+ if (cached) return cached;
21065
+ const st = new SessionStore({ root: this.vmOwnerRoot(personaId) });
21066
+ this.vmOwnerStores.set(key, st);
21067
+ return st;
21068
+ }
21069
+ forVmGuest(personaId, capId) {
21070
+ const key = `${personaId}::${capId}`;
21071
+ const cached = this.vmGuestStores.get(key);
21072
+ if (cached) return cached;
21073
+ const st = new SessionStore({ root: this.vmGuestRoot(personaId, capId) });
21074
+ this.vmGuestStores.set(key, st);
21075
+ return st;
21076
+ }
21077
+ };
21078
+
20763
21079
  // src/session/manager.ts
20764
21080
  var import_node_fs5 = __toESM(require("fs"), 1);
20765
21081
  var import_node_path6 = __toESM(require("path"), 1);
@@ -20881,7 +21197,7 @@ function cloneState(s) {
20881
21197
  pendingSend: s.pendingSend
20882
21198
  };
20883
21199
  }
20884
- var IDLE_KILL_DELAY_MS = 5e3;
21200
+ var IDLE_KILL_DELAY_MS = 6e4;
20885
21201
  var SEEN_UUID_CAP = 2e3;
20886
21202
  function applyEventBatch(state, events, deps) {
20887
21203
  if (events.length === 0) return { state, effects: [] };
@@ -20921,7 +21237,10 @@ function buildSpawnContext(state, deps) {
20921
21237
  toolSessionId: file.toolSessionId,
20922
21238
  model: file.model,
20923
21239
  permissionMode: file.permissionMode,
20924
- effort: file.effort
21240
+ effort: file.effort,
21241
+ // Phase 2 capability platform (plan §3): personaRoot 透传给 buildSpawnArgs;
21242
+ // 内部 shouldSandbox(cwd, personaRoot) 决定是否注入 --settings sandbox-settings.
21243
+ personaRoot: deps.personaRoot
20925
21244
  };
20926
21245
  const meta = state.subSessionMeta;
20927
21246
  if (meta?.personaMode) {
@@ -21706,6 +22025,7 @@ var SessionRunner = class {
21706
22025
  resolveContextWindow: this.hooks.resolveContextWindow,
21707
22026
  genUuid: this.hooks.genUuid ?? v4_default,
21708
22027
  ownerDisplayName: this.hooks.ownerDisplayName,
22028
+ personaRoot: this.hooks.personaRoot,
21709
22029
  mode: this.hooks.mode ?? "sdk"
21710
22030
  };
21711
22031
  const { state, effects } = reduceSession(this.state, inputMsg, deps);
@@ -22137,9 +22457,18 @@ var SessionManager = class {
22137
22457
  attachObserver(observer) {
22138
22458
  this.attachedObserver = observer;
22139
22459
  }
22140
- // 按 scope 拿对应的 SessionStore。default scope 复用 deps.store(保证与
22141
- // bootstrap 注入的 store 一致);persona scope 第一次访问时按需创建并缓存。
22460
+ // 按 scope 拿对应的 SessionStore.
22461
+ // Phase 2 (capability platform plan §1) 路由切到 SessionStoreFactory:
22462
+ // default → factory.forBare() <dataDir>/sessions/
22463
+ // persona/<pid>/owner → factory.forVmOwner(pid) <dataDir>/personas/<pid>/.clawd/sessions/owner/
22464
+ // persona/<pid>/guest → factory.forVmGuest(pid, capId) <dataDir>/personas/<pid>/.clawd/sessions/guests/<capId>/
22465
+ // 缺省 (无 factory 注入): 回退 dataDir+scope 老路径 (向后兼容旧 spec).
22142
22466
  storeFor(scope) {
22467
+ if (this.deps.storeFactory) {
22468
+ if (scope.kind === "default") return this.deps.storeFactory.forBare();
22469
+ if (scope.mode === "owner") return this.deps.storeFactory.forVmOwner(scope.personaId);
22470
+ return this.deps.storeFactory.forVmGuest(scope.personaId, scope.capId);
22471
+ }
22143
22472
  if (scope.kind === "default") return this.deps.store;
22144
22473
  const key = scopeKey(scope);
22145
22474
  const cached = this.storesByScope.get(key);
@@ -22159,19 +22488,18 @@ var SessionManager = class {
22159
22488
  this.capabilitiesCache.set(tool, caps);
22160
22489
  return caps;
22161
22490
  }
22162
- // 由 SessionFile.ownerPersonaId schema 字段推回写入 scope
22163
- // ownerPersonaId create({ownerPersonaId}) schema 落盘时记入,永远只反映
22164
- // session 出身(不可变);此函数只用于 SessionStore 的写入路由,不参与运行时行为
22165
- // 决策(personaMode='owner' 等由路径 → scope → metaFromScope 派生)。
22166
- // 仅覆盖 owner + default 两类——listener sub-session 不会从 default RPC 路径进来。
22491
+ // 由 SessionFile 反推 scope 的 thin wrapper —— 决策矩阵收敛到 scope.scopeForFile
22492
+ // free function(scope.spec.ts transition 表驱动测试),manager 只负责注入 deps.
22167
22493
  scopeForFile(file) {
22168
- return file.ownerPersonaId ? { kind: "persona", personaId: file.ownerPersonaId, mode: "owner" } : { kind: "default" };
22494
+ return scopeForFile(file, this.deps.ownerPrincipalId);
22169
22495
  }
22170
- // <dataDir>/sessions/ 列出所有 persona 命名空间(不含 'default')。
22171
- // 用于 findOwnedSession / listAllOwned scope 查询。
22496
+ // 扫所有 persona 命名空间. 用于 findOwnedSession / listAllOwned scope 查询.
22497
+ // Phase 2 capability platform: 新布局下 persona 资产在 <dataDir>/personas/<pid>/,
22498
+ // 直接 readdir 这个目录拿所有 personaId. 老布局 (无 storeFactory) fallback 扫
22499
+ // <dataDir>/sessions/ 列子目录 (排除 'default').
22172
22500
  listPersonaIdsOnDisk() {
22173
22501
  if (!this.deps.dataDir) return [];
22174
- const root = import_node_path6.default.join(this.deps.dataDir, "sessions");
22502
+ const root = this.deps.storeFactory ? import_node_path6.default.join(this.deps.dataDir, "personas") : import_node_path6.default.join(this.deps.dataDir, "sessions");
22175
22503
  let entries;
22176
22504
  try {
22177
22505
  entries = import_node_fs5.default.readdirSync(root, { withFileTypes: true });
@@ -22182,13 +22510,37 @@ var SessionManager = class {
22182
22510
  }
22183
22511
  return entries.filter((e) => e.isDirectory() && e.name !== "default").map((e) => e.name);
22184
22512
  }
22185
- // owner / default 两个分类下按 sessionId 找文件——前端只传 sessionId 不带 scope,
22186
- // SessionManager 在这里定位。三层快慢路径:
22513
+ // 扫某 persona guests/ 子目录, 拿所有 capId.
22514
+ // 路径: <dataDir>/personas/<pid>/.clawd/sessions/guests/<capId>/
22515
+ // 只在 storeFactory 注入 (新布局) 下生效, 老布局无 guest 目录.
22516
+ listGuestCapIdsForPersona(personaId) {
22517
+ if (!this.deps.dataDir || !this.deps.storeFactory) return [];
22518
+ const root = import_node_path6.default.join(
22519
+ this.deps.dataDir,
22520
+ "personas",
22521
+ personaId,
22522
+ ".clawd",
22523
+ "sessions",
22524
+ "guests"
22525
+ );
22526
+ let entries;
22527
+ try {
22528
+ entries = import_node_fs5.default.readdirSync(root, { withFileTypes: true });
22529
+ } catch (err) {
22530
+ const code = err?.code;
22531
+ if (code === "ENOENT") return [];
22532
+ throw err;
22533
+ }
22534
+ return entries.filter((e) => e.isDirectory()).map((e) => e.name);
22535
+ }
22536
+ // owner / default + persona guests 三大分类下按 sessionId 找文件——前端只传 sessionId 不带 scope,
22537
+ // SessionManager 在这里定位。四层快慢路径:
22187
22538
  // 1) active runner(用户当前在用的 session):runner.state.file 是 SessionFile 内存权威副本,
22188
22539
  // 直接返回,零磁盘 I/O。覆盖 attachment / file-sharing 等"用户操作 → 紧接着 RPC"的
22189
22540
  // 绝大多数场景
22190
22541
  // 2) default scope 磁盘:inactive default session 命中
22191
22542
  // 3) 所有 persona owner 目录扫盘:inactive persona owner session 命中
22543
+ // 4) 每个 persona 的 guests/<capId>/ 扫盘:guest 通过我颁发 token 接进来创建的 session
22192
22544
  // listener sub-session 不走这条——transport 始终明确传 (personaId, sessionId)。
22193
22545
  //
22194
22546
  // 公开方法:attachment / file-sharing handler 通过 deps 闭包消费,不应直接持 SessionStore
@@ -22200,8 +22552,13 @@ var SessionManager = class {
22200
22552
  if (dflt) return dflt;
22201
22553
  for (const personaId of this.listPersonaIdsOnDisk()) {
22202
22554
  const ownerStore = this.storeFor({ kind: "persona", personaId, mode: "owner" });
22203
- const file = ownerStore.read(sessionId);
22204
- if (file) return file;
22555
+ const found = ownerStore.read(sessionId);
22556
+ if (found) return found;
22557
+ for (const capId of this.listGuestCapIdsForPersona(personaId)) {
22558
+ const guestStore = this.storeFor({ kind: "persona", personaId, mode: "guest", capId });
22559
+ const g2 = guestStore.read(sessionId);
22560
+ if (g2) return g2;
22561
+ }
22205
22562
  }
22206
22563
  return null;
22207
22564
  }
@@ -22212,30 +22569,40 @@ var SessionManager = class {
22212
22569
  const file = this.findOwnedSession(sessionId);
22213
22570
  return file ? this.scopeForFile(file) : null;
22214
22571
  }
22215
- // 合并 default + 所有 persona owner SessionFile —— 桌面 App 主 session 列表入口。
22216
- // 同样不含 listener sub-session(listener persona:listSubSessions RPC 单独入口)。
22572
+ // 合并 default + 每个 persona owner/ + guests/<capId>/ —— 桌面 App 主 session 列表入口.
22573
+ // guest sessions 由 handlers/session.ts list 处按 ctx.principal.kind === 'guest' 做权限过滤
22574
+ // (creatorPrincipalId === capId), 这里仅负责数据源汇总.
22575
+ // listener sub-session 不走这条(listener 走 persona:listSubSessions RPC 单独入口)。
22217
22576
  listAllOwned() {
22218
22577
  const out = [];
22219
22578
  out.push(...this.deps.store.list());
22220
22579
  for (const personaId of this.listPersonaIdsOnDisk()) {
22221
22580
  const ownerStore = this.storeFor({ kind: "persona", personaId, mode: "owner" });
22222
22581
  out.push(...ownerStore.list());
22582
+ for (const capId of this.listGuestCapIdsForPersona(personaId)) {
22583
+ const guestStore = this.storeFor({ kind: "persona", personaId, mode: "guest", capId });
22584
+ out.push(...guestStore.list());
22585
+ }
22223
22586
  }
22224
22587
  return out.sort(
22225
22588
  (a, b2) => a.updatedAt > b2.updatedAt ? -1 : a.updatedAt < b2.updatedAt ? 1 : 0
22226
22589
  );
22227
22590
  }
22228
- // 写回 SessionFile,自动根据 ownerPersonaId 派生写入 scope
22229
- // 调用方原先都是 `deps.store.write(updated)`,全部走这里收口路由。
22591
+ // 写回 SessionFile,自动根据 ownerPersonaId / creatorPrincipalId 派生写入 scope.
22592
+ // 调用方原先都是 `deps.store.write(updated)`,全部走这里收口路由.
22230
22593
  writeOwned(file) {
22231
22594
  return this.storeFor(this.scopeForFile(file)).write(file);
22232
22595
  }
22233
- // 删除 owned SessionFile(default persona/owner),与 findOwnedSession 对称扫盘。
22596
+ // 删除 owned SessionFile(default / persona owner / persona guest),与 findOwnedSession 对称扫盘.
22234
22597
  deleteOwned(sessionId) {
22235
22598
  if (this.deps.store.delete(sessionId)) return true;
22236
22599
  for (const personaId of this.listPersonaIdsOnDisk()) {
22237
22600
  const ownerStore = this.storeFor({ kind: "persona", personaId, mode: "owner" });
22238
22601
  if (ownerStore.delete(sessionId)) return true;
22602
+ for (const capId of this.listGuestCapIdsForPersona(personaId)) {
22603
+ const guestStore = this.storeFor({ kind: "persona", personaId, mode: "guest", capId });
22604
+ if (guestStore.delete(sessionId)) return true;
22605
+ }
22239
22606
  }
22240
22607
  return false;
22241
22608
  }
@@ -22243,8 +22610,8 @@ var SessionManager = class {
22243
22610
  // 经过 compressFrameForWire 后决定是 push collector 还是走 deps.broadcastFrame。
22244
22611
  // scope 决定两件事:SessionStore 路由 + SubSessionMeta 派生(metaFromScope)。
22245
22612
  // - default → 复用 deps.store,无 subSessionMeta(普通 session)
22246
- // - persona/owner → <dataDir>/sessions/<personaId>/owner/,meta={idleKillEnabled:false, personaMode:'owner'}
22247
- // - persona/listener<dataDir>/sessions/<personaId>/listener/,meta={idleKillEnabled:true, personaMode:'listener', extraSettings:<sandbox>}
22613
+ // - persona/owner → personas/<pid>/.clawd/sessions/owner/,meta={idleKillEnabled:false, personaMode:'owner'}
22614
+ // - persona/guestpersonas/<pid>/.clawd/sessions/guests/<capId>/,meta={idleKillEnabled:true, personaMode:'guest', extraSettings:<sandbox>}
22248
22615
  newRunner(file, scope) {
22249
22616
  const adapter = this.deps.getAdapter(file.tool ?? "claude");
22250
22617
  const store = this.storeFor(scope);
@@ -22263,6 +22630,9 @@ var SessionManager = class {
22263
22630
  dataDir: this.deps.dataDir,
22264
22631
  personaStore: this.deps.personaStore,
22265
22632
  ownerDisplayName: this.deps.ownerDisplayName,
22633
+ // Phase 2 capability platform (plan §3): 透传 personaRoot, buildSpawnArgs 据
22634
+ // cwd 是否在 personaRoot 下自动决定是否注入 --settings sandbox-settings.json.
22635
+ personaRoot: this.deps.personaRoot,
22266
22636
  // 透传给 ReducerDeps.mode:reducer send !procAlive spawn 路径据此判断要不要把
22267
22637
  // readyForSend 翻 false(TUI 翻 false 让 user_text 走 pendingSend 暂存;SDK 不动保持直写)
22268
22638
  mode: this.deps.mode,
@@ -22360,7 +22730,7 @@ var SessionManager = class {
22360
22730
  }
22361
22731
  }
22362
22732
  // ---- 命令方法:均返回 { response, broadcast[] },由 dispatcher 聚合 ----
22363
- create(args) {
22733
+ create(args, creatorPrincipalId) {
22364
22734
  let cwd;
22365
22735
  if (args.ownerPersonaId) {
22366
22736
  cwd = derivePersonaSpawnCwd(
@@ -22401,10 +22771,11 @@ var SessionManager = class {
22401
22771
  iconKey: args.iconKey,
22402
22772
  forkedFromSessionId: args.forkedFromSessionId,
22403
22773
  ownerPersonaId: args.ownerPersonaId,
22774
+ creatorPrincipalId,
22404
22775
  createdAt: iso,
22405
22776
  updatedAt: iso
22406
22777
  };
22407
- const scope = args.ownerPersonaId ? { kind: "persona", personaId: args.ownerPersonaId, mode: "owner" } : { kind: "default" };
22778
+ const scope = this.scopeForFile(file);
22408
22779
  const written = this.storeFor(scope).write(file);
22409
22780
  return { response: written, broadcast: [] };
22410
22781
  }
@@ -22845,28 +23216,18 @@ var SessionManager = class {
22845
23216
  this.runners.set(file.sessionId, r);
22846
23217
  return r;
22847
23218
  }
22848
- // ---------------- persona / listener sub-session 专用 API ----------------
23219
+ // ---------------- scope-explicit sub-session API ----------------
22849
23220
  //
22850
- // PersonaManager SessionManager 之上的薄编排层,listener sub-session 的持久化
22851
- // (SessionFile)仍由 SessionManager 持有。区别于普通 / owner session:
22852
- // - SessionFile 落到 <dataDir>/sessions/<personaId>/listener/(拓扑分类)
22853
- // - reducer state.subSessionMeta.idleKillEnabled = true(turn_end 自动 idle-kill)
22854
- // - sessionId 由 PersonaManager 派生(personaId-<tokenHash>),不走自动 newSessionId
23221
+ // 区别于 owner / default sessionId 入口,这些 API 接 SessionScope 参数 ——
23222
+ // 调用方(PersonaManager / observer 等)明确传 scope,由 storeFor 路由到正确目录:
23223
+ // - {kind:'persona', personaId, mode:'guest', capId} → guests/<capId>/,meta={idleKill:true, sandbox}
23224
+ // - {kind:'persona', personaId, mode:'owner'} → owner/,meta={idleKill:false}
22855
23225
  //
22856
- // 这些 API SessionScope 参数(不再是单参 agentId)—— transport 始终明确传
22857
- // {kind:'persona', personaId, mode:'listener'},与 owner / default 分类隔离。
23226
+ // SubSessionMeta scope metaFromScope 派生,不进 SessionFile schema。
22858
23227
  /** 按 scope 读取 SessionFile;不存在返回 null */
22859
23228
  readForScope(sessionId, scope) {
22860
23229
  return this.storeFor(scope).read(sessionId);
22861
23230
  }
22862
- /**
22863
- * 列出指定 persona 在指定 mode 下的所有 SessionFile。
22864
- * persona:listSubSessions RPC 入口(mode='listener');
22865
- * 路径下还未写过任何 session 时 list() 返回空数组(SessionStore.list 内部 ENOENT 处理)
22866
- */
22867
- listPersonaSessions(personaId, mode) {
22868
- return this.storeFor({ kind: "persona", personaId, mode }).list();
22869
- }
22870
23231
  /**
22871
23232
  * 创建 sub-session 的 SessionFile + 准备 runner(不 spawn)。
22872
23233
  * SubSessionMeta 不进 SessionFile schema,运行时由 scope → metaFromScope 派生。
@@ -23011,7 +23372,7 @@ var SessionManager = class {
23011
23372
  return { response: { ok: true }, broadcast };
23012
23373
  }
23013
23374
  /**
23014
- * 改 SessionFile.label(用于 listener-bound chat:rename RPC)。
23375
+ * 改 SessionFile.label(scope-explicit sub-session 重命名入口)。
23015
23376
  * 不存在抛 SESSION_NOT_FOUND。
23016
23377
  */
23017
23378
  renameForScope(args) {
@@ -23026,7 +23387,7 @@ var SessionManager = class {
23026
23387
  store.write({ ...file, label: args.label });
23027
23388
  }
23028
23389
  /**
23029
- * 删除 listener-scope sub-session(用于 chat:delete RPC)。
23390
+ * 删除 scope-explicit sub-session
23030
23391
  * - 若 runner 存在:走 reducer 'delete' command 收尾(SIGKILL + 文件 unlink + broadcast)
23031
23392
  * - 若 runner 不存在:直接删 scope store 文件 + 广播 session:deleted
23032
23393
  * 不存在抛 SESSION_NOT_FOUND。
@@ -23297,7 +23658,7 @@ var SessionManager = class {
23297
23658
 
23298
23659
  // src/persona/store.ts
23299
23660
  var fs6 = __toESM(require("fs"), 1);
23300
- var path7 = __toESM(require("path"), 1);
23661
+ var path8 = __toESM(require("path"), 1);
23301
23662
  init_protocol();
23302
23663
  var DEFAULT_SETTINGS = {
23303
23664
  permissions: {
@@ -23326,13 +23687,13 @@ var PersonaStore = class {
23326
23687
  }
23327
23688
  root;
23328
23689
  personaDir(personaId) {
23329
- return path7.join(this.root, safeFileName(personaId));
23690
+ return path8.join(this.root, safeFileName(personaId));
23330
23691
  }
23331
23692
  metaPath(personaId) {
23332
- return path7.join(this.personaDir(personaId), ".clawd", "persona.json");
23693
+ return path8.join(this.personaDir(personaId), ".clawd", "persona.json");
23333
23694
  }
23334
23695
  claudeMdPath(personaId) {
23335
- return path7.join(this.personaDir(personaId), "CLAUDE.md");
23696
+ return path8.join(this.personaDir(personaId), "CLAUDE.md");
23336
23697
  }
23337
23698
  /**
23338
23699
  * Sandbox settings 落盘路径 —— 故意放在 `.clawd/` 而不是 `.claude/`,让 CC 的
@@ -23342,11 +23703,11 @@ var PersonaStore = class {
23342
23703
  * 加载 persona 人格,只有 listener 多一层 OS sandbox。
23343
23704
  */
23344
23705
  sandboxSettingsPath(personaId) {
23345
- return path7.join(this.personaDir(personaId), ".clawd", "sandbox-settings.json");
23706
+ return path8.join(this.personaDir(personaId), ".clawd", "sandbox-settings.json");
23346
23707
  }
23347
23708
  write(persona, personality) {
23348
23709
  const dir = this.personaDir(persona.personaId);
23349
- fs6.mkdirSync(path7.join(dir, ".clawd"), { recursive: true });
23710
+ fs6.mkdirSync(path8.join(dir, ".clawd"), { recursive: true });
23350
23711
  this.atomicWrite(this.claudeMdPath(persona.personaId), personality);
23351
23712
  this.atomicWrite(
23352
23713
  this.sandboxSettingsPath(persona.personaId),
@@ -23364,6 +23725,10 @@ var PersonaStore = class {
23364
23725
  const p2 = this.metaPath(personaId);
23365
23726
  if (!fs6.existsSync(p2)) return null;
23366
23727
  const raw = JSON.parse(fs6.readFileSync(p2, "utf8"));
23728
+ if (raw && typeof raw === "object" && "tokenMap" in raw) {
23729
+ delete raw.tokenMap;
23730
+ this.atomicWrite(p2, JSON.stringify(raw, null, 2));
23731
+ }
23367
23732
  return PersonaFileSchema.parse(raw);
23368
23733
  }
23369
23734
  has(personaId) {
@@ -23389,14 +23754,14 @@ var PersonaStore = class {
23389
23754
  }
23390
23755
  /** Persona 私有 skills 目录路径:<personaDir>/.claude/skills */
23391
23756
  skillsDir(personaId) {
23392
- return path7.join(this.personaDir(personaId), ".claude", "skills");
23757
+ return path8.join(this.personaDir(personaId), ".claude", "skills");
23393
23758
  }
23394
23759
  /**
23395
23760
  * Claude Code 项目级 settings 路径:`<personaDir>/.claude/settings.json`。
23396
23761
  * 这里只读 `enabledPlugins` 字段,由 owner 通过 CC `/plugin` 之类命令维护,daemon 不写。
23397
23762
  */
23398
23763
  claudeSettingsPath(personaId) {
23399
- return path7.join(this.personaDir(personaId), ".claude", "settings.json");
23764
+ return path8.join(this.personaDir(personaId), ".claude", "settings.json");
23400
23765
  }
23401
23766
  /**
23402
23767
  * 读取 persona 的 `.claude/settings.json` 中 `enabledPlugins` map,把 value === true
@@ -23430,7 +23795,7 @@ var PersonaStore = class {
23430
23795
  list() {
23431
23796
  if (!fs6.existsSync(this.root)) return [];
23432
23797
  return fs6.readdirSync(this.root).filter((name) => {
23433
- return fs6.existsSync(path7.join(this.root, name, ".clawd", "persona.json"));
23798
+ return fs6.existsSync(path8.join(this.root, name, ".clawd", "persona.json"));
23434
23799
  });
23435
23800
  }
23436
23801
  remove(personaId) {
@@ -23455,12 +23820,19 @@ var PersonaRegistry = class {
23455
23820
  }
23456
23821
  store;
23457
23822
  cache = /* @__PURE__ */ new Map();
23458
- /** 从 store 全量重建缓存(boot 时 + 测试 setup 时) */
23823
+ /**
23824
+ * 从 store 全量重建缓存(boot 时 + 测试 setup 时)。
23825
+ * 单个 persona 读失败(损坏的 persona.json / schema 不匹配)不应阻塞 daemon 启动 ——
23826
+ * try/catch 隔离,让其它 persona 仍能加载。
23827
+ */
23459
23828
  reload() {
23460
23829
  this.cache.clear();
23461
23830
  for (const id of this.store.list()) {
23462
- const p2 = this.store.read(id);
23463
- if (p2) this.cache.set(p2.personaId, p2);
23831
+ try {
23832
+ const p2 = this.store.read(id);
23833
+ if (p2) this.cache.set(p2.personaId, p2);
23834
+ } catch {
23835
+ }
23464
23836
  }
23465
23837
  }
23466
23838
  get(personaId) {
@@ -23476,21 +23848,6 @@ var PersonaRegistry = class {
23476
23848
  list() {
23477
23849
  return Array.from(this.cache.values());
23478
23850
  }
23479
- /**
23480
- * file-sharing HTTP auth-context 用的逆向查表:只有 Bearer token,没有 personaId。
23481
- * 线性扫所有 persona.tokenMap 找到第一条 ok 命中的;revoked / non-public persona 跳过。
23482
- * persona 数量通常 < 100,性能可忽略。
23483
- */
23484
- findByToken(token) {
23485
- if (!token) return null;
23486
- for (const persona of this.cache.values()) {
23487
- if (!persona.public) continue;
23488
- const entry = persona.tokenMap?.[token];
23489
- if (!entry || entry.revoked) continue;
23490
- return { personaId: persona.personaId, label: entry.label };
23491
- }
23492
- return null;
23493
- }
23494
23851
  };
23495
23852
 
23496
23853
  // src/persona/manager.ts
@@ -23784,9 +24141,6 @@ var PersonaManager = class {
23784
24141
  model: args.model,
23785
24142
  public: args.public ?? false,
23786
24143
  iconKey: args.iconKey,
23787
- // 初始化空 token 集合;后续由 issueToken / revokeToken 维护,
23788
- // file-sharing HTTP Bearer 鉴权用 PersonaRegistry.findByToken 反查
23789
- tokenMap: {},
23790
24144
  createdAt: now,
23791
24145
  updatedAt: now
23792
24146
  };
@@ -23836,52 +24190,22 @@ var PersonaManager = class {
23836
24190
  }
23837
24191
  /**
23838
24192
  * 删除 persona。
23839
- * PersonaStore.remove(personaId) 已级联删除整个 <personaRoot>/<personaId>/ 目录。
24193
+ * PersonaStore.remove(personaId) 已级联删除整个 <personaRoot>/<personaId>/ 目录,
24194
+ * Phase 2 capability platform 新布局下含 .clawd/sessions/owner/ +
24195
+ * .clawd/sessions/guests/<capId>/, rmSync recursive force 一锅清.
24196
+ *
24197
+ * ⚠️ TODO (Phase 3+ design gap, reviewer P1 flagged):
24198
+ * ~/.clawd/capabilities.json 里若有 cap.grants 含本 personaId, grant 会变成
24199
+ * 悬空引用 (guest 仍能持 token 接入, 但调依赖该 persona 的 RPC 会 fail).
24200
+ * 选项: (A) 这里扫所有 cap 过滤掉该 personaId 的 grant; (B) revoke 所有 grant
24201
+ * 含该 personaId 的 cap. 当前因 cleanupGuestSessionsForCapability 用 rmSync
24202
+ * force=true 吞 ENOENT 不会 crash, 但 UI CapabilityManagerDrawer 会展示
24203
+ * "无效的 persona" grant. Phase 3 加 cross-reference 矩阵后正式实现.
23840
24204
  */
23841
24205
  delete(personaId) {
23842
24206
  this.deps.store.remove(personaId);
23843
24207
  this.deps.registry.remove(personaId);
23844
24208
  }
23845
- /**
23846
- * 生成 32-char base64url token 并写入 tokenMap,给 file-sharing HTTP Bearer
23847
- * 鉴权用(PersonaRegistry.findByToken 反查 personaId/label)。
23848
- */
23849
- issueToken(personaId, label) {
23850
- const existing = this.deps.registry.get(personaId);
23851
- if (!existing) throw new Error(`persona not found: ${personaId}`);
23852
- const token = import_node_crypto3.default.randomBytes(24).toString("base64url");
23853
- const entry = {
23854
- label,
23855
- issuedAt: Date.now(),
23856
- revoked: false
23857
- };
23858
- const updated = {
23859
- ...existing,
23860
- tokenMap: { ...existing.tokenMap ?? {}, [token]: entry },
23861
- updatedAt: Date.now()
23862
- };
23863
- this.deps.store.writeMeta(updated);
23864
- this.deps.registry.set(updated);
23865
- return { token, persona: updated };
23866
- }
23867
- /** 标记 token 为 revoked(保留条目用于审计);不存在的 token 抛错。 */
23868
- revokeToken(personaId, token) {
23869
- const existing = this.deps.registry.get(personaId);
23870
- if (!existing) throw new Error(`persona not found: ${personaId}`);
23871
- const tokenEntry = existing.tokenMap?.[token];
23872
- if (!tokenEntry) throw new Error(`token not found in persona ${personaId}`);
23873
- const updated = {
23874
- ...existing,
23875
- tokenMap: {
23876
- ...existing.tokenMap ?? {},
23877
- [token]: { ...tokenEntry, revoked: true }
23878
- },
23879
- updatedAt: Date.now()
23880
- };
23881
- this.deps.store.writeMeta(updated);
23882
- this.deps.registry.set(updated);
23883
- return updated;
23884
- }
23885
24209
  /**
23886
24210
  * label 转 4-16 char slug。优先用 `persona-<slug>`;若 slug 已被占用,追加 4 char
23887
24211
  * base64url 随机后缀直到不撞为止(最多 5 次,理论冲突 ≈ 2^-24,留个 panic 上限)。
@@ -23901,7 +24225,7 @@ var PersonaManager = class {
23901
24225
 
23902
24226
  // src/persona/seed.ts
23903
24227
  var fs8 = __toESM(require("fs"), 1);
23904
- var path9 = __toESM(require("path"), 1);
24228
+ var path10 = __toESM(require("path"), 1);
23905
24229
  var import_node_url = require("url");
23906
24230
  var import_meta = {};
23907
24231
  var DEFAULT_PERSONAS = [
@@ -23937,14 +24261,14 @@ var DEFAULT_PERSONAS = [
23937
24261
  function findDefaultsRoot() {
23938
24262
  const candidates = [];
23939
24263
  try {
23940
- const here = path9.dirname((0, import_node_url.fileURLToPath)(import_meta.url));
23941
- candidates.push(path9.resolve(here, "defaults"));
23942
- candidates.push(path9.resolve(here, "persona-defaults"));
24264
+ const here = path10.dirname((0, import_node_url.fileURLToPath)(import_meta.url));
24265
+ candidates.push(path10.resolve(here, "defaults"));
24266
+ candidates.push(path10.resolve(here, "persona-defaults"));
23943
24267
  } catch {
23944
24268
  }
23945
24269
  if (process.argv[1]) {
23946
- const argvDir = path9.dirname(process.argv[1]);
23947
- candidates.push(path9.resolve(argvDir, "persona-defaults"));
24270
+ const argvDir = path10.dirname(process.argv[1]);
24271
+ candidates.push(path10.resolve(argvDir, "persona-defaults"));
23948
24272
  }
23949
24273
  for (const c of candidates) {
23950
24274
  try {
@@ -23961,7 +24285,7 @@ function seedDefaultPersonas(args) {
23961
24285
  args.logger.info("persona.seed.skip", { personaId: entry.personaId, reason: "exists" });
23962
24286
  continue;
23963
24287
  }
23964
- const bundleDir = path9.join(args.defaultsRoot, entry.personaId);
24288
+ const bundleDir = path10.join(args.defaultsRoot, entry.personaId);
23965
24289
  if (!fs8.existsSync(bundleDir)) {
23966
24290
  args.logger.warn("persona.seed.skip", {
23967
24291
  personaId: entry.personaId,
@@ -23970,7 +24294,7 @@ function seedDefaultPersonas(args) {
23970
24294
  });
23971
24295
  continue;
23972
24296
  }
23973
- const claudeMdPath = path9.join(bundleDir, "CLAUDE.md");
24297
+ const claudeMdPath = path10.join(bundleDir, "CLAUDE.md");
23974
24298
  if (!fs8.existsSync(claudeMdPath)) {
23975
24299
  args.logger.warn("persona.seed.skip", {
23976
24300
  personaId: entry.personaId,
@@ -23987,7 +24311,6 @@ function seedDefaultPersonas(args) {
23987
24311
  model: entry.model,
23988
24312
  public: entry.public,
23989
24313
  iconKey: entry.iconKey,
23990
- tokenMap: {},
23991
24314
  createdAt: now,
23992
24315
  updatedAt: now
23993
24316
  };
@@ -23999,8 +24322,8 @@ function seedDefaultPersonas(args) {
23999
24322
  function copyBundleExtras(srcDir, dstDir) {
24000
24323
  for (const entry of fs8.readdirSync(srcDir, { withFileTypes: true })) {
24001
24324
  if (entry.name === "CLAUDE.md" || entry.name === ".clawd") continue;
24002
- const srcPath = path9.join(srcDir, entry.name);
24003
- const dstPath = path9.join(dstDir, entry.name);
24325
+ const srcPath = path10.join(srcDir, entry.name);
24326
+ const dstPath = path10.join(dstDir, entry.name);
24004
24327
  if (entry.isDirectory()) {
24005
24328
  fs8.cpSync(srcPath, dstPath, { recursive: true, dereference: true });
24006
24329
  } else if (entry.isFile()) {
@@ -25341,6 +25664,7 @@ var import_sender = __toESM(require_sender(), 1);
25341
25664
  var import_subprotocol = __toESM(require_subprotocol(), 1);
25342
25665
  var import_websocket = __toESM(require_websocket(), 1);
25343
25666
  var import_websocket_server = __toESM(require_websocket_server(), 1);
25667
+ var wrapper_default = import_websocket.default;
25344
25668
 
25345
25669
  // src/transport/local-ws-server.ts
25346
25670
  var import_node_http = __toESM(require("http"), 1);
@@ -25355,6 +25679,9 @@ var LocalWsServer = class {
25355
25679
  httpServer = null;
25356
25680
  frameHandler = null;
25357
25681
  clients = /* @__PURE__ */ new Map();
25682
+ // Task 1.7 capability platform:capId → Set<clientId>,撤销时 O(1) 找到该 cap 的
25683
+ // 所有活跃连接做关闭级联(Task 1.10)。同一 cap 可能多端同时连(多设备 / 多 tab)。
25684
+ capabilityIdToClients = /* @__PURE__ */ new Map();
25358
25685
  logger;
25359
25686
  pingIntervalMs;
25360
25687
  async start() {
@@ -25445,6 +25772,49 @@ var LocalWsServer = class {
25445
25772
  this.safeSend(c.ws, frame);
25446
25773
  }
25447
25774
  }
25775
+ // Task 1.9 capability platform:仅广播给 owner 连接(跳过 guest ws)。
25776
+ // 用于 capability:tokenIssued / capability:tokenDeleted 等 owner-only push frame——
25777
+ // guest 没必要也无法消费这类管理帧。noAuth localhost ws 没有 ctx, 视作 owner.
25778
+ broadcastToOwners(frame) {
25779
+ const gate = this.opts.authGate;
25780
+ for (const c of this.clients.values()) {
25781
+ if (gate && !gate.isAuthed(c.handle.id)) continue;
25782
+ if (c.ctx && c.ctx.principal.kind !== "owner") continue;
25783
+ this.safeSend(c.ws, frame);
25784
+ }
25785
+ }
25786
+ // capability platform v3 inbox 重写: 推给某条 cap channel 的两端 ws.
25787
+ // - 所有 owner ws (owner 看自家所有 channel)
25788
+ // - 该 capabilityId 的所有 guest ws (该 cap 持有人的多端/多 tab)
25789
+ // 一次调用同时推两端, inbox:event push 帧由此走.
25790
+ broadcastToCapabilityChannel(capabilityId, frame) {
25791
+ this.broadcastToOwners(frame);
25792
+ const set = this.capabilityIdToClients.get(capabilityId);
25793
+ if (!set) return;
25794
+ for (const id of set) {
25795
+ const c = this.clients.get(id);
25796
+ if (!c) continue;
25797
+ this.safeSend(c.ws, frame);
25798
+ }
25799
+ }
25800
+ // Phase 4 capability platform DM: 广播到指定 principal 的所有活跃 ws.
25801
+ // principalId === ownerPrincipalId → 同 broadcastToOwners (所有 owner ws)
25802
+ // principalId === 'owner' sentinel → 同上(向后兼容遗留 caller / 协议层 sentinel)
25803
+ // principalId === 'cap_xxx' → 该 capability 的所有 guest ws (多端 / 多 tab)
25804
+ // 用于 DM inbox:event 路由: from + to 两侧各播一次, 让双方 UI 实时看到 thread 更新.
25805
+ broadcastToPrincipal(principalId, frame) {
25806
+ if (principalId === "owner" || principalId === this.opts.ownerPrincipalId) {
25807
+ this.broadcastToOwners(frame);
25808
+ return;
25809
+ }
25810
+ const set = this.capabilityIdToClients.get(principalId);
25811
+ if (!set) return;
25812
+ for (const id of set) {
25813
+ const c = this.clients.get(id);
25814
+ if (!c) continue;
25815
+ this.safeSend(c.ws, frame);
25816
+ }
25817
+ }
25448
25818
  firstSubscriber(sessionId) {
25449
25819
  for (const c of this.clients.values()) {
25450
25820
  if (c.handle.subscribedSessions.has(sessionId)) return c.handle;
@@ -25466,6 +25836,40 @@ var LocalWsServer = class {
25466
25836
  if (!c) return;
25467
25837
  this.safeSend(c.ws, frame);
25468
25838
  }
25839
+ // Task 1.7 capability platform:AuthGate.onAuthed 回调里调本方法,把 ConnectionContext
25840
+ // 绑到 client;guest 连接同时进 capId 索引(Task 1.10 撤销级联用)。
25841
+ attachClientContext(clientId, ctx) {
25842
+ const c = this.clients.get(clientId);
25843
+ if (!c) return;
25844
+ c.ctx = ctx;
25845
+ if (ctx.capabilityId) {
25846
+ let set = this.capabilityIdToClients.get(ctx.capabilityId);
25847
+ if (!set) {
25848
+ set = /* @__PURE__ */ new Set();
25849
+ this.capabilityIdToClients.set(ctx.capabilityId, set);
25850
+ }
25851
+ set.add(clientId);
25852
+ }
25853
+ }
25854
+ // 测试 / Task 1.8 dispatcher 用:拿 client 当前 ConnectionContext(null = 未鉴权 / noAuth)
25855
+ getClientContext(clientId) {
25856
+ return this.clients.get(clientId)?.ctx ?? null;
25857
+ }
25858
+ // Task 1.10 撤销级联:关闭某 capability 的所有活跃 ws 连接。close code 4401
25859
+ // 让客户端识别 'capability revoked' 而非普通断连。
25860
+ closeConnectionsByCapability(capabilityId, code = 4401, reason = "TOKEN_REVOKED") {
25861
+ const set = this.capabilityIdToClients.get(capabilityId);
25862
+ if (!set) return;
25863
+ const ids = [...set];
25864
+ for (const id of ids) {
25865
+ const c = this.clients.get(id);
25866
+ if (!c) continue;
25867
+ try {
25868
+ c.ws.close(code, reason);
25869
+ } catch {
25870
+ }
25871
+ }
25872
+ }
25469
25873
  // URL path 路由:'/' → 主连接路径;其他 → close 4404
25470
25874
  routeConnection(socket, req) {
25471
25875
  const remoteAddress = req?.socket?.remoteAddress;
@@ -25500,7 +25904,7 @@ var LocalWsServer = class {
25500
25904
  } catch {
25501
25905
  }
25502
25906
  }, this.pingIntervalMs);
25503
- this.clients.set(id, { handle, ws: socket, pingTimer });
25907
+ this.clients.set(id, { handle, ws: socket, pingTimer, ctx: null });
25504
25908
  this.logger?.info("client connected", { clientId: id, total: this.clients.size, remoteAddress });
25505
25909
  const authGate = this.opts.authGate;
25506
25910
  let authed = true;
@@ -25524,6 +25928,12 @@ var LocalWsServer = class {
25524
25928
  socket.on("close", () => {
25525
25929
  const c = this.clients.get(id);
25526
25930
  if (c?.pingTimer) clearInterval(c.pingTimer);
25931
+ const capId = c?.ctx?.capabilityId;
25932
+ if (capId) {
25933
+ const set = this.capabilityIdToClients.get(capId);
25934
+ set?.delete(id);
25935
+ if (set && set.size === 0) this.capabilityIdToClients.delete(capId);
25936
+ }
25527
25937
  this.clients.delete(id);
25528
25938
  authGate?.unregister(id);
25529
25939
  this.logger?.info("client disconnected", { clientId: id, remaining: this.clients.size });
@@ -25556,6 +25966,17 @@ var LocalWsServer = class {
25556
25966
  return;
25557
25967
  }
25558
25968
  } else if (frame.type === "auth") {
25969
+ const token = typeof frame.token === "string" ? frame.token ?? "" : "";
25970
+ const selfPrincipalId = typeof frame.selfPrincipalId === "string" ? frame.selfPrincipalId ?? "" : "";
25971
+ const selfDisplayName = typeof frame.selfDisplayName === "string" ? frame.selfDisplayName ?? "" : "";
25972
+ if (token && this.opts.tryVerifyCapabilityToken) {
25973
+ const guestCtx = this.opts.tryVerifyCapabilityToken(
25974
+ token,
25975
+ selfPrincipalId || void 0,
25976
+ selfDisplayName || void 0
25977
+ );
25978
+ if (guestCtx) this.attachClientContext(client.id, guestCtx);
25979
+ }
25559
25980
  this.safeSend(this.clients.get(client.id).ws, { type: "auth:ok" });
25560
25981
  return;
25561
25982
  }
@@ -25672,15 +26093,31 @@ var AuthGate = class {
25672
26093
  this.markFailed(handle.id);
25673
26094
  return frame.type === "auth" ? "consumed" : "reject";
25674
26095
  }
25675
- if (!this.opts.expectedToken) {
25676
- this.opts.closeConnection(handle, 1008, "auth not configured");
25677
- this.markFailed(handle.id);
25678
- return "consumed";
25679
- }
25680
- if (!constantTimeEqual(parsed.data.token, this.opts.expectedToken)) {
25681
- this.opts.closeConnection(handle, 1008, "auth failed");
25682
- this.markFailed(handle.id);
25683
- return "consumed";
26096
+ let ctx = null;
26097
+ if (this.opts.authenticate) {
26098
+ const r = this.opts.authenticate(
26099
+ parsed.data.token,
26100
+ parsed.data.selfPrincipalId,
26101
+ parsed.data.selfDisplayName
26102
+ );
26103
+ if (!r.ok) {
26104
+ this.opts.closeConnection(handle, 4401, r.code);
26105
+ this.markFailed(handle.id);
26106
+ return "consumed";
26107
+ }
26108
+ ctx = r.context;
26109
+ } else {
26110
+ if (!this.opts.expectedToken) {
26111
+ this.opts.closeConnection(handle, 1008, "auth not configured");
26112
+ this.markFailed(handle.id);
26113
+ return "consumed";
26114
+ }
26115
+ if (!constantTimeEqual(parsed.data.token, this.opts.expectedToken)) {
26116
+ this.opts.closeConnection(handle, 1008, "auth failed");
26117
+ this.markFailed(handle.id);
26118
+ return "consumed";
26119
+ }
26120
+ ctx = this.opts.buildOwnerContext?.() ?? null;
25684
26121
  }
25685
26122
  if (st.timer != null) {
25686
26123
  const clear = this.opts.clearTimer ?? ((h) => clearTimeout(h));
@@ -25688,6 +26125,7 @@ var AuthGate = class {
25688
26125
  }
25689
26126
  st.authed = true;
25690
26127
  st.timer = null;
26128
+ if (ctx && this.opts.onAuthed) this.opts.onAuthed(handle, ctx);
25691
26129
  this.opts.sendOk(handle, { type: "auth:ok" });
25692
26130
  return "consumed";
25693
26131
  }
@@ -25723,7 +26161,7 @@ var AuthContextResolver = class {
25723
26161
  }
25724
26162
  opts;
25725
26163
  /**
25726
- * 从 `Authorization` 头解析。null = 无凭证 / 不识别 / revoked,调用方一律 401。
26164
+ * 从 `Authorization` 头解析。null = 无凭证 / 不识别,调用方一律 401。
25727
26165
  * remoteAddress 用于 isLoopback 判定(loopback = 127.0.0.1 / ::1 / ::ffff:127.0.0.1)。
25728
26166
  */
25729
26167
  resolveFromHeader(authHeader, remoteAddress) {
@@ -25733,15 +26171,6 @@ var AuthContextResolver = class {
25733
26171
  if (this.opts.ownerToken && constantTimeEqual2(token, this.opts.ownerToken)) {
25734
26172
  return { role: "owner", isLoopback };
25735
26173
  }
25736
- const personalHit = this.opts.personaRegistry.findByToken(token);
25737
- if (personalHit) {
25738
- return {
25739
- role: "personal",
25740
- personaId: personalHit.personaId,
25741
- label: personalHit.label,
25742
- isLoopback
25743
- };
25744
- }
25745
26174
  return null;
25746
26175
  }
25747
26176
  };
@@ -25763,206 +26192,753 @@ function constantTimeEqual2(a, b2) {
25763
26192
  return diff2 === 0;
25764
26193
  }
25765
26194
 
25766
- // src/transport/http-router.ts
25767
- var import_node_fs14 = __toESM(require("fs"), 1);
25768
- var import_node_path16 = __toESM(require("path"), 1);
26195
+ // ../protocol/src/index.ts
26196
+ init_runtime();
25769
26197
 
25770
- // src/attachment/group.ts
25771
- var import_node_fs13 = __toESM(require("fs"), 1);
25772
- var import_node_path14 = __toESM(require("path"), 1);
25773
- var import_node_crypto4 = __toESM(require("crypto"), 1);
25774
- init_protocol();
25775
- var GroupFileStore = class {
26198
+ // src/transport/connection-context.ts
26199
+ function ownerContext(ownerPrincipalId, displayName) {
26200
+ return {
26201
+ principal: makeOwnerPrincipal(ownerPrincipalId, displayName),
26202
+ grants: [{ resource: { type: "*" }, actions: ["admin"] }]
26203
+ };
26204
+ }
26205
+ function guestContext(cap) {
26206
+ return {
26207
+ principal: { id: cap.id, kind: "guest", displayName: cap.displayName },
26208
+ grants: cap.grants,
26209
+ capabilityId: cap.id
26210
+ };
26211
+ }
26212
+ function authenticate(token, deps) {
26213
+ if (!token) return { ok: false, code: "NO_TOKEN" };
26214
+ if (deps.isOwnerToken(token)) {
26215
+ return { ok: true, context: ownerContext(deps.ownerPrincipalId, deps.ownerDisplayName) };
26216
+ }
26217
+ if (!deps.capabilityRegistry) return { ok: false, code: "BAD_TOKEN" };
26218
+ const v2 = deps.capabilityRegistry.verifyToken(token);
26219
+ if (!v2.ok) {
26220
+ if (v2.code === "TOKEN_INVALID") return { ok: false, code: "BAD_TOKEN" };
26221
+ return { ok: false, code: v2.code };
26222
+ }
26223
+ if (deps.selfPrincipalId && deps.selfDisplayName && deps.onGuestHello) {
26224
+ try {
26225
+ deps.onGuestHello(v2.capability.id, deps.selfPrincipalId, deps.selfDisplayName);
26226
+ } catch (e) {
26227
+ if (e.message === "TOKEN_SHARED_LEAK") {
26228
+ return { ok: false, code: "TOKEN_REVOKED" };
26229
+ }
26230
+ throw e;
26231
+ }
26232
+ }
26233
+ return { ok: true, context: guestContext(v2.capability) };
26234
+ }
26235
+
26236
+ // src/permission/capability-store.ts
26237
+ var fs15 = __toESM(require("fs"), 1);
26238
+ var path18 = __toESM(require("path"), 1);
26239
+ var CAPABILITIES_FILE_NAME = "capabilities.json";
26240
+ var FILE_VERSION = 1;
26241
+ var CapabilityStore = class {
26242
+ constructor(dataDir) {
26243
+ this.dataDir = dataDir;
26244
+ fs15.mkdirSync(dataDir, { recursive: true });
26245
+ this.cache = this.readFromDisk();
26246
+ }
25776
26247
  dataDir;
25777
- logger;
25778
- cache = /* @__PURE__ */ new Map();
25779
- constructor(opts) {
25780
- this.dataDir = opts.dataDir;
25781
- this.logger = opts.logger;
26248
+ cache;
26249
+ list() {
26250
+ return [...this.cache];
25782
26251
  }
25783
- rootForScope(scope) {
25784
- return import_node_path14.default.join(this.dataDir, "sessions", ...scopeSubPath(scope).map(safeFileName));
26252
+ upsert(cap) {
26253
+ const idx = this.cache.findIndex((c) => c.id === cap.id);
26254
+ if (idx >= 0) {
26255
+ this.cache[idx] = cap;
26256
+ } else {
26257
+ this.cache.push(cap);
26258
+ }
26259
+ this.flush();
25785
26260
  }
25786
- /** 与 SessionStore.filePath 平级,扩展名 .group-files.json */
25787
- filePath(scope, sessionId) {
25788
- return import_node_path14.default.join(this.rootForScope(scope), `${safeFileName(sessionId)}.group-files.json`);
26261
+ remove(id) {
26262
+ const next = this.cache.filter((c) => c.id !== id);
26263
+ if (next.length === this.cache.length) return;
26264
+ this.cache = next;
26265
+ this.flush();
25789
26266
  }
25790
- cacheKey(scope, sessionId) {
25791
- return scope.kind === "default" ? `default::${sessionId}` : `persona:${scope.personaId}:${scope.mode}::${sessionId}`;
26267
+ filePath() {
26268
+ return path18.join(this.dataDir, CAPABILITIES_FILE_NAME);
25792
26269
  }
25793
- /** 从磁盘读一份;不存在 → 空数组;schema 不匹配的条目 → 跳过(防腐) */
25794
- readFile(scope, sessionId) {
25795
- const file = this.filePath(scope, sessionId);
26270
+ readFromDisk() {
26271
+ const file = this.filePath();
26272
+ let raw;
25796
26273
  try {
25797
- const raw = import_node_fs13.default.readFileSync(file, "utf8");
25798
- const parsed = JSON.parse(raw);
25799
- if (!Array.isArray(parsed)) {
25800
- this.logger?.warn("GroupFileStore.readFile: not an array; resetting session entries", {
25801
- file
25802
- });
25803
- return [];
25804
- }
25805
- const out = [];
25806
- for (const entry of parsed) {
25807
- const r = GroupFileEntrySchema.safeParse(entry);
25808
- if (r.success) out.push(r.data);
25809
- }
25810
- return out;
26274
+ raw = fs15.readFileSync(file, "utf8");
25811
26275
  } catch (err) {
25812
- const code = err?.code;
25813
- if (code === "ENOENT") return [];
25814
- this.logger?.warn("GroupFileStore.readFile failed", {
25815
- file,
25816
- err: err.message
25817
- });
26276
+ if (err?.code === "ENOENT") return [];
25818
26277
  return [];
25819
26278
  }
26279
+ if (!raw.trim()) return [];
26280
+ let parsed;
26281
+ try {
26282
+ parsed = JSON.parse(raw);
26283
+ } catch {
26284
+ return [];
26285
+ }
26286
+ if (!parsed || typeof parsed !== "object") return [];
26287
+ const arr = parsed.capabilities;
26288
+ if (!Array.isArray(arr)) return [];
26289
+ const out = [];
26290
+ for (const item of arr) {
26291
+ const r = CapabilitySchema.safeParse(item);
26292
+ if (r.success) out.push(r.data);
26293
+ }
26294
+ return out;
25820
26295
  }
25821
- writeFile(scope, sessionId, entries) {
25822
- const file = this.filePath(scope, sessionId);
25823
- import_node_fs13.default.mkdirSync(import_node_path14.default.dirname(file), { recursive: true });
25824
- const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
25825
- import_node_fs13.default.writeFileSync(tmp, JSON.stringify(entries, null, 2), { mode: 384 });
25826
- import_node_fs13.default.renameSync(tmp, file);
26296
+ flush() {
26297
+ const content = { version: FILE_VERSION, capabilities: this.cache };
26298
+ this.atomicWrite(this.filePath(), JSON.stringify(content, null, 2));
25827
26299
  }
25828
- /** 拉一份当前 session 的清单。读盘 → cache;之后调用复用 cache */
25829
- list(scope, sessionId) {
25830
- const key = this.cacheKey(scope, sessionId);
25831
- const cached = this.cache.get(key);
25832
- if (cached) return cached.entries;
25833
- const entries = this.readFile(scope, sessionId);
25834
- this.cache.set(key, { entries, scope });
25835
- return entries;
26300
+ atomicWrite(file, content) {
26301
+ const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
26302
+ fs15.writeFileSync(tmp, content, { mode: 384 });
26303
+ fs15.renameSync(tmp, file);
26304
+ try {
26305
+ fs15.chmodSync(file, 384);
26306
+ } catch {
26307
+ }
26308
+ }
26309
+ };
26310
+
26311
+ // src/permission/capability-registry.ts
26312
+ var crypto4 = __toESM(require("crypto"), 1);
26313
+ var CapabilityRegistry = class {
26314
+ constructor(store, now = () => Date.now()) {
26315
+ this.store = store;
26316
+ this.now = now;
26317
+ for (const cap of store.list()) {
26318
+ this.bySecretHash.set(cap.secretHash, cap);
26319
+ }
26320
+ }
26321
+ store;
26322
+ now;
26323
+ // sha256(token) → Capability
26324
+ bySecretHash = /* @__PURE__ */ new Map();
26325
+ list() {
26326
+ return this.store.list();
26327
+ }
26328
+ verifyToken(token) {
26329
+ if (!token) return { ok: false, code: "TOKEN_INVALID" };
26330
+ const hash = sha256Hex(token);
26331
+ const cap = this.bySecretHash.get(hash);
26332
+ if (!cap) return { ok: false, code: "TOKEN_INVALID" };
26333
+ if (cap.expiresAt && this.now() >= cap.expiresAt) {
26334
+ return { ok: false, code: "TOKEN_EXPIRED" };
26335
+ }
26336
+ if (cap.maxUses && cap.usedCount >= cap.maxUses) {
26337
+ return { ok: false, code: "TOKEN_EXHAUSTED" };
26338
+ }
26339
+ const updated = { ...cap, usedCount: cap.usedCount + 1 };
26340
+ this.bySecretHash.set(hash, updated);
26341
+ this.store.upsert(updated);
26342
+ return { ok: true, capability: updated };
26343
+ }
26344
+ upsertCapability(cap) {
26345
+ this.bySecretHash.set(cap.secretHash, cap);
26346
+ this.store.upsert(cap);
25836
26347
  }
25837
26348
  /**
25838
- * upsert:
25839
- * - relPath 已存在 更新 lastEditedAt + clear stale;不动 from / addedAt / id
25840
- * (保留首次入群人 / 入群时刻)
25841
- * - 不存在 → append,id 派生稳定 uuid,addedAt = now
25842
- *
25843
- * 返回最新 entry(caller 可用来 broadcast 通知)。
26349
+ * Hard delete:从 store 物理移除 + 从 Map 清掉 secretHash 索引。
26350
+ * 后续 verifyToken token 'TOKEN_INVALID' 分支(secretHash 已不在 Map)。
26351
+ * idempotent:不存在 → null。
25844
26352
  */
25845
- upsert(scope, sessionId, input, now = Date.now()) {
25846
- const entries = this.list(scope, sessionId).slice();
25847
- const idx = entries.findIndex((e) => e.relPath === input.relPath);
25848
- let next;
25849
- if (idx >= 0) {
25850
- const prev = entries[idx];
25851
- next = {
25852
- ...prev,
25853
- size: input.size,
25854
- mime: input.mime,
25855
- lastEditedAt: now,
25856
- stale: false
25857
- // label 不在 upsert 路径覆盖(owner +Add 走另一条路径)
25858
- };
25859
- entries[idx] = next;
25860
- } else {
25861
- next = {
25862
- id: `gf-${import_node_crypto4.default.randomBytes(6).toString("base64url")}`,
25863
- relPath: input.relPath,
25864
- from: input.from,
25865
- label: input.label,
25866
- size: input.size,
25867
- mime: input.mime,
25868
- addedAt: now
25869
- // agent 第一次 upsert 时不写 lastEditedAt(语义上 = 首次"编辑" = addedAt)
25870
- };
25871
- entries.push(next);
26353
+ delete(id) {
26354
+ const current = this.store.list().find((c) => c.id === id);
26355
+ if (!current) return null;
26356
+ this.bySecretHash.delete(current.secretHash);
26357
+ this.store.remove(id);
26358
+ return current;
26359
+ }
26360
+ findById(id) {
26361
+ return this.store.list().find((c) => c.id === id) ?? null;
26362
+ }
26363
+ };
26364
+ function sha256Hex(s) {
26365
+ return crypto4.createHash("sha256").update(s).digest("hex");
26366
+ }
26367
+
26368
+ // src/permission/capability-manager.ts
26369
+ var crypto5 = __toESM(require("crypto"), 1);
26370
+ var CapabilityManager = class {
26371
+ constructor(registry2, hooks = {}) {
26372
+ this.registry = registry2;
26373
+ this.hooks = hooks;
26374
+ }
26375
+ registry;
26376
+ hooks;
26377
+ list() {
26378
+ return this.registry.list();
26379
+ }
26380
+ // whoami handler 用:根据 capabilityId 反查 caller 的 capability 实体。
26381
+ // 返回 Capability 持久化形态;handler 调 stripSecretHash 后再上 wire。
26382
+ findById(id) {
26383
+ return this.registry.findById(id);
26384
+ }
26385
+ issue(args) {
26386
+ if (!args.displayName.trim()) {
26387
+ throw new Error("CapabilityManager.issue: displayName must be non-empty");
25872
26388
  }
25873
- this.writeFile(scope, sessionId, entries);
25874
- this.cache.set(this.cacheKey(scope, sessionId), { entries, scope });
25875
- return next;
26389
+ const token = (this.hooks.generateToken ?? defaultGenerateToken)();
26390
+ const id = (this.hooks.generateId ?? defaultGenerateId)();
26391
+ const now = (this.hooks.now ?? Date.now)();
26392
+ const cap = {
26393
+ id,
26394
+ secretHash: sha256Hex2(token),
26395
+ displayName: args.displayName,
26396
+ grants: args.grants,
26397
+ issuedAt: now,
26398
+ usedCount: 0,
26399
+ ...args.expiresAt !== void 0 ? { expiresAt: args.expiresAt } : {},
26400
+ ...args.maxUses !== void 0 ? { maxUses: args.maxUses } : {}
26401
+ };
26402
+ this.registry.upsertCapability(cap);
26403
+ this.hooks.onIssued?.(cap, token);
26404
+ return { token, capability: cap };
25876
26405
  }
25877
26406
  /**
25878
- * 标记一个 relPath stale(agent rm / mv 后文件不在)。
25879
- * - 命中 stale=true,UI 灰显
25880
- * - 未命中 noop(不需要为不在群里的文件创建 stale 条目)
25881
- *
25882
- * 注:spec §6 "Bash 命令是 rm 形态"启发式不强求 — runner 暂不调,留给后续优化
26407
+ * guest auth 帧捎带 selfPrincipalId/selfDisplayName 时,AuthGate.authenticate
26408
+ * 校验 cap token 通过后调本方法把 peer 字段写回 cap:
26409
+ * - 首次(cap.firstUsedByPeerAt 未设)→ 写盘 + 触发 onIssued 复用 push 频道
26410
+ * 让 owner UI 通过 capability:updated 实时拿到 peer 字段
26411
+ * - 重复(同 peerOwnerId)→ noop
26412
+ * - 冲突(不同 peerOwnerId)→ throw TOKEN_SHARED_LEAK,AuthGate 转 close 4401
25883
26413
  */
25884
- markStale(scope, sessionId, relPath) {
25885
- const entries = this.list(scope, sessionId).slice();
25886
- const idx = entries.findIndex((e) => e.relPath === relPath);
25887
- if (idx < 0) return;
25888
- if (entries[idx].stale) return;
25889
- entries[idx] = { ...entries[idx], stale: true };
25890
- this.writeFile(scope, sessionId, entries);
25891
- this.cache.set(this.cacheKey(scope, sessionId), { entries, scope });
26414
+ recordPeerHello(capId, args) {
26415
+ const cap = this.registry.findById(capId);
26416
+ if (!cap) return { changed: false };
26417
+ if (cap.firstUsedByPeerAt != null) {
26418
+ if (cap.peerOwnerId !== args.ownerPrincipalId) {
26419
+ throw new Error("TOKEN_SHARED_LEAK");
26420
+ }
26421
+ return { changed: false };
26422
+ }
26423
+ const updated = {
26424
+ ...cap,
26425
+ peerOwnerId: args.ownerPrincipalId,
26426
+ firstUsedByPeerAt: args.now,
26427
+ peerDisplayName: args.displayName
26428
+ };
26429
+ this.registry.upsertCapability(updated);
26430
+ this.hooks.onIssued?.(updated, "__peer_hello__");
26431
+ return { changed: true };
25892
26432
  }
25893
26433
  /**
25894
- * 真删一条群文件条目(用于 owner 撤销自己 +Add 的入群操作)。
25895
- * agent 自动入群的不应走这条 —— markStale 表达"文件不在了"语义;
25896
- * owner 手动加错了,应该能彻底从列表移除而不是留个 stale 占位。
25897
- *
25898
- * 返回值:true=命中并删除;false=relPath 不在群里。
26434
+ * Hard delete:从 registry / store 物理移除 capability。
26435
+ * idempotent:不存在 null(handler 翻译成 VALIDATION_ERROR)
26436
+ * onDeleted hook daemon/index.ts wire 负责 close 连接 + cleanup 文件 + 广播
25899
26437
  */
25900
- remove(scope, sessionId, relPath) {
25901
- const entries = this.list(scope, sessionId).slice();
25902
- const idx = entries.findIndex((e) => e.relPath === relPath);
25903
- if (idx < 0) return false;
25904
- entries.splice(idx, 1);
25905
- this.writeFile(scope, sessionId, entries);
25906
- this.cache.set(this.cacheKey(scope, sessionId), { entries, scope });
25907
- return true;
26438
+ delete(id) {
26439
+ const removed = this.registry.delete(id);
26440
+ if (!removed) return null;
26441
+ this.hooks.onDeleted?.(removed);
26442
+ return { capability: removed };
25908
26443
  }
26444
+ };
26445
+ function defaultGenerateToken() {
26446
+ return crypto5.randomBytes(24).toString("base64url");
26447
+ }
26448
+ function defaultGenerateId() {
26449
+ return "cap_" + crypto5.randomBytes(6).toString("base64url");
26450
+ }
26451
+ function sha256Hex2(s) {
26452
+ return crypto5.createHash("sha256").update(s).digest("hex");
26453
+ }
26454
+
26455
+ // src/permission/cleanup.ts
26456
+ var fs16 = __toESM(require("fs"), 1);
26457
+ function cleanupGuestSessionsForCapability(cap, factory) {
26458
+ const removed = [];
26459
+ for (const g2 of cap.grants) {
26460
+ if (g2.resource.type !== "persona") continue;
26461
+ const dir = factory.vmGuestRoot(g2.resource.id, cap.id);
26462
+ try {
26463
+ fs16.rmSync(dir, { recursive: true, force: true });
26464
+ removed.push(dir);
26465
+ } catch {
26466
+ }
26467
+ }
26468
+ return { removed };
26469
+ }
26470
+
26471
+ // src/inbox/inbox-store.ts
26472
+ var fs17 = __toESM(require("fs"), 1);
26473
+ var path19 = __toESM(require("path"), 1);
26474
+ var INBOX_SUBDIR = "inbox";
26475
+ var InboxStore = class {
26476
+ constructor(dataDir) {
26477
+ this.dataDir = dataDir;
26478
+ fs17.mkdirSync(this.dirPath(), { recursive: true });
26479
+ }
26480
+ dataDir;
25909
26481
  /**
25910
- * session 聚合查询(spec §4 HTTP ACL:personal 视野并集)。
25911
- *
25912
- * <dataDir>/sessions/<personaId>/owner/*.group-files.json 和
25913
- * <dataDir>/sessions/<personaId>/listener/*.group-files.json,每文件读一份。
26482
+ * 列出某 peer 的消息, createdAt 升序.
26483
+ * sinceCreatedAt 缺省时返回全量; 提供时仅返回 createdAt > sinceCreatedAt 的增量.
26484
+ * 文件不存在时返回 [] (不抛, 不创建).
26485
+ */
26486
+ list(peerOwnerId, sinceCreatedAt) {
26487
+ const file = this.filePath(peerOwnerId);
26488
+ let raw;
26489
+ try {
26490
+ raw = fs17.readFileSync(file, "utf8");
26491
+ } catch (err) {
26492
+ if (err?.code === "ENOENT") return [];
26493
+ return [];
26494
+ }
26495
+ const all = parseAllLines(raw);
26496
+ if (sinceCreatedAt === void 0) return all;
26497
+ return all.filter((m2) => m2.createdAt > sinceCreatedAt);
26498
+ }
26499
+ /**
26500
+ * 列出所有 peer 的 peerOwnerId (扫目录所有 *.jsonl 文件名).
26501
+ */
26502
+ listAllPeerOwnerIds() {
26503
+ const dir = this.dirPath();
26504
+ let entries;
26505
+ try {
26506
+ entries = fs17.readdirSync(dir);
26507
+ } catch (err) {
26508
+ if (err?.code === "ENOENT") return [];
26509
+ return [];
26510
+ }
26511
+ return entries.filter((name) => name.endsWith(".jsonl")).map((name) => name.slice(0, -".jsonl".length));
26512
+ }
26513
+ /**
26514
+ * 追加一条消息,按 message.id 幂等去重。
26515
+ * 双投 + 重试场景下保护 — 同 id 已存在则 no-op,不重写不覆盖。
26516
+ */
26517
+ append(message) {
26518
+ const existing = this.list(message.peerOwnerId);
26519
+ if (existing.some((m2) => m2.id === message.id)) return;
26520
+ const file = this.filePath(message.peerOwnerId);
26521
+ const line = JSON.stringify(message) + "\n";
26522
+ fs17.appendFileSync(file, line, { mode: 384 });
26523
+ try {
26524
+ fs17.chmodSync(file, 384);
26525
+ } catch {
26526
+ }
26527
+ }
26528
+ /**
26529
+ * 把该 peer 所有 createdAt<=upToCreatedAt 且 sender!=principalId 的消息
26530
+ * readBy[principalId] 写成 upToCreatedAt. O(N) rewrite 整个文件.
26531
+ * 返写入了几条.
26532
+ */
26533
+ markRead(peerOwnerId, principalId, upToCreatedAt) {
26534
+ const messages = this.list(peerOwnerId);
26535
+ let changed = 0;
26536
+ const next = messages.map((m2) => {
26537
+ if (m2.senderPrincipalId === principalId) return m2;
26538
+ if (m2.createdAt > upToCreatedAt) return m2;
26539
+ const cur = m2.readBy[principalId];
26540
+ if (cur !== void 0 && cur >= upToCreatedAt) return m2;
26541
+ changed++;
26542
+ return { ...m2, readBy: { ...m2.readBy, [principalId]: upToCreatedAt } };
26543
+ });
26544
+ if (changed === 0) return 0;
26545
+ this.rewriteFile(peerOwnerId, next);
26546
+ return changed;
26547
+ }
26548
+ /**
26549
+ * 删整个 peer 的 jsonl (peer:remove cascade). 不存在 no-op.
26550
+ */
26551
+ removeByPeerOwnerId(peerOwnerId) {
26552
+ const file = this.filePath(peerOwnerId);
26553
+ try {
26554
+ fs17.unlinkSync(file);
26555
+ } catch (err) {
26556
+ if (err?.code === "ENOENT") return;
26557
+ }
26558
+ }
26559
+ rewriteFile(peerOwnerId, messages) {
26560
+ const file = this.filePath(peerOwnerId);
26561
+ const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
26562
+ const content = messages.map((m2) => JSON.stringify(m2)).join("\n") + (messages.length > 0 ? "\n" : "");
26563
+ fs17.writeFileSync(tmp, content, { mode: 384 });
26564
+ fs17.renameSync(tmp, file);
26565
+ try {
26566
+ fs17.chmodSync(file, 384);
26567
+ } catch {
26568
+ }
26569
+ }
26570
+ dirPath() {
26571
+ return path19.join(this.dataDir, INBOX_SUBDIR);
26572
+ }
26573
+ filePath(peerOwnerId) {
26574
+ return path19.join(this.dirPath(), `${peerOwnerId}.jsonl`);
26575
+ }
26576
+ };
26577
+ function parseAllLines(raw) {
26578
+ const out = [];
26579
+ for (const line of raw.split("\n")) {
26580
+ const trimmed = line.trim();
26581
+ if (!trimmed) continue;
26582
+ let parsed;
26583
+ try {
26584
+ parsed = JSON.parse(trimmed);
26585
+ } catch {
26586
+ continue;
26587
+ }
26588
+ const r = InboxMessageSchema.safeParse(parsed);
26589
+ if (r.success) out.push(r.data);
26590
+ }
26591
+ return out;
26592
+ }
26593
+
26594
+ // src/inbox/inbox-manager.ts
26595
+ var InboxManager = class {
26596
+ constructor(store, broadcast) {
26597
+ this.store = store;
26598
+ this.broadcast = broadcast;
26599
+ }
26600
+ store;
26601
+ broadcast;
26602
+ /**
26603
+ * 写一条消息到 peer.jsonl + broadcast 给本机所有 owner ws。
26604
+ * 按 id 幂等:同 id 已落盘则 no-op(不广播、不重写)。
25914
26605
  *
25915
- * 复杂度 O(N sessions × N entries),N 通常 < 100,可接受。
26606
+ * caller 必须已通过 handler 鉴权
26607
+ * (owner ctx 必填 args.peerOwnerId;guest ctx 由 handler 反查 peer-store)。
25916
26608
  */
25917
- listByPersona(personaId) {
25918
- const out = [];
25919
- for (const mode of ["owner", "listener"]) {
25920
- const scope = { kind: "persona", personaId, mode };
25921
- const root = this.rootForScope(scope);
25922
- let names;
26609
+ postMessage(args) {
26610
+ const beforeLen = this.store.list(args.peerOwnerId).length;
26611
+ const msg = {
26612
+ id: args.id,
26613
+ peerOwnerId: args.peerOwnerId,
26614
+ senderPrincipalId: args.senderPrincipalId,
26615
+ text: args.text,
26616
+ createdAt: args.createdAt,
26617
+ readBy: {}
26618
+ };
26619
+ this.store.append(msg);
26620
+ const afterLen = this.store.list(args.peerOwnerId).length;
26621
+ if (afterLen === beforeLen) return msg;
26622
+ this.broadcast(args.peerOwnerId, {
26623
+ type: "inbox:event",
26624
+ peerOwnerId: args.peerOwnerId,
26625
+ message: msg
26626
+ });
26627
+ return msg;
26628
+ }
26629
+ list(peerOwnerId, sinceCreatedAt) {
26630
+ return this.store.list(peerOwnerId, sinceCreatedAt);
26631
+ }
26632
+ /**
26633
+ * 标已读. 返写入了几条 (sender !== self 且 createdAt <= upToCreatedAt 的消息).
26634
+ * broadcast 已更新 readBy 的每条消息 (单帧一消息, 复用 inbox:event 通道).
26635
+ */
26636
+ markRead(args) {
26637
+ const before = this.store.list(args.peerOwnerId);
26638
+ const changed = this.store.markRead(args.peerOwnerId, args.principalId, args.upToCreatedAt);
26639
+ if (changed === 0) return 0;
26640
+ const after = this.store.list(args.peerOwnerId);
26641
+ const beforeById = new Map(before.map((m2) => [m2.id, m2]));
26642
+ for (const m2 of after) {
26643
+ const prev = beforeById.get(m2.id);
26644
+ if (!prev) continue;
26645
+ if (prev.readBy[args.principalId] === m2.readBy[args.principalId]) continue;
26646
+ this.broadcast(args.peerOwnerId, {
26647
+ type: "inbox:event",
26648
+ peerOwnerId: args.peerOwnerId,
26649
+ message: m2
26650
+ });
26651
+ }
26652
+ return changed;
26653
+ }
26654
+ /** peer:remove cascade. 删整个 peer 的 jsonl. */
26655
+ removeChannel(peerOwnerId) {
26656
+ this.store.removeByPeerOwnerId(peerOwnerId);
26657
+ }
26658
+ };
26659
+
26660
+ // src/state/received-capability-store.ts
26661
+ var fs18 = __toESM(require("fs"), 1);
26662
+ var path20 = __toESM(require("path"), 1);
26663
+ var FILE_NAME = "received-capabilities.json";
26664
+ var ReceivedCapabilityStore = class {
26665
+ constructor(dataDir) {
26666
+ this.dataDir = dataDir;
26667
+ }
26668
+ dataDir;
26669
+ caps = /* @__PURE__ */ new Map();
26670
+ load() {
26671
+ this.caps.clear();
26672
+ const file = path20.join(this.dataDir, FILE_NAME);
26673
+ let raw;
26674
+ try {
26675
+ raw = fs18.readFileSync(file, "utf8");
26676
+ } catch (err) {
26677
+ if (err?.code !== "ENOENT") this.renameBak(file);
26678
+ return;
26679
+ }
26680
+ let parsed;
26681
+ try {
26682
+ parsed = JSON.parse(raw);
26683
+ } catch {
26684
+ this.renameBak(file);
26685
+ return;
26686
+ }
26687
+ const arr = parsed?.receivedCaps;
26688
+ if (!Array.isArray(arr)) return;
26689
+ for (const entry of arr) {
26690
+ const r = ReceivedCapabilitySchema.safeParse(entry);
26691
+ if (r.success) this.caps.set(r.data.ownerPrincipalId, r.data);
26692
+ }
26693
+ }
26694
+ list() {
26695
+ return Array.from(this.caps.values());
26696
+ }
26697
+ get(ownerPrincipalId) {
26698
+ return this.caps.get(ownerPrincipalId) ?? null;
26699
+ }
26700
+ hasOwner(ownerPrincipalId) {
26701
+ return this.caps.has(ownerPrincipalId);
26702
+ }
26703
+ upsert(cap) {
26704
+ this.caps.set(cap.ownerPrincipalId, cap);
26705
+ this.flush();
26706
+ }
26707
+ remove(ownerPrincipalId) {
26708
+ if (!this.caps.delete(ownerPrincipalId)) return;
26709
+ this.flush();
26710
+ }
26711
+ flush() {
26712
+ const file = path20.join(this.dataDir, FILE_NAME);
26713
+ const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
26714
+ const content = JSON.stringify(
26715
+ { receivedCaps: Array.from(this.caps.values()) },
26716
+ null,
26717
+ 2
26718
+ );
26719
+ fs18.mkdirSync(this.dataDir, { recursive: true });
26720
+ fs18.writeFileSync(tmp, content, { mode: 384 });
26721
+ fs18.renameSync(tmp, file);
26722
+ }
26723
+ renameBak(file) {
26724
+ try {
26725
+ fs18.renameSync(file, `${file}.bak`);
26726
+ } catch {
26727
+ }
26728
+ }
26729
+ };
26730
+
26731
+ // src/state/legacy-migration.ts
26732
+ var fs19 = __toESM(require("fs"), 1);
26733
+ var path21 = __toESM(require("path"), 1);
26734
+ var TARGETS = [
26735
+ { src: "capabilities.json", bak: "capabilities.legacy.bak" },
26736
+ { src: "remote-personas", bak: "remote-personas.legacy.bak" },
26737
+ { src: "peers.json", bak: "peers.legacy.bak" }
26738
+ ];
26739
+ function migrateLegacyFiles(dataDir) {
26740
+ for (const t of TARGETS) {
26741
+ const srcPath = path21.join(dataDir, t.src);
26742
+ const bakPath = path21.join(dataDir, t.bak);
26743
+ if (!fs19.existsSync(srcPath)) continue;
26744
+ if (fs19.existsSync(bakPath)) continue;
26745
+ try {
26746
+ fs19.renameSync(srcPath, bakPath);
26747
+ } catch {
26748
+ }
26749
+ }
26750
+ }
26751
+
26752
+ // src/received-capability/connect-remote.ts
26753
+ var crypto6 = __toESM(require("crypto"), 1);
26754
+ var HANDSHAKE_TIMEOUT_MS = 5e3;
26755
+ var RPC_TIMEOUT_MS = 5e3;
26756
+ async function connectRemote(args) {
26757
+ const ws = new wrapper_default(args.url);
26758
+ await new Promise((resolve2, reject) => {
26759
+ const onError = (e) => reject(e);
26760
+ ws.once("open", () => {
26761
+ ws.off("error", onError);
26762
+ resolve2();
26763
+ });
26764
+ ws.once("error", onError);
26765
+ });
26766
+ ws.send(
26767
+ JSON.stringify({
26768
+ type: "auth",
26769
+ token: args.token,
26770
+ selfPrincipalId: args.selfPrincipalId,
26771
+ selfDisplayName: args.selfDisplayName
26772
+ })
26773
+ );
26774
+ await new Promise((resolve2, reject) => {
26775
+ const onMessage = (raw) => {
26776
+ let f;
25923
26777
  try {
25924
- names = import_node_fs13.default.readdirSync(root);
25925
- } catch (err) {
25926
- const code = err?.code;
25927
- if (code === "ENOENT") continue;
25928
- continue;
26778
+ f = JSON.parse(raw.toString());
26779
+ } catch {
26780
+ return;
26781
+ }
26782
+ if (f.type === "auth:ok" || f.type === "ready") {
26783
+ ws.off("message", onMessage);
26784
+ resolve2();
26785
+ return;
25929
26786
  }
25930
- for (const name of names) {
25931
- if (!name.endsWith(".group-files.json")) continue;
25932
- const sessionId = name.slice(0, -".group-files.json".length);
25933
- if (!sessionId) continue;
25934
- const entries = this.list(scope, sessionId);
25935
- out.push({ sessionId, entries });
26787
+ if (f.type === "auth:error" || f.code === 4401) {
26788
+ ws.off("message", onMessage);
26789
+ reject(new Error(`auth failed: ${f.message ?? f.type ?? "unknown"}`));
26790
+ }
26791
+ };
26792
+ ws.on("message", onMessage);
26793
+ setTimeout(() => {
26794
+ ws.off("message", onMessage);
26795
+ reject(new Error("auth handshake timeout"));
26796
+ }, HANDSHAKE_TIMEOUT_MS);
26797
+ });
26798
+ function call(method, payload) {
26799
+ const requestId = crypto6.randomUUID();
26800
+ return new Promise((resolve2, reject) => {
26801
+ const onMessage = (raw) => {
26802
+ let f;
26803
+ try {
26804
+ f = JSON.parse(raw.toString());
26805
+ } catch {
26806
+ return;
26807
+ }
26808
+ if (f.requestId !== requestId) return;
26809
+ ws.off("message", onMessage);
26810
+ if (f.error || f.type?.endsWith(":error")) {
26811
+ reject(new Error(f.message ?? f.error ?? "rpc error"));
26812
+ } else {
26813
+ resolve2(f);
26814
+ }
26815
+ };
26816
+ ws.on("message", onMessage);
26817
+ ws.send(JSON.stringify({ type: method, requestId, ...payload }));
26818
+ setTimeout(() => {
26819
+ ws.off("message", onMessage);
26820
+ reject(new Error(`${method} timeout`));
26821
+ }, RPC_TIMEOUT_MS);
26822
+ });
26823
+ }
26824
+ return {
26825
+ whoami: async () => {
26826
+ const f = await call("whoami", {});
26827
+ return {
26828
+ ownerId: f.owner.id,
26829
+ displayName: f.owner.displayName,
26830
+ capabilityId: f.capability.id,
26831
+ grants: f.capability.grants
26832
+ };
26833
+ },
26834
+ close: () => {
26835
+ try {
26836
+ ws.close();
26837
+ } catch {
25936
26838
  }
25937
26839
  }
25938
- return out;
26840
+ };
26841
+ }
26842
+
26843
+ // src/migrations/2026-05-20-flatten-sessions.ts
26844
+ var fs20 = __toESM(require("fs"), 1);
26845
+ var path22 = __toESM(require("path"), 1);
26846
+ var MIGRATION_FLAG_NAME = ".migration.v1.done";
26847
+ function migrateFlattenSessions(opts) {
26848
+ const dataDir = opts.dataDir;
26849
+ const now = opts.now ?? Date.now;
26850
+ const sessionsDir = path22.join(dataDir, "sessions");
26851
+ const flagPath = path22.join(sessionsDir, MIGRATION_FLAG_NAME);
26852
+ if (existsSync4(flagPath)) {
26853
+ return { skipped: true, flagWritten: false, movedBare: 0, movedVmOwner: 0, archivedListener: 0 };
26854
+ }
26855
+ let movedBare = 0;
26856
+ let movedVmOwner = 0;
26857
+ let archivedListener = 0;
26858
+ const defaultDir = path22.join(sessionsDir, "default");
26859
+ if (existsSync4(defaultDir)) {
26860
+ for (const entry of readdirSafe(defaultDir)) {
26861
+ if (!entry.endsWith(".json")) continue;
26862
+ const src = path22.join(defaultDir, entry);
26863
+ const dst = path22.join(sessionsDir, entry);
26864
+ fs20.renameSync(src, dst);
26865
+ movedBare += 1;
26866
+ }
26867
+ rmdirIfEmpty(defaultDir);
26868
+ }
26869
+ for (const pid of readdirSafe(sessionsDir)) {
26870
+ const personaDir = path22.join(sessionsDir, pid);
26871
+ if (!isDir(personaDir)) continue;
26872
+ if (pid === "default") continue;
26873
+ const ownerSrc = path22.join(personaDir, "owner");
26874
+ if (existsSync4(ownerSrc) && isDir(ownerSrc)) {
26875
+ const ownerDst = path22.join(dataDir, "personas", pid, ".clawd", "sessions", "owner");
26876
+ fs20.mkdirSync(ownerDst, { recursive: true });
26877
+ for (const file of readdirSafe(ownerSrc)) {
26878
+ if (!file.endsWith(".json")) continue;
26879
+ fs20.renameSync(path22.join(ownerSrc, file), path22.join(ownerDst, file));
26880
+ movedVmOwner += 1;
26881
+ }
26882
+ rmdirIfEmpty(ownerSrc);
26883
+ }
26884
+ const listenerSrc = path22.join(personaDir, "listener");
26885
+ if (existsSync4(listenerSrc) && isDir(listenerSrc)) {
26886
+ const archiveDst = path22.join(dataDir, ".legacy", `listener-${pid}`);
26887
+ fs20.mkdirSync(archiveDst, { recursive: true });
26888
+ for (const file of readdirSafe(listenerSrc)) {
26889
+ if (!file.endsWith(".json")) continue;
26890
+ fs20.renameSync(path22.join(listenerSrc, file), path22.join(archiveDst, file));
26891
+ archivedListener += 1;
26892
+ }
26893
+ rmdirIfEmpty(listenerSrc);
26894
+ }
26895
+ rmdirIfEmpty(personaDir);
26896
+ }
26897
+ fs20.mkdirSync(sessionsDir, { recursive: true });
26898
+ fs20.writeFileSync(flagPath, JSON.stringify({ migratedAt: now() }, null, 2));
26899
+ return {
26900
+ skipped: false,
26901
+ flagWritten: true,
26902
+ movedBare,
26903
+ movedVmOwner,
26904
+ archivedListener
26905
+ };
26906
+ }
26907
+ function existsSync4(p2) {
26908
+ try {
26909
+ fs20.statSync(p2);
26910
+ return true;
26911
+ } catch {
26912
+ return false;
25939
26913
  }
25940
- };
25941
- function personalViewable(groupStore, personaDir, personaId, absPath) {
25942
- const realTarget = safeRealpath(absPath);
25943
- if (!realTarget) {
26914
+ }
26915
+ function isDir(p2) {
26916
+ try {
26917
+ return fs20.statSync(p2).isDirectory();
26918
+ } catch {
25944
26919
  return false;
25945
26920
  }
25946
- const personasUnion = groupStore.listByPersona(personaId);
25947
- for (const { entries } of personasUnion) {
25948
- for (const e of entries) {
25949
- if (e.stale) continue;
25950
- const realEntry = safeRealpath(import_node_path14.default.join(personaDir, e.relPath));
25951
- if (realEntry && realEntry === realTarget) return true;
25952
- }
26921
+ }
26922
+ function readdirSafe(p2) {
26923
+ try {
26924
+ return fs20.readdirSync(p2);
26925
+ } catch {
26926
+ return [];
25953
26927
  }
25954
- return false;
25955
26928
  }
25956
- function safeRealpath(p2) {
26929
+ function rmdirIfEmpty(p2) {
25957
26930
  try {
25958
- return import_node_fs13.default.realpathSync(p2);
26931
+ fs20.rmdirSync(p2);
25959
26932
  } catch {
25960
- return null;
25961
26933
  }
25962
26934
  }
25963
26935
 
25964
- // src/attachment/mime.ts
26936
+ // src/transport/http-router.ts
26937
+ var import_node_fs13 = __toESM(require("fs"), 1);
25965
26938
  var import_node_path15 = __toESM(require("path"), 1);
26939
+
26940
+ // src/attachment/mime.ts
26941
+ var import_node_path14 = __toESM(require("path"), 1);
25966
26942
  var TEXT_PLAIN = "text/plain; charset=utf-8";
25967
26943
  var EXT_TO_NATIVE_MIME = {
25968
26944
  // 图片
@@ -26069,14 +27045,14 @@ var TEXT_EXTENSIONS = /* @__PURE__ */ new Set([
26069
27045
  ".mk"
26070
27046
  ]);
26071
27047
  function lookupMime(filePathOrName) {
26072
- const ext = import_node_path15.default.extname(filePathOrName).toLowerCase();
27048
+ const ext = import_node_path14.default.extname(filePathOrName).toLowerCase();
26073
27049
  if (EXT_TO_NATIVE_MIME[ext]) return EXT_TO_NATIVE_MIME[ext];
26074
27050
  if (TEXT_EXTENSIONS.has(ext)) return TEXT_PLAIN;
26075
27051
  return "application/octet-stream";
26076
27052
  }
26077
27053
 
26078
27054
  // src/attachment/sign-url.ts
26079
- var import_node_crypto5 = __toESM(require("crypto"), 1);
27055
+ var import_node_crypto4 = __toESM(require("crypto"), 1);
26080
27056
  var HMAC_ALGO = "sha256";
26081
27057
  function base64urlEncode(buf) {
26082
27058
  const b2 = typeof buf === "string" ? Buffer.from(buf, "utf8") : buf;
@@ -26093,7 +27069,7 @@ function decodeAbsPathFromUrl(encoded) {
26093
27069
  }
26094
27070
  function computeSig(secret, absPath, e) {
26095
27071
  const msg = e === null ? absPath : `${absPath}|${e}`;
26096
- return import_node_crypto5.default.createHmac(HMAC_ALGO, secret).update(msg).digest();
27072
+ return import_node_crypto4.default.createHmac(HMAC_ALGO, secret).update(msg).digest();
26097
27073
  }
26098
27074
  function signUrlParts(secret, absPath, ttlSeconds, now = Date.now) {
26099
27075
  const e = ttlSeconds === null ? null : Math.floor(now() / 1e3) + ttlSeconds;
@@ -26128,7 +27104,7 @@ function verifySignedUrl(secret, absPath, eRaw, s, now = Date.now) {
26128
27104
  if (provided.length !== expected.length) {
26129
27105
  return { ok: false, code: "BAD_SIG" };
26130
27106
  }
26131
- if (!import_node_crypto5.default.timingSafeEqual(provided, expected)) {
27107
+ if (!import_node_crypto4.default.timingSafeEqual(provided, expected)) {
26132
27108
  return { ok: false, code: "BAD_SIG" };
26133
27109
  }
26134
27110
  if (e !== null && now() / 1e3 > e) {
@@ -26149,7 +27125,7 @@ function createHttpRouter(deps) {
26149
27125
  sendJson(res, 200, { ok: true, version: deps.daemonVersion });
26150
27126
  return true;
26151
27127
  }
26152
- if (!url.pathname.startsWith("/persona/") && !url.pathname.startsWith("/session/") && !url.pathname.startsWith("/files/")) {
27128
+ if (!url.pathname.startsWith("/session/") && !url.pathname.startsWith("/files/")) {
26153
27129
  return false;
26154
27130
  }
26155
27131
  if (url.pathname.startsWith("/files/") && req.method === "GET") {
@@ -26189,43 +27165,8 @@ function createHttpRouter(deps) {
26189
27165
  sendJson(res, 401, { code: "UNAUTHORIZED", message: "missing or invalid bearer token" });
26190
27166
  return true;
26191
27167
  }
26192
- const personaFilesMatch = url.pathname.match(/^\/persona\/([^/]+)\/files$/);
26193
- if (personaFilesMatch && req.method === "GET") {
26194
- const pid = personaFilesMatch[1];
26195
- const pathParam = url.searchParams.get("path");
26196
- if (!pathParam) {
26197
- sendJson(res, 400, { code: "INVALID_PARAM", message: "missing `path` query" });
26198
- return true;
26199
- }
26200
- if (!deps.personaStore || !deps.groupFileStore) {
26201
- sendJson(res, 501, withCtx(ctx, { code: "NOT_IMPLEMENTED", message: "files endpoint not wired" }));
26202
- return true;
26203
- }
26204
- const personaDir = deps.personaStore.personaDirPath(pid);
26205
- const absPath = import_node_path16.default.isAbsolute(pathParam) ? pathParam : import_node_path16.default.join(personaDir, pathParam);
26206
- if (!import_node_path16.default.isAbsolute(pathParam) && !isContainedIn(absPath, personaDir)) {
26207
- sendJson(res, 400, { code: "PATH_TRAVERSAL", message: "rel path escapes personaDir" });
26208
- return true;
26209
- }
26210
- if (ctx.role === "personal") {
26211
- if (ctx.personaId !== pid) {
26212
- sendJson(res, 403, { code: "FORBIDDEN", message: "personal token bound to other persona" });
26213
- return true;
26214
- }
26215
- if (!personalViewable(deps.groupFileStore, personaDir, pid, absPath)) {
26216
- sendJson(res, 403, { code: "FORBIDDEN", message: "path not in personal viewable scope" });
26217
- return true;
26218
- }
26219
- }
26220
- streamFile(res, absPath, deps.logger);
26221
- return true;
26222
- }
26223
27168
  const sessionFilesMatch = url.pathname.match(/^\/session\/([^/]+)\/files$/);
26224
27169
  if (sessionFilesMatch && req.method === "GET") {
26225
- if (ctx.role !== "owner") {
26226
- sendJson(res, 403, { code: "FORBIDDEN", message: "direct session files are owner-only" });
26227
- return true;
26228
- }
26229
27170
  const sid = sessionFilesMatch[1];
26230
27171
  const pathParam = url.searchParams.get("path");
26231
27172
  if (!pathParam) {
@@ -26233,7 +27174,7 @@ function createHttpRouter(deps) {
26233
27174
  return true;
26234
27175
  }
26235
27176
  let absPath;
26236
- if (import_node_path16.default.isAbsolute(pathParam)) {
27177
+ if (import_node_path15.default.isAbsolute(pathParam)) {
26237
27178
  absPath = pathParam;
26238
27179
  } else if (deps.sessionStore) {
26239
27180
  const file = deps.sessionStore.read(sid);
@@ -26241,7 +27182,7 @@ function createHttpRouter(deps) {
26241
27182
  sendJson(res, 404, { code: "NOT_FOUND", message: `session ${sid} not found` });
26242
27183
  return true;
26243
27184
  }
26244
- absPath = import_node_path16.default.join(file.cwd, pathParam);
27185
+ absPath = import_node_path15.default.join(file.cwd, pathParam);
26245
27186
  } else {
26246
27187
  sendJson(res, 501, withCtx(ctx, { code: "NOT_IMPLEMENTED", message: "sessionStore not wired" }));
26247
27188
  return true;
@@ -26266,18 +27207,12 @@ function sendJson(res, status, body) {
26266
27207
  res.end(JSON.stringify(body));
26267
27208
  }
26268
27209
  function withCtx(ctx, body) {
26269
- return { ...body, role: ctx.role, personaId: ctx.personaId };
26270
- }
26271
- function isContainedIn(abs, root) {
26272
- const normalized = import_node_path16.default.resolve(abs);
26273
- const normalizedRoot = import_node_path16.default.resolve(root);
26274
- if (normalized === normalizedRoot) return true;
26275
- return normalized.startsWith(normalizedRoot + import_node_path16.default.sep);
27210
+ return { ...body, role: ctx.role };
26276
27211
  }
26277
27212
  function streamFile(res, absPath, logger) {
26278
27213
  let stat;
26279
27214
  try {
26280
- stat = import_node_fs14.default.statSync(absPath);
27215
+ stat = import_node_fs13.default.statSync(absPath);
26281
27216
  } catch (err) {
26282
27217
  const code = err?.code;
26283
27218
  if (code === "ENOENT") {
@@ -26287,26 +27222,167 @@ function streamFile(res, absPath, logger) {
26287
27222
  }
26288
27223
  return;
26289
27224
  }
26290
- if (!stat.isFile()) {
26291
- sendJson(res, 400, { code: "NOT_A_FILE", message: "path is not a regular file" });
26292
- return;
27225
+ if (!stat.isFile()) {
27226
+ sendJson(res, 400, { code: "NOT_A_FILE", message: "path is not a regular file" });
27227
+ return;
27228
+ }
27229
+ const mime = lookupMime(absPath);
27230
+ const basename = import_node_path15.default.basename(absPath);
27231
+ res.writeHead(200, {
27232
+ "Content-Type": mime,
27233
+ "Content-Length": String(stat.size),
27234
+ "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(basename)}`,
27235
+ // 防止浏览器把任意 mime 当 html 渲染
27236
+ "X-Content-Type-Options": "nosniff"
27237
+ });
27238
+ const stream = import_node_fs13.default.createReadStream(absPath);
27239
+ stream.on("error", (err) => {
27240
+ logger?.warn("streamFile read error", { absPath, err: err.message });
27241
+ res.destroy();
27242
+ });
27243
+ stream.pipe(res);
27244
+ }
27245
+
27246
+ // src/attachment/group.ts
27247
+ var import_node_fs14 = __toESM(require("fs"), 1);
27248
+ var import_node_path16 = __toESM(require("path"), 1);
27249
+ var import_node_crypto5 = __toESM(require("crypto"), 1);
27250
+ init_protocol();
27251
+ var GroupFileStore = class {
27252
+ dataDir;
27253
+ logger;
27254
+ cache = /* @__PURE__ */ new Map();
27255
+ constructor(opts) {
27256
+ this.dataDir = opts.dataDir;
27257
+ this.logger = opts.logger;
27258
+ }
27259
+ rootForScope(scope) {
27260
+ return import_node_path16.default.join(this.dataDir, "sessions", ...scopeSubPath(scope).map(safeFileName));
27261
+ }
27262
+ /** 与 SessionStore.filePath 平级,扩展名 .group-files.json */
27263
+ filePath(scope, sessionId) {
27264
+ return import_node_path16.default.join(this.rootForScope(scope), `${safeFileName(sessionId)}.group-files.json`);
27265
+ }
27266
+ cacheKey(scope, sessionId) {
27267
+ return scope.kind === "default" ? `default::${sessionId}` : `persona:${scope.personaId}:${scope.mode}::${sessionId}`;
27268
+ }
27269
+ /** 从磁盘读一份;不存在 → 空数组;schema 不匹配的条目 → 跳过(防腐) */
27270
+ readFile(scope, sessionId) {
27271
+ const file = this.filePath(scope, sessionId);
27272
+ try {
27273
+ const raw = import_node_fs14.default.readFileSync(file, "utf8");
27274
+ const parsed = JSON.parse(raw);
27275
+ if (!Array.isArray(parsed)) {
27276
+ this.logger?.warn("GroupFileStore.readFile: not an array; resetting session entries", {
27277
+ file
27278
+ });
27279
+ return [];
27280
+ }
27281
+ const out = [];
27282
+ for (const entry of parsed) {
27283
+ const r = GroupFileEntrySchema.safeParse(entry);
27284
+ if (r.success) out.push(r.data);
27285
+ }
27286
+ return out;
27287
+ } catch (err) {
27288
+ const code = err?.code;
27289
+ if (code === "ENOENT") return [];
27290
+ this.logger?.warn("GroupFileStore.readFile failed", {
27291
+ file,
27292
+ err: err.message
27293
+ });
27294
+ return [];
27295
+ }
27296
+ }
27297
+ writeFile(scope, sessionId, entries) {
27298
+ const file = this.filePath(scope, sessionId);
27299
+ import_node_fs14.default.mkdirSync(import_node_path16.default.dirname(file), { recursive: true });
27300
+ const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
27301
+ import_node_fs14.default.writeFileSync(tmp, JSON.stringify(entries, null, 2), { mode: 384 });
27302
+ import_node_fs14.default.renameSync(tmp, file);
27303
+ }
27304
+ /** 拉一份当前 session 的清单。读盘 → cache;之后调用复用 cache */
27305
+ list(scope, sessionId) {
27306
+ const key = this.cacheKey(scope, sessionId);
27307
+ const cached = this.cache.get(key);
27308
+ if (cached) return cached.entries;
27309
+ const entries = this.readFile(scope, sessionId);
27310
+ this.cache.set(key, { entries });
27311
+ return entries;
27312
+ }
27313
+ /**
27314
+ * upsert:
27315
+ * - 同 relPath 已存在 → 更新 lastEditedAt + clear stale;不动 from / addedAt / id
27316
+ * (保留首次入群人 / 入群时刻)
27317
+ * - 不存在 → append,id 派生稳定 uuid,addedAt = now
27318
+ *
27319
+ * 返回最新 entry(caller 可用来 broadcast 通知)。
27320
+ */
27321
+ upsert(scope, sessionId, input, now = Date.now()) {
27322
+ const entries = this.list(scope, sessionId).slice();
27323
+ const idx = entries.findIndex((e) => e.relPath === input.relPath);
27324
+ let next;
27325
+ if (idx >= 0) {
27326
+ const prev = entries[idx];
27327
+ next = {
27328
+ ...prev,
27329
+ size: input.size,
27330
+ mime: input.mime,
27331
+ lastEditedAt: now,
27332
+ stale: false
27333
+ // label 不在 upsert 路径覆盖(owner +Add 走另一条路径)
27334
+ };
27335
+ entries[idx] = next;
27336
+ } else {
27337
+ next = {
27338
+ id: `gf-${import_node_crypto5.default.randomBytes(6).toString("base64url")}`,
27339
+ relPath: input.relPath,
27340
+ from: input.from,
27341
+ label: input.label,
27342
+ size: input.size,
27343
+ mime: input.mime,
27344
+ addedAt: now
27345
+ // agent 第一次 upsert 时不写 lastEditedAt(语义上 = 首次"编辑" = addedAt)
27346
+ };
27347
+ entries.push(next);
27348
+ }
27349
+ this.writeFile(scope, sessionId, entries);
27350
+ this.cache.set(this.cacheKey(scope, sessionId), { entries });
27351
+ return next;
27352
+ }
27353
+ /**
27354
+ * 标记一个 relPath stale(agent rm / mv 后文件不在)。
27355
+ * - 命中 → stale=true,UI 灰显
27356
+ * - 未命中 → noop(不需要为不在群里的文件创建 stale 条目)
27357
+ *
27358
+ * 注:spec §6 "Bash 命令是 rm 形态"启发式不强求 — runner 暂不调,留给后续优化
27359
+ */
27360
+ markStale(scope, sessionId, relPath) {
27361
+ const entries = this.list(scope, sessionId).slice();
27362
+ const idx = entries.findIndex((e) => e.relPath === relPath);
27363
+ if (idx < 0) return;
27364
+ if (entries[idx].stale) return;
27365
+ entries[idx] = { ...entries[idx], stale: true };
27366
+ this.writeFile(scope, sessionId, entries);
27367
+ this.cache.set(this.cacheKey(scope, sessionId), { entries });
27368
+ }
27369
+ /**
27370
+ * 真删一条群文件条目(用于 owner 撤销自己 +Add 的入群操作)。
27371
+ * agent 自动入群的不应走这条 —— 用 markStale 表达"文件不在了"语义;
27372
+ * owner 手动加错了,应该能彻底从列表移除而不是留个 stale 占位。
27373
+ *
27374
+ * 返回值:true=命中并删除;false=relPath 不在群里。
27375
+ */
27376
+ remove(scope, sessionId, relPath) {
27377
+ const entries = this.list(scope, sessionId).slice();
27378
+ const idx = entries.findIndex((e) => e.relPath === relPath);
27379
+ if (idx < 0) return false;
27380
+ entries.splice(idx, 1);
27381
+ this.writeFile(scope, sessionId, entries);
27382
+ this.cache.set(this.cacheKey(scope, sessionId), { entries });
27383
+ return true;
26293
27384
  }
26294
- const mime = lookupMime(absPath);
26295
- const basename = import_node_path16.default.basename(absPath);
26296
- res.writeHead(200, {
26297
- "Content-Type": mime,
26298
- "Content-Length": String(stat.size),
26299
- "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(basename)}`,
26300
- // 防止浏览器把任意 mime 当 html 渲染
26301
- "X-Content-Type-Options": "nosniff"
26302
- });
26303
- const stream = import_node_fs14.default.createReadStream(absPath);
26304
- stream.on("error", (err) => {
26305
- logger?.warn("streamFile read error", { absPath, err: err.message });
26306
- res.destroy();
26307
- });
26308
- stream.pipe(res);
26309
- }
27385
+ };
26310
27386
 
26311
27387
  // src/discovery/state-file.ts
26312
27388
  var import_node_fs15 = __toESM(require("fs"), 1);
@@ -26986,26 +28062,32 @@ function authFilePath(dataDir) {
26986
28062
  }
26987
28063
  function loadOrCreateAuthFile(opts) {
26988
28064
  const file = authFilePath(opts.dataDir);
26989
- const generate = opts.generate ?? defaultGenerate;
28065
+ const generate = opts.generate ?? defaultGenerateToken2;
28066
+ const genId = opts.genOwnerPrincipalId ?? defaultGenerateOwnerPrincipalId;
26990
28067
  const now = opts.now ?? (() => /* @__PURE__ */ new Date());
26991
28068
  const existing = readAuthFile(file);
26992
- if (existing && existing.token && existing.signSecret) {
28069
+ if (existing && existing.token && existing.signSecret && existing.ownerPrincipalId) {
26993
28070
  return {
26994
28071
  token: existing.token,
26995
28072
  signSecret: existing.signSecret,
28073
+ ownerPrincipalId: existing.ownerPrincipalId,
26996
28074
  createdAt: existing.createdAt ?? (/* @__PURE__ */ new Date(0)).toISOString()
26997
28075
  };
26998
28076
  }
26999
28077
  const token = existing?.token || generate();
27000
28078
  const signSecret = existing?.signSecret || generate();
28079
+ const ownerPrincipalId = existing?.ownerPrincipalId || genId();
27001
28080
  const createdAt = existing?.createdAt || now().toISOString();
27002
- const next = { token, signSecret, createdAt };
28081
+ const next = { token, signSecret, ownerPrincipalId, createdAt };
27003
28082
  writeAuthFile(file, next);
27004
28083
  return next;
27005
28084
  }
27006
- function defaultGenerate() {
28085
+ function defaultGenerateToken2() {
27007
28086
  return import_node_crypto8.default.randomBytes(32).toString("base64url");
27008
28087
  }
28088
+ function defaultGenerateOwnerPrincipalId() {
28089
+ return `owner-${import_node_crypto8.default.randomUUID()}`;
28090
+ }
27009
28091
  function readAuthFile(file) {
27010
28092
  try {
27011
28093
  const raw = import_node_fs20.default.readFileSync(file, "utf8");
@@ -27016,6 +28098,7 @@ function readAuthFile(file) {
27016
28098
  return {
27017
28099
  token: parsed.token,
27018
28100
  signSecret: typeof parsed.signSecret === "string" && parsed.signSecret.length > 0 ? parsed.signSecret : void 0,
28101
+ ownerPrincipalId: typeof parsed.ownerPrincipalId === "string" && parsed.ownerPrincipalId.length > 0 ? parsed.ownerPrincipalId : void 0,
27019
28102
  createdAt: typeof parsed.createdAt === "string" ? parsed.createdAt : void 0
27020
28103
  };
27021
28104
  } catch (err) {
@@ -27129,10 +28212,63 @@ function forkSession(input) {
27129
28212
  return { forkedToolSessionId, forkedFilePath };
27130
28213
  }
27131
28214
 
28215
+ // src/permission/capability.ts
28216
+ function matchResource(grant, target) {
28217
+ if (grant.type === "*") return true;
28218
+ if (grant.type !== target.type) return false;
28219
+ if (grant.id === "*") return true;
28220
+ return grant.id === target.id;
28221
+ }
28222
+ function assertGrant(grants, resource, action) {
28223
+ for (const g2 of grants) {
28224
+ if (!matchResource(g2.resource, resource)) continue;
28225
+ if (g2.actions.includes(action)) return true;
28226
+ if (g2.actions.includes("admin")) return true;
28227
+ }
28228
+ return false;
28229
+ }
28230
+
28231
+ // src/permission/session-access.ts
28232
+ function canAccessSession(ctx, sessionId, action, deps) {
28233
+ if (ctx.principal.kind === "owner") return true;
28234
+ const file = deps.readSession(sessionId);
28235
+ if (!file) return true;
28236
+ return canAccessPersona(ctx.grants, file.ownerPersonaId, action);
28237
+ }
28238
+ function canAccessPersona(grants, personaId, action) {
28239
+ if (!personaId) return false;
28240
+ const resource = { type: "persona", id: personaId };
28241
+ return assertGrant(grants, resource, action);
28242
+ }
28243
+
27132
28244
  // src/handlers/session.ts
28245
+ init_protocol();
27133
28246
  function buildSessionHandlers(deps) {
27134
- const { manager, observer, getAdapter: getAdapter2 } = deps;
27135
- const create = async (frame) => {
28247
+ const { manager, observer, getAdapter: getAdapter2, store, ownerPrincipalId } = deps;
28248
+ const ensureSessionAccess = (ctx, sessionId, action) => {
28249
+ if (!ctx) return;
28250
+ const ok = canAccessSession(ctx, sessionId, action, {
28251
+ readSession: (sid) => store.read(sid)
28252
+ });
28253
+ if (!ok) {
28254
+ throw new ClawdError(
28255
+ ERROR_CODES.UNAUTHORIZED,
28256
+ `principal ${ctx.principal.kind}:${ctx.principal.id} cannot ${action} session ${sessionId}`
28257
+ );
28258
+ }
28259
+ };
28260
+ const ensurePersonaAccess = (ctx, personaId, action) => {
28261
+ if (!ctx) return;
28262
+ if (ctx.principal.kind === "owner") return;
28263
+ const ok = canAccessPersona(ctx.grants, personaId, action);
28264
+ if (!ok) {
28265
+ throw new ClawdError(
28266
+ ERROR_CODES.UNAUTHORIZED,
28267
+ `principal ${ctx.principal.kind}:${ctx.principal.id} cannot ${action} on persona:${personaId ?? "<none>"}`
28268
+ );
28269
+ }
28270
+ };
28271
+ const create = async (frame, _client, ctx) => {
27136
28272
  const args = SessionCreateArgs.parse(frame);
27137
28273
  if (args.ownerPersonaId) {
27138
28274
  const persona = deps.personaRegistry.get(args.ownerPersonaId);
@@ -27140,58 +28276,75 @@ function buildSessionHandlers(deps) {
27140
28276
  throw new Error(`persona not found: ${args.ownerPersonaId}`);
27141
28277
  }
27142
28278
  }
27143
- const { response, broadcast } = manager.create(args);
28279
+ ensurePersonaAccess(ctx, args.ownerPersonaId, "send");
28280
+ const creatorPrincipalId = ctx?.principal.id ?? ownerPrincipalId;
28281
+ const { response, broadcast } = manager.create(args, creatorPrincipalId);
27144
28282
  return { response: { type: "session:info", ...response }, broadcast };
27145
28283
  };
27146
- const list = async () => {
28284
+ const list = async (_frame, _client, ctx) => {
27147
28285
  const { response } = manager.list();
28286
+ if (ctx && ctx.principal.kind === "guest") {
28287
+ const sessions = response.sessions ?? [];
28288
+ const capId = ctx.capabilityId;
28289
+ const filtered = sessions.filter((s) => canAccessPersona(ctx.grants, s.ownerPersonaId, "read")).filter((s) => s.creatorPrincipalId === capId);
28290
+ return { response: { type: "session:list", sessions: filtered } };
28291
+ }
27148
28292
  return { response: { type: "session:list", ...response } };
27149
28293
  };
27150
- const get = async (frame) => {
28294
+ const get = async (frame, _client, ctx) => {
27151
28295
  const args = SessionIdArgs.parse(frame);
28296
+ ensureSessionAccess(ctx, args.sessionId, "read");
27152
28297
  const { response } = manager.get(args);
27153
28298
  return { response: { type: "session:info", ...response } };
27154
28299
  };
27155
- const update = async (frame) => {
28300
+ const update = async (frame, _client, ctx) => {
27156
28301
  const args = SessionUpdateArgs.parse(frame);
28302
+ ensureSessionAccess(ctx, args.sessionId, "send");
27157
28303
  const { response, broadcast } = manager.update(args);
27158
28304
  return { response: { type: "session:info", ...response }, broadcast };
27159
28305
  };
27160
- const del = async (frame) => {
28306
+ const del = async (frame, _client, ctx) => {
27161
28307
  const args = SessionIdArgs.parse(frame);
28308
+ ensureSessionAccess(ctx, args.sessionId, "send");
27162
28309
  const { broadcast } = manager.delete(args);
27163
28310
  return {
27164
28311
  response: { type: "session:deleted", sessionId: args.sessionId },
27165
28312
  broadcast
27166
28313
  };
27167
28314
  };
27168
- const send = async (frame) => {
28315
+ const send = async (frame, _client, ctx) => {
27169
28316
  const args = SessionSendArgs.parse(frame);
28317
+ ensureSessionAccess(ctx, args.sessionId, "send");
27170
28318
  const { broadcast } = manager.send(args);
27171
28319
  return { response: { type: "session:send", ok: true }, broadcast };
27172
28320
  };
27173
- const stop = async (frame) => {
28321
+ const stop = async (frame, _client, ctx) => {
27174
28322
  const args = SessionIdArgs.parse(frame);
28323
+ ensureSessionAccess(ctx, args.sessionId, "send");
27175
28324
  const { broadcast } = await manager.stop(args);
27176
28325
  return { response: { type: "session:stop", ok: true }, broadcast };
27177
28326
  };
27178
- const interrupt = async (frame) => {
28327
+ const interrupt = async (frame, _client, ctx) => {
27179
28328
  const args = SessionIdArgs.parse(frame);
28329
+ ensureSessionAccess(ctx, args.sessionId, "send");
27180
28330
  const { broadcast } = await manager.interrupt(args);
27181
28331
  return { response: { type: "session:interrupt", ok: true }, broadcast };
27182
28332
  };
27183
- const rewind = async (frame) => {
28333
+ const rewind = async (frame, _client, ctx) => {
27184
28334
  const args = SessionRewindArgs.parse(frame);
28335
+ ensureSessionAccess(ctx, args.sessionId, "send");
27185
28336
  const { response, broadcast } = await manager.rewind(args);
27186
28337
  return { response: { type: "session:rewind", ...response }, broadcast };
27187
28338
  };
27188
- const rewindDiff = async (frame) => {
28339
+ const rewindDiff = async (frame, _client, ctx) => {
27189
28340
  const args = SessionRewindDiffArgs.parse(frame);
28341
+ ensureSessionAccess(ctx, args.sessionId, "read");
27190
28342
  const { response } = await manager.rewindDiff(args);
27191
28343
  return { response: { type: "session:rewind-diff", ...response } };
27192
28344
  };
27193
- const rewindableMessageIds = async (frame) => {
28345
+ const rewindableMessageIds = async (frame, _client, ctx) => {
27194
28346
  const args = SessionRewindableMessageIdsArgs.parse(frame);
28347
+ ensureSessionAccess(ctx, args.sessionId, "read");
27195
28348
  const { response } = manager.rewindableMessageIds(args);
27196
28349
  return {
27197
28350
  response: { type: "session:rewindable-message-ids", ...response }
@@ -27202,19 +28355,22 @@ function buildSessionHandlers(deps) {
27202
28355
  const result = forkSession(args);
27203
28356
  return { response: { type: "session:fork", ...result } };
27204
28357
  };
27205
- const newSession = async (frame) => {
28358
+ const newSession = async (frame, _client, ctx) => {
27206
28359
  const args = SessionIdArgs.parse(frame);
28360
+ ensureSessionAccess(ctx, args.sessionId, "send");
27207
28361
  observer.stop(args.sessionId);
27208
28362
  const { response, broadcast } = manager.newSession(args);
27209
28363
  return { response: { type: "session:info", ...response }, broadcast };
27210
28364
  };
27211
- const resume = async (frame) => {
28365
+ const resume = async (frame, _client, ctx) => {
27212
28366
  const args = SessionResumeArgs.parse(frame);
28367
+ ensureSessionAccess(ctx, args.sessionId, "send");
27213
28368
  const { response, broadcast } = manager.resume(args);
27214
28369
  return { response: { type: "session:info", ...response }, broadcast };
27215
28370
  };
27216
- const observe = async (frame) => {
28371
+ const observe = async (frame, _client, ctx) => {
27217
28372
  const args = SessionObserveArgs.parse(frame);
28373
+ ensureSessionAccess(ctx, args.sessionId, "read");
27218
28374
  const { response: file } = manager.get({ sessionId: args.sessionId });
27219
28375
  const sessionFile = file;
27220
28376
  manager.ensureSession(sessionFile);
@@ -27228,14 +28384,17 @@ function buildSessionHandlers(deps) {
27228
28384
  });
27229
28385
  return { response: { type: "session:observe", ok: true } };
27230
28386
  };
27231
- const events = async (frame) => {
28387
+ const events = async (frame, _client, ctx) => {
27232
28388
  const args = SessionEventsArgs.parse(frame);
28389
+ ensureSessionAccess(ctx, args.sessionId, "read");
27233
28390
  const { response } = manager.getEvents(args);
27234
28391
  return { response: { type: "session:events", ...response } };
27235
28392
  };
27236
- const subscribe = async (frame, client) => {
27237
- if (typeof frame.sessionId === "string")
28393
+ const subscribe = async (frame, client, ctx) => {
28394
+ if (typeof frame.sessionId === "string") {
28395
+ ensureSessionAccess(ctx, frame.sessionId, "read");
27238
28396
  addSubscription(client, frame.sessionId);
28397
+ }
27239
28398
  return {
27240
28399
  response: { type: "subscribed", sessionId: frame.sessionId }
27241
28400
  };
@@ -27247,8 +28406,9 @@ function buildSessionHandlers(deps) {
27247
28406
  response: { type: "unsubscribed", sessionId: frame.sessionId }
27248
28407
  };
27249
28408
  };
27250
- const pin = async (frame) => {
28409
+ const pin = async (frame, _client, ctx) => {
27251
28410
  const args = SessionPinArgs.parse(frame);
28411
+ ensureSessionAccess(ctx, args.sessionId, "send");
27252
28412
  const { response, broadcast } = manager.pin(args);
27253
28413
  return { response: { type: "session:info", ...response }, broadcast };
27254
28414
  };
@@ -27257,13 +28417,15 @@ function buildSessionHandlers(deps) {
27257
28417
  const { response, broadcast } = manager.reorderPins(args);
27258
28418
  return { response: { type: "session:reorderPins", ...response }, broadcast };
27259
28419
  };
27260
- const answerQuestion = async (frame) => {
28420
+ const answerQuestion = async (frame, _client, ctx) => {
27261
28421
  const args = AnswerQuestionArgs.parse(frame);
28422
+ ensureSessionAccess(ctx, args.sessionId, "send");
27262
28423
  const { response, broadcast } = manager.answerQuestion(args);
27263
28424
  return { response: { type: "session:answerQuestion", ...response }, broadcast };
27264
28425
  };
27265
- const cancelQuestion = async (frame) => {
28426
+ const cancelQuestion = async (frame, _client, ctx) => {
27266
28427
  const args = CancelQuestionArgs.parse(frame);
28428
+ ensureSessionAccess(ctx, args.sessionId, "send");
27267
28429
  const { response, broadcast } = await manager.cancelQuestion(args);
27268
28430
  return { response: { type: "session:cancelQuestion", ...response }, broadcast };
27269
28431
  };
@@ -27358,6 +28520,7 @@ function listRecentDirs(store, limit = 50) {
27358
28520
  }
27359
28521
 
27360
28522
  // src/handlers/history.ts
28523
+ init_protocol();
27361
28524
  function buildHistoryHandlers(deps) {
27362
28525
  const { manager, history, store } = deps;
27363
28526
  const projects = async () => {
@@ -27369,17 +28532,27 @@ function buildHistoryHandlers(deps) {
27369
28532
  const sessions = await history.listSessions(args);
27370
28533
  return { response: { type: "history:list", sessions } };
27371
28534
  };
27372
- const read = async (frame) => {
28535
+ const read = async (frame, _client, ctx) => {
27373
28536
  const args = HistoryReadArgs.parse(frame);
27374
28537
  const { response: file } = manager.get({ sessionId: args.sessionId });
27375
- if (!file.toolSessionId) {
28538
+ const f = file;
28539
+ if (ctx && ctx.principal.kind === "guest") {
28540
+ const ok = canAccessPersona(ctx.grants, f.ownerPersonaId, "read") && f.creatorPrincipalId === ctx.capabilityId;
28541
+ if (!ok) {
28542
+ throw new ClawdError(
28543
+ ERROR_CODES.UNAUTHORIZED,
28544
+ `principal guest:${ctx.principal.id} cannot read history of session ${args.sessionId}`
28545
+ );
28546
+ }
28547
+ }
28548
+ if (!f.toolSessionId) {
27376
28549
  return {
27377
28550
  response: { type: "history:read", messages: [], total: 0, offset: 0 }
27378
28551
  };
27379
28552
  }
27380
28553
  const res = await history.read({
27381
- cwd: file.cwd,
27382
- toolSessionId: file.toolSessionId,
28554
+ cwd: f.cwd,
28555
+ toolSessionId: f.toolSessionId,
27383
28556
  limit: args.limit,
27384
28557
  offset: args.offset
27385
28558
  });
@@ -27561,6 +28734,345 @@ function buildCapabilitiesHandlers(deps) {
27561
28734
  };
27562
28735
  }
27563
28736
 
28737
+ // src/handlers/capability.ts
28738
+ init_zod();
28739
+ init_protocol();
28740
+ var DeleteArgsSchema = external_exports.object({
28741
+ capabilityId: external_exports.string().min(1)
28742
+ }).strict();
28743
+ function buildCapabilityHandlers(deps) {
28744
+ const { manager, getShareBaseUrl } = deps;
28745
+ const issue = async (frame) => {
28746
+ const { type: _type, requestId: _requestId, ...rest } = frame;
28747
+ const args = CapabilityIssueArgsSchema.parse(rest);
28748
+ const { token, capability } = manager.issue({
28749
+ displayName: args.displayName,
28750
+ grants: args.grants,
28751
+ ...args.expiresAt !== void 0 ? { expiresAt: args.expiresAt } : {},
28752
+ ...args.maxUses !== void 0 ? { maxUses: args.maxUses } : {}
28753
+ });
28754
+ const base = getShareBaseUrl().replace(/\/$/, "");
28755
+ const shareUrl = `${base}/?token=${encodeURIComponent(token)}`;
28756
+ return {
28757
+ response: {
28758
+ type: "capability:issued",
28759
+ token,
28760
+ shareUrl,
28761
+ capability: stripSecretHash(capability)
28762
+ }
28763
+ };
28764
+ };
28765
+ const list = async () => {
28766
+ return {
28767
+ response: {
28768
+ type: "capability:list",
28769
+ capabilities: manager.list().map(stripSecretHash)
28770
+ }
28771
+ };
28772
+ };
28773
+ const del = async (frame) => {
28774
+ const { type: _type, requestId: _requestId, ...rest } = frame;
28775
+ const args = DeleteArgsSchema.parse(rest);
28776
+ const result = manager.delete(args.capabilityId);
28777
+ if (!result) {
28778
+ throw new ClawdError(
28779
+ ERROR_CODES.VALIDATION_ERROR,
28780
+ `capability not found: ${args.capabilityId}`
28781
+ );
28782
+ }
28783
+ return {
28784
+ response: {
28785
+ type: "capability:deleted",
28786
+ capabilityId: args.capabilityId
28787
+ }
28788
+ };
28789
+ };
28790
+ const bindPeer = async (frame) => {
28791
+ const { type: _type, requestId: _requestId, ...rest } = frame;
28792
+ const args = CapabilityBindPeerArgsSchema.parse(rest);
28793
+ try {
28794
+ manager.recordPeerHello(args.capabilityId, {
28795
+ ownerPrincipalId: args.peerOwnerId,
28796
+ displayName: args.peerDisplayName,
28797
+ now: Date.now()
28798
+ });
28799
+ } catch (e) {
28800
+ throw new ClawdError(
28801
+ ERROR_CODES.VALIDATION_ERROR,
28802
+ `capability:bindPeer: ${e.message}`
28803
+ );
28804
+ }
28805
+ return {
28806
+ response: { type: "capability:bindPeer:ok", capabilityId: args.capabilityId }
28807
+ };
28808
+ };
28809
+ return {
28810
+ "capability:issue": issue,
28811
+ "capability:list": list,
28812
+ "capability:delete": del,
28813
+ "capability:bindPeer": bindPeer
28814
+ };
28815
+ }
28816
+
28817
+ // src/handlers/inbox.ts
28818
+ init_protocol();
28819
+ function resolvePeerOwnerId(ctx, argsPeerOwnerId, capManager) {
28820
+ if (ctx.principal.kind === "owner") {
28821
+ if (!argsPeerOwnerId) {
28822
+ throw new ClawdError(
28823
+ ERROR_CODES.VALIDATION_ERROR,
28824
+ "inbox: owner ctx requires peerOwnerId"
28825
+ );
28826
+ }
28827
+ return argsPeerOwnerId;
28828
+ }
28829
+ if (!ctx.capabilityId) {
28830
+ throw new ClawdError(ERROR_CODES.UNAUTHORIZED, "inbox: guest ctx missing capabilityId");
28831
+ }
28832
+ const cap = capManager.findById(ctx.capabilityId);
28833
+ if (!cap?.peerOwnerId) {
28834
+ throw new ClawdError(
28835
+ ERROR_CODES.UNAUTHORIZED,
28836
+ `inbox: guest cap ${ctx.capabilityId} has no peerOwnerId (recordPeerHello not yet)`
28837
+ );
28838
+ }
28839
+ if (argsPeerOwnerId && argsPeerOwnerId !== cap.peerOwnerId) {
28840
+ throw new ClawdError(
28841
+ ERROR_CODES.UNAUTHORIZED,
28842
+ `inbox: guest args.peerOwnerId mismatch (cap.peerOwnerId=${cap.peerOwnerId}, args=${argsPeerOwnerId})`
28843
+ );
28844
+ }
28845
+ return cap.peerOwnerId;
28846
+ }
28847
+ function buildInboxHandlers(deps) {
28848
+ const { manager, capManager } = deps;
28849
+ const postMessage = async (frame, _client, ctx) => {
28850
+ const { type: _t, requestId: _r, ...rest } = frame;
28851
+ const args = InboxPostMessageArgsSchema.parse(rest);
28852
+ if (!ctx) {
28853
+ throw new ClawdError(ERROR_CODES.INTERNAL, "inbox:postMessage: missing ConnectionContext");
28854
+ }
28855
+ const peerOwnerId = resolvePeerOwnerId(ctx, args.peerOwnerId, capManager);
28856
+ const message = manager.postMessage({
28857
+ peerOwnerId,
28858
+ senderPrincipalId: ctx.principal.id,
28859
+ text: args.text,
28860
+ id: args.id,
28861
+ createdAt: args.createdAt
28862
+ });
28863
+ return {
28864
+ response: { type: "inbox:postMessage:ok", message }
28865
+ };
28866
+ };
28867
+ const list = async (frame, _client, ctx) => {
28868
+ const { type: _t, requestId: _r, ...rest } = frame;
28869
+ const args = InboxListArgsSchema.parse(rest);
28870
+ if (!ctx) {
28871
+ throw new ClawdError(ERROR_CODES.INTERNAL, "inbox:list: missing ConnectionContext");
28872
+ }
28873
+ const peerOwnerId = resolvePeerOwnerId(ctx, args.peerOwnerId, capManager);
28874
+ const messages = manager.list(peerOwnerId, args.sinceCreatedAt);
28875
+ return {
28876
+ response: { type: "inbox:list:ok", peerOwnerId, messages }
28877
+ };
28878
+ };
28879
+ const markRead = async (frame, _client, ctx) => {
28880
+ const { type: _t, requestId: _r, ...rest } = frame;
28881
+ const args = InboxMarkReadArgsSchema.parse(rest);
28882
+ if (!ctx) {
28883
+ throw new ClawdError(ERROR_CODES.INTERNAL, "inbox:markRead: missing ConnectionContext");
28884
+ }
28885
+ const peerOwnerId = resolvePeerOwnerId(ctx, args.peerOwnerId, capManager);
28886
+ const updated = manager.markRead({
28887
+ peerOwnerId,
28888
+ principalId: ctx.principal.id,
28889
+ upToCreatedAt: args.upToCreatedAt
28890
+ });
28891
+ return {
28892
+ response: {
28893
+ type: "inbox:markRead:ok",
28894
+ peerOwnerId,
28895
+ upToCreatedAt: args.upToCreatedAt,
28896
+ updated
28897
+ }
28898
+ };
28899
+ };
28900
+ return {
28901
+ "inbox:postMessage": postMessage,
28902
+ "inbox:list": list,
28903
+ "inbox:markRead": markRead
28904
+ };
28905
+ }
28906
+
28907
+ // src/handlers/received-capability.ts
28908
+ init_protocol();
28909
+ function ensureOwner(ctx) {
28910
+ if (!ctx || ctx.principal.kind !== "owner") {
28911
+ throw new ClawdError(
28912
+ ERROR_CODES.UNAUTHORIZED,
28913
+ "UNAUTHORIZED: received-capability:* requires owner ctx"
28914
+ );
28915
+ }
28916
+ }
28917
+ function ensureGuestCtx(ctx) {
28918
+ if (!ctx || ctx.principal.kind !== "guest" || !ctx.capabilityId) {
28919
+ throw new ClawdError(
28920
+ ERROR_CODES.UNAUTHORIZED,
28921
+ "UNAUTHORIZED: received-capability:autoAttach requires guest cap ctx"
28922
+ );
28923
+ }
28924
+ }
28925
+ function buildReceivedCapabilityHandlers(deps) {
28926
+ const now = deps.now ?? Date.now;
28927
+ const list = async (_frame, _client, ctx) => {
28928
+ ensureOwner(ctx);
28929
+ return {
28930
+ response: {
28931
+ type: "received-capability:list:ok",
28932
+ receivedCaps: deps.store.list()
28933
+ }
28934
+ };
28935
+ };
28936
+ const add = async (frame, _client, ctx) => {
28937
+ ensureOwner(ctx);
28938
+ const { type: _t, requestId: _r, ...rest } = frame;
28939
+ const args = ReceivedCapabilityAddArgsSchema.parse(rest);
28940
+ let conn;
28941
+ try {
28942
+ conn = await deps.connectRemote({
28943
+ url: args.remoteUrl,
28944
+ token: args.token,
28945
+ selfPrincipalId: deps.selfPrincipalId(),
28946
+ selfDisplayName: deps.selfDisplayName()
28947
+ });
28948
+ } catch (e) {
28949
+ throw new ClawdError(
28950
+ ERROR_CODES.VALIDATION_ERROR,
28951
+ `INVITE_REMOTE_UNREACHABLE: ${e.message}`
28952
+ );
28953
+ }
28954
+ try {
28955
+ const wh = await conn.whoami();
28956
+ const rc = {
28957
+ ownerPrincipalId: wh.ownerId,
28958
+ ownerDisplayName: wh.displayName,
28959
+ remoteUrl: args.remoteUrl,
28960
+ capabilityId: wh.capabilityId,
28961
+ capabilityToken: args.token,
28962
+ grants: wh.grants,
28963
+ receivedAt: now()
28964
+ };
28965
+ deps.store.upsert(rc);
28966
+ deps.broadcast({ type: "received-capability:added", receivedCap: rc });
28967
+ return {
28968
+ response: { type: "received-capability:add:ok", receivedCap: rc }
28969
+ };
28970
+ } finally {
28971
+ try {
28972
+ conn.close();
28973
+ } catch {
28974
+ }
28975
+ }
28976
+ };
28977
+ const remove = async (frame, _client, ctx) => {
28978
+ ensureOwner(ctx);
28979
+ const { type: _t, requestId: _r, ...rest } = frame;
28980
+ const args = ReceivedCapabilityRemoveArgsSchema.parse(rest);
28981
+ deps.store.remove(args.ownerPrincipalId);
28982
+ deps.broadcast({
28983
+ type: "received-capability:removed",
28984
+ ownerPrincipalId: args.ownerPrincipalId
28985
+ });
28986
+ return {
28987
+ response: {
28988
+ type: "received-capability:remove:ok",
28989
+ ownerPrincipalId: args.ownerPrincipalId
28990
+ }
28991
+ };
28992
+ };
28993
+ const autoAttach = async (frame, _client, ctx) => {
28994
+ ensureGuestCtx(ctx);
28995
+ const { type: _t, requestId: _r, ...rest } = frame;
28996
+ const args = ReceivedCapabilityAutoAttachArgsSchema.parse(rest);
28997
+ const rc = {
28998
+ ownerPrincipalId: args.ownerPrincipalId,
28999
+ ownerDisplayName: args.ownerDisplayName,
29000
+ remoteUrl: args.remoteUrl,
29001
+ capabilityId: args.capabilityId,
29002
+ capabilityToken: args.token,
29003
+ grants: args.grants,
29004
+ receivedAt: now()
29005
+ };
29006
+ deps.store.upsert(rc);
29007
+ deps.broadcast({ type: "received-capability:added", receivedCap: rc });
29008
+ return {
29009
+ response: { type: "received-capability:autoAttach:ok" }
29010
+ };
29011
+ };
29012
+ return {
29013
+ "received-capability:list": list,
29014
+ "received-capability:add": add,
29015
+ "received-capability:remove": remove,
29016
+ "received-capability:autoAttach": autoAttach
29017
+ };
29018
+ }
29019
+
29020
+ // src/handlers/whoami.ts
29021
+ init_protocol();
29022
+ function buildWhoamiHandler(deps) {
29023
+ return async (_frame, _client, ctx) => {
29024
+ if (!ctx) {
29025
+ throw new ClawdError(ERROR_CODES.INTERNAL, "whoami: missing ConnectionContext");
29026
+ }
29027
+ const owner = makeOwnerPrincipal(deps.ownerPrincipalId, deps.ownerDisplayName);
29028
+ let capability;
29029
+ if (ctx.principal.kind === "owner") {
29030
+ capability = {
29031
+ id: deps.ownerPrincipalId,
29032
+ displayName: deps.ownerDisplayName,
29033
+ grants: ctx.grants,
29034
+ issuedAt: 0,
29035
+ usedCount: 0
29036
+ };
29037
+ } else {
29038
+ if (!ctx.capabilityId) {
29039
+ throw new ClawdError(ERROR_CODES.UNAUTHORIZED, "whoami: guest ctx without capabilityId");
29040
+ }
29041
+ const cap = deps.capabilityManager.findById(ctx.capabilityId);
29042
+ if (!cap) {
29043
+ throw new ClawdError(ERROR_CODES.UNAUTHORIZED, `whoami: capability not found: ${ctx.capabilityId}`);
29044
+ }
29045
+ capability = stripSecretHash(cap);
29046
+ }
29047
+ const grantedPersonas = [];
29048
+ const isGuest = ctx.principal.kind === "guest";
29049
+ const hasWildcard = capability.grants.some((g2) => g2.resource.type === "*");
29050
+ if (hasWildcard) {
29051
+ for (const id of deps.personaStore.list()) {
29052
+ const file = deps.personaStore.read(id);
29053
+ if (!file) continue;
29054
+ if (isGuest && !file.public) continue;
29055
+ grantedPersonas.push({ id: file.personaId, displayName: file.label });
29056
+ }
29057
+ } else {
29058
+ for (const g2 of capability.grants) {
29059
+ if (g2.resource.type !== "persona") continue;
29060
+ const file = deps.personaStore.read(g2.resource.id);
29061
+ if (!file) continue;
29062
+ grantedPersonas.push({ id: file.personaId, displayName: file.label });
29063
+ }
29064
+ }
29065
+ return {
29066
+ response: {
29067
+ type: "whoami:ok",
29068
+ owner,
29069
+ capability,
29070
+ grantedPersonas
29071
+ }
29072
+ };
29073
+ };
29074
+ }
29075
+
27564
29076
  // src/handlers/meta.ts
27565
29077
  var import_node_os13 = __toESM(require("os"), 1);
27566
29078
  init_protocol();
@@ -27624,9 +29136,14 @@ function buildPersonaHandlers(deps) {
27624
29136
  const persona = personaManager.create(args);
27625
29137
  return { response: { type: "persona:info", ...persona } };
27626
29138
  };
27627
- const list = async () => {
29139
+ const list = async (_frame, _client, ctx) => {
29140
+ const all = personaRegistry.list();
29141
+ const isGuest = ctx?.principal.kind === "guest";
29142
+ const personas = isGuest ? all.filter(
29143
+ (p2) => p2.public && assertGrant(ctx.grants, { type: "persona", id: p2.personaId }, "read")
29144
+ ) : all;
27628
29145
  return {
27629
- response: { type: "persona:list", personas: personaRegistry.list() }
29146
+ response: { type: "persona:list", personas }
27630
29147
  };
27631
29148
  };
27632
29149
  const get = async (frame) => {
@@ -27664,26 +29181,12 @@ function buildPersonaHandlers(deps) {
27664
29181
  response: { type: "persona:deleted", personaId: args.personaId }
27665
29182
  };
27666
29183
  };
27667
- const issueToken = async (frame) => {
27668
- const args = PersonaIssueTokenArgsSchema.parse(frame);
27669
- const { token, persona } = personaManager.issueToken(args.personaId, args.label);
27670
- return {
27671
- response: { type: "persona:tokenIssued", token, persona }
27672
- };
27673
- };
27674
- const revokeToken = async (frame) => {
27675
- const args = PersonaRevokeTokenArgsSchema.parse(frame);
27676
- const persona = personaManager.revokeToken(args.personaId, args.token);
27677
- return { response: { type: "persona:info", ...persona } };
27678
- };
27679
29184
  return {
27680
29185
  "persona:create": create,
27681
29186
  "persona:list": list,
27682
29187
  "persona:get": get,
27683
29188
  "persona:update": update,
27684
- "persona:delete": del,
27685
- "persona:issueToken": issueToken,
27686
- "persona:revokeToken": revokeToken
29189
+ "persona:delete": del
27687
29190
  };
27688
29191
  }
27689
29192
 
@@ -27814,23 +29317,11 @@ function buildAttachmentHandlers(deps) {
27814
29317
  const entries = deps.groupFileStore.list(scope, parsed.data.sessionId);
27815
29318
  return { response: { type: "attachment.groupList", entries } };
27816
29319
  };
27817
- const groupListPersona = async (frame) => {
27818
- if (!deps.groupFileStore) {
27819
- throw new ClawdError(ERROR_CODES.METHOD_NOT_IMPLEMENTED, "groupFileStore not wired");
27820
- }
27821
- const parsed = AttachmentGroupListPersonaArgs.safeParse(frame);
27822
- if (!parsed.success) {
27823
- throw new ClawdError(ERROR_CODES.VALIDATION_ERROR, parsed.error.message);
27824
- }
27825
- const perSession = deps.groupFileStore.listByPersona(parsed.data.personaId);
27826
- return { response: { type: "attachment.groupListPersona", perSession } };
27827
- };
27828
29320
  return {
27829
29321
  "attachment.signUrl": signUrl,
27830
29322
  "attachment.groupAdd": groupAdd,
27831
29323
  "attachment.groupRemove": groupRemove,
27832
- "attachment.groupList": groupList,
27833
- "attachment.groupListPersona": groupListPersona
29324
+ "attachment.groupList": groupList
27834
29325
  };
27835
29326
  }
27836
29327
 
@@ -27848,10 +29339,136 @@ function buildMethodHandlers(deps) {
27848
29339
  personaManager: deps.personaManager,
27849
29340
  personaRegistry: deps.personaRegistry
27850
29341
  }),
29342
+ ...buildCapabilityHandlers({
29343
+ manager: deps.capabilityManager,
29344
+ getShareBaseUrl: deps.getShareBaseUrl
29345
+ }),
29346
+ ...buildInboxHandlers({
29347
+ manager: deps.inboxManager,
29348
+ capManager: deps.capabilityManager
29349
+ }),
29350
+ ...buildReceivedCapabilityHandlers({
29351
+ store: deps.receivedCapabilityStore,
29352
+ connectRemote: deps.connectRemote,
29353
+ broadcast: deps.broadcastToOwners,
29354
+ selfPrincipalId: () => deps.ownerPrincipalId,
29355
+ selfDisplayName: () => deps.ownerDisplayName
29356
+ }),
29357
+ whoami: buildWhoamiHandler({
29358
+ ownerDisplayName: deps.ownerDisplayName,
29359
+ ownerPrincipalId: deps.ownerPrincipalId,
29360
+ personaStore: deps.personaStore,
29361
+ capabilityManager: deps.capabilityManager
29362
+ }),
27851
29363
  ...deps.attachment ? buildAttachmentHandlers(deps.attachment) : {}
27852
29364
  };
27853
29365
  }
27854
29366
 
29367
+ // src/handlers/method-grants.ts
29368
+ var ADMIN_ANY = {
29369
+ kind: "fixed",
29370
+ resource: { type: "*" },
29371
+ action: "admin"
29372
+ };
29373
+ var CAPABILITY_SCOPED = { kind: "capability-scoped" };
29374
+ var METHOD_GRANT_MAP = {
29375
+ // ---- public(meta-only,guest 也能调) ----
29376
+ "info": { kind: "public" },
29377
+ "ping": { kind: "public" },
29378
+ // v2 Phase 6: whoami 是身份反查,任何 authed connection (owner / guest) 都能调;
29379
+ // handler 内部按 ctx.principal.kind 决定返回 "合成 owner self capability" 还是
29380
+ // "查 capabilityManager 拿 caller 的 guest capability"。
29381
+ "whoami": { kind: "public" },
29382
+ // ---- capability platform(admin-only,本 PR 新增) ----
29383
+ "capability:issue": ADMIN_ANY,
29384
+ "capability:list": ADMIN_ANY,
29385
+ "capability:delete": ADMIN_ANY,
29386
+ "capability:bindPeer": ADMIN_ANY,
29387
+ // ---- inbox: channel-based IM (capability platform v3) ----
29388
+ // 三条都 'public' — capability 本身就是授权凭证. handler 内额外校验
29389
+ // guest ctx.capabilityId === args.capabilityId, 防 guest 越权操作别的 channel.
29390
+ "inbox:list": { kind: "public" },
29391
+ "inbox:markRead": { kind: "public" },
29392
+ "inbox:postMessage": { kind: "public" },
29393
+ // ---- received-capability:* (bidirectional cap 视角 B 2026-05-23) ----
29394
+ // owner-only: list / add / remove;guest-only: autoAttach (capability 本身是凭证)
29395
+ "received-capability:list": ADMIN_ANY,
29396
+ "received-capability:add": ADMIN_ANY,
29397
+ "received-capability:remove": ADMIN_ANY,
29398
+ "received-capability:autoAttach": { kind: "public" },
29399
+ // ---- session:* / chat:* 业务方法(v2 Phase 8 两层模型)----
29400
+ // dispatcher 不验资源,handler 内按 ctx + frame.args 反查 ownerPersonaId 调 assertGrant。
29401
+ // owner 自动通过(ctx 自带 '*':'admin' grant 一切 match);guest 在被授权 persona 内可调。
29402
+ "session:create": CAPABILITY_SCOPED,
29403
+ "session:list": CAPABILITY_SCOPED,
29404
+ "session:get": CAPABILITY_SCOPED,
29405
+ "session:update": CAPABILITY_SCOPED,
29406
+ "session:delete": CAPABILITY_SCOPED,
29407
+ "session:send": CAPABILITY_SCOPED,
29408
+ "session:stop": CAPABILITY_SCOPED,
29409
+ "session:interrupt": CAPABILITY_SCOPED,
29410
+ "session:rewind": CAPABILITY_SCOPED,
29411
+ "session:rewind-diff": CAPABILITY_SCOPED,
29412
+ "session:rewindable-message-ids": CAPABILITY_SCOPED,
29413
+ "session:fork": CAPABILITY_SCOPED,
29414
+ "session:new": CAPABILITY_SCOPED,
29415
+ "session:resume": CAPABILITY_SCOPED,
29416
+ "session:observe": CAPABILITY_SCOPED,
29417
+ "session:events": CAPABILITY_SCOPED,
29418
+ "session:subscribe": CAPABILITY_SCOPED,
29419
+ "session:unsubscribe": CAPABILITY_SCOPED,
29420
+ "session:pin": CAPABILITY_SCOPED,
29421
+ "session:reorderPins": ADMIN_ANY,
29422
+ // owner 全局操作,无 personaId 维度
29423
+ "permission:respond": CAPABILITY_SCOPED,
29424
+ "session:answerQuestion": CAPABILITY_SCOPED,
29425
+ "session:cancelQuestion": CAPABILITY_SCOPED,
29426
+ "history:projects": ADMIN_ANY,
29427
+ "history:list": ADMIN_ANY,
29428
+ // history:read 取一条 session 的 CC JSONL 历史。guest 进 persona VM 后必须能读自己
29429
+ // 创建的 session 历史(chat view 加载历史消息走这条),所以下沉到 capability-scoped;
29430
+ // handler 内按 ctx.capabilityId === file.creatorPrincipalId 拦越权。
29431
+ "history:read": CAPABILITY_SCOPED,
29432
+ "history:subagents": ADMIN_ANY,
29433
+ "history:subagent-read": ADMIN_ANY,
29434
+ "history:recentDirs": ADMIN_ANY,
29435
+ "workspace:list": ADMIN_ANY,
29436
+ "workspace:read": ADMIN_ANY,
29437
+ "skills:list": ADMIN_ANY,
29438
+ "agents:list": ADMIN_ANY,
29439
+ "git:root": ADMIN_ANY,
29440
+ "git:branch": ADMIN_ANY,
29441
+ "git:branches": ADMIN_ANY,
29442
+ "capabilities:get": ADMIN_ANY,
29443
+ "persona:create": ADMIN_ANY,
29444
+ // persona:list 下沉到 CAPABILITY_SCOPED:guest 调用合法(用于 People 面板展开
29445
+ // 对方分享的 personas),handler 端按 ctx + persona.public + assertGrant 过滤。
29446
+ "persona:list": CAPABILITY_SCOPED,
29447
+ "persona:get": ADMIN_ANY,
29448
+ "persona:update": ADMIN_ANY,
29449
+ "persona:delete": ADMIN_ANY,
29450
+ "session:pty:input": ADMIN_ANY,
29451
+ "session:pty:resize": ADMIN_ANY,
29452
+ // file-sharing attachment.* RPC:dispatcher 用 admin-only 兜底(wire-level 拦),
29453
+ // 实际只有 owner 能调(personal token 链路 2026-05-21 删除,HTTP Bearer 只识别 owner)
29454
+ "attachment.signUrl": ADMIN_ANY,
29455
+ "attachment.groupAdd": ADMIN_ANY,
29456
+ "attachment.groupRemove": ADMIN_ANY,
29457
+ "attachment.groupList": ADMIN_ANY
29458
+ };
29459
+ function computeGrantForFrame(method, frame) {
29460
+ const rule = METHOD_GRANT_MAP[method];
29461
+ if (!rule) return { kind: "public" };
29462
+ if (rule.kind === "public") return { kind: "public" };
29463
+ if (rule.kind === "capability-scoped") return { kind: "public" };
29464
+ if (rule.kind === "fixed") {
29465
+ return { kind: "check", resource: rule.resource, action: rule.action };
29466
+ }
29467
+ const picked = rule.pick(frame);
29468
+ if (!picked) return { kind: "public" };
29469
+ return { kind: "check", resource: picked.resource, action: picked.action };
29470
+ }
29471
+
27855
29472
  // src/index.ts
27856
29473
  async function startDaemon(config) {
27857
29474
  const logger = createLogger({
@@ -27867,24 +29484,90 @@ async function startDaemon(config) {
27867
29484
  if (pre.status === "stale") {
27868
29485
  logger.warn("stale state file detected, overwriting", { pid: pre.existing.pid });
27869
29486
  }
29487
+ const authFile = loadOrCreateAuthFile({ dataDir: config.dataDir });
27870
29488
  let resolvedAuthToken = null;
27871
- let authFile = null;
27872
29489
  if (config.authToken && config.authToken.trim()) {
27873
29490
  resolvedAuthToken = config.authToken.trim();
27874
29491
  } else if (config.tunnel) {
27875
- authFile = loadOrCreateAuthFile({ dataDir: config.dataDir });
27876
29492
  resolvedAuthToken = authFile.token;
27877
29493
  }
29494
+ const ownerPrincipalId = authFile.ownerPrincipalId;
29495
+ const ownerDisplayName = loadOwnerDisplayName(config.dataDir);
27878
29496
  const authMode = resolvedAuthToken == null ? "none" : "first-message";
29497
+ migrateLegacyFiles(config.dataDir);
29498
+ const inboxStore = new InboxStore(config.dataDir);
29499
+ const inboxManager = new InboxManager(inboxStore, (_peerOwnerId, frame) => {
29500
+ wsServer?.broadcastToOwners(frame);
29501
+ });
29502
+ const receivedCapabilityStore = new ReceivedCapabilityStore(config.dataDir);
29503
+ receivedCapabilityStore.load();
29504
+ const capabilityStore = new CapabilityStore(config.dataDir);
29505
+ const capabilityRegistry = new CapabilityRegistry(capabilityStore);
29506
+ const capabilityManager = new CapabilityManager(capabilityRegistry, {
29507
+ onIssued: (cap, token) => {
29508
+ wsServer?.broadcastToOwners({
29509
+ type: "capability:tokenIssued",
29510
+ capability: stripSecretHash(cap),
29511
+ token
29512
+ });
29513
+ },
29514
+ onDeleted: (cap) => {
29515
+ const deletedAt = Date.now();
29516
+ wsServer?.broadcastToOwners({
29517
+ type: "capability:tokenDeleted",
29518
+ capabilityId: cap.id,
29519
+ deletedAt
29520
+ });
29521
+ wsServer?.closeConnectionsByCapability(cap.id);
29522
+ const cleanup = cleanupGuestSessionsForCapability(cap, sessionStoreFactory);
29523
+ if (cleanup.removed.length > 0) {
29524
+ logger.info("capability delete cascade: guest sessions removed", {
29525
+ capabilityId: cap.id,
29526
+ removedDirs: cleanup.removed
29527
+ });
29528
+ }
29529
+ }
29530
+ });
29531
+ migrateDropPersonsDir(config.dataDir);
27879
29532
  let wsServer = null;
27880
29533
  const authGate = authMode === "first-message" ? new AuthGate({
27881
29534
  shouldEnforce: buildShouldEnforce({ tunnel: config.tunnel }),
29535
+ // Task 1.7:authenticate 注入路径替代 expectedToken 单 token 比对。
29536
+ // owner 路径 constantTimeEqual 防侧信道;guest 路径走 capabilityRegistry.
27882
29537
  expectedToken: resolvedAuthToken,
29538
+ authenticate: (t, selfPrincipalId, selfDisplayName) => {
29539
+ return authenticate(t, {
29540
+ isOwnerToken: (x) => resolvedAuthToken != null && constantTimeEqual(x, resolvedAuthToken),
29541
+ ownerPrincipalId,
29542
+ ownerDisplayName,
29543
+ capabilityRegistry,
29544
+ selfPrincipalId,
29545
+ selfDisplayName,
29546
+ onGuestHello: (capId, ownerId, name) => {
29547
+ capabilityManager.recordPeerHello(capId, {
29548
+ ownerPrincipalId: ownerId,
29549
+ displayName: name,
29550
+ now: Date.now()
29551
+ });
29552
+ }
29553
+ });
29554
+ },
29555
+ onAuthed: (h, ctx) => wsServer?.attachClientContext(h.id, ctx),
29556
+ buildOwnerContext: () => ownerContext(ownerPrincipalId, ownerDisplayName),
27883
29557
  closeConnection: (h, code, reason) => wsServer?.closeClient(h.id, code, reason),
27884
29558
  sendOk: (h, payload) => wsServer?.sendToClient(h.id, payload)
27885
29559
  }) : null;
27886
29560
  resetRegistry();
27887
- const store = new SessionStore({ dataDir: config.dataDir });
29561
+ const migrateResult = migrateFlattenSessions({ dataDir: config.dataDir });
29562
+ if (!migrateResult.skipped && (migrateResult.movedBare || migrateResult.movedVmOwner || migrateResult.archivedListener)) {
29563
+ logger.info("sessions migration applied", {
29564
+ movedBare: migrateResult.movedBare,
29565
+ movedVmOwner: migrateResult.movedVmOwner,
29566
+ archivedListener: migrateResult.archivedListener
29567
+ });
29568
+ }
29569
+ const sessionStoreFactory = new SessionStoreFactory({ dataDir: config.dataDir });
29570
+ const store = sessionStoreFactory.forBare();
27888
29571
  const workspace = new WorkspaceBrowser();
27889
29572
  const skills = new SkillsScanner();
27890
29573
  const agents = new AgentsScanner();
@@ -27897,10 +29580,12 @@ async function startDaemon(config) {
27897
29580
  } else {
27898
29581
  logger.warn("persona.seed.skip", { reason: "defaults-root-not-found" });
27899
29582
  }
27900
- const ownerDisplayName = loadOwnerDisplayName(config.dataDir);
27901
29583
  const groupFileStore = new GroupFileStore({ dataDir: config.dataDir, logger });
27902
29584
  const manager = new SessionManager({
27903
29585
  store,
29586
+ // Phase 2 (capability platform plan §1): factory 注入后 manager.storeFor 走
29587
+ // 新布局派生 (sessions/* + personas/<pid>/.clawd/sessions/owner/*)
29588
+ storeFactory: sessionStoreFactory,
27904
29589
  logger,
27905
29590
  getAdapter,
27906
29591
  historyReader: history,
@@ -27908,6 +29593,7 @@ async function startDaemon(config) {
27908
29593
  personaRoot: import_node_path27.default.join(config.dataDir, "personas"),
27909
29594
  personaStore,
27910
29595
  ownerDisplayName,
29596
+ ownerPrincipalId,
27911
29597
  mode: config.mode,
27912
29598
  broadcastFrame: (frame, target) => {
27913
29599
  if (target === "all") {
@@ -28043,18 +29729,37 @@ async function startDaemon(config) {
28043
29729
  // group RPC + sign 都用:根据 sessionId 反查 scope。owner-mode persona session 走
28044
29730
  // 'persona/<pid>/owner',default 走 'default'。
28045
29731
  getSessionScope: (sid) => manager.findOwnedSessionScope(sid)
28046
- }
29732
+ },
29733
+ // Task 1.9: capability:issue/list/revoke handler 依赖
29734
+ capabilityManager,
29735
+ // v2 Phase 5: capability:issue 返回的 shareUrl 用此 base URL 拼接。
29736
+ // tunnel 拉起后切到反代 wss://... 地址;否则本机 ws://host:port。
29737
+ getShareBaseUrl: () => currentTunnelUrl ?? `ws://${config.host}:${config.port}`,
29738
+ // v2 Phase 6: whoami handler 装在 owner principal.displayName + persona 解析
29739
+ ownerDisplayName,
29740
+ // owner-id stabilization: whoami 用稳定 ownerPrincipalId 替代 'owner' 字面量
29741
+ ownerPrincipalId,
29742
+ personaStore,
29743
+ // capability handler 也用 (capability:issue 走 registry / capability:list)
29744
+ capabilityRegistry,
29745
+ // capability platform v3 inbox: handler 接 manager (post/list/markRead),
29746
+ // cascade 接 store (list 统计 deletedInboxEvents).
29747
+ inboxManager,
29748
+ inboxStore,
29749
+ // bidirectional cap 视角 B: 对方颁给我的 cap 走这个 store
29750
+ receivedCapabilityStore,
29751
+ // received-capability:added / removed broadcast;复用 capability:tokenIssued 同款通路
29752
+ broadcastToOwners: (frame) => wsServer?.broadcastToOwners(frame),
29753
+ // received-capability:add 临时 ws 连远端跑 whoami — 直连 ws (不走 transport listener)
29754
+ connectRemote
28047
29755
  });
28048
29756
  const authResolver = new AuthContextResolver({
28049
- ownerToken: resolvedAuthToken,
28050
- personaRegistry
29757
+ ownerToken: resolvedAuthToken
28051
29758
  });
28052
29759
  const httpRouter = createHttpRouter({
28053
29760
  authResolver,
28054
29761
  daemonVersion: version,
28055
29762
  logger,
28056
- personaStore,
28057
- groupFileStore,
28058
29763
  sessionStore: store,
28059
29764
  // /files HMAC verify 用 auth.json 的 signSecret 字段(与 attachment.signUrl 同源)。
28060
29765
  // --auth-token CLI 模式没 signSecret → 路由返 501,sign URL 功能整体禁用。
@@ -28064,6 +29769,8 @@ async function startDaemon(config) {
28064
29769
  host: config.host,
28065
29770
  port: config.port,
28066
29771
  logger,
29772
+ // broadcastToPrincipal 用此 id 路由 owner-targeted 帧(替代字面量 'owner' sentinel)
29773
+ ownerPrincipalId,
28067
29774
  readyFrameBuilder: (ctx) => buildReadyFrame(
28068
29775
  {
28069
29776
  manager,
@@ -28079,6 +29786,29 @@ async function startDaemon(config) {
28079
29786
  ),
28080
29787
  protocolVersion: PROTOCOL_VERSION,
28081
29788
  authGate: authGate ?? void 0,
29789
+ // noAuth 模式下仍验 capability token: 修 capability platform 漏洞 — 远端 client
29790
+ // 用 cap token 连 noAuth daemon 时若无此 hook 会 fallback owner ctx, whoami 错返
29791
+ // capability.id = ownerPrincipalId, 导致 myCapabilityId 写错 → inbox channel 写错.
29792
+ // 命中 cap → attach guest ctx; 空 token / 未命中 → 保持 noAuth 行为 (handler fallback owner).
29793
+ tryVerifyCapabilityToken: (token, selfPrincipalId, selfDisplayName) => {
29794
+ const v2 = capabilityRegistry.verifyToken(token);
29795
+ if (!v2.ok) return null;
29796
+ if (selfPrincipalId && selfDisplayName) {
29797
+ try {
29798
+ capabilityManager.recordPeerHello(v2.capability.id, {
29799
+ ownerPrincipalId: selfPrincipalId,
29800
+ displayName: selfDisplayName,
29801
+ now: Date.now()
29802
+ });
29803
+ } catch {
29804
+ }
29805
+ }
29806
+ return {
29807
+ principal: { id: v2.capability.id, kind: "guest", displayName: v2.capability.displayName },
29808
+ grants: v2.capability.grants,
29809
+ capabilityId: v2.capability.id
29810
+ };
29811
+ },
28082
29812
  // file-sharing HTTP 路由复用 daemon 同端口(spec §5 第 3 条);router 自己处理 auth + 404
28083
29813
  httpRequestHandler: httpRouter,
28084
29814
  // 订阅成功后给该 client 重放 in-flight pendingQuestions(plan: clawd-question-server-truth)。
@@ -28131,7 +29861,18 @@ async function startDaemon(config) {
28131
29861
  const requestId = typeof frame.requestId === "string" ? frame.requestId : void 0;
28132
29862
  const handler = handlers[type];
28133
29863
  if (!handler) throw new ClawdError(ERROR_CODES.METHOD_NOT_IMPLEMENTED, `not implemented: ${type}`);
28134
- const result = await handler(frame, client);
29864
+ const ctx = wsServer.getClientContext(client.id) ?? ownerContext(ownerPrincipalId, ownerDisplayName);
29865
+ const verdict = computeGrantForFrame(type, frame);
29866
+ if (verdict.kind === "check") {
29867
+ const ok = assertGrant(ctx.grants, verdict.resource, verdict.action);
29868
+ if (!ok) {
29869
+ throw new ClawdError(
29870
+ ERROR_CODES.UNAUTHORIZED,
29871
+ `principal ${ctx.principal.kind}:${ctx.principal.id} cannot ${verdict.action} on ${verdict.resource.type}${"id" in verdict.resource ? ":" + verdict.resource.id : ""}`
29872
+ );
29873
+ }
29874
+ }
29875
+ const result = await handler(frame, client, ctx);
28135
29876
  if (requestId && result.response) {
28136
29877
  client.send({ ...result.response, requestId });
28137
29878
  }
@@ -28254,6 +29995,13 @@ ${bar}
28254
29995
  manager
28255
29996
  };
28256
29997
  }
29998
+ function migrateDropPersonsDir(dataDir) {
29999
+ const dir = import_node_path27.default.join(dataDir, "persons");
30000
+ try {
30001
+ import_node_fs24.default.rmSync(dir, { recursive: true, force: true });
30002
+ } catch {
30003
+ }
30004
+ }
28257
30005
 
28258
30006
  // src/cli.ts
28259
30007
  async function main() {