kankaku-pi 1.0.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.
Files changed (153) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1438 -0
  3. package/dist/adapters/cached-catalog.d.ts +42 -0
  4. package/dist/adapters/cached-catalog.js +121 -0
  5. package/dist/adapters/export-writer.d.ts +13 -0
  6. package/dist/adapters/export-writer.js +28 -0
  7. package/dist/adapters/file-modes.d.ts +20 -0
  8. package/dist/adapters/file-modes.js +34 -0
  9. package/dist/adapters/hub-actions.d.ts +35 -0
  10. package/dist/adapters/hub-actions.js +70 -0
  11. package/dist/adapters/hub-credentials.d.ts +35 -0
  12. package/dist/adapters/hub-credentials.js +58 -0
  13. package/dist/adapters/jsonl-work-log.d.ts +20 -0
  14. package/dist/adapters/jsonl-work-log.js +62 -0
  15. package/dist/adapters/kankaku-dir.d.ts +38 -0
  16. package/dist/adapters/kankaku-dir.js +85 -0
  17. package/dist/adapters/lazy-jsonl-work-log.d.ts +17 -0
  18. package/dist/adapters/lazy-jsonl-work-log.js +31 -0
  19. package/dist/adapters/pocketbase-catalog.d.ts +16 -0
  20. package/dist/adapters/pocketbase-catalog.js +56 -0
  21. package/dist/adapters/pocketbase-client.d.ts +81 -0
  22. package/dist/adapters/pocketbase-client.js +148 -0
  23. package/dist/adapters/pocketbase-sink.d.ts +53 -0
  24. package/dist/adapters/pocketbase-sink.js +181 -0
  25. package/dist/adapters/project-config.d.ts +42 -0
  26. package/dist/adapters/project-config.js +108 -0
  27. package/dist/adapters/report-data.d.ts +12 -0
  28. package/dist/adapters/report-data.js +8 -0
  29. package/dist/adapters/report-views.d.ts +45 -0
  30. package/dist/adapters/report-views.js +73 -0
  31. package/dist/adapters/report.d.ts +112 -0
  32. package/dist/adapters/report.js +236 -0
  33. package/dist/adapters/sync-runner.d.ts +114 -0
  34. package/dist/adapters/sync-runner.js +273 -0
  35. package/dist/adapters/sync-state-store.d.ts +62 -0
  36. package/dist/adapters/sync-state-store.js +188 -0
  37. package/dist/config.d.ts +168 -0
  38. package/dist/config.js +392 -0
  39. package/dist/domain/ancestry-match.d.ts +49 -0
  40. package/dist/domain/ancestry-match.js +82 -0
  41. package/dist/domain/client-label.d.ts +28 -0
  42. package/dist/domain/client-label.js +44 -0
  43. package/dist/domain/day.d.ts +2 -0
  44. package/dist/domain/day.js +8 -0
  45. package/dist/domain/export.d.ts +38 -0
  46. package/dist/domain/export.js +68 -0
  47. package/dist/domain/hub-entry.d.ts +234 -0
  48. package/dist/domain/hub-entry.js +265 -0
  49. package/dist/domain/index.d.ts +19 -0
  50. package/dist/domain/index.js +19 -0
  51. package/dist/domain/intervals.d.ts +17 -0
  52. package/dist/domain/intervals.js +43 -0
  53. package/dist/domain/registry-health.d.ts +49 -0
  54. package/dist/domain/registry-health.js +58 -0
  55. package/dist/domain/segment-rule.d.ts +10 -0
  56. package/dist/domain/segment-rule.js +1 -0
  57. package/dist/domain/subagent-profile.d.ts +278 -0
  58. package/dist/domain/subagent-profile.js +418 -0
  59. package/dist/domain/sync-plan.d.ts +151 -0
  60. package/dist/domain/sync-plan.js +196 -0
  61. package/dist/domain/task-view.d.ts +117 -0
  62. package/dist/domain/task-view.js +428 -0
  63. package/dist/domain/work-record.d.ts +236 -0
  64. package/dist/domain/work-record.js +91 -0
  65. package/dist/domain/work-target.d.ts +101 -0
  66. package/dist/domain/work-target.js +149 -0
  67. package/dist/domain/work-tracker.d.ts +90 -0
  68. package/dist/domain/work-tracker.js +405 -0
  69. package/dist/hub/index.d.ts +25 -0
  70. package/dist/hub/index.js +25 -0
  71. package/dist/ports/catalog.d.ts +31 -0
  72. package/dist/ports/catalog.js +1 -0
  73. package/dist/ports/clock.d.ts +3 -0
  74. package/dist/ports/clock.js +1 -0
  75. package/dist/ports/index.d.ts +11 -0
  76. package/dist/ports/index.js +1 -0
  77. package/dist/ports/inflight-store.d.ts +15 -0
  78. package/dist/ports/inflight-store.js +1 -0
  79. package/dist/ports/process-registry.d.ts +72 -0
  80. package/dist/ports/process-registry.js +1 -0
  81. package/dist/ports/work-log.d.ts +14 -0
  82. package/dist/ports/work-log.js +1 -0
  83. package/dist/ports/work-sink.d.ts +39 -0
  84. package/dist/ports/work-sink.js +1 -0
  85. package/package.json +66 -0
  86. package/src/adapters/agent-info.ts +86 -0
  87. package/src/adapters/ancestry.ts +260 -0
  88. package/src/adapters/cached-catalog.ts +147 -0
  89. package/src/adapters/export-writer.ts +33 -0
  90. package/src/adapters/file-inflight-store.ts +115 -0
  91. package/src/adapters/file-modes.ts +35 -0
  92. package/src/adapters/hub-actions.ts +82 -0
  93. package/src/adapters/hub-credentials.ts +95 -0
  94. package/src/adapters/jsonl-work-log.ts +67 -0
  95. package/src/adapters/kankaku-command.ts +717 -0
  96. package/src/adapters/kankaku-dir.ts +102 -0
  97. package/src/adapters/lazy-file-inflight-store.ts +43 -0
  98. package/src/adapters/lazy-jsonl-work-log.ts +39 -0
  99. package/src/adapters/machine-process-registry.ts +256 -0
  100. package/src/adapters/panel/kankaku-panel.ts +419 -0
  101. package/src/adapters/panel/panel-items.ts +87 -0
  102. package/src/adapters/panel/panel-lines.ts +13 -0
  103. package/src/adapters/panel/panel-theme.ts +32 -0
  104. package/src/adapters/panel/screens/about.ts +69 -0
  105. package/src/adapters/panel/screens/doctor.ts +89 -0
  106. package/src/adapters/panel/screens/export.ts +123 -0
  107. package/src/adapters/panel/screens/report.ts +143 -0
  108. package/src/adapters/panel/screens/sync.ts +136 -0
  109. package/src/adapters/panel/screens/target.ts +384 -0
  110. package/src/adapters/pi-tracker.ts +753 -0
  111. package/src/adapters/pocketbase-catalog.ts +89 -0
  112. package/src/adapters/pocketbase-client.ts +197 -0
  113. package/src/adapters/pocketbase-sink.ts +236 -0
  114. package/src/adapters/process-identity-memo.ts +102 -0
  115. package/src/adapters/process-identity.ts +162 -0
  116. package/src/adapters/project-config.ts +116 -0
  117. package/src/adapters/report-data.ts +13 -0
  118. package/src/adapters/report-views.ts +98 -0
  119. package/src/adapters/report.ts +335 -0
  120. package/src/adapters/session-client.ts +116 -0
  121. package/src/adapters/session-dir.ts +28 -0
  122. package/src/adapters/session-target.ts +431 -0
  123. package/src/adapters/status-bar.ts +86 -0
  124. package/src/adapters/subagent-startup.ts +66 -0
  125. package/src/adapters/sync-runner.ts +340 -0
  126. package/src/adapters/sync-state-store.ts +227 -0
  127. package/src/adapters/target-picker.ts +127 -0
  128. package/src/config.ts +536 -0
  129. package/src/domain/ancestry-match.ts +84 -0
  130. package/src/domain/client-label.ts +56 -0
  131. package/src/domain/day.ts +8 -0
  132. package/src/domain/export.ts +107 -0
  133. package/src/domain/hub-entry.ts +433 -0
  134. package/src/domain/index.ts +19 -0
  135. package/src/domain/intervals.ts +53 -0
  136. package/src/domain/panel-model.ts +270 -0
  137. package/src/domain/registry-health.ts +87 -0
  138. package/src/domain/segment-rule.ts +10 -0
  139. package/src/domain/subagent-profile.ts +495 -0
  140. package/src/domain/sync-plan.ts +266 -0
  141. package/src/domain/task-view.ts +526 -0
  142. package/src/domain/work-record.ts +320 -0
  143. package/src/domain/work-target.ts +234 -0
  144. package/src/domain/work-tracker.ts +485 -0
  145. package/src/extension.ts +346 -0
  146. package/src/hub/index.ts +25 -0
  147. package/src/ports/catalog.ts +33 -0
  148. package/src/ports/clock.ts +3 -0
  149. package/src/ports/index.ts +11 -0
  150. package/src/ports/inflight-store.ts +16 -0
  151. package/src/ports/process-registry.ts +75 -0
  152. package/src/ports/work-log.ts +15 -0
  153. package/src/ports/work-sink.ts +35 -0
