@aiwayds/dsh-opentui 0.27.2 → 0.28.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.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The patch-ownership sidecar: `profiles/<p>/.patch-ownership.json`.
3
+ *
4
+ * `ancestor` is the TEMPLATE projection adopted by the previous converge pass
5
+ * (never the bytes we wrote) — see design 06 R1/§4.3.1. A missing or corrupt
6
+ * sidecar degrades to base-unknown (carry current), so the file only affects
7
+ * merge direction, never the written values. Written inside the same
8
+ * `package.json.lock` critical section as the patch, tmp+rename, mode 0600.
9
+ */
10
+
11
+ import { readFileSync, renameSync, writeFileSync } from 'node:fs'
12
+ import { join } from 'node:path'
13
+
14
+ export const STATE_NAME = '.patch-ownership.json'
15
+
16
+ export function statePath(profileDir) {
17
+ return join(profileDir, STATE_NAME)
18
+ }
19
+
20
+ /** undefined for ENOENT / corrupt / unknown version (deliberately non-throwing). */
21
+ export function readState(profileDir) {
22
+ try {
23
+ const parsed = JSON.parse(readFileSync(statePath(profileDir), 'utf8'))
24
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return undefined
25
+ if (parsed.version !== 1 || parsed.rows === null || typeof parsed.rows !== 'object' || Array.isArray(parsed.rows)) return undefined
26
+ return parsed
27
+ } catch {
28
+ return undefined
29
+ }
30
+ }
31
+
32
+ export function writeState(profileDir, state, { now = () => new Date().toISOString(), pid = process.pid, stamp = Date.now() } = {}) {
33
+ if (!state || state.version !== 1 || !state.rows) return false
34
+ const path = statePath(profileDir)
35
+ const outgoing = { ...state, writtenAt: now() }
36
+ const tmp = `${path}.tmp-${pid}-${stamp}`
37
+ writeFileSync(tmp, `${JSON.stringify(outgoing, null, 2)}\n`, { mode: 0o600 })
38
+ renameSync(tmp, path)
39
+ return true
40
+ }
41
+
42
+ export default { STATE_NAME, statePath, readState, writeState }