// What the board draws, in order: the database's package tools (owner's// ruling, 21 Oct 2026: tools are records only), each a tile drawn from its// home table's definition. The app ships no tool, so there is no tile of its// own beside them. Pure, so the order is settled by tests.importCottonKitimportFoundationenumBoardItem: Equatable, Identifiable {/// A package tool and its home table's definition.case dataTile(ToolDefinition, TableDefinition)var id: String {switchself {case .dataTile(let tool, _): return"data-" + tool.id } }/// The tool's home table.var table: String? {switchself {case .dataTile(_, let definition): return definition.id } }}enumBoardLayout {/// The definition a tool's tile and route are drawn from: its first table,/// else — a tool of views alone, which owns no table (Birthdays over/// People's person, Web pages over Documents' document; a package may own/// zero tables, 8ef9dbd3a) — the table its default view reads. Before/// 21 Oct 2026 such a tool drew no tile at all, so it never reached the fan.staticfunc home(for tool: ToolDefinition, in definitions: TableDefinitions) -> TableDefinition? {iflet first = tool.tables.first, let home = definitions[first] { return home }let views = tool.views.filter { $0.partition == nil }guardlet view = views.first(where: \.isDefault) ?? views.first, case .string(let table)? = view.read["table"] else { returnnil }return definitions[table] }/// The tables a tool's sections page draws: every table it lists but the/// supporting ones — a table the home table's page shows as a `list`/// section is read on each home record's page, never as a section of its own.staticfunc tables(for tool: ToolDefinition, in definitions: TableDefinitions) -> [TableDefinition] {let supporting = supportingTables(for: tool, in: definitions)return tool.tables.filter { !supporting.contains($0) }.compactMap { definitions[$0] } }/// Where a tool opens straight to a list: its home table (`tables.first`)/// when every other table it lists is a `list` section of the home's page —/// Quotas' readings on each account's page — exactly as a one-table tool./// A table that is neither keeps the tool's sections page — unless the/// tool declares views: then it opens on its default view with its views/// on the ring, whatever its table count (architect, 21 Oct 2026: Money/// opens on Past, never on its tables' cards).staticfunc directTable(for tool: ToolDefinition, in definitions: TableDefinitions) -> TableDefinition? {guardlet home = home(for: tool, in: definitions) else { returnnil }if opensOnViews(tool, in: definitions) { return home }let supporting = supportingTables(for: tool, in: definitions)return tool.tables.dropFirst().allSatisfy(supporting.contains) ? home : nil }/// Whether the tool declares views: it opens on its default view, read by/// `ToolViewRoute`, never on its home table's plain list.staticfunc opensOnViews(_ tool: ToolDefinition, in definitions: TableDefinitions) -> Bool {let keyed = definitions.all.first { table in table.tool.map { ($0.key ?? table.id) == tool.id } ?? false }return (keyed?.tool?.views ?? []).contains { $0.partition == nil } || tool.views.contains { $0.partition == nil } }/// The tables, other than the home, that the home table's page lists.staticfunc supportingTables(for tool: ToolDefinition, in definitions: TableDefinitions) -> Set<String> {guardlet home = home(for: tool, in: definitions) else { return [] }let listed = Set(home.page.compactMap { $0.list?.table })returnSet(tool.tables.dropFirst().filter(listed.contains)) }/// A table's own list: the Settings route that draws a Settings source's/// list, else the table's list (`/lists/[table]`), as the tool's page/// opens it through its declared home.staticfunc listRoute(for definition: TableDefinition) -> Route { listRoute(forTable: definition.id) }/// The same, by the table's id — a table tile on Home, which the core/// names by its table.staticfunc listRoute(forTable table: String) -> Route {SettingsTables.route(forTable: table) ?? .tableList(table: table, filter: nil, title: nil, query: nil) }/// Whether a route asked for arrives at the tile drawn for `tool`: its/// own route, or the bare tool URL, which reaches the tile even when an/// older route names a different table.staticfunc arrives(_ asked: Route, at drawn: Route?, tool: String?) -> Bool { asked == drawn || tool.map { asked.path == "/\($0)" } == true }/// THE route rule for a tool's tile — the tile tap, Find and a deep link/// all open a tool here. A kit tool's home table names the hand-built/// screen that draws it (`tool.route`: Todos' `/todos`, Calendar's/// `/calendar`), and that screen opens; else its home table's list/// ([`directTable`]), or — a package of tables that stand on their own —/// its own board (`/tools/board/[id]`). A tool over a table another/// tool owns (`ownsHome`: Birthdays over People's person) opens its/// own page, drawn from its own views (`ToolBoardRoute`), never the/// owner's screen or the table's list.staticfunc route(for tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Route {if !ownsHome(tool, in: definitions, tools: tools) { return .toolBoard(id: tool.id) }iflet declared = declaredRoute(for: tool, in: definitions, tools: tools) { return declared }// Its board route draws the default view (`ToolBoardRoute`).if opensOnViews(tool, in: definitions) { return .toolBoard(id: tool.id) }return directTable(for: tool, in: definitions).map(listRoute(for:)) ?? .toolBoard(id: tool.id) }/// RULE: a table belongs to one tool — the one its tool block is keyed to, or, where no tool carries that key (Notebook's note table is keyed `notes`), a tool that lists it; another tool reading the same table (an imported Birthdays over People's person) does not own it, so it never draws the owner's hand-built screen (lane ringviews, 21 Oct 2026; the core's `tool_views::owns`). A tool with no home table in hand owns nothing to give away.staticfunc ownsHome(_ tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Bool {guardlet home = home(for: tool, in: definitions) else { returntrue }let key = home.tool?.key ?? home.idreturn key == tool.id || (!tools.contains { $0.id == key } && tools.contains { $0.id == tool.id && $0.tables.contains(home.id) }) }/// The screen a tool's home table's definition names (`tool.route`); nil/// when it names none, and the tool opens its list or its board — and nil/// for a tool that does not own that table: the screen draws its owner.staticfunc declaredRoute(for tool: ToolDefinition, in definitions: TableDefinitions, tools: [ToolDefinition]) -> Route? {guard ownsHome(tool, in: definitions, tools: tools) else { returnnil }return home(for: tool, in: definitions).flatMap(declaredRoute(of:)) }/// The screen a table's definition names for its tool; nil for none.staticfunc declaredRoute(of definition: TableDefinition) -> Route? { definition.tool?.route.flatMap(Route.parse) }/// Where a table's tile opens — the same rule by the table: the screen/// its definition names, else its list.staticfunc route(forTable definition: TableDefinition) -> Route { declaredRoute(of: definition) ?? listRoute(for: definition) }/// The tiles in Home's order; Tools is drawn last by the grid itself (B1).staticfunc items(definitions: TableDefinitions, dataTools: ToolDefinitions, order: [String] = []) -> [BoardItem] { laid(definitions: definitions, dataTools: dataTools, order: order, only: false) }/// Home's tiles, and nothing else, in the tiles' draw order — the ones/// with something to show in the person's order, then the empty ones/// (`HomeTiles.keys`; the crate orders them). A tool whose list is no/// tile is not drawn.staticfunc items(definitions: TableDefinitions, dataTools: ToolDefinitions, tiles: HomeTiles) -> [BoardItem] { items(definitions: definitions, dataTools: dataTools, keys: tiles.keys) }/// The tools named by `keys`, in that order, and no other.staticfunc items(definitions: TableDefinitions, dataTools: ToolDefinitions, keys: [String]) -> [BoardItem] { laid(definitions: definitions, dataTools: dataTools, order: keys, only: true) }privatestaticfunc laid(definitions: TableDefinitions, dataTools: ToolDefinitions, order: [String], only: Bool) -> [BoardItem] {let items = dataTools.all.compactMap { tool in home(for: tool, in: definitions).map { BoardItem.dataTile(tool, $0) } }guard !order.isEmpty || only else { return items }func key(_ item: BoardItem) -> String {switch item {case .dataTile(let tool, _): return tool.id } }let positions = Dictionary(order.enumerated().map { ($0.element, $0.offset) }, uniquingKeysWith: { first, _ in first })return items.enumerated().filter { !only || positions[key($0.element)] != nil }.sorted {let a = positions[key($0.element)] ?? Int.maxlet b = positions[key($1.element)] ?? Int.maxreturn a == b ? $0.offset < $1.offset : a < b }.map(\.element) }}