Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(sharing): bound long task titles at API boundary

DavidShen8d38a15aab82
diff --git a/docs/product-rules/shared-task-mode.md b/docs/product-rules/shared-task-mode.md
index 8a739576a1..dccd2b2d06 100644
--- a/docs/product-rules/shared-task-mode.md
+++ b/docs/product-rules/shared-task-mode.md
@@ -46,6 +46,7 @@
## 实现约束
- 复用 packages/device-link;新增跨账号的单任务授权,禁止降级为旧版全设备远控。
+- 长标题不阻止开启共享(2026-09-22 用户确认):只在创建共享的请求边界,将去除首尾空白后的标题截短到服务端现有的 128 个 UTF-16 码元上限,避免截断代理对;共享列表/管理页可显示该短标题,原任务标题和历史不变。昵称与服务端响应仍沿用严格校验,不放宽服务端协议。
- 同时校验 invoke、push、历史、附件、嵌套引用、离线补发与结果缓存的任务及成员归属。
- 共享附件使用 `cindy/device-link/shared-task/<sharedTaskId>/<uploaderAccountId>/<uuid.ext>`,复用既有中转前缀的 OSS 授权与生命周期;共享路径仍按任务和当前成员校验,不回退为同账号对象权限。客户端与服务端同步升级,不保留未发布开发版顶层 `cindy/shared-task/` 路径别名;普通远控附件路径不变。
- Desktop 与 Mobile 复用现有任务页面;房主在手机上通过既有同账号远控管理共享。网页端和匿名访客不在范围内。
diff --git a/packages/device-link/src/__tests__/sharedTaskApi.test.ts b/packages/device-link/src/__tests__/sharedTaskApi.test.ts
index 9ddd704a7a..4876681808 100644
--- a/packages/device-link/src/__tests__/sharedTaskApi.test.ts
+++ b/packages/device-link/src/__tests__/sharedTaskApi.test.ts
@@ -30,6 +30,53 @@ describe('sharedTask management client', () => {
await expect(api.create('session', 'Task', observed)).rejects.toBeInstanceOf(SharedTaskScopeChangedError);
expect(observed).toHaveBeenCalledExactlyOnceWith('sharedTask');
});
+ it('bounds long shared titles without changing the local title source', async () => {
+ const { api, request } = setup();
+ const title = 'x'.repeat(128) + 'overflow';
+ request.mockResolvedValue({ sharedTaskId: 'sharedTask', revision: 1 });
+ await api.create('session', title);
+ expect(request).toHaveBeenCalledWith('/api/device-link/shared-tasks', expect.objectContaining({
+ method: 'POST',
+ body: { sessionId: 'session', title: 'x'.repeat(128) },
+ }));
+ request.mockResolvedValue({ ...snapshot(), title: 'x'.repeat(128) });
+ await expect(api.get('sharedTask')).resolves.toEqual(expect.objectContaining({ title: 'x'.repeat(128) }));
+ request.mockResolvedValue({ sharedTasks: [{ ...snapshot(), title: 'x'.repeat(128) }] });
+ await expect(api.list()).resolves.toEqual([expect.objectContaining({ title: 'x'.repeat(128) })]);
+ });
+ it('does not split a surrogate pair at the shared title boundary', async () => {
+ const { api, request } = setup();
+ const title = 'x'.repeat(127) + '😀';
+ request.mockResolvedValue({ sharedTaskId: 'sharedTask', revision: 1 });
+ await api.create('session', title);
+ expect(request.mock.calls[0][1]).toEqual(expect.objectContaining({
+ body: { sessionId: 'session', title: 'x'.repeat(127) },
+ }));
+ });
+ it.each(['Short title', '中'.repeat(128), 'x'.repeat(126) + '😀'])('preserves titles within the wire limit', async (title) => {
+ const { api, request } = setup();
+ request.mockResolvedValue({ sharedTaskId: 'sharedTask', revision: 1 });
+ await api.create('session', title);
+ expect(request.mock.calls[0][1]).toEqual(expect.objectContaining({ body: { sessionId: 'session', title } }));
+ });
+ it('trims leading whitespace before bounding the shared title', async () => {
+ const { api, request } = setup();
+ request.mockResolvedValue({ sharedTaskId: 'sharedTask', revision: 1 });
+ await api.create('session', ' '.repeat(128) + 'Task');
+ expect(request.mock.calls[0][1]).toEqual(expect.objectContaining({ body: { sessionId: 'session', title: 'Task' } }));
+ });
+ it.each(['', ' '.repeat(129), 'x'.repeat(128) + '\ninvalid'])('rejects invalid titles before truncation', async (title) => {
+ const { api, request } = setup();
+ await expect(api.create('session', title)).rejects.toThrow('title');
+ expect(request).not.toHaveBeenCalled();
+ });
+ it('does not relax member label or response validation', async () => {
+ const { api, request } = setup();
+ await expect(api.join('x'.repeat(43), 'x'.repeat(129))).rejects.toThrow('label');
+ expect(request).not.toHaveBeenCalled();
+ request.mockResolvedValue({ ...snapshot(), title: 'x'.repeat(129) });
+ await expect(api.get('sharedTask')).rejects.toThrow('label');
+ });
it('does not pass malformed create IDs to cleanup', async () => {
const { api, request } = setup();
const observed = vi.fn();
diff --git a/packages/device-link/src/sharedTaskApi.ts b/packages/device-link/src/sharedTaskApi.ts
index aef40e9dd9..2865cf70b4 100644
--- a/packages/device-link/src/sharedTaskApi.ts
+++ b/packages/device-link/src/sharedTaskApi.ts
@@ -45,6 +45,15 @@ function label(value: unknown): string {
if (typeof value !== 'string' || !value.trim() || value.length > 128 || /[\u0000-\u001f\u007f]/.test(value)) throw new Error('Invalid sharedTask label');
return value;
}
+function sharedTaskTitle(value: unknown): string {
+ if (typeof value !== 'string' || !value.trim() || /[\u0000-\u001f\u007f]/.test(value)) throw new Error('Invalid sharedTask title');
+ // The server currently accepts at most 128 UTF-16 code units. Shared-task
+ // metadata is a bounded projection; keep the local session title untouched.
+ const title = value.trim();
+ let end = Math.min(title.length, 128);
+ if (end < title.length && /[\uD800-\uDBFF]/.test(title[end - 1] ?? '') && /[\uDC00-\uDFFF]/.test(title[end] ?? '')) end--;
+ return title.slice(0, end);
+}
function integer(value: unknown, min = 1): number {
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < min) throw new Error('Invalid sharedTask number');
return value;
@@ -80,7 +89,7 @@ export function createSharedTaskApi(options: SharedTaskApiOptions) {
const route = (sharedTaskId: string) => `/${encodeURIComponent(id(sharedTaskId))}`;
return {
async create(sessionId: string, title: string, observeCommitted?: (sharedTaskId: string) => void) {
- const value = await request('', 'POST', { sessionId: id(sessionId), title: label(title) }, (value) => {
+ const value = await request('', 'POST', { sessionId: id(sessionId), title: sharedTaskTitle(title) }, (value) => {
observeCommitted?.(id(value.sharedTaskId));
});
return { sharedTaskId: id(value.sharedTaskId), revision: integer(value.revision) };