mercury-agent 0.24.3 → 0.24.4-beta.0
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.
- package/README.md +1 -1
- package/container/Dockerfile +6 -1
- package/container/agent-package.json +1 -1
- package/docs/auth/dashboard.md +46 -18
- package/docs/behavior-layers.md +4 -1
- package/docs/configuration.md +31 -4
- package/docs/container-lifecycle.md +2 -2
- package/docs/dashboard.md +3 -3
- package/docs/goals/conversation-review/goal.md +5 -0
- package/docs/goals/whatsapp-bot-hardening/archive/handoff-2026-08-09.md +1 -1
- package/docs/goals/whatsapp-bot-hardening/archive/message-author-attribution.md +1 -1
- package/docs/goals/whatsapp-bot-hardening/archive/setup-plan-2026-08-06.md +5 -5
- package/docs/pending-verification.md +68 -7
- package/docs/setup-whatsapp.md +6 -5
- package/examples/extensions/archive/gc.ts +60 -14
- package/examples/extensions/archive/inflight.ts +36 -3
- package/examples/extensions/archive/manifest.ts +19 -1
- package/examples/extensions/archive/queue.ts +26 -6
- package/examples/extensions/feed-watch/feeds.ts +5 -4
- package/examples/extensions/feed-watch/watch.ts +46 -9
- package/examples/extensions/longview/hook.ts +15 -10
- package/examples/extensions/pinchtab/skill/SKILL.md +12 -35
- package/examples/extensions/web-search/skill/references/zap.md +4 -0
- package/package.json +1 -1
- package/resources/skills/context/SKILL.md +9 -0
- package/src/adapters/whatsapp-db-lookups.ts +23 -0
- package/src/adapters/whatsapp-egress.ts +40 -0
- package/src/adapters/whatsapp-media.ts +2 -0
- package/src/adapters/whatsapp-offline.ts +114 -18
- package/src/adapters/whatsapp.ts +504 -42
- package/src/agent/container-entry.ts +9 -2
- package/src/agent/container-env.ts +23 -11
- package/src/agent/container-runner.ts +6 -1
- package/src/agent/image-contract.ts +31 -59
- package/src/bridges/telegram.ts +243 -53
- package/src/bridges/whatsapp.ts +58 -9
- package/src/cli/mercury.ts +29 -18
- package/src/cli/whatsapp-auth.ts +100 -8
- package/src/config-file.ts +64 -3
- package/src/config.ts +15 -0
- package/src/core/alive-stamp.ts +100 -0
- package/src/core/bidi.ts +49 -0
- package/src/core/commands.ts +9 -1
- package/src/core/dashboard-session.ts +197 -0
- package/src/core/global-admin.ts +1 -1
- package/src/core/handler.ts +7 -3
- package/src/core/media.ts +46 -18
- package/src/core/operator-alerts.ts +376 -92
- package/src/core/reply-context.ts +21 -6
- package/src/core/routes/config-builtin.ts +2 -12
- package/src/core/routes/connections.ts +151 -50
- package/src/core/routes/console.ts +63 -15
- package/src/core/routes/dashboard.ts +142 -20
- package/src/core/runtime.ts +12 -2
- package/src/core/system-messages.ts +121 -28
- package/src/core/task-scheduler.ts +133 -5
- package/src/dashboard/index.html +25 -1
- package/src/env-dump/manifest.ts +1 -0
- package/src/extensions/catalog.ts +2 -2
- package/src/extensions/reserved.ts +10 -2
- package/src/extensions/types.ts +4 -1
- package/src/log-redact.ts +114 -0
- package/src/logger.ts +11 -3
- package/src/main.ts +27 -4
- package/src/preflight/checks/credential.ts +21 -2
- package/src/preflight/checks/extensions.ts +60 -3
- package/src/preflight/checks/image-contract.ts +143 -29
- package/src/server.ts +177 -25
- package/src/smoke/boot.ts +6 -3
- package/src/storage/db.ts +158 -0
- package/src/storage/pi-auth.ts +95 -10
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* In-process claims that close two races in the archive. The first, below, is
|
|
3
|
+
* between erasing a file and archiving it again; the second (`beginSpool`,
|
|
4
|
+
* further down) is between a spool copy and the orphan sweep.
|
|
3
5
|
*
|
|
4
6
|
* `collectHash` reads the manifest, finds no live entry referencing a hash, and
|
|
5
7
|
* then *awaits* the backend delete. A drain tick landing inside that await can
|
|
@@ -26,8 +28,9 @@
|
|
|
26
28
|
* drain leaves its record spooled and retries on the next tick. If the drain
|
|
27
29
|
* appended first, `collectHash` reads that line and declines to delete.
|
|
28
30
|
*
|
|
29
|
-
* This is deliberately not a general mutex.
|
|
30
|
-
* space
|
|
31
|
+
* This is deliberately not a general mutex. Each is a claim on one key inside
|
|
32
|
+
* one space — a hash for the duration of one delete, a record id for the
|
|
33
|
+
* duration of one spool copy — in one process. A deployment
|
|
31
34
|
* running two Mercury processes against one archive would need real locking —
|
|
32
35
|
* and would have larger problems, since the manifest assumes a single writer.
|
|
33
36
|
*/
|
|
@@ -59,7 +62,37 @@ export function isDeleting(space: string, sha256: string): boolean {
|
|
|
59
62
|
return inFlight.has(key(space, sha256));
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
/**
|
|
66
|
+
* The second claim: a spool copy in flight.
|
|
67
|
+
*
|
|
68
|
+
* `spool` awaits its copy into `<id>.blob` and writes `<id>.json` only after
|
|
69
|
+
* it, so for the whole copy the blob sits in the spool with no record beside
|
|
70
|
+
* it — exactly the shape `sweepOrphanBlobs` reads as "a crash inside `spool`".
|
|
71
|
+
* A drain tick landing inside the copy would delete the blob, and the record
|
|
72
|
+
* written a moment later would then be dropped as "blob missing": the
|
|
73
|
+
* deliverable is never archived. Same single-thread argument as above: `spool`
|
|
74
|
+
* claims synchronously before its first await, the sweep checks synchronously
|
|
75
|
+
* before it unlinks, so whichever runs first wins cleanly.
|
|
76
|
+
*/
|
|
77
|
+
const spooling = new Set<string>();
|
|
78
|
+
|
|
79
|
+
/** Claim a record id for the duration of its spool copy. */
|
|
80
|
+
export function beginSpool(space: string, id: string): void {
|
|
81
|
+
spooling.add(key(space, id));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Release a spool claim. Always call this in a `finally`. */
|
|
85
|
+
export function endSpool(space: string, id: string): void {
|
|
86
|
+
spooling.delete(key(space, id));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Whether `spool` is still copying this record's blob. */
|
|
90
|
+
export function isSpooling(space: string, id: string): boolean {
|
|
91
|
+
return spooling.has(key(space, id));
|
|
92
|
+
}
|
|
93
|
+
|
|
62
94
|
/** Test-only: drop every claim. */
|
|
63
95
|
export function resetInFlight(): void {
|
|
64
96
|
inFlight.clear();
|
|
97
|
+
spooling.clear();
|
|
65
98
|
}
|
|
@@ -201,13 +201,31 @@ export function readLines(
|
|
|
201
201
|
* copy of the truth to drift.
|
|
202
202
|
*/
|
|
203
203
|
export function liveAdds(lines: ManifestLine[]): ManifestLine[] {
|
|
204
|
+
const tombstoned = tombstonedIds(lines);
|
|
205
|
+
return lines.filter((l) => l.op === "add" && !tombstoned.has(l.id)).reverse();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The `add` lines a `delete` tombstoned — the exact complement of `liveAdds`
|
|
210
|
+
* among the adds, in file order.
|
|
211
|
+
*
|
|
212
|
+
* Their hashes are bytes this manifest positively condemned: reconcile may
|
|
213
|
+
* collect them as erasures still owed, not as unexplained orphans. Built from
|
|
214
|
+
* the same `tombstonedIds` as `liveAdds` so the two sets cannot drift apart.
|
|
215
|
+
*/
|
|
216
|
+
export function erasedAdds(lines: ManifestLine[]): ManifestLine[] {
|
|
217
|
+
const tombstoned = tombstonedIds(lines);
|
|
218
|
+
return lines.filter((l) => l.op === "add" && tombstoned.has(l.id));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function tombstonedIds(lines: ManifestLine[]): Set<string> {
|
|
204
222
|
const tombstoned = new Set<string>();
|
|
205
223
|
for (const line of lines) {
|
|
206
224
|
if (line.op === "delete" && typeof line.target === "string") {
|
|
207
225
|
tombstoned.add(line.target);
|
|
208
226
|
}
|
|
209
227
|
}
|
|
210
|
-
return
|
|
228
|
+
return tombstoned;
|
|
211
229
|
}
|
|
212
230
|
|
|
213
231
|
/**
|
|
@@ -64,9 +64,9 @@ import {
|
|
|
64
64
|
writeFileSync,
|
|
65
65
|
} from "node:fs";
|
|
66
66
|
import { copyFile, rm } from "node:fs/promises";
|
|
67
|
-
import { join } from "node:path";
|
|
67
|
+
import { dirname, join, resolve } from "node:path";
|
|
68
68
|
import type { ArchiveBackend } from "./backends/types.js";
|
|
69
|
-
import { isDeleting } from "./inflight.js";
|
|
69
|
+
import { beginSpool, endSpool, isDeleting, isSpooling } from "./inflight.js";
|
|
70
70
|
import {
|
|
71
71
|
appendLine,
|
|
72
72
|
MANIFEST_VERSION,
|
|
@@ -141,6 +141,10 @@ export async function spool(
|
|
|
141
141
|
mkdirSync(dir, { recursive: true });
|
|
142
142
|
|
|
143
143
|
const blobCopy = join(dir, `${record.id}.blob`);
|
|
144
|
+
// Claimed before the first await, released after the record lands: while
|
|
145
|
+
// the copy runs the blob has no `.json` beside it, and the orphan sweep must
|
|
146
|
+
// not read that as a crash (see inflight.ts).
|
|
147
|
+
beginSpool(record.space, record.id);
|
|
144
148
|
try {
|
|
145
149
|
// Awaited, not `copyFileSync`. This runs inside the `delivery` hook, on
|
|
146
150
|
// the host, in-process — the same thread that owns every other space's
|
|
@@ -159,6 +163,8 @@ export async function spool(
|
|
|
159
163
|
renameSync(tmp, join(dir, `${record.id}.json`));
|
|
160
164
|
} catch (err) {
|
|
161
165
|
throw erasedMidSpool(err, dir, record);
|
|
166
|
+
} finally {
|
|
167
|
+
endSpool(record.space, record.id);
|
|
162
168
|
}
|
|
163
169
|
}
|
|
164
170
|
|
|
@@ -167,14 +173,26 @@ export async function spool(
|
|
|
167
173
|
* non-crash window, opened by making the copy awaited — into a message that
|
|
168
174
|
* names what happened. Every other error is passed through untouched: nothing
|
|
169
175
|
* is swallowed here, and the `delivery` hook logs whichever comes out.
|
|
176
|
+
*
|
|
177
|
+
* Two shapes are the erasure. The directory is already gone; or it still
|
|
178
|
+
* exists but a file of ours *inside* it vanished. The second is the
|
|
179
|
+
* erasure caught mid-walk: `deleteSpool`'s recursive `rm` is asynchronous, so
|
|
180
|
+
* it can unlink the `.tmp` between `writeFileSync` and `renameSync` before it
|
|
181
|
+
* reaches the directory itself (seen on Linux CI, 2026-10-18). Nothing else
|
|
182
|
+
* removes a `.tmp` or the directory while `spool` runs: the orphan sweep
|
|
183
|
+
* removes only `.blob` files, and skips one whose copy is still in flight
|
|
184
|
+
* (`isSpooling`). A missing *source* (`srcPath`, outside the spool) is
|
|
185
|
+
* neither, and passes through as ENOENT.
|
|
170
186
|
*/
|
|
171
|
-
function erasedMidSpool(
|
|
187
|
+
export function erasedMidSpool(
|
|
172
188
|
err: unknown,
|
|
173
189
|
dir: string,
|
|
174
190
|
record: { space: string; name: string },
|
|
175
191
|
): Error {
|
|
176
|
-
const
|
|
177
|
-
|
|
192
|
+
const e = err as NodeJS.ErrnoException | undefined;
|
|
193
|
+
const ours =
|
|
194
|
+
typeof e?.path === "string" && resolve(dirname(e.path)) === resolve(dir);
|
|
195
|
+
if (e?.code === "ENOENT" && (!existsSync(dir) || ours)) {
|
|
178
196
|
return new Error(
|
|
179
197
|
`the spool for space ${record.space} was erased while ${record.name} was being copied into it — not spooled`,
|
|
180
198
|
);
|
|
@@ -450,7 +468,7 @@ export async function drainSpace(
|
|
|
450
468
|
return result;
|
|
451
469
|
}
|
|
452
470
|
|
|
453
|
-
/** Blob copies with no record beside them — a crash inside `spool`. */
|
|
471
|
+
/** Blob copies with no record beside them and no copy in flight — a crash inside `spool`. */
|
|
454
472
|
function sweepOrphanBlobs(
|
|
455
473
|
dataDir: string,
|
|
456
474
|
spaceId: string,
|
|
@@ -462,6 +480,8 @@ function sweepOrphanBlobs(
|
|
|
462
480
|
if (!name.endsWith(".blob")) continue;
|
|
463
481
|
const id = name.slice(0, -".blob".length);
|
|
464
482
|
if (existsSync(join(dir, `${id}.json`))) continue;
|
|
483
|
+
// Not an orphan: `spool` is mid-copy and writes the record when it ends.
|
|
484
|
+
if (isSpooling(spaceId, id)) continue;
|
|
465
485
|
log.warn(`[archive] removing orphaned spool blob ${name} in ${spaceId}`);
|
|
466
486
|
safeUnlink(join(dir, name), log);
|
|
467
487
|
}
|
|
@@ -655,10 +655,11 @@ export async function fetchFeed(
|
|
|
655
655
|
// character, so a code-unit cap would let through twice what it claims.
|
|
656
656
|
//
|
|
657
657
|
// **Truncated, not rejected.** Throwing here would reach the caller as a
|
|
658
|
-
// *source failure* — backoff, then a retry that fails identically, and
|
|
659
|
-
// warning
|
|
660
|
-
// cap would drop out of the watchlist
|
|
661
|
-
// two of the configured sources are
|
|
658
|
+
// *source failure* — backoff, then a retry that fails identically, and
|
|
659
|
+
// one warning when the streak reaches `FAILURE_WARN_STREAK` (watch.ts).
|
|
660
|
+
// A feed that grew past the cap would drop out of the watchlist
|
|
661
|
+
// permanently behind one log line, and two of the configured sources are
|
|
662
|
+
// already within 2× of it. Cutting the
|
|
662
663
|
// body keeps the newest items flowing; a trailing partial `<item>` has no
|
|
663
664
|
// closing tag and is dropped by the parser rather than half-read.
|
|
664
665
|
const bytes = Buffer.byteLength(body, "utf8");
|
|
@@ -85,6 +85,23 @@ export const BUFFER_CAP = 1500;
|
|
|
85
85
|
export const RUNS_WINDOW_MS = 60 * 60 * 1000;
|
|
86
86
|
/** Backoff caps at this many poll intervals — ~96 min at the default 3 min. */
|
|
87
87
|
export const MAX_BACKOFF_TICKS = 32;
|
|
88
|
+
/**
|
|
89
|
+
* Consecutive failures before a source's streak is logged at `warn`.
|
|
90
|
+
*
|
|
91
|
+
* A single failed poll is not an incident. YouTube channel RSS answers 404
|
|
92
|
+
* intermittently and slow sites time out now and then; on the local bot one
|
|
93
|
+
* week of those single-poll blips was 388 `source failed` / 390
|
|
94
|
+
* `source recovered` warn pairs with almost no real outage behind them. The
|
|
95
|
+
* first failure of a streak is therefore logged at `info`, and the streak is
|
|
96
|
+
* escalated to one `warn` when it reaches this length.
|
|
97
|
+
*
|
|
98
|
+
* With the `2 ** failures` backoff the third failure is the attempt after
|
|
99
|
+
* waits of 2 and 4 intervals: about 18 minutes after the first at the default
|
|
100
|
+
* 180 s poll, so a warn reads as "down for ~20 minutes", not "one blip". A
|
|
101
|
+
* source's reliability over time is read from `source_yield` and its
|
|
102
|
+
* `source_status` row (`failures`, `lastError`), not from the warn count.
|
|
103
|
+
*/
|
|
104
|
+
export const FAILURE_WARN_STREAK = 3;
|
|
88
105
|
|
|
89
106
|
/**
|
|
90
107
|
* A source whose newest item is older than this, while its fetch still
|
|
@@ -712,11 +729,12 @@ function applyLiveness(
|
|
|
712
729
|
return { newestItemAt, silentAt: now };
|
|
713
730
|
}
|
|
714
731
|
if (!stale && wasSilent !== undefined) {
|
|
715
|
-
// Also `warn`, and not because a recovery is a problem: the
|
|
716
|
-
//
|
|
717
|
-
//
|
|
718
|
-
//
|
|
719
|
-
//
|
|
732
|
+
// Also `warn`, and not because a recovery is a problem: the two ends of
|
|
733
|
+
// one streak share a level — as `source failing repeatedly` and the
|
|
734
|
+
// `source recovered` that ends a streak of `FAILURE_WARN_STREAK` or more
|
|
735
|
+
// do — so an operator who greps one end of a streak can find the other.
|
|
736
|
+
// Two levels for two ends of one streak is how a `grep -c` comes back
|
|
737
|
+
// with a number that reads as unresolved incidents.
|
|
720
738
|
ctx.log.warn("feed-watch: source active again", {
|
|
721
739
|
spaceId: ctx.spaceId,
|
|
722
740
|
source: ctx.source,
|
|
@@ -885,7 +903,11 @@ export async function pollSpace(
|
|
|
885
903
|
if (outcome?.ok) {
|
|
886
904
|
fetched++;
|
|
887
905
|
if (prior && prior.failures > 0) {
|
|
888
|
-
|
|
906
|
+
// Same level as the streak's start: a streak that reached
|
|
907
|
+
// `FAILURE_WARN_STREAK` warned, so its end warns too; a blip that
|
|
908
|
+
// was only ever `info` ends at `info`.
|
|
909
|
+
const level = prior.failures >= FAILURE_WARN_STREAK ? "warn" : "info";
|
|
910
|
+
log[level]("feed-watch: source recovered", {
|
|
889
911
|
spaceId,
|
|
890
912
|
source: target.key,
|
|
891
913
|
afterFailures: prior.failures,
|
|
@@ -934,13 +956,28 @@ export async function pollSpace(
|
|
|
934
956
|
newestItemAt: prior?.newestItemAt,
|
|
935
957
|
silentAt: prior?.silentAt,
|
|
936
958
|
};
|
|
937
|
-
// Once per streak. A source that has been 503 for a day
|
|
938
|
-
// write 480 identical
|
|
959
|
+
// Once per streak at each level. A source that has been 503 for a day
|
|
960
|
+
// would otherwise write 480 identical lines and drown everything else
|
|
961
|
+
// in the log; and a one-poll blip is `info`, because a warn on every
|
|
962
|
+
// flap is what buried the real ones (see `FAILURE_WARN_STREAK`).
|
|
939
963
|
if (failures === 1) {
|
|
940
|
-
log.
|
|
964
|
+
log.info("feed-watch: source failed, backing off", {
|
|
965
|
+
spaceId,
|
|
966
|
+
source: target.key,
|
|
967
|
+
error: message,
|
|
968
|
+
failures,
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
if (failures === FAILURE_WARN_STREAK) {
|
|
972
|
+
log.warn("feed-watch: source failing repeatedly", {
|
|
941
973
|
spaceId,
|
|
942
974
|
source: target.key,
|
|
943
975
|
error: message,
|
|
976
|
+
failures,
|
|
977
|
+
lastOkAt:
|
|
978
|
+
prior?.lastOkAt === undefined
|
|
979
|
+
? undefined
|
|
980
|
+
: new Date(prior.lastOkAt).toISOString(),
|
|
944
981
|
});
|
|
945
982
|
}
|
|
946
983
|
}
|
|
@@ -242,9 +242,11 @@ const PARAGRAPH_END = /\r?\n[^\S\r\n]*\r?\n/;
|
|
|
242
242
|
/**
|
|
243
243
|
* Split a reply that opened a block and never closed it into summary and rest.
|
|
244
244
|
*
|
|
245
|
-
* `boundary` says which signal was used
|
|
246
|
-
*
|
|
247
|
-
*
|
|
245
|
+
* `boundary` says which signal was used: an article-start line is evidence
|
|
246
|
+
* about where the *page* begins, a blank line is only a guess. No caller
|
|
247
|
+
* branches on it today — a closed block is re-read only when its closed
|
|
248
|
+
* reading publishes nothing, and then either signal beats no page — but it is
|
|
249
|
+
* the fact a future caller that weighs the two would need.
|
|
248
250
|
*
|
|
249
251
|
* Returns `undefined` when nothing marks where the summary stops — a
|
|
250
252
|
* generation cut off mid-block, which `stripStrayMarkers` handles instead.
|
|
@@ -395,18 +397,21 @@ export function extractAuthorSummary(
|
|
|
395
397
|
// re-reading there would name a cause that is not there, and the over-cap
|
|
396
398
|
// warn already describes what happened.
|
|
397
399
|
//
|
|
398
|
-
// And the closed reading has to be the worse of the two:
|
|
399
|
-
// found the article's own first line, or the closed reading would publish
|
|
400
|
+
// And the closed reading has to be the worse of the two: it would publish
|
|
400
401
|
// nothing, because what it leaves behind never reaches the threshold. A
|
|
401
|
-
//
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
//
|
|
402
|
+
// closer followed by a publishable article is well placed, and the closer is
|
|
403
|
+
// the author's own statement of where the summary stops — the scan exists
|
|
404
|
+
// for the case where that statement is missing. No boundary the scan finds
|
|
405
|
+
// outranks it: a blank line falls inside a heading-less article, and an
|
|
406
|
+
// article-start line can be a `## ` inside a long summary, so trusting
|
|
407
|
+
// either over a well-placed closer moves text between chat and page that
|
|
408
|
+
// the author had already put in the right place. Rerouting is only worth it
|
|
409
|
+
// when there is no page either way.
|
|
405
410
|
const misplacedCloser =
|
|
406
411
|
oversized &&
|
|
407
412
|
reread !== undefined &&
|
|
408
413
|
reread.rest !== rest &&
|
|
409
|
-
|
|
414
|
+
[...rest].length <= threshold;
|
|
410
415
|
|
|
411
416
|
if (match && !misplacedCloser) {
|
|
412
417
|
// An empty block is malformed output, but it is still a block: returning
|
|
@@ -16,10 +16,10 @@ Fast, lightweight browser control for AI agents via HTTP + accessibility tree.
|
|
|
16
16
|
The 30-second pattern for browser tasks:
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
|
-
# 1. Start Pinchtab (
|
|
20
|
-
pinchtab
|
|
19
|
+
# 1. Start Pinchtab (local on :9867) and wait until it is ready
|
|
20
|
+
pinchtab_ensure || { echo "pinchtab failed — see /tmp/pinchtab.log"; exit 1; }
|
|
21
21
|
|
|
22
|
-
# 2.
|
|
22
|
+
# 2. Then follow this loop:
|
|
23
23
|
# a) Navigate to a URL
|
|
24
24
|
# b) Snapshot the page (get refs like e0, e5, e12)
|
|
25
25
|
# c) Act on a ref (click e5, type e12 "search text")
|
|
@@ -31,9 +31,9 @@ pinchtab server &
|
|
|
31
31
|
|
|
32
32
|
## Mercury / Docker (required)
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
**The daemon is started only by `pinchtab_ensure`.** It records the daemon's pid in `$PINCHTAB_PIDFILE` (default `/tmp/pinchtab.pid`), and that record is the only thing a later call uses to clear a stale daemon. A daemon started any other way writes no pidfile, is never cleaned up, and if it breaks while still holding the port, the next `pinchtab_ensure` cannot replace it and fails with `pinchtab died during startup`. Never start `pinchtab server` yourself, in the background or otherwise.
|
|
35
35
|
|
|
36
|
-
`pinchtab_ensure` is an **installed command** (`/usr/local/bin/pinchtab_ensure`, from this extension's `bin/`). Call it; never define, copy or rewrite it — it is not a shell function you need to declare, and a hand-typed copy can drift from the real one. It takes one optional argument, the seconds to wait for the port (default 120).
|
|
36
|
+
`pinchtab_ensure` is an **installed command** (`/usr/local/bin/pinchtab_ensure`, from this extension's `bin/`). Call it; never define, copy or rewrite it — it is not a shell function you need to declare, and a hand-typed copy can drift from the real one. It takes one optional argument, the seconds to wait for the port (default 120). It waits until the port is open and the browser instance is running, and captures the daemon log; the host injects `CHROME_BINARY` and `CHROME_FLAGS` (`--no-sandbox` as root).
|
|
37
37
|
|
|
38
38
|
Each run also gets its own pinchtab agent (`PINCHTAB_AGENT_ID` is set by the host), so it drives its own tab. Do not run `pinchtab session create`.
|
|
39
39
|
|
|
@@ -48,43 +48,20 @@ pinchtab text
|
|
|
48
48
|
|
|
49
49
|
If `pinchtab_ensure` fails, show the user the tail of `/tmp/pinchtab.log`; do not only increase `sleep` and retry blindly.
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
records the daemon's pid in `$PINCHTAB_PIDFILE` (default `/tmp/pinchtab.pid`)
|
|
53
|
-
and that record is the only thing a later call uses to clear a stale daemon. A
|
|
54
|
-
daemon started as a bare `pinchtab server &` — the quick-start above and the
|
|
55
|
-
"Advanced" options below both show that form, which is correct outside Mercury —
|
|
56
|
-
writes no pidfile, so it is never cleaned up and will hold the port against the
|
|
57
|
-
next `pinchtab_ensure`.
|
|
51
|
+
## Daemon options
|
|
58
52
|
|
|
59
|
-
|
|
53
|
+
The daemon reads its options from `BRIDGE_*` environment variables (full list: [references/env.md](references/env.md)). Set them on the `pinchtab_ensure` call — it starts the daemon with the environment it was given:
|
|
60
54
|
|
|
61
55
|
```bash
|
|
62
|
-
#
|
|
63
|
-
|
|
64
|
-
BRIDGE_TOKEN="your-strong-secret" \
|
|
65
|
-
BRIDGE_PROFILE=~/.pinchtab/automation-profile \
|
|
66
|
-
pinchtab server &
|
|
56
|
+
# Read-heavy task: skip images
|
|
57
|
+
BRIDGE_BLOCK_IMAGES=true pinchtab_ensure || { echo "pinchtab failed — see /tmp/pinchtab.log"; exit 1; }
|
|
67
58
|
```
|
|
68
59
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
## Setup
|
|
72
|
-
|
|
73
|
-
```bash
|
|
74
|
-
# Headless (default) — no visible window
|
|
75
|
-
pinchtab server &
|
|
60
|
+
Keep the default port and headless mode inside the container: there is no display for a headed window, and the CLI reaches the daemon through `PINCHTAB_URL` (default `http://localhost:9867`), not `BRIDGE_PORT`, so a daemon moved to another port is one the next `pinchtab nav` cannot find.
|
|
76
61
|
|
|
77
|
-
|
|
78
|
-
BRIDGE_HEADLESS=false pinchtab server &
|
|
79
|
-
|
|
80
|
-
# With auth token
|
|
81
|
-
BRIDGE_TOKEN="your-secret-token" pinchtab server &
|
|
82
|
-
|
|
83
|
-
# Custom port
|
|
84
|
-
BRIDGE_PORT=8080 pinchtab server &
|
|
85
|
-
```
|
|
62
|
+
Options apply only when `pinchtab_ensure` actually starts the daemon: if one is already listening on the port, it is reused as it is.
|
|
86
63
|
|
|
87
|
-
Default: **port 9867**, no auth required (local). Set `BRIDGE_TOKEN`
|
|
64
|
+
Default: **port 9867**, bound to `127.0.0.1`, no auth required (local). Set `BRIDGE_TOKEN` if the API is ever exposed beyond loopback. **Never bind to 0.0.0.0 without a token. Never point `BRIDGE_PROFILE` at a daily Chrome profile.**
|
|
88
65
|
|
|
89
66
|
For advanced setup, see [references/profiles.md](references/profiles.md) and [references/env.md](references/env.md).
|
|
90
67
|
|
|
@@ -90,6 +90,8 @@ curl -s --max-time 15 -o /tmp/zap-list.html \
|
|
|
90
90
|
| sed -nE \
|
|
91
91
|
-e 's/^card-v2--compare".*aria-label="להשוואת מחירים ([^"]*)".*card-v2__price-amount[^"]*">([0-9,]+)<.*stores-link" href="\/model\.aspx\?modelid=([0-9]+)"[^>]*>([^<]*)<.*/\2 ₪ | \4 | https:\/\/www.zap.co.il\/model.aspx?modelid=\3 | \1/p' \
|
|
92
92
|
-e 's/^card-v2--[^"]*".*href="([^"]*)"[^>]*aria-label="לקנייה באתר ([^,"]*), ([^"]*)".*card-v2__price-amount[^"]*">([0-9,]+)<.*/\4 ₪ | חנות אחת: \2 | \1 | \3/p' \
|
|
93
|
+
-e 's/^card-v2--one-store-model".*href="([^"]*)"[^>]*aria-label="למפרט טכני ([^"]*)".*card-v2__store-info[^>]*>[^<]*<a href="\/clientcard\.aspx\?siteid=[0-9]+"[^>]*title="([^"]*)".*card-v2__price-amount[^"]*">([0-9,]+)<.*/\4 ₪ | חנות אחת: \3 | \1 | \2/p' \
|
|
94
|
+
-e 's/^card-v2--one-store-model".*href="([^"]*)"[^>]*aria-label="למפרט טכני ([^"]*)".*card-v2__price-amount[^"]*">([0-9,]+)<.*/\3 ₪ | חנות אחת: לא צוינה | \1 | \2/p' \
|
|
93
95
|
| sed -e 's/&rlm;//g' -e 's/&/\&/g' -e 's/"/"/g' -e "s/'/'/g" -e 's#| /#| https://www.zap.co.il/#' \
|
|
94
96
|
> /tmp/zap-rows.txt
|
|
95
97
|
cat /tmp/zap-rows.txt
|
|
@@ -105,6 +107,8 @@ A listing mixes two kinds of card, and both become rows:
|
|
|
105
107
|
- **One-store offer** (`חנות אחת: <store>`): one store's price, often a promoted card at the top of the
|
|
106
108
|
page (Zap's "zap choice") or a Zap-marketplace box. It is frequently the same product as a
|
|
107
109
|
comparison row, at a different price. Prefer the comparison row for that product.
|
|
110
|
+
Some one-store cards are labelled `למפרט טכני` instead of `לקנייה באתר`; their store comes from
|
|
111
|
+
the card's store link, and `חנות אחת: לא צוינה` means the card named no store at all.
|
|
108
112
|
|
|
109
113
|
Rows come in Zap's popularity order, not by price. Sort them yourself if the user asked for the
|
|
110
114
|
cheapest.
|
package/package.json
CHANGED
|
@@ -19,6 +19,15 @@ mrctl config set context.mode <clear|context> # Switch context strategy
|
|
|
19
19
|
| After next run | Context window returns to normal | Old messages stay excluded forever |
|
|
20
20
|
| Use when | User wants a one-time fresh start ("forget what we just discussed") | User wants a hard reset ("start over", conversation is too long/confused) |
|
|
21
21
|
|
|
22
|
+
## Neither one deletes anything
|
|
23
|
+
|
|
24
|
+
`clear` and `compact` only change what goes into your context. Every earlier
|
|
25
|
+
message stays stored and `mrctl recall` still finds it. After running either,
|
|
26
|
+
say the conversation starts fresh from here — never that the messages were
|
|
27
|
+
deleted, erased or forgotten. If the user asks for their data to be deleted,
|
|
28
|
+
say this command does not do that and that a deletion request goes to the
|
|
29
|
+
space admin.
|
|
30
|
+
|
|
22
31
|
## Context modes
|
|
23
32
|
|
|
24
33
|
Set via `mrctl config set context.mode <value>`:
|
|
@@ -18,6 +18,7 @@ import { logger } from "../logger.js";
|
|
|
18
18
|
import type { Db } from "../storage/db.js";
|
|
19
19
|
import type { WaAliasStore } from "./whatsapp-identity.js";
|
|
20
20
|
import type { MentionCandidate } from "./whatsapp-mentions.js";
|
|
21
|
+
import type { OfflineNoticeStore } from "./whatsapp-offline.js";
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Spelled out rather than left to `listSpaceMembers`' default of 100: a
|
|
@@ -93,3 +94,25 @@ export function createWaMentionCandidates(
|
|
|
93
94
|
});
|
|
94
95
|
};
|
|
95
96
|
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Owed "please resend" notices, kept in `pending_offline_notices` so a boot
|
|
100
|
+
* that dies before its notice settles leaves them for the next one. Live
|
|
101
|
+
* adapter only: a shadow has no WhatsApp socket and owes nobody a notice.
|
|
102
|
+
*/
|
|
103
|
+
export function createWaOfflineNoticeStore(db: Db): OfflineNoticeStore {
|
|
104
|
+
return {
|
|
105
|
+
save: (drop) =>
|
|
106
|
+
db.savePendingOfflineNotice({ ...drop, savedAt: Date.now() }),
|
|
107
|
+
list: (chatJid) =>
|
|
108
|
+
db.listPendingOfflineNotices(chatJid).map((row) => ({
|
|
109
|
+
chatJid: row.chatJid,
|
|
110
|
+
threadId: row.threadId,
|
|
111
|
+
tsMs: row.tsMs,
|
|
112
|
+
text: row.text,
|
|
113
|
+
})),
|
|
114
|
+
clear: (chatJid) => {
|
|
115
|
+
db.deletePendingOfflineNotices(chatJid);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -65,6 +65,46 @@ export function prepareWhatsAppOutbound(
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/** What a media send names in its outbound line. */
|
|
69
|
+
export interface OutboundFileLog {
|
|
70
|
+
filename: string;
|
|
71
|
+
/** `image`, `video`, `audio`, `voice` or `document`: the node it went as. */
|
|
72
|
+
kind: string;
|
|
73
|
+
/** WhatsApp's id for the sent message, when the socket returned one. */
|
|
74
|
+
messageId?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The one `WhatsApp outbound` journal line, for every send that reaches the
|
|
79
|
+
* socket: `postMessage`'s text, the queued replay of it, and each file
|
|
80
|
+
* `sendFiles` puts on the wire. `body` is the prepared wire form of the text
|
|
81
|
+
* or caption that went with it, absent for a file sent bare. One statement,
|
|
82
|
+
* so no send path can go silent again while another keeps logging.
|
|
83
|
+
*/
|
|
84
|
+
export function logWhatsAppOutbound(
|
|
85
|
+
chatJid: string,
|
|
86
|
+
body: { text: string; mentions: string[] } | undefined,
|
|
87
|
+
opts: { file?: OutboundFileLog; replayed?: boolean } = {},
|
|
88
|
+
): void {
|
|
89
|
+
const { file } = opts;
|
|
90
|
+
logger.info("WhatsApp outbound", {
|
|
91
|
+
chatJid,
|
|
92
|
+
...(opts.replayed ? { replayed: true } : {}),
|
|
93
|
+
...(file
|
|
94
|
+
? {
|
|
95
|
+
file: file.filename,
|
|
96
|
+
kind: file.kind,
|
|
97
|
+
captioned: body !== undefined,
|
|
98
|
+
...(file.messageId ? { messageId: file.messageId } : {}),
|
|
99
|
+
}
|
|
100
|
+
: {}),
|
|
101
|
+
...(body ? { preview: body.text.slice(0, 120) } : {}),
|
|
102
|
+
...(body && body.mentions.length > 0
|
|
103
|
+
? { mentionCount: body.mentions.length }
|
|
104
|
+
: {}),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
68
108
|
function resolveMentions(
|
|
69
109
|
text: string,
|
|
70
110
|
opts: PrepareOutboundOptions,
|
|
@@ -651,6 +651,7 @@ export async function downloadWhatsAppMedia(
|
|
|
651
651
|
{
|
|
652
652
|
messageId,
|
|
653
653
|
filename: mediaInfo.filename,
|
|
654
|
+
extension: path.extname(mediaInfo.filename).toLowerCase(),
|
|
654
655
|
mimeType: mediaInfo.mimeType,
|
|
655
656
|
type: mediaInfo.type,
|
|
656
657
|
},
|
|
@@ -812,6 +813,7 @@ export async function downloadQuotedMedia(
|
|
|
812
813
|
{
|
|
813
814
|
messageId,
|
|
814
815
|
filename: mediaInfo.filename,
|
|
816
|
+
extension: path.extname(mediaInfo.filename).toLowerCase(),
|
|
815
817
|
mimeType: mediaInfo.mimeType,
|
|
816
818
|
type: mediaInfo.type,
|
|
817
819
|
},
|