fix(server): decouple explicit and auto agent title limits (#78)
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
|||||||
renameCurrentBranch,
|
renameCurrentBranch,
|
||||||
type CheckoutStatusResult,
|
type CheckoutStatusResult,
|
||||||
} from "../../utils/checkout-git.js";
|
} from "../../utils/checkout-git.js";
|
||||||
|
import { MAX_AUTO_AGENT_TITLE_CHARS } from "./agent-title-limits.js";
|
||||||
|
|
||||||
export type AgentMetadataGeneratorDeps = {
|
export type AgentMetadataGeneratorDeps = {
|
||||||
generateStructuredAgentResponseWithFallback?: typeof generateStructuredAgentResponseWithFallback;
|
generateStructuredAgentResponseWithFallback?: typeof generateStructuredAgentResponseWithFallback;
|
||||||
@@ -43,6 +44,14 @@ function hasExplicitTitle(title?: string | null): boolean {
|
|||||||
return Boolean(title && title.trim().length > 0);
|
return Boolean(title && title.trim().length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeAutoTitle(title: string): string | null {
|
||||||
|
const normalized = title.trim();
|
||||||
|
if (!normalized) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return normalized.slice(0, MAX_AUTO_AGENT_TITLE_CHARS).trim() || null;
|
||||||
|
}
|
||||||
|
|
||||||
async function canRenameBranch(
|
async function canRenameBranch(
|
||||||
cwd: string,
|
cwd: string,
|
||||||
paseoHome: string | undefined,
|
paseoHome: string | undefined,
|
||||||
@@ -97,7 +106,7 @@ function buildMetadataSchema(needs: AgentMetadataNeeds): z.ZodObject<any> | null
|
|||||||
|
|
||||||
const shape: Record<string, z.ZodTypeAny> = {};
|
const shape: Record<string, z.ZodTypeAny> = {};
|
||||||
if (needs.needsTitle) {
|
if (needs.needsTitle) {
|
||||||
shape.title = z.string().min(1).max(60);
|
shape.title = z.string().min(1).max(MAX_AUTO_AGENT_TITLE_CHARS);
|
||||||
}
|
}
|
||||||
if (needs.needsBranch) {
|
if (needs.needsBranch) {
|
||||||
shape.branch = z.string().min(1).max(100);
|
shape.branch = z.string().min(1).max(100);
|
||||||
@@ -115,7 +124,9 @@ function buildPrompt(needs: AgentMetadataNeeds): string {
|
|||||||
];
|
];
|
||||||
|
|
||||||
if (needs.needsTitle) {
|
if (needs.needsTitle) {
|
||||||
instructions.push("Title: short descriptive label (<= 60 chars).");
|
instructions.push(
|
||||||
|
`Title: short descriptive label (<= ${MAX_AUTO_AGENT_TITLE_CHARS} chars).`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (needs.needsBranch) {
|
if (needs.needsBranch) {
|
||||||
instructions.push(
|
instructions.push(
|
||||||
@@ -187,8 +198,8 @@ export async function generateAndApplyAgentMetadata(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (needs.needsTitle && typeof result.title === "string") {
|
if (needs.needsTitle && typeof result.title === "string") {
|
||||||
const normalizedTitle = result.title.trim();
|
const normalizedTitle = normalizeAutoTitle(result.title);
|
||||||
if (normalizedTitle.length > 0) {
|
if (normalizedTitle) {
|
||||||
await options.agentManager.setTitle(options.agentId, normalizedTitle);
|
await options.agentManager.setTitle(options.agentId, normalizedTitle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { createTestLogger } from "../../test-utils/test-logger.js";
|
||||||
|
import { MAX_AUTO_AGENT_TITLE_CHARS } from "./agent-title-limits.js";
|
||||||
|
import {
|
||||||
|
generateAndApplyAgentMetadata,
|
||||||
|
type AgentMetadataGeneratorDeps,
|
||||||
|
} from "./agent-metadata-generator.js";
|
||||||
|
import type { AgentManager } from "./agent-manager.js";
|
||||||
|
|
||||||
|
const logger = createTestLogger();
|
||||||
|
|
||||||
|
const NON_GIT_CHECKOUT_STATUS = {
|
||||||
|
isGit: false,
|
||||||
|
isPaseoOwnedWorktree: false,
|
||||||
|
currentBranch: null,
|
||||||
|
repoRoot: "/tmp/repo",
|
||||||
|
} as Awaited<
|
||||||
|
ReturnType<NonNullable<AgentMetadataGeneratorDeps["getCheckoutStatus"]>>
|
||||||
|
>;
|
||||||
|
|
||||||
|
function createDeps(
|
||||||
|
generateStructuredAgentResponseWithFallback: NonNullable<
|
||||||
|
AgentMetadataGeneratorDeps["generateStructuredAgentResponseWithFallback"]
|
||||||
|
>
|
||||||
|
): AgentMetadataGeneratorDeps {
|
||||||
|
return {
|
||||||
|
generateStructuredAgentResponseWithFallback,
|
||||||
|
getCheckoutStatus: vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue(NON_GIT_CHECKOUT_STATUS) as NonNullable<
|
||||||
|
AgentMetadataGeneratorDeps["getCheckoutStatus"]
|
||||||
|
>,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("agent metadata generator auto-title", () => {
|
||||||
|
it("caps generated auto titles at 40 characters before persisting", async () => {
|
||||||
|
const setTitle = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const manager = { setTitle } as unknown as AgentManager;
|
||||||
|
const generatedTitle = "x".repeat(MAX_AUTO_AGENT_TITLE_CHARS + 25);
|
||||||
|
const generateStructured = vi.fn().mockResolvedValue({ title: generatedTitle }) as NonNullable<
|
||||||
|
AgentMetadataGeneratorDeps["generateStructuredAgentResponseWithFallback"]
|
||||||
|
>;
|
||||||
|
|
||||||
|
await generateAndApplyAgentMetadata({
|
||||||
|
agentManager: manager,
|
||||||
|
agentId: "agent-1",
|
||||||
|
cwd: "/tmp/repo",
|
||||||
|
initialPrompt: "Implement this feature",
|
||||||
|
explicitTitle: null,
|
||||||
|
logger,
|
||||||
|
deps: createDeps(generateStructured),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(setTitle).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setTitle).toHaveBeenCalledWith(
|
||||||
|
"agent-1",
|
||||||
|
"x".repeat(MAX_AUTO_AGENT_TITLE_CHARS)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not generate an auto title when an explicit title is provided", async () => {
|
||||||
|
const setTitle = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const manager = { setTitle } as unknown as AgentManager;
|
||||||
|
const generateStructured = vi.fn().mockResolvedValue({ title: "Generated" }) as NonNullable<
|
||||||
|
AgentMetadataGeneratorDeps["generateStructuredAgentResponseWithFallback"]
|
||||||
|
>;
|
||||||
|
|
||||||
|
await generateAndApplyAgentMetadata({
|
||||||
|
agentManager: manager,
|
||||||
|
agentId: "agent-2",
|
||||||
|
cwd: "/tmp/repo",
|
||||||
|
initialPrompt: "Implement this feature",
|
||||||
|
explicitTitle: "Keep this title",
|
||||||
|
logger,
|
||||||
|
deps: createDeps(generateStructured),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(generateStructured).not.toHaveBeenCalled();
|
||||||
|
expect(setTitle).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export const MAX_EXPLICIT_AGENT_TITLE_CHARS = 200;
|
||||||
|
export const MAX_AUTO_AGENT_TITLE_CHARS = 40;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { SessionInboundMessageSchema } from "./messages";
|
import { SessionInboundMessageSchema } from "./messages";
|
||||||
|
import { MAX_EXPLICIT_AGENT_TITLE_CHARS } from "../server/agent/agent-title-limits.js";
|
||||||
|
|
||||||
describe("create_agent_request clientMessageId", () => {
|
describe("create_agent_request clientMessageId", () => {
|
||||||
it("accepts clientMessageId for stable initial prompt transfer", () => {
|
it("accepts clientMessageId for stable initial prompt transfer", () => {
|
||||||
@@ -20,4 +21,36 @@ describe("create_agent_request clientMessageId", () => {
|
|||||||
}
|
}
|
||||||
expect(parsed.clientMessageId).toBe("client-msg-1");
|
expect(parsed.clientMessageId).toBe("client-msg-1");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("accepts explicit titles up to the create-agent limit", () => {
|
||||||
|
const parsed = SessionInboundMessageSchema.parse({
|
||||||
|
type: "create_agent_request",
|
||||||
|
requestId: "req-title-ok",
|
||||||
|
config: {
|
||||||
|
provider: "claude",
|
||||||
|
cwd: "/tmp/project",
|
||||||
|
title: "x".repeat(MAX_EXPLICIT_AGENT_TITLE_CHARS),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(parsed.type).toBe("create_agent_request");
|
||||||
|
if (parsed.type !== "create_agent_request") {
|
||||||
|
throw new Error("Expected create_agent_request");
|
||||||
|
}
|
||||||
|
expect(parsed.config.title).toHaveLength(MAX_EXPLICIT_AGENT_TITLE_CHARS);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects explicit titles longer than the create-agent limit", () => {
|
||||||
|
const parsed = SessionInboundMessageSchema.safeParse({
|
||||||
|
type: "create_agent_request",
|
||||||
|
requestId: "req-title-too-long",
|
||||||
|
config: {
|
||||||
|
provider: "claude",
|
||||||
|
cwd: "/tmp/project",
|
||||||
|
title: "x".repeat(MAX_EXPLICIT_AGENT_TITLE_CHARS + 1),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(parsed.success).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { AGENT_LIFECYCLE_STATUSES } from './agent-lifecycle.js'
|
import { AGENT_LIFECYCLE_STATUSES } from './agent-lifecycle.js'
|
||||||
|
import { MAX_EXPLICIT_AGENT_TITLE_CHARS } from '../server/agent/agent-title-limits.js'
|
||||||
import { AgentProviderSchema } from '../server/agent/provider-manifest.js'
|
import { AgentProviderSchema } from '../server/agent/provider-manifest.js'
|
||||||
import type {
|
import type {
|
||||||
AgentCapabilityFlags,
|
AgentCapabilityFlags,
|
||||||
@@ -89,7 +90,7 @@ const AgentSessionConfigSchema = z.object({
|
|||||||
modeId: z.string().optional(),
|
modeId: z.string().optional(),
|
||||||
model: z.string().optional(),
|
model: z.string().optional(),
|
||||||
thinkingOptionId: z.string().optional(),
|
thinkingOptionId: z.string().optional(),
|
||||||
title: z.string().trim().min(1).max(40).optional().nullable(),
|
title: z.string().trim().min(1).max(MAX_EXPLICIT_AGENT_TITLE_CHARS).optional().nullable(),
|
||||||
approvalPolicy: z.string().optional(),
|
approvalPolicy: z.string().optional(),
|
||||||
sandboxMode: z.string().optional(),
|
sandboxMode: z.string().optional(),
|
||||||
networkAccess: z.boolean().optional(),
|
networkAccess: z.boolean().optional(),
|
||||||
|
|||||||
Reference in New Issue
Block a user