Files
reverse-engineering-anything/src/domain/desktop/appTaskDraftItems.ts
T
012861ce1d feat(desktop): draft child-text selectors, member counts and attribute text, and fold in a third batch's findings (#9132)
* fix(desktop): follow a database mapping and own limitation text

A selected database keeps its log when a parent folder moves or the file
sits outside the home. Trash and seed-lock tests compare the owning notice
with the paths, and launch options that are not captured paths stay as written.

* test(desktop): pass the selected log into the draft

The export's selected home paths decide which log stays in the draft.
The line-guard test now names that log instead of relying on its reason.

* fix(desktop): retarget a settings folder that holds captured files

A nested profile directory is a captured path when a seeded file is inside
it. An option value that only resolves under the home stays as written.

* fix(desktop): keep a program word and a directory's neighbor

The program after env stays the program even when a captured file sits in a
folder of that name. A selected directory is not a database, so a neighboring
file keeps the parent folder's spelling.

* fix(desktop): record whether a selected path is a directory

Capture stores the walked path's kind. A neighboring file of a selected
directory keeps the parent folder's spelling even when that directory never
changes and its children are not content roots.

* fix(desktop): mark containing paths without a pairwise scan

A recorded path marks its known ancestors as directories in one walk up
the path, instead of comparing every recorded path with every other.

* fix(desktop): keep a selected folder that is named like a database log

A folder the caller selected stays a selected root when its name ends in
-journal or -wal. A generated log beside a database file still does not.

* fix(desktop): refuse a build command that exceeds the argument vector

Splitting one argument still leaves the whole command under Linux's E2BIG
limit. A request past that budget is refused before the process starts.

* fix(desktop): remap a captured path in an env assignment

The program word stays the program. A NAME=value assignment keeps its name
and follows a captured path value when that path's spelling moves.

* fix(desktop): keep oversized text out of draft guards

A value or line longer than the draft's text bound is not copied into a
guard, so one large frame cannot fill the task by itself.

* docs(desktop): correct dconf and check-preview guidance

A dconf version kept without its bytes is unreadable, not an empty key
list. A values preview is copied into a check only when it is an uncut scalar.

* fix(desktop): keep an empty env value empty

An empty assignment is not a path. Resolving it named the working directory
and rewrote the value when that directory's spelling moved.

* fix(desktop): keep a run's font on the run, not the frame's text

A formatting attribute such as Scribus FONT is not the document's words.
Joining it across runs made a frame-wide text check of ItalicBold that
failed when a run was split. The font stays a value check on its own run.

* fix(desktop): do not pair JSON records by a selector-shaped field

A field named @record_id matched the id suffix and became [@record_id=one],
which reads an XML attribute. Records paired while their pointers matched
nothing. Names that start with @ or > are not id fields.

* fix(desktop): count a save-numbered id once when pairing members

xml:id and draw:id values such as id123456 were both generated and
numbered, so the same selector was counted twice and refused. A text
edit of two elements keyed only by those ids now keeps its check.

* fix(desktop): preserve selected state directory aliases

* fix(desktop): align nested state selections with aliases

* fix(desktop): check build runtime export capability

* fix(desktop): provide complete runtime repack command

* fix(desktop): retain runtime probe failures

* test(desktop): identify capture independently of schema preflight

* fix(desktop): preserve document URLs and value formats

* fix(desktop): bound added checks and omit noisy text guards

* fix(desktop): exclude unseeded locks from task checks

* fix(desktop): preserve draft bounds and launch URLs

* fix(desktop): render draft values using their source format

* fix(desktop): preserve launch URLs and JSON draft values

* fix(desktop): recognize captured file URL documents

* fix(desktop): render captured file URLs for runtime home

* fix(desktop): distinguish phone numbers from save metadata

* fix(desktop): preserve launch URLs and user text

* fix(desktop): preserve captured URLs and user text

* fix(desktop): preserve local file URL authority

* fix(desktop): preserve local file URL authority

* fix(desktop): preserve local file URL authority

* fix(desktop): render captured file URLs at launch

* fix(desktop): classify numeric timestamps using field context

* fix(desktop): classify numeric timestamps using field context

* fix(desktop): classify numeric timestamps using field context

* fix(desktop): retain context for generated draft values

* fix(desktop): retain context for generated draft values

* fix(desktop): retain context for generated draft values

