tfui latest

PlanFile lifecycle ownership

We decided that cleanup belongs to whoever knows when the data becomes useless — the plan plugin. The exec service is an I/O adapter and must not make lifecycle decisions. The PlanFile type encodes this as an opaque struct with a Cleanup() method: temp files remove themselves, user-provided files no-op. The caller just calls Cleanup() without knowing the policy.

Plan files have two distinct lifecycles: temp files (generated by the plan plugin during a TUI session) and user-provided files (supplied by the user, never deleted). Previously, the exec service unconditionally deleted the plan file after a successful apply — a latent bug for user-provided files that happened to be masked because the CLI seed path never reached that code.

Consequences

  • The exec service’s Apply() no longer calls os.Remove() on the plan file. It only reads from PlanFile.Path().
  • The plan plugin calls PlanFile.Cleanup() in its reset() path, triggered by a chdir or workspace change (ContextChangedEvent beyond a pin toggle), by LockClearedEvent, or by PlanInvalidatedEvent while plan is not showing results.
  • A new plan run removes the temp plan file it supersedes, since that plan can no longer be applied.
  • A successful apply marks plan stale: the plan file stays until plan runs again, plan resets or the program ends.
  • A failed apply leaves the plan file alive while the program runs. Only when plan decides the data is stale does cleanup happen.
  • A temp plan file holds variable values in plaintext, so the plan plugin implements sdk.Shutdowner and its Shutdown() calls PlanFile.Cleanup(). The session calls Shutdown() once on every registered plugin after the BubbleTea program or macro driver stops, when Session.Run or the verb run (RunPluginFrom) returns; the exit code reaches main as an error, so the process exits only after that. On a SIGINT, a SIGTERM or a TUI quit, Shutdown() runs after every terraform process has exited, so it removes the plan file an interrupted plan wrote (ADR-0023). A forced exit (a second signal, or a signal while tfui waits for terraform) skips the wait and still runs Shutdown(). A signal after terraform has finished, while the session writes output or runs Shutdown(), ends tfui once Shutdown() has run. A user-provided plan file is kept.
  • Today only the plan plugin constructs a PlanFile, always a temp file. -plan seeds the plan into the service cache (ADR-0005) without one, and NewUserPlanFile has no caller outside tests.

Considered Options

Exec service owns cleanup (status quo)

The service deletes the file after successful apply. Simple, but wrong — it doesn’t know whether the file was temp or user-provided, and a failed apply followed by a retry would fail with “file not found.”

Apply plugin owns cleanup

Apply consumed the file, so it cleans up. Rejected — apply cannot know if the data is still useful (the user might cancel and retry, or plan might want to show the stale plan). Only plan knows when its output artifact is no longer valid.

Dual-path: service deletes temp, ignores user-provided

Would require the service to understand plan file provenance — an abstraction leak into the I/O layer. The opaque Cleanup() approach keeps the service ignorant of lifecycle policy.

Edit this page on GitHub