siyuan-note/siyuan · error

history path is missing notebook context

Error message

history path is missing notebook context [%s]

What it means

RollbackDocHistory parses the history path as <historyDir>/<datePrefix>/<boxID>/<relativePath> and needs at least three slash-separated segments to identify the owning notebook. The supplied path has fewer segments, so no notebook context can be derived. This guards against rolling back arbitrary or malformed paths that were not produced by the history subsystem.

Solutions

  1. Pass the full history path as returned by the history index (containing date prefix, notebook ID, and relative doc path)
  2. Obtain the path from the history query API rather than constructing it manually
  3. Ensure util.HistoryDir prefix and OS path separators are intact (no manual rewriting to a different base)
  4. If migrating data, rebuild the directory as <historyDir>/<date>/<boxID>/<doc>.sy before rolling back

Example fix

// before
RollbackDocHistory(filepath.Join(util.HistoryDir, "20260918091234-abc123.sy"))
// after
RollbackDocHistory(filepath.Join(util.HistoryDir, "20260918", boxID, "20260918091234-abc123.sy"))
Defensive patterns

Strategy: validation

Validate before calling

const rel = historyPath.replace(/^.*data\/history\//, '').split(/[\\/]/);
if (rel.length < 3) throw new Error('history path must be <date>/<boxID>/<doc>.sy');

Type guard

function isValidHistoryPath(p) { const rel = p.split(/[\\/]/); const i = rel.indexOf('history'); return i >= 0 && rel.length - i - 1 >= 3; }

Try / catch

try { await historyRollback({ historyPath }) } catch (e) { if (String(e.msg).includes('missing notebook context')) { /* fix path construction */ } else { throw e } }

Prevention

When it happens

Trigger: Calling model.RollbackDocHistory (via historyRollback API or tests) with a path directly under data/history/ (e.g. data/history/foo.sy) or otherwise lacking the <datePrefix>/<boxID>/... structure.

Common situations: Client code concatenates the wrong base directory; passing an asset or doc path instead of a history path; a truncated or hand-constructed path after a failed history listing.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/8ce4544c17263c50. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/history.go:291

	}
	return
}

func RollbackDocHistory(historyPath string) (err error) {
	historyPath, err = validateHistoryPath(historyPath)
	if err != nil {
		return
	}

	FlushTxQueue()

	relPath := strings.TrimPrefix(historyPath, util.HistoryDir)
	relPath = strings.TrimPrefix(relPath, string(os.PathSeparator))
	relPath = filepath.ToSlash(relPath)
	parts := strings.SplitN(relPath, "/", 3)
	if len(parts) < 3 {
		logging.LogWarnf("invalid history path [%s]", historyPath)
		return fmt.Errorf("history path is missing notebook context [%s]", historyPath)
	}
	boxID := parts[1]
	origBoxID := boxID // 保留原始 boxID 用于解密(getRollbackBox 可能返回不同的 box)
	encrypted := IsEncryptedBox(origBoxID)
	if encrypted {
		// 整个回滚持有操作租约,内部文件读写可独立获取读锁,锁定等待租约结束后再申请写锁。
		if err = AcquireEncryptedBoxOperation(origBoxID); err != nil {
			return
		}
		defer ReleaseEncryptedBoxOperation(origBoxID)
	}

	// 加密笔记本的历史回滚要求原笔记本已挂载:
	// WriteTree 根据 tree.Box 判断是否加密落盘。若原笔记本未挂载导致
	// getRollbackBox fallback 到普通 Rollback 笔记本,解密后的 .sy 将被 WriteTree
	// 以明文落盘,违反"WriteTree auto-encrypts on write-back"的设计承诺。
	if IsEncryptedBox(origBoxID) && nil == Conf.Box(origBoxID) {
		logging.LogErrorf("rollback encrypted doc history requires notebook [%s] to be mounted", origBoxID)

View on GitHub (pinned to 9f775e8a12)