u-foo 3.1.0 → 3.1.2

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.
package/README.md CHANGED
@@ -269,6 +269,19 @@ and `agent_handle`. Include the handle in heartbeat, activity, send, receive,
269
269
  acknowledgement, report, and unregister calls. The handle is an ownership
270
270
  capability: do not send it to peers or print it in reports.
271
271
 
272
+ For a new external registration, ufoo assigns both the subscriber's
273
+ eight-character session suffix and its automatic `<agent-prefix>-<number>`
274
+ nickname on the server, using the same identity generators as wrapper launches.
275
+ A Cursor MCP registration with `agent_type: "cursor"`, for example, receives
276
+ `cursor:<8-hex>` and `cursor-N`; the model selected inside Cursor does not
277
+ replace the Cursor Agent type.
278
+ A stable `client_instance_id` recovers that same identity after a transport
279
+ restart; even when that value is a UUID, it is not used as the subscriber
280
+ suffix. Legacy MCP subscribers whose suffix is the client UUID are superseded
281
+ by the short server-assigned identity on their next registration. MCP Agents
282
+ cannot self-assign or update these identity fields; an explicit operator rename
283
+ remains available through the controller/CLI rename path.
284
+
272
285
  - **Codex App:** call MCP `wait_for_message` in the foreground with
273
286
  the registered subscriber and handle, `after_seq: 0`, and
274
287
  `timeout_seconds: 0`. The tool call stays pending inside the dedicated
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "u-foo",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "description": "Multi-Agent Workspace Protocol. Just add u. claude → uclaude, codex → ucodex.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "homepage": "https://ufoo.dev",
@@ -5,6 +5,7 @@ const fs = require("fs");
5
5
  const net = require("net");
6
6
  const path = require("path");
7
7
  const EventBus = require("../../coordination/bus");
8
+ const { isMismatchedAutoNickname } = require("../../coordination/bus/nickname");
8
9
  const { isAgentPidAlive } = require("../../coordination/bus/utils");
9
10
  const { showBanner } = require("../../ui/format/banner");
10
11
  const AgentNotifier = require("./notifier");
@@ -206,7 +207,9 @@ function findPreviousSession(cwd, agentType, tty, tmuxPane) {
206
207
  return {
207
208
  sessionId: parts[1],
208
209
  subscriberId: id,
209
- nickname: meta.nickname || "",
210
+ nickname: isMismatchedAutoNickname(meta.nickname, agentType)
211
+ ? ""
212
+ : (meta.nickname || ""),
210
213
  providerSessionId: meta.provider_session_id || "",
211
214
  };
212
215
  }
