@miadi/tide 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/annotate.ts +60 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miadi/tide",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Typed client boundary for the ironsilk `tide` runtime. Daemon Unix-socket transport with CLI fallback.",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/annotate.ts CHANGED
@@ -287,9 +287,19 @@ export function buildDeliverArgv(script: string, target: string, doc: string, su
287
287
  * 0.6.0's `--annotate-block` takes no kind and ignores a fourth argument
288
288
  * (measured 2026-09-12). Callers wanting looks_good/delete pass a quote.
289
289
  */
290
- export function buildMarkArgv(bin: string, doc: string, input: Pick<MarkInput, "block" | "quote" | "text" | "kind">): string[] {
290
+ export function buildMarkArgv(
291
+ bin: string,
292
+ doc: string,
293
+ input: Pick<MarkInput, "block" | "quote" | "text" | "kind">,
294
+ occurrence = 1,
295
+ ): string[] {
291
296
  const kind = input.kind ?? "comment"
292
- if (input.quote !== undefined) return [bin, "--annotate", doc, quoteExact(input.quote), input.text, kind]
297
+ if (input.quote !== undefined) {
298
+ const argv = [bin, "--annotate", doc, quoteExact(input.quote), input.text, kind]
299
+ // plannotator-tui marks the first occurrence unless told otherwise.
300
+ if (occurrence > 1) argv.push("--occurrence", String(occurrence))
301
+ return argv
302
+ }
293
303
  if (input.block !== undefined) return [bin, "--annotate-block", doc, String(input.block), input.text]
294
304
  throw new TideError("a mark needs a block index or a quote")
295
305
  }
@@ -299,8 +309,8 @@ function quoteExact(quote: string | QuoteSelector): string {
299
309
  }
300
310
 
301
311
  /**
302
- * plannotator-tui 0.6.0 anchors a quote on its FIRST occurrence
303
- * (`source.find(quote)`). A quote whose context names a later occurrence would
312
+ * For a plannotator-tui without `--occurrence`, which anchors a quote on its
313
+ * FIRST occurrence (`source.find(quote)`). A quote whose context names a later occurrence would
304
314
  * be marked on the wrong sentence, so it is refused with a way forward. A
305
315
  * phrase that appears once, or whose context names the first occurrence,
306
316
  * passes. Ref: jgwill/Miadi#683 (D3).
@@ -317,6 +327,45 @@ export function assertQuoteAnchors(text: string, quote: string | QuoteSelector):
317
327
  }
318
328
  }
319
329
 
330
+ /**
331
+ * Which occurrence of its exact words a quote names, counted as plannotator-tui
332
+ * counts them (1-based, non-overlapping, `str::match_indices`), for
333
+ * `--annotate … --occurrence N`. Missing words and a phrase whose context
334
+ * cannot pick one occurrence are refused with a way forward.
335
+ */
336
+ export function quoteOccurrence(text: string, quote: string | QuoteSelector): number {
337
+ const selector: QuoteSelector = typeof quote === "string" ? { exact: quote } : quote
338
+ const r = resolveQuote(text, selector)
339
+ if ("missing" in r) throw new TideError("those words are not in the reply: select them again inside one block")
340
+ if ("ambiguous" in r) throw new TideError(`these words appear ${r.ambiguous} times in the reply; select a longer phrase`)
341
+ let n = 0
342
+ for (let at = text.indexOf(selector.exact); at !== -1; at = text.indexOf(selector.exact, at + selector.exact.length)) {
343
+ n++
344
+ if (at === r.start) return n
345
+ if (at > r.start) break
346
+ }
347
+ // The words overlap themselves ("haha" in "hahaha") and the match sits between two counted ones.
348
+ throw new TideError("these words overlap another occurrence of themselves; select a longer phrase")
349
+ }
350
+
351
+ /**
352
+ * Whether the installed plannotator-tui takes `--occurrence` (added in
353
+ * miadisabelle/mia-plannotator-tui#1). Its usage text says so. An older binary
354
+ * would ignore the flag and mark the first occurrence, so marks on a later one
355
+ * are refused there instead (`assertQuoteAnchors`).
356
+ */
357
+ const occurrenceSupport = new Map<string, Promise<boolean>>()
358
+ export function plannotatorTakesOccurrence(bin: string): Promise<boolean> {
359
+ let known = occurrenceSupport.get(bin)
360
+ if (!known) {
361
+ known = run([bin, "--usage-probe"], { timeoutMs: 10_000 })
362
+ .then(r => `${r.stdout}\n${r.stderr}`.includes("--occurrence"))
363
+ .catch(() => false)
364
+ occurrenceSupport.set(bin, known)
365
+ }
366
+ return known
367
+ }
368
+
320
369
  /** Pure containment check; `assertVesselDoc` adds symlink resolution. */
321
370
  export function isInsideRoot(doc: string, root: string): boolean {
322
371
  const rel = relative(resolve(root), resolve(doc))
@@ -651,13 +700,18 @@ export async function mark(doc: string, input: MarkInput): Promise<MarkResult> {
651
700
  if (input.quote === undefined && kind !== "comment") {
652
701
  throw new TideError("looks_good and delete marks need a quote — a block mark is always a comment")
653
702
  }
654
- if (input.quote !== undefined) assertQuoteAnchors(await readFile(doc, "utf8"), input.quote)
655
703
  const bin = await resolvePlannotatorBin()
704
+ let occurrence = 1
705
+ if (input.quote !== undefined) {
706
+ const text = await readFile(doc, "utf8")
707
+ if (await plannotatorTakesOccurrence(bin)) occurrence = quoteOccurrence(text, input.quote)
708
+ else assertQuoteAnchors(text, input.quote)
709
+ }
656
710
  const storeRoot = resolveStoreRoot()
657
711
  const before = await findStoreFile(doc, storeRoot)
658
712
  const idsBefore = before ? recordIds(JSON.parse(await readFile(before, "utf8")) as StoreRecord) : new Set<string>()
659
713
 
660
- const r = await run(buildMarkArgv(bin, doc, { ...input, kind }), { timeoutMs: 20_000 })
714
+ const r = await run(buildMarkArgv(bin, doc, { ...input, kind }, occurrence), { timeoutMs: 20_000 })
661
715
  if (r.code !== 0) throw exitError("plannotator-tui --annotate", r)
662
716
 
663
717
  const store = before ?? (await findStoreFile(doc, storeRoot))