iOfficeAI/OfficeCLI · error · InvalidOperationException

Resident close reported error (exit {closeResp.ExitCode})

Error message

Resident close reported error (exit {closeResp.ExitCode})

What it means

Thrown by the `close` command when the resident process responded with a non-zero exit code AND an empty Stderr. The resident's shutdown failed (e.g. the backing file vanished mid-session, a final flush could not write). This is the fallback message when the resident gave no detail. Surfaced as InvalidOperationException so data-loss is not silently masked as a successful close.

Source

Thrown at src/officecli/CommandBuilder.cs:88

        var closeFileArg = new Argument<FileInfo>("file") { Description = "Office document path (required even with open/close mode)" };
        var closeCommand = new Command("close", "Flush in-memory changes to disk and stop the resident (releases the file). Use 'save' instead to flush but keep the resident warm. Either is needed before a non-officecli program reads the file; a live resident also auto-flushes shortly after going idle (adaptive 2-10s; see OFFICECLI_RESIDENT_FLUSH: each|auto|<seconds>|off).");
        closeCommand.Add(closeFileArg);
        closeCommand.Add(jsonOption);

        closeCommand.SetAction(result => { var json = result.GetValue(jsonOption); return SafeRun(() =>
        {
            var file = result.GetValue(closeFileArg)!;
            if (ResidentClient.SendCloseWithResponse(file.FullName, out var closeResp))
            {
                // BUG-BT-R26-2: resident may report a non-zero shutdown
                // (e.g. file vanished mid-session → data loss). Bubble
                // that up instead of pretending the close succeeded.
                if (closeResp != null && closeResp.ExitCode != 0)
                {
                    var err = !string.IsNullOrEmpty(closeResp.Stderr)
                        ? closeResp.Stderr
                        : $"Resident close reported error (exit {closeResp.ExitCode})";
                    throw new InvalidOperationException(err);
                }
                // BUG-INTERVIEW-EDIT-R10-B: resident reports advisory warnings
                // (e.g. backing file missing at original path) via Stderr with
                // exit=0. Forward to the client's stderr so the user sees the
                // warning instead of a silent success.
                if (closeResp != null && !string.IsNullOrEmpty(closeResp.Stderr))
                    Console.Error.WriteLine(closeResp.Stderr);
                var msg = $"Resident closed for {file.Name}";
                if (json) Console.WriteLine(OutputFormatter.WrapEnvelopeText(msg));
                else Console.WriteLine(msg);
            }
            else
            {
                // No resident is holding this file. In the non-resident model
                // every mutation already eager-saved to disk, so there is
                // nothing to flush or shut down — treat close as an idempotent
                // no-op SUCCESS, not an error. This lets "edit, then close when
                // done" be a safe habit regardless of whether a resident was

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Confirm the file still exists at the original path the resident was opened with.
  2. Use `save` to flush earlier and more frequently so a close-time failure loses less.
  3. Reopen the document from a known-good copy and reapply the intended edits.
  4. Free disk space / release competing locks, then retry open -> edit -> save -> close.
  5. If reproducible, run close with --json to capture the structured error envelope and inspect ExitCode.
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify the backing file is still present and writable before closing.
if (!File.Exists(path)) throw new FileNotFoundException("backing file missing", path);

Try / catch

try { officecli close file.xlsx; }
catch (InvalidOperationException ex) when (ex.Message.Contains("Resident close reported error"))
{
    // shutdown failed — treat edits since last 'save' as at risk;
    // reopen from a known-good copy and reapply.
    logger.Error("resident close failed: {Msg}", ex.Message);
}

Prevention

When it happens

Trigger: Calling `officecli close deck.xlsx` after the file was deleted, moved, or locked by another process while the resident held it open; disk full during the final flush; antivirus quarantining the file mid-session.

Common situations: Temp/workspace directory cleaned while a resident was warm; user moved or renamed the file in Explorer; concurrent non-officecli writer grabbed an exclusive lock; low-disk or quota exhaustion on the save path.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/d2b101370eb19c5d. Report an issue: GitHub.