pi-foreman 0.0.0-stage → 0.1.2

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/src/docs.mjs ADDED
@@ -0,0 +1,148 @@
1
+ /**
2
+ * 基于 pi-durable defineDoc 的 SQLite 事务化持久化状态(彻底取代 conversations.json 与 feed.jsonl)。
3
+ *
4
+ * 两份 session 级文档:
5
+ * 1. ConvMetaDoc (foreman.conv-meta) —— 会话与环境、cwd、标题、归档状态映射(与创建、/cd、fork 同步)
6
+ * 2. AsksDoc (foreman.asks) —— 进行中的跨环境提问账本,供 ask_peer 做防环与限深检查
7
+ */
8
+ import { existsSync, readFileSync } from "node:fs";
9
+ import { join } from "node:path";
10
+ import { defineDoc } from "@earendil-works/pi-durable";
11
+
12
+ export const ConvMetaDoc = defineDoc({
13
+ kind: "foreman.conv-meta",
14
+ version: 1,
15
+ scope: "session",
16
+ initial: () => ({ items: {} }),
17
+ checkpointWhen: (_val, ops) => ops.length > 20,
18
+ });
19
+
20
+ export const AsksDoc = defineDoc({
21
+ kind: "foreman.asks",
22
+ version: 1,
23
+ scope: "session",
24
+ initial: () => ({ items: {}, order: [] }),
25
+ checkpointWhen: (_val, ops) => ops.length > 30,
26
+ });
27
+
28
+ /**
29
+ * 会话元数据索引:内存热缓存 + SQLite durable doc 持久化。
30
+ * 首次加载时若文档为空且磁盘有旧 conversations.json,自动一次性迁移导入。
31
+ */
32
+ export class DurableConvIndex {
33
+ #harness;
34
+ #dir;
35
+ #ctx;
36
+ #cache = new Map();
37
+
38
+ constructor(harness, dir, ctx) {
39
+ this.#harness = harness;
40
+ this.#dir = dir;
41
+ this.#ctx = ctx;
42
+ }
43
+
44
+ async load() {
45
+ const snap = await this.#harness.snapshot(ConvMetaDoc, this.#ctx);
46
+ const items = snap?.items ?? {};
47
+ const count = Object.keys(items).length;
48
+
49
+ if (count === 0) {
50
+ const legacyFile = join(this.#dir, "conversations.json");
51
+ if (existsSync(legacyFile)) {
52
+ try {
53
+ const raw = JSON.parse(readFileSync(legacyFile, "utf8"));
54
+ if (raw && typeof raw === "object" && Object.keys(raw).length > 0) {
55
+ await this.#harness.commit(async (tx) => {
56
+ const doc = await tx.doc(ConvMetaDoc);
57
+ for (const [k, v] of Object.entries(raw)) {
58
+ doc.items[String(k)] = v;
59
+ }
60
+ }, this.#ctx);
61
+ for (const [k, v] of Object.entries(raw)) {
62
+ this.#cache.set(Number(k), v);
63
+ }
64
+ return;
65
+ }
66
+ } catch { /* 忽略坏旧文件 */ }
67
+ }
68
+ }
69
+
70
+ this.#cache.clear();
71
+ for (const [k, v] of Object.entries(items)) {
72
+ this.#cache.set(Number(k), v);
73
+ }
74
+ }
75
+
76
+ get(conversationId) {
77
+ return this.#cache.get(Number(conversationId));
78
+ }
79
+
80
+ /** 在已打开的事务内写入(例如创建会话 / fork 时同事务落盘) */
81
+ async setInTx(tx, conversationId, record) {
82
+ const id = Number(conversationId);
83
+ const doc = await tx.doc(ConvMetaDoc);
84
+ doc.items[String(id)] = record;
85
+ this.#cache.set(id, record);
86
+ }
87
+
88
+ async set(conversationId, record) {
89
+ const id = Number(conversationId);
90
+ await this.#harness.commit(async (tx) => {
91
+ const doc = await tx.doc(ConvMetaDoc);
92
+ doc.items[String(id)] = record;
93
+ }, this.#ctx);
94
+ this.#cache.set(id, record);
95
+ return record;
96
+ }
97
+
98
+ async patch(conversationId, partial) {
99
+ const id = Number(conversationId);
100
+ const prev = this.#cache.get(id);
101
+ if (!prev) return null;
102
+ const next = { ...prev, ...partial };
103
+ await this.set(id, next);
104
+ return next;
105
+ }
106
+
107
+ async delete(conversationId) {
108
+ const id = Number(conversationId);
109
+ if (!this.#cache.has(id)) return false;
110
+ await this.#harness.commit(async (tx) => {
111
+ const doc = await tx.doc(ConvMetaDoc);
112
+ delete doc.items[String(id)];
113
+ }, this.#ctx);
114
+ this.#cache.delete(id);
115
+ return true;
116
+ }
117
+
118
+ forEnv(envName, { includeArchived = false } = {}) {
119
+ return [...this.#cache.entries()]
120
+ .filter(([, v]) => v.env === envName && (includeArchived || !v.archived))
121
+ .map(([id, v]) => ({ conversationId: id, ...v }));
122
+ }
123
+
124
+ all({ includeArchived = false } = {}) {
125
+ return [...this.#cache.entries()]
126
+ .filter(([, v]) => includeArchived || !v.archived)
127
+ .map(([id, v]) => ({ conversationId: id, ...v }));
128
+ }
129
+ }
130
+
131
+ /** 读写 AsksDoc 的助手 */
132
+ const MAX_ASKS_HISTORY = 250;
133
+
134
+ export async function upsertAskRecord(harness, ctx, record) {
135
+ await harness.commit(async (tx) => {
136
+ const doc = await tx.doc(AsksDoc);
137
+ const key = String(record.id);
138
+ const exists = Boolean(doc.items[key]);
139
+ doc.items[key] = { ...(doc.items[key] ?? {}), ...record };
140
+ if (!exists) {
141
+ doc.order.push(key);
142
+ while (doc.order.length > MAX_ASKS_HISTORY) {
143
+ const drop = doc.order.shift();
144
+ if (drop !== undefined) delete doc.items[drop];
145
+ }
146
+ }
147
+ }, ctx);
148
+ }