Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 4 min read

What a DOCX formatter taught me about validating AI edits

Disclosure: I build KeepFormat, the Word-formatting tool discussed here. This article was drafted by an AI agent from the implementation and test records, with automated checks against those sources. The code snippets be

Disclosure: I build KeepFormat, the Word-formatting tool discussed here. This article was drafted by an AI agent from the implementation and test records, with automated checks against those sources. The code snippets below illustrate the design; they are not a complete document processor.

A formatting brief can look simple: use one font for Chinese text, another for Latin characters, change heading spacing, and keep the body text unchanged. The difficult part starts when those requirements have to be applied to an existing DOCX rather than rendered into a new HTML page.

The most useful architectural decision in this project was to separate three things: interpreting a requirement, authorizing an edit, and executing it.

A plausible explanation is not an executable plan

A model can produce a convincing explanation without providing operations a document processor can safely execute. It can also produce a technically valid operation that targets the wrong paragraph.

The system therefore reads the document into indexed blocks and sends a bounded text excerpt alongside the requirements. The planner proposes structured operations. The server validates those operations against the document service's supported operation catalog and checks targets and source evidence.

That gives the application something concrete to reject. Unknown operations, unsupported parameters and invalid targets should fail validation rather than quietly becoming best-effort edits.

An illustrative boundary looks like this:

const source = await readDocx(file);
const proposal = await proposeChanges(requirements, source.excerpt);

validateOperations(proposal, source.operationCatalog);
validateTargets(proposal, source.blocks);
validateRequirementEvidence(proposal, requirements);
await preflightAgainstOriginal(file, proposal);

return proposal; // The user still needs to review it.

This does not prove the interpretation is correct. It makes the allowed behavior narrower and the remaining uncertainty easier to inspect.

Paragraph positions need context

One concrete defect came from preparing an incomplete excerpt of a longer document. A plan based on only the beginning of the file cannot reliably account for requirements that refer to later sections.

The current context preparation preserves all block positions within the supported limit of 2,000 blocks and a 90 KiB text excerpt. Long paragraph text may be shortened. The interface shows the excerpt and identifies truncation rather than suggesting that the model has seen every character.

The important distinction is between retaining a block's position and retaining its complete text. Neither is a reason to promise that the model understands the entire document.

Approval must refer to the same file and plan

A user can prepare one file, generate a plan, and then choose a different file. Applying the old plan to the replacement would be a serious error even if both documents contain similar headings.

The document hash binds the plan to its source. Changing the source or requirements invalidates the relevant review state. Applying a plan requires explicit confirmation and a matching source document.

The original remains separate from the output. This is useful for ordinary recovery, but it also makes verification practical: source text, tables and media can be compared with the edited result.

A lost response is different from a failed operation

Disabling a button helps prevent repeated clicks. It cannot tell the server whether the first request already completed when the network response disappears.

Each plan request has a stable request ID tied to its input. Recovering an unchanged request reuses that ID. A known failed request can be refunded and a new attempt started explicitly; an ambiguous network outcome should not automatically trigger a fresh paid model request.

For example:

const requestId = getOrCreateIdForCurrentInput();
const result = await submitPlan({ requestId, documentHash, requirements });
// If the response is lost, recover this request rather than inventing a new ID.

Site credits are also distinct from provider billing. Returning a user's site credits cannot undo a model provider's charge for work it already performed. The UI and support messages need to make that distinction clear.

Loading feedback belongs near the action

The first loading indicator existed, but a responsive CSS rule hid its status area on smaller screens. The remaining status text was far below the requirement input. A disabled button consequently looked like an unresponsive page.

The fix was small: a visible indicator on the active button, plus a compact viewport-anchored status for document preparation, AI planning and Word export. A longer wait changes the explanatory text after 15 seconds; it does not invent a completion percentage.

The live region sits outside the busy fieldset, controls remain disabled while the operation runs, and reduced-motion settings disable the animation. Existing finally paths clear the indicator on both success and failure.

Test the wait without paying for another generation

The browser tests intercept gateway requests and hold their responses. That allows the test to inspect the intermediate state instead of only checking the finished page.

The cases include:

  • A visible loader at 320, 390, 1,024 and 1,440 pixels.
  • Repeated clicks while a request is in progress.
  • Preparation and export errors, and a provider timeout.
  • A lost plan response followed by recovery of the same request ID.
  • Reduced motion, slower responses and cleanup after completion.
  • Exporting an existing plan without charging for another plan.

These synthetic tests do not establish model quality or perfect Word rendering. They verify the application's state transitions without issuing paid model calls. The resulting DOCX still needs a final visual review in Word, especially for pagination, fields, unavailable fonts and complex academic layouts.

The implementation is running at KeepFormat. The broader lesson is that an AI document editor needs a reviewable plan and a constrained executor, as well as a model that can interpret the instructions.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.