- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/queue.ts?version=271&format=json
179 linesFirst import 83% · pinch 9% · rename 7% · infra-split 1% · review 1%Last change rename in version 100, 7 versions changed it
You're reading version 271. The newest is 318, and no version since then changed this file.
1// The merge queue on the client (#39): with "queue": true in abso.json, a
2// publish to shared waits in the queue until a batch with it passes the
3// project's check, and lands, or is pulled with the check's output. publish
4// waits for that, up to a cap under the chat's 2-minute command limit; status
5// shows the queue; abso unqueue takes a queued publish back.
7import { type Api, newsLines, type QueueItem, type QueueState } from "./api.ts";
8import { type Parsed, UsageError } from "./args.ts";
9import { type Env, type Output, open } from "./commands.ts";
10import { logNews } from "./folder.ts";
12/** How long publish waits by default: under the 2 minutes a Claude Code shell command gets (#39). */
13export const WAIT_SECONDS = 100;
15/** Exit code for a publish still in the queue when the wait ends (#39). */
16export const STILL_QUEUED = 3;
18export type QueueOutcome =
19 | { readonly kind: "landed"; readonly version: number; readonly news: string[] }
20 | { readonly kind: "pulled"; readonly text: string; readonly news: string[] }
21 /** Out of the queue, but neither the last round nor the news says how (another round ran since, or the news was taken). */
22 | { readonly kind: "gone"; readonly news: string[] }
23 | { readonly kind: "waiting"; readonly state: QueueState | undefined };
25/** Where a queued publish stands: being checked, or how many wait ahead of it. */
26export function standing(items: readonly QueueItem[], id: number): string {
27 const at = items.findIndex((q) => q.id === id);
28 if (at < 0) return "out of the queue";
29 if (items[at]!.checking) {
30 const batch = items.filter((q) => q.checking).length;
31 return batch === 1 ? "being checked" : `being checked in a batch of ${batch}`;
32 }
33 return at === 0 ? "next to be checked" : `waiting, ${at} ${at === 1 ? "push" : "pushes"} ahead of it`;
34}
36/**
37 * Wait for queued publish `id` to land or be pulled, polling the queue every
38 * `pollMs` until `waitMs` has passed. A failed poll isn't a failed publish:
39 * it tries again until the time is up. The outcome comes from the last
40 * round, else from the news that says it (which is taken, so it's handed back
41 * to print).
42 */
43export async function waitForQueued(
44 api: Api,
45 id: number,
46 waitMs: number,
47 say: (line: string) => void,
48 pollMs = 3000,
49): Promise<QueueOutcome> {
50 const until = Date.now() + waitMs;
51 let state: QueueState | undefined;
52 let said = "";
53 while (Date.now() < until) {
54 await new Promise((ok) => setTimeout(ok, Math.min(pollMs, Math.max(0, until - Date.now()))));
55 try {
56 state = await api.queueState();
57 } catch {
58 continue;
59 }
60 if (!state.items.some((q) => q.id === id)) return outcomeOf(api, id, state);
61 const now = state.paused
62 ? `the queue is paused: ${state.paused.reason}; next try ${state.paused.nextTry.slice(11, 16)} UTC`
63 : standing(state.items, id);
64 if (now !== said) say(`abso: your push is ${now}.\n`);
65 said = now;
66 }
67 return { kind: "waiting", state };
68}
70async function outcomeOf(api: Api, id: number, state: QueueState): Promise<QueueOutcome> {
71 const all = await api.news().then(
72 (a) => newsLines(a.news),
73 () => [] as string[],
74 );
75 // The news names the outcome; one publish per agent is the usual case, so the newest such line is ours.
76 // publish says the outcome itself, so that line isn't printed again as news.
77 const said = [...all]
78 .reverse()
79 .find((n) => /^Your queued push ".*" (landed as version \d+|was turned back: )/s.test(n));
80 const news = all.filter((n) => n !== said);
81 const last = state.last;
82 const landed =
83 last && (last.kind === "landed" || last.kind === "pulled") ? last.versions.find((v) => v.id === id) : undefined;
84 if (landed) return { kind: "landed", version: landed.version, news };
85 const version = said === undefined ? undefined : /landed as version (\d+)\.$/.exec(said)?.[1];
86 if (version !== undefined) return { kind: "landed", version: Number(version), news };
87 if (said !== undefined) return { kind: "pulled", text: said, news };
88 if (last?.kind === "pulled" && last.pulled.includes(id))
89 return {
90 kind: "pulled",
91 text: `Your queued push was turned back: the project's check failed on it.${last.output ? `\n${last.output.trimEnd()}` : ""}`,
92 news,
93 };
94 return { kind: "gone", news };
95}
97/**
98 * A pull, said for the publisher (#39): the check's output
99 * first, then the verdict as the last line, since that's the line an agent
100 * acts on. The engine's news has the output in the middle of its sentence.
101 */
102export function pulledText(news: string): string {
103 const m =
104 /^Your queued push (".*") was turned back: ([^\n]*?)(?::\n([\s\S]*?))?\.? Its changes are still in your draft\.$/s.exec(
105 news.replace(/\n\. Its changes/, ". Its changes"),
106 );
107 if (!m) return `${news}\nNot pushed. Fix it, then push again.`;
108 const output = m[3]?.trimEnd() ?? "";
109 return [
110 ...(output === "" ? [] : ["The check's output ends:", output]),
111 `Not pushed: your queued push ${m[1]} was turned back: ${m[2]!.replace(/\.$/, "")}. Its changes are still in your draft; fix them, then push again.`,
112 ].join("\n");
113}
115/** The lines status shows about the queue: who waits, what is being checked, a pause. Nothing while it's empty. */
116export function queueText(queue: QueueState | null | undefined, me: string): string[] {
117 if (!queue) return [];
118 const lines: string[] = [];
119 if (queue.items.length > 0) {
120 const checking = queue.items.filter((q) => q.checking);
121 const waiting = queue.items.filter((q) => !q.checking);
122 const who = (q: QueueItem) => `${q.agent === me ? `yours (${q.id})` : q.agent}`;
123 lines.push(
124 `Merge queue: ${[
125 checking.length > 0
126 ? `checking ${checking.length === 1 ? who(checking[0]!) : `a batch of ${checking.length} (${checking.map(who).join(", ")})`}`
127 : "",
128 waiting.length > 0 ? `waiting: ${waiting.map(who).join(", ")}` : "",
129 ]
130 .filter((p) => p !== "")
131 .join("; ")}.`,
132 );
133 }
134 if (queue.paused)
135 lines.push(
136 `The merge queue is paused since ${queue.paused.since.slice(11, 16)} UTC: ${queue.paused.reason}; next try ${queue.paused.nextTry.slice(11, 16)} UTC.`,
137 );
138 return lines;
139}
141/**
142 * What abso waiting --mine says about this agent's own publishes in the merge queue: where each stands, whether
143 * the queue is paused, and how the outcome comes. Nothing when none of theirs waits.
144 */
145export function queuedLines(queue: QueueState | null | undefined, me: string): string[] {
146 const items = queue?.items ?? [];
147 const mine = items.filter((q) => q.agent === me);
148 if (mine.length === 0) return [];
149 return [
150 ...mine.map(
151 (q) => `Your push ${q.id} (${JSON.stringify(q.message)}) is in the merge queue: ${standing(items, q.id)}.`,
152 ),
153 ...(queue?.paused ? [`The merge queue is paused: ${queue.paused.reason}.`] : []),
154 `${mine.length === 1 ? "It lands or is turned back" : "Each lands or is turned back"} once the project's check runs on it; the outcome comes as news, and abso unqueue takes ${mine.length === 1 ? "it" : "them"} back.`,
155 ];
156}
158/** abso unqueue: take this agent's queued publishes back; their changes stay in the draft. */
159export async function runUnqueue(args: Parsed, env: Env): Promise<Output> {
160 if (args.positionals.length > 0)
161 throw new UsageError("unqueue takes no arguments: it takes back every push of yours still in the merge queue");
162 const { root, api } = open(env);
163 const answer = await api.unqueue();
164 // The engine says it as news too ("you withdrew it"); this command's own text says the same, so it isn't printed twice.
165 const news = await api.news().then(
166 (a) => newsLines(a.news),
167 () => [] as string[],
168 );
169 logNews(root, news);
170 const others = news.filter((n) => !/^Your queued push ".*" was turned back: you withdrew it\./s.test(n));
171 const ids = answer.pulled;
172 return {
173 text: [
174 `Took back ${ids.length === 1 ? `queued push ${ids[0]}` : `queued pushes ${ids.join(", ")}`}: ${ids.length === 1 ? "its" : "their"} changes are still in your draft, unpushed.`,
175 ...others.map((n) => ` ${n}`),
176 ].join("\n"),
177 json: { pulled: ids, news: others },
178 };
179}