Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/fork-seams.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@
"continueThreadsAfterServerUpdate: false,",
"automaticUpdates: false,"
]
},
"packages/shared/src/AgentScope.ts": {
"why": "The slot observer (apps/server/src/slots/SlotObserver.ts) reads an agent's memory from its own systemd scope; one optional method on upstream's interface asks for it.",
"added": [
"/** Fork: the thread's own systemd scope and slice, for reading its memory; none when shared. */",
"readonly ownScope?: (",
"threadId: string,",
") => Effect.Effect<{ unit: string; slice: string } | undefined>;"
]
},
"apps/server/src/process/agentScope.ts": {
"why": "The scope map is private to upstream's make(); the fork's ownScope (see packages/shared/src/AgentScope.ts) is a 7-line lookup in it.",
"added": [
"ownScope: (threadId) =>",
"Effect.sync(() => {",
"const tracked = scopeByThread.get(threadId);",
"return tracked === undefined || tracked.shared",
"? undefined",
": { unit: tracked.unit, slice: ownedSlice };",
"}),"
]
}
},
"//pending": "Upstream files whose fork lines predate counting every upstream file, each with its ceiling: the most fork lines it may hold against upstream (counted from upstream, as fork-follow runs the check). To move into fork files or pin as seams; lower a ceiling when lines move out and take a file off when none are left. A pull request may change them but not grow them.",
Expand Down
89 changes: 63 additions & 26 deletions apps/server/src/slots/SlotGate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ import { makeSlotObserver } from "./SlotObserver.ts";
export type SlotToken = number;

export type SlotAcquire =
/** `token` is `null` when the gate is off or has no limit (nothing to release). */
/**
* `token` is `null` when the gate is off or has no limit (nothing to release), unless the
* capacity rule beside it keeps the start itself (`observe` mode).
*/
| { readonly type: "allow"; readonly token: SlotToken | null }
| { readonly type: "refuse"; readonly message: string };

