Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: converge foundation repairs by dimension

[._.]/ Adam Eivyebed6d1f9e65
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 87dcd9f856..861e7f8926 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -40,6 +40,8 @@
- Route foundation findings to the editor that can actually apply them, so a plot synopsis that violates an established world rule is repaired as structure instead of looping through world-bible rewrites that cannot change the episode.
+- Track foundation convergence per owning editor instead of against one global weighted-score high-water mark, so deepening a character can legitimately surface a new structure or craft gap without Autopilot pausing before that newly selected editor gets its first bounded repair attempt.
+
- **A successful agent run is no longer failed just because someone pulled `main` while it worked.** The branch-jack detector — which watches whether a worktree-isolated agent committed into the primary checkout instead of its own worktree — treated *any* movement of the primary as a branch-jack. So an ordinary `git pull` landing an already-merged PR flipped a run that had shipped its own PR into a failure, and told the user to consider `git reset --hard origin/main` over commits that already *were* `origin/main`. The guard now reports what a branch-jack actually leaves behind: commits the branch's upstream does not have, or the checkout parked on a different branch than it started on. A pure fast-forward onto upstream commits is logged as movement and nothing more. When damage is real, the recovery advice is sharper too — it quotes the number of *stranded* commits rather than how far HEAD travelled, and a checkout merely left on the wrong branch with nothing stranded is told to `git checkout` back instead of being pointed at a `--hard` reset that would discard nothing. A branch with no upstream to compare against still reports, since an unpushed commit is unreviewed by definition.
- **Recovering an AI provider now affects the very next routed run.** PortOS accidentally kept two in-memory provider-status services over the same JSON file: the Providers UI updated one while the AI Toolkit's runner consulted the other. A manual “Recover now” therefore looked successful and persisted correctly, yet a run started moments later could still bypass the recovered primary and spend time on a fallback. All legacy callers and the PortOS status routes now delegate to the Toolkit-owned singleton, and provider status socket events have one broadcast path instead of being emitted twice.
diff --git a/server/services/pipeline/seriesAutopilot.test.js b/server/services/pipeline/seriesAutopilot.test.js
index 3dc43630bb..e9e4aeb795 100644
--- a/server/services/pipeline/seriesAutopilot.test.js
+++ b/server/services/pipeline/seriesAutopilot.test.js
@@ -1567,6 +1567,51 @@ describe('autopilot conductor', () => {
expect(applyFoundationFix).toHaveBeenCalledWith(seriesId, 'character', expect.any(Object));
});
+ it('foundation gate: lets a newly surfaced target run before applying divergence patience', async () => {
+ const snap = (weightedScore, scores) => ({
+ seriesId: 'ser-example', status: 'complete', weightedScore,
+ dimensions: {
+ worldbuilding: { score: scores.worldbuilding, gap: 'g', fix: 'f' },
+ character: { score: scores.character, gap: 'g', fix: 'f' },
+ structure: { score: scores.structure, gap: 'g', fix: 'f' },
+ craft: { score: scores.craft, gap: 'g', fix: 'f' },
+ },
+ });
+ judgeFoundation
+ .mockImplementationOnce(async () => snap(7.4, {
+ worldbuilding: 7.4, character: 7.4, structure: 7.4, craft: 7.4,
+ }))
+ .mockImplementationOnce(async () => snap(6.6, {
+ worldbuilding: 7, character: 7, structure: 5, craft: 7,
+ }))
+ .mockImplementationOnce(async () => snap(6.8, {
+ worldbuilding: 7, character: 7, structure: 7, craft: 5,
+ }))
+ .mockImplementationOnce(async () => snap(8, {
+ worldbuilding: 8, character: 8, structure: 8, craft: 8,
+ }));
+
+ const { seriesId } = await seedComplete();
+ await autopilot.startSeriesAutopilot(seriesId, { maxFoundationRounds: 4 });
+ await waitFor(runFinished(seriesId));
+
+ expect(autopilot.__testing.runs.get(seriesId)?.lastPayload?.type).toBe('complete');
+ expect(applyFoundationFix.mock.calls.slice(0, 3).map(([, dimension]) => dimension))
+ .toEqual(['worldbuilding', 'structure', 'craft']);
+ });
+
+ it('foundation gate: still stops after the same target repeatedly fails to improve', async () => {
+ foundationScore = 7;
+ const { seriesId } = await seedComplete();
+ await autopilot.startSeriesAutopilot(seriesId, { maxFoundationRounds: 5 });
+ await waitFor(runFinished(seriesId));
+
+ const last = autopilot.__testing.runs.get(seriesId)?.lastPayload;
+ expect(last).toMatchObject({ type: 'paused', scope: 'foundationGate', pauseKind: 'divergence' });
+ expect(judgeFoundation).toHaveBeenCalledTimes(3);
+ expect(applyFoundationFix).toHaveBeenCalledTimes(2);
+ });
+
it('foundation gate: pauses immediately when the weakest dimension cannot be auto-fixed', async () => {
foundationScore = 3;
foundationFixApplied = false; // owning service can't apply a fix
diff --git a/server/services/pipeline/seriesAutopilot/childRuns.js b/server/services/pipeline/seriesAutopilot/childRuns.js
index 8ca66138f4..ed4f0b1201 100644
--- a/server/services/pipeline/seriesAutopilot/childRuns.js
+++ b/server/services/pipeline/seriesAutopilot/childRuns.js
@@ -269,8 +269,11 @@ export async function runCharacterFoundation(seriesId, record) {
// content-hash-cached, so an unchanged foundation short-circuits (no LLM) and
// can't loop. Bounded; pauses with the residual per-dimension findings on
// non-convergence. Each judge + each fix bills one cos action, budget-gated like
-// the arc loop. The improve-loop convergence is tracked on the weighted score
-// (higher = better), so divergence = the score failing to reach a NEW HIGH.
+// the arc loop. Convergence is tracked PER TARGET DIMENSION: foundation work can
+// legitimately expose a different weak layer (a new antagonist can reveal a
+// structure gap; a repaired structure can expose missing craft). Comparing all
+// of those owned repairs to one global weighted-score high-water mark made a
+// newly surfaced dimension look like divergence before its editor ran once.
export async function runFoundationGate(seriesId, record) {
const maxRounds = Number.isInteger(record.options.maxFoundationRounds)
? record.options.maxFoundationRounds
@@ -289,10 +292,12 @@ export async function runFoundationGate(seriesId, record) {
const model = record.options.modelOverride;
const effort = record.options.effortOverride;
- // Convergence tracker keyed on the weighted score (higher is better) — invert
- // to a "distance below 10" so trackConvergence's fewer-is-better minimum logic
- // applies unchanged (a new low distance = a new high score = progress).
- let convergence = { best: null, sinceBest: 0 };
+ // Each owning editor gets its own convergence history. Invert the target's
+ // raw score to a distance below 10 so trackConvergence's fewer-is-better
+ // minimum logic applies unchanged. A target seen for the first time always
+ // receives at least one repair attempt; repeated no-improvement in that same
+ // dimension still trips the bounded divergence guard.
+ const convergenceByDimension = new Map();
let changed = false;
for (let round = 1; round <= maxRounds; round += 1) {
if (record.cancelRequested) return { canceled: true };
@@ -337,13 +342,16 @@ export async function runFoundationGate(seriesId, record) {
residual: residualFindings(snap.dimensions),
};
}
- // Divergence guard (#1571): bail when fixes stop improving the score.
- const floorDistance = gate.failingDimensions.reduce(
- (total, dimension) => total + gate.dimensionFloor - (snap.dimensions?.[dimension]?.score || 0),
- 0,
+ // Divergence guard (#1571): bail only when repeated repairs fail to improve
+ // THEIR OWN target. A global weighted high-water mark is not comparable
+ // across different targets: improving character can make a previously
+ // latent structure or craft gap judgeable and lower the aggregate score.
+ const targetConvergence = trackConvergence(
+ convergenceByDimension.get(weak.dimension) || { best: null, sinceBest: 0 },
+ 10 - weak.score,
);
- convergence = trackConvergence(convergence, 10 - score + floorDistance);
- if (convergence.sinceBest >= DIVERGENCE_PATIENCE) {
+ convergenceByDimension.set(weak.dimension, targetConvergence);
+ if (targetConvergence.sinceBest >= DIVERGENCE_PATIENCE) {
const floorReason = gate.failingDimensions.length > 0
? `Foundation quality stopped improving with ${gate.failingDimensions.join(', ')} below the ${gate.dimensionFloor} dimension floor. Review those foundations and resume.`
: foundationDivergenceReason(score, threshold, DIVERGENCE_PATIENCE);