- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/writer-agents.ts?version=278&format=json
240 linescapacity 80% · codex-capacity-chair 20%Last change capacity in version 262, 4 versions changed it
You're reading version 278. The newest is 318, and no version since then changed this file.
1import { type GraphStateStorage, StoredMap, storedArray } from "../../src/engine/state-source.ts";
2import type { AgentState } from "./project-types.ts";
4type StoredAgent = Omit<AgentState, "told" | "parked"> & {
5 /** The told list as rows written before the told buckets kept it (abso Capacity); absent since. */
6 told?: string[];
7 parked: [string, { base: number; mine: AgentState["mine"] }][];
8};
10/**
11 * Each agent's told keys (the news it has been told about) in TOLD_BUCKETS rows of their own, beside its agent row
12 * (abso Capacity, issue 664; Security yes on ask 2102). The list grows for as long as the agent works and was most of
13 * its row: abso's 109 agent rows were 4.3 MB, read in full by every status, where and drafts, none of which reads it.
14 * A row is keyed by the agent's name and a bucket (a fixed hash of the key), and holds that bucket's keys, sorted.
15 */
16const TOLD = "project:agents:told";
17export const TOLD_BUCKETS = 64;
19/** The bucket a told key goes in: 32-bit FNV-1a of its UTF-16 code units. */
20export function toldBucket(key: string): number {
21 let hash = 0x811c9dc5;
22 for (let i = 0; i < key.length; i++) {
23 hash ^= key.charCodeAt(i);
24 hash = Math.imul(hash, 0x01000193);
25 }
26 return (hash >>> 0) % TOLD_BUCKETS;
27}
29const toldRow = (agent: string, bucket: number) => JSON.stringify([agent, bucket]);
31function sortedIndex(keys: readonly string[], key: string): number {
32 let lo = 0;
33 let hi = keys.length;
34 while (lo < hi) {
35 const mid = (lo + hi) >> 1;
36 if (keys[mid]! < key) lo = mid + 1;
37 else hi = mid;
38 }
39 return lo;
40}
42/**
43 * An agent's told keys on a stored writer: has() reads one bucket and add() rewrites one. A row written before the
44 * buckets carries its list itself (`listed`), read alongside the buckets until the agent's row is next written, which
45 * moves the list into the buckets (WriterAgents' encode).
46 */
47export class StoredTold extends Set<string> {
48 readonly #storage: GraphStateStorage;
49 readonly #agent: string;
50 readonly #listed: Set<string>;
51 constructor(storage: GraphStateStorage, agent: string, listed: Iterable<string> = []) {
52 super();
53 this.#storage = storage;
54 this.#agent = agent;
55 this.#listed = new Set(listed);
56 }
57 /** Keys an old row listed and the buckets don't hold yet. */
58 get listed(): ReadonlySet<string> {
59 return this.#listed;
60 }
61 #bucket(bucket: number): string[] {
62 return (this.#storage.read(TOLD, toldRow(this.#agent, bucket)) as string[] | undefined) ?? [];
63 }
64 #inBuckets(key: string): boolean {
65 const keys = this.#bucket(toldBucket(key));
66 return keys[sortedIndex(keys, key)] === key;
67 }
68 override has(key: string): boolean {
69 return this.#listed.has(key) || this.#inBuckets(key);
70 }
71 override add(key: string): this {
72 if (this.has(key)) return this;
73 storeTold(this.#storage, this.#agent, [key]);
74 return this;
75 }
76 override delete(key: string): boolean {
77 const listed = this.#listed.delete(key);
78 const bucket = toldBucket(key);
79 const keys = this.#bucket(bucket);
80 const at = sortedIndex(keys, key);
81 if (keys[at] !== key) return listed;
82 keys.splice(at, 1);
83 if (keys.length > 0) this.#storage.write(TOLD, toldRow(this.#agent, bucket), keys);
84 else this.#storage.remove(TOLD, toldRow(this.#agent, bucket));
85 return true;
86 }
87 override clear(): void {
88 this.#listed.clear();
89 clearTold(this.#storage, this.#agent);
90 }
91 override get size(): number {
92 let n = 0;
93 for (const _ of this.values()) n++;
94 return n;
95 }
96 override *values(): SetIterator<string> {
97 yield* this.#listed;
98 for (let bucket = 0; bucket < TOLD_BUCKETS; bucket++)
99 for (const key of this.#bucket(bucket)) if (!this.#listed.has(key)) yield key;
100 }
101 override keys(): SetIterator<string> {
102 return this.values();
103 }
104 override *entries(): SetIterator<[string, string]> {
105 for (const key of this.values()) yield [key, key];
106 }
107 override [Symbol.iterator](): SetIterator<string> {
108 return this.values();
109 }
110 override forEach(callback: (value: string, key: string, set: Set<string>) => void, thisArg?: unknown): void {
111 for (const key of this.values()) callback.call(thisArg, key, key, this);
112 }
113 /** Move an old row's list into the buckets. */
114 moveListed(): void {
115 if (this.#listed.size === 0) return;
116 storeTold(this.#storage, this.#agent, this.#listed);
117 this.#listed.clear();
118 }
119}
121/** Put keys in an agent's buckets: each bucket read and written once. */
122function storeTold(storage: GraphStateStorage, agent: string, keys: Iterable<string>): void {
123 const byBucket = new Map<number, string[]>();
124 for (const key of keys) {
125 const bucket = toldBucket(key);
126 let list = byBucket.get(bucket);
127 if (!list) byBucket.set(bucket, (list = []));
128 list.push(key);
129 }
130 for (const [bucket, adding] of byBucket) {
131 const keys = (storage.read(TOLD, toldRow(agent, bucket)) as string[] | undefined) ?? [];
132 let changed = false;
133 for (const key of adding) {
134 const at = sortedIndex(keys, key);
135 if (keys[at] === key) continue;
136 keys.splice(at, 0, key);
137 changed = true;
138 }
139 if (changed) storage.write(TOLD, toldRow(agent, bucket), keys);
140 }
141}
143/** Take out every bucket of an agent's (when the agent is removed). */
144function clearTold(storage: GraphStateStorage, agent: string): void {
145 for (let bucket = 0; bucket < TOLD_BUCKETS; bucket++) storage.remove(TOLD, toldRow(agent, bucket));
146}
148/**
149 * Identity map for agents touched by the current operation. Project's existing
150 * reducers share mutable agent references, so copies cannot be substituted
151 * halfway through an operation. Flush those rows with the canonical append,
152 * then drop the references. Opening this map does not load any agents.
153 */
154export class WriterAgents extends StoredMap<string, AgentState> {
155 readonly #touched = new Map<string, AgentState>();
156 readonly #storage: GraphStateStorage;
157 constructor(storage: GraphStateStorage) {
158 super(storage, "project:agents", {
159 // The row without its told keys, which go to the agent's buckets: an old row's list moves there now.
160 encode: (agent) => {
161 const { told, ...rest } = agent;
162 if (told instanceof StoredTold) told.moveListed();
163 else storeTold(storage, agent.name, told);
164 return { ...rest, parked: [...agent.parked] };
165 },
166 decode: (value) => {
167 const { told, ...agent } = value as StoredAgent;
168 return { ...agent, told: new StoredTold(storage, agent.name, told), parked: new Map(agent.parked) };
169 },
170 });
171 this.#storage = storage;
172 }
173 override get(key: string): AgentState | undefined {
174 let value = this.#touched.get(key);
175 if (!value) {
176 value = super.get(key);
177 if (value) this.#touched.set(key, value);
178 }
179 return value;
180 }
181 override set(key: string, value: AgentState): this {
182 super.set(key, value);
183 this.#touched.set(key, value);
184 return this;
185 }
186 override delete(key: string): boolean {
187 this.#touched.delete(key);
188 // Its told keys go with it (Security on ask 2102).
189 clearTold(this.#storage, key);
190 return super.delete(key);
191 }
192 override *entries(): MapIterator<[string, AgentState]> {
193 for (const [key, stored] of super.entries()) {
194 let value = this.#touched.get(key);
195 if (!value) this.#touched.set(key, (value = stored));
196 yield [key, value];
197 }
198 }
199 /** Let go of the agents read, when nothing changed them: a reader kept between calls (abso Capacity). */
200 release(): void {
201 this.#touched.clear();
202 }
204 flush(): void {
205 for (const [key, value] of this.#touched) super.set(key, value);
206 this.#touched.clear();
207 }
209 /**
210 * Move up to `limit` more agents' old told lists into their buckets (abso Capacity, issue 664; Agent VCS's go): a
211 * cursor walks the agents in the order they joined, once, a few rows at each write, so a project adopted before the
212 * buckets moves every row over without a separate step. Rows written since are in the new form already, and an
213 * agent read in between answers the same (StoredTold.listed). Returns how many rows it rewrote.
214 */
215 moveOldTold(limit: number): number {
216 const done = (this.#storage.read(TOLD_META, "cursor") as number | undefined) ?? 0;
217 const order = this.#order();
218 if (done >= order.length) return 0;
219 let at = done;
220 let moved = 0;
221 for (; at < order.length && at < done + limit; at++) {
222 const key = order[at]!;
223 if (this.#touched.has(key)) continue; // written with this operation: its row is new already
224 const agent = super.get(key);
225 if (!(agent?.told instanceof StoredTold) || agent.told.listed.size === 0) continue;
226 super.set(key, agent);
227 moved++;
228 }
229 this.#storage.write(TOLD_META, "cursor", at);
230 return moved;
231 }
233 /** The agents' keys in the order they joined (StoredMap's slots, a removed agent's included: it reads as absent). */
234 #order(): readonly string[] {
235 return storedArray<string>(this.#storage, "project:agents:order");
236 }
237}
239/** Where the move of old told lists into buckets has got to (WriterAgents.moveOldTold). */
240const TOLD_META = "project:agents:told:meta";