@mulmoclaude/core 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/assets/helps/error-recovery.md +55 -0
  2. package/dist/collection/core/errorMessage.d.ts +1 -4
  3. package/dist/collection/core/ontologyGraph.d.ts +58 -0
  4. package/dist/collection/core/uiTypes.d.ts +7 -0
  5. package/dist/collection/index.cjs +81 -10
  6. package/dist/collection/index.cjs.map +1 -1
  7. package/dist/collection/index.d.ts +1 -0
  8. package/dist/collection/index.js +74 -4
  9. package/dist/collection/index.js.map +1 -1
  10. package/dist/collection/registry/server/index.cjs +8 -7
  11. package/dist/collection/registry/server/index.cjs.map +1 -1
  12. package/dist/collection/registry/server/index.js +3 -2
  13. package/dist/collection/registry/server/index.js.map +1 -1
  14. package/dist/collection/server/index.cjs +3 -3
  15. package/dist/collection/server/index.js +3 -3
  16. package/dist/collection/server/ontology.d.ts +2 -24
  17. package/dist/collection/server/util.d.ts +1 -2
  18. package/dist/collection-watchers/index.cjs +5 -4
  19. package/dist/collection-watchers/index.cjs.map +1 -1
  20. package/dist/collection-watchers/index.js +4 -3
  21. package/dist/collection-watchers/index.js.map +1 -1
  22. package/dist/{discovery-QUi_7aNG.js → discovery-B9Vfojyy.js} +2 -2
  23. package/dist/{discovery-QUi_7aNG.js.map → discovery-B9Vfojyy.js.map} +1 -1
  24. package/dist/{discovery-cwtfyDxl.cjs → discovery-BxbJy2VN.cjs} +2 -2
  25. package/dist/{discovery-cwtfyDxl.cjs.map → discovery-BxbJy2VN.cjs.map} +1 -1
  26. package/dist/errors-7P5eMOSX.cjs +30 -0
  27. package/dist/errors-7P5eMOSX.cjs.map +1 -0
  28. package/dist/errors-eid6Mes3.js +19 -0
  29. package/dist/errors-eid6Mes3.js.map +1 -0
  30. package/dist/feeds/index.cjs +2 -2
  31. package/dist/feeds/index.js +2 -2
  32. package/dist/feeds/server/index.cjs +4 -4
  33. package/dist/feeds/server/index.js +4 -4
  34. package/dist/google/index.cjs +8 -18
  35. package/dist/google/index.cjs.map +1 -1
  36. package/dist/google/index.js +3 -13
  37. package/dist/google/index.js.map +1 -1
  38. package/dist/google/util.d.ts +2 -4
  39. package/dist/{ids-P2T89Gou.js → ids-D1M1T6KJ.js} +26 -3
  40. package/dist/ids-D1M1T6KJ.js.map +1 -0
  41. package/dist/{ids-Dl9Ij63K.cjs → ids-DWmHjm17.cjs} +37 -2
  42. package/dist/ids-DWmHjm17.cjs.map +1 -0
  43. package/dist/{ingestTypes-C-IQB4nw.js → ingestTypes-Ci4eOgv-.js} +2 -2
  44. package/dist/{ingestTypes-C-IQB4nw.js.map → ingestTypes-Ci4eOgv-.js.map} +1 -1
  45. package/dist/{ingestTypes-DZpAC3N_.cjs → ingestTypes-DbuQNuK6.cjs} +2 -2
  46. package/dist/{ingestTypes-DZpAC3N_.cjs.map → ingestTypes-DbuQNuK6.cjs.map} +1 -1
  47. package/dist/{promptSafety-BOc1B3im.cjs → promptSafety-0ZKHX-6J.cjs} +5 -39
  48. package/dist/promptSafety-0ZKHX-6J.cjs.map +1 -0
  49. package/dist/{promptSafety-B5KPkaNY.js → promptSafety-RE1SPxBN.js} +3 -25
  50. package/dist/promptSafety-RE1SPxBN.js.map +1 -0
  51. package/dist/remote-host/server/index.cjs +4 -4
  52. package/dist/remote-host/server/index.cjs.map +1 -1
  53. package/dist/remote-host/server/index.js +1 -1
  54. package/dist/scheduler/index.cjs +2 -4
  55. package/dist/scheduler/index.cjs.map +1 -1
  56. package/dist/scheduler/index.js +1 -3
  57. package/dist/scheduler/index.js.map +1 -1
  58. package/dist/{server-B7mtx4Cd.cjs → server-DOGKfUuD.cjs} +9 -17
  59. package/dist/server-DOGKfUuD.cjs.map +1 -0
  60. package/dist/{server-CNLZZONa.js → server-sGNe-msA.js} +9 -11
  61. package/dist/server-sGNe-msA.js.map +1 -0
  62. package/dist/text-CaAh9ZSO.cjs +27 -0
  63. package/dist/text-CaAh9ZSO.cjs.map +1 -0
  64. package/dist/text-CjDbP2O1.js +22 -0
  65. package/dist/text-CjDbP2O1.js.map +1 -0
  66. package/dist/utils/errors.d.ts +2 -0
  67. package/dist/utils/index.cjs +6 -0
  68. package/dist/utils/index.d.ts +2 -0
  69. package/dist/utils/index.js +3 -0
  70. package/dist/utils/text.d.ts +12 -0
  71. package/dist/whisper/index.cjs +7 -9
  72. package/dist/whisper/index.cjs.map +1 -1
  73. package/dist/whisper/index.js +1 -3
  74. package/dist/whisper/index.js.map +1 -1
  75. package/dist/whisper/internal.d.ts +1 -1
  76. package/dist/workspace-setup/index.js +12 -3
  77. package/dist/workspace-setup/index.js.map +1 -1
  78. package/package.json +7 -1
  79. package/dist/errorMessage--LvXe_Jx.cjs +0 -16
  80. package/dist/errorMessage--LvXe_Jx.cjs.map +0 -1
  81. package/dist/errorMessage-DIdhheHk.js +0 -11
  82. package/dist/errorMessage-DIdhheHk.js.map +0 -1
  83. package/dist/ids-Dl9Ij63K.cjs.map +0 -1
  84. package/dist/ids-P2T89Gou.js.map +0 -1
  85. package/dist/promptSafety-B5KPkaNY.js.map +0 -1
  86. package/dist/promptSafety-BOc1B3im.cjs.map +0 -1
  87. package/dist/server-B7mtx4Cd.cjs.map +0 -1
  88. package/dist/server-CNLZZONa.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"promptSafety-BOc1B3im.cjs","names":[],"sources":["../src/collection/core/fieldText.ts","../src/collection/core/actionVisible.ts","../src/collection/core/backlinks.ts","../src/collection/core/where.ts","../src/collection/core/completion.ts","../src/collection/core/dynamicIcon.ts","../src/collection/core/derivedFormula.ts","../src/collection/core/deriveAll.ts","../src/collection/core/calendarGrid.ts","../src/collection/core/promptSafety.ts"],"sourcesContent":["// Turning a record field into text.\n//\n// `CollectionItem` is `Record<string, unknown>`, so a field holds whatever the\n// record's JSON had — including arrays and objects (real workspace data has\n// plenty: weather `hourly`, GeoJSON `geometry`, `sites` lists). Bare\n// `String(value)` on one of those yields `\"[object Object]\"`, which then gets\n// compared, matched or displayed as if it were a value. Nothing throws; a\n// predicate just silently stops matching, or the UI shows `[object Object]`.\n//\n// These two helpers are the only sanctioned way to read a field as text. The\n// rule they encode was already in `itemLabelOf`: accept primitives, let\n// everything else fall through to the caller's fallback.\n\n/** A field value that has a meaningful text form. Dates are included because\n * a JSON record can carry one once it has been revived. */\nconst isTextable = (value: unknown): value is string | number | boolean | Date =>\n typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\" || value instanceof Date;\n\n/** The field's text, or `null` when it has no meaningful one (absent, or an\n * array/object that would stringify to `\"[object Object]\"`).\n *\n * Returning `null` rather than `\"\"` keeps \"the field is empty\" distinct from\n * \"the field can't be text\" — a matcher must not treat an object-valued field\n * as an empty string and match `\"\"`. */\nexport function fieldTextOrNull(value: unknown): string | null {\n if (value === undefined || value === null) return null;\n if (!isTextable(value)) return null;\n if (value instanceof Date) {\n // `new Date(\"nonsense\")` is still `instanceof Date`, and `toISOString()`\n // throws `RangeError` on it. This helper sits on the match, sort and\n // display paths, so one unparseable date in one record would take the whole\n // render down — the loud version of the bug this module exists to prevent.\n return Number.isNaN(value.getTime()) ? null : value.toISOString();\n }\n return String(value);\n}\n\n/** The field's text, or `fallback` (default `\"\"`) when it has none. Use where\n * a string is required and an empty one is a safe stand-in — display, sort\n * keys, CSV cells. Where the distinction matters, use {@link fieldTextOrNull}. */\nexport function fieldText(value: unknown, fallback = \"\"): string {\n return fieldTextOrNull(value) ?? fallback;\n}\n","// Pure `when`-predicate visibility helpers for schema-driven\n// collections — used both for action buttons and for conditionally\n// shown fields. Kept as their own module (no Vue) so CollectionView\n// can stay thin and the match semantics are pinned by unit tests.\n// Domain-free: the host compares the stringified record value against\n// the allowed set with no knowledge of what the field means.\n\nimport { fieldTextOrNull } from \"./fieldText\";\n\n/** A `when` predicate: render only when the open record's `field`\n * (stringified) is one of `in`. Shared shape for action buttons and\n * conditionally visible fields. */\nexport interface WhenPredicate {\n field: string;\n in: string[];\n}\n\n/** Core matcher:\n * - no `when` ⇒ always true (visible);\n * - otherwise true only when `record[when.field]` is present and its\n * stringified value is one of `when.in`.\n * A missing/undefined/null field is treated as \"not a match\"\n * (hidden), so a status-gated target never shows on a record that\n * lacks the status. */\nexport function whenMatches(when: WhenPredicate | undefined, record: Record<string, unknown>): boolean {\n if (!when) return true;\n // `fieldTextOrNull` rather than `String(...)`: an array/object field would\n // stringify to \"[object Object]\" and could match a `when.in` entry by\n // accident. No text ⇒ no match, same as an absent field.\n const text = fieldTextOrNull(record[when.field]);\n if (text === null) return false;\n return when.in.includes(text);\n}\n\n/** Minimal shape this helper needs from an action — the optional state\n * gate, whichever name its kind uses: `when` on the seeded kinds\n * (chat/agent), `require` on mutate. Accepts the full CollectionAction\n * union (each variant declares at most one of the two). */\nexport interface ActionWithWhen {\n when?: WhenPredicate;\n require?: WhenPredicate;\n}\n\n/** True when the action's button should render against `record` — and,\n * server-side, whether it may RUN (visibility is the authorization\n * rule, for every kind). */\nexport function actionVisible(action: ActionWithWhen, record: Record<string, unknown>): boolean {\n return whenMatches(action.when ?? action.require, record);\n}\n\n/** The run key naming one in-flight `kind: \"agent\"` action button —\n * written by the server's dispatch guard, read back by the client from\n * the detail response's `runningActions` to drive the spinner. ONE\n * builder (isomorphic) so the two sides can't drift. Collection-level\n * and per-record actions live in distinct namespaces so an id collision\n * between `actions` and `collectionActions` can't alias. */\nexport function agentActionRunKey(actionId: string, itemId?: string): string {\n return itemId === undefined ? `collection/${actionId}` : `item/${itemId}/${actionId}`;\n}\n\n/** Minimal shape this helper needs from a field spec — just its\n * optional `when` predicate. Accepts the full FieldSpec too. */\nexport interface FieldWithWhen {\n when?: WhenPredicate;\n}\n\n/** True when the field should render against `record`. A field with\n * no `when` is always shown; otherwise it's shown only when the\n * record matches (e.g. hide a rating field until `visited` is true).\n * Purely presentational — a hidden field's stored value is never\n * altered, so toggling the gate back on restores it. */\nexport function fieldVisible(field: FieldWithWhen, record: Record<string, unknown>): boolean {\n return whenMatches(field.when, record);\n}\n","// Pure resolution for `backlinks` fields (plan step ② of\n// plans/done/collection-ontology.md): the display-only reverse side of `ref`.\n// Both the server enrichment (`server/derive.ts`) and the client detail\n// view derive the row set through THESE helpers, so the LLM (getItems)\n// and the user (record panel) always see the same rows — the same\n// single-implementation rule `deriveAll` follows for formulas. No zod,\n// no I/O; safe for the browser barrel.\n\nimport { whenMatches } from \"./actionVisible\";\nimport { fieldTextOrNull } from \"./fieldText\";\nimport type { CollectionFieldSpec, CollectionItem } from \"./schema\";\n\n/** The `backlinks` member of the field-spec union. */\nexport type BacklinksFieldSpec = Extract<CollectionFieldSpec, { type: \"backlinks\" }>;\n\n/** The SOURCE records whose `via` field stores `recordId` (compared as\n * strings, like every ref deref), with the optional `filter` applied —\n * in the source items' given order. Fail-soft by construction: a `via`\n * key that doesn't exist on the source records simply matches nothing.\n * Callers pass DERIVED source records, so a `filter`/`display` on a\n * derived column works when its formula is SELF-CONTAINED (an invoice\n * `total` = sum over its own line items); a source column that derefs\n * yet another collection stays absent — the same each-record-derives-\n * against-itself rule ref targets follow. */\nexport function backlinkRows(spec: Pick<BacklinksFieldSpec, \"via\" | \"filter\">, recordId: string, sourceItems: CollectionItem[]): CollectionItem[] {\n if (!recordId) return [];\n // A `via` field holding an array/object has no id to compare — skip it\n // rather than testing \"[object Object]\" against the record id.\n return sourceItems.filter((item) => fieldTextOrNull(item[spec.via]) === recordId && whenMatches(spec.filter, item));\n}\n\n/** Project one backlink row to the keys consumers surface: the source\n * collection's primaryKey (rows must stay addressable — it's the link\n * target) plus the declared `display` columns. Keys the row doesn't\n * carry are simply absent, mirroring `projectFields` in getItems. */\nexport function projectBacklinkRow(row: CollectionItem, display: readonly string[], primaryKey: string): CollectionItem {\n const keys = display.includes(primaryKey) ? display : [primaryKey, ...display];\n return Object.fromEntries(keys.filter((key) => key in row).map((key) => [key, row[key]]));\n}\n\n/** The `rollup` member of the field-spec union. */\nexport type RollupFieldSpec = Extract<CollectionFieldSpec, { type: \"rollup\" }>;\n\n/** Numeric coercion shared by the strict record lint (`./recordZ`) and\n * rollup sums: a plain number, or a non-blank numeric string (renderers\n * coerce those via `Number(...)`, so they display fine). Anything else —\n * arrays (`[]` stringifies to `\"\"` = 0, `[42]` to `\"42\"`), booleans,\n * objects — is NaN. Lives here (zod-free) so both consumers share one\n * definition of \"numeric\". */\nexport function coerceNumeric(value: unknown): number {\n if (typeof value === \"number\") return value;\n if (typeof value === \"string\" && value.trim() !== \"\") return Number(value);\n return NaN;\n}\n\n/** The rollup aggregate over the matching source rows (plan step ⑤):\n * `count` = how many rows match; `sum` = the total of `column` over\n * them, skipping non-numeric / absent values (a partially-filled source\n * still sums what's there). An EMPTY match set is a real 0 — the\n * fail-soft null lives at the caller, for a source collection that\n * couldn't be resolved at all. Same derived-source-records contract as\n * `backlinkRows`: pass records derived against themselves, so summing a\n * self-contained derived column (an invoice `total`) works. */\nexport function rollupValue(spec: Pick<RollupFieldSpec, \"via\" | \"filter\" | \"op\" | \"column\">, recordId: string, sourceItems: CollectionItem[]): number {\n const rows = backlinkRows(spec, recordId, sourceItems);\n if (spec.op === \"count\") return rows.length;\n let total = 0;\n for (const row of rows) {\n const value = coerceNumeric(spec.column === undefined ? undefined : row[spec.column]);\n if (Number.isFinite(value)) total += value;\n }\n return total;\n}\n","// Pure SQL-like `where` predicate for `dynamicIcon` (see\n// `DynamicIconSource.where` / `DynamicIconRule.where` in `./schema`).\n// An AND of typed conditions — richer than the single-field `CollectionWhen`\n// used elsewhere (fields/actions via `./actionVisible`), which stays as-is\n// for its existing callers. No fs, no host state. The condition SHAPES are\n// derived from the zod source of truth in `./schemaZ` (type-only imports —\n// this evaluator stays zod-free at runtime).\n\nimport type { z } from \"zod\";\nimport type { ValueRefZ, WhereCondZ, WhereZ } from \"./schemaZ\";\n\n/** Reads the comparison value from a field instead of a schema literal:\n * - `record` set → another record: `recordsById[record][field]` (e.g. a\n * `_config` singleton's `defaultCity`, following a per-user setting);\n * - `record` omitted → the SAME record being matched (field-to-field, e.g.\n * `spent > budget`). */\nexport type ValueRef = z.infer<typeof ValueRefZ>;\n\n/** One typed condition: `record[field] <op> value`. The comparison value is\n * either a literal `value` (a plain string for every op except `in`, which\n * takes the allowed set) or a `valueFrom` reference resolved against the\n * `recordsById` map passed to `matchesWhere`. Exactly one of the two is\n * expected — enforced by zod at the schema boundary (`./schemaZ`), not\n * here. */\nexport type WhereCond = z.infer<typeof WhereCondZ>;\n\n/** Comparison operators one `WhereCond` may apply to `record[field]`. */\nexport type WhereOp = WhereCond[\"op\"];\n\n/** A `where` clause is the AND of its conditions — every one must match. */\nexport type Where = z.infer<typeof WhereZ>;\n\n/** True when `record[field]` is absent (`undefined`/`null`) — the only case\n * where `ne` and every other op disagree on the result. */\nfunction isMissing(raw: unknown): boolean {\n return raw === undefined || raw === null;\n}\n\n/** The effective comparison value for `cond`: its literal `value`, or — for\n * a `valueFrom` reference — the target field read out of `recordsById`.\n * `undefined` means UNRESOLVED (no such record, or the field on it is\n * missing); the caller must treat that as \"never matches\", not as a\n * literal `undefined` value to compare against. */\nfunction resolveValue(cond: WhereCond, record: Record<string, unknown>, recordsById: Record<string, Record<string, unknown>>): string | string[] | undefined {\n if (!cond.valueFrom) return cond.value;\n const { record: refRecord, field } = cond.valueFrom;\n const target = refRecord === undefined ? record : recordsById[refRecord];\n const raw = target?.[field];\n return isMissing(raw) ? undefined : String(raw);\n}\n\nfunction matchesNumericOp(operator: \"gt\" | \"gte\" | \"lt\" | \"lte\", left: number, right: number): boolean {\n if (operator === \"gt\") return left > right;\n if (operator === \"gte\") return left >= right;\n if (operator === \"lt\") return left < right;\n return left <= right;\n}\n\n/** `Number(\"\")` / `Number(\" \")` are `0`, not `NaN`, so treat a blank string\n * as non-numeric explicitly — an empty field must fail a numeric compare,\n * not read as zero. */\nfunction toNumber(raw: string): number {\n return raw.trim() === \"\" ? NaN : Number(raw);\n}\n\nfunction matchesNumeric(operator: \"gt\" | \"gte\" | \"lt\" | \"lte\", raw: string, value: string | string[]): boolean {\n if (Array.isArray(value)) return false;\n const left = toNumber(raw);\n const right = toNumber(value);\n if (Number.isNaN(left) || Number.isNaN(right)) return false;\n return matchesNumericOp(operator, left, right);\n}\n\n/** True when the present string `raw` satisfies `operator` against the\n * resolved `value` (field known to exist — MISSING is handled by the\n * caller before this runs, and an UNRESOLVED `valueFrom` never reaches\n * here either). */\nfunction matchesPresent(operator: WhereOp, raw: string, value: string | string[]): boolean {\n switch (operator) {\n case \"eq\":\n return raw === String(value);\n case \"ne\":\n return raw !== String(value);\n case \"in\":\n return Array.isArray(value) && value.includes(raw);\n case \"contains\":\n return raw.includes(String(value));\n case \"gt\":\n case \"gte\":\n case \"lt\":\n case \"lte\":\n return matchesNumeric(operator, raw, value);\n default:\n return false;\n }\n}\n\n/** True when `record` satisfies one condition, given `recordsById` to\n * resolve a `valueFrom` reference. Two independent MISSING cases, checked\n * in order:\n * - `record[cond.field]` absent (`undefined`/`null`) → matches only `ne`\n * (vacuously true — \"not equal to X\" holds when there's no value at\n * all); every other op is false. Unchanged from the literal-`value`\n * behaviour, regardless of whether `valueFrom` would also resolve.\n * - the resolved comparison value is UNRESOLVED (a `valueFrom` whose\n * target record/field doesn't exist) → false for EVERY op, including\n * `ne` — a broken reference must never spuriously match. */\nfunction matchesCond(cond: WhereCond, record: Record<string, unknown>, recordsById: Record<string, Record<string, unknown>>): boolean {\n const raw = record[cond.field];\n if (isMissing(raw)) return cond.op === \"ne\";\n const value = resolveValue(cond, record, recordsById);\n if (value === undefined) return false;\n return matchesPresent(cond.op, String(raw), value);\n}\n\n/** True when `record` satisfies every condition in `where` (AND). An empty\n * `where` matches everything. `recordsById` — the source collection's\n * records keyed by primaryKey — resolves any `valueFrom` reference;\n * omitted (default `{}`) for callers with no cross-record lookups, in\n * which case every `valueFrom` condition is UNRESOLVED and so never\n * matches. */\nexport function matchesWhere(where: Where, record: Record<string, unknown>, recordsById: Record<string, Record<string, unknown>> = {}): boolean {\n return where.every((cond) => matchesCond(cond, record, recordsById));\n}\n","// The \"is this record done?\" predicate — THE single implementation,\n// shared by the notification reconciler (bell clearing, collection-watchers),\n// spawn (successor-predicate fallback, ../server/spawn.ts), and view-side\n// completion filters. Zod-free and I/O-free like the rest of `core/`, so\n// it is browser-safe through the collection barrel.\n//\n// Two completion forms (see `CollectionSchemaZ`'s completion refine):\n// - legacy pair: `completionField` names a stored field and\n// `completionDoneValues` lists the values that mean done —\n// done ⇔ `String(item[completionField])` ∈ `completionDoneValues`.\n// - flag form: `completionField` names a `flag` field (and\n// `completionDoneValues` is absent) — done ⇔ the flag's `where`\n// matches. Evaluated directly against the raw record here (NOT read\n// from a materialized value) because callers like the reconciler and\n// spawn work on records straight off disk, before any `deriveAll`\n// enrichment. That raw evaluation is CORRECT BY CONSTRUCTION: a\n// schema-level refine rejects a completion flag whose `where`\n// references computed fields, so every condition reads stored data.\n\nimport { fieldTextOrNull } from \"./fieldText\";\nimport { matchesWhere, type Where } from \"./where\";\n\n/** The slice of a parsed schema the done predicate reads — minimal\n * structural shape (like `DerivableSchema`) so the client and server\n * `CollectionSchema` types both satisfy it as-is. */\nexport interface CompletionSchemaView {\n /** Optional so legacy-pair callers (and their test fixtures) that\n * never consult field specs keep working; only the flag form needs\n * to look the completion field up. */\n fields?: Record<string, { type: string; where?: Where }>;\n completionField?: string;\n completionDoneValues?: readonly string[];\n}\n\n/** True iff the schema declares completion tracking AND `item` is done\n * under whichever completion form the schema uses (see module doc). */\nexport function itemIsDone(schema: CompletionSchemaView, item: Record<string, unknown>): boolean {\n const { completionField, completionDoneValues } = schema;\n if (!completionField) return false;\n const spec = schema.fields?.[completionField];\n if (spec?.type === \"flag\" && spec.where) return matchesWhere(spec.where, item);\n if (!completionDoneValues) return false;\n // An array/object field has no text form; treat it as \"not done\" rather than\n // letting \"[object Object]\" match a configured done-value.\n const text = fieldTextOrNull(item[completionField]);\n if (text === null) return false;\n return completionDoneValues.includes(text);\n}\n","// Pure resolver for a collection's dynamic launcher-shortcut icon (see\n// `CollectionSchema.dynamicIcon`). Selects one \"source\" record from a\n// (possibly cross-collection, optionally `where`-filtered) records pool,\n// then maps it through a first-match-wins rules list to an icon name.\n// No fs, no host state — the server-side compute\n// (`packages/core/src/collection/server/dynamicIcon.ts`) loads the source\n// collection's records and calls these.\n\nimport { matchesWhere } from \"./where\";\nimport type { CollectionFieldSpec, CollectionItem, CollectionSchema, DynamicIconSource, DynamicIconSpec } from \"./schema\";\n\n/** The record with the greatest `String(record[field])` (localeCompare) —\n * ties keep the first-seen record (stable left-to-right `reduce`). */\nfunction latestByField(pool: CollectionItem[], field: string): CollectionItem {\n return pool.reduce((latest, candidate) => (String(candidate[field] ?? \"\").localeCompare(String(latest[field] ?? \"\")) > 0 ? candidate : latest));\n}\n\n/** Reduce `records` to the one record that decides the effective icon, per\n * `source`'s `where` filter + `from` strategy:\n * - pool = `source.where`-filtered records, or every record when unset;\n * - an empty pool resolves to `null` (no source record → fallback);\n * - `from: \"first\"` / `\"when\"` → the first pool record (storage order);\n * - `from: \"latest\"` (default), with `orderBy` given → the pool record\n * whose `String(record[orderBy])` sorts highest;\n * - `from: \"latest\"`, with no `orderBy` → the last pool record.\n * `recordsById` (the source collection's records keyed by primaryKey)\n * resolves any `valueFrom` reference inside `source.where`; omitted for\n * callers with no cross-record lookups. */\nexport function selectDynamicRecord(\n records: CollectionItem[],\n source: DynamicIconSource,\n orderBy: string | undefined,\n recordsById: Record<string, CollectionItem> = {},\n): CollectionItem | null {\n const { where } = source;\n const pool = where ? records.filter((record) => matchesWhere(where, record, recordsById)) : records;\n if (pool.length === 0) return null;\n if (source.from === \"first\" || source.from === \"when\") return pool[0];\n return orderBy ? latestByField(pool, orderBy) : pool[pool.length - 1];\n}\n\n/** Map a resolved source record to the effective icon: `spec.fallback`\n * (or the collection's own static `icon`) when there's no record or no\n * rule matches; otherwise the `icon` of the first rule whose `where`\n * matches the record. `recordsById` resolves any `valueFrom` reference\n * inside a rule's `where`, same as `selectDynamicRecord`. */\nexport function resolveIcon(\n record: CollectionItem | null,\n spec: DynamicIconSpec,\n staticIcon: string,\n recordsById: Record<string, CollectionItem> = {},\n): string {\n const fallback = spec.fallback ?? staticIcon;\n if (!record) return fallback;\n const matched = spec.rules.find((rule) => matchesWhere(rule.where, record, recordsById));\n return matched ? matched.icon : fallback;\n}\n\nconst isDateLikeField = (field: CollectionFieldSpec): boolean => field.type === \"date\" || field.type === \"datetime\";\n\n/** The first field key (declaration order) whose type is `date` or\n * `datetime` — the default `orderBy` for `from: \"latest\"` when a\n * `DynamicIconSource` doesn't name one. `undefined` when the schema has\n * no date-like field. */\nexport function firstDateField(schema: CollectionSchema): string | undefined {\n return Object.entries(schema.fields).find(([, field]) => isDateLikeField(field))?.[0];\n}\n","// Tiny expression evaluator for the `derived` field type on\n// schema-driven collections (see plans/done/feat-mc-invoice.md).\n//\n// Grammar (recursive-descent, no precedence climbing — six\n// non-terminals total):\n//\n// expr := term (('+' | '-') term)*\n// term := factor (('*' | '/') factor)*\n// factor := number | sumCall | refAccess | identifier | '(' expr ')'\n// sumCall:= 'sum' '(' sumArg ')'\n// sumArg := tableCol (('*' | '/') tableCol)* // e.g. lineItems[].quantity * lineItems[].rate\n// tableCol := identifier '[]' '.' identifier\n// refAccess := identifier '.' identifier // e.g. ticker.price — deref a ref field into its target record\n//\n// `identifier` accepts top-level field names (single segment).\n// Inside `sumArg`, identifiers are the `<table>[].col` form.\n// A two-segment `<field>.<col>` at factor level is a *ref deref*:\n// `<field>` must be a `ref`-typed field on this record (its stored\n// value is the target item's slug), and `<col>` is a numeric column\n// read from that target record. The caller resolves the target into\n// `ctx.refs` (it owns the schema + the loaded target collection);\n// the evaluator stays pure and never does I/O.\n//\n// What's deliberately NOT supported (and would parse-error rather\n// than silently misbehave):\n// - String literals, boolean operators, comparisons, conditionals\n// - Nested function calls beyond `sum(...)`\n// - Anything in the record that isn't a number / table-of-objects\n//\n// All evaluation is pure — no eval(), no Function constructor.\n// Returns `null` on any failure (parse error, unbound identifier,\n// non-finite arithmetic). The caller renders `null` as em-dash in\n// the table cell + form display.\n\nexport interface FormulaContext {\n /** The record being evaluated. For derived fields in the form,\n * this is the live draft (text + table both converted via the\n * same `draftToRecord` pipeline). For the main table cell,\n * this is the persisted item. */\n record: Record<string, unknown>;\n /** Resolved ref-target records for THIS row, keyed by the local\n * `ref` field name. The caller (which has the schema + the linked\n * collection's items loaded) maps each ref field's stored slug to\n * the full target record and passes it here, so a `<field>.<col>`\n * formula can read a numeric column off the referenced record\n * (e.g. `shares * ticker.price`). A missing key or `null` value\n * (unknown field / dangling slug) makes that deref evaluate to\n * NaN → the whole formula returns `null` → em-dash, consistent\n * with every other failure mode. Absent ⇒ no refs available. */\n refs?: Record<string, Record<string, unknown> | null>;\n}\n\nexport function evaluateDerived(formula: string, ctx: FormulaContext): number | null {\n let tokens: Token[];\n try {\n tokens = tokenize(formula);\n } catch {\n return null;\n }\n // eslint-disable-next-line @typescript-eslint/no-use-before-define -- Parser class is defined later in the file (grouped with its AST + evaluator); evaluateDerived runs after module init so the TDZ concern doesn't apply.\n const parser = new Parser(tokens);\n let ast: Node;\n try {\n ast = parser.parseExpr();\n if (!parser.atEnd()) return null; // trailing junk\n } catch {\n return null;\n }\n const value = evaluate(ast, ctx);\n return Number.isFinite(value) ? value : null;\n}\n\n// ─── Tokens ────────────────────────────────────────────────\n\ntype TokenKind = \"number\" | \"ident\" | \"(\" | \")\" | \"+\" | \"-\" | \"*\" | \"/\" | \"[]\" | \".\";\n\ninterface Token {\n kind: TokenKind;\n value?: string | number;\n}\n\nconst SINGLE_CHAR_PUNCT = new Set<TokenKind>([\"(\", \")\", \"+\", \"-\", \"*\", \"/\", \".\"]);\n\ninterface Cursor {\n input: string;\n index: number;\n}\n\nfunction consumeWhitespace(cur: Cursor): boolean {\n const char = cur.input[cur.index];\n if (char === \" \" || char === \"\\t\" || char === \"\\n\") {\n cur.index++;\n return true;\n }\n return false;\n}\n\nfunction consumeNumber(cur: Cursor): Token | null {\n const char = cur.input[cur.index] ?? \"\";\n const next = cur.input[cur.index + 1] ?? \"\";\n if (!isDigit(char) && !(char === \".\" && isDigit(next))) return null;\n let raw = \"\";\n while (cur.index < cur.input.length) {\n const here = cur.input[cur.index] ?? \"\";\n if (!isDigit(here) && here !== \".\") break;\n raw += here;\n cur.index++;\n }\n const num = Number(raw);\n if (!Number.isFinite(num)) throw new Error(\"bad number\");\n return { kind: \"number\", value: num };\n}\n\nfunction consumeIdent(cur: Cursor): Token | null {\n const char = cur.input[cur.index] ?? \"\";\n if (!isIdentStart(char)) return null;\n let raw = \"\";\n while (cur.index < cur.input.length && isIdentChar(cur.input[cur.index] ?? \"\")) {\n raw += cur.input[cur.index];\n cur.index++;\n }\n return { kind: \"ident\", value: raw };\n}\n\nfunction consumePunct(cur: Cursor): Token | null {\n const char = cur.input[cur.index] ?? \"\";\n if (char === \"[\" && cur.input[cur.index + 1] === \"]\") {\n cur.index += 2;\n return { kind: \"[]\" };\n }\n if (SINGLE_CHAR_PUNCT.has(char as TokenKind)) {\n cur.index++;\n return { kind: char as TokenKind };\n }\n return null;\n}\n\nfunction tokenize(input: string): Token[] {\n const tokens: Token[] = [];\n const cur: Cursor = { input, index: 0 };\n while (cur.index < input.length) {\n if (consumeWhitespace(cur)) continue;\n // Number FIRST so a leading-dot literal (`.25`) isn't split by\n // the `.` punctuation branch.\n const numTok = consumeNumber(cur);\n if (numTok) {\n tokens.push(numTok);\n continue;\n }\n const punctTok = consumePunct(cur);\n if (punctTok) {\n tokens.push(punctTok);\n continue;\n }\n const identTok = consumeIdent(cur);\n if (identTok) {\n tokens.push(identTok);\n continue;\n }\n throw new Error(`unexpected char ${input[cur.index]}`);\n }\n return tokens;\n}\n\nfunction isDigit(char: string): boolean {\n return char >= \"0\" && char <= \"9\";\n}\nfunction isIdentStart(char: string): boolean {\n return (char >= \"a\" && char <= \"z\") || (char >= \"A\" && char <= \"Z\") || char === \"_\";\n}\nfunction isIdentChar(char: string): boolean {\n return isIdentStart(char) || isDigit(char);\n}\n\n// ─── AST + Parser ───────────────────────────────────────────\n\ntype Node =\n | { kind: \"num\"; value: number }\n | { kind: \"ident\"; name: string }\n | { kind: \"ref\"; field: string; col: string }\n | { kind: \"binop\"; operator: \"+\" | \"-\" | \"*\" | \"/\"; left: Node; right: Node }\n | { kind: \"sum\"; arg: SumArg };\n\ninterface SumArg {\n // factors multiplied/divided together; each is a (tableName, colName) ref into a row.\n factors: { table: string; col: string }[];\n /** Operators between factors: length = factors.length - 1; each\n * is \"*\" or \"/\". For a single-factor sum (`sum(lineItems[].amount)`)\n * this is empty. */\n operators: (\"*\" | \"/\")[];\n}\n\nclass Parser {\n private cursor = 0;\n constructor(private readonly tokens: Token[]) {}\n\n atEnd(): boolean {\n return this.cursor >= this.tokens.length;\n }\n private peek(): Token | undefined {\n return this.tokens[this.cursor];\n }\n private consume(): Token {\n const tok = this.tokens[this.cursor++];\n if (!tok) throw new Error(\"unexpected end of input\");\n return tok;\n }\n private expect(kind: TokenKind): Token {\n const tok = this.consume();\n if (tok.kind !== kind) throw new Error(`expected ${kind}, got ${tok.kind}`);\n return tok;\n }\n\n parseExpr(): Node {\n let left = this.parseTerm();\n while (this.peek()?.kind === \"+\" || this.peek()?.kind === \"-\") {\n const operator = this.consume().kind as \"+\" | \"-\";\n const right = this.parseTerm();\n left = { kind: \"binop\", operator, left, right };\n }\n return left;\n }\n\n private parseTerm(): Node {\n let left = this.parseFactor();\n while (this.peek()?.kind === \"*\" || this.peek()?.kind === \"/\") {\n const operator = this.consume().kind as \"*\" | \"/\";\n const right = this.parseFactor();\n left = { kind: \"binop\", operator, left, right };\n }\n return left;\n }\n\n private parseFactor(): Node {\n const tok = this.peek();\n if (!tok) throw new Error(\"unexpected end in factor\");\n if (tok.kind === \"number\") {\n this.consume();\n return { kind: \"num\", value: tok.value as number };\n }\n if (tok.kind === \"(\") {\n this.consume();\n const inner = this.parseExpr();\n this.expect(\")\");\n return inner;\n }\n if (tok.kind === \"ident\") {\n const name = (tok.value as string) ?? \"\";\n // sum(...) — only function call we support\n if (name === \"sum\" && this.tokens[this.cursor + 1]?.kind === \"(\") {\n this.consume(); // ident\n this.expect(\"(\");\n const arg = this.parseSumArg();\n this.expect(\")\");\n return { kind: \"sum\", arg };\n }\n this.consume(); // ident\n // ref deref: `<field>.<col>` (e.g. ticker.price). The table-row\n // form `<table>[].col` only appears inside sum(), so a `.`\n // immediately after a top-level ident is unambiguously a ref\n // dereference here.\n if (this.peek()?.kind === \".\") {\n this.consume(); // '.'\n const col = this.expect(\"ident\");\n return { kind: \"ref\", field: name, col: col.value as string };\n }\n return { kind: \"ident\", name };\n }\n throw new Error(`unexpected token ${tok.kind} in factor`);\n }\n\n private parseSumArg(): SumArg {\n const factors: { table: string; col: string }[] = [];\n const operators: (\"*\" | \"/\")[] = [];\n factors.push(this.parseTableCol());\n while (this.peek()?.kind === \"*\" || this.peek()?.kind === \"/\") {\n const operator = this.consume().kind as \"*\" | \"/\";\n operators.push(operator);\n factors.push(this.parseTableCol());\n }\n return { factors, operators };\n }\n\n private parseTableCol(): { table: string; col: string } {\n const tableTok = this.expect(\"ident\");\n this.expect(\"[]\");\n this.expect(\".\");\n const colTok = this.expect(\"ident\");\n return { table: tableTok.value as string, col: colTok.value as string };\n }\n}\n\n// ─── Evaluator ──────────────────────────────────────────────\n\nfunction evaluate(node: Node, ctx: FormulaContext): number {\n if (node.kind === \"num\") return node.value;\n if (node.kind === \"ident\") {\n const raw = ctx.record[node.name];\n return toFiniteNumber(raw);\n }\n if (node.kind === \"ref\") {\n // `<field>.<col>`: read `col` off the resolved target record the\n // caller put in ctx.refs. Unknown field / dangling slug → null →\n // NaN, so the whole formula fails soft to an em-dash.\n const target = ctx.refs?.[node.field] ?? null;\n if (!target) return Number.NaN;\n return toFiniteNumber(target[node.col]);\n }\n if (node.kind === \"binop\") {\n const left = evaluate(node.left, ctx);\n const right = evaluate(node.right, ctx);\n return applyBinop(node.operator, left, right);\n }\n if (node.kind === \"sum\") {\n return evaluateSum(node.arg, ctx);\n }\n // Exhaustive — TS narrows above branches but throw keeps runtime honest.\n throw new Error(`unknown node`);\n}\n\nfunction applyBinop(operator: \"+\" | \"-\" | \"*\" | \"/\", left: number, right: number): number {\n if (!Number.isFinite(left) || !Number.isFinite(right)) return Number.NaN;\n if (operator === \"+\") return left + right;\n if (operator === \"-\") return left - right;\n if (operator === \"*\") return left * right;\n // operator === \"/\"\n if (right === 0) return Number.NaN;\n return left / right;\n}\n\nfunction evaluateSum(arg: SumArg, ctx: FormulaContext): number {\n if (arg.factors.length === 0) return 0;\n const tableName = arg.factors[0].table;\n // All factors must reference the SAME table (you can't multiply\n // a row from lineItems against a row from another table — the\n // semantics would be ambiguous). Reject mismatch.\n for (const factor of arg.factors) {\n if (factor.table !== tableName) return Number.NaN;\n }\n const rows = ctx.record[tableName];\n if (!Array.isArray(rows)) return 0;\n let total = 0;\n for (const row of rows) {\n if (!row || typeof row !== \"object\") continue;\n let product = toFiniteNumber((row as Record<string, unknown>)[arg.factors[0].col]);\n if (!Number.isFinite(product)) return Number.NaN;\n for (let i = 1; i < arg.factors.length; i++) {\n const value = toFiniteNumber((row as Record<string, unknown>)[arg.factors[i].col]);\n if (!Number.isFinite(value)) return Number.NaN;\n product = applyBinop(arg.operators[i - 1], product, value);\n }\n total += product;\n }\n return total;\n}\n\nfunction toFiniteNumber(value: unknown): number {\n if (typeof value === \"number\") return Number.isFinite(value) ? value : Number.NaN;\n if (typeof value === \"string\" && value.length > 0) {\n const num = Number(value);\n return Number.isFinite(num) ? num : Number.NaN;\n }\n return Number.NaN;\n}\n","// The derived-field saturation loop for schema-driven collections,\n// extracted from `composables/collections/useCollectionRendering.ts` so\n// the server (manageCollection getItems enrichment) and the client\n// (table cells, form display) evaluate formulas through ONE\n// implementation — if the two ever diverged, the UI and the LLM would\n// disagree on a number. Pure module: no Vue, no I/O.\n//\n// Like `actionVisible.ts`, the input types are minimal structural\n// shapes so both the client `FieldSpec`/`CollectionSchema`\n// (src/components/collectionTypes.ts) and the server\n// `CollectionFieldSpec`/`CollectionSchema`\n// (server/workspace/collections/types.ts) satisfy them as-is.\n\nimport { evaluateDerived, type FormulaContext } from \"./derivedFormula\";\nimport { matchesWhere, type Where } from \"./where\";\n\n/** Minimal field shape the derive loop needs — accepts both the client\n * FieldSpec and the server CollectionFieldSpec. */\nexport interface DerivableFieldSpec {\n type: string;\n /** When type === \"ref\": slug of the target collection. */\n to?: string;\n /** When type === \"derived\": formula evaluated against the record. */\n formula?: string;\n /** When type === \"flag\": predicate matched against the record. */\n where?: Where;\n}\n\n/** Minimal schema shape: just the ordered field map. */\nexport interface DerivableSchema {\n fields: Record<string, DerivableFieldSpec>;\n}\n\nexport type DerivableRecord = Record<string, unknown>;\n\n/** Per-target-collection cache of loaded referenced records:\n * target collection slug → item slug → full record. Mirrors the\n * client's `RefRecordCache` / the server's enrichment loader. */\nexport type DeriveRefRecords = Record<string, Record<string, DerivableRecord>>;\n\n/** Map each `ref` field's stored slug to its loaded target record (or\n * null when dangling / not loaded), keyed by the LOCAL field name —\n * the shape `evaluateDerived` reads for `<field>.<col>` derefs. */\nexport function resolveRowRefs(schema: DerivableSchema, record: DerivableRecord, refRecords: DeriveRefRecords): NonNullable<FormulaContext[\"refs\"]> {\n const refs: NonNullable<FormulaContext[\"refs\"]> = {};\n for (const [key, field] of Object.entries(schema.fields)) {\n if (field.type !== \"ref\" || !field.to) continue;\n const slug = record[key];\n refs[key] = typeof slug === \"string\" ? (refRecords[field.to]?.[slug] ?? null) : null;\n }\n return refs;\n}\n\n/** True for the field types the saturation loop below (re)computes:\n * `derived` formulas and `flag` predicates. */\nfunction isLoopComputed(field: DerivableFieldSpec): boolean {\n return field.type === \"derived\" || field.type === \"flag\";\n}\n\n/** The value one loop-computed field takes against the current\n * `enriched` record: a `derived` formula result (`null` = failed) or a\n * `flag` predicate match (total — always a boolean). `undefined` for\n * every other field type (nothing to compute). */\nfunction computeFieldValue(\n field: DerivableFieldSpec,\n enriched: DerivableRecord,\n refs: NonNullable<FormulaContext[\"refs\"]>,\n): number | boolean | null | undefined {\n if (field.type === \"derived\" && field.formula) return evaluateDerived(field.formula, { record: enriched, refs });\n if (field.type === \"flag\" && field.where) return matchesWhere(field.where, enriched);\n return undefined;\n}\n\n/** Evaluate every `derived` and `flag` field against `base`, saturating\n * so a computed field can read another one computed in an earlier pass\n * (`subtotal → tax → total` converges in ≤ field-count passes; a flag\n * may read a derived value, or another flag via its stringified\n * boolean). Cycles can't loop forever — passes are bounded by the\n * number of computed fields and the loop breaks as soon as a pass\n * changes nothing. Failed formulas stay ABSENT (the UI renders them as\n * em-dash); flags are total (always true/false). Returns a copy;\n * `base` is never mutated.\n *\n * Computed keys already present in `base` are stripped before\n * evaluation: computed output is host-truth, never persisted-input\n * fallback. A record JSON can carry a stale (or forged) computed value\n * — raw Write/Edit, legacy data — and without the strip, a failing\n * formula would silently surface that value as if the host computed\n * it. */\nexport function deriveAll(schema: DerivableSchema, base: DerivableRecord, refRecords: DeriveRefRecords): DerivableRecord {\n const computedKeys = new Set(\n Object.entries(schema.fields)\n .filter(([, field]) => isLoopComputed(field))\n .map(([key]) => key),\n );\n const enriched: DerivableRecord = Object.fromEntries(Object.entries(base).filter(([key]) => !computedKeys.has(key)));\n const refs = resolveRowRefs(schema, base, refRecords);\n for (let pass = 0; pass < computedKeys.size; pass++) {\n let mutated = false;\n for (const [key, field] of Object.entries(schema.fields)) {\n const next = computeFieldValue(field, enriched, refs);\n if (next !== undefined && next !== null && enriched[key] !== next) {\n enriched[key] = next;\n mutated = true;\n }\n }\n if (!mutated) break;\n }\n return enriched;\n}\n","// Pure, deterministic helpers for the collection calendar view: parse\n// `date`-field values, build a month grid, and bucket records onto the\n// days they cover. No `Date.now()` / `new Date()` (argless) here — every\n// function takes its inputs explicitly so the logic is unit-testable\n// without faking the clock. All internal arithmetic runs in UTC (which\n// has no DST), so fixed 86_400_000 ms steps never skip or double a day.\n\nconst MS_PER_DAY = 86_400_000;\nconst ISO_DATE_RE = /^(\\d{4})-(\\d{2})-(\\d{2})$/;\n// A two-digit field (hours / minutes / seconds) of a clock value.\nconst TWO_DIGIT_RE = /^\\d{2}$/;\n// A single clock token inside a free-form `time` string field (e.g. the\n// \"14:00-17:00\" / \"17:00-\" / \"16:30\" / \"終日\" shapes seen in user data).\nconst CLOCK_RE = /(\\d{1,2}):(\\d{2})/g;\n// Range separators we tolerate between two clock tokens: ASCII hyphen, en/em\n// dash, tilde, and the Japanese wave dashes.\nconst RANGE_SEP_RE = /[-–—~〜~]/;\n\n/** Minutes in a full day — the timeline's vertical extent. */\nexport const MINUTES_PER_DAY = 1440;\n\n/** A civil date triple. `month` is 1-12 (NOT the 0-based `Date` month). */\nexport interface Ymd {\n year: number;\n month: number;\n day: number;\n}\n\n/** One cell of the 6×7 month grid. */\nexport interface DayCell {\n ymd: Ymd;\n /** False for the leading/trailing days that belong to the adjacent\n * month (rendered greyed). */\n inMonth: boolean;\n /** Canonical `YYYY-MM-DD` key for this cell. */\n key: string;\n}\n\n/** A record placed on the calendar: the inclusive `[start, end]` span of\n * days it covers. `end === start` for a single-day record. `startMin` /\n * `endMin` are minutes-of-day for the time-allocation (day) view, resolved\n * from either a `datetime` field's clock or a separate time-string field.\n * `null` means \"no clock\" — `startMin === null && endMin === null` is an\n * all-day record; a non-null `startMin` with a null `endMin` is a\n * point-in-time record (rendered as a single line). */\nexport interface RecordSpan<T> {\n item: T;\n start: Ymd;\n end: Ymd;\n startMin: number | null;\n endMin: number | null;\n}\n\nfunction pad2(value: number): string {\n return String(value).padStart(2, \"0\");\n}\n\n/** Canonical `YYYY-MM-DD` string for a civil date. */\nexport function ymdKey(ymd: Ymd): string {\n return `${String(ymd.year).padStart(4, \"0\")}-${pad2(ymd.month)}-${pad2(ymd.day)}`;\n}\n\n/** Strictly parse a `YYYY-MM-DD` string into a civil date, rejecting\n * anything that isn't a real calendar day (e.g. `2026-02-30`, `2026-13-01`).\n * Returns null for non-strings and malformed values so callers can route\n * records with no usable date into the \"no date\" tray rather than crash. */\nexport function parseIsoDate(value: unknown): Ymd | null {\n if (typeof value !== \"string\") return null;\n const match = ISO_DATE_RE.exec(value.trim());\n if (!match) return null;\n const year = Number(match[1]);\n const month = Number(match[2]);\n const day = Number(match[3]);\n // Round-trip through a UTC Date to reject impossible days: a value the\n // Date constructor rolls over (Feb 30 → Mar 2) won't match back.\n const probe = new Date(Date.UTC(year, month - 1, day));\n if (probe.getUTCFullYear() !== year || probe.getUTCMonth() !== month - 1 || probe.getUTCDate() !== day) return null;\n return { year, month, day };\n}\n\n/** Minutes-of-day for an `HH:MM` pair, or null when out of range. */\nfunction clockToMinutes(hours: number, minutes: number): number | null {\n if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null;\n return hours * 60 + minutes;\n}\n\n/** Strictly parse a `YYYY-MM-DDTHH:MM` (optional `:SS`) datetime into its\n * civil date and minutes-of-day. Returns null for anything that isn't a real\n * calendar day or a valid 24h clock. */\nexport function parseIsoDateTime(value: unknown): { ymd: Ymd; minutes: number } | null {\n if (typeof value !== \"string\") return null;\n const trimmed = value.trim();\n const tIndex = trimmed.indexOf(\"T\");\n if (tIndex === -1) return null;\n const ymd = parseIsoDate(trimmed.slice(0, tIndex));\n if (!ymd) return null;\n // `HH:MM` with an optional `:SS` the browser appends for non-zero seconds.\n const parts = trimmed.slice(tIndex + 1).split(\":\");\n if (parts.length < 2 || parts.length > 3 || !parts.every((part) => TWO_DIGIT_RE.test(part))) return null;\n const minutes = clockToMinutes(Number(parts[0]), Number(parts[1]));\n if (minutes === null) return null;\n return { ymd, minutes };\n}\n\n/** Civil date from either a `YYYY-MM-DD` or a `YYYY-MM-DDTHH:MM` value, so the\n * month grid buckets date-only and datetime anchors alike. */\nexport function dateOf(value: unknown): Ymd | null {\n return parseIsoDate(value) ?? parseIsoDateTime(value)?.ymd ?? null;\n}\n\n/** Minutes-of-day from a datetime value, or null for date-only / invalid. */\nfunction timeOf(value: unknown): number | null {\n return parseIsoDateTime(value)?.minutes ?? null;\n}\n\n/** Parse a free-form time-string field into start/end minutes-of-day.\n * Handles the common shapes in user data:\n * \"14:00-17:00\" → { start: 840, end: 1020 } (range → block)\n * \"17:00-\" → { start: 1020, end: null } (open end → single line)\n * \"16:30\" → { start: 990, end: null } (point in time → single line)\n * \"終日\" / \"\" → null (no clock → all-day)\n * Returns null when no clock token is parseable. */\nexport function parseTimeRange(value: unknown): { startMin: number | null; endMin: number | null } | null {\n if (typeof value !== \"string\") return null;\n const text = value.trim();\n if (!text) return null;\n const tokens = [...text.matchAll(CLOCK_RE)];\n if (tokens.length === 0) return null;\n const minutesOf = (match: RegExpMatchArray): number | null => clockToMinutes(Number(match[1]), Number(match[2]));\n // No separator → a single point in time (start only).\n if (!RANGE_SEP_RE.test(text)) {\n const startMin = minutesOf(tokens[0]);\n return startMin === null ? null : { startMin, endMin: null };\n }\n // Separator present → assign each token to the side of the first separator.\n const sepIndex = text.search(RANGE_SEP_RE);\n let startMin: number | null = null;\n let endMin: number | null = null;\n for (const token of tokens) {\n if ((token.index ?? 0) < sepIndex) startMin = minutesOf(token);\n else endMin = minutesOf(token);\n }\n // A start-less range (\"-09:00\") has no anchor on the timeline → treat as\n // unparseable so the record falls back to the all-day strip.\n if (startMin === null) return null;\n return { startMin, endMin };\n}\n\nfunction ymdToUtcMs(ymd: Ymd): number {\n return Date.UTC(ymd.year, ymd.month - 1, ymd.day);\n}\n\nfunction utcMsToYmd(epochMs: number): Ymd {\n const date = new Date(epochMs);\n return { year: date.getUTCFullYear(), month: date.getUTCMonth() + 1, day: date.getUTCDate() };\n}\n\n/** Chronological comparison: negative if `left` precedes `right`, 0 if the\n * same day, positive if after. */\nexport function compareYmd(left: Ymd, right: Ymd): number {\n return ymdToUtcMs(left) - ymdToUtcMs(right);\n}\n\n/** True iff `day` falls within the inclusive span `[span.start, span.end]`. */\nexport function spanCoversDay<T>(span: RecordSpan<T>, day: Ymd): boolean {\n return compareYmd(span.start, day) <= 0 && compareYmd(day, span.end) <= 0;\n}\n\n/** Build the 6×7 (42-cell) grid for the given month, including the\n * leading/trailing days of the adjacent months so every week is full.\n * `month` is 1-12. `weekStartsOn` is 0 (Sunday) … 6 (Saturday). */\nexport function buildMonthGrid(year: number, month: number, weekStartsOn = 0): DayCell[] {\n const firstWeekday = new Date(Date.UTC(year, month - 1, 1)).getUTCDay();\n const lead = (firstWeekday - weekStartsOn + 7) % 7;\n const startMs = Date.UTC(year, month - 1, 1) - lead * MS_PER_DAY;\n const cells: DayCell[] = [];\n for (let i = 0; i < 42; i++) {\n const ymd = utcMsToYmd(startMs + i * MS_PER_DAY);\n cells.push({ ymd, inMonth: ymd.year === year && ymd.month === month, key: ymdKey(ymd) });\n }\n return cells;\n}\n\n/** Resolve a record's calendar span from its date/datetime fields. Returns\n * null when the anchor date is missing/invalid (→ the caller's \"no date\"\n * tray). An end date that is missing, invalid, or earlier than the start\n * collapses to a single-day span — never an inverted range.\n *\n * Times for the day (time-allocation) view come from, in priority order:\n * 1. the clock on a `datetime` anchor/end value, else\n * 2. `timeField` — a separate free-form time-string column (e.g. \"14:00-17:00\").\n * A record with no resolvable clock has `startMin === endMin === null`. */\nexport function recordSpan<T extends Record<string, unknown>>(item: T, anchorField: string, endField?: string, timeField?: string): RecordSpan<T> | null {\n const startRaw = item[anchorField];\n const start = dateOf(startRaw);\n if (!start) return null;\n let end = start;\n let startMin = timeOf(startRaw);\n let endMin: number | null = null;\n if (endField) {\n const endRaw = item[endField];\n const parsedEnd = dateOf(endRaw);\n if (parsedEnd && compareYmd(parsedEnd, start) >= 0) {\n end = parsedEnd;\n endMin = timeOf(endRaw);\n }\n }\n // Fall back to a separate time-string field only when the date fields\n // carried no clock (the date-only anchor + `time` column shape).\n if (timeField && startMin === null && endMin === null) {\n const range = parseTimeRange(item[timeField]);\n if (range) {\n ({ startMin, endMin } = range);\n }\n }\n return { item, start, end, startMin, endMin };\n}\n\n/** Split records into those that land on the calendar (with their spans)\n * and those with no usable anchor date (the \"no date\" tray). Spans are\n * sorted by start day so same-day stacking is stable across renders. */\nexport function bucketRecords<T extends Record<string, unknown>>(\n items: readonly T[],\n anchorField: string,\n endField?: string,\n timeField?: string,\n): { spans: RecordSpan<T>[]; noDate: T[] } {\n const spans: RecordSpan<T>[] = [];\n const noDate: T[] = [];\n for (const item of items) {\n const span = recordSpan(item, anchorField, endField, timeField);\n if (span) spans.push(span);\n else noDate.push(item);\n }\n spans.sort((left, right) => compareYmd(left.start, right.start));\n return { spans, noDate };\n}\n\n/** Geometry for one record on one day of the time-allocation view.\n * `kind`:\n * \"allDay\" — no clock anywhere → render in the bottom all-day strip.\n * \"line\" — a single point in time → a 1px marker at `startMin`.\n * \"block\" — a [startMin, endMin) span, clamped to this day's [0, 1440).\n * `bleedsBefore` / `bleedsAfter` flag a multi-day span that began on an\n * earlier day or continues onto a later one (so the view can show arrows). */\nexport interface DaySlice {\n kind: \"allDay\" | \"line\" | \"block\";\n startMin: number;\n endMin: number;\n bleedsBefore: boolean;\n bleedsAfter: boolean;\n}\n\n/** Project a record's span onto a single day for the time-allocation view, or\n * null when the span doesn't cover that day. */\nexport function daySlice<T>(span: RecordSpan<T>, day: Ymd): DaySlice | null {\n if (!spanCoversDay(span, day)) return null;\n const hasStart = span.startMin !== null;\n const hasEnd = span.endMin !== null;\n if (!hasStart && !hasEnd) {\n return { kind: \"allDay\", startMin: 0, endMin: MINUTES_PER_DAY, bleedsBefore: false, bleedsAfter: false };\n }\n const singleDay = compareYmd(span.start, span.end) === 0;\n const isStartDay = compareYmd(day, span.start) === 0;\n const isEndDay = compareYmd(day, span.end) === 0;\n // A point in time: a start clock with no end, all on one day.\n if (singleDay && hasStart && !hasEnd) {\n return { kind: \"line\", startMin: span.startMin as number, endMin: span.startMin as number, bleedsBefore: false, bleedsAfter: false };\n }\n const startMin = isStartDay && hasStart ? (span.startMin as number) : 0;\n const endMin = isEndDay && hasEnd ? (span.endMin as number) : MINUTES_PER_DAY;\n // Zero-length or inverted same-day range → degrade to a line.\n if (singleDay && endMin <= startMin) {\n return { kind: \"line\", startMin, endMin: startMin, bleedsBefore: false, bleedsAfter: false };\n }\n return { kind: \"block\", startMin, endMin, bleedsBefore: !isStartDay, bleedsAfter: !isEndDay };\n}\n\n/** Side-by-side lane assignment for overlapping timeline blocks. Each input\n * is an `[startMin, endMin)` interval; the result (parallel to the input)\n * gives each item its `lane` (column index) and the `lanes` total of its\n * overlap cluster, so a renderer can size every block to `1 / lanes` width\n * and offset it by `lane / lanes`. Non-overlapping items get `lanes === 1`. */\nexport interface LaneSpan {\n startMin: number;\n endMin: number;\n}\nexport interface LaneAssignment {\n lane: number;\n lanes: number;\n}\n\nexport function assignLanes(blocks: readonly LaneSpan[]): LaneAssignment[] {\n const order = [...blocks.keys()].sort((left, right) => blocks[left].startMin - blocks[right].startMin || blocks[left].endMin - blocks[right].endMin);\n const result: LaneAssignment[] = blocks.map(() => ({ lane: 0, lanes: 1 }));\n let cluster: number[] = [];\n let clusterEnd = Number.NEGATIVE_INFINITY;\n const laneEnds: number[] = [];\n const flush = (): void => {\n for (const index of cluster) result[index].lanes = laneEnds.length;\n cluster = [];\n laneEnds.length = 0;\n clusterEnd = Number.NEGATIVE_INFINITY;\n };\n for (const index of order) {\n const block = blocks[index];\n if (cluster.length > 0 && block.startMin >= clusterEnd) flush();\n let lane = laneEnds.findIndex((end) => end <= block.startMin);\n if (lane === -1) {\n lane = laneEnds.length;\n laneEnds.push(block.endMin);\n } else {\n laneEnds[lane] = block.endMin;\n }\n result[index].lane = lane;\n cluster.push(index);\n clusterEnd = Math.max(clusterEnd, block.endMin);\n }\n flush();\n return result;\n}\n\n/** Month label key inputs — returns the 1st of the month as a `Date` so the\n * component can feed it to `Intl.DateTimeFormat(locale, …)` for a localized\n * \"April 2026\" header without this module taking a locale dependency. */\nexport function monthAnchorDate(year: number, month: number): Date {\n return new Date(Date.UTC(year, month - 1, 1));\n}\n","// Neutralize structural prompt-injection vectors in a short, record-derived\n// string before it rides into an LLM-facing prompt: strip angle brackets,\n// defang backticks / `${` template openings, collapse whitespace (so an\n// embedded newline can't fabricate a pseudo-instruction on its own line), and\n// clip to a small budget. Mirrors the host server's own defang so the two\n// can't drift (#1677).\n\n/** Max chars kept — the first batch of a validation issue is enough to act on. */\nconst DEFANG_MAX_LEN = 200;\n\nexport function defangForPrompt(value: string): string {\n return value.replace(/[<>]/g, \"\").replace(/`/g, \"'\").replace(/\\$\\{/g, \"$ {\").replace(/\\s+/g, \" \").slice(0, DEFANG_MAX_LEN);\n}\n"],"mappings":";;;AAeA,IAAM,cAAc,UAClB,OAAO,UAAU,YAAY,OAAO,UAAU,YAAY,OAAO,UAAU,aAAa,iBAAiB;;;;;;;AAQ3G,SAAgB,gBAAgB,OAA+B;CAC7D,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM,OAAO;CAClD,IAAI,CAAC,WAAW,KAAK,GAAG,OAAO;CAC/B,IAAI,iBAAiB,MAKnB,OAAO,OAAO,MAAM,MAAM,QAAQ,CAAC,IAAI,OAAO,MAAM,YAAY;CAElE,OAAO,OAAO,KAAK;AACrB;;;;AAKA,SAAgB,UAAU,OAAgB,WAAW,IAAY;CAC/D,OAAO,gBAAgB,KAAK,KAAK;AACnC;;;;;;;;;;AClBA,SAAgB,YAAY,MAAiC,QAA0C;CACrG,IAAI,CAAC,MAAM,OAAO;CAIlB,MAAM,OAAO,gBAAgB,OAAO,KAAK,MAAM;CAC/C,IAAI,SAAS,MAAM,OAAO;CAC1B,OAAO,KAAK,GAAG,SAAS,IAAI;AAC9B;;;;AAcA,SAAgB,cAAc,QAAwB,QAA0C;CAC9F,OAAO,YAAY,OAAO,QAAQ,OAAO,SAAS,MAAM;AAC1D;;;;;;;AAQA,SAAgB,kBAAkB,UAAkB,QAAyB;CAC3E,OAAO,WAAW,KAAA,IAAY,cAAc,aAAa,QAAQ,OAAO,GAAG;AAC7E;;;;;;AAaA,SAAgB,aAAa,OAAsB,QAA0C;CAC3F,OAAO,YAAY,MAAM,MAAM,MAAM;AACvC;;;;;;;;;;;;ACjDA,SAAgB,aAAa,MAAkD,UA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B,CAAC;CAC5B,MAAM,cAAoB;EACxB,KAAK,MAAM,SAAS,SAAS,OAAO,MAAM,CAAC,QAAQ,SAAS;EAC5D,UAAU,CAAC;EACX,SAAS,SAAS;EAClB,aAAa,OAAO;CACtB;CACA,KAAK,MAAM,SAAS,OAAO;EACzB,MAAM,QAAQ,OAAO;EACrB,IAAI,QAAQ,SAAS,KAAK,MAAM,YAAY,YAAY,MAAM;EAC9D,IAAI,OAAO,SAAS,WAAW,QAAQ,OAAO,MAAM,QAAQ;EAC5D,IAAI,SAAS,IAAI;GACf,OAAO,SAAS;GAChB,SAAS,KAAK,MAAM,MAAM;EAC5B,OACE,SAAS,QAAQ,MAAM;EAEzB,OAAO,MAAM,CAAC,OAAO;EACrB,QAAQ,KAAK,KAAK;EAClB,aAAa,KAAK,IAAI,YAAY,MAAM,MAAM;CAChD;CACA,MAAM;CACN,OAAO;AACT;;;;AAKA,SAAgB,gBAAgB,MAAc,OAAqB;CACjE,OAAO,IAAI,KAAK,KAAK,IAAI,MAAM,QAAQ,GAAG,CAAC,CAAC;AAC9C;;;;AC/TA,IAAM,iBAAiB;AAEvB,SAAgB,gBAAgB,OAAuB;CACrD,OAAO,MAAM,QAAQ,SAAS,EAAE,CAAC,CAAC,QAAQ,MAAM,GAAG,CAAC,CAAC,QAAQ,SAAS,KAAK,CAAC,CAAC,QAAQ,QAAQ,GAAG,CAAC,CAAC,MAAM,GAAG,cAAc;AAC3H"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"server-B7mtx4Cd.cjs","names":[],"sources":["../src/collection/server/jsonlQuery.ts","../src/collection/server/derive.ts","../src/collection/server/queryRunner.ts","../src/collection/core/recordZ.ts","../src/collection/server/validate.ts","../src/collection/server/mutate.ts","../src/collection/server/ontology.ts","../src/collection/server/dynamicIcon.ts","../src/collection/server/util.ts","../src/collection/server/spawn.ts","../src/collection/server/delete.ts","../src/collection/server/views.ts","../src/workspace-setup/slug.ts","../src/collection/server/manageTool.ts"],"sourcesContent":["// Aggregation-query executor for FILE-BACKED collections: the same DSL\n// (`core/queryZ.ts`) and the same DuckDB engine as the CSV store, run\n// over a temp JSONL of the collection's rows. Deliberately takes\n// ALREADY-LOADED rows rather than pointing DuckDB at the record files,\n// for two correctness reasons:\n//\n// 1. **Computed fields.** Stored record files never contain `derived` /\n// `rollup` / `toggle` values — the caller passes ENRICHED rows\n// (`listItems` → `enrichItems`), so \"sum of invoice totals\" works\n// even when `total` is a formula.\n// 2. **The symlink defense.** A `read_json('<dataDir>/*.json')` glob\n// would follow a symlinked record file (the exact file-disclosure\n// hole `listItems`' lstat check closes) — DuckDB only ever sees\n// content that already passed the guarded reader.\n//\n// The temp JSONL lives in the same 0700 cache dir as CSV decode copies,\n// is written 0600 (shared OS tmpdir), and is unlinked when the query\n// finishes. Caller wiring lives in `queryRunner.ts` (NOT `store.ts` —\n// enrichment lives in `derive.ts`, which itself consumes `storeFor`, so\n// wiring it into the store would create an import cycle).\n\nimport { mkdir, open, unlink } from \"node:fs/promises\";\nimport { randomBytes } from \"node:crypto\";\nimport path from \"node:path\";\nimport type { CollectionItem } from \"../core/schema\";\nimport type { CollectionQuery, CollectionQueryAggregate } from \"../core/queryZ\";\nimport { compileJsonlQuery } from \"./csvQuery\";\nimport { cacheDir, normalizeCsvValue, queryCsv } from \"./csvStore\";\nimport { log } from \"./host\";\n\n/** SQL semantics for an aggregate-only query over ZERO rows: one scalar\n * row (`count` = 0, everything else NULL) — the same shape the CSV path\n * produces for a header-only file, so callers reading `rows[0]` never\n * break on storage kind or emptiness. A grouped query over zero rows is\n * zero groups (`[]`), also matching SQL. Synthesized here because DuckDB\n * has no empty file to infer a schema from. */\nfunction emptyCollectionResult(query: CollectionQuery): Record<string, unknown>[] {\n if ((query.groupBy?.length ?? 0) > 0) return [];\n const aggregates = Object.entries(query.aggregates ?? {});\n return [Object.fromEntries(aggregates.map(([alias, aggregate]: [string, CollectionQueryAggregate]) => [alias, aggregate.op === \"count\" ? 0 : null]))];\n}\n\n/** The SOURCE columns a query reads: groupBy columns, aggregate input\n * columns, and where fields. (orderBy resolves to groupBy columns or\n * aggregate ALIASES — never to a new source column — so it adds none.) */\nfunction referencedSourceColumns(query: CollectionQuery): string[] {\n const columns = new Set<string>(query.groupBy ?? []);\n for (const aggregate of Object.values(query.aggregates ?? {})) {\n if (aggregate.column !== undefined) columns.add(aggregate.column);\n }\n for (const cond of query.where ?? []) columns.add(cond.field);\n return [...columns];\n}\n\n/** Referenced columns that appear in NO row — a freshly-added optional\n * field, or a derived field whose inputs no record has yet. DuckDB only\n * infers columns from keys that occur in the JSONL, so without help a\n * valid query on such a field binder-errors here while the CSV path\n * (whose header always declares the column) returns NULLs. Padding the\n * FIRST line with `col: null` makes the column exist (full-scan\n * inference unions keys across lines) with matching NULL semantics. */\nfunction absentReferencedColumns(rows: CollectionItem[], query: CollectionQuery): string[] {\n const present = new Set<string>();\n for (const row of rows) for (const key of Object.keys(row)) present.add(key);\n return referencedSourceColumns(query).filter((column) => !present.has(column));\n}\n\n/** Run a validated query over enriched collection rows. */\nexport async function runQueryOverRows(rows: CollectionItem[], query: CollectionQuery): Promise<Record<string, unknown>[]> {\n if (rows.length === 0) return emptyCollectionResult(query);\n await mkdir(cacheDir(), { recursive: true, mode: 0o700 });\n const jsonlPath = path.join(cacheDir(), `q-${randomBytes(8).toString(\"hex\")}.jsonl`);\n try {\n // Stream row-by-row — a map+join would duplicate an uncapped\n // collection as one giant string before writing it. The first line\n // carries null placeholders for referenced-but-absent columns (see\n // `absentReferencedColumns`); a row's own values always win.\n const nullPads = Object.fromEntries(absentReferencedColumns(rows, query).map((column) => [column, null]));\n const handle = await open(jsonlPath, \"wx\", 0o600);\n try {\n let first = true;\n for (const row of rows) {\n await handle.write(`${JSON.stringify(first ? { ...nullPads, ...row } : row)}\\n`);\n first = false;\n }\n } finally {\n await handle.close();\n }\n const { sql, params } = compileJsonlQuery(query);\n const result = await queryCsv(sql, [jsonlPath, ...params]);\n return result.map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, normalizeCsvValue(value)])));\n } finally {\n // The finally covers the WRITE too — a partially-written file from a\n // rejected write must not linger in the shared tmpdir. A failed unlink\n // (other than \"never created\") is only logged: throwing here would\n // mask the query's own error.\n await unlink(jsonlPath).catch((err: unknown) => {\n if ((err as { code?: string }).code !== \"ENOENT\") log.warn(\"collections\", \"temp JSONL cleanup failed\", { path: jsonlPath, error: String(err) });\n });\n }\n}\n","// Server-side computed-field enrichment for collection records: the\n// host evaluates `derived` formulas (via the SHARED saturation loop in\n// src/utils/collections/deriveAll.ts — never a server reimplementation),\n// projects `toggle` fields off their enum, and resolves `embed` fields\n// to their fixed target record. The client does the same at render\n// time; this module gives server consumers (manageCollection getItems)\n// the same numbers the user sees on screen.\n\nimport { backlinkRows, projectBacklinkRow, rollupValue } from \"../core/backlinks\";\nimport { deriveAll, type DeriveRefRecords } from \"../core/deriveAll\";\nimport { loadCollection, type DiscoveryOptions } from \"./discovery\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport { storeFor } from \"./store\";\nimport { embedTargetId } from \"../core/schema\";\nimport type { CollectionFieldSpec, CollectionItem, CollectionSchema } from \"../core/schema\";\n\n/** Slugs of every collection referenced by a `ref` field — top-level\n * and one level into `table` sub-fields (nested tables are\n * schema-rejected). Mirrors the client's `uniqueRefTargets`. */\nfunction uniqueRefTargets(schema: CollectionSchema): string[] {\n const targets = new Set<string>();\n const walk = (fields: Record<string, CollectionFieldSpec>): void => {\n for (const field of Object.values(fields)) {\n if (field.type === \"ref\" && typeof field.to === \"string\" && field.to.length > 0) targets.add(field.to);\n if (field.type === \"table\" && field.of) walk(field.of);\n }\n };\n walk(schema.fields);\n return [...targets];\n}\n\n/** Slugs of every collection referenced by an `embed` field (top-level\n * only, like the client). */\nfunction uniqueEmbedTargets(schema: CollectionSchema): string[] {\n const targets = new Set<string>();\n for (const field of Object.values(schema.fields)) {\n if (field.type === \"embed\" && typeof field.to === \"string\" && field.to.length > 0) targets.add(field.to);\n }\n return [...targets];\n}\n\n/** Slugs of every SOURCE collection a `backlinks` or `rollup` field\n * reverses over — loaded exactly like ref/embed targets (whole\n * collection, once; the two field kinds share one load). */\nfunction uniqueBacklinkSources(schema: CollectionSchema): string[] {\n const sources = new Set<string>();\n for (const field of Object.values(schema.fields)) {\n if ((field.type === \"backlinks\" || field.type === \"rollup\") && field.from.length > 0) sources.add(field.from);\n }\n return [...sources];\n}\n\ninterface LinkedTarget {\n schema: CollectionSchema;\n /** primary-key slug → record (ref targets store the DERIVED record,\n * mirroring the client's `buildRefRecordMap`, so a formula deref\n * like `ticker.price` can read the target's own derived columns). */\n byId: Record<string, CollectionItem>;\n}\n\nasync function loadTarget(slug: string, opts: DiscoveryOptions): Promise<LinkedTarget | null> {\n const target = await loadCollection(slug, opts);\n if (!target) return null;\n const items = await storeFor(target, { workspaceRoot: opts.workspaceRoot }).list();\n const byId: Record<string, CollectionItem> = {};\n for (const item of items) {\n const itemId = item[target.schema.primaryKey];\n if (typeof itemId === \"string\" && itemId.length > 0) byId[itemId] = deriveAll(target.schema, item, {});\n }\n return { schema: target.schema, byId };\n}\n\n/** Load every ref/embed target and backlink source collection once.\n * Unknown / unloadable targets are simply absent — downstream derefs\n * resolve to null (em-dash) and backlinks to an empty row set, the\n * same fail-soft the UI renders. */\nasync function loadLinkedTargets(schema: CollectionSchema, opts: DiscoveryOptions): Promise<Record<string, LinkedTarget>> {\n const slugs = [...new Set([...uniqueRefTargets(schema), ...uniqueEmbedTargets(schema), ...uniqueBacklinkSources(schema)])];\n const loaded: Record<string, LinkedTarget> = {};\n for (const slug of slugs) {\n const target = await loadTarget(slug, opts);\n if (target) loaded[slug] = target;\n }\n return loaded;\n}\n\nfunction toRefRecords(linked: Record<string, LinkedTarget>): DeriveRefRecords {\n return Object.fromEntries(Object.entries(linked).map(([slug, target]) => [slug, target.byId]));\n}\n\n/** The matching source rows for one `backlinks` field, projected to the\n * source primaryKey + `display` columns — so getItems on a\n * heavily-referenced record stays a summary, not a dump of the source\n * collection. Missing source ⇒ [] (fail-soft). The rows come from the\n * DERIVED source records (`byId`), so `display`/`filter` on a derived\n * source column (an invoice `total`) works. */\nfunction projectBacklinks(\n field: Extract<CollectionFieldSpec, { type: \"backlinks\" }>,\n schema: CollectionSchema,\n enriched: CollectionItem,\n linked: Record<string, LinkedTarget>,\n): CollectionItem[] {\n const source = linked[field.from];\n if (!source) return [];\n const selfId = String(enriched[schema.primaryKey] ?? \"\");\n return backlinkRows(field, selfId, Object.values(source.byId)).map((row) => projectBacklinkRow(row, field.display, source.schema.primaryKey));\n}\n\n/** Project the computed (never-stored) field kinds onto one derived\n * record: `toggle` → boolean off its enum, `embed` → the target record\n * (fixed `id` or per-record `idField`), or null when missing,\n * `backlinks` → the matching source rows (see `projectBacklinks`). */\nfunction projectComputed(schema: CollectionSchema, enriched: CollectionItem, linked: Record<string, LinkedTarget>): CollectionItem {\n for (const [key, field] of Object.entries(schema.fields)) {\n if (field.type === \"toggle\" && field.field) {\n enriched[key] = String(enriched[field.field] ?? \"\") === field.onValue;\n }\n if (field.type === \"embed\" && field.to) {\n const targetId = embedTargetId(field, enriched);\n enriched[key] = (targetId && linked[field.to]?.byId[targetId]) || null;\n }\n if (field.type === \"backlinks\") {\n enriched[key] = projectBacklinks(field, schema, enriched, linked);\n }\n }\n return enriched;\n}\n\n/** Resolve every rollup field onto a COPY of the record, BEFORE the\n * formula pass — a `derived` formula may reference rollup values as\n * plain identifiers (`played = homePlayed + awayPlayed`). Same reverse\n * machinery as backlinks, collapsed to a number: an unresolvable source\n * is null (a formula reading it fails soft to em-dash); an empty match\n * set is a real 0. Returns `record` unchanged when the schema declares\n * no rollups. */\nfunction projectRollups(schema: CollectionSchema, record: CollectionItem, linked: Record<string, LinkedTarget>): CollectionItem {\n let out = record;\n for (const [key, field] of Object.entries(schema.fields)) {\n if (field.type !== \"rollup\") continue;\n if (out === record) out = { ...record };\n const source = linked[field.from];\n const selfId = String(record[schema.primaryKey] ?? \"\");\n out[key] = source ? rollupValue(field, selfId, Object.values(source.byId)) : null;\n }\n return out;\n}\n\n/** Enrich records with every host-computed field: derived formulas\n * evaluated (cross-collection derefs included), toggles projected,\n * embeds resolved. Loads each linked collection ONCE per call. Input\n * records are not mutated. */\nexport async function enrichItems(collection: LoadedCollection, items: CollectionItem[], opts: DiscoveryOptions = {}): Promise<CollectionItem[]> {\n const { schema } = collection;\n const linked = await loadLinkedTargets(schema, opts);\n const refRecords = toRefRecords(linked);\n // Rollups FIRST (formulas may read them), then the formula pass, then\n // the remaining projections — the client mirrors this order exactly\n // (`deriveRecord` / `evaluateDerived` in collection-plugin).\n return items.map((item) => projectComputed(schema, deriveAll(schema, projectRollups(schema, item, linked), refRecords), linked));\n}\n","// Engine-level aggregation runner: ONE entry point for the structured\n// query DSL (`core/queryZ.ts`) over any collection, regardless of its\n// storage backend. Native when the store has a query engine (the CSV\n// store scans the whole file in DuckDB); otherwise the shared fallback —\n// enrich the full record set (computed fields become real columns) and\n// run the same compiled SQL over a temp JSONL (`jsonlQuery.ts`).\n//\n// Callers (manageCollection's queryItems, which also backs the desktop\n// custom view's `/query` route) go through here so the two engines can\n// never drift — and so a future backend gets aggregation for free by\n// either declaring `nativeQuery` or doing nothing.\n//\n// Lives at the derive layer (above the store), which is what resolves the\n// old wiring note in jsonlQuery.ts: enrichment consumes `storeFor`, so\n// this could never live inside `store.ts` without an import cycle.\n\nimport type { CollectionQuery } from \"../core/queryZ\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport type { DiscoveryOptions } from \"./discovery\";\nimport { enrichItems } from \"./derive\";\nimport { runQueryOverRows } from \"./jsonlQuery\";\nimport { storeFor } from \"./store\";\n\nexport async function runCollectionQuery(\n collection: LoadedCollection,\n query: CollectionQuery,\n opts: DiscoveryOptions = {},\n): Promise<Record<string, unknown>[]> {\n const store = storeFor(collection, { workspaceRoot: opts.workspaceRoot });\n if (store.query) return store.query(query);\n // File-backed: load through the guarded reader (symlink defenses) and\n // enrich BEFORE DuckDB sees anything — a raw read_json over the record\n // files would both follow symlinks and miss every computed field.\n const enriched = await enrichItems(collection, await store.list(), opts);\n return runQueryOverRows(enriched, query);\n}\n","// Compiled per-collection zod RECORD validators (plan step ⓪ Phase B of\n// plans/done/collection-ontology.md): `compileRecordZ(schema, tier)` turns a\n// CollectionSchema's `fields` into a zod object validator for the stored\n// record JSON, so every consumer of record validation — the putItems write\n// gate, the post-hoc file scan, and (future) `mutate` action `params` forms —\n// derives its checks from the same compiler instead of growing parallel\n// hand-rolled loops.\n//\n// Two tiers, per the lint-not-lock principle:\n//\n// - `\"enforced\"` — the write-gate tier. Reproduces EXACTLY the historical\n// three checks (required fields non-empty, enum values in the closed set;\n// the primaryKey↔filename identity check stays in `validateRecordObject`,\n// which owns the id). Message strings are part of the contract: they are\n// the `problem` feedback the authoring LLM acts on, and the file scan and\n// the write gate must report enforced-tier violations IDENTICALLY.\n// - `\"strict\"` — the report-only tier: enforced plus per-type checks\n// (number/money hold numerics, booleans are booleans, dates parse, table\n// rows conform to their sub-schema). Consumed by `validateCollectionRecords`\n// (the scan surfaced through presentCollection / the detail response) so\n// legacy records written under the loose rules get REPORTED, never\n// rejected. Promote a strict check into the enforced tier only once the\n// lint runs clean across real workspaces.\n//\n// Isomorphic (zod + pure logic, no node built-ins) but — like `./schemaZ` —\n// deliberately NOT exported through the browser barrel; the server surface\n// re-exports it via `../server/validate`.\n\nimport { z } from \"zod\";\nimport { coerceNumeric } from \"./backlinks\";\nimport { parseIsoDate, parseIsoDateTime } from \"./calendarGrid\";\nimport { COMPUTED_TYPES } from \"./schema\";\nimport type { CollectionFieldSpec, CollectionItem, CollectionSchema, CollectionSubFieldSpec } from \"./schema\";\n\n// COMPUTED_TYPES moved to `./schema` (zod-free, so `./draft` can share\n// it); re-exported here for the existing server-surface importers.\nexport { COMPUTED_TYPES };\n\nexport type RecordCheckTier = \"enforced\" | \"strict\";\n\ntype AnyFieldSpec = CollectionFieldSpec | CollectionSubFieldSpec;\n\n/** The emptiness rule shared by `required` and the \"only check present\n * values\" gate. NOT a truthiness check — `0` and `false` are filled. */\nconst isEmptyValue = (value: unknown): boolean => value === undefined || value === null || value === \"\";\n\n/** The historical write-gate checks, verbatim: required non-empty, enum\n * membership (compared as strings, so a numeric `5` satisfies `\"5\"`). */\nfunction enforcedProblem(key: string, spec: AnyFieldSpec, value: unknown): string | null {\n const empty = isEmptyValue(value);\n if (spec.required && empty) return `missing required field '${key}'`;\n if (!empty && spec.type === \"enum\" && !spec.values.includes(String(value))) {\n return `'${key}' = '${String(value)}' is not one of [${spec.values.join(\", \")}]`;\n }\n return null;\n}\n\n// Numeric coercion for the strict `number`/`money` check — moved to\n// `./backlinks` so rollup sums share the same definition of \"numeric\".\n\n/** Report-only per-type checks on a PRESENT value. Date / datetime reuse the\n * calendar's STRICT civil parsers (`parseIsoDate` / `parseIsoDateTime`), so\n * the lint flags exactly the values the calendar / trigger / spawn code\n * would silently drop — impossible days like `2026-02-30`, and datetimes\n * outside the canonical `YYYY-MM-DDTHH:MM[:SS]` shape (e.g. a `Z` suffix,\n * which the day view can't place). `string`-backed types accept anything\n * stringifiable; `ref` existence is out of scope. */\nfunction strictTypeProblem(key: string, spec: AnyFieldSpec, value: unknown): string | null {\n switch (spec.type) {\n case \"number\":\n case \"money\":\n return Number.isFinite(coerceNumeric(value)) ? null : `'${key}' = '${String(value)}' is not numeric (a '${spec.type}' field stores a plain number)`;\n case \"boolean\":\n return value === true || value === false ? null : `'${key}' = '${String(value)}' is not a boolean (store true or false, unquoted)`;\n case \"date\":\n return parseIsoDate(value) !== null ? null : `'${key}' = '${String(value)}' is not a real YYYY-MM-DD date`;\n case \"datetime\":\n return parseIsoDateTime(value) !== null\n ? null\n : `'${key}' = '${String(value)}' is not a YYYY-MM-DDTHH:MM datetime (seconds optional, no timezone suffix — the shape the calendar parses)`;\n default:\n return null;\n }\n}\n\n/** Strict check for a PRESENT `table` value: an array of row objects, each\n * row conforming to the sub-schema (required / enum / typed sub-values).\n * First row problem wins, prefixed with the row number so the fix is\n * locatable. */\nfunction strictTableProblem(key: string, spec: Extract<CollectionFieldSpec, { type: \"table\" }>, value: unknown): string | null {\n if (!Array.isArray(value)) return `'${key}' = '${String(value)}' is not an array of rows (a 'table' field stores an array of row objects)`;\n for (let index = 0; index < value.length; index++) {\n const row: unknown = value[index];\n if (!row || typeof row !== \"object\" || Array.isArray(row)) return `'${key}' row ${index + 1} is not an object`;\n for (const [subKey, subSpec] of Object.entries(spec.of)) {\n const subValue = (row as Record<string, unknown>)[subKey];\n // Typed checks apply to PRESENT sub-values only — an empty optional\n // cell is fine, mirroring the top-level gate in `recordFieldProblem`.\n const problem = enforcedProblem(subKey, subSpec, subValue) ?? (isEmptyValue(subValue) ? null : strictTypeProblem(subKey, subSpec, subValue));\n if (problem) return `'${key}' row ${index + 1}: ${problem}`;\n }\n }\n return null;\n}\n\n/** First problem for one field's stored value under `tier`, or null.\n * Enforced checks always run (and their messages never vary by tier — the\n * scan and the write gate must agree on them); strict adds the per-type\n * layer on present values only. */\nexport function recordFieldProblem(key: string, spec: AnyFieldSpec, value: unknown, tier: RecordCheckTier): string | null {\n const enforced = enforcedProblem(key, spec, value);\n if (enforced || tier === \"enforced\") return enforced;\n if (isEmptyValue(value)) return null;\n if (spec.type === \"table\") return strictTableProblem(key, spec, value);\n return strictTypeProblem(key, spec, value);\n}\n\n// Compiled validators cached per schema OBJECT (a putItems batch validates\n// many rows against the same LoadedCollection.schema instance; a re-loaded\n// collection gets a fresh schema object and naturally re-compiles).\nconst compiled = new WeakMap<CollectionSchema, Partial<Record<RecordCheckTier, z.ZodType>>>();\n\n/** Compile `schema.fields` into a zod validator for a stored record.\n * Loose object: unknown keys are allowed and any declared key may be\n * absent (records are user files, not parse-and-rewrite targets —\n * callers validate, they never persist the parse output). The checks run\n * as ONE object-level refine iterating fields in declaration order —\n * per-key shape schemas can't express \"key may be absent BUT its absence\n * must still reach the required check\", and the single loop keeps the\n * first reported issue identical to the historical first-problem-wins\n * contract. */\nexport function compileRecordZ(schema: CollectionSchema, tier: RecordCheckTier): z.ZodType {\n const cached = compiled.get(schema)?.[tier];\n if (cached) return cached;\n const stored = Object.entries(schema.fields).filter(([, spec]) => !COMPUTED_TYPES.has(spec.type));\n const validator = z.looseObject({}).superRefine((record, ctx) => {\n for (const [key, spec] of stored) {\n const problem = recordFieldProblem(key, spec, record[key], tier);\n if (problem) ctx.addIssue({ code: \"custom\", message: problem, path: [key] });\n }\n });\n const entry = compiled.get(schema) ?? {};\n entry[tier] = validator;\n compiled.set(schema, entry);\n return validator;\n}\n\n/** First schema problem on an in-memory record under `tier`, or null. One\n * issue per record keeps the report short and the fix obvious (the\n * historical contract of `validateRecordObject`). */\nexport function firstRecordProblem(record: CollectionItem, schema: CollectionSchema, tier: RecordCheckTier): string | null {\n const result = compileRecordZ(schema, tier).safeParse(record);\n if (result.success) return null;\n return result.error.issues[0]?.message ?? \"record failed schema validation\";\n}\n","// Validate a collection's record files and report problems back to the\n// authoring LLM. The host SILENTLY skips unparseable records at read time\n// (see `listItems`), so without this a single malformed file looks like\n// \"records vanished.\" `presentCollection` — which the LLM is told to call\n// after every write — runs this and surfaces the problems in its result,\n// closing the loop so the model fixes the file instead of a human noticing\n// missing rows much later.\n\nimport { readdir, readFile, lstat } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { getWorkspaceRoot, log } from \"./host\";\nimport { isContainedInRoot } from \"./paths\";\nimport { storeFor } from \"./store\";\nimport { firstRecordProblem, type RecordCheckTier } from \"../core/recordZ\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport type { CollectionItem, CollectionSchema } from \"../core/schema\";\n\n// The compiled record validators (and COMPUTED_TYPES, which moved next to\n// them) live in the isomorphic ../core/recordZ; re-exported here so the\n// existing `collection/server` importers resolve unchanged and future\n// consumers (the reserved `mutate` action's `params` form) share the\n// same compiler.\nexport { COMPUTED_TYPES, compileRecordZ, recordFieldProblem, type RecordCheckTier } from \"../core/recordZ\";\n\nexport interface RecordIssue {\n /** Record filename, e.g. `lesson-003.json`. */\n file: string;\n /** Human-readable problem, written to be actionable by the LLM. */\n problem: string;\n}\n\n// Don't flood the result; the first batch is enough to act on.\nconst MAX_ISSUES = 25;\n\n/** Read every `<id>.json` under the collection's dataDir and report the\n * ones that won't load or violate the schema. An empty list means every\n * record is fine. */\n/** List entries under the data dir, guarding realpath containment (against a\n * symlinked dir swapped in after discovery, like `listItems`) and treating a\n * missing dir as empty while surfacing real I/O faults. */\nasync function listRecordFilenames(dataDir: string, workspaceRoot: string): Promise<string[]> {\n if (!isContainedInRoot(dataDir, workspaceRoot)) {\n log.warn(\"collections\", \"validate refused: dataDir escapes workspace via symlink\", { dataDir });\n return [];\n }\n try {\n return await readdir(dataDir);\n } catch (err) {\n const error = err as { code?: string };\n if (error.code === \"ENOENT\") return []; // no dir yet = no records\n throw err; // surface permission / I/O faults instead of silently passing\n }\n}\n\nexport async function validateCollectionRecords(collection: LoadedCollection, opts: { workspaceRoot?: string } = {}): Promise<RecordIssue[]> {\n // A `dataSource` collection has no record FILES to validate — its rows\n // come from the external data file (type mismatches there surface as\n // raw values in the views, not as repairable record files).\n if (collection.schema.dataSource !== undefined) return [];\n // A `storage` collection's records live in its database, not as files —\n // validate what the store serves instead of scanning the phantom dataDir.\n if (collection.schema.storage !== undefined) return validateStoreRecords(collection, opts);\n const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();\n const entries = await listRecordFilenames(collection.dataDir, workspaceRoot);\n const issues: RecordIssue[] = [];\n for (const name of entries.sort()) {\n if (!name.endsWith(\".json\") || name.startsWith(\".\")) continue;\n if (issues.length >= MAX_ISSUES) break;\n const issue = await inspectRecord(path.join(collection.dataDir, name), name, collection.schema);\n if (issue) issues.push(issue);\n }\n return issues;\n}\n\n/** Store-backed twin of the file scan: list every record through the\n * collection's store and lint it with the same \"strict\" report-only tier.\n * A row the store can't even parse is invisible here (the store skips\n * it), so the read/parse classifications of the file scan don't apply —\n * schema violations are what this catches. `file` carries the record id\n * (there is no per-record filename). */\nasync function validateStoreRecords(collection: LoadedCollection, opts: { workspaceRoot?: string }): Promise<RecordIssue[]> {\n let items: CollectionItem[];\n try {\n items = await storeFor(collection, { workspaceRoot: opts.workspaceRoot }).list();\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err);\n return [{ file: \"(store)\", problem: `records could not be read from the storage backend: ${reason}` }];\n }\n const issues: RecordIssue[] = [];\n for (const item of items) {\n if (issues.length >= MAX_ISSUES) break;\n const itemId = String(item[collection.schema.primaryKey] ?? \"\");\n const problem = validateRecordObject(item, itemId, collection.schema, \"strict\");\n if (problem) issues.push({ file: itemId, problem });\n }\n return issues;\n}\n\n// Read a record file's text, or classify why it can't be read (missing /\n// symlink / unreadable). Split out to keep `inspectRecord` under the line limit.\nasync function readRecordText(fullPath: string, name: string): Promise<{ raw: string } | RecordIssue> {\n try {\n const stat = await lstat(fullPath);\n if (!stat.isFile()) return { file: name, problem: \"not a regular file (symlink?) — skipped, won't appear\" };\n return { raw: await readFile(fullPath, \"utf-8\") };\n } catch {\n return { file: name, problem: \"could not be read — skipped, won't appear\" };\n }\n}\n\n/** Classify a single record file: unreadable / unparseable / non-object /\n * schema violation, or null when it's fine. */\nasync function inspectRecord(fullPath: string, name: string, schema: CollectionSchema): Promise<RecordIssue | null> {\n const read = await readRecordText(fullPath, name);\n if (\"problem\" in read) return read;\n let parsed: unknown;\n try {\n parsed = JSON.parse(read.raw);\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err);\n return {\n file: name,\n problem: `invalid JSON (${reason}) — SKIPPED, won't appear. Usual cause: an unescaped \" inside a string value; use 「」/『』 or write \\\\\" instead.`,\n };\n }\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return { file: name, problem: \"not a JSON object — skipped, won't appear\" };\n }\n // \"strict\" tier: the file scan is REPORT-ONLY (surfaced through\n // presentCollection / the detail response), so it lints the per-type\n // rules the write gate does not yet enforce — legacy records written\n // under the loose rules get reported here, never rejected on write.\n const problem = validateRecordObject(parsed as CollectionItem, name.replace(/\\.json$/, \"\"), schema, \"strict\");\n return problem ? { file: name, problem } : null;\n}\n\n/** First schema problem on an in-memory record (primaryKey↔id mismatch,\n * then the compiled per-field checks — see `../core/recordZ` for the two\n * tiers), or null when it's fine. One issue per record keeps the report\n * short and the fix obvious. Pure + exported so write paths\n * (manageCollection putItems) can gate on the SAME enforced rules the\n * post-hoc file scan reports — `itemId` is the id the record is (or\n * would be) stored under. The default `\"enforced\"` tier keeps every\n * write gate on the historical three checks; only pass `\"strict\"` from\n * report-only surfaces. */\nexport function validateRecordObject(record: CollectionItem, itemId: string, schema: CollectionSchema, tier: RecordCheckTier = \"enforced\"): string | null {\n const idValue = record[schema.primaryKey];\n if (typeof idValue !== \"string\" || idValue !== itemId) {\n return `'${schema.primaryKey}' is '${String(idValue ?? \"\")}' but must equal the filename ('${itemId}'), or the record can't be opened`;\n }\n return firstRecordProblem(record, schema, tier);\n}\n","// Execute a `kind: \"mutate\"` action (plan step ④ of\n// plans/done/collection-ontology.md): the HOST applies a declarative,\n// merge-semantics write to one record — no LLM, no tokens. The pipeline\n// deliberately reuses the governed write path's pieces, never a third\n// mechanism:\n//\n// 1. params — the mini-form is validated per spec by the SAME\n// compiled record checks `putItems` uses (`recordFieldProblem`,\n// strict tier: required/enum plus typed values, since a form\n// rejects rather than lints);\n// 2. merge — the resolved `set` merges over the stored record with\n// computed keys stripped (healing stale copies, exactly like\n// manageCollection's `mode: \"merge\"`);\n// 3. gate — `validateRecordObject`, the same write gate every\n// governed path runs;\n// 4. write — `writeItem` (atomic, change-publishing).\n//\n// The `require` state gate is enforced HERE, against the exact snapshot\n// the merge runs on — the route's earlier `actionVisible` pre-check is a\n// fast 409 on a stale button, but a concurrent write between the route's\n// read and ours could otherwise slip a mutation past a gate that no\n// longer holds (Codex on PR #2105). Visibility is the authorization\n// rule, shared with the chat/agent kinds.\n\nimport { actionVisible } from \"../core/actionVisible\";\nimport { COMPUTED_TYPES, recordFieldProblem } from \"../core/recordZ\";\nimport { resolveMutateSet } from \"../core/mutateAction\";\nimport type { IoOptions } from \"./io\";\nimport { storeFor } from \"./store\";\nimport { validateRecordObject } from \"./validate\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport type { CollectionItem, CollectionMutateAction } from \"../core/schema\";\n\nexport type MutateActionOutcome =\n | { ok: true; item: CollectionItem }\n /** `status` picks the HTTP mapping: bad params / a write-gate reject are\n * the caller's 400s, a missing record its 404, an unmet `require` its\n * 409, a refused write its 500. */\n | { ok: false; status: \"invalid-params\" | \"invalid-record\" | \"not-found\" | \"require-unmet\" | \"write-refused\"; problem: string };\n\n/** First problem with the submitted params, or null. Every declared param\n * is checked by the shared record-field validator; keys the action never\n * declared are rejected outright — a stray key would otherwise ride the\n * resolved `set` semantics silently. */\nexport function firstMutateParamProblem(action: CollectionMutateAction, params: Record<string, unknown>): string | null {\n const declared = action.params ?? {};\n for (const key of Object.keys(params)) {\n if (declared[key] === undefined) return `unknown param '${key}' — not declared by action '${action.id}'`;\n }\n for (const [key, spec] of Object.entries(declared)) {\n const problem = recordFieldProblem(key, spec, params[key], \"strict\");\n if (problem) return problem;\n }\n return null;\n}\n\n/** Apply one mutate action to one record. Never throws for data-shaped\n * failures — the outcome's `problem` doubles as agent feedback when an\n * LLM (not the user) pressed the button. */\nexport async function applyMutateAction(\n collection: LoadedCollection,\n action: CollectionMutateAction,\n itemId: string,\n params: Record<string, unknown>,\n opts: IoOptions = {},\n): Promise<MutateActionOutcome> {\n const paramProblem = firstMutateParamProblem(action, params);\n if (paramProblem) return { ok: false, status: \"invalid-params\", problem: paramProblem };\n\n // Discovery statically rejects mutate actions on read-only (dataSource)\n // schemas, so a missing `write` is defense in depth, not a live path.\n const store = storeFor(collection, opts);\n if (!store.write) return { ok: false, status: \"write-refused\", problem: \"write refused (read-only collection)\" };\n\n const existing = await store.read(itemId);\n if (!existing) return { ok: false, status: \"not-found\", problem: `item '${itemId}' not found` };\n\n // Re-check `require` against THIS read — the snapshot the merge below\n // actually uses — so a write that landed after the route's pre-check\n // can't let the mutation run with its gate no longer satisfied.\n if (!actionVisible(action, existing)) {\n return { ok: false, status: \"require-unmet\", problem: `action '${action.id}' is not available for item '${itemId}' in its current state` };\n }\n\n // Merge over the stored record with computed keys stripped — a legacy /\n // raw-written record can carry a stale derived/embed value, and\n // re-writing it would perpetuate a forged host-computed value.\n const stored = Object.entries(existing).filter(([key]) => {\n const spec = collection.schema.fields[key];\n return !spec || !COMPUTED_TYPES.has(spec.type);\n });\n const merged: CollectionItem = { ...Object.fromEntries(stored), ...resolveMutateSet(action.set, params) };\n\n const invalid = validateRecordObject(merged, itemId, collection.schema);\n if (invalid) return { ok: false, status: \"invalid-record\", problem: invalid };\n\n const result = await store.write(itemId, merged);\n if (result.kind !== \"ok\") return { ok: false, status: \"write-refused\", problem: `write refused (${result.kind})` };\n return { ok: true, item: merged };\n}\n","// The machine-readable workspace ontology (plan step ① of\n// plans/done/collection-ontology.md): one entry per discovered collection with\n// the identity + relationship facts an agent needs to reason ACROSS\n// collections — which slugs exist, how they point at each other, how big\n// they are — without re-reading every schema.json. Derived on demand from\n// the schemas (no authoring, no storage), so it can never go stale.\n// Deliberately NOT a unified schema: collections stay bounded contexts;\n// semantic joining happens at read time in the caller.\n\nimport { readdir } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { discoverCollections, type DiscoveryOptions } from \"./discovery\";\nimport { storeFor } from \"./store\";\nimport { storageKindFor } from \"../core/schema\";\nimport { isRegularFile } from \"./io\";\nimport { isContainedInRoot } from \"./paths\";\nimport { getWorkspaceRoot } from \"./host\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport type { CollectionSchema } from \"../core/schema\";\n\n/** One relationship a schema declares: a `ref` (the record stores the\n * target's primaryKey slug) or `embed` (display-only pull) pointing at\n * collection `to`, or `backlinks` (display-only REVERSE refs — `to` is\n * the backlink's source collection, i.e. its `from`). A `ref` column\n * inside a `table` field is reported with a dotted path\n * (`lines.clientId`). Whether `to` exists is NOT checked — resolution\n * is fail-soft at render, and the caller holds the full slug list to\n * compare against anyway. */\nexport interface OntologyRelation {\n field: string;\n kind: \"ref\" | \"embed\" | \"backlinks\" | \"rollup\";\n to: string;\n}\n\nexport interface CollectionOntologyEntry {\n slug: string;\n title: string;\n icon: string;\n primaryKey: string;\n /** The effective display field: the schema's `displayField`, falling\n * back to the primaryKey exactly as render-time labelling does. */\n displayField: string;\n recordCount: number;\n relations: OntologyRelation[];\n}\n\n/** Extract the relations a schema declares, in field declaration order:\n * top-level `ref` / `embed` / `backlinks` fields plus `ref` sub-fields\n * inside `table` columns. Pure — exported so the phase-2 graph panel\n * can reuse it on already-loaded schemas. */\nexport function schemaRelations(schema: CollectionSchema): OntologyRelation[] {\n const relations: OntologyRelation[] = [];\n for (const [key, spec] of Object.entries(schema.fields)) {\n if (spec.type === \"ref\" || spec.type === \"embed\") relations.push({ field: key, kind: spec.type, to: spec.to });\n if (spec.type === \"backlinks\" || spec.type === \"rollup\") relations.push({ field: key, kind: spec.type, to: spec.from });\n if (spec.type !== \"table\") continue;\n for (const [subKey, subSpec] of Object.entries(spec.of)) {\n if (subSpec.type === \"ref\") relations.push({ field: `${key}.${subKey}`, kind: \"ref\", to: subSpec.to });\n }\n }\n return relations;\n}\n\n/** Count the record files in a collection's data dir — the same\n * `<id>.json` entries `listItems` considers, WITHOUT parsing them (the\n * ontology is a summary; a malformed record is still a record). Each\n * candidate is classified by the SHARED `isRegularFile` lstat helper —\n * the exact file-disclosure defense `listItems` applies — so a\n * symlinked record never counts and the count can't diverge from the\n * readable set on any filesystem (Codex review on PR #2099, twice).\n * Fail-soft: a missing dir or a dataDir escaping the workspace via\n * symlink counts 0. */\nasync function countRecordFiles(dataDir: string, workspaceRoot: string): Promise<number> {\n if (!isContainedInRoot(dataDir, workspaceRoot)) return 0;\n try {\n const names = (await readdir(dataDir)).filter((name) => name.endsWith(\".json\") && !name.startsWith(\".\"));\n const checks = await Promise.all(names.map((name) => isRegularFile(path.join(dataDir, name))));\n return checks.filter(Boolean).length;\n } catch {\n return 0;\n }\n}\n\n/** A collection's record count for the ontology. File-backed: the cheap\n * readdir count. Every other backend (`dataSource` CSV, `storage`\n * sqlite): the store's page total — the dataDir is a phantom there, so\n * the readdir count would always misreport 0. Fail-soft to 0 like\n * `countRecordFiles` (an unreadable file / missing engine must not\n * break the whole ontology). */\nasync function countRecords(collection: LoadedCollection, workspaceRoot: string): Promise<number> {\n if (storageKindFor(collection.schema) !== \"file\") {\n try {\n return (await storeFor(collection, { workspaceRoot }).page({ limit: 0 })).total;\n } catch {\n return 0;\n }\n }\n return countRecordFiles(collection.dataDir, workspaceRoot);\n}\n\nasync function toOntologyEntry(collection: LoadedCollection, workspaceRoot: string): Promise<CollectionOntologyEntry> {\n const { schema } = collection;\n return {\n slug: collection.slug,\n title: schema.title,\n icon: schema.icon,\n primaryKey: schema.primaryKey,\n displayField: schema.displayField ?? schema.primaryKey,\n recordCount: await countRecords(collection, workspaceRoot),\n relations: schemaRelations(schema),\n };\n}\n\n/** Build the workspace ontology: every discovered collection (slug-sorted,\n * discovery's order), each with its outbound relations and a\n * readdir-cheap record count — fine to call on demand. */\nexport async function buildWorkspaceOntology(opts: DiscoveryOptions = {}): Promise<CollectionOntologyEntry[]> {\n const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();\n const collections = await discoverCollections(opts);\n return Promise.all(collections.map((collection) => toOntologyEntry(collection, workspaceRoot)));\n}\n","// Server-side compute for a collection's dynamic launcher-shortcut icon\n// (see `CollectionSchema.dynamicIcon`). Wraps the pure resolver in\n// `../core/dynamicIcon` with the one bit of I/O it needs: loading the\n// source collection's raw stored records.\n\nimport { firstDateField, resolveIcon, selectDynamicRecord } from \"../core/dynamicIcon\";\nimport { loadCollection, type DiscoveryOptions } from \"./discovery\";\nimport { storeFor } from \"./store\";\nimport { log } from \"./host\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport type { CollectionItem } from \"../core/schema\";\n\n/** Index `items` by their `primaryKey` value — the `recordsById` map a\n * `valueFrom` reference (e.g. `_config.defaultCity`) resolves against.\n * Items whose primary key isn't a string (shouldn't happen for a valid\n * schema, but records are untyped storage) are skipped rather than\n * coerced, so a broken key never silently shadows a real one. */\nfunction buildRecordsById(items: CollectionItem[], primaryKey: string): Record<string, CollectionItem> {\n const entries = items.filter((item) => typeof item[primaryKey] === \"string\").map((item): [string, CollectionItem] => [String(item[primaryKey]), item]);\n return Object.fromEntries(entries);\n}\n\n/** Order records by their `primaryKey` so record selection is deterministic:\n * `listItems` returns filesystem `readdir` order (arbitrary across machines),\n * which would let `from: \"first\"`, the no-`orderBy` `latest`, and `orderBy`\n * ties pick a different record — and thus a different icon — between\n * reconciles. A stable id sort pins one answer. */\nfunction sortByPrimaryKey(items: CollectionItem[], primaryKey: string): CollectionItem[] {\n return [...items].sort((left, right) => String(left[primaryKey] ?? \"\").localeCompare(String(right[primaryKey] ?? \"\")));\n}\n\n/** Compute the effective launcher icon for `collection`: its static\n * `schema.icon` when it declares no `dynamicIcon`, else the icon\n * resolved from `dynamicIcon.source`'s RAW stored records (no\n * derive/enrich — the icon rules match against stored values) via the\n * pure resolver. Fails soft on any read/discovery error (missing source\n * collection, filesystem error): falls back to `dynamicIcon.fallback ??\n * schema.icon` rather than surfacing to the collections list. */\nexport async function computeCollectionIcon(collection: LoadedCollection, opts: DiscoveryOptions = {}): Promise<string> {\n const { schema } = collection;\n const spec = schema.dynamicIcon;\n if (!spec) return schema.icon;\n try {\n const source = await loadCollection(spec.source.collection, opts);\n if (!source) return spec.fallback ?? schema.icon;\n const items = await storeFor(source, { workspaceRoot: opts.workspaceRoot }).list();\n const ordered = sortByPrimaryKey(items, source.schema.primaryKey);\n const orderBy = spec.source.orderBy ?? firstDateField(source.schema);\n const recordsById = buildRecordsById(ordered, source.schema.primaryKey);\n const record = selectDynamicRecord(ordered, spec.source, orderBy, recordsById);\n return resolveIcon(record, spec, schema.icon, recordsById);\n } catch (err) {\n log.warn(\"collections\", \"dynamic icon compute failed, falling back\", {\n slug: collection.slug,\n source: spec.source.collection,\n error: String(err),\n });\n return spec.fallback ?? schema.icon;\n }\n}\n","// Tiny self-contained helpers ported from the host (server/utils/errors.ts,\n// server/utils/time.ts) so the engine carries no dependency on host utils.\n\n/** Human-readable message from an unknown thrown value. */\nexport function errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\nexport const ONE_SECOND_MS = 1_000;\nexport const ONE_DAY_MS = 24 * 60 * 60 * 1000;\n","// Host-driven recurrence for schema-driven collections.\n//\n// When a record satisfies its schema's `spawn.when` predicate (default:\n// the item is \"done\" per completionField/completionDoneValues), the host\n// creates the NEXT record with a forward-advanced `triggerField` date.\n//\n// The mechanism is CONVERGENT, not event-driven: we reconcile on a\n// predicate (\"matches `when` AND its successor doesn't exist yet\"), and\n// the successor's id + contents are a pure function of (source record,\n// rule). Creation is create-if-absent (`writeItem`'s `refuseOverwrite`),\n// so observing the predicate N times writes the successor exactly once —\n// the successor record's own existence is the \"already spawned?\" flag.\n// No stored side-state, so fs.watch coalescing / boot re-reads / the\n// wall-clock tick can all re-run this freely.\n//\n// All date math operates on the civil (year, month, day) triple — never\n// by adding milliseconds — so month lengths and leap years are handled\n// correctly. The day-of-month anchor is read from the RULE, never\n// re-derived from the prior concrete date, so \"31st of every month\"\n// never drifts (it clamps per-month at compute time, not stored\n// clamped). See `advanceTriggerDate`.\n\nimport { log } from \"./host\";\nimport { errorMessage, ONE_DAY_MS } from \"./util\";\nimport type { IoOptions } from \"./io\";\nimport { storeFor } from \"./store\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport { isFieldDrivenEvery } from \"../core/schema\";\nimport { itemIsDone } from \"../core/completion\";\nimport type { CollectionEvery, CollectionItem, CollectionSchema, CollectionSpawnEvery, CollectionWhen } from \"../core/schema\";\n\n/** A timezone-free calendar date. `m` is 1-12. */\nexport interface CivilDate {\n y: number;\n m: number;\n d: number;\n}\n\nconst YMD_PATTERN = /^(\\d{4})-(\\d{2})-(\\d{2})$/;\n\nfunction pad2(value: number): string {\n return String(value).padStart(2, \"0\");\n}\n\nfunction pad4(value: number): string {\n return String(value).padStart(4, \"0\");\n}\n\n/** Days in `month` (1-12) of `year`, leap-year-aware. `new Date(y, m, 0)`\n * is day 0 of the *next* month = the last day of `month`; `.getDate()`\n * reads the civil day regardless of timezone. */\nexport function daysInMonth(year: number, month: number): number {\n return new Date(year, month, 0).getDate();\n}\n\n/** Parse a `YYYY-MM-DD` string into a CivilDate, or null when the value\n * isn't a well-formed in-range calendar date. */\nexport function parseCivil(raw: unknown): CivilDate | null {\n if (typeof raw !== \"string\") return null;\n const match = YMD_PATTERN.exec(raw.trim());\n if (!match) return null;\n const year = Number(match[1]);\n const month = Number(match[2]);\n const day = Number(match[3]);\n if (month < 1 || month > 12) return null;\n if (day < 1 || day > daysInMonth(year, month)) return null;\n return { y: year, m: month, d: day };\n}\n\n/** `YYYY-MM-DD` for storage in a `date` field. */\nexport function formatCivil(date: CivilDate): string {\n return `${pad4(date.y)}-${pad2(date.m)}-${pad2(date.d)}`;\n}\n\n/** A monotonic integer key for a civil date (YYYYMMDD), for ordering /\n * equality without timezone concerns. */\nfunction ordinal(date: CivilDate): number {\n return date.y * 10000 + date.m * 100 + date.d;\n}\n\n/** Add `n` whole days to a civil date. Uses UTC epoch arithmetic so it\n * is DST-immune (we only ever read back the civil Y/M/D). */\nfunction addDays(date: CivilDate, days: number): CivilDate {\n const shifted = new Date(Date.UTC(date.y, date.m - 1, date.d) + days * ONE_DAY_MS);\n return { y: shifted.getUTCFullYear(), m: shifted.getUTCMonth() + 1, d: shifted.getUTCDate() };\n}\n\n/** Advance a civil date by one `every` step. Month/year units preserve\n * the rule's day-of-month anchor, clamped to the target month's length\n * (no drift); day/week units do civil day arithmetic. */\nexport function advanceTriggerDate(source: CivilDate, every: CollectionEvery): CivilDate {\n const { unit, interval } = every;\n if (unit === \"day\") return addDays(source, interval);\n if (unit === \"week\") return addDays(source, interval * 7);\n // month / year\n const monthsToAdd = interval * (unit === \"year\" ? 12 : 1);\n const total = source.y * 12 + (source.m - 1) + monthsToAdd;\n const nextYear = Math.floor(total / 12);\n const nextMonth = (total % 12) + 1;\n const dim = daysInMonth(nextYear, nextMonth);\n const anchor = every.dayOfMonth === \"last\" ? dim : (every.dayOfMonth ?? source.d);\n return { y: nextYear, m: nextMonth, d: Math.min(anchor, dim) };\n}\n\n/** True iff `now`'s civil date (local timezone) has reached the fire date\n * for `triggerRaw` — i.e. the trigger date minus `leadDays` (so a 10-day\n * lead fires 10 days early). Returns null when `triggerRaw` isn't a\n * parseable date — callers treat that as \"don't fire\" and warn. */\nexport function isTriggerDue(triggerRaw: unknown, now: Date, leadDays = 0): boolean | null {\n const due = parseCivil(triggerRaw);\n if (due === null) return null;\n const fireDate = leadDays > 0 ? addDays(due, -leadDays) : due;\n const today: CivilDate = { y: now.getFullYear(), m: now.getMonth() + 1, d: now.getDate() };\n return ordinal(today) >= ordinal(fireDate);\n}\n\nconst DATE_SUFFIX_PATTERN = /-\\d{8}$/;\n\n/** Deterministic successor id: `<stem>-<YYYYMMDD>`, where `<stem>` is the\n * source id with a trailing `-YYYYMMDD` stripped if present. So a chain\n * shares one stem and each instance is dated:\n * `rent` → `rent-20260610`\n * `rent-20260610` → `rent-20260710`\n * Slug-safe (alphanumeric + hyphen) and a pure function of the inputs,\n * which is what makes create-if-absent idempotent. */\nexport function successorId(sourceId: string, next: CivilDate): string {\n const stem = sourceId.replace(DATE_SUFFIX_PATTERN, \"\");\n return `${stem}-${pad4(next.y)}${pad2(next.m)}${pad2(next.d)}`;\n}\n\n/** True iff `item` satisfies the spawn predicate. With an explicit\n * `when`, matches `String(item[when.field]) ∈ when.in`. Without one,\n * defaults to the completion-done condition — the shared, flag-aware\n * `itemIsDone` (core/completion; downhill import, so the module graph\n * stays acyclic). */\nfunction matchesWhen(when: CollectionWhen | undefined, schema: CollectionSchema, item: CollectionItem): boolean {\n if (when) {\n const raw = item[when.field];\n return raw !== undefined && raw !== null && when.in.includes(String(raw));\n }\n return itemIsDone(schema, item);\n}\n\n/** Resolve the literal `every` that applies to `sourceItem`. Literal-arm\n * `spawn.every` passes through unchanged. Field-driven `spawn.every` reads\n * `sourceItem[fromField]` and looks it up in `map`; an empty field or a\n * value with no map entry yields null (caller skips + logs — see plan §5).\n * Discovery rejects a map that doesn't cover the enum's values, so null\n * here means a record that predates a map/enum edit. */\nexport function resolveEvery(every: CollectionSpawnEvery, sourceItem: CollectionItem): CollectionEvery | null {\n if (!isFieldDrivenEvery(every)) return every;\n const raw = sourceItem[every.fromField];\n if (raw === undefined || raw === null || raw === \"\") return null;\n return every.map[String(raw)] ?? null;\n}\n\nexport interface ComputedSuccessor {\n id: string;\n record: CollectionItem;\n}\n\n/** Build the successor record purely from (schema, source record, source\n * id). Returns null when the schema has no spawn/triggerField or the\n * source's trigger date is unparseable. */\nexport function computeSuccessor(schema: CollectionSchema, sourceItem: CollectionItem, sourceId: string): ComputedSuccessor | null {\n const { spawn, triggerField } = schema;\n if (!spawn || !triggerField) return null;\n const srcDate = parseCivil(sourceItem[triggerField]);\n if (srcDate === null) return null;\n const every = resolveEvery(spawn.every, sourceItem);\n if (every === null) return null;\n const next = advanceTriggerDate(srcDate, every);\n const nextId = successorId(sourceId, next);\n const record: CollectionItem = {};\n for (const field of spawn.carry ?? []) {\n if (Object.prototype.hasOwnProperty.call(sourceItem, field)) record[field] = sourceItem[field];\n }\n Object.assign(record, spawn.set ?? {});\n record[triggerField] = formatCivil(next);\n record[schema.primaryKey] = nextId;\n return { id: nextId, record };\n}\n\n/** Warn precisely about which of `computeSuccessor`'s two null causes fired\n * (plan §5): an unparseable source trigger date (the original cause), or a\n * field-driven `every` whose record value has no `map` entry (a record that\n * predates a map/enum edit — discovery rejects this statically otherwise). */\nfunction logSpawnSkip(slug: string, triggerField: string, every: CollectionSpawnEvery, sourceItem: CollectionItem, sourceId: string): void {\n if (parseCivil(sourceItem[triggerField]) === null) {\n log.warn(\"collections\", \"spawn skipped: source trigger date unparseable\", { slug, sourceId, triggerField });\n return;\n }\n const fromField = isFieldDrivenEvery(every) ? every.fromField : undefined;\n log.warn(\"collections\", \"spawn skipped: no `every` mapping for frequency value\", {\n slug,\n sourceId,\n fromField,\n value: fromField === undefined ? undefined : sourceItem[fromField],\n });\n}\n\n/** Idempotently create the successor for `sourceItem` when it matches the\n * spawn predicate. No-op when the schema declares no spawn, the\n * predicate doesn't match, the trigger date is unparseable, or the\n * successor already exists (create-if-absent). Never overwrites an\n * existing successor — protects any edits the user made to it. */\nexport async function maybeSpawnSuccessor(collection: LoadedCollection, sourceItem: CollectionItem, sourceId: string, ioOpts: IoOptions = {}): Promise<void> {\n const { slug, schema } = collection;\n const { spawn } = schema;\n if (!spawn || !schema.triggerField) return;\n if (!matchesWhen(spawn.when, schema, sourceItem)) return;\n const computed = computeSuccessor(schema, sourceItem, sourceId);\n if (computed === null) {\n logSpawnSkip(slug, schema.triggerField, spawn.every, sourceItem, sourceId);\n return;\n }\n // Runaway guard: a successor born already matching its own `when` would\n // re-spawn on its first reconcile (and so on) — an unbounded chain. A\n // well-formed schema sets the successor to a non-matching state (e.g.\n // `set: { status: \"pending\" }`); refuse + warn if it doesn't. Discovery\n // also rejects this statically, so this is belt-and-suspenders.\n if (matchesWhen(spawn.when, schema, computed.record)) {\n log.warn(\"collections\", \"spawn skipped: successor would be born matching its own predicate (unbounded respawn)\", {\n slug,\n sourceId,\n successorId: computed.id,\n });\n return;\n }\n try {\n // Store-mediated write: works for any writable backend (file, sqlite).\n // A read-only store can't spawn — schema validation forbids `spawn` on\n // dataSource collections, so an absent `write` is defense in depth.\n const { write } = storeFor(collection, ioOpts);\n if (!write) {\n log.warn(\"collections\", \"spawn skipped: collection store is read-only\", { slug, sourceId });\n return;\n }\n const result = await write(computed.id, computed.record, { refuseOverwrite: true });\n if (result.kind === \"ok\") {\n log.info(\"collections\", \"spawned successor\", { slug, sourceId, successorId: computed.id });\n } else if (result.kind !== \"conflict\") {\n // \"conflict\" = successor already exists = idempotent no-op (expected).\n log.warn(\"collections\", \"spawn write failed\", { slug, sourceId, successorId: computed.id, kind: result.kind });\n }\n } catch (err) {\n log.warn(\"collections\", \"spawn write threw\", { slug, sourceId, successorId: computed.id, error: errorMessage(err) });\n }\n}\n","// Delete a user-authored collection, archiving a full restorable copy\n// first. A collection spans three on-disk locations (see\n// docs/papers/collections-architecture.md \"Deleting a collection\"):\n//\n// 1. data/skills/<slug>/ staging — the canonical skill source\n// 2. .claude/skills/<slug>/ active mirror — what discovery scans\n// 3. <schema.dataPath>/ the records (one <id>.json per record)\n//\n// Locations 1 and 2 are a source→mirror pair maintained by the\n// skill-bridge hook, but that hook only fires on the agent's own tool\n// calls — a server-side delete must remove BOTH explicitly. Before\n// anything is removed we write a single skill copy (from the canonical\n// staging dir), the records, and an LLM-runnable RESTORE.md to\n// `archive/<date>-<uuid>/`.\n//\n// Only project-scope, non-preset collections are deletable: user-scope\n// skills (`~/.claude/skills/`) are read-only from MulmoClaude, and a\n// preset (`mc-*`) re-seeds on next boot so deleting it is futile.\n\nimport { cp, mkdir, rm, rmdir, stat, writeFile } from \"node:fs/promises\";\nimport { randomUUID } from \"node:crypto\";\nimport path from \"node:path\";\nimport { log, getWorkspaceRoot, isPresetSlug, skillsStagingDir, archiveDir as archiveRelDir } from \"./host\";\nimport { isContainedInRoot } from \"./paths\";\nimport { checkpointSqliteDatabase } from \"./sqliteStore\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\n\nexport type DeleteCollectionResult =\n | { kind: \"ok\"; slug: string; archivePath: string }\n | { kind: \"user-scope\"; slug: string }\n | { kind: \"preset\"; slug: string }\n | { kind: \"unsafe-data-path\"; slug: string }\n | { kind: \"path-escape\"; slug: string };\n\ntype DeleteRefusal = Exclude<DeleteCollectionResult, { kind: \"ok\" }>;\n\n/** Human-readable reason for a non-`ok` delete result. Exported so the\n * route maps `kind` → message without inlining the switch (keeps the\n * handler short and the mapping unit-testable). The `Record` is\n * exhaustive — a new refusal kind won't compile until it's added. */\nexport function deleteCollectionRefusalMessage(result: DeleteRefusal): string {\n const { slug } = result;\n const messages: Record<DeleteRefusal[\"kind\"], string> = {\n \"user-scope\": `collection '${slug}' is user-scope (~/.claude/skills/) and is read-only from MulmoClaude`,\n preset: `collection '${slug}' is a preset (mc-*) and re-seeds on restart; unstar it from the catalog instead`,\n \"unsafe-data-path\": `collection '${slug}' declares a dataPath outside its own data/${slug}/ subtree; refusing to delete`,\n \"path-escape\": `a directory for collection '${slug}' escapes the workspace`,\n };\n return messages[result.kind];\n}\n\nexport interface DeleteCollectionOptions {\n /** Override the workspace root for containment checks + archive\n * placement. Default: the live `workspacePath`. Tests point this at\n * a `mkdtempSync` tree (same pattern as the IO helpers). */\n workspaceRoot?: string;\n /** Override the `<date>` half of the archive folder name. Tests pass\n * a fixed stamp so the asserted path is deterministic; production\n * leaves it unset and the current UTC date (YYYY-MM-DD) is used. */\n dateStamp?: string;\n}\n\n/** The canonical staging dir for a slug: `data/skills/<slug>`. */\nfunction stagingSkillDir(workspaceRoot: string, slug: string): string {\n return path.join(skillsStagingDir(workspaceRoot), slug);\n}\n\nasync function pathExists(target: string): Promise<boolean> {\n try {\n await stat(target);\n return true;\n } catch {\n return false;\n }\n}\n\n/** UTC `YYYY-MM-DD` — keeps the archive folder human-sortable. */\nfunction todayStamp(): string {\n return new Date().toISOString().slice(0, 10);\n}\n\n/** Every directory the delete will touch must resolve under the\n * workspace root — guards against a symlinked ancestor escaping it. */\nfunction deleteTargets(collection: LoadedCollection, workspaceRoot: string): string[] {\n return [\n stagingSkillDir(workspaceRoot, collection.slug),\n collection.skillDir,\n collection.dataDir,\n ...(collection.storageFile !== undefined ? [collection.storageFile] : []),\n ];\n}\n\n/** The records directory the delete recursively archives + removes\n * (`collection.dataDir`) must live in this collection's OWN per-slug\n * subtree. `dataDir` is normally derived from `schema.dataPath`, but\n * `deleteCollection` accepts a `LoadedCollection` whose fields could be\n * inconsistent — so we validate the RESOLVED target the destructive ops\n * actually touch, not the schema string. A shared root like `data` or\n * `data/skills` would otherwise turn the recursive removal into a\n * workspace-wide wipe whose archive captures only this collection.\n * `path.resolve` collapses any `..` before the prefix test (symlink\n * escapes are caught separately by the realpath containment check in\n * `deleteTargets`).\n *\n * TWO acceptable per-slug subtrees:\n * - `data/<slug>/...` — the convention authored collections default to.\n * - `data/collections/<slug>/...` — what `normalizedDataPath` in the\n * registry-import path stamps onto every imported schema (so imported\n * collections never collide with `data/<slug>/`-shaped authored ones).\n * Both are equally per-collection and equally safe; a delete that targets\n * anything else (e.g. raw `data/`, `data/wiki/`, a sibling slug) is\n * refused. */\nfunction isDataDirSafe(dataDir: string, slug: string, workspaceRoot: string): boolean {\n const acceptableRoots = [path.resolve(workspaceRoot, \"data\", slug), path.resolve(workspaceRoot, \"data\", \"collections\", slug)];\n const resolved = path.resolve(dataDir);\n return acceptableRoots.some((root) => resolved === root || resolved.startsWith(root + path.sep));\n}\n\n/** Step 2 of the restore doc — how to bring the records back. A\n * `dataSource` collection has no record files to copy (its rows live in\n * the external data file, which the delete never touches). */\nfunction restoreRecordsStep(schema: LoadedCollection[\"schema\"]): string {\n if (schema.storage !== undefined) {\n return `2. Records: copy the archived database file\n \\`${path.basename(schema.storage.path)}\\` (next to this document) back to\n \\`${schema.storage.path}\\` (workspace-relative, \\`cp\\`). It holds every\n record. If \\`-wal\\`/\\`-journal\\` sidecar files were archived alongside\n it, copy them back too.`;\n }\n if (schema.dataPath === undefined) {\n return `2. Records: nothing to copy. This is a \\`dataSource\\` collection —\n its records are the rows of \\`${schema.dataSource?.path}\\`, which the\n delete never touched.`;\n }\n return `2. Copy the item data: \\`cp\\` every file under \\`records/\\` into\n \\`${schema.dataPath}/\\`. The records are part of the collection and\n must be restored. They are plain data files (NOT bridged), so use\n \\`cp\\` — the Write-tool rule in step 1 applies ONLY to the skill\n files, not to these records (there may be many; copy them, do not\n Write them one by one).`;\n}\n\nfunction buildRestoreDoc(collection: LoadedCollection): string {\n const { slug, schema } = collection;\n return `# Restore \"${schema.title}\" (collection \\`${slug}\\`)\n\nThis folder is an automatic backup made when the collection was deleted.\nFollow these steps to restore it.\n\n1. Recreate the skill files in \\`data/skills/${slug}/\\` using the **Write\n tool**: read each file under \\`skill/\\` and Write it to the matching\n path — \\`SKILL.md\\`, \\`schema.json\\`, and any \\`templates/*\\`.\n\n IMPORTANT — use the Write tool, NOT \\`cp\\` / \\`mv\\` / a shell redirect.\n The skill-bridge hook mirrors \\`data/skills/${slug}/\\` into\n \\`.claude/skills/${slug}/\\`, and that mirror is what actually registers\n the collection. The hook only fires on Write/Edit tool calls, so a\n \\`cp\\` would leave the files in staging with no \\`.claude/skills/\\`\n mirror — the collection would stay invisible. (Writing\n \\`.claude/skills/\\` directly is not an option either: that path is\n permission-gated.)\n\n${restoreRecordsStep(schema)}\n\n3. Confirm the collection reappears at \\`/collections/${slug}\\`.\n\n- slug: \\`${slug}\\`\n- title: ${schema.title}\n- dataPath: \\`${schema.dataPath ?? (schema.storage !== undefined ? `(storage) ${schema.storage.path}` : `(dataSource) ${schema.dataSource?.path}`)}\\`\n`;\n}\n\n/** Copy one skill copy + the records + RESTORE.md into `archiveDir`. */\nasync function writeArchive(collection: LoadedCollection, archiveDir: string, workspaceRoot: string): Promise<void> {\n // Prefer the canonical staging dir; fall back to the active mirror\n // for a project collection that was created without the bridge.\n const staging = stagingSkillDir(workspaceRoot, collection.slug);\n const skillSrc = (await pathExists(staging)) ? staging : collection.skillDir;\n await cp(skillSrc, path.join(archiveDir, \"skill\"), { recursive: true });\n if (await pathExists(collection.dataDir)) {\n await cp(collection.dataDir, path.join(archiveDir, \"records\"), { recursive: true });\n }\n // A `storage` collection's records live in its database file (the\n // dataDir is a phantom) — archive it under its own basename so RESTORE\n // can point `storage.path` back at it. Checkpoint first so the main file\n // alone is a complete snapshot (in WAL mode, committed rows can live\n // only in `<db>-wal`); if the checkpoint fails (older Node, locked db),\n // archive the sidecars too — either way no committed data is lost.\n // (`dataSourceFile` is deliberately NOT archived or removed: an external\n // dataSource file is user-owned.)\n if (collection.storageFile !== undefined && (await pathExists(collection.storageFile))) {\n const checkpointed = await checkpointSqliteDatabase(collection.storageFile);\n await cp(collection.storageFile, path.join(archiveDir, path.basename(collection.storageFile)));\n if (!checkpointed) {\n for (const suffix of [\"-wal\", \"-journal\", \"-shm\"]) {\n const sidecar = `${collection.storageFile}${suffix}`;\n if (await pathExists(sidecar)) await cp(sidecar, path.join(archiveDir, path.basename(sidecar)));\n }\n }\n }\n await writeFile(path.join(archiveDir, \"RESTORE.md\"), buildRestoreDoc(collection), \"utf-8\");\n}\n\n/** Remove all three locations. `rm -rf`-style (force) so a missing dir\n * is a no-op; the now-empty data parent (`data/<slug>/` after its\n * `items/` is gone) is swept too, but only when empty. */\nasync function removeLocations(collection: LoadedCollection, workspaceRoot: string): Promise<void> {\n await rm(stagingSkillDir(workspaceRoot, collection.slug), { recursive: true, force: true });\n await rm(collection.skillDir, { recursive: true, force: true });\n await rm(collection.dataDir, { recursive: true, force: true });\n await rmdir(path.dirname(collection.dataDir)).catch(() => undefined);\n // The archived copy above is the backup; remove the live db (+ sqlite\n // sidecars) so a deleted collection doesn't leave orphaned data behind.\n if (collection.storageFile !== undefined) {\n await rm(collection.storageFile, { force: true });\n await rm(`${collection.storageFile}-wal`, { force: true });\n await rm(`${collection.storageFile}-journal`, { force: true });\n await rm(`${collection.storageFile}-shm`, { force: true });\n }\n}\n\nexport async function deleteCollection(collection: LoadedCollection, opts: DeleteCollectionOptions = {}): Promise<DeleteCollectionResult> {\n const { slug } = collection;\n const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();\n if (collection.source === \"user\") return { kind: \"user-scope\", slug };\n if (isPresetSlug(slug)) return { kind: \"preset\", slug };\n if (!isDataDirSafe(collection.dataDir, slug, workspaceRoot)) {\n log.warn(\"collections\", \"deleteCollection refused: dataDir is not under the per-collection root\", { slug, dataDir: collection.dataDir });\n return { kind: \"unsafe-data-path\", slug };\n }\n if (deleteTargets(collection, workspaceRoot).some((target) => !isContainedInRoot(target, workspaceRoot))) {\n log.warn(\"collections\", \"deleteCollection refused: a target escapes the workspace\", { slug });\n return { kind: \"path-escape\", slug };\n }\n const archiveRel = path.join(archiveRelDir(), `${opts.dateStamp ?? todayStamp()}-${randomUUID()}`);\n const archiveDir = path.join(workspaceRoot, archiveRel);\n await mkdir(archiveDir, { recursive: true });\n await writeArchive(collection, archiveDir, workspaceRoot);\n await removeLocations(collection, workspaceRoot);\n log.info(\"collections\", \"collection deleted + archived\", { slug, archive: archiveRel });\n return { kind: \"ok\", slug, archivePath: archiveRel };\n}\n","// Server-side custom-view management: removing a collection's custom view.\n//\n// A custom view spans two on-disk facts that must be removed together:\n// 1. an entry in the collection's schema.json `views[]` array\n// 2. its HTML file at `<base>/views/<file>` (the entry's `file` field)\n//\n// The base dir is source-aware, mirroring `readCustomViewHtml`. A PROJECT\n// collection authored in-place keeps schema + view HTML in the staging tree\n// (`data/skills/<slug>/`) AND mirrors schema into the active dir\n// (`.claude/skills/<slug>/`, i.e. `collection.skillDir`). A PROJECT collection\n// IMPORTED via the discover panel (rename-on-conflict) lives entirely under\n// the active dir — no staging mirror is created. We pick the canonical base\n// (and the schema-write set) by *what's actually on disk*, so both layouts\n// delete cleanly without ENOENT. The skill-bridge hook that normally keeps\n// staging+active in sync only fires on the agent's own tool calls, never from\n// an API route — exactly as `deleteCollection` reasons. A FEED / USER\n// collection is a single tree at `collection.skillDir`.\n//\n// Custom-view HTML is staging-only for project collections (never mirrored —\n// rendering is host-side), so only the canonical base's copy is unlinked.\n\nimport { readFile, stat, unlink } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { writeFileAtomic } from \"./atomic\";\nimport { getWorkspaceRoot, isPresetSlug, skillsStagingDir } from \"./host\";\nimport { resolveTemplatePath, safeSlugName, SCHEMA_FILE } from \"./paths\";\nimport type { IoOptions } from \"./io\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\n\nexport type DeleteViewResult =\n | { kind: \"ok\"; viewId: string }\n | { kind: \"not-found\"; viewId: string }\n | { kind: \"user-scope\" }\n | { kind: \"preset\" }\n | { kind: \"unsafe-path\"; viewId: string };\n\nasync function fileExists(target: string): Promise<boolean> {\n try {\n await stat(target);\n return true;\n } catch (err) {\n const { code } = err as { code?: string };\n if (code === \"ENOENT\" || code === \"ENOTDIR\") return false;\n throw err;\n }\n}\n\n/** The authoritative base dir for a collection's schema.json + view HTML.\n * For a project collection, prefer the staging tree when its schema.json is\n * present (authoring layout); otherwise fall back to the active skill dir\n * (imported layout — staging never materialised). For feed / user, it's\n * always the discovered skillDir. Matches `readCustomViewHtml` so reads and\n * deletes agree on both layouts. */\nasync function canonicalBase(collection: Pick<LoadedCollection, \"source\" | \"skillDir\">, workspaceRoot: string, safeSlug: string): Promise<string> {\n if (collection.source !== \"project\") return collection.skillDir;\n const staging = path.join(skillsStagingDir(workspaceRoot), safeSlug);\n if (await fileExists(path.join(staging, SCHEMA_FILE))) return staging;\n return collection.skillDir;\n}\n\n/** Every on-disk schema.json that must reflect the removal. The active\n * `<skillDir>/schema.json` is the discovery anchor and is always present.\n * The staging copy is included only when it actually exists, so an imported\n * project collection (no staging mirror) doesn't have an empty staging tree\n * materialised by a side effect of the delete. */\nasync function schemaWriteTargets(collection: Pick<LoadedCollection, \"source\" | \"skillDir\">, workspaceRoot: string, safeSlug: string): Promise<string[]> {\n const active = path.join(collection.skillDir, SCHEMA_FILE);\n if (collection.source !== \"project\") return [active];\n const stagingSchema = path.join(skillsStagingDir(workspaceRoot), safeSlug, SCHEMA_FILE);\n const targets: string[] = [];\n if (await fileExists(stagingSchema)) targets.push(stagingSchema);\n targets.push(active);\n return targets;\n}\n\n/** Idempotent unlink — a missing file is fine (the schema entry still gets\n * cleaned), but a real error (permissions, etc.) propagates. */\nasync function unlinkIfPresent(target: string): Promise<void> {\n try {\n await unlink(target);\n } catch (err) {\n if ((err as { code?: string }).code !== \"ENOENT\") throw err;\n }\n}\n\n/** Re-read the canonical schema.json, drop the `views[]` entry, and write the\n * result back to every on-disk copy so staging + active stay identical. Reads\n * raw (not `collection.schema`) so fields the typed schema doesn't model are\n * preserved verbatim. */\nasync function removeViewFromSchemas(collection: LoadedCollection, viewId: string, workspaceRoot: string, safeSlug: string): Promise<void> {\n const base = await canonicalBase(collection, workspaceRoot, safeSlug);\n const canonical = path.join(base, SCHEMA_FILE);\n const parsed = JSON.parse(await readFile(canonical, \"utf-8\")) as { views?: { id?: unknown }[] };\n if (Array.isArray(parsed.views)) parsed.views = parsed.views.filter((entry) => entry?.id !== viewId);\n const serialized = `${JSON.stringify(parsed, null, 2)}\\n`;\n for (const target of await schemaWriteTargets(collection, workspaceRoot, safeSlug)) {\n await writeFileAtomic(target, serialized);\n }\n}\n\n/** Delete one custom view from `collection`: unlink its HTML file and drop it\n * from every schema.json copy. User-scope and preset (mc-*) collections are\n * refused (read-only / re-seeded on boot), consistent with `deleteCollection`. */\nexport async function deleteCustomView(collection: LoadedCollection, viewId: string, opts: IoOptions = {}): Promise<DeleteViewResult> {\n if (collection.source === \"user\") return { kind: \"user-scope\" };\n if (isPresetSlug(collection.slug)) return { kind: \"preset\" };\n const safeSlug = safeSlugName(collection.slug);\n if (safeSlug === null) return { kind: \"unsafe-path\", viewId };\n const views = collection.schema.views ?? [];\n const view = views.find((entry) => entry.id === viewId);\n if (!view) return { kind: \"not-found\", viewId };\n const workspaceRoot = opts.workspaceRoot ?? getWorkspaceRoot();\n const htmlPath = resolveTemplatePath(await canonicalBase(collection, workspaceRoot, safeSlug), view.file);\n if (htmlPath === null) return { kind: \"unsafe-path\", viewId };\n // Rewrite the schema BEFORE unlinking: if the write fails the request errors\n // out, but the HTML stays put and the still-registered view keeps working —\n // an orphaned `views[]` entry pointing at a deleted file would 404 forever.\n await removeViewFromSchemas(collection, viewId, workspaceRoot, safeSlug);\n // Distinct ids may point at the same `file` (unique ids are enforced, unique\n // files are not), so only unlink when no remaining view still references it.\n const stillReferenced = views.some((entry) => entry.id !== viewId && entry.file === view.file);\n if (!stillReferenced) await unlinkIfPresent(htmlPath);\n return { kind: \"ok\", viewId };\n}\n","// Preset-skill namespace — the ONE definition of \"is this a launcher-managed preset\n// skill\". Browser-safe (no node imports) so the Vue UI can import it via the package's\n// `./slug` entry while the server-only sync logic (`.` entry, uses node:fs) imports it\n// here too. Shared by MulmoClaude and MulmoTerminal so the `mc-` convention can't drift.\n\n/** Launcher preset namespace. Preset skills (and only those) are `mc-*`. */\nexport const PRESET_SLUG_PREFIX = \"mc-\";\n\n/** True for a launcher-managed preset slug (`mc-<something>`). The sync logic uses\n * this to bound which active skills it may refresh/prune, so user-authored skills are\n * never touched. */\nexport function isPresetSlug(slug: string): boolean {\n return slug.startsWith(PRESET_SLUG_PREFIX) && slug.length > PRESET_SLUG_PREFIX.length;\n}\n","// `manageCollection` is the agent's data plane for schema-driven\n// collections — the paved road over the same record files that raw\n// Read/Write/Edit reach (the workspace stays the database; this tool is\n// a convenience + gate, not a second store):\n//\n// - getItems: records WITH the host-computed fields the stored JSON\n// never contains — `derived` formulas evaluated (cross-collection\n// derefs included), `toggle` projected, `embed` resolved — i.e. the\n// same numbers the user sees rendered. One call instead of N file\n// Reads plus a mental join.\n// - putItems: rows validated against the schema BEFORE the write\n// (primaryKey↔id, required fields, enum membership, no computed\n// keys), written atomically, with per-row accept/reject results the\n// model can fix and retry — instead of writing a broken file and\n// meeting it later in the presentCollection repair loop.\n// - deleteItems: removal by id, so a collection whose records aren't\n// files (or whose caller shouldn't be handed raw unlink) still has a\n// delete. A missing id rejects rather than reporting success.\n// - getOntology: the machine-readable workspace ontology — every\n// collection with its record count and outbound ref/embed relations,\n// so a cross-collection question starts from the map instead of\n// re-reading every schema.\n//\n// It is also the paved road for a collection's STRUCTURE — so an edit\n// gets the same authoring reference + validation a create does:\n//\n// - schemaDocs: the collection-authoring reference (`collection-skills.md`)\n// delivered as a method, so the agent never needs to know the help\n// file's path or that it exists — the gap that made schema EDITS fail\n// (create-time prompts point at the doc; edit-time had no pointer).\n// - getSchema / putSchema: read the raw schema.json, and validate it\n// against `CollectionSchemaZ` BEFORE writing the canonical staging\n// copy + mirroring it active (an internal write skips the skill-bridge\n// hook, so the mirror is explicit). Edit-only; creation stays the\n// normal \"write SKILL.md + schema.json under data/skills/\" flow.\n//\n// Shared by both hosts (MulmoClaude's mcp-tools shim + MulmoTerminal):\n// everything host-specific rides in `ManageCollectionDeps` — the\n// workspace root falls back to the configured collection host\n// (`getWorkspaceRoot`), the post-putSchema UI refresh and the\n// evaluation-only validation ablation are injected, and tests point the\n// whole tool at a tmpdir workspace via the same deps.\n\nimport { readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { COMPUTED_TYPES } from \"../core/schema\";\nimport type { CollectionItem, CollectionSchema } from \"../core/schema\";\nimport { CollectionSchemaZ } from \"../core/schemaZ\";\nimport { CollectionQueryZ } from \"../core/queryZ\";\nimport { defangForPrompt } from \"../core/promptSafety\";\nimport { loadCollection, type DiscoveryOptions } from \"./discovery\";\nimport type { LoadedCollection } from \"./discoveredCollection\";\nimport { resolveCreateItemId } from \"./io\";\nimport { readOnlyRefusal, storeFor, type CollectionStore } from \"./store\";\nimport { runCollectionQuery } from \"./queryRunner\";\nimport { enrichItems } from \"./derive\";\nimport { validateCollectionRecords, validateRecordObject } from \"./validate\";\nimport { buildWorkspaceOntology } from \"./ontology\";\nimport { resolveDataDir } from \"./paths\";\nimport { getWorkspaceRoot } from \"./host\";\nimport { writeFileAtomic } from \"./atomic\";\nimport { dataSkillDir, mirrorSkillWrite } from \"../../skill-bridge/index.js\";\n// NOTE: only the browser-safe `slug` module — workspace-setup's assets.ts uses\n// `import.meta.url` and is ESM-only (build pass 2), while this entry builds\n// dual ESM+CJS. The bundled-docs dir is injected instead (`bundledHelpsDir`).\nimport { isPresetSlug } from \"../../workspace-setup/slug\";\n\n/** Refuse an unselective getItems beyond this many records — a silent\n * truncation would read as \"covered everything\", and an unbounded dump\n * of a large collection is a token bomb. `ids` or `fields` lifts it. */\nexport const MAX_UNSELECTIVE_ITEMS = 200;\n\n/** schema.json basename under a skill dir (canonical staging + active mirror). */\nconst SCHEMA_FILE = \"schema.json\";\n/** The collection-authoring reference, served by the `schemaDocs` action. */\nconst SCHEMA_DOCS_FILE = \"collection-skills.md\";\n/** Cap the rejected-schema issue list so a deeply-broken schema can't flood the result. */\nexport const MAX_SCHEMA_ISSUES = 20;\n/** The workspace help-docs dir both hosts seed (`@mulmoclaude/core/workspace-setup`\n * syncs the bundled assets here) — the user-editable copy schemaDocs prefers. */\nconst HELPS_DIR = \"config/helps\";\n\n/** Workspace-targeting overrides, threaded to every collections call.\n * Production: `{}` (the configured collection host's workspace).\n * Tests: a tmpdir + empty user skills dir. `refreshAfterWrite` is the\n * best-effort UI-refresh fired after a `putSchema` write — hosts with\n * schema-driven side state (MulmoClaude's scheduled skills / user\n * tasks) inject their refreshers; omitted, no refresh runs (discovery\n * re-reads schema.json on every call, so only a live UI update is\n * delayed, never the data). */\nexport type ManageCollectionDeps = DiscoveryOptions & {\n refreshAfterWrite?: () => Promise<void>;\n /** Evaluation-only: skip pre-write record validation in putItems and\n * the getItems record-issue scan. MulmoClaude's production singleton\n * binds this from its ablation env; leave unset everywhere else. */\n ablateValidation?: boolean;\n /** The host's bundled help-docs dir (workspace-setup's `helpsAssetDir()`)\n * — the `schemaDocs` fallback when the workspace has no `config/helps`\n * copy. Injected because that module is ESM-only (`import.meta.url`)\n * while this entry builds dual ESM+CJS. Omitted, only the workspace\n * copy is tried. */\n bundledHelpsDir?: () => string;\n};\n\n/** Resolve the workspace root the same way every collections call does:\n * the injected override (tests) or the configured collection host. */\nfunction resolveBase(deps: ManageCollectionDeps): string {\n return deps.workspaceRoot ?? getWorkspaceRoot();\n}\n\n/** Shared \"unknown collection\" message — its schema.json is missing or\n * failed validation, so discovery skipped it. */\nfunction unknownCollection(slug: string): string {\n return `manageCollection: unknown collection '${defangForPrompt(slug)}' — its schema.json is missing or failed validation.`;\n}\n\ninterface GetItemsArgs {\n slug: string;\n ids?: string[];\n fields?: string[];\n}\n\ntype PutMode = \"upsert\" | \"create\" | \"merge\";\n\ninterface PutItemsArgs {\n slug: string;\n items: CollectionItem[];\n mode: PutMode;\n}\n\nfunction optionalStringArray(value: unknown, name: string): { ok: true; value?: string[] } | { ok: false; error: string } {\n if (value === undefined) return { ok: true };\n if (!Array.isArray(value) || !value.every((entry) => typeof entry === \"string\" && entry.length > 0)) {\n return { ok: false, error: `manageCollection: \\`${name}\\` must be an array of non-empty strings when present.` };\n }\n return { ok: true, value: value as string[] };\n}\n\n/** Project a record down to the requested fields. The primary key is\n * always kept so every returned record stays addressable for a\n * follow-up getItems/putItems. */\nfunction projectFields(record: CollectionItem, fields: string[], primaryKey: string): CollectionItem {\n const keys = fields.includes(primaryKey) ? fields : [primaryKey, ...fields];\n return Object.fromEntries(keys.filter((key) => key in record).map((key) => [key, record[key]]));\n}\n\n/** The validation warning appended to a getItems result when stored\n * record files are malformed (they're silently skipped at read time,\n * so without this they'd just look missing). Issue strings quote\n * record-controlled text → defanged, mirroring the presentCollection\n * dispatch. The record VALUES in `items` ride verbatim, like a raw\n * file Read — only host-composed report strings are defanged. */\nasync function recordIssuesWarning(collection: LoadedCollection, deps: ManageCollectionDeps): Promise<string | undefined> {\n if (deps.ablateValidation) return undefined;\n const issues = await validateCollectionRecords(collection, { workspaceRoot: deps.workspaceRoot });\n if (issues.length === 0) return undefined;\n const lines = issues.map((issue) => `- ${defangForPrompt(issue.file)}: ${defangForPrompt(issue.problem)}`).join(\"\\n\");\n return `${issues.length} record file(s) have data problems and are missing from this result. Fix each (Read → correct → Write):\\n${lines}`;\n}\n\nasync function loadRequestedItems(\n collection: LoadedCollection,\n ids: string[] | undefined,\n deps: ManageCollectionDeps,\n): Promise<{ items: CollectionItem[]; missing: string[] }> {\n const store = storeFor(collection, { workspaceRoot: deps.workspaceRoot });\n if (!ids) return { items: await store.list(), missing: [] };\n const items: CollectionItem[] = [];\n const missing: string[] = [];\n for (const recordId of ids) {\n // The file store's read THROWS on a malformed record file (only ENOENT\n // is null) — for the tool that's a `missing` entry, not a failed call:\n // the warning scan that runs whenever something is missing then names\n // the broken file and how to fix it.\n const item = await store.read(recordId).catch(() => null);\n if (item) items.push(item);\n else missing.push(recordId);\n }\n return { items, missing };\n}\n\nasync function handleGetItems(collection: LoadedCollection, args: GetItemsArgs, deps: ManageCollectionDeps): Promise<string> {\n const { items, missing } = await loadRequestedItems(collection, args.ids, deps);\n if (!args.ids && !args.fields && items.length > MAX_UNSELECTIVE_ITEMS) {\n return `manageCollection: refused — '${collection.slug}' has ${items.length} records, over the unselective limit of ${MAX_UNSELECTIVE_ITEMS}. Pass \\`ids\\` for specific records or \\`fields\\` to project only the columns you need.`;\n }\n const enriched = await enrichItems(collection, items, deps);\n const projected = args.fields ? enriched.map((item) => projectFields(item, args.fields as string[], collection.schema.primaryKey)) : enriched;\n // The warning scan reads every record file, so don't pay it on a\n // selective read that found everything it asked for — only a full\n // listing (where a malformed file silently looks absent) or a missing\n // requested id (where the scan explains WHY it's missing) needs it.\n const warning = !args.ids || missing.length > 0 ? await recordIssuesWarning(collection, deps) : undefined;\n return JSON.stringify({\n collection: collection.slug,\n count: projected.length,\n items: projected,\n ...(missing.length > 0 ? { missing: missing.map((recordId) => defangForPrompt(recordId)) } : {}),\n ...(warning ? { warning } : {}),\n });\n}\n\n/** Reject writes that set host-computed keys, with a pointer at the\n * writable source of truth (the toggle's enum) where one exists. */\nfunction computedKeyProblem(record: CollectionItem, schema: CollectionSchema): string | null {\n for (const key of Object.keys(record)) {\n const spec = schema.fields[key];\n if (!spec || !COMPUTED_TYPES.has(spec.type)) continue;\n if (spec.type === \"toggle\" && spec.field) return `'${key}' is a toggle projection — write the enum field '${spec.field}' instead`;\n const kindLabel: Record<string, string> = { derived: \"derived\", embed: \"an embed\", backlinks: \"a backlinks view\", rollup: \"a rollup\" };\n return `'${key}' is ${kindLabel[spec.type] ?? \"computed\"} — computed by the host, remove it from the record`;\n }\n return null;\n}\n\ninterface RejectedRow {\n id: string;\n problem: string;\n}\n\n/** `mode: \"merge\"` resolves the row against the EXISTING record —\n * a partial row updates just the fields it carries, instead of a\n * whole-record upsert silently erasing the optional fields it omits\n * (an upsert of `{id, status}` would pass validation yet drop\n * `notes`/`lesson`/…). Merge is a partial UPDATE by definition, so a\n * missing record is a reject, not an implicit create — a merged-over-\n * nothing partial record is exactly the data shape this mode exists\n * to prevent.\n *\n * Computed keys found in the STORED record are stripped before the\n * merge: the caller's own row was already computed-key-rejected, but a\n * raw-written / legacy record can carry a stale `derived`/`embed`/\n * `toggle` value, and re-writing it would perpetuate a forged\n * host-computed value. A merge heals the record instead.\n *\n * the store read THROWS on a malformed stored file (only ENOENT is null) —\n * downgraded to a per-row rejection here, like loadRequestedItems'\n * `missing`, so one broken file can't abort the whole putItems batch. */\nasync function mergeWithExisting(\n collection: LoadedCollection,\n store: CollectionStore,\n record: CollectionItem,\n itemId: string,\n): Promise<CollectionItem | string> {\n let existing: CollectionItem | null;\n try {\n existing = await store.read(itemId);\n } catch {\n return `'${itemId}' has a malformed stored file — mode \"merge\" needs to read it; fix the file (Read → correct → Write) or replace it whole with \"upsert\"`;\n }\n if (!existing) return `'${itemId}' not found — mode \"merge\" updates an existing record; use \"upsert\" or \"create\" to add it`;\n const stored = Object.entries(existing).filter(([key]) => {\n const spec = collection.schema.fields[key];\n return !spec || !COMPUTED_TYPES.has(spec.type);\n });\n return { ...Object.fromEntries(stored), ...record };\n}\n\nasync function putOneItem(\n collection: LoadedCollection,\n store: CollectionStore,\n write: NonNullable<CollectionStore[\"write\"]>,\n record: CollectionItem,\n mode: PutMode,\n deps: ManageCollectionDeps,\n): Promise<{ written?: string; rejected?: RejectedRow }> {\n const { schema } = collection;\n const itemId = resolveCreateItemId(schema, record);\n const reject = (about: string, problem: string): { rejected: RejectedRow } => ({\n rejected: { id: defangForPrompt(about), problem: defangForPrompt(problem) },\n });\n if (itemId === null) return reject(\"(no id)\", `record has no '${schema.primaryKey}' value — set it (it doubles as the filename)`);\n const computed = computedKeyProblem(record, schema);\n if (computed) return reject(itemId, computed);\n let toWrite = record;\n if (mode === \"merge\") {\n const merged = await mergeWithExisting(collection, store, record, itemId);\n if (typeof merged === \"string\") return reject(itemId, merged);\n toWrite = merged;\n }\n if (!deps.ablateValidation) {\n const invalid = validateRecordObject(toWrite, itemId, schema);\n if (invalid) return reject(itemId, invalid);\n }\n const result = await write(itemId, toWrite, { refuseOverwrite: mode === \"create\" });\n if (result.kind === \"ok\") return { written: result.itemId };\n if (result.kind === \"invalid-id\")\n return reject(itemId, `'${itemId}' is not a valid record id (letters/digits at the ends; -, _, or . inside; no '..' or path characters)`);\n if (result.kind === \"conflict\") return reject(itemId, `'${itemId}' already exists — mode \"create\" refuses overwrite; use \"upsert\" to update it`);\n return reject(itemId, \"write refused: the collection's data dir escapes the workspace\");\n}\n\n/** Aggregation over a collection via the structured query DSL\n * (`core/queryZ.ts`) — the paved road for counts / sums / group-bys\n * that a row listing can't answer honestly. The engine choice (native\n * store query vs enrich-then-JSONL fallback) lives in\n * `runCollectionQuery`, shared with the desktop custom view's `/query`\n * route so the two surfaces can never drift. */\nasync function handleQueryItems(collection: LoadedCollection, queryArg: unknown, deps: ManageCollectionDeps): Promise<string> {\n const parsed = CollectionQueryZ.safeParse(queryArg);\n if (!parsed.success) {\n const lines = parsed.error.issues\n .slice(0, MAX_SCHEMA_ISSUES)\n .map((issue) => `- ${issue.path.map(String).join(\".\") || \"(root)\"}: ${defangForPrompt(issue.message)}`);\n return `manageCollection: \\`query\\` rejected — fix and retry:\\n${lines.join(\"\\n\")}`;\n }\n const rows = await runCollectionQuery(collection, parsed.data, { workspaceRoot: deps.workspaceRoot });\n return JSON.stringify({ collection: collection.slug, count: rows.length, rows });\n}\n\nasync function handlePutItems(collection: LoadedCollection, args: PutItemsArgs, deps: ManageCollectionDeps): Promise<string> {\n // Server-enforced read-only: a `dataSource` collection's rows live in\n // the external data file — point the agent at the real update path\n // instead of writing phantom record files. The store encodes this as an\n // absent `write` method.\n const store = storeFor(collection, { workspaceRoot: deps.workspaceRoot });\n const { write } = store;\n if (!write) {\n return `manageCollection: ${readOnlyRefusal(collection.slug)} (its records are the rows of '${collection.schema.dataSource?.path}'; edit that file to change the data).`;\n }\n const written: string[] = [];\n const rejected: RejectedRow[] = [];\n for (const record of args.items) {\n const outcome = await putOneItem(collection, store, write, record, args.mode, deps);\n if (outcome.written) written.push(outcome.written);\n if (outcome.rejected) rejected.push(outcome.rejected);\n }\n return JSON.stringify({ collection: collection.slug, written, rejected });\n}\n\n/** Delete records by id, through the store — so it works on any writable\n * backend, not just file records. Same read-only refusal as putItems (an\n * absent `delete` IS the refusal), and the same per-id result shape so a\n * partially-bad batch reports per id instead of failing whole. */\nasync function handleDeleteItems(collection: LoadedCollection, ids: string[], deps: ManageCollectionDeps): Promise<string> {\n const { delete: removeItem } = storeFor(collection, { workspaceRoot: deps.workspaceRoot });\n if (!removeItem) {\n return `manageCollection: ${readOnlyRefusal(collection.slug)} (its records are the rows of '${collection.schema.dataSource?.path}'; edit that file to change the data).`;\n }\n const deleted: string[] = [];\n const rejected: RejectedRow[] = [];\n for (const itemId of ids) {\n const outcome = await deleteOneItem(removeItem, itemId);\n if (outcome.deleted) deleted.push(outcome.deleted);\n if (outcome.rejected) rejected.push(outcome.rejected);\n }\n return JSON.stringify({ collection: collection.slug, deleted, rejected });\n}\n\n/** A missing id is a rejection, not a silent success: deleting by a typo'd\n * id would otherwise report \"done\" while the real record survives. */\nasync function deleteOneItem(removeItem: NonNullable<CollectionStore[\"delete\"]>, itemId: string): Promise<{ deleted?: string; rejected?: RejectedRow }> {\n const result = await removeItem(itemId);\n if (result.kind === \"ok\") return { deleted: result.itemId };\n const reject = (problem: string): { rejected: RejectedRow } => ({ rejected: { id: defangForPrompt(itemId), problem: defangForPrompt(problem) } });\n if (result.kind === \"invalid-id\")\n return reject(`'${itemId}' is not a valid record id (letters/digits at the ends; -, _, or . inside; no '..' or path characters)`);\n if (result.kind === \"not-found\") return reject(`'${itemId}' not found — nothing was deleted; confirm the id with getItems`);\n return reject(\"delete refused: the collection's data dir escapes the workspace\");\n}\n\nfunction parseDeleteIds(args: Record<string, unknown>): string[] | string {\n const { ids } = args;\n const valid = Array.isArray(ids) && ids.length > 0 && ids.every((entry) => typeof entry === \"string\" && entry.trim().length > 0);\n if (!valid) return \"manageCollection: `ids` is required for deleteItems — a non-empty array of record ids.\";\n return ids as string[];\n}\n\nfunction parsePutItems(args: Record<string, unknown>, slug: string): PutItemsArgs | string {\n const { items, mode } = args;\n const validItems = Array.isArray(items) && items.length > 0 && items.every((entry) => Boolean(entry) && typeof entry === \"object\" && !Array.isArray(entry));\n if (!validItems) return \"manageCollection: `items` is required for putItems — a non-empty array of record objects.\";\n if (mode !== undefined && mode !== \"upsert\" && mode !== \"create\" && mode !== \"merge\") {\n return 'manageCollection: `mode` must be \"upsert\" (default), \"create\", or \"merge\".';\n }\n return { slug, items: items as CollectionItem[], mode: (mode as PutItemsArgs[\"mode\"] | undefined) ?? \"upsert\" };\n}\n\n/** The machine-readable workspace ontology: every collection with its\n * identity, record count, and outbound `ref`/`embed` relations. Slugs\n * are discovery-sanitized; titles/labels are workspace-authored schema\n * text and ride verbatim — the same trust class as the record values\n * getItems returns. */\nasync function handleGetOntology(deps: ManageCollectionDeps): Promise<string> {\n const collections = await buildWorkspaceOntology(deps);\n return JSON.stringify({ count: collections.length, collections });\n}\n\n/** Return the collection-authoring reference (`collection-skills.md`).\n * Workspace copy first (reflects user edits), bundled asset as the\n * always-present fallback. Both reads guarded; if neither resolves the\n * agent still gets an actionable message instead of a thrown call. */\nasync function handleSchemaDocs(deps: ManageCollectionDeps): Promise<string> {\n const candidates = [\n path.join(resolveBase(deps), HELPS_DIR, SCHEMA_DOCS_FILE),\n ...(deps.bundledHelpsDir ? [path.join(deps.bundledHelpsDir(), SCHEMA_DOCS_FILE)] : []),\n ];\n for (const candidate of candidates) {\n try {\n return await readFile(candidate, \"utf-8\");\n } catch {\n // try the next source\n }\n }\n return `manageCollection: could not read the collection-authoring reference (${SCHEMA_DOCS_FILE}).`;\n}\n\n/** Return the raw schema.json of an existing collection, for editing.\n * Staging (the canonical writable copy) first, the active mirror as a\n * fallback for user-scope skills that have no staging copy. Raw text —\n * not the parsed schema — so the agent edits the true on-disk source. */\nasync function handleGetSchema(slug: string, deps: ManageCollectionDeps): Promise<string> {\n const collection = await loadCollection(slug, deps);\n if (!collection) return unknownCollection(slug);\n // Path from the discovered (sanitized) slug, never the raw arg.\n const candidates = [path.join(dataSkillDir(resolveBase(deps), collection.slug), SCHEMA_FILE), path.join(collection.skillDir, SCHEMA_FILE)];\n for (const candidate of candidates) {\n try {\n return await readFile(candidate, \"utf-8\");\n } catch {\n // fall through to the next location\n }\n }\n return `manageCollection: '${defangForPrompt(slug)}' has no readable ${SCHEMA_FILE}.`;\n}\n\n/** Refuse a schema edit the host can't honour: user-scope/feed collections\n * are read-only, and presets (mc-*) re-seed on restart so an edit is lost. */\nfunction schemaEditRefusal(collection: LoadedCollection, slug: string): string | null {\n if (collection.source !== \"project\") {\n return `manageCollection: '${defangForPrompt(slug)}' is ${collection.source}-scope and read-only here — only project collections (data/skills/) can be edited.`;\n }\n if (isPresetSlug(slug)) {\n return `manageCollection: '${defangForPrompt(slug)}' is a preset (mc-*) and re-seeds on restart — copy it under a different slug to customise.`;\n }\n return null;\n}\n\n/** Turn a CollectionSchemaZ failure into a short, actionable list the\n * agent can fix, pointing back at the field reference. */\nfunction formatSchemaIssues(issues: readonly { path: PropertyKey[]; message: string }[]): string {\n const shown = issues.slice(0, MAX_SCHEMA_ISSUES);\n const lines = shown.map((issue) => `- ${issue.path.map(String).join(\".\") || \"(root)\"}: ${defangForPrompt(issue.message)}`).join(\"\\n\");\n const omitted = issues.length - shown.length;\n const more = omitted > 0 ? `\\n- …and ${omitted} more issue(s); fix these first and retry.` : \"\";\n return `manageCollection: schema rejected — fix and retry (call schemaDocs for the field reference):\\n${lines}${more}`;\n}\n\n/** Write the validated schema to the canonical staging copy, then mirror\n * it into the active `.claude/skills/` tree discovery reads — an internal\n * write doesn't fire the skill-bridge hook, so we mirror explicitly. */\nasync function writeAndMirrorSchema(slug: string, schema: unknown, deps: ManageCollectionDeps): Promise<void> {\n const base = resolveBase(deps);\n await writeFileAtomic(path.join(dataSkillDir(base, slug), SCHEMA_FILE), `${JSON.stringify(schema, null, 2)}\\n`);\n mirrorSkillWrite(base, { slug, relSegments: [SCHEMA_FILE] });\n try {\n await deps.refreshAfterWrite?.();\n } catch {\n // best-effort — see ManageCollectionDeps.refreshAfterWrite\n }\n}\n\n/** The post-Zod acceptance gates discovery applies before a parsed schema\n * becomes a live collection. Mirrors discovery's checks (`loadOneCollection`)\n * so a write can't pass here yet be silently skipped on the next load.\n * putSchema only runs for project-scope collections, so the feed-`ingest`\n * gate doesn't apply. Returns a one-line reason, or null when the schema\n * would be accepted. */\nfunction schemaDiscoveryGate(schema: CollectionSchema, base: string): string | null {\n const primaryField = schema.fields[schema.primaryKey];\n if (!primaryField) return `primaryKey '${schema.primaryKey}' is not one of the declared fields`;\n if (primaryField.primary !== true) return `the primaryKey field '${schema.primaryKey}' must be flagged \\`primary: true\\``;\n if (schema.dataPath !== undefined && resolveDataDir(schema.dataPath, base) === null) return `dataPath '${schema.dataPath}' escapes the workspace`;\n if (schema.dataSource !== undefined && resolveDataDir(schema.dataSource.path, base) === null) {\n return `dataSource.path '${schema.dataSource.path}' escapes the workspace`;\n }\n return null;\n}\n\n/** Validate a schema against CollectionSchemaZ and, on success, persist it.\n * Edit-only: a new collection is created by writing SKILL.md + schema.json\n * under data/skills/<slug>/ (the normal create flow), not through here. */\nasync function handlePutSchema(slug: string, schemaArg: unknown, deps: ManageCollectionDeps): Promise<string> {\n if (!schemaArg || typeof schemaArg !== \"object\" || Array.isArray(schemaArg)) {\n return \"manageCollection: `schema` is required for putSchema — the full collection schema object.\";\n }\n const collection = await loadCollection(slug, deps);\n if (!collection) {\n return `manageCollection: unknown collection '${defangForPrompt(slug)}' — create it by writing SKILL.md + ${SCHEMA_FILE} under data/skills/${defangForPrompt(slug)}/, then edit it here.`;\n }\n const refusal = schemaEditRefusal(collection, slug);\n if (refusal) return refusal;\n const parsed = CollectionSchemaZ.safeParse(schemaArg);\n if (!parsed.success) return formatSchemaIssues(parsed.error.issues);\n // Run the SAME post-Zod gates discovery applies, so a write can't pass\n // here yet be silently skipped on the next load (hiding the collection).\n const gate = schemaDiscoveryGate(parsed.data, resolveBase(deps));\n if (gate) {\n return `manageCollection: schema rejected — ${gate} (call schemaDocs for the field reference). It passes basic validation but discovery would skip it, hiding the collection.`;\n }\n // Path from the discovered (sanitized) slug, never the raw arg.\n await writeAndMirrorSchema(collection.slug, parsed.data, deps);\n return JSON.stringify({ collection: collection.slug, written: true });\n}\n\nconst MANAGE_COLLECTION_PROMPT =\n \"Use `manageCollection` instead of raw Read/Write/Edit when working with a collection's records OR its schema (raw file I/O stays available as the escape hatch). \" +\n \"Before authoring or changing a collection's `schema.json`, call `schemaDocs` to load the field/DSL reference, then read with `getSchema` and write with `putSchema` — `putSchema` validates the whole schema before writing and returns actionable errors instead of silently failing discovery's validation. \" +\n \"`getItems` is the only way to see computed values — `derived` fields (e.g. a portfolio's value), `toggle` projections, and `embed` records are host-computed and never present in the stored JSON files. On large collections pass `ids` and/or `fields` to keep the result small. \" +\n 'For a question that spans collections (\"which clients have unpaid invoices?\"), start with `getOntology`: it lists every collection with its primaryKey, record count, and outbound `ref`/`embed` relations, so you know which collections to join before reading any records. ' +\n \"`putItems` validates every row against the schema before writing (required fields, enum values, primaryKey = record id) and returns `{ written, rejected }`; fix each rejected row using its `problem` text and retry just those rows. Never include computed fields in a row you write. \" +\n 'To update a few fields of an existing record, use `mode: \"merge\"` with a partial row ({ id, <changed fields> }) — the default upsert replaces the WHOLE record, so a partial upsert would silently erase every optional field it omits. ' +\n \"`deleteItems` removes records by id and returns `{ deleted, rejected }`; an id that doesn't exist comes back rejected rather than counted as deleted, so check `rejected` before reporting a deletion as done. \" +\n \"Answer aggregation questions (counts, sums, averages, group-bys) with `queryItems` on ANY collection — on a dataSource (CSV) collection it scans the whole file (getItems is row-capped, so aggregates computed from its output can be silently wrong on large files); on a file-backed collection it aggregates the enriched records, so computed fields (derived/rollup/toggle) are queryable columns.\";\n\n/** Validate getItems' optional `ids`/`fields` args, then delegate. */\nasync function dispatchGetItems(collection: LoadedCollection, args: Record<string, unknown>, deps: ManageCollectionDeps): Promise<string> {\n const ids = optionalStringArray(args.ids, \"ids\");\n if (!ids.ok) return ids.error;\n const fields = optionalStringArray(args.fields, \"fields\");\n if (!fields.ok) return fields.error;\n return handleGetItems(collection, { slug: collection.slug, ids: ids.value, fields: fields.value }, deps);\n}\n\n/** Actions that operate on a collection's RECORDS — i.e. the ones that\n * need the collection loaded first. Schema/workspace actions don't. */\nconst RECORD_ACTIONS = new Set([\"getItems\", \"putItems\", \"deleteItems\", \"queryItems\"]);\n\n/** Record-action dispatch, split from `manageCollectionHandler` to keep\n * both within the cognitive-complexity budget. */\nasync function dispatchRecordAction(action: string, collection: LoadedCollection, args: Record<string, unknown>, deps: ManageCollectionDeps): Promise<string> {\n if (action === \"getItems\") return dispatchGetItems(collection, args, deps);\n if (action === \"queryItems\") return handleQueryItems(collection, args.query, deps);\n if (action === \"deleteItems\") {\n const ids = parseDeleteIds(args);\n return typeof ids === \"string\" ? ids : handleDeleteItems(collection, ids, deps);\n }\n const parsed = parsePutItems(args, collection.slug);\n return typeof parsed === \"string\" ? parsed : handlePutItems(collection, parsed, deps);\n}\n\n// The tool's action dispatch. Extracted from the factory's returned object so\n// `makeManageCollectionTool` stays under the max-lines threshold; each branch\n// already delegates to a handler.\nasync function manageCollectionHandler(deps: ManageCollectionDeps, args: Record<string, unknown>): Promise<string> {\n const action = typeof args.action === \"string\" ? args.action : \"\";\n if (action === \"schemaDocs\") return handleSchemaDocs(deps);\n if (action === \"getOntology\") return handleGetOntology(deps);\n const slug = typeof args.slug === \"string\" ? args.slug.trim() : \"\";\n if (!slug) return \"manageCollection: `slug` is required (the collection's slug).\";\n if (action === \"getSchema\") return handleGetSchema(slug, deps);\n if (action === \"putSchema\") return handlePutSchema(slug, args.schema, deps);\n if (!RECORD_ACTIONS.has(action)) {\n return 'manageCollection: `action` must be \"getItems\", \"putItems\", \"deleteItems\", \"queryItems\", \"getOntology\", \"schemaDocs\", \"getSchema\", or \"putSchema\".';\n }\n const collection = await loadCollection(slug, deps);\n if (!collection) return unknownCollection(slug);\n return dispatchRecordAction(action, collection, args, deps);\n}\n\n// Static tool definition, hoisted out of the factory so the function body\n// stays within the line budget (the schema only ever grows).\nconst MANAGE_COLLECTION_DEFINITION = {\n name: \"manageCollection\",\n description:\n \"Read and write a schema-driven collection through the host — both its records and its structure. getItems returns records WITH computed values (derived formulas, toggles, embeds) the stored JSON files don't contain; putItems validates each row against the schema before writing; deleteItems removes records by id. getOntology maps the whole workspace: every collection with its record count and outbound ref/embed relations — call it first for cross-collection questions. schemaDocs returns the collection-authoring reference; getSchema/putSchema read and validate-then-write the collection's schema.json. Prefer it over raw file I/O on collections.\",\n inputSchema: {\n type: \"object\",\n properties: {\n action: {\n type: \"string\",\n enum: [\"getItems\", \"putItems\", \"deleteItems\", \"queryItems\", \"getOntology\", \"schemaDocs\", \"getSchema\", \"putSchema\"],\n description: \"What to do.\",\n },\n slug: {\n type: \"string\",\n description: \"The collection's slug (its directory name, e.g. `stock-quotes`). Required for everything except schemaDocs and getOntology.\",\n },\n ids: {\n type: \"array\",\n items: { type: \"string\" },\n description:\n \"getItems: only these record ids (primary-key values); omit for all records. deleteItems: the record ids to delete — required, and never optional there.\",\n },\n fields: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"getItems: only these fields per record (the primary key is always included). Omit for all fields. Use on large collections.\",\n },\n items: {\n type: \"array\",\n items: { type: \"object\" },\n description: \"putItems: the record objects to store. Each must carry the schema's primaryKey value (it doubles as the filename).\",\n },\n mode: {\n type: \"string\",\n enum: [\"upsert\", \"create\", \"merge\"],\n description:\n 'putItems: \"upsert\" (default) replaces existing records WHOLE; \"create\" rejects rows whose id already exists; \"merge\" updates only the fields a row carries, keeping the rest of the existing record (rejects unknown ids). Use \"merge\" when changing a few fields.',\n },\n schema: {\n type: \"object\",\n description:\n \"putSchema: the full collection schema object (same shape as schema.json — title, icon, dataPath, primaryKey, fields, …). Call getSchema first for the current one, and schemaDocs for the field DSL.\",\n },\n query: {\n type: \"object\",\n description:\n 'queryItems: a structured aggregation query — `{ groupBy?: [\"col\"], aggregates?: { alias: { op: \"count\"|\"sum\"|\"avg\"|\"min\"|\"max\", column? } }, where?: [{ field, op, value }], orderBy?: [{ field, dir? }], limit? }`. At least one of groupBy/aggregates. On a dataSource (CSV) collection it scans the WHOLE file uncapped (unlike getItems); on a file-backed collection it aggregates the ENRICHED records, so computed fields (derived/rollup/toggle) are queryable columns. Use it for counts / sums / averages / group-bys instead of arithmetic over getItems output.',\n },\n },\n required: [\"action\"],\n },\n};\n\nexport function makeManageCollectionTool(deps: ManageCollectionDeps = {}) {\n return {\n definition: MANAGE_COLLECTION_DEFINITION,\n\n // Collections are workspace data every role can already reach via\n // raw Read/Write/Edit — gating the SAFER path per-role would only\n // push unlisted roles back onto unvalidated file I/O.\n alwaysActive: true,\n\n prompt: MANAGE_COLLECTION_PROMPT,\n\n handler: (args: Record<string, unknown>): Promise<string> => manageCollectionHandler(deps, args),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoCA,SAAS,sBAAsB,OAAmD;CAChF,KAAK,MAAM,SAAS,UAAU,KAAK,GAAG,OAAO,CAAC;CAC9C,MAAM,aAAa,OAAO,QAAQ,MAAM,cAAc,CAAC,CAAC;CACxD,OAAO,CAAC,OAAO,YAAY,WAAW,KAAK,CAAC,OAAO,eAAmD,CAAC,OAAO,UAAU,OAAO,UAAU,IAAI,IAAI,CAAC,CAAC,CAAC;AACtJ;;;;AAKA,SAAS,wBAAwB,OAAkC;CACjE,MAAM,UAAU,IAAI,IAAY,MAAM,WAAW,CAAC,CAAC;CACnD,KAAK,MAAM,aAAa,OAAO,OAAO,MAAM,cAAc,CAAC,CAAC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