Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/agent/subagent/fork-resolution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,37 @@ describe('id and resume', () => {
expect(resolveForkInputs(args).id).toMatch(/^my-prefix-/);
});

it('sanitizes idPrefix by replacing disallowed characters with hyphens (#1319)', () => {
// A model-supplied id_prefix containing dots, spaces, or other chars outside
// [A-Za-z0-9_-] would cause assertSafeJobId to throw in SubagentLogWriter.
// Verify they are replaced with '-' before the id is assembled.
const args = makeArgs({
options: {
parent: { sessionId: 'sid' },
config: { model: 'sonnet' },
agentType: 'a',
idPrefix: 'research.agent',
},
counter: 1,
});

expect(resolveForkInputs(args).id).toMatch(/^research-agent-/);
});

it('sanitizes idPrefix with spaces', () => {
const args = makeArgs({
options: {
parent: { sessionId: 'sid' },
config: { model: 'sonnet' },
agentType: 'a',
idPrefix: 'my agent',
},
counter: 1,
});

expect(resolveForkInputs(args).id).toMatch(/^my-agent-/);
});

it('defaults to "subagent" when idPrefix is omitted', () => {
const args = makeArgs({
options: {
Expand Down
3 changes: 2 additions & 1 deletion src/agent/subagent/fork-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export interface ForkResolved {
export function resolveForkInputs(args: ResolveForkInputsArgs): ForkResolved {
const { options, counter, managerHookRegistry, parentTraceWriter, parentModel } = args;

const id = `${options.idPrefix ?? 'subagent'}-${Date.now()}-${counter}`;
const safePrefix = (options.idPrefix ?? 'subagent').replace(/[^A-Za-z0-9_-]/g, '-');
const id = `${safePrefix}-${Date.now()}-${counter}`;
Comment on lines +95 to +96

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Cap the prefix to fit the guarded ID length

When the model supplies a long id_prefix (the agent tool schema has no maxLength), replacing invalid characters does not prevent the generated ID from exceeding assertSafeJobId's 128-character limit; for example, a 128-character prefix produces a roughly 144-character ID. With subagent logging enabled, SubagentLogWriter still throws during fork creation, so the stated failure remains possible. Truncate the sanitized prefix to leave room for the timestamp, separators, and counter.

Useful? React with 👍 / 👎.

const resume = options.parent.sessionId;

const registry =
Expand Down
Loading