@@ -330,6 +330,7 @@ async function resolveUpstreamRuntime({
330
330
  return {
331
331
  provider: "codex",
332
332
  transport: "codex-responses",
333
+ requestProfile: useCodexOAuth ? "codex-subscription" : "openai-responses",
333
334
  model: resolvedModel,
334
335
  baseUrl,
335
336
  credential,
@@ -472,6 +473,13 @@ async function sendUpstreamRequest({
472
473
  }
473
474
  }
474
475
  const requestBody = request && typeof request === "object" ? { ...request } : {};
476
+ // The ChatGPT subscription Codex endpoint is Responses-shaped but does not
477
+ // accept the public Responses API's optional max_output_tokens field. Keep
478
+ // the limit for api.openai.com/API-key traffic and omit it only for the
479
+ // OAuth-backed Codex subscription profile.
480
+ if (isCodexResponses && resolvedRuntime.requestProfile === "codex-subscription") {
481
+ delete requestBody.max_output_tokens;
482
+ }
475
483
  if (isKimi) requestBody.model = requestModel;
476
484
  const body = JSON.stringify(requestBody);
477
485
 
@@ -1,6 +1,30 @@
1
1
  /**
2
2
  * 昵称管理和解析
3
3
  */
4
+ const AUTO_NICKNAME_PREFIXES = new Set([
5
+ "claude",
6
+ "codex",
7
+ "cursor",
8
+ "agy",
9
+ "grok",
10
+ "kimi",
11
+ "ucode",
12
+ ]);
13
+
14
+ function resolveAutoNicknamePrefix(agentType = "") {
15
+ const normalized = String(agentType || "").trim().toLowerCase();
16
+ if (normalized === "claude" || normalized === "claude-code") return "claude";
17
+ if (normalized === "ufoo" || normalized === "ucode" || normalized === "ufoo-code") return "ucode";
18
+ return normalized;
19
+ }
20
+
21
+ function isMismatchedAutoNickname(nickname = "", agentType = "") {
22
+ const match = String(nickname || "").trim().toLowerCase().match(/^([a-z][a-z0-9]*)-(\d+)$/);
23
+ if (!match || !AUTO_NICKNAME_PREFIXES.has(match[1])) return false;
24
+ const expectedPrefix = resolveAutoNicknamePrefix(agentType);
25
+ return Boolean(expectedPrefix && match[1] !== expectedPrefix);
26
+ }
27
+
4
28
  class NicknameManager {
5
29
  constructor(busData) {
6
30
  this.busData = busData;
@@ -47,9 +71,7 @@ class NicknameManager {
47
71
  */
48
72
  generateAutoNickname(agentType) {
49
73
  const subscribers = this.busData.agents || {};
50
- const prefix = agentType === "claude-code" ? "claude"
51
- : agentType === "ufoo-code" ? "ucode"
52
- : agentType;
74
+ const prefix = resolveAutoNicknamePrefix(agentType);
53
75
 
54
76
  // 找出所有相同前缀的昵称
55
77
  const existing = Object.values(subscribers)
@@ -97,3 +119,5 @@ class NicknameManager {
97
119
  }
98
120
 
99
121
  module.exports = NicknameManager;
122
+ module.exports.isMismatchedAutoNickname = isMismatchedAutoNickname;
123
+ module.exports.resolveAutoNicknamePrefix = resolveAutoNicknamePrefix;
@@ -2,6 +2,7 @@ const fs = require("fs");
2
2
  const path = require("path");
3
3
  const { getTimestamp, isAgentPidAlive, isMetaActive, isValidTty, getTtyProcessInfo } = require("./utils");
4
4
  const NicknameManager = require("./nickname");
5
+ const { isMismatchedAutoNickname } = require("./nickname");
5
6
  const { spawnSync } = require("child_process");
6
7
  const { appendAgentRegistryDiagnostic } = require("../state/agentRegistryDiagnostics");
7
8
 
@@ -185,7 +186,12 @@ class SubscriberManager {
185
186
  if (metaTty === ttyPath) {
186
187
  const sameAgentType = !currentAgentType || meta?.agent_type === currentAgentType;
187
188
  // Inherit user-set nickname only when replacing the same agent type.
188
- if (sameAgentType && meta.nickname && !inheritedNickname) {
189
+ if (
190
+ sameAgentType
191
+ && meta.nickname
192
+ && !inheritedNickname
193
+ && !isMismatchedAutoNickname(meta.nickname, currentAgentType)
194
+ ) {
189
195
  inheritedNickname = meta.nickname;
190
196
  }
191
197
  // Remove stale subscriber using same tty
@@ -236,6 +242,11 @@ class SubscriberManager {
236
242
 
237
243
  // 检查是否是重新加入(rejoin)
238
244
  const existingMeta = this.busData.agents[subscriber];
245
+ const existingNicknameMismatched = Boolean(
246
+ existingMeta
247
+ && existingMeta.nickname
248
+ && isMismatchedAutoNickname(existingMeta.nickname, agentType)
249
+ );
239
250
  let finalNickname = nickname;
240
251
  let finalScopedNickname = typeof options.scopedNickname === "string"
241
252
  ? options.scopedNickname.trim()
@@ -248,7 +259,11 @@ class SubscriberManager {
248
259
  throw new Error(`Nickname "${nickname}" already exists`);
249
260
  }
250
261
  finalNickname = nickname;
251
- } else if (existingMeta && existingMeta.nickname) {
262
+ } else if (
263
+ existingMeta
264
+ && existingMeta.nickname
265
+ && !existingNicknameMismatched
266
+ ) {
252
267
  // 重新加入,保留原昵称
253
268
  finalNickname = existingMeta.nickname;
254
269
  finalScopedNickname = existingMeta.scoped_nickname || finalScopedNickname || finalNickname;
@@ -259,7 +274,9 @@ class SubscriberManager {
259
274
  }
260
275
 
261
276
  if (!finalScopedNickname) {
262
- finalScopedNickname = existingMeta?.scoped_nickname || finalNickname;
277
+ finalScopedNickname = existingNicknameMismatched
278
+ ? finalNickname
279
+ : (existingMeta?.scoped_nickname || finalNickname);
263
280
  }
264
281
 
265
282
  const explicitLaunchMode = typeof options.launchMode === "string"
@@ -362,6 +362,7 @@ function isMetaActive(meta) {
362
362
  function normalizeAgentTypeAlias(value = "") {
363
363
  const text = String(value || "").trim().toLowerCase();
364
364
  if (!text) return "";
365
+ if (text === "cursor") return "cursor";
365
366
  if (text === "codex") return "codex";
366
367
  if (text === "claude" || text === "claude-code") return "claude-code";
367
368
  if (text === "agy") return "agy";
@@ -45,6 +45,16 @@ function normalizeBusAgentType(agentType = "") {
45
45
  return value;
46
46
  }
47
47
 
48
+ function assertServerOwnedExternalIdentity(args = {}, fields = [], operation = "register_agent") {
49
+ const field = fields.find((name) => Object.prototype.hasOwnProperty.call(args, name));
50
+ if (!field) return;
51
+ const err = new Error(
52
+ `${operation} ${field} is server-assigned; use the subscriber and nickname returned by ufoo`
53
+ );
54
+ err.code = "external_identity_field_forbidden";
55
+ throw err;
56
+ }
57
+
48
58
  function ensureBusLoaded(projectRoot) {
49
59
  const bus = new EventBus(projectRoot);
50
60
  bus.ensureBus();
@@ -72,14 +82,14 @@ function resolveSubscriberArg(args = {}) {
72
82
  return subscriber;
73
83
  }
74
84
 
75
- function createSessionId() {
76
- return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
77
- }
78
-
79
85
  function createCryptoSessionId() {
80
86
  return crypto.randomBytes(4).toString("hex");
81
87
  }
82
88
 
89
+ function isServerAllocatedExternalSessionId(sessionId = "") {
90
+ return /^[0-9a-f]{8}$/.test(String(sessionId || ""));
91
+ }
92
+
83
93
  function createAgentHandle() {
84
94
  return crypto.randomBytes(32).toString("base64url");
85
95
  }
@@ -174,7 +184,9 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
174
184
  ).trim();
175
185
  const bus = ensureBusLoaded(projectRoot);
176
186
 
177
- // Session ID: explicit > reuse > generate
187
+ // Wrapper launches may provide resume metadata. External MCP registrations
188
+ // recover only through client_instance_id; otherwise the server owns the
189
+ // short session id so subscriber shapes stay consistent across launch modes.
178
190
  let sessionId;
179
191
  const explicitSessionId = String(args.session_id || args.sessionId || "").trim();
180
192
  const reuseSession = args.reuseSession && typeof args.reuseSession === "object"
@@ -187,14 +199,18 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
187
199
  const reuseProviderSessionId = typeof reuseSession?.providerSessionId === "string"
188
200
  ? reuseSession.providerSessionId.trim() : "";
189
201
 
190
- const recoveredEntry = !validateParentPid && clientInstanceId
191
- ? Object.entries(bus.busData.agents || {}).find(([, meta]) => (
202
+ const clientInstanceEntries = !validateParentPid && clientInstanceId
203
+ ? Object.entries(bus.busData.agents || {}).filter(([, meta]) => (
192
204
  meta
193
205
  && meta.mcp_bridge === true
194
- && meta.agent_type === agentType
195
206
  && meta.mcp_client_instance_id === clientInstanceId
196
207
  ))
197
- : null;
208
+ : [];
209
+ const recoveredEntry = clientInstanceEntries.find(([subscriber, meta]) => (
210
+ meta.agent_type === agentType
211
+ && subscriber.startsWith(`${agentType}:`)
212
+ && isServerAllocatedExternalSessionId(subscriber.slice(agentType.length + 1))
213
+ ));
198
214
  const recoveredSubscriber = recoveredEntry ? recoveredEntry[0] : "";
199
215
  const recoveredSessionId = recoveredSubscriber.startsWith(`${agentType}:`)
200
216
  ? recoveredSubscriber.slice(agentType.length + 1)
@@ -202,12 +218,16 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
202
218
 
203
219
  if (recoveredSessionId) {
204
220
  sessionId = recoveredSessionId;
205
- } else if (explicitSessionId) {
221
+ } else if (validateParentPid && explicitSessionId) {
206
222
  sessionId = explicitSessionId;
207
- } else if (reuseSessionId && reuseSubscriberId === `${agentType}:${reuseSessionId}`) {
223
+ } else if (
224
+ validateParentPid
225
+ && reuseSessionId
226
+ && reuseSubscriberId === `${agentType}:${reuseSessionId}`
227
+ ) {
208
228
  sessionId = reuseSessionId;
209
229
  } else {
210
- sessionId = validateParentPid ? createCryptoSessionId() : createSessionId();
230
+ sessionId = createCryptoSessionId();
211
231
  }
212
232
 
213
233
  // parentPid validation
@@ -284,6 +304,20 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
284
304
  extendMcpAgentLease(meta);
285
305
  }
286
306
  if (hostCapabilities) meta.mcp_capabilities = hostCapabilities;
307
+ const supersededSubscribers = [];
308
+ if (!validateParentPid && clientInstanceId) {
309
+ for (const [previousSubscriber, previousMeta] of clientInstanceEntries) {
310
+ if (previousSubscriber === subscriber) continue;
311
+ const revokedAt = nowIso();
312
+ previousMeta.mcp_revoked_at = revokedAt;
313
+ previousMeta.mcp_lease_expires_at = revokedAt;
314
+ previousMeta.mcp_superseded_by = subscriber;
315
+ await bus.subscriberManager.leave(previousSubscriber);
316
+ previousMeta.status = "inactive";
317
+ previousMeta.activity_state = "";
318
+ supersededSubscribers.push(previousSubscriber);
319
+ }
320
+ }
287
321
  bus.saveBusData();
288
322
  notifyDaemonRefresh(projectRoot);
289
323
  return {
@@ -301,6 +335,7 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
301
335
  lease_expires_at: meta.mcp_lease_expires_at,
302
336
  client_instance_id: clientInstanceId,
303
337
  recovered: Boolean(recoveredSubscriber),
338
+ ...(supersededSubscribers.length > 0 ? { superseded_subscribers: supersededSubscribers } : {}),
304
339
  } : {}),
305
340
  reuseProviderSessionId,
306
341
  skipSessionResolve: !!args.skipSessionResolve,
@@ -308,6 +343,18 @@ async function registerAgentFull(projectRoot, args = {}, options = {}) {
308
343
  }
309
344
 
310
345
  async function registerAgent(projectRoot, args = {}) {
346
+ if (!String(args.agent_type || args.agentType || "").trim()) {
347
+ const err = new Error("register_agent requires agent_type for canonical identity allocation");
348
+ err.code = "external_agent_type_required";
349
+ throw err;
350
+ }
351
+ assertServerOwnedExternalIdentity(args, [
352
+ "session_id",
353
+ "sessionId",
354
+ "nickname",
355
+ "scoped_nickname",
356
+ "scopedNickname",
357
+ ]);
311
358
  return registerAgentFull(projectRoot, args, {
312
359
  validateParentPid: false,
313
360
  checkNicknameConflicts: false,
@@ -360,13 +407,14 @@ async function publishActivityState(projectRoot, args = {}) {
360
407
  }
361
408
 
362
409
  async function updateAgentMetadata(projectRoot, args = {}) {
410
+ assertServerOwnedExternalIdentity(args, [
411
+ "nickname",
412
+ "scoped_nickname",
413
+ "scopedNickname",
414
+ ], "update_agent_metadata");
363
415
  const subscriber = resolveSubscriberArg(args);
364
416
  const bus = ensureBusLoaded(projectRoot);
365
417
  const meta = assertAgentHandle(bus, subscriber, args);
366
- const nickname = String(args.nickname || "").trim();
367
- if (nickname) {
368
- await bus.subscriberManager.rename(subscriber, nickname);
369
- }
370
418
  const metadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
371
419
  if (Object.keys(metadata).length > 0) {
372
420
  meta.mcp_metadata = {
@@ -631,7 +679,6 @@ module.exports = {
631
679
  ensureBusLoaded,
632
680
  assertSubscriberExists,
633
681
  resolveSubscriberArg,
634
- createSessionId,
635
682
  createAgentHandle,
636
683
  hashAgentHandle,
637
684
  assertAgentHandle,
@@ -73,6 +73,7 @@ function sleep(ms) {
73
73
  function normalizeBusAgentType(agentType = "") {
74
74
  const value = String(agentType || "").trim().toLowerCase();
75
75
  if (!value) return "claude-code";
76
+ if (value === "cursor") return "cursor";
76
77
  if (value === "codex") return "codex";
77
78
  if (value === "claude" || value === "claude-code") return "claude-code";
78
79
  if (value === "agy" || value === "antigravity") return "agy";
@@ -51,16 +51,16 @@ const CUSTOM_TOOL_DEFINITIONS = Object.freeze([
51
51
  },
52
52
  {
53
53
  name: "register_agent",
54
- description: "Register an externally launched Agent only when its shell has no wrapper-provided UFOO_SUBSCRIBER_ID.",
54
+ description: "Register an externally launched Agent only when its shell has no wrapper-provided UFOO_SUBSCRIBER_ID. ufoo assigns the subscriber id and nickname with the same format used by wrapper launches (for example, cursor:<8-hex> and cursor-N).",
55
55
  input_schema: {
56
56
  type: "object",
57
- required: ["project_root"],
57
+ required: ["project_root", "agent_type"],
58
58
  properties: {
59
59
  project_root: { type: "string" },
60
- agent_type: { type: "string" },
61
- session_id: { type: "string" },
62
- nickname: { type: "string" },
63
- scoped_nickname: { type: "string" },
60
+ agent_type: {
61
+ type: "string",
62
+ description: "Agent family used as the subscriber and automatic nickname prefix, such as cursor, codex, claude, agy, grok, kimi, or ucode.",
63
+ },
64
64
  launch_mode: { type: "string" },
65
65
  capabilities: { type: "object", additionalProperties: true },
66
66
  client_instance_id: {
@@ -107,7 +107,7 @@ const CUSTOM_TOOL_DEFINITIONS = Object.freeze([
107
107
  },
108
108
  {
109
109
  name: "update_agent_metadata",
110
- description: "Update the caller agent nickname or MCP metadata in its project bus.",
110
+ description: "Update caller-owned MCP metadata in the project bus.",
111
111
  input_schema: {
112
112
  type: "object",
113
113
  required: ["project_root", "subscriber", "agent_handle"],
@@ -115,7 +115,6 @@ const CUSTOM_TOOL_DEFINITIONS = Object.freeze([
115
115
  project_root: { type: "string" },
116
116
  subscriber: { type: "string" },
117
117
  agent_handle: { type: "string" },
118
- nickname: { type: "string" },
119
118
  metadata: { type: "object", additionalProperties: true },
120
119
  },
121
120
  additionalProperties: false,