feat: per-case default session group

Case settings gains a "Default session group" selector (Ungrouped plus every
named tab group of the owner). A new session of the case lands in that group
in the same versioned layout write that registers it, so it is drawn in place
with no Ungrouped-to-group jump.

- Stored per owner and case in state.json (caseDefaultGroups), as the group's
  stable id or null for Ungrouped; read and written only through
  TabLayoutService under the owner lock.
- Precedence in sessionCreated: explicit tabGroupId, then the parent
  placement, then the case default, else Ungrouped. A deleted or foreign id
  never fails a spawn and is forgotten only after the creating write lands.
- POST /api/quick-start (local, linked, remote, Docker) and
  /api/ralph-loop/start pass their case; POST /api/sessions infers it
  lexically from workingDir. The create response carries tabLayout when a
  case default placed the session.
- GET/PUT /api/cases/:name/default-group; deleting a case (or an admin
  deleting a user's case folder) forgets the value.
- The selector mirrors desktop and phone, reverts on a refused save, and is
  redrawn from every adopted tab layout; zh-CN strings added.
This commit is contained in:
Aamer Akhter committed 2026-10-22 03:42:42 -04:00
1 parent 39077281d1
commit 74c980413a
24 files changed
+1362 -26

No files matched your search

+91 -3
View File
@@ -50,6 +50,11 @@ interface TabLayoutServiceDeps {
* from then on; on, it keeps following its parent until moved by hand.
*/
childrenFollowParent?(): boolean | Promise<boolean>;
/**
* Where per-case default session groups live (StateStore). Optional so a
* harness without it simply never applies a case default.
*/
caseDefaults?: Pick<StateStore, 'getCaseDefaultGroup' | 'setCaseDefaultGroup' | 'deleteCaseDefaultGroup'>;
now?: () => string;
}
@@ -78,6 +83,17 @@ export interface RemovedTabLayoutSession {
/** Where a newly created session asked to be placed. Advisory: an unknown group is ignored. */
export interface SessionPlacementHint {
tabGroupId?: string;
/**
* The case the session was started for. Its default session group (if any)
* is the fallback when neither `tabGroupId` nor a parent places the session.
*/
caseName?: string;
}
/** A case's default session group plus the owner's current named groups to choose from. */
export interface CaseDefaultGroupView {
groupId: string | null;
groups: Array<{ id: string; name: string }>;
}
/**
@@ -158,6 +174,20 @@ function pinCreatedChild(layout: TabLayout, sessionId: string, metadata: readonl
return moveRef(pinned, { kind: 'session', id: sessionId }, { groupId: group?.id ?? null, index: to }, metadata);
}
/**
* Does the just-created session have a parent that places it (right after that
* parent)? Mirrors the conditions under which normalization and
* {@link pinCreatedChild} treat it as a child.
*/
function hasPlacingParent(layout: TabLayout, sessionId: string, metadata: readonly TabRefMetadata[]): boolean {
const item = metadata.find((fact) => fact.kind === 'session' && fact.id === sessionId);
const parentId = item?.parentSessionId;
if (!item?.ownerValid || !parentId) return false;
const parentValid = metadata.some((fact) => fact.kind === 'session' && fact.id === parentId && fact.ownerValid);
const refs = [...layout.groups.flatMap((group) => group.refs), ...layout.ungrouped];
return parentValid && refs.some((ref) => ref.kind === 'session' && ref.id === parentId);
}
/** Move a just-created session to the end of `groupId`, or leave the layout alone. */
function placeCreatedSession(
layout: TabLayout,
@@ -550,17 +580,31 @@ export class TabLayoutService {
): Promise<TabLayout> {
return this.withOwner(owner, async () => {
const follow = sessionId ? (await this.deps.childrenFollowParent?.()) === true : true;
const caseName = sessionId ? placement.caseName : undefined;
const caseDefault = caseName ? this.deps.caseDefaults?.getCaseDefaultGroup(owner, caseName) : undefined;
let staleCaseDefault = false;
const rollback = register?.();
try {
const groupId = placement.tabGroupId;
const adjust =
sessionId && (groupId || !follow)
sessionId && (groupId || !follow || typeof caseDefault === 'string')
? (layout: TabLayout, metadata: readonly TabRefMetadata[]) => {
const placed = groupId ? placeCreatedSession(layout, sessionId, groupId, metadata) : layout;
// Precedence: explicit group > parent placement > case default > none.
const explicit = groupId && layout.groups.some((group) => group.id === groupId) ? groupId : undefined;
let placed = explicit ? placeCreatedSession(layout, sessionId, explicit, metadata) : layout;
if (!explicit && typeof caseDefault === 'string') {
if (!layout.groups.some((group) => group.id === caseDefault)) staleCaseDefault = true;
else if (!hasPlacingParent(layout, sessionId, metadata))
placed = placeCreatedSession(layout, sessionId, caseDefault, metadata);
}
return follow ? placed : pinCreatedChild(placed, sessionId, metadata);
}
: undefined;
return await this.getUnlocked(owner, adjust);
const layout = await this.getUnlocked(owner, adjust);
// A default whose group was deleted is ignored above and forgotten here,
// only once the creation's own write has landed.
if (staleCaseDefault && caseName) this.deps.caseDefaults?.deleteCaseDefaultGroup(owner, caseName);
return layout;
} catch (error) {
if (typeof rollback === 'function') rollback();
throw error;
@@ -568,6 +612,50 @@ export class TabLayoutService {
});
}
/**
* A case's default session group and the owner's named groups to choose from.
* A stored id that is no longer one of this owner's groups is forgotten and
* reported as `null` (Ungrouped), which is also where creation would put it.
*/
async caseDefaultGroup(owner: string, caseName: string): Promise<CaseDefaultGroupView> {
return this.withOwner(owner, async () => {
const { authoritative } = await this.prepareUnlocked(owner);
const groups = authoritative.groups.map((group) => ({ id: group.id, name: group.name }));
const stored = this.deps.caseDefaults?.getCaseDefaultGroup(owner, caseName);
if (typeof stored === 'string' && !groups.some((group) => group.id === stored)) {
this.deps.caseDefaults?.deleteCaseDefaultGroup(owner, caseName);
return { groupId: null, groups };
}
return { groupId: typeof stored === 'string' ? stored : null, groups };
});
}
/**
* Set a case's default session group: one of this owner's current groups, or
* `null` for Ungrouped. Only future creations read it; nothing is moved.
* Throws {@link TabLayoutValidationError} for an id not in the owner's layout.
*/
async setCaseDefaultGroup(owner: string, caseName: string, groupId: string | null): Promise<CaseDefaultGroupView> {
return this.withOwner(owner, async () => {
const caseDefaults = this.deps.caseDefaults;
if (!caseDefaults) throw new Error('Case default groups are not available');
const { authoritative } = await this.prepareUnlocked(owner);
const groups = authoritative.groups.map((group) => ({ id: group.id, name: group.name }));
if (groupId !== null && !groups.some((group) => group.id === groupId)) {
throw new TabLayoutValidationError('Unknown tab group');
}
caseDefaults.setCaseDefaultGroup(owner, caseName, groupId);
return { groupId, groups };
});
}
/** Forget a case's default session group (the case was deleted or unlinked). */
async forgetCaseDefaultGroup(owner: string, caseName: string): Promise<void> {
await this.withOwner(owner, async () => {
this.deps.caseDefaults?.deleteCaseDefaultGroup(owner, caseName);
});
}
/** Reconcile one completed saved-webview creation into one versioned mutation. */
async webviewCreated(owner: string): Promise<TabLayout> {
return this.get(owner);