DreamLake

Nodes moved across episode boundaries

Before issue #1352, PATCH /nodes/:id could move a node across an episode boundary inside one project. The move rewrote path for the node and its descendants but never episodeNodeId, which every writer and the episode file list rely on. Three shapes follow:

  • Moved out of an episode. The rows still name the old episode. Its file list drops them, the metadata route reports a wrong prefix, and writes to the moved folder answer 409 node already exists at this path.
  • Moved into another episode. The same rows, now under a second episode's path. Neither episode lists them, and new files there answer 409.
  • Episode moved off its project root, into a folder or another episode. Uploads to it answer 403 LOST_AUTHORITY, and creating an episode with the same name makes a second one at the project root, which then receives the data.

No rows are lost. Servers with the fix (not yet released) refuse these moves before writing anything. Existing rows stay as they are until someone repairs them; there is no migration.

Find them

Read-only: aggregate and print only. Run it in mongosh against a database you are authorized to inspect, preferably a secondary, and scope it to one namespace on a large database. Running it, and any repair, against production needs explicit approval.

js
const scope = {}; // e.g. { namespaceId: ObjectId('<namespace id>') }
const live = { $or: [{ deletedAt: null }, { deletedAt: { $exists: false } }] };
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const report = (issue, n, extra) => print(JSON.stringify({ issue, id: String(n._id),
  namespaceId: String(n.namespaceId), kind: n.kind, path: n.path, name: n.name, ...extra }));

const episodes = await db.Node.aggregate([
  { $match: { ...scope, kind: 'episode', ...live } },
  { $lookup: { from: 'Node', localField: 'projectNodeId', foreignField: '_id', as: 'project' } },
  { $set: { project: { $first: '$project' } } },
]).toArray();

for (const ep of episodes) {
  const episode = String(ep._id);
  if (!ep.project || ep.project.path !== ',' || ep.path !== `,${ep.project.name},`)
    report('episode-off-project-root', ep, { projectNodeId: String(ep.projectNodeId) });
  const prefix = `${ep.path}${ep.name},`;
  // Under this episode's path, naming another episode or none.
  for (const n of await db.Node.aggregate([{ $match: { namespaceId: ep.namespaceId,
    path: { $regex: '^' + esc(prefix) }, episodeNodeId: { $ne: ep._id }, ...live } }]).toArray())
    report('inside-episode-names-other', n, { pathEpisode: episode, episodeNodeId: String(n.episodeNodeId ?? null) });
  // Naming this episode, outside its path.
  for (const n of await db.Node.aggregate([{ $match: { episodeNodeId: ep._id, ...live,
    $expr: { $ne: [{ $indexOfCP: ['$path', prefix] }, 0] } } }]).toArray())
    report('names-episode-outside-it', n, { episodeNodeId: episode });
}

// Live rows naming an episode that is soft-deleted, gone or not an episode.
for (const g of await db.Node.aggregate([
  { $match: { ...scope, episodeNodeId: { $type: 'objectId' }, ...live } },
  { $group: { _id: '$episodeNodeId', namespaceId: { $first: '$namespaceId' }, rows: { $sum: 1 } } },
  { $lookup: { from: 'Node', localField: '_id', foreignField: '_id', as: 'episode' } },
  { $set: { episode: { $first: '$episode' } } },
  { $match: { $or: [{ episode: null }, { 'episode.kind': { $ne: 'episode' } }, { 'episode.deletedAt': { $type: 'date' } }] } },
]).toArray())
  print(JSON.stringify({ issue: 'names-unavailable-episode', episodeNodeId: String(g._id),
    namespaceId: String(g.namespaceId), rows: g.rows,
    state: !g.episode ? 'missing' : g.episode.kind !== 'episode' ? 'not-an-episode' : 'soft-deleted' }));

A subtree reports every row, so a moved folder is listed with each of its descendants. A folder moved from one episode into another appears twice, once for each episode. An episode nested in another episode is reported as inside-episode-names-other, together with its own subtree.

names-unavailable-episode is one line per referenced episode, with a row count. Moves do not cause it: a move never deletes or retypes an episode. It comes from deleting an episode while rows under it stay live, so it is a separate finding with no repair here. A soft-deleted state may be expected where that deletion keeps the subtree on purpose. Raise anything else as its own issue rather than moving rows to hide it.

Repair

Repair through the normal route, as the project's owner or a WRITE user. Every move below is one the fixed server still allows, because it brings a node back to where its episodeNodeId says it belongs. Move only the topmost reported node of each subtree; its descendants follow.

  1. Episodes first. For each episode-off-project-root row, move it back: PATCH /nodes/<episode id> with { "parentId": "<projectNodeId>" }. A 409 means the by-name upsert already made a second episode with that name. Merging the two is a product decision, not a repair. Rename one with { "name": "..." } first, then move it.
  2. Rows naming an episode. For a names-episode-outside-it or inside-episode-names-other subtree whose episodeNodeId is an episode, move its topmost node into that episode: { "parentId": "<episodeNodeId>" }, or a folder inside it.
  3. Project-level rows. For an inside-episode-names-other subtree whose episodeNodeId is null, move its topmost node to the project root or a project-level folder.

A 409 on a move means a same-named node already exists there; rename one first. Run the query again afterwards; it should return nothing for the rows you repaired.

Do not rewrite path or episodeNodeId directly. If moving back is unacceptable for a project, that needs a reviewed migration that updates a node and its whole subtree in one transaction, and a product decision on nested episodes. Neither exists.