@consilium108/mcp-module-devops 1.1.0 → 1.3.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,145 @@
1
+ // generated by Claude (Opus 5.5), 2026-11-24; Konsilium TASK-P37-486, SDD-P37-160 (spec §6): the orchestrator's durable
2
+ // local state — ONE file state.json (tmp + rename after every change, 0600 in a 0700 directory) with
3
+ // cursors per project: the last event of get_events / SSE handled (saved AFTER the event was handled: at-least-once)
4
+ // seen dedup keys of handled events (p<project>:e<event id>) → time; pruned (30 days, at most 5000)
5
+ // jobs the queue: every job with its status, lock key, lease, attempts, sources and history
6
+ // outbox reports to Konsilium not delivered yet (retried with backoff; idempotent on the server)
7
+ // dispatches the last dry-run dispatch plans (what would have been started)
8
+ // The server stays the source of truth for tasks, deploy requests and approvals; this file only coordinates local agents.
9
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
10
+ import { join } from 'node:path';
11
+
12
+ const SEEN_MAX = 5000;
13
+ const SEEN_TTL_MS = 30 * 24 * 3600_000;
14
+ const KEEP_FINISHED = 200;
15
+ const KEEP_DISPATCHES = 50;
16
+ const KEEP_HISTORY = 30;
17
+
18
+ export const JOB_STATUSES = ['queued', 'assigned', 'running', 'waiting_for_user', 'blocked', 'succeeded', 'failed', 'cancelled'];
19
+ /** Statuses that hold the lock of a target (one deploy-related job per project + environment + target at a time). */
20
+ export const LOCKING = new Set(['assigned', 'running', 'waiting_for_user']);
21
+ export const FINISHED = new Set(['succeeded', 'failed', 'cancelled', 'blocked']);
22
+
23
+ const empty = () => ({ version: 1, seq: 0, cursors: {}, seen: {}, jobs: [], outbox: [], dispatches: [] });
24
+
25
+ /** @param {{ dir: string, now?: () => number }} o */
26
+ export function createOrchestratorStore({ dir, now = Date.now }) {
27
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
28
+ const file = join(dir, 'state.json');
29
+ let s = empty();
30
+ let broken = false;
31
+ if (existsSync(file)) {
32
+ try {
33
+ s = { ...empty(), ...JSON.parse(readFileSync(file, 'utf8')) };
34
+ } catch {
35
+ broken = true; // a broken file: start clean (events are re-read from the server journal; deploys are on the server)
36
+ }
37
+ }
38
+ const save = () => {
39
+ const tmp = `${file}.${process.pid}.tmp`;
40
+ writeFileSync(tmp, JSON.stringify(s), { mode: 0o600 });
41
+ renameSync(tmp, file);
42
+ };
43
+ const iso = (t = now()) => new Date(t).toISOString();
44
+ const find = (id) => s.jobs.find((j) => j.id === id) ?? null;
45
+
46
+ function prune() {
47
+ const cut = now() - SEEN_TTL_MS;
48
+ let entries = Object.entries(s.seen).filter(([, t]) => t >= cut);
49
+ if (entries.length > SEEN_MAX) entries = entries.sort((a, b) => b[1] - a[1]).slice(0, SEEN_MAX);
50
+ s.seen = Object.fromEntries(entries);
51
+ const done = s.jobs.filter((j) => FINISHED.has(j.status));
52
+ if (done.length > KEEP_FINISHED) {
53
+ const drop = new Set(done.sort((a, b) => Date.parse(a.updatedAt) - Date.parse(b.updatedAt)).slice(0, done.length - KEEP_FINISHED).map((j) => j.id));
54
+ s.jobs = s.jobs.filter((j) => !drop.has(j.id));
55
+ }
56
+ }
57
+
58
+ return {
59
+ file,
60
+ dir,
61
+ recoveredFromBrokenFile: broken,
62
+ snapshot: () => JSON.parse(JSON.stringify(s)),
63
+ cursor: {
64
+ get: (pid) => (s.cursors[pid] ?? null),
65
+ set(pid, c) {
66
+ if (Number.isFinite(c) && c > (s.cursors[pid] ?? 0)) {
67
+ s.cursors[pid] = c;
68
+ save();
69
+ }
70
+ },
71
+ },
72
+ seen: {
73
+ has: (key) => !!s.seen[key],
74
+ add(key) {
75
+ if (s.seen[key]) return false;
76
+ s.seen[key] = now();
77
+ if (Object.keys(s.seen).length > SEEN_MAX * 1.1) prune();
78
+ save();
79
+ return true;
80
+ },
81
+ },
82
+ jobs: {
83
+ all: () => s.jobs.map((j) => ({ ...j })),
84
+ get: (id) => {
85
+ const j = find(id);
86
+ return j ? JSON.parse(JSON.stringify(j)) : null;
87
+ },
88
+ byIdempotency: (key) => s.jobs.find((j) => j.idempotencyKey === key) ?? null,
89
+ create(job) {
90
+ const id = `J${++s.seq}`;
91
+ const at = iso();
92
+ const j = { id, status: 'queued', attempts: 0, nextAttemptAt: 0, lease: null, agentId: null, deployId: null, deployStatus: null, result: null, createdAt: at, updatedAt: at, history: [{ at, status: 'queued', note: job.note ?? 'created' }], ...job };
93
+ delete j.note;
94
+ s.jobs.push(j);
95
+ save();
96
+ return { ...j };
97
+ },
98
+ /** patch + optional status change (recorded in history). */
99
+ update(id, patch = {}, note = null) {
100
+ const j = find(id);
101
+ if (!j) return null;
102
+ const prev = j.status;
103
+ Object.assign(j, patch);
104
+ j.updatedAt = iso();
105
+ if ((patch.status && patch.status !== prev) || note) j.history = [...j.history, { at: j.updatedAt, status: j.status, note: note ?? null }].slice(-KEEP_HISTORY);
106
+ if (FINISHED.has(j.status)) prune();
107
+ save();
108
+ return { ...j };
109
+ },
110
+ addSource(id, src) {
111
+ const j = find(id);
112
+ if (!j) return null;
113
+ if (!j.sources.some((x) => x.project_id === src.project_id && x.event_id === src.event_id)) j.sources = [...j.sources, src].slice(-20);
114
+ j.updatedAt = iso();
115
+ save();
116
+ return { ...j };
117
+ },
118
+ },
119
+ outbox: {
120
+ list: () => s.outbox.map((o) => ({ ...o })),
121
+ add(item) {
122
+ if (s.outbox.some((o) => o.id === item.id)) return false;
123
+ s.outbox.push({ attempts: 0, nextAt: 0, ...item });
124
+ save();
125
+ return true;
126
+ },
127
+ update(id, patch) {
128
+ const o = s.outbox.find((x) => x.id === id);
129
+ if (o) Object.assign(o, patch);
130
+ save();
131
+ },
132
+ remove(id) {
133
+ s.outbox = s.outbox.filter((x) => x.id !== id);
134
+ save();
135
+ },
136
+ },
137
+ dispatches: {
138
+ list: () => s.dispatches.map((d) => ({ ...d })),
139
+ add(d) {
140
+ s.dispatches = [...s.dispatches, d].slice(-KEEP_DISPATCHES);
141
+ save();
142
+ },
143
+ },
144
+ };
145
+ }
@@ -17,10 +17,10 @@ const alive = (pid) => {
17
17
  }