* test(desktop): cover generated IDs in text guards

* test(desktop): cover generated IDs in text guards

* test(desktop): cover generated IDs in text guards

* fix(desktop): isolate launcher URL encoding

* fix(desktop): isolate launcher URL encoding

* fix(desktop): isolate launcher URL encoding

* fix(desktop): isolate launcher URL encoding

* fix(desktop): retain clean text in noisy record fields

* fix(desktop): classify added line values in one pass

* fix(desktop): apply XML quote rules to text drafts

* fix(desktop): distinguish XML attributes from quoted text

* fix(desktop): preserve parsed XML line context

* fix(desktop): retain partial XML syntax facts

* fix(desktop): recognize markdown autolinks in drafts

* fix(desktop): reuse export exclusions in task drafts

* docs(desktop): clarify gzip scrub limits

* style(desktop): format the noisy-record draft files with the pinned oxfmt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(desktop): move launch execution to boundary suite

* fix(desktop): redact build schema failure output

* refactor(desktop): normalize schema failure once

* fix(desktop): omit unsafe schema parser snippets

* fix(desktop): preserve draft change and selected path evidence

* test(desktop): group retained dconf note regressions

* fix(desktop): respect archive entry format in draft checks

* test(desktop): separate unowned-file draft note cases

* fix(desktop): preserve explicitly selected journal documents

* fix(xml): avoid scanning selected ancestors per tag

* fix(desktop): exclude covered text runs from draft counts

* fix: preserve text when drafting desktop checks

* fix: restore nested desktop state aliases

* refactor: share UUID recognition across draft checks

* fix: retain multiline XML attribute context

* fix: retain relationship identifiers in task drafts

* docs: clarify desktop draft identifier handling

* test(desktop): type formatting run cases as pairs

* test(desktop): separate markup boundary regression group

* docs(desktop): clarify task checks before scratch export

* fix(desktop): respell selected paths in file URLs

* test(desktop): group captured document spelling cases

* fix(desktop): bound draft preview serialization

* fix(desktop): restore selected state aliases

* fix(desktop): respect entry format in draft noise

* test(desktop): match exported alias evidence at its JSON boundary

* fix(desktop): retain app state logs in exported seeds

* fix: align desktop numeric seed comparisons

* test(desktop): keep one draft exclusion fixture import

* test(desktop): retain log eligibility across draft test groups

* style: fix desktop launch test formatting

* style: format native command path schema

* fix(desktop): map selected external launch paths

* style(desktop): format seed document regressions

* test(desktop): group launch URL regressions

* fix(desktop): omit redundant nested state aliases

* fix(desktop): compare added free text as a whole

* fix(desktop): check recompressed seeds as shipped

* docs(desktop): clarify recovered seed format

* test(desktop): group shipped gzip seed checks

* fix: retain quoted identifier text in task drafts

* fix: recognize Unicode XML in desktop state readers

* fix(desktop): bound nested draft checks

* fix(desktop): explain missing arrays in truncated views

* fix(desktop): find XML type references in archives

* test(desktop): remove duplicated capture assertions

* fix(desktop): retain nested omitted text boundaries

* style(desktop): format draft value imports

* test(desktop): group model and draft value checks

* fix(desktop): map captured launch paths once

* refactor: remove unused whole-text guard state

* refactor: stop passing unused text children

* test: align free-text checks with whole-note policy

* fix: validate recompressed desktop archive seeds

* style(desktop): format archive seed regression

* fix(desktop): retain placed-item sibling guards

* fix(desktop): redact runtime schema errors

* fix(desktop): normalize captured file URLs

* fix(desktop): report seeded files by capture path

* fix(desktop): retain startup readiness failure stage

* fix(desktop): retain JSON date checks

* fix(desktop): preserve archive formatting checks

* fix(desktop): preserve explicitly selected journal files

* fix(desktop): report build command transport limits

* docs(desktop): clarify unreadable dconf versions

* style(desktop): format capture review fixes

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:56:44 +08:00

140 lines
4.7 KiB
TypeScript

