pi-crew 0.10.1 → 0.10.3
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/CHANGELOG.md +347 -0
- package/NOTICE.md +21 -0
- package/README.md +44 -2
- package/agents/executor.md +1 -1
- package/agents/test-engineer.md +1 -1
- package/dist/index.mjs +2555 -932
- package/package.json +2 -1
- package/schema.json +503 -94
- package/skills/real-test-pi-crew/REPORT-TEMPLATE.md +6 -2
- package/skills/real-test-pi-crew/SKILL.md +278 -79
- package/src/config/config-merge.ts +11 -1
- package/src/config/config-validation.ts +47 -2
- package/src/config/config.ts +28 -6
- package/src/config/defaults.ts +43 -11
- package/src/config/env-vars.ts +27 -2
- package/src/config/role-tools.ts +4 -2
- package/src/config/types.ts +55 -1
- package/src/extension/crew-cleanup.ts +13 -0
- package/src/extension/crew-vibes/footer.ts +19 -0
- package/src/extension/crew-vibes/index.ts +11 -1
- package/src/extension/register.ts +8 -0
- package/src/extension/registration/command-registration.ts +1 -0
- package/src/extension/registration/commands/run.ts +15 -1
- package/src/extension/registration/commands/shared.ts +8 -0
- package/src/extension/registration/foreground-run-controller.ts +10 -2
- package/src/extension/registration/lifecycle-handlers.ts +92 -11
- package/src/extension/registration/runtime-cleanup.ts +23 -5
- package/src/extension/registration/team-tool.ts +58 -6
- package/src/extension/registration/ui.ts +5 -4
- package/src/extension/team-tool/doctor.ts +364 -7
- package/src/extension/team-tool/handle-settings.ts +19 -0
- package/src/extension/team-tool/inspect.ts +10 -2
- package/src/extension/team-tool/run-deadline.ts +20 -3
- package/src/extension/team-tool/run.ts +26 -2
- package/src/extension/team-tool/status.ts +7 -0
- package/src/extension/team-tool.ts +35 -2
- package/src/hooks/registry.ts +59 -56
- package/src/prompt/inbox-poll.ts +90 -0
- package/src/prompt/message-tool.ts +166 -0
- package/src/prompt/prompt-runtime.ts +201 -18
- package/src/prompt/surface-worker.ts +720 -0
- package/src/prompt/worker-events-channel.ts +49 -3
- package/src/runtime/async-runner.ts +29 -1
- package/src/runtime/background-runner.ts +13 -7
- package/src/runtime/broker/broker-issuer.ts +27 -2
- package/src/runtime/broker/crew-broker-tokens.ts +56 -4
- package/src/runtime/broker/crew-broker.ts +261 -41
- package/src/runtime/child-pi/child-pi-constants.ts +8 -0
- package/src/runtime/child-pi/child-pi-spawn.ts +23 -9
- package/src/runtime/child-pi/child-pi-streams.ts +30 -2
- package/src/runtime/child-pi/child-pi.ts +353 -5
- package/src/runtime/crew-agent-records.ts +13 -1
- package/src/runtime/detached-run-results.ts +90 -0
- package/src/runtime/dispatch-batch.ts +12 -1
- package/src/runtime/event-log-tail-source.ts +374 -0
- package/src/runtime/finalize-run.ts +4 -0
- package/src/runtime/goal-workflow/adaptive-plan.ts +30 -3
- package/src/runtime/goal-workflow/dynamic-workflow-runner.ts +5 -1
- package/src/runtime/live-session/live-agent-manager.ts +34 -1
- package/src/runtime/live-session/live-control-realtime.ts +10 -0
- package/src/runtime/live-session/live-session-runtime.ts +47 -27
- package/src/runtime/manifest-cache.ts +128 -17
- package/src/runtime/merge-gate.ts +25 -9
- package/src/runtime/model/model-fallback.ts +7 -3
- package/src/runtime/model/pi-args.ts +54 -65
- package/src/runtime/output/sidechain-output.ts +61 -6
- package/src/runtime/process/proc-stat.ts +46 -0
- package/src/runtime/process/zombie-scanner.ts +32 -19
- package/src/runtime/process-status.ts +16 -1
- package/src/runtime/recovery/crash-recovery.ts +16 -0
- package/src/runtime/run-tracker.ts +77 -10
- package/src/runtime/spawn-policy.ts +27 -41
- package/src/runtime/surface/degrade.ts +776 -0
- package/src/runtime/surface/herdr-provider.ts +546 -0
- package/src/runtime/surface/launch-script.ts +172 -0
- package/src/runtime/surface/resolve-surface.ts +274 -0
- package/src/runtime/surface/surface-provider.ts +129 -0
- package/src/runtime/surface/surface-spawn.ts +475 -0
- package/src/runtime/surface/tmux-provider.ts +400 -0
- package/src/runtime/task-runner/child-executor.ts +47 -0
- package/src/runtime/task-runner/post-execution.ts +57 -2
- package/src/runtime/task-runner/prompt-builder.ts +1 -0
- package/src/runtime/task-runner/retrieval-orchestrator.ts +191 -56
- package/src/runtime/task-runner/state-helpers.ts +54 -30
- package/src/runtime/task-runner.ts +4 -2
- package/src/runtime/team-runner.ts +104 -2
- package/src/schema/config-schema.ts +25 -1
- package/src/state/atomic-write.ts +219 -40
- package/src/state/coordination/locks.ts +7 -5
- package/src/state/coordination/mailbox.ts +56 -10
- package/src/state/event-log/cursor.ts +413 -23
- package/src/state/event-log/event-log.ts +120 -113
- package/src/state/event-log/sequence-cache.ts +21 -3
- package/src/state/stores/plan-store.ts +1 -1
- package/src/state/stores/state-store.ts +171 -9
- package/src/state/types.ts +53 -0
- package/src/ui/dock-footer.ts +49 -0
- package/src/ui/inline-panel/agent-pane.ts +378 -0
- package/src/ui/inline-panel/agent-transcript.ts +338 -0
- package/src/ui/inline-panel/agent-view-overlay.ts +225 -0
- package/src/ui/inline-panel/crew-editor.ts +192 -0
- package/src/ui/inline-panel/index.ts +290 -0
- package/src/ui/inline-panel/panel-rows.ts +37 -0
- package/src/ui/inline-panel/panel-selection.ts +157 -0
- package/src/ui/inline-panel/panel-store.ts +111 -0
- package/src/ui/inline-panel/view-session-store.ts +36 -0
- package/src/ui/pi-ui-compat.ts +9 -0
- package/src/ui/render-diff.ts +16 -8
- package/src/ui/run-dashboard.ts +87 -42
- package/src/ui/run-event-bus.ts +10 -1
- package/src/ui/run-snapshot-cache.ts +83 -35
- package/src/ui/transcript-cache.ts +101 -13
- package/src/ui/transcript-viewer.ts +92 -24
- package/src/ui/widget/index.ts +203 -25
- package/src/ui/widget/task-list.ts +198 -0
- package/src/ui/widget/widget-formatters.ts +240 -4
- package/src/ui/widget/widget-renderer.ts +234 -37
- package/src/ui/widget/widget-types.ts +11 -0
- package/src/utils/child-process-shield.ts +106 -0
- package/src/utils/redaction.ts +7 -0
- package/src/utils/safe-abort.ts +45 -0
- package/src/utils/visual.ts +43 -0
- package/src/workflows/discover-workflows.ts +1 -0
- package/src/workflows/workflow-config.ts +7 -0
- package/src/worktree/worktree-manager.ts +65 -4
- package/workflows/default.workflow.md +36 -26
- package/workflows/strict-fast-fix.workflow.md +26 -0
|
@@ -7,16 +7,13 @@ import { emitFromTeamEvent } from "../../ui/run-event-bus.ts";
|
|
|
7
7
|
import { logInternalError } from "../../utils/internal-error.ts";
|
|
8
8
|
import { redactSecrets } from "../../utils/redaction.ts";
|
|
9
9
|
import { sleep, sleepSync } from "../../utils/sleep.ts";
|
|
10
|
-
import { atomicWriteFile } from "../atomic-write.ts";
|
|
11
10
|
import { applyCompactionUnlocked, needsRotation, prepareCompaction, rotateEventLogUnlocked } from "./event-log-rotation.ts";
|
|
12
11
|
import {
|
|
13
12
|
advanceSequenceCounter,
|
|
14
|
-
evictOldestSequenceCacheEntries,
|
|
15
|
-
MAX_SEQUENCE_CACHE_ENTRIES,
|
|
16
13
|
persistSequenceMonotonic,
|
|
14
|
+
reservedSequenceEnd,
|
|
17
15
|
reserveSequence,
|
|
18
16
|
reserveSequenceUnderLockAsync,
|
|
19
|
-
sequenceCache,
|
|
20
17
|
} from "./sequence-cache.ts";
|
|
21
18
|
import { appendFileViaWorker, isWorkerAtomicWriterEnabled } from "./worker-atomic-writer.ts";
|
|
22
19
|
|
|
@@ -134,9 +131,18 @@ export function withEventLogLockSync<T>(eventsPath: string, fn: () => T, options
|
|
|
134
131
|
// (reduced from 120s) is appropriate.
|
|
135
132
|
fs.mkdirSync(lockDir);
|
|
136
133
|
try {
|
|
137
|
-
|
|
134
|
+
// PERF (2026-08-25): pid file is disposable + mtime-stale-detected, so the
|
|
135
|
+
// full atomicWriteFile (2 fsync) here was dead ceremony — the .alock async
|
|
136
|
+
// path already writes pid via "wx". Mirror it: O_EXCL open, plain write.
|
|
137
|
+
// The lock dir itself is the mutex.
|
|
138
|
+
const pidFd = fs.openSync(pidFile, "wx");
|
|
139
|
+
try {
|
|
140
|
+
fs.writeSync(pidFd, String(process.pid));
|
|
141
|
+
} finally {
|
|
142
|
+
fs.closeSync(pidFd);
|
|
143
|
+
}
|
|
138
144
|
} catch {
|
|
139
|
-
/* best-effort */
|
|
145
|
+
/* best-effort — e.g. EEXIST under a re-taken dir; stale pid is mtime-detected */
|
|
140
146
|
}
|
|
141
147
|
acquired = true;
|
|
142
148
|
break;
|
|
@@ -330,8 +336,18 @@ async function withEventLogLockAsync<T>(
|
|
|
330
336
|
try {
|
|
331
337
|
await fs.promises.mkdir(lockDir);
|
|
332
338
|
try {
|
|
333
|
-
//
|
|
334
|
-
|
|
339
|
+
// PERF (2026-08-24): "wx" (O_CREAT|O_EXCL) — fails rather than
|
|
340
|
+
// following a planted symlink, so no O_NOFOLLOW/temp/rename
|
|
341
|
+
// ceremony needed for this disposable, mtime-stale-detected
|
|
342
|
+
// 4-byte file. We own the lock dir (we just mkdir'd it), so
|
|
343
|
+
// EEXIST means a crashed holder's leftover under OUR fresh dir
|
|
344
|
+
// or an attack — either way, skip: the dir itself is the mutex.
|
|
345
|
+
const fh = await fs.promises.open(pidFile, "wx");
|
|
346
|
+
try {
|
|
347
|
+
await fh.write(String(process.pid));
|
|
348
|
+
} finally {
|
|
349
|
+
await fh.close();
|
|
350
|
+
}
|
|
335
351
|
} catch {
|
|
336
352
|
/* best-effort */
|
|
337
353
|
}
|
|
@@ -566,9 +582,6 @@ export async function appendEventAsync(eventsPath: string, event: AppendTeamEven
|
|
|
566
582
|
logInternalError("event-log.size-check", error, `eventsPath=${eventsPath}`);
|
|
567
583
|
}
|
|
568
584
|
|
|
569
|
-
// FIND-10: post-append stat captured from the same fd (non-worker path)
|
|
570
|
-
// for reuse in the cache update below, avoiding a redundant path stat.
|
|
571
|
-
let postAppendStat: fs.Stats | undefined;
|
|
572
585
|
if (!skippedDueToSize) {
|
|
573
586
|
const line = JSON.stringify(redactSecrets(fullEvent)) + "\n";
|
|
574
587
|
// Phase 1.5: when worker atomic writer is enabled, append via worker.
|
|
@@ -595,16 +608,6 @@ export async function appendEventAsync(eventsPath: string, event: AppendTeamEven
|
|
|
595
608
|
await fd.appendFile(line, "utf-8");
|
|
596
609
|
// P0-4 (F3a mirror): skip the data fsync for non-terminal events.
|
|
597
610
|
if (isTerminal) await fd.sync();
|
|
598
|
-
// FIND-10 R1 fix: the cache-optimization fd.stat() must NOT sit in the
|
|
599
|
-
// seq-durability critical path. If it threw (rare — fd invalidated),
|
|
600
|
-
// it would skip persistSequence below and reopen the seq-reuse
|
|
601
|
-
// window the fsync just closed. Guard it; fall back to undefined
|
|
602
|
-
// (the later cache-update takes a path stat instead).
|
|
603
|
-
try {
|
|
604
|
-
postAppendStat = await fd.stat();
|
|
605
|
-
} catch {
|
|
606
|
-
postAppendStat = undefined;
|
|
607
|
-
}
|
|
608
611
|
} finally {
|
|
609
612
|
await fd.close();
|
|
610
613
|
}
|
|
@@ -618,11 +621,7 @@ export async function appendEventAsync(eventsPath: string, event: AppendTeamEven
|
|
|
618
621
|
// can never roll the sidecar back either.
|
|
619
622
|
if (baseMetadata?.seq !== undefined) persistSequenceMonotonic(eventsPath, seq);
|
|
620
623
|
}
|
|
621
|
-
// FIND-10: track whether compaction happened after the append so the
|
|
622
|
-
// cache-update stat can safely reuse postAppendStat (file unchanged).
|
|
623
|
-
let compactedAfterAppend = false;
|
|
624
624
|
if (tickAppendCounter(eventsPath) && needsRotation(eventsPath)) {
|
|
625
|
-
compactedAfterAppend = true;
|
|
626
625
|
try {
|
|
627
626
|
const prepared = prepareCompaction(eventsPath);
|
|
628
627
|
if (prepared) applyCompactionUnlocked(eventsPath, prepared);
|
|
@@ -641,39 +640,13 @@ export async function appendEventAsync(eventsPath: string, event: AppendTeamEven
|
|
|
641
640
|
}
|
|
642
641
|
}
|
|
643
642
|
|
|
644
|
-
//
|
|
645
|
-
//
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
let statResult: fs.Stats | undefined;
|
|
652
|
-
if (postAppendStat && !compactedAfterAppend) {
|
|
653
|
-
statResult = postAppendStat;
|
|
654
|
-
} else {
|
|
655
|
-
try {
|
|
656
|
-
statResult = await fs.promises.stat(eventsPath).catch(() => undefined);
|
|
657
|
-
} catch {
|
|
658
|
-
/* file may not exist */
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
if (statResult) {
|
|
662
|
-
if (sequenceCache.size >= MAX_SEQUENCE_CACHE_ENTRIES) {
|
|
663
|
-
evictOldestSequenceCacheEntries();
|
|
664
|
-
}
|
|
665
|
-
sequenceCache.set(eventsPath, {
|
|
666
|
-
size: statResult.size,
|
|
667
|
-
mtimeMs: statResult.mtimeMs,
|
|
668
|
-
seq: finalSeq,
|
|
669
|
-
lastAccessMs: Date.now(),
|
|
670
|
-
});
|
|
671
|
-
}
|
|
672
|
-
// Note: persistSequence is NOT called here again - it was already called
|
|
673
|
-
// after the append to ensure the sidecar is current after the event is written.
|
|
674
|
-
} catch (error) {
|
|
675
|
-
logInternalError("event-log.persist-sequence", error, `eventsPath=${eventsPath}`);
|
|
676
|
-
}
|
|
643
|
+
// PERF (2026-08-24): the per-append sequenceCache upkeep that lived here
|
|
644
|
+
// (post-append stat + Map set + occasional O(n log n) evict sort) fed no
|
|
645
|
+
// hot reader — sequenceCache is read only by nextSequence, which is
|
|
646
|
+
// consulted solely by seeding/test helpers (see the ST-12 note in
|
|
647
|
+
// sequence-cache.ts); all three append paths allocate seqs via
|
|
648
|
+
// reserveSequence, which reads the .seq sidecar + seqCounters instead.
|
|
649
|
+
// nextSequence() re-seeds via its sidecar/scan fallback when called.
|
|
677
650
|
return fullEvent;
|
|
678
651
|
};
|
|
679
652
|
// C-01: Two-tier lock — asyncQueues (in-process serialize) →
|
|
@@ -726,11 +699,13 @@ export async function appendEventAsync(eventsPath: string, event: AppendTeamEven
|
|
|
726
699
|
/**
|
|
727
700
|
* Batch variant used by the buffered flush path. Computes metadata for each
|
|
728
701
|
* event, writes the whole batch in a single appendFileSync + fsync, persists
|
|
729
|
-
* the sequence sidecar once with the last seq
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
* the
|
|
733
|
-
*
|
|
702
|
+
* the sequence sidecar once with the last seq (no per-append sequenceCache
|
|
703
|
+
* upkeep — that cache no longer exists on the append paths; see the PERF
|
|
704
|
+
* 2026-08-24 note in appendEventAsync). Resolves each item with its finalized
|
|
705
|
+
* event (carrying the assigned seq). This collapses N fsyncs into 1 for the
|
|
706
|
+
* buffered write path, which is the entire point of buffering — the previous
|
|
707
|
+
* per-event fsync made buffer coalescing useless and added ~30ms/event on
|
|
708
|
+
* tmpfs.
|
|
734
709
|
*/
|
|
735
710
|
async function appendEventBatchInsideLock(eventsPath: string, queue: BufferedAppend[]): Promise<void> {
|
|
736
711
|
if (queue.length === 0) return;
|
|
@@ -738,12 +713,23 @@ async function appendEventBatchInsideLock(eventsPath: string, queue: BufferedApp
|
|
|
738
713
|
tickAppendCounter(eventsPath, queue.length);
|
|
739
714
|
fs.mkdirSync(path.dirname(eventsPath), { recursive: true });
|
|
740
715
|
|
|
716
|
+
// PERF (2026-08-24): one hoisted pre-append stat replaces the pre-flight
|
|
717
|
+
// existsSync+statSync pair below. The after-compaction re-checks further
|
|
718
|
+
// down keep their own fresh stats — compaction/rotation may have changed
|
|
719
|
+
// the file since preStat was taken, so they must not see a stale
|
|
720
|
+
// (pre-compaction) size.
|
|
721
|
+
let preStat: fs.Stats | undefined;
|
|
722
|
+
try {
|
|
723
|
+
preStat = fs.statSync(eventsPath);
|
|
724
|
+
} catch {
|
|
725
|
+
/* log absent — first append */
|
|
726
|
+
}
|
|
727
|
+
|
|
741
728
|
// Pre-flight size check (mirrors appendEventInsideLock). We do it once for
|
|
742
729
|
// the batch instead of once per event.
|
|
743
730
|
try {
|
|
744
|
-
if (
|
|
745
|
-
|
|
746
|
-
if (stat.size > MAX_EVENTS_BYTES) {
|
|
731
|
+
if (preStat) {
|
|
732
|
+
if (preStat.size > MAX_EVENTS_BYTES) {
|
|
747
733
|
try {
|
|
748
734
|
const prepared = prepareCompaction(eventsPath);
|
|
749
735
|
if (prepared) applyCompactionUnlocked(eventsPath, prepared);
|
|
@@ -774,8 +760,8 @@ async function appendEventBatchInsideLock(eventsPath: string, queue: BufferedApp
|
|
|
774
760
|
// then increment locally for each subsequent event in the batch. Calling
|
|
775
761
|
// nextSequence() per-event would re-read file stat/sidecar with no writes
|
|
776
762
|
// in between — every call would see the same file state and return the same
|
|
777
|
-
// seq, breaking the "unique monotonic seq" contract.
|
|
778
|
-
//
|
|
763
|
+
// seq, breaking the "unique monotonic seq" contract. persistSequenceMonotonic
|
|
764
|
+
// at the end refreshes the sidecar to the last assigned seq.
|
|
779
765
|
// B7: use reserveSequence for atomic seq assignment across all paths.
|
|
780
766
|
// R16-B1 (Phase 3.6): reserve the WHOLE batch range (count = queue.length)
|
|
781
767
|
// under the .seqlock in one acquire — the locally incremented nextSeq below
|
|
@@ -816,6 +802,13 @@ async function appendEventBatchInsideLock(eventsPath: string, queue: BufferedApp
|
|
|
816
802
|
finalized.push({ item, line: `${JSON.stringify(redactSecrets(fullEvent))}\n`, fullEvent });
|
|
817
803
|
lastSeq = seq;
|
|
818
804
|
}
|
|
805
|
+
// PERF (2026-08-24): snapshot the in-process reservation end BEFORE the B7
|
|
806
|
+
// advance below — seqCounters is about to be raised to lastSeq, so reading
|
|
807
|
+
// reservedSequenceEnd() at the persist site would ALWAYS see lastSeq ≤
|
|
808
|
+
// counter and skip the persist even for explicit seqs the reservation never
|
|
809
|
+
// covered (sidecar would lag the file → cross-process re-reservation inside
|
|
810
|
+
// the file's true range — the exact R16-B1 duplicate-seq race).
|
|
811
|
+
const reservedEnd = reservedSequenceEnd(eventsPath);
|
|
819
812
|
// B7: advance counter past the entire batch so next reserveSequence returns the correct value.
|
|
820
813
|
advanceSequenceCounter(eventsPath, lastSeq);
|
|
821
814
|
|
|
@@ -838,41 +831,56 @@ async function appendEventBatchInsideLock(eventsPath: string, queue: BufferedApp
|
|
|
838
831
|
}
|
|
839
832
|
|
|
840
833
|
fs.appendFileSync(eventsPath, finalized.map((f) => f.line).join(""), "utf-8");
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
834
|
+
// PERF (2026-08-25): skip the fsync when the whole batch is non-terminal.
|
|
835
|
+
// Terminal events never route through this buffer (appendEventBuffered
|
|
836
|
+
// bypasses to appendEvent, which fsyncs itself), so a batch here has no
|
|
837
|
+
// terminal event unless a caller deliberately mixed one in — mirror F3a:
|
|
838
|
+
// the event-reconstructor tolerates an inconsistent tail; the explicit
|
|
839
|
+
// persistSequenceMonotonic below still lands the reserved end range.
|
|
840
|
+
const hasTerminal = finalized.some((f) => TERMINAL_EVENT_TYPES.has(f.fullEvent.type));
|
|
841
|
+
if (hasTerminal) {
|
|
842
|
+
const fd = fs.openSync(eventsPath, "r+");
|
|
843
|
+
try {
|
|
844
|
+
fs.fsyncSync(fd);
|
|
845
|
+
} catch {
|
|
846
|
+
// EPERM on Windows CI: best-effort flush
|
|
847
|
+
} finally {
|
|
848
|
+
fs.closeSync(fd);
|
|
849
|
+
}
|
|
848
850
|
}
|
|
849
851
|
// R16-B1 (Phase 3.6): monotonic persist under the .seqlock — the reservation
|
|
850
852
|
// already persisted the batch range end; this covers explicit (pre-assigned)
|
|
851
853
|
// seqs that may exceed it and can never REGRESS the sidecar (the old bare
|
|
852
854
|
// persistSequence could write a lower lastSeq over a higher reserved value).
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
//
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
sequenceCache.set(eventsPath, {
|
|
862
|
-
size: stat.size,
|
|
863
|
-
mtimeMs: stat.mtimeMs,
|
|
864
|
-
seq: lastSeq,
|
|
865
|
-
lastAccessMs: Date.now(),
|
|
866
|
-
});
|
|
867
|
-
} catch (error) {
|
|
868
|
-
logInternalError("event-log.batch-cache-update", error, `eventsPath=${eventsPath}`);
|
|
855
|
+
// PERF (2026-08-24): R16-B1 advance-on-reserve already persisted the
|
|
856
|
+
// reserved end inside the .seqlock at reservation time. Re-acquiring
|
|
857
|
+
// the seqlock (~12 syscalls) to conclude "no write needed" is pure
|
|
858
|
+
// overhead in the single-writer common case — skip when lastSeq is
|
|
859
|
+
// covered by the reservation snapshot above; explicit seqs beyond the
|
|
860
|
+
// reserved range still persist.
|
|
861
|
+
if (lastSeq > reservedEnd) {
|
|
862
|
+
persistSequenceMonotonic(eventsPath, lastSeq);
|
|
869
863
|
}
|
|
870
864
|
|
|
865
|
+
// Phase 3: resolve all promises. (PERF 2026-08-24: the sequenceCache upkeep
|
|
866
|
+
// that used to live here fed no hot reader — see the note in
|
|
867
|
+
// appendEventAsync; nextSequence re-seeds via its sidecar/scan fallback.)
|
|
871
868
|
for (const { item, fullEvent } of finalized) item.resolve(fullEvent);
|
|
872
869
|
}
|
|
873
870
|
|
|
874
871
|
function appendEventInsideLock(eventsPath: string, event: AppendTeamEvent): TeamEvent {
|
|
875
872
|
fs.mkdirSync(path.dirname(eventsPath), { recursive: true });
|
|
873
|
+
// PERF (2026-08-24): one hoisted pre-append stat replaces the overflow
|
|
874
|
+
// existsSync+statSync pair below. The after-compaction re-checks further
|
|
875
|
+
// down keep their own fresh stats — compaction/rotation may have changed
|
|
876
|
+
// the file since preStat was taken, so they must not see a stale
|
|
877
|
+
// (pre-compaction) size.
|
|
878
|
+
let preStat: fs.Stats | undefined;
|
|
879
|
+
try {
|
|
880
|
+
preStat = fs.statSync(eventsPath);
|
|
881
|
+
} catch {
|
|
882
|
+
/* log absent — first append */
|
|
883
|
+
}
|
|
876
884
|
const baseMetadata = event.metadata;
|
|
877
885
|
// B7: use reserveSequence for atomic seq assignment across all paths.
|
|
878
886
|
const explicitSeq = baseMetadata?.seq;
|
|
@@ -909,9 +917,8 @@ function appendEventInsideLock(eventsPath: string, event: AppendTeamEvent): Team
|
|
|
909
917
|
// 3. After compact, if still over limit, rotate.
|
|
910
918
|
const isTerminal = TERMINAL_EVENT_TYPES.has(fullEvent.type);
|
|
911
919
|
let skippedDueToSize = false;
|
|
912
|
-
if (!isTerminal &&
|
|
913
|
-
|
|
914
|
-
if (stat.size > MAX_EVENTS_BYTES) {
|
|
920
|
+
if (!isTerminal && preStat) {
|
|
921
|
+
if (preStat.size > MAX_EVENTS_BYTES) {
|
|
915
922
|
// Try immediate compact (not waiting for counter % 100).
|
|
916
923
|
// Round 24 (BUG 1): we are INSIDE withEventLogLockSync. Use the unlocked
|
|
917
924
|
// apply/rotate cores — the locked variants would deadlock (mkdir lock
|
|
@@ -967,7 +974,8 @@ function appendEventInsideLock(eventsPath: string, event: AppendTeamEvent): Team
|
|
|
967
974
|
} catch (error) {
|
|
968
975
|
logInternalError("event-log.size-check", error, `eventsPath=${eventsPath}`);
|
|
969
976
|
}
|
|
970
|
-
// seq is already computed above via reserveSequence — reuse it for
|
|
977
|
+
// seq is already computed above via reserveSequence — reuse it for the
|
|
978
|
+
// explicit-seq persist below (no sequenceCache upkeep on append paths).
|
|
971
979
|
// const seq declaration removed (B7: seq is now computed before metadata object).
|
|
972
980
|
if (!skippedDueToSize) {
|
|
973
981
|
fs.appendFileSync(eventsPath, `${JSON.stringify(redactSecrets(fullEvent))}\n`, "utf-8");
|
|
@@ -997,24 +1005,11 @@ function appendEventInsideLock(eventsPath: string, event: AppendTeamEvent): Team
|
|
|
997
1005
|
// Only EXPLICIT (pre-assigned) seqs bypassed the reservation — persist
|
|
998
1006
|
// those, monotonically and under the .seqlock.
|
|
999
1007
|
if (explicitSeq !== undefined) persistSequenceMonotonic(eventsPath, seq);
|
|
1000
|
-
//
|
|
1001
|
-
//
|
|
1002
|
-
//
|
|
1003
|
-
//
|
|
1004
|
-
|
|
1005
|
-
const stat = fs.statSync(eventsPath);
|
|
1006
|
-
if (sequenceCache.size >= MAX_SEQUENCE_CACHE_ENTRIES) {
|
|
1007
|
-
evictOldestSequenceCacheEntries();
|
|
1008
|
-
}
|
|
1009
|
-
sequenceCache.set(eventsPath, {
|
|
1010
|
-
size: stat.size,
|
|
1011
|
-
mtimeMs: stat.mtimeMs,
|
|
1012
|
-
seq,
|
|
1013
|
-
lastAccessMs: Date.now(),
|
|
1014
|
-
});
|
|
1015
|
-
} catch (error) {
|
|
1016
|
-
logInternalError("event-log.persist-sequence", error, `eventsPath=${eventsPath}`);
|
|
1017
|
-
}
|
|
1008
|
+
// PERF (2026-08-24): the per-append sequenceCache upkeep that lived here
|
|
1009
|
+
// (post-append stat + Map set + occasional evict sort) fed no hot reader —
|
|
1010
|
+
// see the note in appendEventAsync. The .seq sidecar is already current
|
|
1011
|
+
// (advance-on-reserve / persistSequenceMonotonic above), and nextSequence()
|
|
1012
|
+
// re-seeds via its sidecar/scan fallback when called.
|
|
1018
1013
|
}
|
|
1019
1014
|
if (tickAppendCounter(eventsPath) && needsRotation(eventsPath)) {
|
|
1020
1015
|
// Round 24 (BUG 1): we are INSIDE withEventLogLockSync here (called via
|
|
@@ -1190,6 +1185,18 @@ export function flushBufferedQueuesSync(): void {
|
|
|
1190
1185
|
for (const eventsPath of [...bufferedTimers.keys()]) bufferedTimers.delete(eventsPath);
|
|
1191
1186
|
}
|
|
1192
1187
|
|
|
1188
|
+
// Exported only for the buffered-batch unit tests. NOT part of the public API —
|
|
1189
|
+
// the `__test__` prefix follows the project convention used in atomic-write.ts,
|
|
1190
|
+
// state-store.ts, team-runner.ts, etc. `appendEventBuffered` rejects terminal
|
|
1191
|
+
// events BEFORE they reach the batch (see appendEventBuffered), so a batch that
|
|
1192
|
+
// deliberately mixes in a terminal event is unreachable through the public API;
|
|
1193
|
+
// this hook lets T2 assert the fsync gate directly. Both production call paths
|
|
1194
|
+
// (async flush via withEventLogLockAsync and the EL-2 sync flush via
|
|
1195
|
+
// withEventLogLockSync) route through appendEventBatchInsideLock.
|
|
1196
|
+
export function __test__appendBatchForUnitTest(eventsPath: string, queue: BufferedAppend[]): Promise<void> {
|
|
1197
|
+
return appendEventBatchInsideLock(eventsPath, queue);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1193
1200
|
/**
|
|
1194
1201
|
* Schedule an async event append without waiting for the result.
|
|
1195
1202
|
* Uses the non-blocking async queue to avoid blocking the event loop.
|
|
@@ -221,10 +221,20 @@ function withSeqLock<T>(eventsPath: string, fn: () => T): T {
|
|
|
221
221
|
try {
|
|
222
222
|
fs.mkdirSync(lockDir);
|
|
223
223
|
try {
|
|
224
|
-
//
|
|
225
|
-
|
|
224
|
+
// PERF (2026-08-24): "wx" (O_CREAT|O_EXCL) — fails rather than
|
|
225
|
+
// following a planted symlink, so no O_NOFOLLOW/temp/rename
|
|
226
|
+
// ceremony needed for this disposable, mtime-stale-detected
|
|
227
|
+
// 4-byte file. We own the lock dir (we just mkdir'd it), so
|
|
228
|
+
// EEXIST means a crashed holder's leftover under OUR fresh dir
|
|
229
|
+
// or an attack — either way, skip: the dir itself is the mutex.
|
|
230
|
+
const fd = fs.openSync(pidFile, "wx");
|
|
231
|
+
try {
|
|
232
|
+
fs.writeSync(fd, String(process.pid));
|
|
233
|
+
} finally {
|
|
234
|
+
fs.closeSync(fd);
|
|
235
|
+
}
|
|
226
236
|
} catch {
|
|
227
|
-
/* best-effort */
|
|
237
|
+
/* best-effort — see withEventLogLockAsync note */
|
|
228
238
|
}
|
|
229
239
|
acquired = true;
|
|
230
240
|
} catch {
|
|
@@ -286,6 +296,14 @@ export function persistSequenceMonotonic(eventsPath: string, seq: number): void
|
|
|
286
296
|
});
|
|
287
297
|
}
|
|
288
298
|
|
|
299
|
+
/** PERF (2026-08-24): callers that just reserved a range can skip the lock+read
|
|
300
|
+
* sidecar round-trip in persistSequenceMonotonic when their value is already
|
|
301
|
+
* covered by the in-process reservation (R16-B1 advance-on-reserve persisted it
|
|
302
|
+
* inside the .seqlock at reservation time). */
|
|
303
|
+
export function reservedSequenceEnd(eventsPath: string): number {
|
|
304
|
+
return seqCounters.get(eventsPath) ?? 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
289
307
|
// B7: single in-process monotonic sequence counter per eventsPath. The three
|
|
290
308
|
// append paths — sync appendEvent (withEventLogLockSync file lock), buffered
|
|
291
309
|
// flush (asyncLocks promise chain), and direct appendEventAsync (asyncQueues
|
|
@@ -50,7 +50,7 @@ export function loadPlanRecords(manifest: TeamRunManifest): PlanRecord[] {
|
|
|
50
50
|
const raw = fs.readFileSync(planFilePath(manifest), "utf-8");
|
|
51
51
|
const parsed = JSON.parse(raw) as Partial<PlanFile>;
|
|
52
52
|
if (!parsed || !Array.isArray(parsed.revisions)) return [];
|
|
53
|
-
return parsed.revisions.filter((r): r is PlanRecord => Boolean(r
|
|
53
|
+
return parsed.revisions.filter((r): r is PlanRecord => Boolean(r?.id && typeof r.version === "number"));
|
|
54
54
|
} catch (err) {
|
|
55
55
|
if ((err as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
56
56
|
logInternalError("plan-store.read-failed", err instanceof Error ? err : new Error(String(err)), `run=${manifest.runId}`);
|