Broken after some lua

I modified a lua script located in CONFIG.md to aid with task management and after a reload got this issue.

lua code:

-- Task priority scoring
index.defineTag {
  name = "task",
  metatable = {
    __index = function(self, attr)
      if attr == "priorityScore" then
        local t = self.tags
        if table.includes(t, "P-CRITICAL") then return 0
        elseif table.includes(t, "P-HIGH") then return 1
        elseif table.includes(t, "P-MEDIUM") then return 2
        elseif table.includes(t, "P-LOW") then return 3
        else return 999 end

      elseif attr == "effectiveOrder" then
        -- We define a computed attribute 'effectiveOrder'
        -- This avoids infinite loops if you were to override 'order' directly
        return getInheritedOrder(self)
      end
    end
  }
}

-- Helper function to find a task by its reference string
function getTaskByRef(ref)
  -- We query the 'task' tag where the 'ref' attribute matches our string
  -- Space Lua allows direct variable usage (ref) inside the query
  local results = query[[
    from index.tag "task"
    where ref = ref
    limit 1
  ]]
  
  if #results > 0 then
    return results[1]
  else
    return nil
  end
end

-- The recursive function you asked for
function getInheritedOrder(task)
  -- 1. If the task has an explicit order, return it immediately
  if task.order then
    return task.order
  end

  -- 2. If no explicit order, check if we have a parent
  if task.parent then
    -- 3. Fetch the parent object using its reference string
    local parentTask = getTaskByRef(task.parent)
    
    -- 4. If parent exists, recurse upwards
    if parentTask then
      return getInheritedOrder(parentTask)
    end
  end

  -- 5. Default fallback if no order is found in the chain
  -- (Return nil or a default number like 9999 depending on your sorting needs)
  return 9999 
end

prioritySystemLoaded = "âś“ Working inheritance v2"

UPDATE:
the issue was in getTaskByRef, here is the fixed version:

...
function getInheritedOrder(task)
  if not task or type(task) ~= "table" then return 999 end
  if task.order then return task.order end
  
  if task.parent then
    -- Extract number from ref like "CONFIG@3233"
    local parentNum = tonumber(task.parent:match("@(%d+)"))
    if parentNum then
      -- Apply +2 offset
      local adjustedRef = task.parent:gsub("@%d+", "@" .. (parentNum + 2))
      local parentTask = index.getObjectByRef(task.page, "task", adjustedRef)
      if parentTask then
        return getInheritedOrder(parentTask)
      end
    end
  end
  
  return 999
end
...

if the parsing error could be more explicit it would be great.

I ran into a similar situation.

One other suggestion for the long term; it’d be great if SB loaded in a “safe mode” (completely ignoring CONFIG.md and perhaps all scripts?) if CONFIG.md had errors.

This way one does not have to go and change the file in the back end, which is especially a pain on mobile devices.

1 Like

A fall back safe mode would be a good feature.

I have similar issues where I had to go into the back end and fix some space-lua that I just added and broke SB.

1 Like

This was also reported here: Hard failure on config parsing errors after refresh · Issue #1679 · silverbulletmd/silverbullet · GitHub

Just fixed it on main

2 Likes