neurastream 0.1.16 → 0.1.17
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/package.json +1 -1
- package/src/core/queue-close.ts +71 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neurastream",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Guardrails for running many AI coding agents at once: plan queue, blockers, call budgets, provider caps, scoring, merge-verified close.",
|
|
5
5
|
"keywords": ["ai-agents", "coding-agents", "orchestration", "guardrails", "omp", "bun", "cli"],
|
|
6
6
|
"homepage": "https://github.com/umiddey/neurastream#readme",
|
package/src/core/queue-close.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { appendFileSync, existsSync, fstatSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { appendFileSync, existsSync, fstatSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import type { Config } from "./config.ts";
|
|
@@ -65,6 +65,25 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
65
65
|
if (lines.length) writeFileSync(file, `${lines.join("\n")}\n`);
|
|
66
66
|
else rmSync(file, { force: true });
|
|
67
67
|
}
|
|
68
|
+
// A clone of a clone judges "landed" against the middle copy's default branch, which can be
|
|
69
|
+
// stale. Follow local-path origins (at most 3 hops) to the upstream repo and fetch its default
|
|
70
|
+
// branch: that commit is the base. No local origin: the existing base, unchanged.
|
|
71
|
+
function upstreamBase(repo: string, localBase: string): string {
|
|
72
|
+
let cur = repo;
|
|
73
|
+
let hops = 0;
|
|
74
|
+
while (hops < 3) {
|
|
75
|
+
const url = git(cur, "config", "--get", "remote.origin.url").out;
|
|
76
|
+
if (!url || !path.isAbsolute(url) || !existsSync(url) || !git(url, "rev-parse", "--git-dir").ok) break;
|
|
77
|
+
cur = url;
|
|
78
|
+
hops++;
|
|
79
|
+
}
|
|
80
|
+
if (hops === 0) return localBase;
|
|
81
|
+
const named = git(cur, "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD").out.replace(/^origin\//, "");
|
|
82
|
+
const def = named || git(cur, "symbolic-ref", "-q", "--short", "HEAD").out;
|
|
83
|
+
const fetched = git(repo, "fetch", "-q", cur, def);
|
|
84
|
+
if (!fetched.ok) die(`could not fetch ${def} from ${cur}: ${fetched.out}`);
|
|
85
|
+
return git(repo, "rev-parse", "FETCH_HEAD").out;
|
|
86
|
+
}
|
|
68
87
|
const today = new Date().toISOString().slice(0, 10);
|
|
69
88
|
|
|
70
89
|
const arg = args[0];
|
|
@@ -95,10 +114,26 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
95
114
|
|
|
96
115
|
git(plan.repo, "fetch", "-q", "origin");
|
|
97
116
|
const head = git(plan.repo, "symbolic-ref", "-q", "refs/remotes/origin/HEAD");
|
|
98
|
-
const base = head.ok ? head.out : "refs/remotes/origin/main";
|
|
117
|
+
const base = upstreamBase(plan.repo, head.ok ? head.out : "refs/remotes/origin/main");
|
|
118
|
+
const worktrees = (): { path: string; branch: string | null; head: string }[] => {
|
|
119
|
+
const list: { path: string; branch: string | null; head: string }[] = [];
|
|
120
|
+
for (const line of git(plan.repo, "worktree", "list", "--porcelain").out.split("\n")) {
|
|
121
|
+
if (line.startsWith("worktree ")) list.push({ path: path.resolve(line.slice(9)), branch: null, head: "" });
|
|
122
|
+
const cur = list.at(-1);
|
|
123
|
+
if (!cur) continue;
|
|
124
|
+
if (line.startsWith("HEAD ")) cur.head = line.slice(5);
|
|
125
|
+
else if (line.startsWith("branch refs/heads/")) cur.branch = line.slice(18);
|
|
126
|
+
}
|
|
127
|
+
return list;
|
|
128
|
+
};
|
|
129
|
+
const wtEntry = plan.worktree ? worktrees().find(w => w.path === path.resolve(plan.worktree!)) : undefined;
|
|
99
130
|
const wtExists = !!plan.worktree && existsSync(plan.worktree);
|
|
100
|
-
|
|
101
|
-
|
|
131
|
+
// A plan worktree is the plan's only while it sits on the plan's branch (or a detached HEAD already merged into it).
|
|
132
|
+
const owned = wtExists && !!plan.branch && !!wtEntry && (wtEntry.branch !== null
|
|
133
|
+
? wtEntry.branch === plan.branch
|
|
134
|
+
: git(plan.repo, "merge-base", "--is-ancestor", wtEntry.head, `refs/heads/${plan.branch}`).ok);
|
|
135
|
+
if (wtExists && !owned) console.log(`queue-close: worktree ${plan.worktree} kept: it is on ${wtEntry?.branch ?? (wtEntry ? "a detached HEAD" : "no branch of this repo")}`);
|
|
136
|
+
const branch = plan.branch;
|
|
102
137
|
const branchExists = !!branch && git(plan.repo, "rev-parse", "--verify", "-q", `refs/heads/${branch}`).ok;
|
|
103
138
|
const merged = branchExists && (
|
|
104
139
|
git(plan.repo, "merge-base", "--is-ancestor", `refs/heads/${branch}`, base).ok ||
|
|
@@ -106,17 +141,33 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
106
141
|
declaredLanded(plan.repo, `refs/heads/${branch}`, base, readFileSync(file, "utf8"))
|
|
107
142
|
);
|
|
108
143
|
|
|
109
|
-
|
|
144
|
+
// A plan whose worktree is missing or kept is checked against the upstream base, never a foreign checkout.
|
|
145
|
+
const detached = !!plan.worktree && !owned;
|
|
146
|
+
const runIn = owned ? plan.worktree! : plan.repo;
|
|
110
147
|
if (!cancelling) {
|
|
111
148
|
const tip = branchExists ? git(plan.repo, "rev-parse", `refs/heads/${branch}`).out : "";
|
|
112
|
-
const dirty = git(runIn, "status", "--porcelain").out;
|
|
149
|
+
const dirty = detached ? `base:${base}` : git(runIn, "status", "--porcelain").out;
|
|
113
150
|
const doneKey = sha(`${plan.doneCheck}\n${tip}\n${dirty}`);
|
|
114
151
|
const doneCache = path.join(STATE_DIR, `done-ok-${sha(file)}`);
|
|
115
152
|
const cached = existsSync(doneCache) && readFileSync(doneCache, "utf8").trim() === doneKey;
|
|
116
153
|
if (cached) console.log(`queue-close: done check passed before for this state (cached ${doneKey})`);
|
|
117
154
|
else {
|
|
118
|
-
|
|
119
|
-
|
|
155
|
+
let scratch: string | undefined;
|
|
156
|
+
if (detached) {
|
|
157
|
+
scratch = mkdtempSync(path.join(STATE_DIR, "done-base-"));
|
|
158
|
+
const add = git(plan.repo, "worktree", "add", "--detach", scratch, base);
|
|
159
|
+
if (!add.ok) {
|
|
160
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
161
|
+
die(`could not check out ${base} for the done check: ${add.out}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
console.log(`queue-close: done check in ${detached ? `${base} (temporary worktree)` : runIn}: ${plan.doneCheck}`);
|
|
165
|
+
const dc = spawnSync("bash", ["-lc", plan.doneCheck!], { cwd: scratch ?? runIn, stdio: "inherit", timeout: 3600_000 });
|
|
166
|
+
if (scratch) {
|
|
167
|
+
git(plan.repo, "worktree", "remove", "--force", scratch);
|
|
168
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
169
|
+
git(plan.repo, "worktree", "prune");
|
|
170
|
+
}
|
|
120
171
|
if (dc.status !== 0) {
|
|
121
172
|
rmSync(doneCache, { force: true });
|
|
122
173
|
const text = readFileSync(file, "utf8")
|
|
@@ -139,8 +190,9 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
139
190
|
const [ref, t] = line.split(" ");
|
|
140
191
|
if (ref && Number(t) < cutoff) git(plan.repo, "update-ref", "-d", ref);
|
|
141
192
|
}
|
|
142
|
-
if (
|
|
143
|
-
const
|
|
193
|
+
if (owned) {
|
|
194
|
+
const stash = git(plan.worktree!, "stash", "create", `queue-close: uncommitted work of ${path.basename(file)}`);
|
|
195
|
+
const saved = stash.ok ? stash.out : "";
|
|
144
196
|
if (saved) {
|
|
145
197
|
const ref = `refs/queue-archive/${path.basename(file, ".md")}-${today}`;
|
|
146
198
|
if (!git(plan.repo, "update-ref", ref, saved).ok || !git(plan.repo, "stash", "show", "-p", ref).out)
|
|
@@ -155,9 +207,9 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
155
207
|
if (!keep.ok || !git(plan.repo, "update-ref", ref, keep.out).ok) die(`could not save branch ${branch} to ${ref}; nothing removed`);
|
|
156
208
|
console.log(`queue-close: branch ${branch} saved as ${ref} (restore: git -C ${plan.repo} branch ${branch} ${ref}^; pruned after ${ARCHIVE_DAYS} days)`);
|
|
157
209
|
}
|
|
158
|
-
if (
|
|
210
|
+
if (owned) {
|
|
159
211
|
let rm = git(plan.repo, "worktree", "remove", "--force", plan.worktree!);
|
|
160
|
-
if (!rm.ok &&
|
|
212
|
+
if (!rm.ok && existsSync(plan.worktree!)) {
|
|
161
213
|
spawnSync("docker", ["run", "--rm", "-v", `${plan.worktree}:/w`, "alpine", "chown", "-R", `${os.userInfo().uid}:${os.userInfo().gid}`, "/w"], { stdio: "ignore" });
|
|
162
214
|
rm = git(plan.repo, "worktree", "remove", "--force", plan.worktree!);
|
|
163
215
|
if (!rm.ok && existsSync(plan.worktree!)) {
|
|
@@ -170,9 +222,13 @@ export function queueClose(args: string[], cfg: Config): number {
|
|
|
170
222
|
console.log(`queue-close: worktree ${plan.worktree} removed`);
|
|
171
223
|
}
|
|
172
224
|
if (branchExists) {
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
|
|
225
|
+
const holder = worktrees().find(w => w.branch === branch);
|
|
226
|
+
if (holder) console.log(`queue-close: branch ${branch} kept: checked out in ${holder.path}`);
|
|
227
|
+
else {
|
|
228
|
+
const d = git(plan.repo, "branch", "-D", branch!);
|
|
229
|
+
if (!d.ok) die(`could not delete branch ${branch}: ${d.out}`);
|
|
230
|
+
console.log(`queue-close: branch ${branch} deleted`);
|
|
231
|
+
}
|
|
176
232
|
}
|
|
177
233
|
|
|
178
234
|
const month = today.slice(0, 7).replace("-", "");
|