import type { JsonValue } from "../jsonValue.js";
import type { DesktopTaskCheckInput } from "./appTask.js";
import { isStamp } from "./appTaskDraftNoise.js";
import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js";
/** The fewest characters of an item's text a check looks for, and the kept items a draft guards. */
const MIN_TEXT = 3;
const MAX_KEPT = 4;
/** The list a member sits in, and the element holding it. */
const placeOf = (pointer: string) =>
/^(?<owner>.+)\/children\/[^/]+$/u.exec(pointer)?.groups?.owner;
const NUMBER = /^-?\d+(?:\.\d+)?$/u;
/** Whether an element holds a position of its own, numeric `x` and `y`. */
const placed = (element: JsonValue | undefined) =>
isXmlElement(element) &&
["x", "y"].every((key) => NUMBER.test(String(element.attributes[key] ?? "")));
/** A run of a text element. Its `x` and `y` place that run; they do not make it a note of its own. */
const TEXT_RUN = /^(?:tspan|span)$/iu;
/**
* Whether the element at a pointer is an item placed by its own position, as
* Xournal++ places each `<text x="88.3" y="156.5">` note on a layer: it and a
* like sibling hold numeric `x` and `y`. Each is then an item of its own,
* however its tag names text, and never a run of the others' text.
*/
export const isPlacedItem = (
view: JsonValue | null,
pointer: string,
): boolean => {
const element = resolvePointer(view, pointer);
const local = isXmlElement(element)
? (element.name.split(":").at(-1) ?? element.name)
: "";
if (TEXT_RUN.test(local)) return false;
const owner = placeOf(pointer);
const list =
owner === undefined ? undefined : resolvePointer(view, `${owner}/children`);
return (
isXmlElement(element) &&
placed(element) &&
Array.isArray(list) &&
list.some(
(item) =>
item !== element &&
isXmlElement(item) &&
item.name === element.name &&
placed(item),
)
);
};
/** What an item's check reads: a file's two ends, the pointer a check names an element by, and the longest text it compares. */
export interface ItemContext {
readonly file: string;
readonly ends: {
readonly baseline: JsonValue | null;
readonly final: JsonValue | null;
};
readonly name: (pointer: string) => string | undefined;
readonly chars: number;
}
/** The texts of the other members of a tag in a list, in a view, with their places. */
const siblingTexts = (
view: JsonValue | null,
owner: string,
item: JsonValue & { readonly name: string },
) => {
const list = resolvePointer(view, `${owner}/children`);
return (Array.isArray(list) ? list : []).flatMap((sibling, index) =>
sibling !== item && isXmlElement(sibling) && sibling.name === item.name
? [{ text: elementText(sibling).trim(), index }]
: [],
);
};
/**
* The check of an item placed by its own position and added whole: a
* `text_contains` of its text on the element holding the items, which holds
* wherever the user places it, and guards of the text of up to four items
* both ends hold, nearest first; undefined when that element is no check's,
* or the text is short, a time, past the limit, or there at the seed.
*/
export const placedItemCheck = (
context: ItemContext,
pointer: string,
):
| {
readonly element: string;
readonly check: DesktopTaskCheckInput;
readonly guards: readonly DesktopTaskCheckInput[];
}
| undefined => {
const { ends, file } = context;
const owner = placeOf(pointer);
const at = owner === undefined ? undefined : context.name(owner);
const item = resolvePointer(ends.final, pointer);
const value = isXmlElement(item) ? elementText(item).trim() : "";
const seed = at === undefined ? undefined : resolvePointer(ends.baseline, at);
if (
owner === undefined ||
at === undefined ||
!isXmlElement(item) ||
!isXmlElement(seed) ||
value.length < MIN_TEXT ||
value.length > context.chars ||
isStamp(value) ||
elementText(seed).includes(value)
)
return undefined;
const kept = new Set(
siblingTexts(ends.baseline, owner, item).map(({ text }) => text),
);
const list = resolvePointer(ends.final, `${owner}/children`);
const here = Array.isArray(list) ? list.indexOf(item) : 0;
const guards = siblingTexts(ends.final, owner, item)
.filter(({ text }) => text.length >= MIN_TEXT && kept.has(text))
.toSorted(
(left, right) =>
Math.abs(left.index - here) - Math.abs(right.index - here),
)
.slice(0, MAX_KEPT)
.toSorted((left, right) => left.index - right.index)
.map(({ text }) => ({
kind: "text_contains" as const,
file,
pointer: at,
value: text,
}));
return {
element: pointer,
check: { kind: "text_contains", file, pointer: at, value },
guards,
};
};