dsh-plain-memory 1.0.1 → 1.1.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.
package/cards.js CHANGED
@@ -1,69 +1,69 @@
1
- /**
2
- * 窗口状态卡:每个活跃 DSH 会话一张,落在记忆仓的 .state/cards/ 下。
3
- * 卡片是运行时协调数据,不是记忆资产,所以被 .state/.gitignore 排除,不进 git。
4
- */
5
-
6
- import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
7
- import { join } from 'node:path'
8
-
9
- const CARD_VERSION = 1
10
-
11
- function stateDir(repo) {
12
- return join(repo, '.state')
13
- }
14
-
15
- /** 确保 .state 存在且被 git 忽略。 */
16
- export function ensureState(repo) {
17
- const dir = stateDir(repo)
18
- mkdirSync(join(dir, 'cards'), { recursive: true })
19
- mkdirSync(join(dir, 'pending'), { recursive: true })
20
- const ignore = join(dir, '.gitignore')
21
- if (!existsSync(ignore)) writeFileSync(ignore, '*\n', 'utf8')
22
- return dir
23
- }
24
-
25
- function cardFile(repo, sessionId) {
26
- return join(stateDir(repo), 'cards', `${String(sessionId).replace(/[^\w.-]/g, '_')}.json`)
27
- }
28
-
29
- function clip(text, max) {
30
- const s = String(text || '').replace(/\s+/g, ' ').trim()
31
- return s.length > max ? s.slice(0, max - 1) : s
32
- }
33
-
34
- /** 写一张状态卡(原子替换)。 */
35
- export function writeCard(repo, card) {
36
- const dir = ensureState(repo)
37
- const file = cardFile(repo, card.session)
38
- const value = {
39
- v: CARD_VERSION,
40
- session: String(card.session),
41
- cwd: clip(card.cwd, 200),
42
- line: clip(card.line, 80),
43
- lineExplicit: card.lineExplicit === true,
44
- files: (card.files || []).slice(0, 12).map((f) => clip(f, 160)),
45
- objective: clip(card.objective, 300),
46
- conclusion: clip(card.conclusion, 300),
47
- updatedAt: Number(card.updatedAt) || Date.now(),
48
- }
49
- const tmp = `${file}.${process.pid}.tmp`
50
- writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
51
- try { renameSync(tmp, file) } catch { writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`, 'utf8') }
52
- return dir
53
- }
54
-
55
- /** 读全部状态卡;坏文件直接跳过。 */
56
- export function readCards(repo) {
57
- const dir = join(stateDir(repo), 'cards')
58
- let names = []
59
- try { names = readdirSync(dir) } catch { return [] }
60
- const out = []
61
- for (const name of names) {
62
- if (!name.endsWith('.json')) continue
63
- try {
64
- const card = JSON.parse(readFileSync(join(dir, name), 'utf8'))
65
- if (card && typeof card.session === 'string') out.push(card)
66
- } catch { /* 半写入的卡片下一轮会被覆盖 */ }
67
- }
68
- return out
69
- }
1
+ /**
2
+ * 窗口状态卡:每个活跃 DSH 会话一张,落在记忆仓的 .state/cards/ 下。
3
+ * 卡片是运行时协调数据,不是记忆资产,所以被 .state/.gitignore 排除,不进 git。
4
+ */
5
+
6
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
7
+ import { join } from 'node:path'
8
+
9
+ const CARD_VERSION = 1
10
+
11
+ function stateDir(repo) {
12
+ return join(repo, '.state')
13
+ }
14
+
15
+ /** 确保 .state 存在且被 git 忽略。 */
16
+ export function ensureState(repo) {
17
+ const dir = stateDir(repo)
18
+ mkdirSync(join(dir, 'cards'), { recursive: true })
19
+ mkdirSync(join(dir, 'pending'), { recursive: true })
20
+ const ignore = join(dir, '.gitignore')
21
+ if (!existsSync(ignore)) writeFileSync(ignore, '*\n', 'utf8')
22
+ return dir
23
+ }
24
+
25
+ function cardFile(repo, sessionId) {
26
+ return join(stateDir(repo), 'cards', `${String(sessionId).replace(/[^\w.-]/g, '_')}.json`)
27
+ }
28
+
29
+ function clip(text, max) {
30
+ const s = String(text || '').replace(/\s+/g, ' ').trim()
31
+ return s.length > max ? s.slice(0, max - 1) : s
32
+ }
33
+
34
+ /** 写一张状态卡(原子替换)。 */
35
+ export function writeCard(repo, card) {
36
+ const dir = ensureState(repo)
37
+ const file = cardFile(repo, card.session)
38
+ const value = {
39
+ v: CARD_VERSION,
40
+ session: String(card.session),
41
+ cwd: clip(card.cwd, 200),
42
+ line: clip(card.line, 80),
43
+ lineExplicit: card.lineExplicit === true,
44
+ files: (card.files || []).slice(0, 12).map((f) => clip(f, 160)),
45
+ objective: clip(card.objective, 300),
46
+ conclusion: clip(card.conclusion, 300),
47
+ updatedAt: Number(card.updatedAt) || Date.now(),
48
+ }
49
+ const tmp = `${file}.${process.pid}.tmp`
50
+ writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
51
+ try { renameSync(tmp, file) } catch { writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`, 'utf8') }
52
+ return dir
53
+ }
54
+
55
+ /** 读全部状态卡;坏文件直接跳过。 */
56
+ export function readCards(repo) {
57
+ const dir = join(stateDir(repo), 'cards')
58
+ let names = []
59
+ try { names = readdirSync(dir) } catch { return [] }
60
+ const out = []
61
+ for (const name of names) {
62
+ if (!name.endsWith('.json')) continue
63
+ try {
64
+ const card = JSON.parse(readFileSync(join(dir, name), 'utf8'))
65
+ if (card && typeof card.session === 'string') out.push(card)
66
+ } catch { /* 半写入的卡片下一轮会被覆盖 */ }
67
+ }
68
+ return out
69
+ }
package/cordis.patch.yml CHANGED
@@ -1,5 +1,5 @@
1
- # dsh-plain-memory bundle layer: inserts the Host plugin into the profile.
2
- # Install with `dsh plugin --profile <name> add <path-or-package>`.
3
- - insert:
4
- - id: plain-memory
5
- name: 'dsh-plain-memory'
1
+ # dsh-plain-memory bundle layer: inserts the Host plugin into the profile.
2
+ # Install with `dsh plugin --profile <name> add <path-or-package>`.
3
+ - insert:
4
+ - id: plain-memory
5
+ name: 'dsh-plain-memory'
package/git.js CHANGED
@@ -1,119 +1,119 @@
1
- /**
2
- * 最小 git 封装:只做记忆仓需要的 pull / commit / push。
3
- *
4
- * 三条纪律,都是为了「不挡回合、不打断对话」:
5
- * 1. 所有调用都有超时,且禁止交互(GIT_TERMINAL_PROMPT=0、GCM_INTERACTIVE=never)。
6
- * 2. 提交遇到 index.lock 时退避重试;仍失败就把改动留在工作区,下次提交带上。
7
- * 3. 推送在后台跑,回合不等它;失败只记日志,下次再补推。
8
- */
9
-
10
- import { execFile, spawn } from 'node:child_process'
11
-
12
- const PULL_MS = 5_000
13
- const COMMIT_MS = 5_000
14
- const PUSH_MS = 15_000
15
-
16
- function gitEnv() {
17
- return {
18
- ...process.env,
19
- GIT_TERMINAL_PROMPT: '0',
20
- GCM_INTERACTIVE: 'never',
21
- GIT_OPTIONAL_LOCKS: '0',
22
- }
23
- }
24
-
25
- /** 跑一条 git 命令;永远 resolve,不抛。 */
26
- export function runGit(repo, args, timeoutMs = COMMIT_MS) {
27
- return new Promise((resolve) => {
28
- execFile(
29
- 'git',
30
- ['-C', repo, ...args],
31
- { timeout: timeoutMs, windowsHide: true, maxBuffer: 4 * 1024 * 1024, env: gitEnv() },
32
- (error, stdout, stderr) => {
33
- resolve({
34
- ok: error === null,
35
- code: typeof error?.code === 'number' ? error.code : error === null ? 0 : 1,
36
- out: String(stdout || '').trim(),
37
- err: String(stderr || '').trim(),
38
- timedOut: error?.killed === true,
39
- })
40
- },
41
- )
42
- })
43
- }
44
-
45
- const sleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms) })
46
-
47
- /** 是否是一个 git 工作树。 */
48
- export async function isRepo(repo) {
49
- const r = await runGit(repo, ['rev-parse', '--is-inside-work-tree'], 4_000)
50
- return r.ok && r.out === 'true'
51
- }
52
-
53
- /** 工作树根目录。 */
54
- export async function topLevel(repo) {
55
- const r = await runGit(repo, ['rev-parse', '--show-toplevel'], 4_000)
56
- return r.ok ? r.out : ''
57
- }
58
-
59
- /** 目录所在的 git 分支;不是仓库(或 detached)就返回空串。用它当「工作线」。 */
60
- export async function branchOf(dir) {
61
- const r = await runGit(dir, ['symbolic-ref', '--short', 'HEAD'], 4_000)
62
- return r.ok ? r.out : ''
63
- }
64
-
65
- /** 是否有未提交改动。 */
66
- export async function isDirty(repo) {
67
- const r = await runGit(repo, ['status', '--porcelain'], COMMIT_MS)
68
- return r.ok && r.out.length > 0
69
- }
70
-
71
- /** 拉取(只快进)。 */
72
- export async function pull(repo) {
73
- const r = await runGit(repo, ['pull', '--ff-only', '--quiet'], PULL_MS)
74
- return { ok: r.ok, detail: r.ok ? '' : (r.err || r.out).slice(0, 200) }
75
- }
76
-
77
- /**
78
- * 提交全部改动。撞上另一个窗口的 index.lock 时退避重试;
79
- * 重试仍失败就把改动留在工作区并返回失败,由下次提交带上。
80
- */
81
- export async function commit(repo, message, attempts = 3) {
82
- for (let attempt = 1; attempt <= attempts; attempt += 1) {
83
- const add = await runGit(repo, ['add', '-A'], COMMIT_MS)
84
- if (!add.ok) return { ok: false, committed: false, detail: (add.err || add.out).slice(0, 200) }
85
- const done = await runGit(repo, ['commit', '-m', message, '--quiet'], COMMIT_MS)
86
- if (done.ok) return { ok: true, committed: true, detail: '' }
87
- const text = `${done.err}\n${done.out}`
88
- if (/nothing to commit|no changes added/i.test(text)) return { ok: true, committed: false, detail: '' }
89
- const locked = /index\.lock|Another git process|Unable to create|File exists/i.test(text)
90
- if (locked && attempt < attempts) {
91
- await sleep(150 * attempt)
92
- continue
93
- }
94
- return { ok: false, committed: false, detail: text.trim().slice(0, 200) }
95
- }
96
- return { ok: false, committed: false, detail: 'commit retries exhausted' }
97
- }
98
-
99
- /** 后台推送:分离子进程,回合不等它,父进程退出也不等。 */
100
- export function pushBackground(repo) {
101
- try {
102
- const child = spawn('git', ['-C', repo, 'push', '--quiet'], {
103
- env: gitEnv(),
104
- stdio: 'ignore',
105
- windowsHide: true,
106
- detached: true,
107
- })
108
- child.unref()
109
- return true
110
- } catch {
111
- return false
112
- }
113
- }
114
-
115
- /** 同步等待的推送,只给进程收尾用;超时即放弃。 */
116
- export async function pushNow(repo) {
117
- const r = await runGit(repo, ['push', '--quiet'], PUSH_MS)
118
- return { ok: r.ok, detail: r.ok ? '' : (r.err || r.out).slice(0, 200) }
119
- }
1
+ /**
2
+ * 最小 git 封装:只做记忆仓需要的 pull / commit / push。
3
+ *
4
+ * 三条纪律,都是为了「不挡回合、不打断对话」:
5
+ * 1. 所有调用都有超时,且禁止交互(GIT_TERMINAL_PROMPT=0、GCM_INTERACTIVE=never)。
6
+ * 2. 提交遇到 index.lock 时退避重试;仍失败就把改动留在工作区,下次提交带上。
7
+ * 3. 推送在后台跑,回合不等它;失败只记日志,下次再补推。
8
+ */
9
+
10
+ import { execFile, spawn } from 'node:child_process'
11
+
12
+ const PULL_MS = 5_000
13
+ const COMMIT_MS = 5_000
14
+ const PUSH_MS = 15_000
15
+
16
+ function gitEnv() {
17
+ return {
18
+ ...process.env,
19
+ GIT_TERMINAL_PROMPT: '0',
20
+ GCM_INTERACTIVE: 'never',
21
+ GIT_OPTIONAL_LOCKS: '0',
22
+ }
23
+ }
24
+
25
+ /** 跑一条 git 命令;永远 resolve,不抛。 */
26
+ export function runGit(repo, args, timeoutMs = COMMIT_MS) {
27
+ return new Promise((resolve) => {
28
+ execFile(
29
+ 'git',
30
+ ['-C', repo, ...args],
31
+ { timeout: timeoutMs, windowsHide: true, maxBuffer: 4 * 1024 * 1024, env: gitEnv() },
32
+ (error, stdout, stderr) => {
33
+ resolve({
34
+ ok: error === null,
35
+ code: typeof error?.code === 'number' ? error.code : error === null ? 0 : 1,
36
+ out: String(stdout || '').trim(),
37
+ err: String(stderr || '').trim(),
38
+ timedOut: error?.killed === true,
39
+ })
40
+ },
41
+ )
42
+ })
43
+ }
44
+
45
+ const sleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms) })
46
+
47
+ /** 是否是一个 git 工作树。 */
48
+ export async function isRepo(repo) {
49
+ const r = await runGit(repo, ['rev-parse', '--is-inside-work-tree'], 4_000)
50
+ return r.ok && r.out === 'true'
51
+ }
52
+
53
+ /** 工作树根目录。 */
54
+ export async function topLevel(repo) {
55
+ const r = await runGit(repo, ['rev-parse', '--show-toplevel'], 4_000)
56
+ return r.ok ? r.out : ''
57
+ }
58
+
59
+ /** 目录所在的 git 分支;不是仓库(或 detached)就返回空串。用它当「工作线」。 */
60
+ export async function branchOf(dir) {
61
+ const r = await runGit(dir, ['symbolic-ref', '--short', 'HEAD'], 4_000)
62
+ return r.ok ? r.out : ''
63
+ }
64
+
65
+ /** 是否有未提交改动。 */
66
+ export async function isDirty(repo) {
67
+ const r = await runGit(repo, ['status', '--porcelain'], COMMIT_MS)
68
+ return r.ok && r.out.length > 0
69
+ }
70
+
71
+ /** 拉取(只快进)。 */
72
+ export async function pull(repo) {
73
+ const r = await runGit(repo, ['pull', '--ff-only', '--quiet'], PULL_MS)
74
+ return { ok: r.ok, detail: r.ok ? '' : (r.err || r.out).slice(0, 200) }
75
+ }
76
+
77
+ /**
78
+ * 提交全部改动。撞上另一个窗口的 index.lock 时退避重试;
79
+ * 重试仍失败就把改动留在工作区并返回失败,由下次提交带上。
80
+ */
81
+ export async function commit(repo, message, attempts = 3) {
82
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
83
+ const add = await runGit(repo, ['add', '-A'], COMMIT_MS)
84
+ if (!add.ok) return { ok: false, committed: false, detail: (add.err || add.out).slice(0, 200) }
85
+ const done = await runGit(repo, ['commit', '-m', message, '--quiet'], COMMIT_MS)
86
+ if (done.ok) return { ok: true, committed: true, detail: '' }
87
+ const text = `${done.err}\n${done.out}`
88
+ if (/nothing to commit|no changes added/i.test(text)) return { ok: true, committed: false, detail: '' }
89
+ const locked = /index\.lock|Another git process|Unable to create|File exists/i.test(text)
90
+ if (locked && attempt < attempts) {
91
+ await sleep(150 * attempt)
92
+ continue
93
+ }
94
+ return { ok: false, committed: false, detail: text.trim().slice(0, 200) }
95
+ }
96
+ return { ok: false, committed: false, detail: 'commit retries exhausted' }
97
+ }
98
+
99
+ /** 后台推送:分离子进程,回合不等它,父进程退出也不等。 */
100
+ export function pushBackground(repo) {
101
+ try {
102
+ const child = spawn('git', ['-C', repo, 'push', '--quiet'], {
103
+ env: gitEnv(),
104
+ stdio: 'ignore',
105
+ windowsHide: true,
106
+ detached: true,
107
+ })
108
+ child.unref()
109
+ return true
110
+ } catch {
111
+ return false
112
+ }
113
+ }
114
+
115
+ /** 同步等待的推送,只给进程收尾用;超时即放弃。 */
116
+ export async function pushNow(repo) {
117
+ const r = await runGit(repo, ['push', '--quiet'], PUSH_MS)
118
+ return { ok: r.ok, detail: r.ok ? '' : (r.err || r.out).slice(0, 200) }
119
+ }