Skip to content

Commit b8e7f27

Browse files
authored
feat(ui): let a link's slug settle a shared short-id prefix (#114486)
* feat(ui): let a link's slug settle a shared short-id prefix Two sessions can share the first block of their uuid, which sent an otherwise exact link to the disambiguation view even though the slug in that link already said which session was meant. Capture the slug on short references and use it to break the tie. It can only narrow: a slug matching none or several of the tied sessions leaves the chooser alone, and a reference that already resolves uniquely is untouched. Generated links stay at their normal length instead of growing a full uuid. * fix(ui): never settle a slug tie from an incomplete search A truncated candidate set is an unfinished search, not a tie: an unexamined page could hold the same short-id prefix under the same slug. Settling there would be the guess the bounded search exists to avoid, so keep the chooser.
1 parent b7cc681 commit b8e7f27

5 files changed

Lines changed: 123 additions & 15 deletions

File tree

‎docs/web/urls.md‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ The following parts are stable URL contracts:
7777
- The key UUID short id in short-id URLs.
7878
- The arity and short-versus-literal parsing rules above.
7979

80-
In short-id form, the agent segment and slug are explicitly decorative. They may
81-
change without notice and are not used to identify or validate the session.
82-
After resolution, the Control UI replaces the address bar with the current
83-
agent id and current display-name slug without adding a browser-history entry.
80+
In short-id form, the agent segment is decorative and the slug is almost
81+
decorative. Neither identifies the session on its own, and both may change
82+
without notice. The one exception is a tie: if the short id matches more than
83+
one session and exactly one of them still carries the slug in the link, that
84+
session is used, so a generated link keeps working even when two ids happen to
85+
share a prefix. A slug that matches none or several of the tied sessions is
86+
ignored and the disambiguation view is shown. After resolution, the Control UI
87+
replaces the address bar with the current agent id and current display-name slug
88+
without adding a browser-history entry.
8489

8590
In literal-key form, the agent segment is authoritative because it is part of
8691
the reconstructed session key. The remaining literal segments are authoritative
@@ -95,9 +100,10 @@ the slug, the UI shows the same disambiguation view used for short-id ties
95100
instead of guessing. Exact short-id and literal-key references always win over
96101
slug matching.
97102

98-
If one short id matches more than one session, the UI does not guess. It shows a
99-
small disambiguation view with the matching display names, agents, and longer id
100-
prefixes. Use a longer prefix to make the URL unique. Resolution examines at
103+
If one short id matches more than one session and the slug does not settle it,
104+
the UI does not guess. It shows a small disambiguation view with the matching
105+
display names, agents, and longer id prefixes. Use a longer prefix to make the
106+
URL unique. Resolution examines at
101107
most five pages of search results; if more remain, the view says that the search
102108
was incomplete instead of guessing.
103109

‎packages/session-url-contract/src/parse.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ export type ControlUiSessionPathTarget =
55
kind: "short";
66
agentId: string;
77
shortId: string;
8+
/**
9+
* Display-name slug that preceded the id, when the reference carried one. The id
10+
* stays authoritative; this only breaks a tie between sessions whose ids share the
11+
* given prefix, so a short link keeps resolving to one session.
12+
*/
13+
slugHint?: string;
814
}
915
| {
1016
namespace: "chat" | "dashboard";
@@ -126,9 +132,13 @@ export function parseControlUiSessionPath(
126132
return { namespace, kind: "literal", agentId, sessionKey };
127133
}
128134
const shortId = segment.match(SHORT_SESSION_REF_RE)?.[1]?.toLowerCase();
129-
return shortId
130-
? { namespace, kind: "short", agentId, shortId }
131-
: { namespace, kind: "literal", agentId, sessionKey, slugCandidate: segment };
135+
if (!shortId) {
136+
return { namespace, kind: "literal", agentId, sessionKey, slugCandidate: segment };
137+
}
138+
const slugHint = segment.slice(0, segment.length - shortId.length).replace(/-+$/u, "");
139+
return slugHint
140+
? { namespace, kind: "short", agentId, shortId, slugHint }
141+
: { namespace, kind: "short", agentId, shortId };
132142
}
133143
return null;
134144
}

