> For the complete documentation index, see [llms.txt](https://faction-os.gitbook.io/faction-os-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://faction-os.gitbook.io/faction-os-docs/.spec_system/archive/sessions/phase20-session06-file-mutation-core/code-review.md).

# Code Review and Repair Report

**Session ID**: `phase20-session06-file-mutation-core` **Package**: null **Reviewed**: 2026-06-28 **Scope**: All uncommitted changes in the working tree **Result**: RESOLVED

## Review Surface

**Files reviewed** (all uncommitted changes):

* `.spec_system/state.json` - tracked-modified
* `.spec_system/specs/phase20-session06-file-mutation-core/spec.md` - untracked
* `.spec_system/specs/phase20-session06-file-mutation-core/tasks.md` - untracked
* `.spec_system/specs/phase20-session06-file-mutation-core/implementation-notes.md` - untracked
* `.spec_system/specs/phase20-session06-file-mutation-core/code-review.md` - untracked
* `apps/server/README_server.md` - tracked-modified
* `apps/server/src/managers/fileMutationManager.ts` - untracked
* `apps/server/src/managers/taskExecutionCoordinator.ts` - tracked-modified
* `apps/server/src/routes/terminalContainers.ts` - tracked-modified
* `apps/server/tests/commandCenterManager.test.ts` - tracked-modified
* `apps/server/tests/fileMutationManager.test.ts` - untracked
* `apps/web/src/store/useGameStore.ts` - tracked-modified
* `docs/api/README_api.md` - tracked-modified
* `packages/protocol/src/orchestrationCommandCenter.ts` - tracked-modified
* `packages/protocol/src/rest.ts` - tracked-modified
* `packages/protocol/tests/fileMutation.test.ts` - untracked
* `packages/protocol/tests/orchestrationCommandCenter.test.ts` - tracked-modified

**Inventory commands**: `git status`, `git diff HEAD`, `git diff --cached`, `git ls-files --others --exclude-standard`

## Findings by Severity

### Critical

* No findings.

### High

* `apps/server/src/managers/fileMutationManager.ts:180` - Mutating operations validated `expectedSha256` during prepare, then created a backup and mutated without a final pre-mutation recheck. A file changed between preflight and backup could be overwritten, and a destination file appearing before move/rename could be overwritten by `fs.rename`. | Fix: Added final source hash and destination existence revalidation after backup creation, backup discard on failed revalidation, and backup-read hash checks. | Status: FIXED

### Medium

* `packages/protocol/src/orchestrationCommandCenter.ts:6237` - Rollback backup ids used the generic command-center id grammar, which accepted `.` and `..`; `FileMutationManager.loadBackup` also trusted backup metadata JSON via a type cast. This could address metadata outside the intended `backups/<backup-id>/metadata.json` shape and accept malformed local backup records. | Fix: Added file-mutation backup id validation requiring `backup_...`, applied it to rollback requests, backup metadata, rollback metadata, and file execution attachments, and parsed loaded backup metadata before use. | Status: FIXED

### Low

* No findings.

## Assumptions and Deliberate Non-Fixes

None.

## Behavior Changes

* Unsafe rollback backup ids such as `.` and `..` now return `400 invalid_request`.
* File mutations now fail with `409 conflict` instead of mutating when the source hash or move/rename destination changes after initial preflight.

## Verification

* Tests: `npx vitest run packages/protocol/tests/fileMutation.test.ts apps/server/tests/fileMutationManager.test.ts packages/protocol/tests/orchestrationCommandCenter.test.ts apps/server/tests/commandCenterManager.test.ts` - PASS - 4 files, 42 tests.
* Tests: `npm test` - PASS - 268 files passed, 1 skipped; 3042 tests passed, 1 skipped.
* Linter: `npm run lint` - PASS - Biome checked 764 files.
* Formatter: `npm run format:check` - PASS - Biome checked 762 files.
* Type checker: `npm --workspace @factionos/protocol run typecheck` - PASS.
* Type checker: `npm --workspace @factionos/server run typecheck` - PASS.
* Type checker: `npm run typecheck --workspaces --if-present` - PASS - adapters, server, warroom, web, protocol, and public-website.
* Whitespace: `git diff --check` - PASS.
* Final diff re-read: no remaining issues found.

## Summary

1. Reviewed 17 uncommitted files across spec artifacts, protocol contracts, server manager/tests, integration type narrowing, web store narrowing, docs, and this report.
2. Findings: 0 critical, 1 high, 1 medium, 0 low; all resolved with focused code and regression tests.
3. Deliberately not fixed: none.
4. Verification passed for focused tests, full tests, lint, format, typechecks, and whitespace.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://faction-os.gitbook.io/faction-os-docs/.spec_system/archive/sessions/phase20-session06-file-mutation-core/code-review.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