Expand Down Expand Up @@ -200,23 +203,30 @@ const make = Effect.gen(function* () {
/**
* Every live held slot: granted in memory and kept in the wait store, one per wait (counted one
* per thread, `onePerThread`). None while waits are off (nothing of the store is read then). A
* store that cannot be read holds none.
* store that cannot be read holds none. Taken now; the store's part is read when the returned
* effect runs, without the used and hidden keys of now (the capacity rule reads it later).
*/
const liveHeld = Effect.fn("SlotGate.liveHeld")(function* (waitsOn: boolean, nowMs: number) {
const heldAt = Effect.fn("SlotGate.heldAt")(function* (waitsOn: boolean, nowMs: number) {
for (const [key, untilMs] of usedKeys) if (untilMs <= nowMs) usedKeys.delete(key);
// A grant never kept nor dropped (a cut-off call) hides the old holds only until they end.
for (const [key, olds] of replacing) {
if (olds.every((old) => old.untilMs <= nowMs)) replacing.delete(key);
}
const memory = yield* Ref.updateAndGet(held, (current) => liveReservations(current, nowMs));
if (!waitsOn) return [];
const fromStore = stored === null ? [] : yield* stored.read;
const hidden = hiddenKeys();
return liveReservations([...memory, ...fromStore], nowMs).filter(
(reservation) => !usedKeys.has(reservation.key) && !hidden.has(reservation.key),
if (!waitsOn) return Effect.succeed<ReadonlyArray<SlotReservation>>([]);
const skipped = new Set([...usedKeys.keys(), ...hiddenKeys()]);
const source = stored;
return (source === null ? Effect.succeed([]) : source.read).pipe(
Effect.map((fromStore) =>
liveReservations([...memory, ...fromStore], nowMs).filter(
(reservation) => !skipped.has(reservation.key),
),
),
);
});

const liveHeld = (waitsOn: boolean, nowMs: number) => Effect.flatten(heldAt(waitsOn, nowMs));

/**
* The live holds that re-asks not kept yet hide, of threads other than `threadId`. They still
* count against their own account (never the machine, where the new hold counts), so a slot
Expand Down Expand Up @@ -245,6 +255,7 @@ const make = Effect.gen(function* () {
startGraceMs: settings.value.agentSlots.startGraceSeconds * 1000,
// Off or without a limit, the gate keeps no records of new starts.
gateRecords: effectiveSlotLimits(settings.value.agentSlots) !== null,
waitsOn: settings.value.waitFor.enabled,
})
: Option.none(),
),
Expand All @@ -253,26 +264,50 @@ const make = Effect.gen(function* () {
/**
* Runs a decision and, in `observe` mode, hands today's answers to the rule beside it with the
* records as they were before the decision changed them. Forked there: never changed, never
* delayed. Inside the semaphore, so observations come in decision order.
* delayed (the wait store's held slots are read by the rule, off this path). Inside the
* semaphore, so observations come in decision order.
*/
const observeBeside = Effect.fn("SlotGate.observeBeside")(function* <A, E, R>(
decision: Effect.Effect<A, E, R>,
answers: (result: A) => ReadonlyArray<"allow" | "refuse">,
/**
* `acquire` only. An allow the gate keeps no record of (`untracked`) gets a token of the
* rule's own, so `started` and `release` reach the rule's record of that start.
*/
caller?: {
readonly threadId: string;
readonly instanceId: string;
readonly untracked: (result: A) => boolean;
readonly withToken: (result: A, token: SlotToken) => A;
},
) {
const observe = yield* observing;
if (Option.isNone(observe)) return yield* decision;
const { startGraceMs, gateRecords, waitsOn } = observe.value;
const atMs = yield* Clock.currentTimeMillis;
const starting = yield* Ref.get(entries);
const heldBefore = yield* Ref.get(held);
const heldBefore = yield* heldAt(waitsOn, atMs);
const result = yield* decision;
// Only a start the gate did not record: never a token of the gate's own (settings changed
// between the two reads).
const ownToken =
!gateRecords && caller !== undefined && caller.untracked(result)
? yield* Ref.getAndUpdate(nextToken, (value) => value + 1)
: null;
yield* observer.observe({
today: answers(result),
atMs,
starting,
held: heldBefore,
...observe.value,
callerThreadId: caller?.threadId,
startGraceMs,
gateRecords,
ownStart:
ownToken === null || caller === undefined
? undefined
: { token: ownToken, instanceId: caller.instanceId, atMs },
});
return result;
return ownToken === null || caller === undefined ? result : caller.withToken(result, ownToken);
});

/** Used up: they stop counting (memory ones go; stored ones are skipped until their end). */
Expand Down Expand Up @@ -319,6 +354,9 @@ const make = Effect.gen(function* () {
),
);

/** A decision and the caller's held slots it used up (typed whole, for `observeBeside`). */
const asDecided = (outcome: SlotAcquire, used: ReadonlyArray<string>) => ({ outcome, used });

/**
* The decision, under the semaphore, and the caller's held slot it used up (if any). The
* caller's own held slots never count against it; they are not a pass past a limit either.
Expand All @@ -333,10 +371,7 @@ const make = Effect.gen(function* () {
? null
: effectiveSlotLimits(settings.value.agentSlots);
if (Option.isNone(settings) || limits === null) {
return {
outcome: { type: "allow", token: null } satisfies SlotAcquire,
used: [] as ReadonlyArray<string>,
};
return asDecided({ type: "allow", token: null }, []);
}
const waitsOn = settings.value.waitFor.enabled;
const nowMs = yield* Clock.currentTimeMillis;
Expand All @@ -357,13 +392,7 @@ const make = Effect.gen(function* () {
freeMemoryBytes: yield* freeMemoryBytes(limits),
});
if (decision.type === "refuse") {
return {
outcome: {
type: "refuse",
message: slotRefusalText(decision, waitsOn),
} satisfies SlotAcquire,
used: [] as ReadonlyArray<string>,
};
return asDecided({ type: "refuse", message: slotRefusalText(decision, waitsOn) }, []);
}
const token = yield* Ref.getAndUpdate(nextToken, (value) => value + 1);
yield* Ref.update(entries, (current) => [
Expand All @@ -372,9 +401,15 @@ const make = Effect.gen(function* () {
]);
// The agent it held a slot for starts now: its held slot is used up, with every older copy.
const used = yield* useUp(reservations, input.callerThreadId);
return { outcome: { type: "allow", token } satisfies SlotAcquire, used };
return asDecided({ type: "allow", token }, used);
},
(decided) => observeBeside(decided, (result) => [result.outcome.type]),
(decided, input) =>
observeBeside(decided, (result) => [result.outcome.type], {
threadId: input.callerThreadId,
instanceId: input.targetInstanceId,
untracked: (result) => result.outcome.type === "allow" && result.outcome.token === null,
withToken: (result, token) => asDecided({ type: "allow", token }, result.used),
}),
// One at a time: the next caller sees the entry the last one added.
semaphore.withPermits(1),
);
Expand Down Expand Up @@ -575,12 +610,13 @@ const make = Effect.gen(function* () {
for (const key of keys) replacing.delete(key);
yield* Ref.update(held, (current) => current.filter((entry) => !keys.includes(entry.key)));
}),
// A token is the gate's or the capacity rule's (`observeBeside`); each ignores the other's.
started: Effect.fn("SlotGate.started")((token: SlotToken | null, childThreadId: ThreadId) =>
token === null
? Effect.void
: Ref.update(entries, (current) =>
current.map((entry) => (entry.token === token ? { ...entry, childThreadId } : entry)),
),
).pipe(Effect.andThen(observer.started(token, childThreadId))),
),
release: Effect.fn("SlotGate.release")(function* (token: SlotToken | null) {
if (token === null) return;
Expand All @@ -594,6 +630,7 @@ const make = Effect.gen(function* () {
: [entry.endedAtMs === undefined ? { ...entry, endedAtMs: nowMs } : entry],
),
);
yield* observer.released(token, nowMs);
}),
});
});
Expand Down
Loading
Loading