18
18
  };
19
19
 
20
- /** A lock file with our pid (O_EXCL); a stale lock of a dead process is taken over. */
21
- export function acquireLock(dir) {
20
+ /** A lock file with our pid (O_EXCL); a stale lock of a dead process is taken over. `name`: the orchestrator uses its own. */
21
+ export function acquireLock(dir, name = 'runner.lock') {
22
22
  mkdirSync(dir, { recursive: true, mode: 0o700 });
23
- const file = join(dir, 'runner.lock');
23
+ const file = join(dir, name);
24
24
  for (let i = 0; i < 2; i++) {
25
25
  try {
26
26
  const fd = openSync(file, 'wx', 0o600);
@@ -30,7 +30,7 @@ export function acquireLock(dir) {
30
30
  } catch (e) {
31
31
  if (e.code !== 'EEXIST') throw e;
32
32
  const pid = Number(readFileSync(file, 'utf8').trim());
33
- if (pid && pid !== process.pid && alive(pid)) throw new DevopsMcpError('already_running', `another runner (pid ${pid}) holds ${file} — one deploy at a time`);
33
+ if (pid && pid !== process.pid && alive(pid)) throw new DevopsMcpError('already_running', `another ${name === 'runner.lock' ? 'runner' : 'process'} (pid ${pid}) holds ${file}${name === 'runner.lock' ? ' — one deploy at a time' : ''}`);
34
34
  rmSync(file, { force: true });
35
35
  }
36
36
  }