Skip to content

Commit 2a77e6e

Browse files
authored
feat(onboard): named first agent (#123521)
* feat(onboard): named first agent * fix(onboard): harden named-agent handoff * test(onboard): preserve authored roster provenance * test(gateway): restore authorized steering coverage * fix(onboard): report imported roster name conflicts
1 parent b694ee1 commit 2a77e6e

41 files changed

Lines changed: 1360 additions & 248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/cli/onboard.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ openclaw onboard --tui
4040
openclaw onboard --classic
4141
openclaw onboard --modern
4242
openclaw onboard --flow quickstart
43+
openclaw onboard --agent-name robby
4344
openclaw onboard --flow manual
4445
openclaw onboard --flow import
4546
openclaw onboard --import-from hermes --import-source ~/.hermes
@@ -77,6 +78,9 @@ not overwrite the existing skill.
7778

7879
- `--classic`: opens the full step-by-step wizard. It cannot be combined with
7980
`--non-interactive`; omit `--classic` for automated setup.
81+
- `--agent-name <name>`: names the first agent when no roster exists. Interactive
82+
onboarding asks **What should we call your first agent?** and suggests `main`;
83+
non-interactive onboarding keeps `main` unless this flag is provided.
8084
- `--flow quickstart`: opens the classic wizard with minimal prompts, uses
8185
token auth by default, and generates a token when no stored or explicit
8286
credential applies. Explicit local Gateway flags such as
@@ -90,14 +94,15 @@ not overwrite the existing skill.
9094
- `--tailscale-reset-on-exit` and `--no-tailscale-reset-on-exit`: explicitly control whether Tailscale Serve or Funnel configuration is reset when the Gateway exits. Omitting both preserves the current setting during non-interactive reruns.
9195
- `--modern` is a compatibility alias for the OpenClaw conversational setup
9296
assistant. It uses the same live-inference gate as `openclaw setup` and
93-
accepts only `--workspace`, `--accept-risk`,
97+
accepts only `--workspace`, `--agent-name`, `--accept-risk`,
9498
`--non-interactive`, and `--json`. Other setup flags are rejected instead of
9599
being silently ignored.
96100

97101
## Guided flow
98102

99103
Plain `openclaw onboard` starts the guided flow. It shows the security notice,
100-
then asks one question up front: **full access** (recommended — setup looks for
104+
asks for the first agent's name when no roster exists, then asks one discovery
105+
question up front: **full access** (recommended — setup looks for
101106
AI apps, keys, and local runtimes automatically) or **ask first** (setup asks
102107
once before looking around, or lets you configure manually). The
103108
choice persists as `wizard.accessMode`. With discovery allowed, onboarding
@@ -221,6 +226,7 @@ OPENCLAW_LOCALE=en openclaw onboard # Explicit English override
221226

222227
```bash
223228
openclaw onboard --non-interactive --accept-risk --skip-health \
229+
--agent-name robby \
224230
--auth-choice custom-api-key \
225231
--custom-base-url "https://llm.example.com/v1" \
226232
--custom-model-id "foo-large" \

‎docs/gateway/config-agents.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ top-level keys, see [Configuration reference](/gateway/configuration-reference).
1313

1414
OpenClaw stamps `agents.ownership: "explicit"` when creating a multi-agent fleet. Such fleets have no default: channels and ambient services need bindings or surface-specific `agentId` targets. Doctor materializes legacy owners during upgrade; sole-agent configs need no marker.
1515

16+
On a fresh install, interactive onboarding asks for the first agent's name and
17+
uses `main` as the suggested value. Automated onboarding keeps the historical
18+
`main` default unless you pass `openclaw onboard --non-interactive --agent-name
19+
<name> ...`. A sole named agent uses the same default workspace and shared auth
20+
store as `main`; onboarding also migrates legacy `agent:main:*` session history
21+
to that sole owner before it finishes.
22+
1623
## Agent defaults
1724

1825
### `agents.defaults.workspace`

‎src/agents/agent-create.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe("createAgent", () => {
9797
}) => {
9898
const transformed = (await transform(structuredClone(mocks.config), {
9999
snapshot: { exists: false },
100+
previousHash: null,
100101
})) as {
101102
nextConfig: Record<string, unknown>;
102103
result: unknown;
@@ -166,6 +167,45 @@ describe("createAgent", () => {
166167
expect((mocks.persisted.agents as { list?: unknown }).list).toBeUndefined();
167168
});
168169

170+
it("replaces only the load-time compatibility roster when creating a named first agent", async () => {
171+
await createAgent({
172+
entry: { id: "robby", name: "robby", workspace: "/tmp/robby" },
173+
bootstrapFirstAgent: true,
174+
});
175+
176+
expect(mocks.transformConfigFileWithRetry).toHaveBeenCalledOnce();
177+
expect(mocks.transformConfigFileWithRetry).toHaveBeenCalledWith(
178+
expect.objectContaining({
179+
writeOptions: { allowedAgentRosterRemovals: ["main"] },
180+
}),
181+
);
182+
expect(mocks.persisted).toMatchObject({
183+
agents: { entries: { robby: expect.objectContaining({ workspace: "/tmp/robby" }) } },
184+
});
185+
expect(
186+
(mocks.persisted.agents as { entries?: Record<string, unknown> }).entries,
187+
).not.toHaveProperty("main");
188+
});
189+
190+
it("rejects first-agent creation when the approved config hash changed under the lock", async () => {
191+
mocks.transformConfigFileWithRetry.mockImplementationOnce(async ({ transform }) =>
192+
transform(structuredClone(mocks.config), {
193+
snapshot: { exists: true },
194+
previousHash: "concurrent",
195+
}),
196+
);
197+
198+
await expect(
199+
createAgent({
200+
entry: { id: "robby", name: "robby", workspace: "/tmp/robby" },
201+
bootstrapFirstAgent: true,
202+
expectedConfigHash: "approved",
203+
}),
204+
).rejects.toThrow("config changed before first-agent creation");
205+
206+
expect(mocks.ensureAgentWorkspace).not.toHaveBeenCalled();
207+
});
208+
169209
it("keeps the first staged roster entry marker-free", async () => {
170210
mocks.config = { agents: { list: [] } };
171211

‎src/agents/agent-create.ts‎

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import {
66
findAgentEntryIndex,
77
listAgentEntries,
88
} from "../commands/agents.config.js";
9-
import { transformConfigFileWithRetry, withConfigMutationExclusive } from "../config/config.js";
9+
import { hasResolvedRosterBeforeMigrations } from "../config/agent-roster-provenance.js";
10+
import {
11+
ConfigMutationConflictError,
12+
transformConfigFileWithRetry,
13+
withConfigMutationExclusive,
14+
} from "../config/config.js";
1015
import { resolveSessionTranscriptsDirForAgent } from "../config/sessions/paths.js";
1116
import type { OptionalBootstrapFileName } from "../config/types.agent-defaults.js";
1217
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -36,6 +41,7 @@ type CreateAgentResult =
3641
agentDir: string;
3742
model?: string;
3843
bootstrapPending: boolean;
44+
configHash?: string;
3945
bindingResult?: ReturnType<typeof applyAgentBindings>;
4046
}
4147
| {
@@ -60,6 +66,10 @@ type CreateAgentParams = {
6066
entry?: CreateAgentEntry;
6167
/** Internal authorization for onboarding to materialize the reserved sole `main` agent. */
6268
bootstrapMain?: boolean;
69+
/** Replace the load-time compatibility roster when onboarding creates the first real agent. */
70+
bootstrapFirstAgent?: boolean;
71+
/** Config revision that must still own first-agent creation under the write lock. */
72+
expectedConfigHash?: string | null;
6373
workspace?: string;
6474
model?: string;
6575
emoji?: unknown;
@@ -87,6 +97,30 @@ function hasValidRawAgentIdCharacters(value: string): boolean {
8797
return /[a-z0-9]/iu.test(value);
8898
}
8999

100+
export function validateAgentIdInput(
101+
rawId: string,
102+
options: { allowBootstrapMain?: boolean; displayName?: string } = {},
103+
):
104+
| { ok: true; agentId: string }
105+
| { ok: false; reason: "invalid-name" | "reserved-id"; message: string; agentId?: string } {
106+
const displayName = options.displayName ?? rawId;
107+
if (!hasValidRawAgentIdCharacters(rawId)) {
108+
return {
109+
ok: false,
110+
reason: "invalid-name",
111+
message: `agent name "${displayName}" has no valid id characters`,
112+
};
113+
}
114+
const agentId = normalizeAgentId(rawId);
115+
if (
116+
(agentId === RESERVED_BOOTSTRAP_AGENT_ID && options.allowBootstrapMain !== true) ||
117+
isReservedSystemAgentId(agentId)
118+
) {
119+
return { ok: false, reason: "reserved-id", message: `"${agentId}" is reserved`, agentId };
120+
}
121+
return { ok: true, agentId };
122+
}
123+
90124
function isInjectedBootstrapMainEntry(entry: CreateAgentEntry | undefined): boolean {
91125
return (
92126
entry?.id === RESERVED_BOOTSTRAP_AGENT_ID && Object.keys(entry).every((key) => key === "id")
@@ -120,17 +154,15 @@ export async function createAgent(params: CreateAgentParams): Promise<CreateAgen
120154
return createError("invalid-name", "agent name is required");
121155
}
122156
const rawId = params.entry?.id ?? rawName;
123-
if (!hasValidRawAgentIdCharacters(rawId)) {
124-
return createError("invalid-name", `agent name "${rawName}" has no valid id characters`);
157+
const validation = validateAgentIdInput(rawId, {
158+
allowBootstrapMain: params.bootstrapMain,
159+
displayName: rawName,
160+
});
161+
if (!validation.ok) {
162+
return createError(validation.reason, validation.message, validation.agentId);
125163
}
126-
const agentId = normalizeAgentId(rawId);
164+
const agentId = validation.agentId;
127165
const isBootstrapMain = agentId === RESERVED_BOOTSTRAP_AGENT_ID && params.bootstrapMain === true;
128-
if (
129-
(!isBootstrapMain && agentId === RESERVED_BOOTSTRAP_AGENT_ID) ||
130-
isReservedSystemAgentId(agentId)
131-
) {
132-
return createError("reserved-id", `"${agentId}" is reserved`, agentId);
133-
}
134166

135167
const safeName = sanitizeAgentIdentityLine(rawName);
136168
const model = normalizeOptionalString(params.model);
@@ -173,8 +205,27 @@ export async function createAgent(params: CreateAgentParams): Promise<CreateAgen
173205
const committed = await transformConfig<CreateAgentResult>({
174206
afterWrite: { mode: "auto" },
175207
maxAttempts: 1,
208+
...(params.bootstrapFirstAgent
209+
? { writeOptions: { allowedAgentRosterRemovals: [RESERVED_BOOTSTRAP_AGENT_ID] } }
210+
: {}),
176211
transform: async (currentConfig, context) => {
177-
const currentEntries = listAgentEntries(currentConfig);
212+
if (
213+
Object.hasOwn(params, "expectedConfigHash") &&
214+
context.previousHash !== params.expectedConfigHash
215+
) {
216+
throw new ConfigMutationConflictError("config changed before first-agent creation", {
217+
currentHash: context.previousHash,
218+
retryable: false,
219+
});
220+
}
221+
const hasAuthoredRoster =
222+
params.bootstrapFirstAgent === true &&
223+
hasResolvedRosterBeforeMigrations(context.snapshot);
224+
if (params.bootstrapFirstAgent && hasAuthoredRoster) {
225+
throw new DuplicateAgentError();
226+
}
227+
const bootstrappingFirstAgent = params.bootstrapFirstAgent === true;
228+
const currentEntries = bootstrappingFirstAgent ? [] : listAgentEntries(currentConfig);
178229
const existingIndex = findAgentEntryIndex(currentEntries, agentId);
179230
const existingEntry = currentEntries[existingIndex];
180231
if (
@@ -219,17 +270,27 @@ export async function createAgent(params: CreateAgentParams): Promise<CreateAgen
219270
isBootstrapMain &&
220271
isInjectedBootstrapMainEntry(existingEntry) &&
221272
!context.snapshot.exists;
273+
const creationBase = bootstrappingFirstAgent
274+
? {
275+
...currentConfig,
276+
agents: {
277+
...currentConfig.agents,
278+
entries: {},
279+
list: undefined,
280+
},
281+
}
282+
: currentConfig;
222283
let nextConfig =
223284
existingIndex < 0 || materializeInjectedMain
224-
? applyAgentConfig(currentConfig, {
285+
? applyAgentConfig(creationBase, {
225286
agentId,
226287
name: safeName,
227288
workspace: workspaceDir,
228289
agentDir,
229290
model,
230291
identity,
231292
})
232-
: currentConfig;
293+
: creationBase;
233294
if (params.entry) {
234295
const { default: _retiredDefault, ...stagedEntry } = params.entry;
235296
const list = listAgentEntries(nextConfig);
@@ -324,7 +385,10 @@ export async function createAgent(params: CreateAgentParams): Promise<CreateAgen
324385
) {
325386
throw new Error(`agent "${agentId}" deletion tombstone changed during creation`);
326387
}
327-
return committed.result!;
388+
const result = committed.result!;
389+
return typeof committed.persistedHash === "string"
390+
? { ...result, configHash: committed.persistedHash }
391+
: result;
328392
});
329393
} catch (error) {
330394
if (error instanceof DuplicateAgentError) {

‎src/cli/program/register.onboard.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ describe("registerOnboardCommand", () => {
177177
expect(setupWizardOptions().skipBootstrap).toBe(true);
178178
});
179179

180+
it("forwards --agent-name to onboarding", async () => {
181+
await runCli(["onboard", "--agent-name", "robby"]);
182+
expect(setupWizardOptions().agentName).toBe("robby");
183+
});
184+
180185
it("forwards explicit --tailscale-reset-on-exit", async () => {
181186
await runCli(["onboard", "--tailscale-reset-on-exit"]);
182187
expect(setupWizardOptions().tailscaleResetOnExit).toBe(true);

‎src/cli/program/register.onboard.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function resolveTailscaleResetOnExitFlag(command: Command): boolean | und
4343
const MODERN_ONBOARD_OPTION_KEYS = new Set([
4444
"modern",
4545
"workspace",
46+
"agentName",
4647
"acceptRisk",
4748
"nonInteractive",
4849
"json",
@@ -205,6 +206,7 @@ export function registerOnboardCommand(program: Command): void {
205206
"--workspace <dir>",
206207
"Workspace proposal for guided setup; persisted by classic/non-interactive setup",
207208
)
209+
.option("--agent-name <name>", "Name for the first agent (default: main)")
208210
.option(
209211
"--reset",
210212
"Reset config + credentials + sessions before running onboard (workspace only with --reset-scope full)",
@@ -335,10 +337,12 @@ export function registerOnboardCommand(program: Command): void {
335337
interactive: !opts.nonInteractive,
336338
welcomeVariant: "onboarding",
337339
...(opts.workspace ? { setupWorkspace: opts.workspace as string } : {}),
340+
...(opts.agentName ? { setupAgentName: opts.agentName as string } : {}),
338341
},
339342
defaultRuntime,
340343
{
341344
...(opts.workspace ? { workspace: opts.workspace as string } : {}),
345+
...(opts.agentName ? { agentName: opts.agentName as string } : {}),
342346
...(opts.acceptRisk ? { acceptRisk: true } : {}),
343347
},
344348
);
@@ -354,6 +358,7 @@ export function registerOnboardCommand(program: Command): void {
354358
await setupWizardCommand(
355359
{
356360
workspace: opts.workspace as string | undefined,
361+
agentName: opts.agentName as string | undefined,
357362
nonInteractive: Boolean(opts.nonInteractive),
358363
acceptRisk: Boolean(opts.acceptRisk),
359364
classic: Boolean(opts.classic),

0 commit comments

Comments
 (0)