ostacky 0.10.2 → 0.11.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/README.md +18 -3
- package/assets/agents/ostacky.md +20 -6
- package/assets/commands/install-stack.md +1 -1
- package/assets/docs/ostacky-reference.md +10 -1
- package/assets/mcp/ostacky-controller/controller-core.js +30 -0
- package/assets/mcp/ostacky-controller/index.js +405 -53
- package/assets/mcp/ostacky-controller/session-slots.js +110 -0
- package/assets/mcp/ostacky-controller/session-store.js +290 -0
- package/assets/plugins/engram.ts +13 -9
- package/assets/plugins/ostacky-controller/compaction-threshold.ts +274 -0
- package/assets/plugins/ostacky-controller/controller-core.ts +30 -0
- package/assets/plugins/ostacky-controller/index.ts +532 -113
- package/assets/plugins/ostacky-controller/session-slots.ts +167 -0
- package/assets/plugins/ostacky-controller/session-store.ts +394 -0
- package/assets/plugins/ostacky-controller/tiered.ts +29 -0
- package/dist/cli.js +446 -141
- package/dist/mcp/openspec/index.js +19335 -0
- package/dist/mcp/openspec/package.json +1 -0
- package/dist/mcp/ostacky-controller/index.js +23662 -0
- package/dist/mcp/ostacky-controller/package.json +1 -0
- package/manifest.json +31 -29
- package/package.json +9 -6
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-slots — mirror (JS) de src/session-slots.ts
|
|
3
|
+
* Generado desde src/session-slots.ts — NO EDITAR. Ejecutá `bun run scripts/sync-controller-core.ts`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const TERMINAL_REQUEST_STATES = ["DONE", "ABANDONED"];
|
|
7
|
+
export const POOL_DEFAULTS = {
|
|
8
|
+
poolMax: 10,
|
|
9
|
+
overflowMax: 5,
|
|
10
|
+
activeMinutes: 15,
|
|
11
|
+
recycleDays: 7
|
|
12
|
+
};
|
|
13
|
+
export function parsePoolConfig(env = process.env) {
|
|
14
|
+
const num = (raw, fallback) => {
|
|
15
|
+
const n = Number(raw);
|
|
16
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
poolMax: Math.floor(num(env.OSTACKY_SESSION_POOL_MAX, POOL_DEFAULTS.poolMax)),
|
|
20
|
+
overflowMax: Math.floor(num(env.OSTACKY_SESSION_OVERFLOW_MAX, POOL_DEFAULTS.overflowMax)),
|
|
21
|
+
activeMs: num(env.OSTACKY_SESSION_ACTIVE_MIN, POOL_DEFAULTS.activeMinutes) * 60000,
|
|
22
|
+
recycleMs: num(env.OSTACKY_SESSION_RECYCLE_DAYS, POOL_DEFAULTS.recycleDays) * 86400000
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function isActiveSlot(s, now, cfg) {
|
|
26
|
+
if (!s.sessionId || !s.requestId || s.quarantined)
|
|
27
|
+
return false;
|
|
28
|
+
if (s.state && TERMINAL_REQUEST_STATES.includes(s.state))
|
|
29
|
+
return false;
|
|
30
|
+
return s.ownerHeartbeat > 0 && now - s.ownerHeartbeat <= cfg.activeMs;
|
|
31
|
+
}
|
|
32
|
+
export function isRecycleEligible(s, now, cfg) {
|
|
33
|
+
if (s.quarantined)
|
|
34
|
+
return false;
|
|
35
|
+
const terminal = !s.requestId || !!s.state && TERMINAL_REQUEST_STATES.includes(s.state);
|
|
36
|
+
if (!terminal)
|
|
37
|
+
return false;
|
|
38
|
+
return now - s.lastUsedAt > cfg.recycleMs;
|
|
39
|
+
}
|
|
40
|
+
const byOldestFirst = (a, b) => a.lastUsedAt - b.lastUsedAt || a.slot.localeCompare(b.slot);
|
|
41
|
+
export function decideSlot(sessionId, slots, now, cfg, opts = {}) {
|
|
42
|
+
const usable = slots.filter((s) => !s.quarantined);
|
|
43
|
+
const own = usable.find((s) => s.sessionId === sessionId);
|
|
44
|
+
if (own)
|
|
45
|
+
return { kind: "reuse", slot: own.slot };
|
|
46
|
+
const free = usable.filter((s) => !s.sessionId);
|
|
47
|
+
const freeBase = free.filter((s) => !s.overflow).sort(byOldestFirst);
|
|
48
|
+
const freeAny = freeBase.length > 0 ? freeBase : free.sort(byOldestFirst);
|
|
49
|
+
if (freeAny.length > 0)
|
|
50
|
+
return { kind: "free", slot: freeAny[0].slot };
|
|
51
|
+
const baseCount = slots.filter((s) => !s.overflow).length;
|
|
52
|
+
const overflowCount = slots.filter((s) => s.overflow).length;
|
|
53
|
+
const activeCount = usable.filter((s) => isActiveSlot(s, now, cfg)).length;
|
|
54
|
+
const notify = [];
|
|
55
|
+
if (activeCount < cfg.poolMax && baseCount < cfg.poolMax) {
|
|
56
|
+
return { kind: "create", slot: nextSlotId(slots, false), overflow: false, notify };
|
|
57
|
+
}
|
|
58
|
+
const eligible = usable.filter((s) => isRecycleEligible(s, now, cfg));
|
|
59
|
+
const eligibleBase = eligible.filter((s) => !s.overflow).sort(byOldestFirst);
|
|
60
|
+
const eligibleAny = eligibleBase.length > 0 ? eligibleBase : eligible.sort(byOldestFirst);
|
|
61
|
+
if (eligibleAny.length > 0) {
|
|
62
|
+
const pick = eligibleAny[0];
|
|
63
|
+
if (activeCount >= cfg.poolMax)
|
|
64
|
+
notify.push(`Ya hay ${cfg.poolMax} sesiones activas ejecutando al mismo tiempo (${activeCount} activas).`);
|
|
65
|
+
return { kind: "recycle", slot: pick.slot, previousSessionId: pick.sessionId, notify };
|
|
66
|
+
}
|
|
67
|
+
if (activeCount >= cfg.poolMax) {
|
|
68
|
+
notify.push(`Ya hay ${cfg.poolMax} sesiones activas ejecutando al mismo tiempo (${activeCount} activas).`);
|
|
69
|
+
if (opts.allowOverflow && overflowCount < cfg.overflowMax) {
|
|
70
|
+
notify.push(`Extensión ${overflowCount + 1}/${cfg.overflowMax} en uso (confirmada por el usuario).`);
|
|
71
|
+
return { kind: "create", slot: nextSlotId(slots, true), overflow: true, notify };
|
|
72
|
+
}
|
|
73
|
+
if (!opts.allowOverflow) {
|
|
74
|
+
notify.push(`Para extender el pool hay que confirmar (extensión 1/${cfg.overflowMax}); o esperar a que termine una de las sesiones activas.`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const candidates = usable.filter((s) => s.sessionId).sort(byOldestFirst);
|
|
78
|
+
notify.push("No hay cupo: elegí una sesión para sustituir o cancelá (sin respuesta no se cambia nada).");
|
|
79
|
+
return { kind: "ask", candidates, notify };
|
|
80
|
+
}
|
|
81
|
+
export function nextSlotId(slots, overflow) {
|
|
82
|
+
const taken = new Set(slots.map((s) => s.slot));
|
|
83
|
+
const start = overflow ? 11 : 1;
|
|
84
|
+
for (let n = start;n < start + 100; n++) {
|
|
85
|
+
const id = `pool-${String(n).padStart(2, "0")}`;
|
|
86
|
+
if (!taken.has(id))
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
throw new Error("no hay ids de slot disponibles");
|
|
90
|
+
}
|
|
91
|
+
export function parseSessionListTitle(json, sessionId) {
|
|
92
|
+
let rows;
|
|
93
|
+
try {
|
|
94
|
+
rows = JSON.parse(json);
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(rows))
|
|
99
|
+
return null;
|
|
100
|
+
const row = rows.find((r) => r && r.id === sessionId);
|
|
101
|
+
if (!row || typeof row.title !== "string")
|
|
102
|
+
return null;
|
|
103
|
+
const title = row.title.trim();
|
|
104
|
+
if (!title || title === "Untitled session")
|
|
105
|
+
return null;
|
|
106
|
+
return title.slice(0, 120);
|
|
107
|
+
}
|
|
108
|
+
export function nextRevision(previousRevision) {
|
|
109
|
+
return Math.max(0, Math.floor(previousRevision)) + 1;
|
|
110
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-store — mirror (JS) de src/session-store.ts
|
|
3
|
+
* Generado desde src/session-store.ts — NO EDITAR. Ejecutá `bun run scripts/sync-controller-core.ts`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { DEFAULT_STATE } from "./controller-core.js";
|
|
9
|
+
import { decideSlot, isActiveSlot, nextRevision } from "./session-slots.js";
|
|
10
|
+
const FILE_ACTIVITY_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
11
|
+
export function noteFileEdit(opencodeDir, sessionId, filePath, now, windowMs) {
|
|
12
|
+
return withIndexLock(opencodeDir, () => {
|
|
13
|
+
const idx = readIndex(opencodeDir);
|
|
14
|
+
const own = Object.values(idx.slots).find((r) => r.sessionId === sessionId);
|
|
15
|
+
if (!own)
|
|
16
|
+
return [];
|
|
17
|
+
const activity = idx.fileActivity ?? {};
|
|
18
|
+
for (const [file, entry] of Object.entries(activity)) {
|
|
19
|
+
if (now - entry.ts > FILE_ACTIVITY_RETENTION_MS)
|
|
20
|
+
delete activity[file];
|
|
21
|
+
}
|
|
22
|
+
const prev = activity[filePath];
|
|
23
|
+
const warnings = [];
|
|
24
|
+
if (prev && prev.sessionId !== sessionId && now - prev.ts <= windowMs) {
|
|
25
|
+
warnings.push({
|
|
26
|
+
file: filePath,
|
|
27
|
+
otherName: prev.sessionName || prev.requestId || "sesión sin nombre",
|
|
28
|
+
otherRequestId: prev.requestId,
|
|
29
|
+
minutesAgo: Math.round((now - prev.ts) / 60000)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
activity[filePath] = { sessionId, sessionName: own.sessionName, requestId: own.requestId, ts: now };
|
|
33
|
+
idx.fileActivity = activity;
|
|
34
|
+
idx.revision += 1;
|
|
35
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
36
|
+
return warnings;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export function sessionsDir(opencodeDir) {
|
|
40
|
+
return join(opencodeDir, "sessions");
|
|
41
|
+
}
|
|
42
|
+
export function slotPath(opencodeDir, slot) {
|
|
43
|
+
return join(sessionsDir(opencodeDir), `${slot}.json`);
|
|
44
|
+
}
|
|
45
|
+
function indexPath(opencodeDir) {
|
|
46
|
+
return join(sessionsDir(opencodeDir), "index.json");
|
|
47
|
+
}
|
|
48
|
+
function emptyIndex() {
|
|
49
|
+
return { version: 1, revision: 0, migrated: false, slots: {} };
|
|
50
|
+
}
|
|
51
|
+
export function readIndex(opencodeDir) {
|
|
52
|
+
try {
|
|
53
|
+
const parsed = JSON.parse(readFileSync(indexPath(opencodeDir), "utf-8"));
|
|
54
|
+
if (parsed && typeof parsed === "object" && parsed.slots && typeof parsed.slots === "object")
|
|
55
|
+
return parsed;
|
|
56
|
+
} catch {}
|
|
57
|
+
return emptyIndex();
|
|
58
|
+
}
|
|
59
|
+
function writeAtomic(path, content) {
|
|
60
|
+
const tmp = `${path}.tmp.${process.pid}.${Date.now()}`;
|
|
61
|
+
writeFileSync(tmp, content, "utf-8");
|
|
62
|
+
renameSync(tmp, path);
|
|
63
|
+
}
|
|
64
|
+
function sleepSync(ms) {
|
|
65
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
66
|
+
}
|
|
67
|
+
function withIndexLock(opencodeDir, fn) {
|
|
68
|
+
mkdirSync(sessionsDir(opencodeDir), { recursive: true });
|
|
69
|
+
const lockDir = indexPath(opencodeDir) + ".lock";
|
|
70
|
+
for (let attempt = 0;attempt < 200; attempt++) {
|
|
71
|
+
try {
|
|
72
|
+
mkdirSync(lockDir);
|
|
73
|
+
try {
|
|
74
|
+
return fn();
|
|
75
|
+
} finally {
|
|
76
|
+
try {
|
|
77
|
+
rmSync(lockDir, { recursive: true, force: true });
|
|
78
|
+
} catch {}
|
|
79
|
+
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err?.code !== "EEXIST")
|
|
82
|
+
throw err;
|
|
83
|
+
try {
|
|
84
|
+
const ageMs = Date.now() - statMtime(lockDir);
|
|
85
|
+
if (ageMs > 15000)
|
|
86
|
+
rmSync(lockDir, { recursive: true, force: true });
|
|
87
|
+
} catch {}
|
|
88
|
+
sleepSync(10 + Math.floor(Math.random() * 20));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
throw new Error("no se pudo tomar el lock del índice de sesiones");
|
|
92
|
+
}
|
|
93
|
+
function statMtime(path) {
|
|
94
|
+
return statSync(path).mtimeMs;
|
|
95
|
+
}
|
|
96
|
+
function toInfo(slot, r) {
|
|
97
|
+
return {
|
|
98
|
+
slot,
|
|
99
|
+
overflow: r.overflow,
|
|
100
|
+
quarantined: r.quarantined,
|
|
101
|
+
sessionId: r.sessionId,
|
|
102
|
+
requestId: r.requestId,
|
|
103
|
+
state: r.state,
|
|
104
|
+
ownerHeartbeat: r.ownerHeartbeat,
|
|
105
|
+
lastUsedAt: r.lastUsedAt
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function resetSlotFile(opencodeDir, slot, previousRevision) {
|
|
109
|
+
const state = { ...structuredClone(DEFAULT_STATE), revision: nextRevision(previousRevision) };
|
|
110
|
+
writeAtomic(slotPath(opencodeDir, slot), JSON.stringify(state, null, 2));
|
|
111
|
+
}
|
|
112
|
+
function readSlotRevision(opencodeDir, slot) {
|
|
113
|
+
try {
|
|
114
|
+
const st = JSON.parse(readFileSync(slotPath(opencodeDir, slot), "utf-8"));
|
|
115
|
+
return typeof st.revision === "number" ? st.revision : 0;
|
|
116
|
+
} catch {
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function slotProblem(opencodeDir, slot) {
|
|
121
|
+
const path = slotPath(opencodeDir, slot);
|
|
122
|
+
if (!existsSync(path))
|
|
123
|
+
return null;
|
|
124
|
+
try {
|
|
125
|
+
const st = JSON.parse(readFileSync(path, "utf-8"));
|
|
126
|
+
if (!st || typeof st !== "object" || typeof st.state !== "string")
|
|
127
|
+
return "estado sin campo state";
|
|
128
|
+
return null;
|
|
129
|
+
} catch (err) {
|
|
130
|
+
return `JSON inválido: ${err?.message ?? "error"}`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export function bindSession(args) {
|
|
134
|
+
const { opencodeDir, sessionId, now, cfg } = args;
|
|
135
|
+
const name = args.sessionName ?? null;
|
|
136
|
+
return withIndexLock(opencodeDir, () => {
|
|
137
|
+
const idx = readIndex(opencodeDir);
|
|
138
|
+
if (!idx.migrated && args.legacyStatePath && existsSync(args.legacyStatePath) && Object.keys(idx.slots).length === 0) {
|
|
139
|
+
const legacy = readFileSync(args.legacyStatePath, "utf-8");
|
|
140
|
+
writeAtomic(slotPath(opencodeDir, "pool-01"), legacy);
|
|
141
|
+
idx.slots["pool-01"] = emptyRecord({ requestId: null });
|
|
142
|
+
idx.migrated = true;
|
|
143
|
+
try {
|
|
144
|
+
renameSync(args.legacyStatePath, args.legacyStatePath + ".migrated");
|
|
145
|
+
} catch {}
|
|
146
|
+
}
|
|
147
|
+
for (const [slot, rec] of Object.entries(idx.slots)) {
|
|
148
|
+
const problem = slotProblem(opencodeDir, slot);
|
|
149
|
+
if (problem) {
|
|
150
|
+
rec.quarantined = true;
|
|
151
|
+
rec.quarantineReason = problem;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const view = Object.entries(idx.slots).map(([s, r]) => toInfo(s, r));
|
|
155
|
+
const names = new Map(Object.entries(idx.slots).map(([s, r]) => [s, r.sessionName]));
|
|
156
|
+
if (args.replaceSlot) {
|
|
157
|
+
const target = idx.slots[args.replaceSlot];
|
|
158
|
+
if (!target)
|
|
159
|
+
return { kind: "blocked", reason: `slot ${args.replaceSlot} no existe` };
|
|
160
|
+
resetSlotFile(opencodeDir, args.replaceSlot, readSlotRevision(opencodeDir, args.replaceSlot));
|
|
161
|
+
if (target.sessionId) {
|
|
162
|
+
idx.replaced = idx.replaced ?? {};
|
|
163
|
+
idx.replaced[target.sessionId] = { slot: args.replaceSlot, at: now };
|
|
164
|
+
}
|
|
165
|
+
Object.assign(target, emptyRecord({ sessionId, sessionName: name, assignedAt: now, lastUsedAt: now, ownerHeartbeat: now, overflow: target.overflow }));
|
|
166
|
+
idx.revision += 1;
|
|
167
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
168
|
+
return { kind: "bound", slot: args.replaceSlot, statePath: slotPath(opencodeDir, args.replaceSlot), notify: [`Slot ${args.replaceSlot} sustituido por confirmación del usuario (slot_takeover).`] };
|
|
169
|
+
}
|
|
170
|
+
const replacedInfo = idx.replaced?.[sessionId];
|
|
171
|
+
if (replacedInfo) {
|
|
172
|
+
return { kind: "blocked", reason: `tu sesión fue sustituida en ${replacedInfo.slot} por confirmación del usuario; iniciá una sesión nueva de OpenCode para continuar` };
|
|
173
|
+
}
|
|
174
|
+
const own = Object.entries(idx.slots).find(([, r]) => r.sessionId === sessionId && !r.quarantined);
|
|
175
|
+
if (own) {
|
|
176
|
+
const rec = own[1];
|
|
177
|
+
rec.lastUsedAt = now;
|
|
178
|
+
rec.ownerHeartbeat = now;
|
|
179
|
+
if (name)
|
|
180
|
+
rec.sessionName = name;
|
|
181
|
+
idx.revision += 1;
|
|
182
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
183
|
+
return { kind: "bound", slot: own[0], statePath: slotPath(opencodeDir, own[0]), notify: [] };
|
|
184
|
+
}
|
|
185
|
+
const decision = decideSlot(sessionId, view, now, cfg, { allowOverflow: !!args.allowOverflow });
|
|
186
|
+
if (decision.kind === "ask") {
|
|
187
|
+
return {
|
|
188
|
+
kind: "ask",
|
|
189
|
+
candidates: decision.candidates.map((c) => ({ ...c, sessionName: names.get(c.slot) ?? null })),
|
|
190
|
+
notify: decision.notify
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
let slot;
|
|
194
|
+
let notify = [];
|
|
195
|
+
if (decision.kind === "create") {
|
|
196
|
+
slot = decision.slot;
|
|
197
|
+
notify = decision.notify;
|
|
198
|
+
idx.slots[slot] = emptyRecord({ overflow: decision.overflow });
|
|
199
|
+
resetSlotFile(opencodeDir, slot, 0);
|
|
200
|
+
} else if (decision.kind === "free") {
|
|
201
|
+
slot = decision.slot;
|
|
202
|
+
idx.slots[slot] = emptyRecord({ overflow: idx.slots[slot].overflow });
|
|
203
|
+
} else {
|
|
204
|
+
slot = decision.slot;
|
|
205
|
+
if (decision.kind === "recycle")
|
|
206
|
+
notify = decision.notify;
|
|
207
|
+
const overflow = idx.slots[slot].overflow;
|
|
208
|
+
resetSlotFile(opencodeDir, slot, readSlotRevision(opencodeDir, slot));
|
|
209
|
+
idx.slots[slot] = emptyRecord({ overflow });
|
|
210
|
+
}
|
|
211
|
+
const rec = idx.slots[slot];
|
|
212
|
+
rec.sessionId = sessionId;
|
|
213
|
+
rec.sessionName = name;
|
|
214
|
+
rec.assignedAt = now;
|
|
215
|
+
rec.lastUsedAt = now;
|
|
216
|
+
rec.ownerHeartbeat = now;
|
|
217
|
+
idx.revision += 1;
|
|
218
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
219
|
+
return { kind: "bound", slot, statePath: slotPath(opencodeDir, slot), notify };
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function emptyRecord(patch = {}) {
|
|
223
|
+
return {
|
|
224
|
+
sessionId: null,
|
|
225
|
+
sessionName: null,
|
|
226
|
+
requestId: null,
|
|
227
|
+
changeId: null,
|
|
228
|
+
state: null,
|
|
229
|
+
ownerHeartbeat: 0,
|
|
230
|
+
assignedAt: 0,
|
|
231
|
+
lastUsedAt: 0,
|
|
232
|
+
overflow: false,
|
|
233
|
+
quarantined: false,
|
|
234
|
+
quarantineReason: null,
|
|
235
|
+
...patch
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
export function checkBinding(opencodeDir, sessionId) {
|
|
239
|
+
const idx = readIndex(opencodeDir);
|
|
240
|
+
if (idx.replaced?.[sessionId])
|
|
241
|
+
return { ok: false, reason: `sesión sustituida en ${idx.replaced[sessionId].slot}` };
|
|
242
|
+
const own = Object.entries(idx.slots).find(([, r]) => r.sessionId === sessionId);
|
|
243
|
+
if (!own)
|
|
244
|
+
return { ok: false, reason: "la sesión no tiene slot asignado" };
|
|
245
|
+
return { ok: true, slot: own[0] };
|
|
246
|
+
}
|
|
247
|
+
export function recordRequestSummary(opencodeDir, sessionId, summary, now) {
|
|
248
|
+
withIndexLock(opencodeDir, () => {
|
|
249
|
+
const idx = readIndex(opencodeDir);
|
|
250
|
+
const own = Object.entries(idx.slots).find(([, r]) => r.sessionId === sessionId);
|
|
251
|
+
if (!own)
|
|
252
|
+
return;
|
|
253
|
+
Object.assign(own[1], { requestId: summary.requestId, changeId: summary.changeId, state: summary.state, lastUsedAt: now, ownerHeartbeat: now });
|
|
254
|
+
idx.revision += 1;
|
|
255
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
export function touchSession(opencodeDir, sessionId, now) {
|
|
259
|
+
withIndexLock(opencodeDir, () => {
|
|
260
|
+
const idx = readIndex(opencodeDir);
|
|
261
|
+
const own = Object.entries(idx.slots).find(([, r]) => r.sessionId === sessionId);
|
|
262
|
+
if (!own)
|
|
263
|
+
return;
|
|
264
|
+
own[1].ownerHeartbeat = now;
|
|
265
|
+
own[1].lastUsedAt = now;
|
|
266
|
+
writeAtomic(indexPath(opencodeDir), JSON.stringify(idx, null, 2));
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
export function poolReport(opencodeDir, now, cfg) {
|
|
270
|
+
const idx = readIndex(opencodeDir);
|
|
271
|
+
const entries = Object.entries(idx.slots);
|
|
272
|
+
const infos = entries.map(([s, r]) => toInfo(s, r));
|
|
273
|
+
const active = infos.filter((i) => i.sessionId && i.requestId && i.ownerHeartbeat > 0 && now - i.ownerHeartbeat <= cfg.activeMs && !["DONE", "ABANDONED"].includes(i.state ?? "")).length;
|
|
274
|
+
return {
|
|
275
|
+
base: entries.filter(([, r]) => !r.overflow).length,
|
|
276
|
+
overflow: entries.filter(([, r]) => r.overflow).length,
|
|
277
|
+
active,
|
|
278
|
+
quarantined: entries.filter(([, r]) => r.quarantined).map(([s, r]) => `${s}: ${r.quarantineReason ?? "motivo desconocido"}`),
|
|
279
|
+
occupied: infos.filter((i) => i.sessionId).sort((a, b) => a.lastUsedAt - b.lastUsedAt).map((i) => ({ ...i, sessionName: idx.slots[i.slot].sessionName, active: isActiveSlot(i, now, cfg) }))
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
export const HANDOFF_FALLBACK_BASE = ".ostacky-handoff-compaction";
|
|
283
|
+
export function handoffFallbackPath(opencodeDir, sessionId) {
|
|
284
|
+
const sid = process.env.OSTACKY_SESSION_SLOTS === "off" ? null : sessionId;
|
|
285
|
+
const key = sid ? sid.replace(/[^A-Za-z0-9_-]/g, "_") : null;
|
|
286
|
+
return join(opencodeDir, key ? `${HANDOFF_FALLBACK_BASE}-${key}.json` : `${HANDOFF_FALLBACK_BASE}.json`);
|
|
287
|
+
}
|
|
288
|
+
export function isHandoffFallbackFile(name) {
|
|
289
|
+
return name === HANDOFF_FALLBACK_BASE + ".json" || name.startsWith(HANDOFF_FALLBACK_BASE + "-") && name.endsWith(".json");
|
|
290
|
+
}
|
package/assets/plugins/engram.ts
CHANGED
|
@@ -575,17 +575,21 @@ export default {
|
|
|
575
575
|
// 2) try local opencode.json / jsonc in project
|
|
576
576
|
if (!statePath) {
|
|
577
577
|
const candidates = [join(directory, "opencode.json"), join(directory, "opencode.jsonc")]
|
|
578
|
-
// also try global config (XDG /
|
|
578
|
+
// also try global config (OPENCODE_CONFIG_DIR / XDG / ~/.config)
|
|
579
579
|
try {
|
|
580
|
+
const explicit = process.env.OPENCODE_CONFIG_DIR?.trim()
|
|
580
581
|
const home = process.env.HOME ?? process.env.USERPROFILE ?? ""
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
582
|
+
const xdg = process.env.XDG_CONFIG_HOME?.trim()
|
|
583
|
+
const globalDir = explicit
|
|
584
|
+
? explicit
|
|
585
|
+
: xdg
|
|
586
|
+
? join(xdg, "opencode")
|
|
587
|
+
: home
|
|
588
|
+
? join(home, ".config", "opencode")
|
|
589
|
+
: ""
|
|
590
|
+
if (globalDir) {
|
|
591
|
+
candidates.push(join(globalDir, "opencode.json"))
|
|
592
|
+
candidates.push(join(globalDir, "opencode.jsonc"))
|
|
589
593
|
}
|
|
590
594
|
} catch {}
|
|
591
595
|
for (const cand of candidates) {
|