- MCP
- blame on this site's /mcp
- CLI
- abso blame client/src/queue.ts --version 0 --json
- JSON
- /christopher/abso-core/blame/client/src/queue.ts?version=0&format=json
162 linesFirst import 100%Last change First import in version 0,
162 lines from 1 change in 1 version.
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, plural } 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, agreed with Engine). */
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 ? "publish" : "publishes"} 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(`avcs: your publish 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 publish ".*" (landed as version \d+|was pulled: )/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 publish was pulled: 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, agreed with Engine): 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 publish (".*") was pulled: ([^\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 published. Fix it, then publish again.`;
108 const output = m[3]?.trimEnd() ?? "";
109 return [
110 ...(output === "" ? [] : ["The check's output ends:", output]),
111 `Not published: your queued publish ${m[1]} was pulled: ${m[2]!.replace(/\.$/, "")}. Its changes are still in your draft; fix them, then publish 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/** abso unqueue: take this agent's queued publishes back; their changes stay in the draft. */
142export async function runUnqueue(args: Parsed, env: Env): Promise<Output> {
143 if (args.positionals.length > 0)
144 throw new UsageError("unqueue takes no arguments: it takes back every publish of yours still in the merge queue");
145 const { root, api } = open(env);
146 const answer = await api.unqueue();
147 // The engine says it as news too ("you withdrew it"); this command's own text says the same, so it isn't printed twice.
148 const news = await api.news().then(
149 (a) => newsLines(a.news),
150 () => [] as string[],
151 );
152 logNews(root, news);
153 const others = news.filter((n) => !/^Your queued publish ".*" was pulled: you withdrew it\./s.test(n));
154 const ids = answer.pulled;
155 return {
156 text: [
157 `Took back ${ids.length === 1 ? `queued publish ${ids[0]}` : `queued publishes ${ids.join(", ")}`}: ${ids.length === 1 ? "its" : "their"} changes are still in your draft, unpublished.`,
158 ...others.map((n) => ` ${n}`),
159 ].join("\n"),
160 json: { pulled: ids, news: others },
161 };
162}