Skip to content

Commit b44997b

Browse files
nullStack65claude
andcommitted
fix(placement): stop counting a reservation once its host reports the run
A placement reserved a lane for a flat 60 seconds on top of the host's fresh count, so sequential launches were refused at about half the cap. Each reservation now records the lane count the host reported when it was placed, and a host counts the larger of its reported lanes and any reservation's lanes plus the reservations made since. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent aa474b0 commit b44997b

2 files changed

Lines changed: 76 additions & 23 deletions

File tree

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,17 +1759,55 @@ it.effect(
17591759
expect(behind.isError).toBe(true);
17601760
expect(JSON.stringify(behind.content)).toContain("no-eligible-environment");
17611761
expect(yield* Ref.get(localLaunches)).toBe(0);
1762+
}),
1763+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1764+
);
17621765

1763-
yield* Ref.set(thread.resources, { ...loadOf(0.1), runningAgents: 18, activeRuns: 2 });
1764-
const idleScopes = yield* launchUnplaced(a, {
1765-
clientRequestId: "idle-scopes",
1766+
it.effect(
1767+
"sequential launches fill every active lane once the host reports them, and launches on one sample cannot share the last",
1768+
() =>
1769+
Effect.scoped(
1770+
Effect.gen(function* () {
1771+
const thread = yield* makeBoxThread;
1772+
const b = yield* serveBox(thread);
1773+
const offers = yield* SubscriptionRef.make<
1774+
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1775+
>([]);
1776+
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1777+
const a = yield* makeLaptop(database, offers, undefined, Effect.void, {
1778+
localLaunch: Effect.void,
1779+
});
1780+
yield* linkTo(a.links, b, "full-access");
1781+
yield* a.settings.updateSettings({
1782+
automaticPlacementHosts: [
1783+
{ environmentId: box.environmentId, maxLanes: 4, minAvailableMemoryGiB: 4 },
1784+
{ environmentId: laptop.environmentId, maxLanes: 0, minAvailableMemoryGiB: 4 },
1785+
],
1786+
});
1787+
const fresh = { type: "worktree", baseRef: "main", startFromOrigin: true };
1788+
for (const active of [0, 1, 2, 3]) {
1789+
yield* Ref.set(thread.resources, {
1790+
...loadOf(0.1),
1791+
runningAgents: 18,
1792+
activeRuns: active,
1793+
});
1794+
const placed = yield* launchUnplaced(a, {
1795+
clientRequestId: `lane-${active}`,
1796+
workspaceStrategy: fresh,
1797+
});
1798+
expect(placed.isError, JSON.stringify(placed.content)).toBe(false);
1799+
expect(placed.structuredContent).toMatchObject({ environmentId: box.environmentId });
1800+
}
1801+
const behind = yield* launchUnplaced(a, {
1802+
clientRequestId: "lane-same-sample",
17661803
workspaceStrategy: fresh,
17671804
});
1768-
expect(idleScopes.isError, JSON.stringify(idleScopes.content)).toBe(false);
1769-
expect(idleScopes.structuredContent).toMatchObject({ environmentId: box.environmentId });
1805+
expect(behind.isError).toBe(true);
1806+
expect(JSON.stringify(behind.content)).toContain("no-eligible-environment");
17701807

1808+
yield* Ref.set(thread.resources, { ...loadOf(0.1), runningAgents: 18, activeRuns: 4 });
17711809
const full = yield* launchUnplaced(a, {
1772-
clientRequestId: "active-full",
1810+
clientRequestId: "lane-full",
17731811
workspaceStrategy: fresh,
17741812
});
17751813
expect(full.isError).toBe(true);

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -503,29 +503,35 @@ const make = Effect.gen(function* () {
503503
);
504504
});
505505

506-
const recentPlacements = new Map<string, ReadonlyArray<number>>();
506+
const recentPlacements = new Map<
507+
string,
508+
ReadonlyArray<{ readonly at: number; readonly lanes: number }>
509+
>();
507510
const recentAt = (environmentId: string, now: number) =>
508-
(recentPlacements.get(environmentId) ?? []).filter((at) => now - at < 60_000);
511+
(recentPlacements.get(environmentId) ?? []).filter((entry) => now - entry.at < 60_000);
512+
const reportedLanes = (resources: HostResourcesSnapshot | null) =>
513+
resources === null ? undefined : (resources.activeRuns ?? resources.runningAgents);
509514
const withRecentPlacements = <
510515
C extends { readonly environmentId: string; readonly resources: HostResourcesSnapshot | null },
511516
>(
512517
candidate: C,
513518
now: number,
514519
): C => {
515-
const recent = recentAt(candidate.environmentId, now).length;
516520
const resources = candidate.resources;
517-
if (recent === 0 || resources === null) return candidate;
518-
if (resources.activeRuns !== undefined)
519-
return {
520-
...candidate,
521-
resources: { ...resources, activeRuns: resources.activeRuns + recent },
522-
};
523-
return resources.runningAgents === undefined
524-
? candidate
525-
: {
526-
...candidate,
527-
resources: { ...resources, runningAgents: resources.runningAgents + recent },
528-
};
521+
const reported = reportedLanes(resources);
522+
if (resources === null || reported === undefined) return candidate;
523+
const recent = recentAt(candidate.environmentId, now);
524+
let expected = reported;
525+
for (let index = recent.length - 1; index >= 0; index--)
526+
expected = Math.max(expected, recent[index]!.lanes + recent.length - index);
527+
if (expected === reported) return candidate;
528+
return {
529+
...candidate,
530+
resources:
531+
resources.activeRuns !== undefined
532+
? { ...resources, activeRuns: expected }
533+
: { ...resources, runningAgents: expected },
534+
};
529535
};
530536

531537
/** The shared choice: this environment and the askable links, from their own answers. */
@@ -573,8 +579,17 @@ const make = Effect.gen(function* () {
573579
candidates.map((candidate) => withRecentPlacements(candidate, now)),
574580
now,
575581
);
576-
if (chosen.accepted)
577-
recentPlacements.set(chosen.environmentId, [...recentAt(chosen.environmentId, now), now]);
582+
const lanes = chosen.accepted
583+
? reportedLanes(
584+
candidates.find((candidate) => candidate.environmentId === chosen.environmentId)
585+
?.resources ?? null,
586+
)
587+
: undefined;
588+
if (chosen.accepted && lanes !== undefined)
589+
recentPlacements.set(chosen.environmentId, [
590+
...recentAt(chosen.environmentId, now),
591+
{ at: now, lanes },
592+
]);
578593
return chosen;
579594
});
580595
yield* Effect.annotateCurrentSpan({

0 commit comments

Comments
 (0)