Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(agents): count agent descriptions in code points everywhere (#11487)
Eric Yum6d54300f33af
diff --git a/packages/core/agents/constants.ts b/packages/core/agents/constants.tsindex 9317d2e6e4..5ae85724bf 100644--- a/packages/core/agents/constants.ts+++ b/packages/core/agents/constants.ts@@ -3,6 +3,20 @@// Kept in core so both apps and the test suite read from one source.export const AGENT_DESCRIPTION_MAX_LENGTH = 255;+/**+ * Description length the way the server counts it: Unicode code points+ * (`utf8.RuneCountInString` in the handler, `char_length` in the DB check),+ * not UTF-16 units. `"π".length` is 2 but counts as 1 here. Use this for the+ * counter and every save/submit gate so they always agree.+ */+export function agentDescriptionLength(description: string): number {+ return [...description].length;+}++export function isAgentDescriptionWithinLimit(description: string): boolean {+ return agentDescriptionLength(description) <= AGENT_DESCRIPTION_MAX_LENGTH;+}+export const AGENT_CONVERSATION_STARTERS_MAX = 3;export const AGENT_CONVERSATION_STARTER_LABEL_MAX_LENGTH = 80;export const AGENT_CONVERSATION_STARTER_MAX_LENGTH = 4000;diff --git a/packages/core/agents/draft.test.ts b/packages/core/agents/draft.test.tsindex 9080763897..41f49b0624 100644--- a/packages/core/agents/draft.test.ts+++ b/packages/core/agents/draft.test.ts@@ -10,6 +10,10 @@ import {isDraftDescriptionWithinLimit,type AgentDraft,} from "./draft";+import {+ agentDescriptionLength,+ isAgentDescriptionWithinLimit,+} from "./constants";const draft = (): AgentDraft => ({qoderAgentId: "",@@ -339,6 +343,19 @@ describe("agent draft execution overrides", () => {// Runes, not UTF-16 units: 255 CJK characters are exactly at the limit.expect(isDraftDescriptionWithinLimit("ζ±".repeat(255))).toBe(true);expect(isDraftDescriptionWithinLimit("ζ±".repeat(256))).toBe(false);+ // An emoji is 2 UTF-16 units but 1 code point.+ expect(isDraftDescriptionWithinLimit("π".repeat(128))).toBe(true);+ expect(isDraftDescriptionWithinLimit("π".repeat(255))).toBe(true);+ expect(isDraftDescriptionWithinLimit("π".repeat(256))).toBe(false);+ });++ it("counts description length in code points, like the server", () => {+ expect(agentDescriptionLength("π".repeat(128))).toBe(128);+ expect("π".repeat(128).length).toBe(256);+ expect(isAgentDescriptionWithinLimit("π".repeat(128))).toBe(true);+ expect(isAgentDescriptionWithinLimit("π".repeat(256))).toBe(false);+ // A ZWJ family is several code points, as utf8.RuneCountInString sees it.+ expect(agentDescriptionLength("π¨βπ©βπ§")).toBe(5);});});diff --git a/packages/core/agents/draft.ts b/packages/core/agents/draft.tsindex 25cce5f24e..aca8d8b762 100644--- a/packages/core/agents/draft.ts+++ b/packages/core/agents/draft.ts@@ -9,9 +9,9 @@ import type {RuntimeDevice,} from "../types";import {- AGENT_DESCRIPTION_MAX_LENGTH,AGENT_MAX_CONCURRENT_TASKS_MAX,AGENT_MAX_CONCURRENT_TASKS_MIN,+ isAgentDescriptionWithinLimit,} from "./constants";// Declared with the other agent wire types so `StoredAgentDraft` can name it@@ -95,13 +95,12 @@ export function applyDraftModelChange(}/**- * Mirrors the server's `utf8.RuneCountInString` check (agent.go:1013). The- * textarea's `maxLength` covers typing and pasting, but a duplicated agent or- * an AI-builder draft can seed a longer value programmatically β without this- * the create button would submit into a guaranteed 400.+ * Mirrors the server's `utf8.RuneCountInString` check. The textarea has no+ * hard cap (a UTF-16 `maxLength` would disagree with the counter on emoji),+ * so this gate is what keeps the create button from submitting into a 400.*/export function isDraftDescriptionWithinLimit(description: string): boolean {- return [...description].length <= AGENT_DESCRIPTION_MAX_LENGTH;+ return isAgentDescriptionWithinLimit(description);}export function buildInvocationTargets(diff --git a/packages/views/agents/components/agent-detail-inspector.description.test.tsx b/packages/views/agents/components/agent-detail-inspector.description.test.tsxnew file mode 100644index 0000000000..365f5b4308--- /dev/null+++ b/packages/views/agents/components/agent-detail-inspector.description.test.tsx@@ -0,0 +1,105 @@+// @vitest-environment jsdom++// The description counter, the autosave gate and the+// server all count Unicode code points. 128 emoji are 256 UTF-16 units, which+// the old `description.length` gate refused to save while the counter read+// 128 / 255.++import { afterEach, describe, expect, it, vi } from "vitest";+import { cleanup, fireEvent, screen, waitFor } from "@testing-library/react";+import type { Agent, AgentRuntime } from "@multica/core/types";+import { renderWithI18n } from "../../test/i18n";+import { AgentDetailInspector } from "./agent-detail-inspector";++vi.mock("@tanstack/react-query", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@tanstack/react-query")>()),+ useQuery: () => ({ data: undefined, isSuccess: false }),+}));+vi.mock("../../common/avatar-upload-control", () => ({+ AvatarUploadControl: () => <div data-testid="avatar-upload" />,+}));+vi.mock("./inspector/model-picker", () => ({+ ModelPicker: () => <div data-testid="model-picker" />,+}));+vi.mock("./inspector/runtime-picker", () => ({+ RuntimePicker: () => <div data-testid="runtime-picker" />,+}));+vi.mock("./inspector/thinking-prop-row", () => ({+ ThinkingSettingField: () => <div data-testid="thinking-field" />,+}));+vi.mock("./inspector/service-tier-setting-field", () => ({+ ServiceTierSettingField: () => <div data-testid="service-tier-field" />,+}));++const agent = {+ id: "agent-1",+ workspace_id: "workspace-1",+ name: "Lambda",+ description: "Coding agent",+ runtime_id: "runtime-1",+ avatar_url: null,+ conversation_starters: [],+ max_concurrent_tasks: 1,+} as unknown as Agent;++function renderInspector({+ runtime = null,+}: { runtime?: AgentRuntime | null } = {}) {+ const onUpdate = vi.fn(+ async (_id: string, _data: Record<string, unknown>) => {},+ );+ renderWithI18n(+ <AgentDetailInspector+ agent={agent}+ runtime={runtime}+ runtimes={[]}+ members={[]}+ currentUserId="user-1"+ canEdit+ onUpdate={onUpdate}+ />,+ );+ return onUpdate;+}++describe("AgentDetailInspector description limit", () => {+ afterEach(() => {+ cleanup();+ });++ it("saves 128 emoji: counter and autosave both count code points", async () => {+ const onUpdate = renderInspector();+ const value = "π".repeat(128);++ fireEvent.change(screen.getByLabelText("Description"), {+ target: { value },+ });++ expect(screen.getByText("128 / 255")).toBeInTheDocument();+ await waitFor(+ () =>+ expect(onUpdate).toHaveBeenCalledWith("agent-1", {+ name: "Lambda",+ description: value,+ }),+ { timeout: 2000 },+ );+ });++ it("does not save 256 emoji and says by how much it is over", async () => {+ const onUpdate = renderInspector();++ fireEvent.change(screen.getByLabelText("Description"), {+ target: { value: "π".repeat(256) },+ });++ expect(screen.getByText(/256 \/ 255/)).toHaveClass("text-destructive");+ await new Promise((resolve) => setTimeout(resolve, 1200));+ expect(onUpdate).not.toHaveBeenCalled();+ });++ it("has no UTF-16 maxLength on the textarea", () => {+ renderInspector();+ expect(screen.getByLabelText("Description")).not.toHaveAttribute("maxlength");+ });+});diff --git a/packages/views/agents/components/agent-detail-inspector.tsx b/packages/views/agents/components/agent-detail-inspector.tsxindex 831b78de31..3b94d631e9 100644--- a/packages/views/agents/components/agent-detail-inspector.tsx+++ b/packages/views/agents/components/agent-detail-inspector.tsx@@ -11,6 +11,8 @@ import {AGENT_DESCRIPTION_MAX_LENGTH,AGENT_MAX_CONCURRENT_TASKS_MAX,AGENT_MAX_CONCURRENT_TASKS_MIN,+ agentDescriptionLength,+ isAgentDescriptionWithinLimit,} from "@multica/core/agents";import { useConfigStore } from "@multica/core/config";import { isImeComposing } from "@multica/core/utils";@@ -140,7 +142,7 @@ export function AgentDetailInspector({enabled:canEdit &&profileDraft.name.length > 0 &&- profileDraft.description.length <= AGENT_DESCRIPTION_MAX_LENGTH,+ isAgentDescriptionWithinLimit(profileDraft.description),isEqual: profileDraftsEqual,});@@ -253,12 +255,14 @@ export function AgentDetailInspector({onBlur={profileAutoSave.flush}disabled={!canEdit}rows={5}- maxLength={AGENT_DESCRIPTION_MAX_LENGTH}+ // No maxLength: it counts UTF-16 units and would disagree+ // with the counter on emoji. Past the limit the counter+ // turns red and autosave holds off.className="resize-y"placeholder={t(($) => $.inspector.description_placeholder)}/><CharCounter- length={[...description].length}+ length={agentDescriptionLength(description)}max={AGENT_DESCRIPTION_MAX_LENGTH}/></div>diff --git a/packages/views/agents/create/agent-configuration-panel.tsx b/packages/views/agents/create/agent-configuration-panel.tsxindex d4c52e54fe..3630f8bb42 100644--- a/packages/views/agents/create/agent-configuration-panel.tsx+++ b/packages/views/agents/create/agent-configuration-panel.tsx@@ -6,6 +6,7 @@ import {runtimeModelsOptions} from "@multica/core/runtimes";import {findModelCapabilityEntry,thinkingLevelRequiredForProvider} from "../components/inspector/model-capability";import {AGENT_DESCRIPTION_MAX_LENGTH,+ agentDescriptionLength,applyDraftModelChange,applyDraftRuntimeChange,type AgentDraft,@@ -160,14 +161,14 @@ export function AgentConfigurationPanel({onChange={(event) => set("description", event.target.value)}placeholder={t(($) => $.create_dialog.description_placeholder)}rows={compact ? 3 : 4}- // The create API rejects >255 characters with a 400. Cap the- // input and show the counter so the limit is visible before- // submitting, matching the settings page.- maxLength={AGENT_DESCRIPTION_MAX_LENGTH}+ // The create API rejects >255 code points with a 400. No+ // maxLength (UTF-16 units, wrong for emoji): the counter shows+ // the limit and Create stays disabled past it, matching the+ // settings page.className="resize-y"/><CharCounter- length={[...draft.description].length}+ length={agentDescriptionLength(draft.description)}max={AGENT_DESCRIPTION_MAX_LENGTH}/></div>