fix(server): reconcile Pi native session rewinds #13839
MergedStiensWout(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/13839/headmerged by Yash-Singh1(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- packages/provider-pi/src/server/adapter.ts lines 3171 to 3173Resolvedby StiensWout(GitHub profile, opens in a new tab)@@ -2846,0 +2898,21 @@2898+2899+/**2900+ * Walk pi's active branch from `leafId` towards the root through `entries`,2901+ * stopping at `stopAt`. Returns how many entries it passed, the user entries2902+ * among them (newest first), and whether the walk reached `stopAt`. Leaving a2903+ * partial listing anywhere else means the branch does not descend from `stopAt`.2904+ */2905+function walkPiBranch(2906+ entries: ReadonlyMap<string, PiSessionEntry>,2907+ leafId: string | null,2908+ stopAt: string | null,2909+): {2910+ readonly entryCount: number;2911+ readonly userEntryIds: ReadonlyArray<string>;2912+ readonly reachedStop: boolean;2913+} {2914+ let entryCount = 0;2915+ const userEntryIds: Array<string> = [];2916+ let current = leafId;2917+ while (current !== null && current !== stopAt) {2918+ const entry = entries.get(current);
juliusmarminge added labelsmacroscope-review
juliusmarminge closed this
juliusmarminge added labelstriage:keep-open
juliusmarminge reopened this
- apps/server/src/mcp/OrchestratorMcpService.ts line 1284OutdatedResolvedby StiensWout(GitHub profile, opens in a new tab)@@ -0,0 +1058,21 @@1058+ })1059+ .pipe(Effect.mapError(threadManagementFailure));1060+ const childProjection = {1061+ ...childControls,1062+ messages: [...childControls.messages, ...resultRecords.messages],1063+ turnItems: resultRecords.turnItems,1064+ };1065+ const workState = task.result !== null ? "result_available" : progress.state;1066+ const status =1067+ task.result !== null1068+ ? taskStatusForRun(1069+ task.status === "completed" ||1070+ task.status === "failed" ||1071+ task.status === "cancelled" ||1072+ task.status === "interrupted"1073+ ? { status: task.status }1074+ : childRun,1075+ )1076+ : workState === "result_available"1077+ ? taskStatusForRun(progress.resultRun ?? childRun)1078+ : taskStatusForRun(childRun) === "queued"apps/server/src/project/ProjectService.ts line 460OutdatedResolvedby StiensWout(GitHub profile, opens in a new tab)@@ -0,0 +443,21 @@443+ });444+ if (445+ committed.receipt.threadId !== command.threadId ||446+ committed.receipt.commandType !== command.type447+ ) {448+ return yield* Effect.fail("The thread deletion command ID belongs to a different command.");449+ }450+ if (committed.receipt.status === "rejected") {451+ return yield* Effect.fail(452+ committed.receipt.error ?? "Thread deletion was previously rejected.",453+ );454+ }455+ });456+457+ /** Refuse a non-empty project without force, else delete its live threads first. */458+ const deleteChildThreads = Effect.fn("ProjectService.deleteChildThreads")(function* (459+ input: ProjectDeleteInput,460+ ) {461+ const { projectId } = input;462+ // The V2 shell is the only record of which threads are live.463+ const snapshot = yield* threadProjectionsapps/server/src/project/ProjectService.ts line 397OutdatedResolvedby StiensWout(GitHub profile, opens in a new tab)@@ -0,0 +380,21 @@380+ ...(input.defaultModelSelection === undefined381+ ? {}382+ : { defaultModelSelection: input.defaultModelSelection }),383+ ...(input.autoPull === undefined ? {} : { autoPull: input.autoPull }),384+ ...(input.projectIcon === undefined ? {} : { projectIcon: input.projectIcon }),385+ ...(input.faviconPath === undefined ? {} : { faviconPath: input.faviconPath }),386+ ...(input.defaultThreadEnvMode === undefined387+ ? {}388+ : { defaultThreadEnvMode: input.defaultThreadEnvMode }),389+ ...(input.scripts === undefined ? {} : { scripts: input.scripts }),390+ });391+ if (workspaceRoot !== previousRoot) {392+ yield* projectEnrichment.invalidate([previousRoot, workspaceRoot]);393+ }394+ return yield* readCommitted(input.projectId);395+ },396+ );397+398+ const bootstrap: ProjectService["Service"]["bootstrap"] = Effect.fn("ProjectService.bootstrap")(399+ function* (input) {400+ const existing = yield* getByWorkspaceRoot(input.workspaceRoot);apps/server/src/provider/acp/AcpRegistryAuth.ts line 364OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +344,21 @@344+ ),345+ ),346+ );347+ }),348+ ).pipe(349+ Effect.timeoutOrElse({350+ duration: "60 seconds",351+ orElse: () =>352+ Effect.fail(failure("logout", "The ACP agent did not finish signing out in time.")),353+ }),354+ );355+ const signOut = (356+ Option.isSome(coordinator)357+ ? coordinator.value.withForegroundStartup(options.settings.agentId, logout)358+ : logout359+ ).pipe(Effect.andThen(options.onChanged(false)));360+ return yield* makeProviderAuthFlow({361+ instanceId: options.instanceId,362+ // ACP doesn't advertise its credential scope. Conservatively treat all363+ // instances of the same agent on this environment as sharing credentials.364+ credentialBinding: { owner: "provider", key: `acp:${options.settings.agentId}` },apps/server/src/mcp/WorktreeMcpService.ts line 260OutdatedResolvedby StiensWout(GitHub profile, opens in a new tab)@@ -0,0 +227,21 @@227+ cwd: projectCwd,228+ refName: baseRef,229+ fallbackRemoteName: "origin",230+ })231+ .pipe(asOperationFailed(`Unable to resolve the remote-tracking commit of '${baseRef}'`));232+ worktreeBaseRef = resolvedRemoteBase.commitSha;233+ }234+235+ const ids = yield* handoffIds(scope);236+237+ // uninterruptibleMask: only the potentially slow worktree creation itself238+ // stays interruptible (restore). From the moment it succeeds, through the239+ // binding, continuation queue, setup script launch, and result240+ // construction, there is no interruptible gap: a client cancel can241+ // therefore neither orphan the created worktree before the rollback is242+ // armed, nor skip setting up a worktree the thread was just bound to243+ // (once the binding commits, the scheduled session detach can sever this244+ // request's connection and interrupt the fiber).245+ return yield* Effect.uninterruptibleMask((restore) =>246+ Effect.gen(function* () {247+ const worktree = yield* restore(
StiensWout(GitHub profile, opens in a new tab) changed the title
coderabbitai[bot](GitHub profile, opens in a new tab) approved these changes
Yash-Singh1(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This change adds substantial native-session ancestry reconciliation and can alter persisted run and checkpoint state when Pi rewinds a conversation. It spans the provider adapter, shared event contract, and server orchestration rather than remaining a small local fix.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).