‎ui/src/app-navigation.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,14 @@ describe("routeIdFromPath", () => {
540540
agentId: "main",
541541
shortId: "12345678",
542542
});
543+
// The slug is captured so it can settle a tie between ids sharing this prefix, but it
544+
// never changes the parsed id: a wrong agent and a wrong slug both stay decorative.
543545
expect(sessionRefFromPath("/chat/wrong/wrong-slug-1234567890ab")).toEqual({
544546
namespace: "chat",
545547
kind: "short",
546548
agentId: "wrong",
547549
shortId: "1234567890ab",
550+
slugHint: "wrong-slug",
548551
});
549552
expect(sessionRefFromPath("/chat/main/telegram/12345")).toEqual({
550553
namespace: "chat",

‎ui/src/pages/chat/route-loader.ts‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ function sessionReferenceMatches(
192192
return result.sessions.filter((row) => sessionKeyUuid(row.key)?.startsWith(prefix) === true);
193193
}
194194

195+
// Two sessions can share a short id's prefix, which would send an otherwise exact link to
196+
// the disambiguation view. When the link also carries a display-name slug, that slug says
197+
// which one was meant, so it settles the tie and keeps generated links durable at their
198+
// normal length. It can only narrow: a hint that matches nothing (a stale or hand-edited
199+
// name) leaves the original candidates for the chooser rather than dropping the session.
200+
//
201+
// A truncated set is not a tie, it is an unfinished search. Another page could hold the
202+
// same prefix under the same slug, so settling here would be the guess the bounded search
203+
// exists to avoid.
204+
function narrowBySlugHint(
205+
resolution: SessionReferenceResolution,
206+
slugHint: string | undefined,
207+
): SessionReferenceResolution {
208+
if (resolution.kind !== "ambiguous" || resolution.truncated || !slugHint) {
209+
return resolution;
210+
}
211+
const matched = resolution.sessions.filter(
212+
(row) => controlUiSessionSlug(row.displayName) === slugHint,
213+
);
214+
return matched.length === 1 && matched[0] ? { kind: "unique", session: matched[0] } : resolution;
215+
}
216+
195217
function incompleteSessionReferenceResolution(
196218
search: SessionReferenceSearch,
197219
sessions: GatewaySessionRow[],
@@ -685,12 +707,15 @@ export async function loadChatRoute(
685707
...(canonicalLocationReady ? { canonicalLocationReady } : {}),
686708
};
687709
}
688-
const resolution = requireSessionReferenceResolution(
689-
await querySessionReference(
690-
context,
691-
{ kind: "short", value: target.shortId, agentId: target.agentId },
692-
signal,
710+
const resolution = narrowBySlugHint(
711+
requireSessionReferenceResolution(
712+
await querySessionReference(
713+
context,
714+
{ kind: "short", value: target.shortId, agentId: target.agentId },
715+
signal,
716+
),
693717
),
718+
target.slugHint,
694719
);
695720
if (resolution.kind === "not-found") {
696721
return notFound({ routeId: face });

‎ui/src/pages/chat/route-resolution.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,70 @@ describe("gateway-backed session route resolution", () => {
390390
]);
391391
});
392392

393+
it("settles a shared short-id prefix with the slug the link carries", async () => {
394+
const rows = [
395+
row({ key: "agent:roboclaw:thread:12345678-0aaa-4000-8000-000000000001" }),
396+
row({
397+
key: "agent:roboclaw:thread:12345678-0bbb-4000-8000-000000000002",
398+
displayName: "Deploy monitor",
399+
}),
400+
];
401+
const { context } = contextFor(() => result(rows));
402+
const loaded = await loadChatRoute(
403+
context,
404+
{ pathname: "/chat/roboclaw/deploy-monitor-12345678", search: "", hash: "" },
405+
"chat",
406+
new AbortController().signal,
407+
);
408+
409+
// Both ids start with 12345678; the slug says which one, so the short link still
410+
// resolves instead of bouncing to the chooser.
411+
expect(loaded).toMatchObject({ kind: "session", sessionKey: rows[1]?.key });
412+
});
413+
414+
it("keeps the chooser when the slug matches neither or both tied sessions", async () => {
415+
const rows = [
416+
row({ key: "agent:roboclaw:thread:12345678-0aaa-4000-8000-000000000001" }),
417+
row({ key: "agent:roboclaw:thread:12345678-0bbb-4000-8000-000000000002" }),
418+
];
419+
const { context } = contextFor(() => result(rows));
420+
for (const pathname of [
421+
// Stale slug: the session was renamed since the link was made.
422+
"/chat/roboclaw/an-old-name-12345678",
423+
// Both tied sessions share the slug, so it cannot decide.
424+
"/chat/roboclaw/default-mode-with-rare-surprises-12345678",
425+
]) {
426+
const loaded = await loadChatRoute(
427+
context,
428+
{ pathname, search: "", hash: "" },
429+
"chat",
430+
new AbortController().signal,
431+
);
432+
433+
expect(loaded).toMatchObject({ kind: "ambiguous", shortId: "12345678" });
434+
}
435+
});
436+
437+
it("does not settle a slug tie while the bounded search is incomplete", async () => {
438+
// Only one loaded row carries the slug, but pagination stopped early: an unexamined
439+
// page could hold the same prefix under the same name, so the chooser has to stand.
440+
const storedRow = row({
441+
key: "agent:roboclaw:thread:12345678-0aaa-4000-8000-000000000001",
442+
displayName: "Deploy monitor",
443+
});
444+
const { context } = contextFor(({ offset = 0 }) =>
445+
result(offset === 0 ? [storedRow] : [], { hasMore: true, nextOffset: offset + 20, offset }),
446+
);
447+
const loaded = await loadChatRoute(
448+
context,
449+
{ pathname: "/chat/roboclaw/deploy-monitor-12345678", search: "", hash: "" },
450+
"chat",
451+
new AbortController().signal,
452+
);
453+
454+
expect(loaded).toMatchObject({ kind: "ambiguous", shortId: "12345678", truncated: true });
455+
});
456+
393457
it("prefers an exact literal key over slug matches", async () => {
394458
const literal = row({
395459
key: "agent:roboclaw:default-mode-with-rare-surprises",

0 commit comments

Comments
 (0)