Skip to content

Commit 0222018

Browse files
authored
Merge pull request #154 from aictrl-dev/fix/issue-145-skip-title-call
fix(run): default the session title to skip the title LLM call (#145)
2 parents 1d5270b + d3ffc47 commit 0222018

2 files changed

Lines changed: 160 additions & 9 deletions

File tree

‎packages/cli/src/cli/cmd/run.ts‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,36 @@ function normalizePath(input?: string) {
303303
return input
304304
}
305305

306+
function truncatedTitle(text: string) {
307+
// Count code points so an emoji or other astral character is never split in half.
308+
const chars = [...text]
309+
return chars.slice(0, 50).join("") + (chars.length > 50 ? "..." : "")
310+
}
311+
312+
/**
313+
* Title for a session the run creates. Giving every new session a non-default
314+
* title keeps the prompt loop from sending the first message to the title agent
315+
* (SessionPrompt `ensureTitle`): an extra request nobody reads in headless runs,
316+
* whose tokens bypass usage accounting (#145).
317+
*
318+
* @internal exported for unit-testing only
319+
*/
320+
export function sessionTitle(input: {
321+
title?: string
322+
/** The positional words as typed, before they are re-quoted for the model. */
323+
prompt: string
324+
/** The message sent to the model: the re-quoted prompt plus any stdin. */
325+
message: string
326+
command?: string
327+
}) {
328+
if (input.title) return input.title
329+
// `--title ""` predates the default and keeps titling with the raw message.
330+
if (input.title === "") return truncatedTitle(input.message)
331+
const readable = input.prompt.trim() || input.message.trim()
332+
if (input.command) return truncatedTitle(`/${input.command} ${readable}`.trimEnd())
333+
return truncatedTitle(readable)
334+
}
335+
306336
export const RunCommand = cmd({
307337
command: "run [message..]",
308338
describe: "run aictrl with a message",
@@ -369,7 +399,7 @@ export const RunCommand = cmd({
369399
})
370400
.option("title", {
371401
type: "string",
372-
describe: "title for the session (uses truncated prompt if no value provided)",
402+
describe: "title for the session (defaults to the truncated prompt, or /<command> <args> for command runs)",
373403
})
374404
.option("attach", {
375405
type: "string",
@@ -484,12 +514,6 @@ export const RunCommand = cmd({
484514
},
485515
]
486516

487-
function title() {
488-
if (args.title === undefined) return
489-
if (args.title !== "") return args.title
490-
return message.slice(0, 50) + (message.length > 50 ? "..." : "")
491-
}
492-
493517
function input(id?: string, body?: Record<string, unknown>) {
494518
if (!args.attach) return { ...(id ? { sessionID: id } : {}), ...body }
495519
return { ...(id ? { path: { id } } : {}), ...(body ? { body } : {}), throwOnError: true }
@@ -505,8 +529,9 @@ export const RunCommand = cmd({
505529

506530
if (baseID) return baseID
507531

508-
const name = title()
509-
const result = await sdk.session.create(input(undefined, { title: name, permission: rules }))
532+
const prompt = [...args.message, ...(args["--"] || [])].join(" ")
533+
const title = sessionTitle({ title: args.title, prompt, message, command: args.command })
534+
const result = await sdk.session.create(input(undefined, { title, permission: rules }))
510535
return result.data?.id
511536
}
512537

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { tmpdir } from "../fixture/fixture"
4+
import { sessionTitle } from "../../src/cli/cmd/run"
5+
6+
const entry = path.resolve(import.meta.dir, "../../src/index.ts")
7+
const TITLE_PROMPT = "Generate a title for this conversation"
8+
9+
describe("sessionTitle (#145)", () => {
10+
test("--title <text> is used as given", () => {
11+
expect(sessionTitle({ title: "Release notes", prompt: "Summarise.", message: "Summarise." })).toBe("Release notes")
12+
})
13+
14+
test('--title "" keeps the raw message, truncated', () => {
15+
const message = '"Summarise the changes."\n'
16+
expect(sessionTitle({ title: "", prompt: "Summarise the changes.", message })).toBe(message)
17+
expect(sessionTitle({ title: "", prompt: "", message: "x".repeat(60) })).toBe("x".repeat(50) + "...")
18+
})
19+
20+
test("defaults to the prompt as typed, without the quoting added for the model", () => {
21+
const message = '"fix the \\"login\\" bug"\n'
22+
expect(sessionTitle({ prompt: 'fix the "login" bug', message })).toBe('fix the "login" bug')
23+
expect(sessionTitle({ prompt: "x".repeat(60), message: "x".repeat(60) })).toBe("x".repeat(50) + "...")
24+
})
25+
26+
test("defaults to the trimmed message when the prompt only arrives on stdin", () => {
27+
expect(sessionTitle({ prompt: "", message: "\nReview this diff.\n" })).toBe("Review this diff.")
28+
})
29+
30+
test("truncates by code point, never splitting a surrogate pair", () => {
31+
const prompt = "a" + "\u{1F600}".repeat(60)
32+
expect(sessionTitle({ prompt, message: prompt })).toBe("a" + "\u{1F600}".repeat(49) + "...")
33+
})
34+
35+
test("defaults to the command and its arguments for command runs", () => {
36+
expect(sessionTitle({ prompt: "this input", message: '"this input"\n', command: "check" })).toBe(
37+
"/check this input",
38+
)
39+
expect(sessionTitle({ prompt: "", message: "\n", command: "review" })).toBe("/review")
40+
})
41+
})
42+
43+
describe("headless run title generation (#145)", () => {
44+
test.each([
45+
["prompt", ["Check this input."]],
46+
["command", ["--command", "check", "this input"]],
47+
] as const)(
48+
"a %s run without --title makes no title-generation request",
49+
async (_, input) => {
50+
await using tmp = await tmpdir()
51+
const requests: string[] = []
52+
const server = Bun.serve({
53+
hostname: "127.0.0.1",
54+
port: 0,
55+
async fetch(request) {
56+
requests.push(await request.text())
57+
const chunk = {
58+
candidates: [{ index: 0, content: { role: "model", parts: [{ text: "Done." }] }, finishReason: "STOP" }],
59+
usageMetadata: { promptTokenCount: 7, candidatesTokenCount: 3, totalTokenCount: 10 },
60+
}
61+
return new Response(`data: ${JSON.stringify(chunk)}\n\n`, {
62+
headers: { "content-type": "text/event-stream" },
63+
})
64+
},
65+
})
66+
// Unlike the other run fixtures, the title agent stays enabled, and small_model routes
67+
// any title-generation request to this fixture server so the test can observe it.
68+
await Bun.write(
69+
path.join(tmp.path, "aictrl.json"),
70+
JSON.stringify({
71+
provider: {
72+
fixture: {
73+
npm: "@ai-sdk/google",
74+
options: { apiKey: "fixture", baseURL: `http://127.0.0.1:${server.port}` },
75+
models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } },
76+
},
77+
},
78+
small_model: "fixture/gemini-fixture",
79+
command: { check: { template: "Check $ARGUMENTS." } },
80+
}),
81+
)
82+
const proc = Bun.spawn(
83+
[
84+
"bun",
85+
"run",
86+
"--conditions=browser",
87+
entry,
88+
"run",
89+
"--format",
90+
"json",
91+
"--model",
92+
"fixture/gemini-fixture",
93+
...input,
94+
],
95+
{
96+
cwd: tmp.path,
97+
env: {
98+
...process.env,
99+
AICTRL_DISABLE_DEFAULT_PLUGINS: "true",
100+
AICTRL_DISABLE_MODELS_FETCH: "true",
101+
AICTRL_DISABLE_AUTOCOMPACT: "true",
102+
},
103+
stdin: "ignore",
104+
stdout: "pipe",
105+
stderr: "pipe",
106+
},
107+
)
108+
const timeout = setTimeout(() => proc.kill("SIGKILL"), 15000)
109+
try {
110+
const [stdout, stderr, exit] = await Promise.all([
111+
new Response(proc.stdout).text(),
112+
new Response(proc.stderr).text(),
113+
proc.exited,
114+
])
115+
expect(exit, stderr + stdout).toBe(0)
116+
expect(requests.filter((body) => body.includes(TITLE_PROMPT))).toHaveLength(0)
117+
expect(requests).toHaveLength(1)
118+
} finally {
119+
clearTimeout(timeout)
120+
proc.kill("SIGKILL")
121+
server.stop(true)
122+
}
123+
},
124+
20000,
125+
)
126+
})

0 commit comments

Comments
 (0)