Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
feat(web): remember last me location
Gener34332d641e05
diff --git a/src/web/src/app/c/channels/layout.tsx b/src/web/src/app/c/channels/layout.tsxindex f85df5ebd..207da9aeb 100644--- a/src/web/src/app/c/channels/layout.tsx+++ b/src/web/src/app/c/channels/layout.tsx@@ -33,6 +33,7 @@ import {runAuthoritativeServerEject,} from "@/components/community/eject-server"import { clearLastChannel } from "@/lib/community/last-channel"+import { getLastMeLeaf, pickMeLandingLocation } from "@/lib/community/last-me-location"import { usePresence } from "@/hooks/community/use-server-panels"import {patchForumSidebarUnreadExact,@@ -271,7 +272,7 @@ export default function ServerLayout({ children }: { children: ReactNode }) {const goHome = useCallback(() => {setMobileZone("nav")- router.push("/c/me")+ router.push(pickMeLandingLocation(getLastMeLeaf()))}, [router])const goServer = useCallback(() => { setMobileZone("nav") }, [])diff --git a/src/web/src/app/c/me/layout.tsx b/src/web/src/app/c/me/layout.tsxindex ec1a38980..3fc33b943 100644--- a/src/web/src/app/c/me/layout.tsx+++ b/src/web/src/app/c/me/layout.tsx@@ -13,6 +13,15 @@ import { useFriends, useFriendsPresence } from "@/hooks/community/use-friends"import { communityKeys } from "@/lib/query-keys"import { useCommunityWsStore, useOnlineUserIds } from "@/stores/community/ws"import { resolveRowPresence } from "@/lib/community/presence"+import {+ clearLastMeLocation,+ getLastMeLeaf,+ ME_ROOT,+ meLeafFromPathname,+ pickMeLandingLocation,+ resolveMeLocationStatus,+ setLastMeLocation,+} from "@/lib/community/last-me-location"// DM-side layout. The DM subtree has no server settings, no channel sidebar,// and no `[serverId]` param — everything is scoped to the current user.@@ -21,7 +30,7 @@ export default function MeLayout({ children }: { children: ReactNode }) {const bp = useBreakpoint()const pathname = usePathname()const params = useParams<{ dmId?: string }>()- const { dms: rawDms, isLoading: dmsLoading } = useDms()+ const { dms: rawDms, isLoading: dmsLoading, isSuccess: dmsReady } = useDms()const onlineUserIds = useOnlineUserIds()const dms = useMemo(() =>@@ -59,6 +68,23 @@ export default function MeLayout({ children }: { children: ReactNode }) {const botsActive = pathname === "/c/me/bots"const friendsActive = !hasDm && !machinesActive && !botsActive+ const meLocationStatus = resolveMeLocationStatus({+ pathname,+ dmId: params.dmId,+ dmsReady,+ dmIds: rawDms.map((dm) => dm.id),+ })++ useEffect(() => {+ if (meLocationStatus === "remember") {+ setLastMeLocation(pathname)+ return+ }+ if (meLocationStatus !== "stale") return+ if (getLastMeLeaf() === meLeafFromPathname(pathname)) clearLastMeLocation()+ router.replace(ME_ROOT)+ }, [meLocationStatus, pathname, router])+const [mobileZone, setMobileZone] = useState<MobileZone>(() => (hasDm ? "messages" : "nav"))// Mirror channel sidebar-click behavior (channels/layout.tsx:226-236): do@@ -106,7 +132,7 @@ export default function MeLayout({ children }: { children: ReactNode }) {const goHome = useCallback(() => {setMobileZone("nav")- router.push("/c/me")+ router.push(pickMeLandingLocation(getLastMeLeaf()))}, [router])const goServer = useCallback(() => { setMobileZone("nav") }, [])diff --git a/src/web/src/lib/community/last-channel.ts b/src/web/src/lib/community/last-channel.tsindex 6c9e9b9ee..f352872f8 100644--- a/src/web/src/lib/community/last-channel.ts+++ b/src/web/src/lib/community/last-channel.ts@@ -10,6 +10,12 @@// mirroring `composer-draft.ts` — any failure degrades to "no memory", i.e.// exactly today's default-channel behavior, never a throw.+import {+ clearNavigationMemory,+ readNavigationMemory,+ writeNavigationMemory,+} from "./navigation-memory"+const PREFIX = "community:lastChannel:"export function lastChannelKey(serverId: string): string {@@ -17,22 +23,11 @@ export function lastChannelKey(serverId: string): string {}export function getLastChannel(serverId: string): string | null {- if (typeof window === "undefined") return null- try {- return localStorage.getItem(lastChannelKey(serverId))- } catch {- return null- }+ return readNavigationMemory(lastChannelKey(serverId))}export function setLastChannel(serverId: string, channelId: string): void {- if (typeof window === "undefined") return- try {- localStorage.setItem(lastChannelKey(serverId), channelId)- } catch {- // Best-effort: private-mode / quota failures just mean no memory this- // session, which falls back to the default channel — never surface it.- }+ writeNavigationMemory(lastChannelKey(serverId), channelId)}/**@@ -44,12 +39,7 @@ export function setLastChannel(serverId: string, channelId: string): void {* setters; a failure just means the stale id lingers, never a throw.*/export function clearLastChannel(serverId: string): void {- if (typeof window === "undefined") return- try {- localStorage.removeItem(lastChannelKey(serverId))- } catch {- /* best-effort — see setLastChannel */- }+ clearNavigationMemory(lastChannelKey(serverId))}/**diff --git a/src/web/src/lib/community/last-me-location.test.ts b/src/web/src/lib/community/last-me-location.test.tsnew file mode 100644index 000000000..49422fece--- /dev/null+++ b/src/web/src/lib/community/last-me-location.test.ts@@ -0,0 +1,110 @@+import { beforeEach, describe, expect, it, vi } from "vitest"+import {+ clearLastMeLocation,+ getLastMeLeaf,+ isRememberableMeLocation,+ lastMeLocationKey,+ pickMeLandingLocation,+ resolveMeLocationStatus,+ setLastMeLocation,+} from "./last-me-location"++describe("last-me-location", () => {+ let storage: Record<string, string>++ beforeEach(() => {+ storage = {}+ vi.unstubAllGlobals()+ vi.stubGlobal("window", {})+ vi.stubGlobal("localStorage", {+ getItem: vi.fn((key: string) => storage[key] ?? null),+ setItem: vi.fn((key: string, value: string) => { storage[key] = value }),+ removeItem: vi.fn((key: string) => { delete storage[key] }),+ })+ })++ it("uses me as the shared last-channel scope and stores only the leaf", () => {+ expect(lastMeLocationKey()).toBe("community:lastChannel:me")+ setLastMeLocation("/c/me/machines")+ expect(getLastMeLeaf()).toBe("machines")+ clearLastMeLocation()+ expect(getLastMeLeaf()).toBeNull()+ })++ it.each([+ "/c/me",+ "/c/me/machines",+ "/c/me/bots",+ "/c/me/dm_123",+ ])("accepts %s", (pathname) => {+ expect(isRememberableMeLocation(pathname)).toBe(true)+ })++ it.each([+ "/c/channels/srv_1",+ "/c/me/dm_1/messages",+ "/c/me/dm_1?seq=42",+ "/c/me/machines?reconnect=1",+ "/c/me/bots#owned",+ "/c/me/",+ ])("rejects %s", (pathname) => {+ expect(isRememberableMeLocation(pathname)).toBe(false)+ })++ it("does not overwrite a valid value with a query-bearing URL", () => {+ setLastMeLocation("/c/me/bots")+ setLastMeLocation("/c/me/dm_1?seq=42")+ expect(getLastMeLeaf()).toBe("bots")+ })++ it("falls back to friends for missing or dirty memory", () => {+ expect(pickMeLandingLocation(null)).toBe("/c/me")+ expect(pickMeLandingLocation("dm_1/messages")).toBe("/c/me")+ expect(pickMeLandingLocation("friends")).toBe("/c/me")+ expect(pickMeLandingLocation("dm_1")).toBe("/c/me/dm_1")+ })+})++describe("resolveMeLocationStatus", () => {+ it("remembers static surfaces without waiting for DMs", () => {+ expect(resolveMeLocationStatus({+ pathname: "/c/me/machines",+ dmId: undefined,+ dmsReady: false,+ dmIds: [],+ })).toBe("remember")+ })++ it("waits for an unresolved DM list", () => {+ expect(resolveMeLocationStatus({+ pathname: "/c/me/dm_1",+ dmId: "dm_1",+ dmsReady: false,+ dmIds: [],+ })).toBe("wait")+ })++ it("remembers a confirmed DM and marks a missing DM stale", () => {+ expect(resolveMeLocationStatus({+ pathname: "/c/me/dm_1",+ dmId: "dm_1",+ dmsReady: true,+ dmIds: ["dm_1"],+ })).toBe("remember")+ expect(resolveMeLocationStatus({+ pathname: "/c/me/dm_missing",+ dmId: "dm_missing",+ dmsReady: true,+ dmIds: ["dm_1"],+ })).toBe("stale")+ })++ it("ignores paths outside the supported me route shape", () => {+ expect(resolveMeLocationStatus({+ pathname: "/c/me/dm_1/messages",+ dmId: "dm_1",+ dmsReady: true,+ dmIds: ["dm_1"],+ })).toBe("ignore")+ })+})diff --git a/src/web/src/lib/community/last-me-location.ts b/src/web/src/lib/community/last-me-location.tsnew file mode 100644index 000000000..17b24661d--- /dev/null+++ b/src/web/src/lib/community/last-me-location.ts@@ -0,0 +1,68 @@+import {+ clearLastChannel,+ getLastChannel,+ lastChannelKey,+ setLastChannel,+} from "./last-channel"++const ME_SCOPE_ID = "me"+export const ME_ROOT = "/c/me"+const FRIENDS_LEAF = "friends"++export type MeLocationStatus = "ignore" | "wait" | "remember" | "stale"++function isSafeMeLeaf(leaf: string): boolean {+ return leaf !== "." && leaf !== ".." && !/[\/\\?#\s]/.test(leaf)+}++export function lastMeLocationKey(): string {+ return lastChannelKey(ME_SCOPE_ID)+}++export function meLeafFromPathname(pathname: string): string | null {+ if (pathname === ME_ROOT) return FRIENDS_LEAF+ const prefix = `${ME_ROOT}/`+ if (!pathname.startsWith(prefix)) return null+ const leaf = pathname.slice(prefix.length)+ return leaf && isSafeMeLeaf(leaf) ? leaf : null+}++export function isRememberableMeLocation(pathname: string): boolean {+ return meLeafFromPathname(pathname) !== null+}++export function getLastMeLeaf(): string | null {+ return getLastChannel(ME_SCOPE_ID)+}++export function setLastMeLocation(pathname: string): void {+ const leaf = meLeafFromPathname(pathname)+ if (leaf === null) return+ setLastChannel(ME_SCOPE_ID, leaf)+}++export function clearLastMeLocation(): void {+ clearLastChannel(ME_SCOPE_ID)+}++export function pickMeLandingLocation(lastLeaf: string | null): string {+ if (lastLeaf === null || !lastLeaf || !isSafeMeLeaf(lastLeaf)) return ME_ROOT+ return lastLeaf === FRIENDS_LEAF ? ME_ROOT : `${ME_ROOT}/${lastLeaf}`+}++export function resolveMeLocationStatus({+ pathname,+ dmId,+ dmsReady,+ dmIds,+}: {+ pathname: string+ dmId: string | undefined+ dmsReady: boolean+ dmIds: readonly string[]+}): MeLocationStatus {+ if (!isRememberableMeLocation(pathname)) return "ignore"+ if (!dmId) return "remember"+ if (!dmsReady) return "wait"+ return dmIds.includes(dmId) ? "remember" : "stale"+}diff --git a/src/web/src/lib/community/navigation-memory.test.ts b/src/web/src/lib/community/navigation-memory.test.tsnew file mode 100644index 000000000..cffa3fc35--- /dev/null+++ b/src/web/src/lib/community/navigation-memory.test.ts@@ -0,0 +1,50 @@+import { beforeEach, describe, expect, it, vi } from "vitest"+import {+ clearNavigationMemory,+ readNavigationMemory,+ writeNavigationMemory,+} from "./navigation-memory"++describe("navigation-memory", () => {+ let storage: Record<string, string>++ beforeEach(() => {+ storage = {}+ vi.unstubAllGlobals()+ vi.stubGlobal("window", {})+ vi.stubGlobal("localStorage", {+ getItem: vi.fn((key: string) => storage[key] ?? null),+ setItem: vi.fn((key: string, value: string) => {+ storage[key] = value+ }),+ removeItem: vi.fn((key: string) => {+ delete storage[key]+ }),+ })+ })++ it("round-trips and clears a value", () => {+ writeNavigationMemory("nav:key", "/c/me/machines")+ expect(readNavigationMemory("nav:key")).toBe("/c/me/machines")+ clearNavigationMemory("nav:key")+ expect(readNavigationMemory("nav:key")).toBeNull()+ })++ it("degrades safely when storage access throws", () => {+ vi.stubGlobal("localStorage", {+ getItem: vi.fn(() => { throw new Error("SecurityError") }),+ setItem: vi.fn(() => { throw new Error("QuotaExceededError") }),+ removeItem: vi.fn(() => { throw new Error("SecurityError") }),+ })+ expect(readNavigationMemory("nav:key")).toBeNull()+ expect(() => writeNavigationMemory("nav:key", "value")).not.toThrow()+ expect(() => clearNavigationMemory("nav:key")).not.toThrow()+ })++ it("is SSR-safe", () => {+ vi.stubGlobal("window", undefined)+ expect(readNavigationMemory("nav:key")).toBeNull()+ expect(() => writeNavigationMemory("nav:key", "value")).not.toThrow()+ expect(() => clearNavigationMemory("nav:key")).not.toThrow()+ })+})diff --git a/src/web/src/lib/community/navigation-memory.ts b/src/web/src/lib/community/navigation-memory.tsnew file mode 100644index 000000000..ec83eec8a--- /dev/null+++ b/src/web/src/lib/community/navigation-memory.ts@@ -0,0 +1,26 @@+export function readNavigationMemory(key: string): string | null {+ if (typeof window === "undefined") return null+ try {+ return localStorage.getItem(key)+ } catch {+ return null+ }+}++export function writeNavigationMemory(key: string, value: string): void {+ if (typeof window === "undefined") return+ try {+ localStorage.setItem(key, value)+ } catch {+ return+ }+}++export function clearNavigationMemory(key: string): void {+ if (typeof window === "undefined") return+ try {+ localStorage.removeItem(key)+ } catch {+ return+ }+}