@@ -0,0 +1,340 @@
1
+ /**
2
+ * Orchestrates one sync run: read every record, build tasks (never
3
+ * re-implementing the aggregation — `buildTasks` is the only place it
4
+ * lives), plan what needs pushing, push it, and persist the new state.
5
+ * Never throws to its caller: every failure mode is folded into the
6
+ * returned {@link SyncSummary}.
7
+ */
8
+
9
+ import { buildTasks } from "../domain/task-view.ts";
10
+ import { computeTaskContentHash, planSync, pruneHashes } from "../domain/sync-plan.ts";
11
+ import type { SyncState } from "../domain/sync-plan.ts";
12
+ import type { TaskView } from "../domain/task-view.ts";
13
+ import type { Clock } from "../ports/clock.ts";
14
+ import type { WorkLog } from "../ports/work-log.ts";
15
+ import type { WorkSink } from "../ports/work-sink.ts";
16
+ import type { SyncStateStore } from "./sync-state-store.ts";
17
+
18
+ export interface SyncSummary {
19
+ uploaded: number;
20
+ updated: number;
21
+ skipped: number;
22
+ failed: Array<{ id: string; reason: string }>;
23
+ /** Count of tasks routed to the unassigned client this run, grouped by their historical free-text label. */
24
+ unassigned: Record<string, number>;
25
+ syncedThrough: string | undefined;
26
+ durationMs: number;
27
+ /** Set when a network/timeout/5xx/auth failure stopped the run before every candidate task was attempted. */
28
+ error?: string;
29
+ /** `true` when another sync already holds the lock; nothing was attempted this run. */
30
+ locked?: boolean;
31
+ }
32
+
33
+ /**
34
+ * Which automatic trigger asked for this run, or `undefined` for a manual
35
+ * one (`/kankaku sync`, `sync all`, `backfill`) — see `runSync`'s
36
+ * short-circuit and throttle, which apply only to the automatic path.
37
+ * `session_shutdown` (pi awaits this handler — see `adapters/pi-tracker.ts`)
38
+ * is, like `session_start`, never throttled: only `agent_settled` is.
39
+ */
40
+ export type SyncTrigger = "session_start" | "agent_settled" | "session_shutdown";
41
+
42
+ export interface SyncRunnerDeps {
43
+ log: WorkLog;
44
+ sink: WorkSink;
45
+ stateStore: SyncStateStore;
46
+ clock: Clock;
47
+ /** The configured hub URL — a state file synced against a different one triggers a full sync. */
48
+ target: string;
49
+ windowHours?: number;
50
+ /**
51
+ * `KANKAKU_SYNC_MIN_INTERVAL_MINUTES`, already converted to ms. Only
52
+ * applies to the automatic path (`options.trigger` set). Defaults to 5
53
+ * minutes; `0` disables throttling.
54
+ */
55
+ minAutoIntervalMs?: number;
56
+ }
57
+
58
+ const NO_LABEL = "(no label)";
59
+ const DEFAULT_MIN_AUTO_INTERVAL_MS = 5 * 60 * 1000;
60
+
61
+ /** The later of two ISO timestamps, treating `undefined` as earlier than anything. */
62
+ function laterIso(a: string | undefined, b: string): string {
63
+ if (a === undefined) return b;
64
+ return Date.parse(b) > Date.parse(a) ? b : a;
65
+ }
66
+
67
+ function emptySummary(durationMs: number, syncedThrough: string | undefined): SyncSummary {
68
+ return { uploaded: 0, updated: 0, skipped: 0, failed: [], unassigned: {}, syncedThrough, durationMs };
69
+ }
70
+
71
+ /**
72
+ * Whether the automatic path's throttle should block this run right now.
73
+ * Only `agent_settled` — fired once per prompt, far more often than a
74
+ * session starts or ends — is ever throttled; `session_start` and
75
+ * `session_shutdown` always bypass it (a session boundary is a good time
76
+ * to catch up regardless of how recently the last automatic run happened,
77
+ * and the shutdown one is awaited and time-bounded on its own — see
78
+ * `adapters/pi-tracker.ts`). `undefined`/non-finite `lastRunAt` (never
79
+ * run, or a malformed on-disk value) never throttles either — there is
80
+ * nothing to measure the interval against.
81
+ */
82
+ function isThrottled(state: SyncState | undefined, trigger: SyncTrigger, now: number, minIntervalMs: number): boolean {
83
+ if (trigger !== "agent_settled") return false;
84
+ if (minIntervalMs <= 0) return false;
85
+ const lastRunAt = state?.lastRunAt;
86
+ if (!Number.isFinite(lastRunAt)) return false;
87
+ if (now - (lastRunAt as number) >= minIntervalMs) return false;
88
+ return true;
89
+ }
90
+
91
+ /**
92
+ * Run one sync pass. Acquires the cross-process lock for the whole run
93
+ * (never held across `await` boundaries outside this function) so two pi
94
+ * processes never race on the same `sync-state.json`.
95
+ *
96
+ * When `options.trigger` is set (the automatic `session_start`/
97
+ * `agent_settled`/`session_shutdown` path, as opposed to a manual
98
+ * `/kankaku sync`), two cheap gates run before any `WorkLog.readAll()` or
99
+ * network call: (a) if the log's `version()` is unchanged since the last
100
+ * successful sync and that sync did not error, skip entirely, for every
101
+ * trigger; otherwise (b) throttle to at most one real attempt per
102
+ * `minAutoIntervalMs`, but only for `agent_settled` — fired once per
103
+ * prompt, so `version()` almost always differs right after it appended a
104
+ * record. `session_start` and `session_shutdown` never throttle (see
105
+ * `isThrottled`). Neither gate ever applies to a manual sync.
106
+ */
107
+ export async function runSync(deps: SyncRunnerDeps, options: { full?: boolean; trigger?: SyncTrigger } = {}): Promise<SyncSummary> {
108
+ const startedAt = deps.clock.now();
109
+
110
+ if (options.trigger !== undefined) {
111
+ // Gated like every other read below: a state written for another hub
112
+ // says nothing about THIS hub, so neither its `logVersion` (which
113
+ // would otherwise skip the new hub's very first run until the next
114
+ // record is appended) nor its `lastRunAt` throttle may apply.
115
+ const peek = stateForTarget(deps, deps.stateStore.read());
116
+ const currentVersion = deps.log.version?.();
117
+ const versionUnchanged = currentVersion !== undefined && peek?.logVersion === currentVersion;
118
+ if (versionUnchanged && peek?.lastError === undefined) {
119
+ return emptySummary(deps.clock.now() - startedAt, peek?.syncedThrough);
120
+ }
121
+
122
+ const minIntervalMs = deps.minAutoIntervalMs ?? DEFAULT_MIN_AUTO_INTERVAL_MS;
123
+ if (isThrottled(peek, options.trigger, deps.clock.now(), minIntervalMs)) {
124
+ return emptySummary(deps.clock.now() - startedAt, peek?.syncedThrough);
125
+ }
126
+ }
127
+
128
+ let lockAcquired = false;
129
+ try {
130
+ lockAcquired = deps.stateStore.tryLock();
131
+ if (!lockAcquired) {
132
+ const state = stateForTarget(deps, deps.stateStore.read());
133
+ return { ...emptySummary(deps.clock.now() - startedAt, state?.syncedThrough), locked: true };
134
+ }
135
+
136
+ const state = deps.stateStore.read();
137
+ // A state from another hub is ignored wholesale, on EVERY path below
138
+ // (the summary, the error path, and the successful merge): its
139
+ // watermark and its hashes describe rows the configured hub does not
140
+ // have (mirrors `domain/sync-plan.ts#planSync`'s gate). `planSync`
141
+ // still receives the raw `state`, since it applies that same gate
142
+ // itself and its `target` comparison is what makes the run a full one.
143
+ const priorState = stateForTarget(deps, state);
144
+ // Captured once, here, and persisted as-is below: this is the version
145
+ // the tasks below were actually built from, not whatever the log might
146
+ // become by the time an awaited push finishes.
147
+ const logVersionAtRead = deps.log.version?.();
148
+ const tasks = buildTasks(deps.log.readAll());
149
+ const plan = planSync(tasks, state, { target: deps.target, ...(deps.windowHours !== undefined ? { windowHours: deps.windowHours } : {}), ...(options.full !== undefined ? { full: options.full } : {}) });
150
+
151
+ let results: Awaited<ReturnType<WorkSink["push"]>>;
152
+ try {
153
+ results = await deps.sink.push(plan.toSync);
154
+ } catch (error) {
155
+ // WorkSink implementations are expected never to throw, but this
156
+ // runner must hold that guarantee even if one does.
157
+ const message = error instanceof Error ? error.message : String(error);
158
+ const summary = emptySummary(deps.clock.now() - startedAt, priorState?.syncedThrough);
159
+ summary.skipped = plan.unchangedCount;
160
+ summary.error = message;
161
+ // `priorState`, never `state`: a throwing sink on the first run
162
+ // against a NEW hub must not carry the old hub's hashes and
163
+ // watermark over under the new target, or the next run would
164
+ // skip every task as "unchanged" (the exact bug 0.8.1 fixed on the
165
+ // successful path).
166
+ persistError(deps, priorState, message, logVersionAtRead);
167
+ return summary;
168
+ }
169
+
170
+ const byId = new Map(plan.toSync.map((task) => [task.id, task]));
171
+ // Both are keyed by content that ultimately traces back to free-text
172
+ // worklog/legacy-client data (task ids, legacy client labels): built in
173
+ // a `Map` and emitted via `Object.fromEntries` below (never
174
+ // `newHashes[task.id] = ...` on a plain object), so a value like
175
+ // `__proto__` or `constructor` becomes a normal own entry instead of
176
+ // silently colliding with an inherited `Object.prototype` property.
177
+ const newHashes = new Map<string, string>();
178
+ const failed: Array<{ id: string; reason: string }> = [];
179
+ const unassigned = new Map<string, number>();
180
+ let uploaded = 0;
181
+ let updated = 0;
182
+ let syncedThrough = priorState?.syncedThrough;
183
+ let stopError: string | undefined;
184
+ // Whether at least one task was actually resolved (pushed or recorded
185
+ // as failed) this run — as opposed to the run stopping on its very
186
+ // first attempt. Guards `target` below: a run against a new/unreachable
187
+ // target that resolves nothing must not overwrite the state's `target`,
188
+ // or a later sync against the *real* target would wrongly see it as
189
+ // unchanged and skip the full re-evaluation it needs.
190
+ let progressed = false;
191
+
192
+ for (const result of results) {
193
+ const task = byId.get(result.taskId);
194
+ if (!task) continue; // defensive: a WorkSink implementation misbehaving should not crash the runner.
195
+
196
+ if (result.outcome.kind === "created" || result.outcome.kind === "updated") {
197
+ if (result.outcome.kind === "created") uploaded += 1;
198
+ else updated += 1;
199
+ newHashes.set(task.id, computeTaskContentHash(task));
200
+ syncedThrough = laterIso(syncedThrough, task.endedAt);
201
+ progressed = true;
202
+ if (result.outcome.unassigned) {
203
+ const label = result.outcome.legacyLabel || NO_LABEL;
204
+ unassigned.set(label, (unassigned.get(label) ?? 0) + 1);
205
+ }
206
+ } else if (result.outcome.kind === "failed") {
207
+ // Recorded and skipped, not retried forever: stamp its hash too so
208
+ // an unchanged, permanently-invalid task is not resent every run.
209
+ failed.push({ id: task.id, reason: result.outcome.reason });
210
+ newHashes.set(task.id, computeTaskContentHash(task));
211
+ syncedThrough = laterIso(syncedThrough, task.endedAt);
212
+ progressed = true;
213
+ } else {
214
+ // "error": a network/timeout/5xx/auth failure. Stop here — nothing
215
+ // after this point in the results is considered resolved. The
216
+ // results follow `plan.toSync`'s order (new work oldest-first,
217
+ // then corrections newest-first), and `syncedThrough` is a max
218
+ // over what WAS resolved, so stopping early can only leave it
219
+ // lower, never advance it past an unresolved task.
220
+ stopError = result.outcome.reason;
221
+ break;
222
+ }
223
+ }
224
+
225
+ const mergedHashes = { ...(priorState?.hashes ?? {}), ...Object.fromEntries(newHashes) };
226
+ // G2: no longer window-bound — see `domain/sync-plan.ts#pruneHashes`'s
227
+ // doc comment. `tasks` here is every task `buildTasks` currently knows
228
+ // about (the full `readAll()`, not just this run's eligible/window
229
+ // subset), so a hash is only ever dropped for a task id that has
230
+ // genuinely vanished, never merely because it is old.
231
+ const prunedHashes = pruneHashes(mergedHashes, tasks);
232
+ // Only adopt deps.target as the persisted target once this run has
233
+ // actually resolved something against it; otherwise keep whatever
234
+ // target (if any) the previous state was synced against.
235
+ const persistedTarget = progressed || state === undefined ? deps.target : state.target;
236
+
237
+ deps.stateStore.write({
238
+ target: persistedTarget,
239
+ ...(syncedThrough !== undefined ? { syncedThrough } : {}),
240
+ hashes: prunedHashes,
241
+ ...(stopError !== undefined ? { lastError: { message: stopError, at: new Date(deps.clock.now()).toISOString() } } : {}),
242
+ ...(logVersionAtRead !== undefined ? { logVersion: logVersionAtRead } : {}),
243
+ lastRunAt: deps.clock.now(),
244
+ });
245
+
246
+ return {
247
+ uploaded,
248
+ updated,
249
+ skipped: plan.unchangedCount,
250
+ failed,
251
+ unassigned: Object.fromEntries(unassigned),
252
+ syncedThrough,
253
+ durationMs: deps.clock.now() - startedAt,
254
+ ...(stopError !== undefined ? { error: stopError } : {}),
255
+ };
256
+ } finally {
257
+ if (lockAcquired) deps.stateStore.unlock();
258
+ }
259
+ }
260
+
261
+ /** `state` when it was written against `deps.target`; `undefined` (as if there were no state at all) when it belongs to another hub. */
262
+ function stateForTarget(deps: SyncRunnerDeps, state: ReturnType<SyncStateStore["read"]>): ReturnType<SyncStateStore["read"]> {
263
+ return state !== undefined && state.target === deps.target ? state : undefined;
264
+ }
265
+
266
+ /** Persist a failed run. `state` must already be gated by {@link stateForTarget}: what it carries is re-written under `deps.target`. */
267
+ function persistError(deps: SyncRunnerDeps, state: ReturnType<SyncStateStore["read"]>, message: string, logVersionAtRead: string | number | undefined): void {
268
+ deps.stateStore.write({
269
+ target: deps.target,
270
+ ...(state?.syncedThrough !== undefined ? { syncedThrough: state.syncedThrough } : {}),
271
+ hashes: state?.hashes ?? {},
272
+ lastError: { message, at: new Date(deps.clock.now()).toISOString() },
273
+ ...(logVersionAtRead !== undefined ? { logVersion: logVersionAtRead } : {}),
274
+ lastRunAt: deps.clock.now(),
275
+ });
276
+ }
277
+
278
+ /**
279
+ * Number of tasks pending a sync right now — computed locally, no network.
280
+ *
281
+ * @deprecated Superseded by {@link computeSyncStatus}, which returns the same
282
+ * `pending` count together with `state` and `staleOutsideWindow`. Nothing in
283
+ * kankaku calls this any more; it stays only because it is part of the
284
+ * published `kankaku-pi/hub` surface. It will be removed in the next minor.
285
+ */
286
+ export function pendingCount(tasks: TaskView[], state: ReturnType<SyncStateStore["read"]>, target: string, windowHours?: number): number {
287
+ const plan = planSync(tasks, state, { target, ...(windowHours !== undefined ? { windowHours } : {}) });
288
+ return plan.toSync.length + plan.correctionsDeferred;
289
+ }
290
+
291
+ /**
292
+ * {@link computeSyncStatus}'s return shape, exported so a caller (e.g.
293
+ * `hub-actions.ts#buildSyncStatusLines`, or a future standalone TUI reusing
294
+ * `kankaku-pi/hub`) can depend on this type without importing anything that
295
+ * touches `@earendil-works/*` — see AGENTS.md "Code conventions".
296
+ */
297
+ export interface SyncStatusSnapshot {
298
+ state: SyncState | undefined;
299
+ pending: number;
300
+ staleOutsideWindow: number;
301
+ }
302
+
303
+ /**
304
+ * `/kankaku sync status`: the persisted state, a locally-computed pending
305
+ * count, and (R3) how many tasks changed since their last sync but fall
306
+ * outside this run's revisit window — a `sync all` needed to pick them up
307
+ * (see `domain/sync-plan.ts#SyncPlan.staleOutsideWindow`, and README "Hub
308
+ * (PocketBase)" > "Sync" > "Limitations"). No network.
309
+ */
310
+ export function computeSyncStatus(log: WorkLog, stateStore: SyncStateStore, target: string, windowHours?: number): SyncStatusSnapshot {
311
+ const state = stateStore.read();
312
+ const tasks = buildTasks(log.readAll());
313
+ const plan = planSync(tasks, state, { target, ...(windowHours !== undefined ? { windowHours } : {}) });
314
+ // Deferred corrections are pending too: the cap only spreads them over runs.
315
+ return { state, pending: plan.toSync.length + plan.correctionsDeferred, staleOutsideWindow: plan.staleOutsideWindow.length };
316
+ }
317
+
318
+ /**
319
+ * Wrap an async function so concurrent callers share one in-flight call
320
+ * instead of starting a new one each — kankaku's single-flight guard for
321
+ * sync: `/kankaku sync`, the `session_start` auto-sync and the
322
+ * `agent_settled` auto-sync all go through the same wrapped function, so
323
+ * only one sync is ever running at a time within this process. (The
324
+ * cross-process case is covered separately by `SyncStateStore`'s lock
325
+ * file.) A caller that arrives while one is in flight joins its result
326
+ * rather than queuing a fresh run — the next trigger (the next
327
+ * `session_start` or `agent_settled`) will pick up anything missed, since
328
+ * every sync also revisits the trailing window.
329
+ */
330
+ export function singleFlight<Args extends unknown[], T>(fn: (...args: Args) => Promise<T>): (...args: Args) => Promise<T> {
331
+ let inFlight: Promise<T> | undefined;
332
+ return (...args: Args): Promise<T> => {
333
+ if (!inFlight) {
334
+ inFlight = fn(...args).finally(() => {
335
+ inFlight = undefined;
336
+ });
337
+ }
338
+ return inFlight;
339
+ };
340
+ }
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Disk-backed store for `<KANKAKU_DIR>/sync-state.json`, plus a simple
3
+ * cross-process lock so two pi processes (e.g. an orchestrator's
4
+ * auto-sync and a manual `/kankaku sync` in another terminal) never sync
5
+ * the same directory concurrently. Mirrors `file-inflight-store.ts`'s
6
+ * atomic-write and liveness-probe conventions.
7
+ */
8
+
9
+ import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
10
+ import { join } from "node:path";
11
+ import type { SyncState } from "../domain/sync-plan.ts";
12
+
13
+ const STATE_FILE_NAME = "sync-state.json";
14
+ const LOCK_FILE_NAME = "sync.lock";
15
+ /** A lock older than this is considered abandoned even if its owning pid still (coincidentally) exists. */
16
+ const STALE_LOCK_MS = 5 * 60 * 1000;
17
+
18
+ interface LockFile {
19
+ pid: number;
20
+ at: number;
21
+ }
22
+
23
+ /** The subset of `node:fs` the lock's acquire/recover path needs, injectable so tests can simulate cross-process interleaving deterministically. */
24
+ export interface SyncStateStoreFsOps {
25
+ existsSync: typeof existsSync;
26
+ readFileSync: typeof readFileSync;
27
+ writeFileSync: typeof writeFileSync;
28
+ renameSync: typeof renameSync;
29
+ unlinkSync: typeof unlinkSync;
30
+ openSync: typeof openSync;
31
+ closeSync: typeof closeSync;
32
+ }
33
+
34
+ const defaultFsOps: SyncStateStoreFsOps = { existsSync, readFileSync, writeFileSync, renameSync, unlinkSync, openSync, closeSync };
35
+
36
+ function isEnoent(error: unknown): boolean {
37
+ return (error as NodeJS.ErrnoException)?.code === "ENOENT";
38
+ }
39
+
40
+ function isEexist(error: unknown): boolean {
41
+ return (error as NodeJS.ErrnoException)?.code === "EEXIST";
42
+ }
43
+
44
+ function isSyncState(value: unknown): value is SyncState {
45
+ if (!value || typeof value !== "object") return false;
46
+ const record = value as Record<string, unknown>;
47
+ return (
48
+ typeof record["target"] === "string" &&
49
+ typeof record["hashes"] === "object" &&
50
+ record["hashes"] !== null &&
51
+ (record["syncedThrough"] === undefined || typeof record["syncedThrough"] === "string")
52
+ );
53
+ }
54
+
55
+ function isLockFile(value: unknown): value is LockFile {
56
+ if (!value || typeof value !== "object") return false;
57
+ const record = value as Record<string, unknown>;
58
+ return typeof record["pid"] === "number" && typeof record["at"] === "number";
59
+ }
60
+
61
+ function atomicWrite(filePath: string, content: string): void {
62
+ const tmp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
63
+ writeFileSync(tmp, content);
64
+ renameSync(tmp, filePath);
65
+ }
66
+
67
+ function safeUnlink(fs: Pick<SyncStateStoreFsOps, "unlinkSync">, filePath: string): void {
68
+ try {
69
+ fs.unlinkSync(filePath);
70
+ } catch {
71
+ // Best effort: already removed, or never existed.
72
+ }
73
+ }
74
+
75
+ export interface SyncStateStoreDeps {
76
+ dir: string;
77
+ pid: number;
78
+ /** Whether a pid is still alive. Defaults to the same signal-probe used elsewhere. */
79
+ isAlive?: (pid: number) => boolean;
80
+ /** Injectable for tests. Defaults to `Date.now`. */
81
+ now?: () => number;
82
+ /** Injectable `node:fs` primitives for the lock's acquire/recover path. Defaults to the real ones. */
83
+ fs?: SyncStateStoreFsOps;
84
+ }
85
+
86
+ /**
87
+ * `read()`/`write()` tolerate a missing or malformed file (return
88
+ * `undefined` / overwrite, respectively) since this state is disposable —
89
+ * losing it only costs a full re-evaluation on the next sync, never data.
90
+ * `tryLock()`/`unlock()` implement a simple pid+timestamp lock file, stale
91
+ * after {@link STALE_LOCK_MS}.
92
+ */
93
+ export class SyncStateStore {
94
+ private readonly deps: Required<SyncStateStoreDeps>;
95
+
96
+ constructor(deps: SyncStateStoreDeps) {
97
+ this.deps = {
98
+ dir: deps.dir,
99
+ pid: deps.pid,
100
+ isAlive: deps.isAlive ?? defaultIsAlive,
101
+ now: deps.now ?? (() => Date.now()),
102
+ fs: deps.fs ?? defaultFsOps,
103
+ };
104
+ }
105
+
106
+ private get statePath(): string {
107
+ return join(this.deps.dir, STATE_FILE_NAME);
108
+ }
109
+
110
+ private get lockPath(): string {
111
+ return join(this.deps.dir, LOCK_FILE_NAME);
112
+ }
113
+
114
+ read(): SyncState | undefined {
115
+ try {
116
+ if (!existsSync(this.statePath)) return undefined;
117
+ const parsed: unknown = JSON.parse(readFileSync(this.statePath, "utf8"));
118
+ return isSyncState(parsed) ? parsed : undefined;
119
+ } catch {
120
+ return undefined;
121
+ }
122
+ }
123
+
124
+ write(state: SyncState): void {
125
+ mkdirSync(this.deps.dir, { recursive: true });
126
+ atomicWrite(this.statePath, JSON.stringify(state));
127
+ }
128
+
129
+ /**
130
+ * Try to acquire the cross-process sync lock. Acquisition itself is
131
+ * atomic: it always goes through an exclusive create ({@link acquireFresh},
132
+ * `open` with the `wx` flag), never a read-then-write, so two processes
133
+ * racing to create the lock file can never both succeed. Returns `true`
134
+ * (and takes ownership) when there is no lock file, this process already
135
+ * owns it (re-entrant), or the existing one is stale (its pid is no
136
+ * longer alive, or it is older than {@link STALE_LOCK_MS}) and this
137
+ * process wins the race to recover it; `false` when a live, fresh lock is
138
+ * held by another process, or this process loses a stale-lock recovery
139
+ * race to another one.
140
+ */
141
+ tryLock(): boolean {
142
+ mkdirSync(this.deps.dir, { recursive: true });
143
+
144
+ if (this.acquireFresh()) return true;
145
+
146
+ const existing = this.readLock();
147
+ if (!existing) {
148
+ // Raced with a release between our failed create and this read; the
149
+ // slot may be free again now. One more attempt, then give up rather
150
+ // than looping forever.
151
+ return this.acquireFresh();
152
+ }
153
+
154
+ if (existing.pid === this.deps.pid) return true; // re-entrant: we already own it.
155
+
156
+ const age = this.deps.now() - existing.at;
157
+ const stale = age > STALE_LOCK_MS || !this.deps.isAlive(existing.pid);
158
+ if (!stale) return false; // live, fresh lock held by someone else.
159
+
160
+ // Stale-lock recovery, made race-safe: rename the stale file to a
161
+ // unique tombstone name first. `rename` is atomic, so only one racer's
162
+ // call can succeed; every loser gets ENOENT and backs off instead of
163
+ // deleting (or overwriting) a lock it never proved was still stale.
164
+ const tombstone = `${this.lockPath}.stale.${this.deps.pid}.${this.deps.now()}.tmp`;
165
+ try {
166
+ this.deps.fs.renameSync(this.lockPath, tombstone);
167
+ } catch (error) {
168
+ if (isEnoent(error)) return false; // lost the recovery race; back off.
169
+ throw error;
170
+ }
171
+ safeUnlink(this.deps.fs, tombstone);
172
+
173
+ return this.acquireFresh(); // false here means a third racer won it first.
174
+ }
175
+
176
+ /** Create the lock file exclusively (`wx`): fails with EEXIST when another lock already exists, never silently overwrites one. Assumes `this.deps.dir` already exists (`tryLock` ensures it once up front). */
177
+ private acquireFresh(): boolean {
178
+ let fd: number;
179
+ try {
180
+ fd = this.deps.fs.openSync(this.lockPath, "wx");
181
+ } catch (error) {
182
+ if (isEexist(error)) return false;
183
+ throw error;
184
+ }
185
+ try {
186
+ this.deps.fs.writeFileSync(fd, JSON.stringify({ pid: this.deps.pid, at: this.deps.now() }));
187
+ } catch (error) {
188
+ try {
189
+ this.deps.fs.closeSync(fd);
190
+ } catch {
191
+ // Best effort: still try to clean up the partially written file below.
192
+ }
193
+ safeUnlink(this.deps.fs, this.lockPath);
194
+ throw error;
195
+ }
196
+ this.deps.fs.closeSync(fd);
197
+ return true;
198
+ }
199
+
200
+ /** Release the lock, but only if this process still owns it (never clobber someone else's fresher lock). */
201
+ unlock(): void {
202
+ const existing = this.readLock();
203
+ if (existing && existing.pid === this.deps.pid) {
204
+ safeUnlink(this.deps.fs, this.lockPath);
205
+ }
206
+ }
207
+
208
+ private readLock(): LockFile | undefined {
209
+ try {
210
+ if (!this.deps.fs.existsSync(this.lockPath)) return undefined;
211
+ const parsed: unknown = JSON.parse(this.deps.fs.readFileSync(this.lockPath, "utf8"));
212
+ return isLockFile(parsed) ? parsed : undefined;
213
+ } catch {
214
+ return undefined;
215
+ }
216
+ }
217
+ }
218
+
219
+ /** Default `isAlive`: probe with signal 0 — mirrors `pi-tracker.ts`'s default. */
220
+ function defaultIsAlive(pid: number): boolean {
221
+ try {
222
+ process.kill(pid, 0);
223
+ return true;
224
+ } catch (error) {
225
+ return (error as NodeJS.ErrnoException).code === "EPERM";
226
+ }
227
+ }