Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: filter agent messages locally before sending (#1068)

Gustavo Yea8d9474e10fb
diff --git a/assets/app-store/duo-2007x2853/01-share-agents.jpg b/assets/app-store/duo-2007x2853/01-share-agents.jpg
index 6143b4fce..a64d06578 100644
Binary files a/assets/app-store/duo-2007x2853/01-share-agents.jpg and b/assets/app-store/duo-2007x2853/01-share-agents.jpg differ
diff --git a/assets/app-store/duo-2007x2853/02-teamwork.jpg b/assets/app-store/duo-2007x2853/02-teamwork.jpg
index acfb6575d..65208a8f7 100644
Binary files a/assets/app-store/duo-2007x2853/02-teamwork.jpg and b/assets/app-store/duo-2007x2853/02-teamwork.jpg differ
diff --git a/assets/app-store/duo-2007x2853/03-companion.jpg b/assets/app-store/duo-2007x2853/03-companion.jpg
index e8c4a3a43..6fbed5253 100644
Binary files a/assets/app-store/duo-2007x2853/03-companion.jpg and b/assets/app-store/duo-2007x2853/03-companion.jpg differ
diff --git a/assets/app-store/duo-2007x2853/04-context.jpg b/assets/app-store/duo-2007x2853/04-context.jpg
index ac2509830..f4fc649a8 100644
Binary files a/assets/app-store/duo-2007x2853/04-context.jpg and b/assets/app-store/duo-2007x2853/04-context.jpg differ
diff --git a/assets/app-store/duo-2007x2853/05-phone.jpg b/assets/app-store/duo-2007x2853/05-phone.jpg
index ea3470554..2dc51cdf3 100644
Binary files a/assets/app-store/duo-2007x2853/05-phone.jpg and b/assets/app-store/duo-2007x2853/05-phone.jpg differ
diff --git a/assets/app-store/duo-2007x2853/06-existing-setup.jpg b/assets/app-store/duo-2007x2853/06-existing-setup.jpg
index 15e33dcf8..300ed05ae 100644
Binary files a/assets/app-store/duo-2007x2853/06-existing-setup.jpg and b/assets/app-store/duo-2007x2853/06-existing-setup.jpg differ
diff --git a/assets/app-store/manifest.json b/assets/app-store/manifest.json
index a8a7733b8..244bcf260 100644
--- a/assets/app-store/manifest.json
+++ b/assets/app-store/manifest.json
@@ -1,24 +1,24 @@
{
- "renderer": "156.0.8078.48",
+ "renderer": "155.0.8059.85",
"encoding": "RGB JPEG quality 80",
"clock": "2026-10-25T00:00:00Z",
"locale": "en-US",
"timezone": "UTC",
- "repositoryHead": "05a157cee111881c31108cc14727b38b7276e974",
+ "repositoryHead": "bf829d93a220f3797d1a395712bdde8a7abe73c4",
"inputs": {
"scripts/ci/pnpm-lock.mjs": "c6ad91cbd38cc08234e2da4122079d76bfef5eb61560862018cbab9d3e29f4cc",
"src/shared/src/billing.ts": "53d36e6bbab90d72ae86b9748d97bef4116a7f567d8587067f469d294c562b4a",
"src/shared/src/community-attention.ts": "33f1670a82de25026c3e4437a6324dc57657ceb028cd528d4824d8789cd8a845",
"src/shared/src/community-bot-creation.ts": "b3f3a70550cf35b8e094450d2652879790aab6ef1d1330f91f402a01e2208bc2",
"src/shared/src/community-channel-order.ts": "20a65b60b53bccd2ae962738541ad46cf9a20b08b9f40f379febdbdb6b566cfe",
- "src/shared/src/community-cli-contract.ts": "bb17c61cb7b5399b8cdcab5d0752b133d52f042111f55399014b97e0a3921041",
+ "src/shared/src/community-cli-contract.ts": "7112011a2f11b5c92bdd72a4c365e1a1ae6fec8ca175f9ffcd64df581c63aaab",
"src/shared/src/community-contract.ts": "885285b5706082bdadc4cf34a737582ee0a5a4f19c2dabda8b33e1508e2b9ce1",
"src/shared/src/community-friend-approval.ts": "c44b5e7a73c77c07856cb109ba1c2870994424a12f1341e6f335617589e7b570",
"src/shared/src/community-message-delivery.ts": "3c36f3fa9e8114dbe21a78204d66e88d5a46e3ae1f35c05279acfed92e4e733c",
"src/shared/src/community-resources.ts": "c170f755781cd3b8b5a86e15dc38ff0307cf637b6916732cacea87bb8fff71fc",
"src/shared/src/community-server-rail.ts": "4329a4f594aef47c937e39fcb42d597a9805d246d1151ad2684343100a5d5ef5",
"src/shared/src/community-ws-bundle.ts": "6b0de4a8e3ff35d3b96c7c3ecc1499f8f2ccb837b18d47130a831b9b89f1b607",
- "src/shared/src/community-ws-events.ts": "3bfd5e2e416c15a7341f0efe71338504491cf5834e1ae4027443b63d57efde1d",
+ "src/shared/src/community-ws-events.ts": "22498a1a768ecfdc0a7daf1b17acfff647462e64fc33e69e810a02452255b26e",
"src/shared/src/community/bot-activity-presets.ts": "d2ea578c2852a2b0fe67cfb2bd57c6b69c6edeecbac7e9f5b83a24d002363ef4",
"src/shared/src/community/bot-model.ts": "759e91391a3c3008784dc6f6ee524ed6ed12c3f2d22f79a48699564dcc3d7cb3",
"src/shared/src/community/bot-reasoning-effort.ts": "1c470349ab5c6ec9f111b1dfb5319e624df6c347c7c9b682917010ee69aea9c2",
@@ -45,7 +45,7 @@
"src/shared/src/db/queries/community/attention.ts": "b7af9d4dd91cab62f80b874b92e04706e55410f866e45c32a801a6c4df6bc08a",
"src/shared/src/db/queries/community/bot-audit-log.ts": "4d627a985cc94581ed2dcfd49805d62cf849ad744f28faa315af2bdc8f2aa88b",
"src/shared/src/db/queries/community/bot-creation.ts": "b0b0536fe84e06a11f2aa5e6963c99fa1e2d29f7286d065cc00983a09f392033",
- "src/shared/src/db/queries/community/bot.ts": "d6f9f3f395bc441769a4c378b7163c144b56b01a0c2c89221676c0c91c55071c",
+ "src/shared/src/db/queries/community/bot.ts": "98cf18030c49b3159d2b7fff15d647efcf259f5f91566a8ef1b77385955b240d",
"src/shared/src/db/queries/community/category.ts": "f358a75697cebbf6e471b6d09d6241400cc8db5ac6fefc5e7206bf697daca8f6",
"src/shared/src/db/queries/community/channel.ts": "fe162c4c5c2e2821463dde5aca535ecbf0201c69ebeb8d3aab60729142397a88",
"src/shared/src/db/queries/community/delete-media.ts": "d496aacea7b9ef89672ca6aa1dac78f2652467072da2bd1efaa7d8ff2bb18986",
@@ -98,7 +98,7 @@
"src/shared/src/db/schema.ts": "5ea32d89f510f65ad68e8624aba126dedb7933dd6c0b9007af36b035d7ae1326",
"src/shared/src/diagnostics-contract.ts": "993e65bf8398f3cbd1aff7604652c033f9d410e4d65263a1880b94c8ae40c37c",
"src/shared/src/http-json.ts": "60624ae70c109ab2e6d20e805acee5dd57fd13471e44a8d7444c774f6f4fd8fd",
- "src/shared/src/index.ts": "5700e6e4f1228924e2bda83ece8ae6674bf9f17ae78b1a39d6b0bd207678cc05",
+ "src/shared/src/index.ts": "cedd56d99554bfa7027a68bdd4681d9b9d3dba6f153d03632c3539de42d9d6bf",
"src/shared/src/lib/community-name.ts": "eb0384f04fe7b746c566a090ce0072f17ef4d2b915941df6720eb2d1dd9a48a0",
"src/shared/src/lib/context-key.ts": "e606b9c5564247a7bab957ec610d24fa323145d92d30db023705e63bae3f89ba",
"src/shared/src/lib/discriminator.ts": "d378fb77ab23fd7a15f2b8805c70a3946f374925717092b893364d2971a54a99",
@@ -113,6 +113,7 @@
"src/shared/src/lib/sqlite-binary.ts": "7d85f7b45255a4f4c83aa5c34cc6c94681d0d90dbf556bc991d7f06ad5e5e3ff",
"src/shared/src/logger.ts": "1df81601143b3b50550296f6d3926c89349a38b0c701be3453b3520512b94252",
"src/shared/src/mode.ts": "79003038b9e0a7332e86f10e87da20f394946acfe853bd5af4952b638700f3be",
+ "src/shared/src/pii-contract.ts": "9e36fd0890a3673758c4947507ef644a10f9877ba03a600c13289d5cb53d3780",
"src/shared/src/product-entitlements.ts": "df11f57d543145d922316f64fa3c948a4b14270499a7e0a305667ba6c9fd3f51",
"src/shared/src/provider-telemetry.ts": "7d5bbc33fd3fd290b175ce014456dbab339ea4cde5aebc4839962174f7b49ba3",
"src/shared/src/runtime-config.ts": "3d7f247e8575894d3c65bc73f56816c748d295e60ef4a729bfe2ca794db8e5e0",
@@ -209,7 +210,7 @@
"src/web/src/components/community/channels/channel-unread-indicator.tsx": "643e15bea20dd691a1a13e91164be614240ba1b08b5471f7500793410480cd12",
"src/web/src/components/community/channels/conversation-resolution-error-frame.tsx": "560b1db194115d74a9a3c6bcceefd850952922417aad968d3ea6ac7d18144074",
"src/web/src/components/community/channels/dm-header.tsx": "2455cbc40636bd64a831a1e8a314911e617537a41942112b2d82a64372f04d98",
- "src/web/src/components/community/channels/dm-sidebar.tsx": "e0eef67c827dd04e7404390a7acc834893bfc7c3675d1d59ed22e4ecf86154b4",
+ "src/web/src/components/community/channels/dm-sidebar.tsx": "f0348db29b1fa8eb0c1b20456ddca73746e6379817bb79e139c36b6af52fc6bf",
"src/web/src/components/community/channels/message-header.tsx": "78329c58d1563029e05ade2f530cf33c216e8d9176c49d2a16ff1db3b7748693",
"src/web/src/components/community/channels/sortable-category.tsx": "c0562c000e46283c0186f2ffec179f93bcb9da510215ef0c1cc7fa259faea816",
"src/web/src/components/community/channels/sortable-channel.tsx": "2a2f9523177513cbd5f6f106d9ded0d8254b2762d3e43c880f9920961641063a",
@@ -401,13 +402,14 @@
"src/web/src/hooks/community/community-ws/invalidation-projections.ts": "d948ce7a9c65ae3ce9980cf02aae469c2593e3f01d65efcd15e6ff9e0ad077dc",
"src/web/src/hooks/community/community-ws/membership-events.ts": "c71bc9416b13d69690a4f4e153d51d7ffd3199a1c344fc953e909a0b6c537ce1",
"src/web/src/hooks/community/community-ws/message-events.ts": "780ce7915c80bd678f90e2659e003c5d4fcece85fb7bd9df6bd3e4a9c46fd790",
+ "src/web/src/hooks/community/community-ws/pii-events.ts": "ea7a0f73facfa489c4910646c2fd3cc7e60fc36e9e98daebda4786792c6d6ae6",
"src/web/src/hooks/community/community-ws/presence-machine-events.ts": "d4b9c5cf790aec8677a6ae7e71386d695dc83aee4da623658f797404ed5b7426",
"src/web/src/hooks/community/community-ws/projection-transaction.ts": "5f4b848c4d039fc845b34ea19ae54db73486f0a84e9181ebecb95a57b4a3275e",
"src/web/src/hooks/community/community-ws/reaction-details-invalidation.ts": "33553bfddf9f3e7b35cf6127cce2bb132f8b7c40e519d93cc7c5c74ea7f967c0",
"src/web/src/hooks/community/community-ws/read-state-reconciliation.ts": "0012ff8b076604c722586e579b6af15311f8455b4a849e562c384b4da87e1073",
"src/web/src/hooks/community/community-ws/reconnect-messages.ts": "c2bba29c116268bf043a0c9b01582504f6aadea9f29a1c688c9ab4e64fd51135",
"src/web/src/hooks/community/community-ws/reconnect.ts": "f5337b74e93d3ba396c985adcb822dbc8fc30ce94c1b9c30d45d6940d1a415e2",
- "src/web/src/hooks/community/community-ws/registry.ts": "1675789962173419dd4d38f8156a823f15be7e60ae2ab6ec09dced9a4f4544d3",
+ "src/web/src/hooks/community/community-ws/registry.ts": "68020314e8efa987c4885070d80a567ae33f4683db11ae8d0752b860e47a6ed1",
"src/web/src/hooks/community/community-ws/scope-eviction.ts": "fc96e1b312860f2e8025c6476a0cc9eecc0c52147897908e406642fde9ab3d4c",
"src/web/src/hooks/community/community-ws/server-rail-reconciliation.ts": "0d8e3fff2b64bb4af50b9781a4d47d85f8b74e1c31dbe41a8c8f20c37dcbabf1",
"src/web/src/hooks/community/community-ws/social-events.ts": "7ef9faf5365cbe6a8f09ace818ce33d367574c9085625324c9eb1f23820c3250",
@@ -514,7 +516,7 @@
"src/web/src/lib/community/channel-ref.ts": "3064fb597531cd36c765e9e5f902e5ebdf17a008e7edc6193d370e532f678903",
"src/web/src/lib/community/chat-markdown.ts": "075cc6f1e11d3159324655fd94f70d1d4bb83778994315f1c2ab1713c8428177",
"src/web/src/lib/community/chat-syntax-plugin.ts": "03699b5a6bfcc0cf8678e1bd543eb08a31b33259f102aa3744213748cdb41294",
- "src/web/src/lib/community/community-route.ts": "60ef45b1f9aa576d687fbf5e3c25b51265271bae5269fa6d0f4b8f83e1cb0eef",
+ "src/web/src/lib/community/community-route.ts": "2c1b2323014fbebb594e79cdc1266577a86091ec2a9aaf10d64264f68ab2e06b",
"src/web/src/lib/community/composer-attachment-session.ts": "fdde9849de208e6073e4cf9e85826e4230ef4cc378b6060c7927c7a4ab217207",
"src/web/src/lib/community/composer-draft.ts": "420302eaa26a1ef887bff6d5fdfbe35ee31a49ae44eb12630a53e6bf611468f0",
"src/web/src/lib/community/conversation-entry.ts": "a6061d0c225e2482fd7cf41ef3ac15926a1be2b7a6707c2b82752cfd6ea5f614",
@@ -533,7 +535,7 @@
"src/web/src/lib/community/highlight-range.ts": "469677da44acca865954e53696144f53897b160668016659eddb85e82b43e4b6",
"src/web/src/lib/community/image-crop.ts": "e61d578bff4713ce3d1a919f7555f45551b231c697036c642f9ae10807ba7a1b",
"src/web/src/lib/community/last-channel.ts": "a7a62f33d81442bea061a153241fca75c7d3c584c25a83cb15b3265678e78a50",
- "src/web/src/lib/community/last-community-route.ts": "7d1e89ad1aec7c221c7a7206a42f77631fd04d7907fa578bec6fb369a727cdd2",
+ "src/web/src/lib/community/last-community-route.ts": "1027dd0b8ff6847916d6df1966e22d90f1734b26e5889566c1c1d203fe284a6e",
"src/web/src/lib/community/last-me-location.ts": "f529ec751a9411da73d81a60a1d8d69959311f2163512bd5b186f621f15e3ca9",
"src/web/src/lib/community/list-persistence.ts": "c81dd9f5c040e4536c57964a7059d19e86dde80d814946f7ebe7d8eb6600de43",
"src/web/src/lib/community/machine-name.ts": "ef1c1afbdbe86b081f7f7df94a3b14207a99091813490bf884f802d7991db4c8",
@@ -582,7 +584,7 @@
"src/web/src/lib/observability/clock-tracing.ts": "d35d78c2e2b4ff621e43e3c9bd3fc00e0c7c57d38d6217faf3fa50c2f957c819",
"src/web/src/lib/observability/clock.ts": "46ea2a702a56dc05ccd7a73e61b29fd638c78371fa96387b99e8e202276773a7",
"src/web/src/lib/observability/context.ts": "f1a58fd85dda2ace9a4ab6d0e20f9b25ec692fa30aaaee11058fc58ea447da9a",
- "src/web/src/lib/observability/coverage.ts": "1afd98a0449ed2650a7c2458b0c2a0ece71a0fb16872d86a85552880204080b5",
+ "src/web/src/lib/observability/coverage.ts": "0fea74dad5db5476d494e839906c97da6b236584f2fc2ee35a48201370fcb652",
"src/web/src/lib/observability/document-timing.ts": "36010d41686a732ea04269fa25e937d76a3fdf9d590c826ed4c12bafd4331a2b",
"src/web/src/lib/observability/messages.ts": "c7e81e37e33f19eaa42175bc4859eb1807cc454884e95320bb36b05e5a0e9a09",
"src/web/src/lib/observability/query-observer.ts": "a63d2721c9074c12d051e4af3473886c626d90fac5124ad93378faf3ac7908d4",
@@ -599,7 +601,7 @@
"src/web/src/lib/product-analytics-contract.ts": "104b0a343add78bbaf124488a9cb942cb0f970adc0528f82e9e5446e17baf886",
"src/web/src/lib/product-analytics.ts": "72c3410a8c79b37311200d0ef9c6375deefae038aedc736c414bb8779adf563e",
"src/web/src/lib/query-client.ts": "2fa0ba02e04bd287ff9c1d058a7a685499aa2ab40fb1d41b28872d3c24a9bc17",
- "src/web/src/lib/query-keys.ts": "8a27a19bff002d20ef4d04e67dedb350215b8cabe6c2caea5fe70b5216dcc904",
+ "src/web/src/lib/query-keys.ts": "2668f3d6524d3139866cd2dc375c8d8905c3024d0d398769dba4c7b138850d2e",
"src/web/src/lib/query-persister.ts": "bbe0207b054d66993ea5d741436732eae24a8a9af7fb7335b095ac947ad247bc",
"src/web/src/lib/service-worker/cache-identity.ts": "398f6799d7768f50ed6cc5ac9cd80a60be3383fe292890b1135c4e508e96bdb1",
"src/web/src/lib/service-worker/public-cache-storage.ts": "8857e70e77421e64ae9912fcf285548382c418f5d8d17e46e6280aee80e8a733",
@@ -626,7 +628,7 @@
"path": "phone-1206x2622/01-share-agents.jpg",
"width": 1206,
"height": 2622,
- "sha256": "8ea52c42d5e609ec3356a3c6d14dd6e029019f1760b9e75c4213795590010e43",
+ "sha256": "67c08bcbe2abaf278d95807ee67f0bde4194e1db2a38dffd370a21c3f02a138f",
"surfaces": [
{
"scene": "workspace",
@@ -652,7 +654,7 @@
"path": "phone-1206x2622/02-teamwork.jpg",
"width": 1206,
"height": 2622,
- "sha256": "add088d8ee0e7025b12319633bfbe64531ab660841faa0f8fa2de06c65b245cb",
+ "sha256": "449e517899bf6dca21feb6ba9bb4e396fe2dad45309af91a76dc3daf5288dcb3",
"surfaces": [
{
"scene": "workspace",
@@ -678,7 +680,7 @@
"path": "phone-1206x2622/03-companion.jpg",
"width": 1206,
"height": 2622,
- "sha256": "052587efc5557f46915e6d673f7af1f7527b6b50614e82590caaad94e6b33a49",
+ "sha256": "41992422965debf02f29d3ef5b6f3bb9c1e6a06edb93e2f0ca3872c1ef4cead7",
"surfaces": [
{
"scene": "identity",
@@ -704,7 +706,7 @@
"path": "phone-1206x2622/04-context.jpg",
"width": 1206,
"height": 2622,
- "sha256": "7aff1b04edb171c5379077c092d212cad2e190339394724ce75db3d42ed430ff",
+ "sha256": "2662ad08618ef2a62efa93697b10b3f497990dbade7b93b847b99f1f94f199c3",
"surfaces": [
{
"scene": "continuity",
@@ -739,7 +741,7 @@
"path": "phone-1206x2622/05-phone.jpg",
"width": 1206,
"height": 2622,
- "sha256": "48b911e0b0fd0abf337b6f66cbe85a3cbc83e10b8733a2f95d2030f4ef697f99",
+ "sha256": "9ea0770d0a086284493ecd59430f9568cc646156c77b4806d5e6a70b1403edcb",
"surfaces": [
{
"scene": "workspace",
@@ -765,7 +767,7 @@
"path": "phone-1206x2622/06-existing-setup.jpg",
"width": 1206,
"height": 2622,
- "sha256": "ef7716e477d72e694ae3f9e27ba359507109034474f8bfc20871eef3835b5e6d",
+ "sha256": "14c73e1c6f5fea3c6d02a2b5850077e137531ea1bc5e8260d0bc99466b788a21",
"surfaces": [
{
"scene": "workspace",
@@ -791,7 +793,7 @@
"path": "phone-1320x2868/01-share-agents.jpg",
"width": 1320,
"height": 2868,
- "sha256": "52d8cf32d86474a3ffd6950e9a5b736989f2f306bf9a4fb2326c087450c93787",
+ "sha256": "310b2e3df2ace678b5649543c301add45555f5057bcd9364c823c87c8ac1b6ba",
"surfaces": [
{
"scene": "workspace",
@@ -817,7 +819,7 @@
"path": "phone-1320x2868/02-teamwork.jpg",
"width": 1320,
"height": 2868,
- "sha256": "7bb9b14fdbb91bf0aee6eb645bddf2b4c038e3de4dc6660d97328d71dd7fe6dc",
+ "sha256": "8e3dd3072d5b7828723b47b002c62bdf8fbf7c4e809e0197774e0adfd1750ec5",
"surfaces": [
{
"scene": "workspace",
@@ -843,7 +845,7 @@
"path": "phone-1320x2868/03-companion.jpg",
"width": 1320,
"height": 2868,
- "sha256": "4fbf6cbffb5464768e482637e7d6d5bf838422edcd77eecfa4a364a180083608",
+ "sha256": "42794e949a7997c6d9c4a6fdca9e24396cd41bc310b3b0128dde24d5c0907d0c",
"surfaces": [
{
"scene": "identity",
@@ -869,7 +871,7 @@
"path": "phone-1320x2868/04-context.jpg",
"width": 1320,
"height": 2868,
- "sha256": "58bc2850948f57ba04f74a12ec90654de400b4c327ac4c5b5a3f24b142a89a0b",
+ "sha256": "bd2d061c2f5f443dd6c34760a8b78708bde301a1a653f82a51bd5ae9e605caca",
"surfaces": [
{
"scene": "continuity",
@@ -904,7 +906,7 @@
"path": "phone-1320x2868/05-phone.jpg",
"width": 1320,
"height": 2868,
- "sha256": "270e04dedb164cbf854c5596fcb20461cfb927d616fdc0c67578a0c5af627a2d",
+ "sha256": "48c0436b8a9eb3ba5ba7799183866c698b3e7a87aac8dfaecd280360031465d8",
"surfaces": [
{
"scene": "workspace",
@@ -930,7 +932,7 @@
"path": "phone-1320x2868/06-existing-setup.jpg",
"width": 1320,
"height": 2868,
- "sha256": "c3c1eb9ddb5ac0c30362afed2fb991daf004cf87c0a81c00384a0394c9feeaad",
+ "sha256": "5deac2a6bdb05807993fe721fa194e0bfb5351bdefd4f9683e2b6c680ff9c378",
"surfaces": [
{
"scene": "workspace",
@@ -956,7 +958,7 @@
"path": "tablet-2752x2064/01-share-agents.jpg",
"width": 2752,
"height": 2064,
- "sha256": "fd4725e892edfa23221a34ee95644b1601510c1876406e28f17c4b6b8229cbd4",
+ "sha256": "90579226e073bf7dda3f07671d4c032a215c222de7acb2af4e90cfd6dc211e97",
"surfaces": [
{
"scene": "workspace",
@@ -982,7 +984,7 @@
"path": "tablet-2752x2064/02-teamwork.jpg",
"width": 2752,
"height": 2064,
- "sha256": "6deb347b840adc93c2f638c7e5eec7a685c14682c5a1840ff1f608ed539a78af",
+ "sha256": "b103d966cff5c5595b5cea5f777196e280479b42725b5e9bc7e68fd3378cd88c",
"surfaces": [
{
"scene": "workspace",
@@ -1008,7 +1010,7 @@
"path": "tablet-2752x2064/03-companion.jpg",
"width": 2752,
"height": 2064,
- "sha256": "be6315e33c113a0a346378ef8f0853a1c8616dc0557ca86e2d0ff8aff778ee74",
+ "sha256": "78c9ad75a900f3853b322267545e64ebfe1958ecae42527cf490fed3842ae8f3",
"surfaces": [
{
"scene": "identity",
@@ -1034,7 +1036,7 @@
"path": "tablet-2752x2064/04-context.jpg",
"width": 2752,
"height": 2064,
- "sha256": "236d43a931eed51052dcbfd58462cc64036c0a6d4ea4a02a5a8c3bc368da2562",
+ "sha256": "e2883c7d31f89234810cd9f77b6ad90100691f9e45ffe12a63dd7285403c07c5",
"surfaces": [
{
"scene": "continuity",
@@ -1069,7 +1071,7 @@
"path": "tablet-2752x2064/05-phone.jpg",
"width": 2752,
"height": 2064,
- "sha256": "f5f1998a4efdbae47d10b766e60214e4729f180aa7521e76b14acc1bcb552f96",
+ "sha256": "93673a8d5eeb349eae8c5a7dc447767f1088262b0f275f45e49c11c621ace59a",
"surfaces": [
{
"scene": "workspace",
@@ -1095,7 +1097,7 @@
"path": "tablet-2752x2064/06-existing-setup.jpg",
"width": 2752,
"height": 2064,
- "sha256": "471ee8c0f3bf0ec38c3c174b271ba95df3e8a39cbd218870206569b675394690",
+ "sha256": "71edd998847b991c77a7568183937f44cc4c1ce524dfd66c98d7684e0f5214d9",
"surfaces": [
{
"scene": "workspace",
@@ -1121,7 +1123,7 @@
"path": "duo-2007x2853/01-share-agents.jpg",
"width": 2007,
"height": 2853,
- "sha256": "d366591ea67a9fb80bcb956537fb90db5e1431ca012b3e7992d1873880c6e84e",
+ "sha256": "41ba4dcb99285c74b855f1e824ac0a56413c101e5832e22dcbc0b4e3e4c70910",
"surfaces": [
{
"scene": "workspace",
@@ -1147,7 +1149,7 @@
"path": "duo-2007x2853/02-teamwork.jpg",
"width": 2007,
"height": 2853,
- "sha256": "04bf417c53b92af207b5e2fc3a42e45e529fa2014b16a7d0714635034870665e",
+ "sha256": "913c19fe269aac05993ddfd9c049ba281e1b9a827c7fdf508c72e2934722701f",
"surfaces": [
{
"scene": "workspace",
@@ -1173,7 +1175,7 @@
"path": "duo-2007x2853/03-companion.jpg",
"width": 2007,
"height": 2853,
- "sha256": "aba2c5f88993e61c952270d853a23c4c6c3c7fdfa77d7dbd3531247eac696018",
+ "sha256": "627afc8590c0a790cd16e983d1e4f63125c975beff61a94b6bd9e8e0d5c15256",
"surfaces": [
{
"scene": "identity",
@@ -1199,7 +1201,7 @@
"path": "duo-2007x2853/04-context.jpg",
"width": 2007,
"height": 2853,
- "sha256": "ef6a3ea747bf3d90fe85dd99522be3f9c8ff80ba7b85014baf87abd4d719dc7c",
+ "sha256": "e07f2aa40054fb36b53298e65028df5ffd14bf622495acf28dbe1dab1b615e02",
"surfaces": [
{
"scene": "continuity",
@@ -1234,7 +1236,7 @@
"path": "duo-2007x2853/05-phone.jpg",
"width": 2007,
"height": 2853,
- "sha256": "073a43adf519295d84cf19faac7441e595f64083b5a4be54b21e285884fabc5f",
+ "sha256": "611a8268b8ed0cbde480d10cf428406a45f6d04ea129d5d627582d0be4df69b5",
"surfaces": [
{
"scene": "workspace",
@@ -1260,7 +1262,7 @@
"path": "duo-2007x2853/06-existing-setup.jpg",
"width": 2007,
"height": 2853,
- "sha256": "59728cd89412df2f095cbcf18bc25770be07c398715c251c4db52f2f46409e18",
+ "sha256": "610f42c74b1cfcbdaa8a11f011c84ffdccbbb03166c4e39ff758529a98769f49",
"surfaces": [
{
"scene": "workspace",
diff --git a/assets/app-store/phone-1206x2622/01-share-agents.jpg b/assets/app-store/phone-1206x2622/01-share-agents.jpg
index f919d92a7..583dc69cd 100644
Binary files a/assets/app-store/phone-1206x2622/01-share-agents.jpg and b/assets/app-store/phone-1206x2622/01-share-agents.jpg differ
diff --git a/assets/app-store/phone-1206x2622/02-teamwork.jpg b/assets/app-store/phone-1206x2622/02-teamwork.jpg
index 2cf01610a..3228858c4 100644
Binary files a/assets/app-store/phone-1206x2622/02-teamwork.jpg and b/assets/app-store/phone-1206x2622/02-teamwork.jpg differ
diff --git a/assets/app-store/phone-1206x2622/03-companion.jpg b/assets/app-store/phone-1206x2622/03-companion.jpg
index 2acf61dde..9a5df2a01 100644
Binary files a/assets/app-store/phone-1206x2622/03-companion.jpg and b/assets/app-store/phone-1206x2622/03-companion.jpg differ
diff --git a/assets/app-store/phone-1206x2622/04-context.jpg b/assets/app-store/phone-1206x2622/04-context.jpg
index 33d8fd47b..30c4f4f5e 100644
Binary files a/assets/app-store/phone-1206x2622/04-context.jpg and b/assets/app-store/phone-1206x2622/04-context.jpg differ
diff --git a/assets/app-store/phone-1206x2622/05-phone.jpg b/assets/app-store/phone-1206x2622/05-phone.jpg
index 4370a3589..7ccbf89e4 100644
Binary files a/assets/app-store/phone-1206x2622/05-phone.jpg and b/assets/app-store/phone-1206x2622/05-phone.jpg differ
diff --git a/assets/app-store/phone-1206x2622/06-existing-setup.jpg b/assets/app-store/phone-1206x2622/06-existing-setup.jpg
index ff8201b1c..7db7cd1b1 100644
Binary files a/assets/app-store/phone-1206x2622/06-existing-setup.jpg and b/assets/app-store/phone-1206x2622/06-existing-setup.jpg differ
diff --git a/assets/app-store/phone-1320x2868/01-share-agents.jpg b/assets/app-store/phone-1320x2868/01-share-agents.jpg
index 0b557dbf8..821306e5d 100644
Binary files a/assets/app-store/phone-1320x2868/01-share-agents.jpg and b/assets/app-store/phone-1320x2868/01-share-agents.jpg differ
diff --git a/assets/app-store/phone-1320x2868/02-teamwork.jpg b/assets/app-store/phone-1320x2868/02-teamwork.jpg
index 031fc9551..6a5af944b 100644
Binary files a/assets/app-store/phone-1320x2868/02-teamwork.jpg and b/assets/app-store/phone-1320x2868/02-teamwork.jpg differ
diff --git a/assets/app-store/phone-1320x2868/03-companion.jpg b/assets/app-store/phone-1320x2868/03-companion.jpg
index efe986ea5..7c8320abb 100644
Binary files a/assets/app-store/phone-1320x2868/03-companion.jpg and b/assets/app-store/phone-1320x2868/03-companion.jpg differ
diff --git a/assets/app-store/phone-1320x2868/04-context.jpg b/assets/app-store/phone-1320x2868/04-context.jpg
index 04983d0c2..da30d6a9d 100644
Binary files a/assets/app-store/phone-1320x2868/04-context.jpg and b/assets/app-store/phone-1320x2868/04-context.jpg differ
diff --git a/assets/app-store/phone-1320x2868/05-phone.jpg b/assets/app-store/phone-1320x2868/05-phone.jpg
index 8c5bc4311..e963dcec8 100644
Binary files a/assets/app-store/phone-1320x2868/05-phone.jpg and b/assets/app-store/phone-1320x2868/05-phone.jpg differ
diff --git a/assets/app-store/phone-1320x2868/06-existing-setup.jpg b/assets/app-store/phone-1320x2868/06-existing-setup.jpg
index aab96b9d5..6e1e256b9 100644
Binary files a/assets/app-store/phone-1320x2868/06-existing-setup.jpg and b/assets/app-store/phone-1320x2868/06-existing-setup.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/01-share-agents.jpg b/assets/app-store/tablet-2752x2064/01-share-agents.jpg
index 69e1be68f..e857de6ec 100644
Binary files a/assets/app-store/tablet-2752x2064/01-share-agents.jpg and b/assets/app-store/tablet-2752x2064/01-share-agents.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/02-teamwork.jpg b/assets/app-store/tablet-2752x2064/02-teamwork.jpg
index a5a424908..1b49ca210 100644
Binary files a/assets/app-store/tablet-2752x2064/02-teamwork.jpg and b/assets/app-store/tablet-2752x2064/02-teamwork.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/03-companion.jpg b/assets/app-store/tablet-2752x2064/03-companion.jpg
index 4a3ba7329..7cc4be57a 100644
Binary files a/assets/app-store/tablet-2752x2064/03-companion.jpg and b/assets/app-store/tablet-2752x2064/03-companion.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/04-context.jpg b/assets/app-store/tablet-2752x2064/04-context.jpg
index f18341fa4..34c59e3e3 100644
Binary files a/assets/app-store/tablet-2752x2064/04-context.jpg and b/assets/app-store/tablet-2752x2064/04-context.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/05-phone.jpg b/assets/app-store/tablet-2752x2064/05-phone.jpg
index 77b0998eb..915657df6 100644
Binary files a/assets/app-store/tablet-2752x2064/05-phone.jpg and b/assets/app-store/tablet-2752x2064/05-phone.jpg differ
diff --git a/assets/app-store/tablet-2752x2064/06-existing-setup.jpg b/assets/app-store/tablet-2752x2064/06-existing-setup.jpg
index 43bdb99f2..801f53306 100644
Binary files a/assets/app-store/tablet-2752x2064/06-existing-setup.jpg and b/assets/app-store/tablet-2752x2064/06-existing-setup.jpg differ
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d06b5063d..06cd69003 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -87,9 +87,15 @@ importers:
commander:
specifier: ^15.0.0
version: 15.0.0
+ quickjs-emscripten:
+ specifier: ^0.32.0
+ version: 0.32.0
sharp:
specifier: ^0.35.5
version: 0.35.5(@types/[email protected])
+ sucrase:
+ specifier: ^3.35.1
+ version: 3.35.1
ws:
specifier: ^8.22.0
version: 8.22.0
@@ -121,6 +127,9 @@ importers:
vitest:
specifier: 4.1.12
+ zod:
+ specifier: ^4.6.7
+ version: 4.6.7
src/daemon/agent-driver:
devDependencies:
@@ -1887,6 +1896,21 @@ packages:
resolution: {integrity: sha512-+Sg6GCR/wy1oSmQDFq4LQDAhm3ETKnorxN+y5nbLULOR3P0c14f2Wurzj3/xqPXtasLFfHd5iRFQ7AJt4KH2cw==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-v9T+GQpmk43VDJ7d72sf0Nexhk+ArvtUihW27dy7lqAl0zBObFKtSBBIm5RBjwIhE8VwsPPm9PNuvPvNqLWUEg==}
+
+ resolution: {integrity: sha512-EX8zbXwGqCgAE764M+qvkHtyXDi/FUoMBea0JnES7vCM3P7a2+EOZOjGv85wtZ2sJhI1oJ+nekmqpOODFDY+hw==}
+
+ resolution: {integrity: sha512-LeYWrPGC1uNCTBWvibo3ZLJj0CSVNYUXvJpXMCmuQ5Sap2cCACc3uvGvYV4homHHBAzfw5akoTqMMS4YFRtw+Q==}
+
+ resolution: {integrity: sha512-3oSwPfja12ICz4aIblB58cuY8JlEq5Txt8Cut4VLo+LH47QN+mzCnSgnbB03hWzg1LBcc+VyyI9UOag7a1NF+Q==}
+
+ resolution: {integrity: sha512-BKNDI/TPBfGlLNGYpLrhcDGXmIk4xHm4MRAisOBnOzpXVn9HZWsfmMAc9WMBrAHjvvds6HOikKeaOBKdPdpVrg==}
+
'@jridgewell/[email protected]':
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
@@ -4502,6 +4526,9 @@ packages:
resolution: {integrity: sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==}
engines: {node: '>=12'}
+ resolution: {integrity: sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==}
+
resolution: {integrity: sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==}
@@ -5017,6 +5044,10 @@ packages:
resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==}
+ resolution: {integrity: sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==}
+ engines: {node: '>= 6'}
+
resolution: {integrity: sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==}
engines: {node: '>= 10'}
@@ -7056,6 +7087,9 @@ packages:
resolution: {integrity: sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==}
engines: {node: '>= 12.0.0'}
+ resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
+
resolution: {integrity: sha512-P8aEP5U/D1/IlTY2OeYsErdwh9bGuLE30NcXtKEjgdHcahveQoQwM2yZNsioQHsWFz0P7KKudisbrzCgR0sDHg==}
@@ -7540,6 +7574,9 @@ packages:
resolution: {integrity: sha512-dkEJPVvun4FryqBmZ5KhDo0K9iDXAwn08tMLDinNdRBNPcYEDiWYysLcc6k3mjTMlbP9KyylvRpd4wFtwrT9rw==}
engines: {node: ^20.17.0 || >=22.9.0}
+ resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==}
+
resolution: {integrity: sha512-bzlKTyNJ7+LdGIIwy8ijFpIqEQIvafahV7eYykJ8Cvh42EdJeODoJ6gUJXpQJvej1BddH8OqTXZNE/KfbWAu8Q==}
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
@@ -7935,6 +7972,10 @@ packages:
resolution: {integrity: sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==}
engines: {node: '>=12'}
+ resolution: {integrity: sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA==}
+ engines: {node: '>= 6'}
+
resolution: {integrity: sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ==}
engines: {node: '>=16.20.0'}
@@ -8110,6 +8151,13 @@ packages:
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
+ resolution: {integrity: sha512-QFnPfjFey8EqknSrSxe1hZrf1/8z7/6s1QzGOmKo6++02r7QRRX7ZoyNaZh7JuVjWsVW87KnQrbZqnHkOAzUyg==}
+
+ resolution: {integrity: sha512-So0Sqw869y/S2oE3Nuc0uT3Dhqgvsj8FSrwBdsuTosVsG8ME5/OcudU1GxsrIFdFABgy17GHnTVO9TYV/bLQcA==}
+ engines: {node: '>=16.0.0'}
+
resolution: {integrity: sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==}
@@ -8866,6 +8914,11 @@ packages:
resolution: {integrity: sha512-5Z9ZpRzfuH6l/UAvCPAPUo3665Nk2wLaZU3x+TLHKVzIz33+sbJqbtrYoC3KD4/uVOr2Zp+L0LySezP9OHV9yA==}
+ resolution: {integrity: sha512-DhuTmvZWux4H1UOnWMB3sk0sbaCVOoQZjv8u1rDoTV0HTdGem9hkAZtl4JZy8P2z4Bg0nT+YMeOFyVr4zcG5Tw==}
+ engines: {node: '>=16 || 14 >=14.17'}
+ hasBin: true
+
resolution: {integrity: sha512-H+ue8Zo4vJmV2nRjpx86P35lzwDT3nItnIsocgumgr0hHMQ+ZGq5vrERg9kJBo5AWGmxZDhzDo+WVIJqkB0cGA==}
engines: {node: '>=16'}
@@ -8937,6 +8990,13 @@ packages:
resolution: {integrity: sha512-vlLytXkeP4xvEq2otHeJfSQIRyWxo/oZGEbXrtEEF9Hnmrdly59sUbzZ/QgyWuLYHctCHxFF4tRQZNQ9k60ExQ==}
+ resolution: {integrity: sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==}
+ engines: {node: '>=0.8'}
+
+ resolution: {integrity: sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==}
+
resolution: {integrity: sha512-oB7yIimd8SuGptespDAZnNkzIz+NWaJCu2RMsbs4Wmp9zSDUM8Nhi3s2OOcqYuv3mN4hitXc8DVx+LyUmbUDiA==}
@@ -9027,6 +9087,9 @@ packages:
resolution: {integrity: sha512-JfJeIHke7y2egdGGgRAvpCwYFUsHlM2gPcrVOxFkznt/4uzQ7HFmvE63iFHVLBJNDuyDOQgijDK/tXH/f6Msjg==}
engines: {node: '>=6.10'}
+ resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}
+
resolution: {integrity: sha512-ztMO++owQnz8c/gIENcM9XfCEzgoGphTv+nKpYNM1bgsdOVC/jRZuEBf6N+mLLDNg68Kl+GgUZfOySaRiG1/Ug==}
@@ -11186,6 +11249,24 @@ snapshots:
'@istanbuljs/[email protected]': {}
+ '@jitl/[email protected]': {}
+
+ dependencies:
+ '@jitl/quickjs-ffi-types': 0.32.0
+
+ dependencies:
+ '@jitl/quickjs-ffi-types': 0.32.0
+
+ dependencies:
+ '@jitl/quickjs-ffi-types': 0.32.0
+
+ dependencies:
+ '@jitl/quickjs-ffi-types': 0.32.0
+
'@jridgewell/[email protected]':
dependencies:
'@jridgewell/sourcemap-codec': 1.6.0
@@ -13712,6 +13793,8 @@ snapshots:
+
dependencies:
sprintf-js: 1.0.3
@@ -14221,6 +14304,8 @@ snapshots:
+
@@ -16537,6 +16622,8 @@ snapshots:
lightningcss-win32-arm64-msvc: 1.33.0
lightningcss-win32-x64-msvc: 1.33.0
+
@@ -17399,6 +17486,12 @@ snapshots:
optional: true
+ dependencies:
+ any-promise: 1.3.0
+ object-assign: 4.1.1
+ thenify-all: 1.6.0
+
@@ -17862,6 +17955,8 @@ snapshots:
+
@@ -18094,6 +18189,18 @@ snapshots:
+ dependencies:
+ '@jitl/quickjs-ffi-types': 0.32.0
+
+ dependencies:
+ '@jitl/quickjs-wasmfile-debug-asyncify': 0.32.0
+ '@jitl/quickjs-wasmfile-debug-sync': 0.32.0
+ '@jitl/quickjs-wasmfile-release-asyncify': 0.32.0
+ '@jitl/quickjs-wasmfile-release-sync': 0.32.0
+ quickjs-emscripten-core: 0.32.0
+
dependencies:
performance-now: 2.1.0
@@ -19238,6 +19345,16 @@ snapshots:
+ dependencies:
+ '@jridgewell/gen-mapping': 0.3.13
+ commander: 4.1.1
+ lines-and-columns: 1.2.4
+ mz: 2.7.0
+ pirates: 4.0.7
+ tinyglobby: 0.2.17
+ ts-interface-checker: 0.1.13
+
dependencies:
copy-anything: 4.1.5
@@ -19328,6 +19445,14 @@ snapshots:
transitivePeerDependencies:
- react-native-b4a
+ dependencies:
+ thenify: 3.3.1
+
+ dependencies:
+ any-promise: 1.3.0
+
@@ -19397,6 +19522,8 @@ snapshots:
+
dependencies:
'@ts-morph/common': 0.27.0
diff --git a/src/daemon/README.md b/src/daemon/README.md
index a49040436..88ec4af3f 100644
--- a/src/daemon/README.md
+++ b/src/daemon/README.md
@@ -373,3 +373,39 @@ const result = await session.closed;
await eventsDone;
void result;
```
+
+## Alook PII
+
+The daemon filters outgoing Alook message body text locally, in pipeline order, before forwarding a send. Each bot has its own persisted pipeline at `<baseDir>/daemons/<machineId>/<botId>/pii/pipelines.json`, containing only that bot's plugin source and configuration. It loads that file once and reuses its configuration and compiled filters in memory; subsequent sends do not reread JSON or recompile unchanged filters. Successful management changes replace only that bot's snapshot. Manual file edits require a daemon restart. Another bot's invalid configuration does not block it. Attachments and requests to cloud models are outside this filter. Enabled filters fail closed: an error, invalid result, or execution limit blocks the send.
+
+Fresh configurations contain five default filters: **Keys**, **Email addresses**, **ID numbers**, **Phone numbers** and **Bank cards**. Keys runs the previous private-key, recognized API-key, JWT/Authorization and credential-assignment/connection-password checks together, preserving their category-specific replacement markers. Already saved pipelines keep their installed filters, sources, flags and deletions; there is no automatic consolidation.
+
+The default **ID numbers** filter covers selected identity, social-security and personal tax numbers across 16 countries/regions. Ambiguous numeric formats require a document label. See the [supported formats, checks and limitations](src/pii/ID_NUMBERS.md). Previously initialized configurations retain their installed rule sources.
+
+The default **Phone numbers** filter recognizes international numbers and phone-labelled national numbers using pinned Google numbering metadata, alongside the existing standalone formats. See [recognition boundaries and source attribution](src/pii/PHONE_NUMBERS.md).
+
+Inside a bot session:
+
+```sh
+alook pii list
+alook pii add --file ./company-terms.ts --description "Redacts internal project names"
+```
+
+The TypeScript file default-exports a synchronous pure transform returning `{ text: string, triggered: boolean }`:
+
+```ts
+export default (text: string) => {
+ let triggered = false;
+ const output = text.replace(/Project Wren/g, () => {
+ triggered = true;
+ return "[INTERNAL_PROJECT]";
+ });
+ return { text: output, triggered };
+};
+```
+
+The bool is explicit per plugin, including when a later plugin restores the text. Legacy string-returning sources remain executable with per-node input/output comparison as a compatibility estimate; that cannot report no-op matches. Successful sends include `pii.triggeredFilters` only when a filter reports a hit, without original values. See [credential formats, protocol and receipt limits](src/pii/CREDENTIALS.md).
+
+Plugins have no provided filesystem, process, environment, network, or module-import capabilities. They run with memory and execution limits. Adding appends a new filter and cannot replace an existing filter. The CLI has no disable/delete operation. The human owner manages those actions in Privacy while the daemon is online, including for inactive bots. A dispatched request is not an applied change; the page waits for the local daemon's result. Deleted defaults stay deleted after restart. Rules can miss sensitive values; review the pipeline and its scope before relying on it.
+
+The Privacy playground relays test text through Alook's server to the daemon and returns the selected pipeline's output. It uses the same runner and checks the displayed configuration revision. Test input and output are transient: they are not stored in configuration, chat history, replay queues or body logs. No chat message or model request is sent. Input and output must each fit 1024 UTF-8 bytes; a failed filter returns a safe error instead of partial output.
diff --git a/src/daemon/THIRD_PARTY_NOTICES.md b/src/daemon/THIRD_PARTY_NOTICES.md
new file mode 100644
index 000000000..8d5a317b5
--- /dev/null
+++ b/src/daemon/THIRD_PARTY_NOTICES.md
@@ -0,0 +1,387 @@
+# Third-party notices
+
+## Magpie rule reference
+
+The built-in detection rules in src/pii/defaults.ts are adapted from yetone/magpie, commit 46c41883277b30937364a27f1dbdc8a0d35eacf2, internal/redact/redact.go and ids.go. This implementation transforms outgoing Alook message text and does not implement Magpie's gateway or reversible masking.
+
+MIT License
+
+Copyright (c) 2026 yetone
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+The following packages are included in the bundled TypeScript transformer. Their license texts are reproduced from the installed package releases.
+
+## sucrase
+
+The MIT License (MIT)
+
+Copyright (c) 2012-2018 various contributors (see AUTHORS)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+## @jridgewell/gen-mapping
+
+Copyright 2024 Justin Ridgewell <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+## @jridgewell/resolve-uri
+
+Copyright 2019 Justin Ridgewell <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+## @jridgewell/sourcemap-codec
+
+Copyright 2024 Justin Ridgewell <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+## @jridgewell/trace-mapping
+
+Copyright 2024 Justin Ridgewell <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+## lines-and-columns
+
+The MIT License (MIT)
+
+Copyright (c) 2015 Brian Donovan
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+## ts-interface-checker
+
+Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+## Google libphonenumber numbering-plan metadata
+
+`src/pii/phone-number-plans.ts` and `src/pii/fixtures/phone-examples.json` adapt `resources/PhoneNumberMetadata.xml` from google/libphonenumber commit 1ae0d623301b302f828628dfc368c983e8d27141.
+
+Copyright (C) 2009 The Libphonenumber Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. The complete Apache License, Version 2.0 is included in LICENSE.
diff --git a/src/daemon/package.json b/src/daemon/package.json
index d5a85fca1..f99a63c58 100644
--- a/src/daemon/package.json
+++ b/src/daemon/package.json
@@ -1,7 +1,7 @@
{
"name": "@alook/daemon",
"version": "0.1.59",
- "description": "Alook agent daemon — host-side runtime backend, process manager, credential proxy, and control plane.",
+ "description": "Alook agent daemon \u2014 host-side runtime backend, process manager, credential proxy, and control plane.",
"license": "Apache-2.0",
"homepage": "https://github.com/alookai/alook#readme",
"repository": {
@@ -27,7 +27,8 @@
"files": [
"dist",
"README.md",
- "LICENSE"
+ "LICENSE",
+ "THIRD_PARTY_NOTICES.md"
],
"engines": {
"node": ">=20.9"
@@ -37,7 +38,7 @@
},
"scripts": {
"prebuild": "pnpm -C agent-driver run build",
- "build": "bun build src/index.ts --outdir dist --target node --format esm --external commander --external ws --external sharp && bun build src/cli/index.ts --outdir dist/cli --target node --format esm --external commander --external ws --external sharp && node scripts/prepare-dist.mjs",
+ "build": "bun build src/index.ts --outdir dist --target node --format esm --external commander --external ws --external sharp --external quickjs-emscripten && bun build src/cli/index.ts --outdir dist/cli --target node --format esm --external commander --external ws --external sharp --external quickjs-emscripten && node scripts/prepare-dist.mjs",
"prepack": "pnpm run build",
"typecheck": "tsc --noEmit",
"test": "pnpm test:unit && pnpm test:packed",
@@ -50,7 +51,9 @@
},
"dependencies": {
"commander": "^15.0.0",
+ "quickjs-emscripten": "^0.32.0",
"sharp": "^0.35.5",
+ "sucrase": "^3.35.1",
"ws": "^8.22.0"
},
"devDependencies": {
@@ -62,6 +65,7 @@
"tsx": "^4.23.15",
"typescript": "^6.0.3",
"typescript-eslint": "^8.72.0",
- "vitest": "4.1.12"
+ "vitest": "4.1.12",
+ "zod": "^4.6.7"
}
}
diff --git a/src/daemon/src/cli/index.test.ts b/src/daemon/src/cli/index.test.ts
index 5cd7c3e6f..16e768552 100644
--- a/src/daemon/src/cli/index.test.ts
+++ b/src/daemon/src/cli/index.test.ts
@@ -810,6 +810,52 @@ describe("message send — idempotent retry (mutation-idempotency ②)", () => {
});
});
+describe("message send PII receipt", () => {
+ const args = ["message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0"];
+ const message = { seq: "#8", channel: "/s#0042/general", sender: "@a", content: { text: "filtered" }, time: "" };
+ const pii = { triggeredFilters: [{ id: "builtin.secrets", name: "Keys" }] };
+
+ it.each([false, true])("preserves local hit metadata in final success output, including deduped=%s", async deduped => {
+ setApiForTesting(stubApi({ send: async () => ({ state: "sent", message, deduped, pii }) }));
+ await mainWithStdin(args);
+ expect(parseEnvelope(cap.lines()).success).toMatchObject({ sent: "/s#0042/general#8", pii });
+ });
+
+ it("does not add PII information for no hit", async () => {
+ setApiForTesting(stubApi({ send: async () => ({ state: "sent", message }) }));
+ await mainWithStdin(args);
+ expect(parseEnvelope(cap.lines()).success).not.toHaveProperty("pii");
+ });
+
+ it("preserves hit information if local reminder arming fails after a committed send", async () => {
+ mockArmMessageReminder.mockRejectedValueOnce(new Error("local failure"));
+ setApiForTesting(stubApi({ send: async () => ({ state: "sent", message, pii }) }));
+ await mainWithStdin(args);
+ expect(parseEnvelope(cap.lines()).success).toMatchObject({ sent: "/s#0042/general#8", pii, reminder: { armed: false } });
+ });
+
+ it.each(["#invalid", "#0", "#9007199254740992"])("preserves the committed hit receipt when canonical seq %s cannot arm a reminder", async seq => {
+ const send = vi.fn(async () => ({ state: "sent" as const, message: { ...message, seq }, pii }));
+ setApiForTesting(stubApi({ send }));
+ await mainWithStdin(args);
+ expect(parseEnvelope(cap.lines())).toEqual({ success: {
+ sent: `/s#0042/general${seq}`, pii,
+ reminder: { armed: false, reason: "server returned an invalid canonical message seq" },
+ } });
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(mockArmMessageReminder).not.toHaveBeenCalled();
+ });
+
+ it("does not turn a blocked send into success or expose PII metadata", async () => {
+ setApiForTesting(stubApi({ send: async () => ({ state: "blocked", reason: "unaligned", unreadCount: 1, latestSeq: 9 }) }));
+ await mainWithStdin(args);
+ const result = parseEnvelope(cap.lines());
+ expect(result).not.toHaveProperty("success");
+ expect(result).not.toHaveProperty("pii");
+ expect(result.error).toContain("channel not aligned");
+ });
+});
+
describe("message send --force", () => {
const args = ["message", "send", "--target", "/s#0042/general", "--stdin", "--remind-after", "0"];
it.each([false, true])("forwards only an explicit force flag (%s)", async (force) => {
diff --git a/src/daemon/src/cli/index.ts b/src/daemon/src/cli/index.ts
index 9731c8ca6..739b5bc0e 100755
--- a/src/daemon/src/cli/index.ts
+++ b/src/daemon/src/cli/index.ts
@@ -16,6 +16,7 @@ import { registerPlatformCommands } from "./platform.js";
* the sole result channel.
*/
import { Command, CommanderError } from "commander";
+import { registerPiiCommands } from "./pii.js";
import { realpathSync } from "node:fs";
import { randomUUID } from "node:crypto";
import { pathToFileURL } from "node:url";
@@ -390,10 +391,11 @@ async function cmdMessageSend(opts: Record<string, unknown>, stdin: CliInputStre
// SUCCESS — the message is in the channel; surface its canonical ref exactly
// like a fresh send, never as an error.
const sent = `${res.message.channel}${res.message.seq}`;
+ const pii = res.pii?.triggeredFilters.length ? { pii: res.pii } : {};
const seqText = res.message.seq.replace(/^#/, "");
const sentSeq = Number(seqText);
if (!/^\d+$/.test(seqText) || !Number.isSafeInteger(sentSeq) || sentSeq < 1) {
- return { sent, reminder: { armed: false, reason: "server returned an invalid canonical message seq" } };
+ return { sent, ...pii, reminder: { armed: false, reason: "server returned an invalid canonical message seq" } };
}
try {
const reminder = await armMessageReminderFromEnv({
@@ -401,11 +403,11 @@ async function cmdMessageSend(opts: Record<string, unknown>, stdin: CliInputStre
sentSeq,
remindAfterMs,
});
- return { sent, reminder };
+ return { sent, ...pii, reminder };
} catch {
// The send is already committed. Never turn a local arming problem into a
// command error that invites the agent to re-run and duplicate the send.
- return { sent, reminder: { armed: false, reason: "local reminder request failed" } };
+ return { sent, ...pii, reminder: { armed: false, reason: "local reminder request failed" } };
}
}
@@ -813,6 +815,7 @@ function buildProgram(stdin: CliInputStream): Command {
registerPlatformCommands(program, getApi, printEnvelope);
+ registerPiiCommands(program, printEnvelope);
const message = program.command("message").description("message operations").exitOverride();
message.configureOutput({ writeOut: () => {}, writeErr: () => {} });
diff --git a/src/daemon/src/cli/pii.test.ts b/src/daemon/src/cli/pii.test.ts
new file mode 100644
index 000000000..e2ded6d96
--- /dev/null
+++ b/src/daemon/src/cli/pii.test.ts
@@ -0,0 +1,95 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { Command } from "commander";
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { PII_SOURCE_MAX_BYTES } from "@alook/shared";
+import { CredentialBroker, startCredentialProxy } from "../credentials/credentialProxy.js";
+import { PiiStore } from "../pii/store.js";
+import { registerPiiCommands } from "./pii.js";
+
+const cleanup: (() => void | Promise<void>)[] = [];
+afterEach(async () => {
+ vi.unstubAllGlobals();
+ vi.unstubAllEnvs();
+ vi.restoreAllMocks();
+ for (const close of cleanup.splice(0).reverse()) await close();
+});
+
+async function fixture() {
+ const directory = mkdtempSync(join(tmpdir(), "pii-cli-"));
+ cleanup.push(() => rmSync(directory, { recursive: true, force: true }));
+ const store = new PiiStore(join(directory, "bots"));
+ cleanup.push(() => store.close());
+ const broker = new CredentialBroker({ upstreamBaseUrl: "http://127.0.0.1:9", voucherDir: join(directory, "vouchers") });
+ const voucher = broker.mint("old-bot", "launch", ["send", "read"], "synthetic-key");
+ const proxy = await startCredentialProxy(broker, { pii: store });
+ cleanup.push(() => proxy.close());
+ const token = join(directory, "token");
+ writeFileSync(token, `${voucher.voucher}\n`, { mode: 0o600 });
+ vi.stubEnv("ALOOK_PROXY_URL", `${proxy.url}///`);
+ vi.stubEnv("ALOOK_PROXY_TOKEN_FILE", token);
+ const run = async (...args: string[]) => {
+ const output = vi.fn();
+ const program = new Command().exitOverride().configureOutput({ writeErr() {}, writeOut() {} });
+ registerPiiCommands(program, output);
+ await program.parseAsync(["node", "alook", "pii", ...args]);
+ return output;
+ };
+ return { directory, store, run };
+}
+
+it("lists defaults and appends a working local TS filter only to the voucher-bound bot", async () => {
+ const { directory, store, run } = await fixture();
+ const listed = await run("list");
+ expect(listed).toHaveBeenCalledWith({ success: expect.objectContaining({ revision: 0, plugins: expect.any(Array) }) });
+ expect(store.list("old-bot").plugins).toHaveLength(5);
+ const source = join(directory, "project.ts");
+ writeFileSync(source, 'export default (text: string) => text.replace("PROJECT", "[INTERNAL]");');
+ const added = await run("add", "--file", source, "--description", "Hide the project name");
+ expect(added).toHaveBeenCalledWith({ success: expect.objectContaining({ revision: 1 }) });
+ expect(store.list("old-bot").plugins.at(-1)).toMatchObject({ name: "project", description: "Hide the project name", enabled: true });
+ expect(await store.filter("old-bot", "PROJECT [email protected]")).toBe("[INTERNAL] [REDACTED_EMAIL]");
+ expect(store.list("other-bot").plugins).toHaveLength(5);
+ expect(JSON.stringify([listed.mock.calls, added.mock.calls])).not.toContain("export default");
+});
+
+it.each(["ALOOK_PROXY_URL", "ALOOK_PROXY_TOKEN_FILE"])("rejects a missing %s before making a request", async name => {
+ const { run } = await fixture();
+ vi.stubEnv(name, undefined);
+ const fetch = vi.fn();
+ vi.stubGlobal("fetch", fetch);
+ await expect(run("list")).rejects.toThrow("PII requires the running bot's local daemon proxy");
+ expect(fetch).not.toHaveBeenCalled();
+});
+
+it("rejects an oversized source before sending it to the daemon", async () => {
+ const { directory, run } = await fixture();
+ const source = join(directory, "oversized.ts");
+ writeFileSync(source, "x".repeat(PII_SOURCE_MAX_BYTES + 1));
+ const fetch = vi.fn();
+ vi.stubGlobal("fetch", fetch);
+ await expect(run("add", "--file", source, "--description", "Too large")).rejects.toThrow("PII_SOURCE_TOO_LARGE");
+ expect(fetch).not.toHaveBeenCalled();
+});
+
+it.each([
+ [{ code: "PII_PLUGIN_LIMIT", error: "PRIVATE_RESPONSE" }, "PII_PLUGIN_LIMIT"],
+ [null, "PII_UNAVAILABLE"],
+ ["PRIVATE_RESPONSE", "PII_UNAVAILABLE"],
+ [{ code: 17, error: "PRIVATE_RESPONSE" }, "PII_UNAVAILABLE"],
+])("returns only a safe code for a rejected proxy response %#", async (body, code) => {
+ const { run } = await fixture();
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(JSON.stringify(body), { status: 422 })));
+ await expect(run("list")).rejects.toMatchObject({ message: code, code });
+});
+
+it("does not accept invalid success payloads or expose mutation commands", async () => {
+ const { run } = await fixture();
+ const fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ revision: "invalid", plugins: [] })));
+ vi.stubGlobal("fetch", fetch);
+ await expect(run("list")).rejects.toThrow();
+ fetch.mockClear();
+ await expect(run("remove", "--plugin", "builtin.emails")).rejects.toMatchObject({ code: "commander.unknownCommand" });
+ expect(fetch).not.toHaveBeenCalled();
+});
diff --git a/src/daemon/src/cli/pii.ts b/src/daemon/src/cli/pii.ts
new file mode 100644
index 000000000..bfedd22f0
--- /dev/null
+++ b/src/daemon/src/cli/pii.ts
@@ -0,0 +1,35 @@
+import { readFileSync, statSync } from "node:fs";
+import { basename, extname } from "node:path";
+import type { Command } from "commander";
+import { PII_SOURCE_MAX_BYTES, PiiSnapshotSchema } from "@alook/shared";
+import { LOCAL_PII_PATH } from "../pii/proxy.js";
+
+export function registerPiiCommands(program: Command, output: (value: { success: unknown }) => void) {
+ const pii = program.command("pii").description("local outgoing message text filters; list or append a TypeScript plugin").exitOverride();
+ const request = async (input?: { action: "add"; name: string; description: string; source: string }) => {
+ const url = process.env.ALOOK_PROXY_URL;
+ const tokenFile = process.env.ALOOK_PROXY_TOKEN_FILE;
+ if (!url || !tokenFile) throw new Error("PII requires the running bot's local daemon proxy");
+ const response = await fetch(`${url.replace(/\/+$/, "")}${LOCAL_PII_PATH}`, {
+ method: input ? "POST" : "GET",
+ headers: { authorization: `Bearer ${readFileSync(tokenFile, "utf8").trim()}`, "content-type": "application/json" },
+ ...(input ? { body: JSON.stringify(input) } : {}),
+ signal: AbortSignal.timeout(5_000),
+ });
+ const body: unknown = await response.json();
+ if (!response.ok) {
+ const code = body && typeof body === "object" && typeof (body as { code?: unknown }).code === "string" ? (body as { code: string }).code : "PII_UNAVAILABLE";
+ throw Object.assign(new Error(code), { code });
+ }
+ output({ success: PiiSnapshotSchema.parse(body) });
+ };
+ pii.command("list").description("show this bot's filters in execution order").exitOverride().action(() => request());
+ const add = pii.command("add").description("append a pure synchronous TS function returning { text, triggered: boolean }").exitOverride()
+ .requiredOption("--file <path>", "local TypeScript module; imports and host APIs are unavailable")
+ .requiredOption("--description <text>", "one sentence describing this filter");
+ add.action(async () => {
+ const options = add.opts<{ file: string; description: string }>();
+ if (statSync(options.file).size > PII_SOURCE_MAX_BYTES) throw new Error("PII_SOURCE_TOO_LARGE");
+ await request({ action: "add", name: basename(options.file, extname(options.file)), description: options.description, source: readFileSync(options.file, "utf8") });
+ });
+}
diff --git a/src/daemon/src/credentials/credentialProxy.ts b/src/daemon/src/credentials/credentialProxy.ts
index c23c98106..e5a430e8b 100644
--- a/src/daemon/src/credentials/credentialProxy.ts
+++ b/src/daemon/src/credentials/credentialProxy.ts
@@ -46,6 +46,7 @@ import * as path from "path";
import { URL } from "url";
import { formatRef, parseRef, type Cursor, type Message } from "../server/contract.js";
import { parseNameAndTag } from "@alook/shared/lib/discriminator";
+import { LOCAL_PII_PATH, handleLocalPii, filterPiiRequest, forwardPiiSendResponse, writePiiError, type PiiProxy } from "../pii/proxy.js";
export const LOCAL_MESSAGE_REMINDER_PATH = "/__alook/local/message-reminder";
const LOCAL_MESSAGE_REMINDER_BODY_MAX_BYTES = 4 * 1024;
@@ -305,6 +306,7 @@ export const DEFAULT_CAPABILITY_RESOLVER: CapabilityResolver = (method, pathname
const DEFAULT_UPSTREAM_TIMEOUT_MS = 20_000;
export interface CredentialProxyOptions {
+ pii?: PiiProxy;
/** Bind host (default loopback). Keep it loopback in production. */
host?: string;
/** Port (default 0 ⇒ OS picks a free port). */
@@ -545,8 +547,14 @@ export async function startCredentialProxy(
}
};
- const server = http.createServer((req, res) => {
+ const server = http.createServer(async (req, res) => {
const pathname = new URL(req.url ?? "/", "http://placeholder").pathname;
+ if (pathname === LOCAL_PII_PATH && (req.method === "GET" || req.method === "POST")) {
+ const localVerdict = broker.check(req.headers["authorization"], req.method === "GET" ? "read" : "send");
+ if (!localVerdict.ok) { writeJson(res, localVerdict.status, { error: localVerdict.error, code: localVerdict.code }); req.resume(); return; }
+ await handleLocalPii(req, res, localVerdict.reg.agentId, options.pii);
+ return;
+ }
if (pathname.startsWith("/__alook/local/")) {
if (req.url !== LOCAL_MESSAGE_REMINDER_PATH) {
writeJson(res, 404, { error: "unknown local route", code: "not_found" });
@@ -605,6 +613,22 @@ export async function startCredentialProxy(
}
const reg = verdict.reg;
+ if (/^\/api\/community\/bots\/[^/]+\/pii\/?$/.test(pathname)) {
+ writeJson(res, 403, { error: "PII management requires a human owner session", code: "forbidden" });
+ req.resume();
+ return;
+ }
+ let filteredBody: Buffer | undefined;
+ let piiTriggeredFilters: { id: string; name: string }[] = [];
+ if (options.pii && req.method === "POST" && /^\/api\/community\/channels\/[^/]+\/messages\/?$/.test(pathname)) {
+ try {
+ const filtered = await filterPiiRequest(req, reg.agentId, options.pii);
+ filteredBody = filtered.body;
+ piiTriggeredFilters = filtered.triggeredFilters;
+ }
+ catch (error) { writePiiError(res, error); return; }
+ if (res.destroyed) return;
+ }
// Tightened from `.endsWith("/inboxPull")` — the attachment-download
// endpoint returns raw binary, and a loose match here would try to JSON-
// parse it as an inbox response. Exact-path match the canonical inbox-pull
@@ -664,6 +688,10 @@ export async function startCredentialProxy(
outHeaders[broker.headerNames.client.toLowerCase()] = broker.clientLabel;
outHeaders[broker.headerNames.capabilities.toLowerCase()] = [...reg.capabilities].join(",");
if (isInboxPull || isInboxAck) outHeaders["accept-encoding"] = "identity";
+ if (filteredBody) {
+ delete outHeaders["transfer-encoding"];
+ outHeaders["content-length"] = filteredBody.byteLength;
+ }
// `responded` guards only against writing a second response to the
// DOWNSTREAM client (writeHead/end can't fire twice) — it does NOT gate
@@ -674,6 +702,7 @@ export async function startCredentialProxy(
// tracked so close/timeout can destroy it too once it exists — an
// in-flight `.pipe(res)` doesn't end `res` on a non-graceful destroy.
let responded = false;
+ if (piiTriggeredFilters.length) outHeaders["accept-encoding"] = "identity";
let upstreamRes: http.IncomingMessage | undefined;
const upstreamReq = upstreamClient.request(
@@ -804,6 +833,9 @@ export async function startCredentialProxy(
res.writeHead(res_.statusCode!, res_.headers);
res.end(body);
});
+ } else if (piiTriggeredFilters.length && res_.statusCode !== undefined && res_.statusCode >= 200 && res_.statusCode < 300
+ && normalizeContentEncoding(res_.headers["content-encoding"]) === "identity") {
+ forwardPiiSendResponse(res_, res, piiTriggeredFilters);
} else {
res.writeHead(res_.statusCode ?? 502, res_.headers);
res_.pipe(res);
@@ -852,7 +884,8 @@ export async function startCredentialProxy(
upstreamReq.destroy();
upstreamRes?.destroy();
});
- req.pipe(upstreamReq);
+ if (filteredBody) upstreamReq.end(filteredBody);
+ else req.pipe(upstreamReq);
});
await new Promise<void>((resolve, reject) => {
diff --git a/src/daemon/src/daemon/createDaemon.test.ts b/src/daemon/src/daemon/createDaemon.test.ts
index f326801e7..0a54192e4 100644
--- a/src/daemon/src/daemon/createDaemon.test.ts
+++ b/src/daemon/src/daemon/createDaemon.test.ts
@@ -56,6 +56,7 @@ const timelineSweepHarness = vi.hoisted(() => {
});
const credentialProxyHarness = vi.hoisted(() => ({
+ pii: undefined as import("../pii/proxy.js").PiiProxy | undefined,
onInboxPullStart: undefined as ((agentId: string) => unknown) | undefined,
onInboxPullResponse: undefined as (
((agentId: string, messages: unknown[], observationToken?: unknown) => void) | undefined
@@ -105,6 +106,7 @@ vi.mock("../credentials/index.js", async (importOriginal) => {
startCredentialProxy: (
...args: Parameters<typeof actual.startCredentialProxy>
): ReturnType<typeof actual.startCredentialProxy> => {
+ credentialProxyHarness.pii = args[1]?.pii;
credentialProxyHarness.onInboxPullStart = args[1]?.onInboxPullStart;
credentialProxyHarness.onInboxPullResponse = args[1]?.onInboxPullResponse as
| ((agentId: string, messages: unknown[], observationToken?: unknown) => void)
@@ -131,6 +133,7 @@ afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
timelineSweepHarness.reset();
+ credentialProxyHarness.pii = undefined;
credentialProxyHarness.onInboxPullStart = undefined;
credentialProxyHarness.onInboxPullResponse = undefined;
credentialProxyHarness.onInboxPullObservationError = undefined;
@@ -3248,6 +3251,64 @@ describe("emitImplicitTypingStopOnSend", () => {
});
});
+describe("createDaemon — inactive bot PII management", () => {
+ it("uses fresh inclusive inventory for configuration without starting a model, and rejects a bot after unbinding", async () => {
+ const sockets: FakeSocket[] = [];
+ const workingDirectoryBase = mkdtempSync(join(tmpdir(), "daemon-inactive-pii-"));
+ startupSweepDirs.push(workingDirectoryBase);
+ let bound = true;
+ let inventoryFailure = false;
+ const fetchMock = vi.fn(async (input: string | URL | Request) => {
+ if (inventoryFailure) throw new Error("inventory unavailable");
+ const url = new URL(String(input));
+ return Response.json({ bots: bound && url.searchParams.get("includeInactive") === "true"
+ ? [{ id: "inactive", name: "Inactive", discriminator: "0001" }]
+ : [] });
+ });
+ vi.stubGlobal("fetch", fetchMock);
+ const sessionFactory = vi.fn();
+ const routerEntry = spyOnRouterCommandEntry();
+ const logger = stubLogger();
+ const daemon = await createDaemon({
+ machineKey: "synthetic-pii-machine", serverUrl: "http://server.invalid", serverWsUrl: "ws://x",
+ webSocketFactory: factory(sockets) as never, runtimeReport: [], driverFor: () => fakeDriver,
+ capabilities: [], workingDirectoryBase, logger, sessionFactory,
+ });
+ sockets[0]!.emit("open");
+ const manage = async (requestId: string, operation: import("@alook/shared").PiiOperation = { action: "list" }) => {
+ sockets[0]!.emit("message", JSON.stringify({ type: "pii:manage", botId: "inactive", requestId, operation }));
+ let response: { result: { status: string; code?: string; output?: string; snapshot?: { plugins: unknown[] } } } | undefined;
+ await vi.waitFor(() => {
+ response = sockets[0]!.sent.map(frame => JSON.parse(frame)).find(frame => frame.type === "pii_result" && frame.requestId === requestId);
+ expect(response).toBeDefined();
+ });
+ return response!.result;
+ };
+ try {
+ const listed = await manage("before-unbind");
+ expect(listed.status).toBe("complete");
+ expect(listed.snapshot?.plugins).toHaveLength(5);
+ expect(await credentialProxyHarness.pii!.local("inactive", { action: "list" })).toMatchObject({ revision: 0, plugins: expect.any(Array) });
+ expect(await credentialProxyHarness.pii!.filterWithResult("inactive", "[email protected]")).toEqual({ text: "[REDACTED_EMAIL]", triggeredFilters: [{ id: "builtin.emails", name: "Email addresses" }] });
+ const configFile = join(workingDirectoryBase, "inactive", "pii", "pipelines.json");
+ const configuration = readFileSync(configFile, "utf8");
+ expect(await manage("preview", { action: "preview", text: "[email protected]", expectedRevision: 0 }))
+ .toMatchObject({ status: "complete", output: "[REDACTED_EMAIL]" });
+ expect(readFileSync(configFile, "utf8")).toBe(configuration);
+ expect(JSON.stringify(logger.calls)).not.toContain("[email protected]");
+ expect(JSON.stringify(logger.calls)).not.toContain("[REDACTED_EMAIL]");
+ bound = false;
+ expect(await manage("after-unbind")).toEqual({ status: "error", code: "PII_BOT_NOT_FOUND" });
+ expect(fetchMock.mock.calls.some(([url]) => String(url).endsWith("/daemon/bots"))).toBe(true);
+ expect(fetchMock.mock.calls.filter(([url]) => String(url).endsWith("/daemon/bots?includeInactive=true"))).toHaveLength(3);
+ inventoryFailure = true;
+ expect(await manage("inventory-down")).toEqual({ status: "error", code: "PII_UNAVAILABLE" });
+ expect(sessionFactory).not.toHaveBeenCalled();
+ expect(routerEntry).not.toHaveBeenCalledWith(expect.objectContaining({ type: "pii:manage" }));
+ } finally { await daemon.stop(); }
+ });
+});
+
describe("AgentProcessManager.auditContext", () => {
// Producer B (credential-proxy sighting) reads this so `cli_invocation`
// rows carry the same context Producer A's tool_call / thinking rows do.
diff --git a/src/daemon/src/daemon/createDaemon.ts b/src/daemon/src/daemon/createDaemon.ts
index 1ee006d93..92f0357f1 100644
--- a/src/daemon/src/daemon/createDaemon.ts
+++ b/src/daemon/src/daemon/createDaemon.ts
@@ -23,11 +23,12 @@
* — the `driverFor` is INJECTED by the caller.
*/
import { homedir } from "os";
+import { PiiStore } from "../pii/store.js";
import { appendFileSync, mkdirSync } from "node:fs";
import { createRotatingFileSink, type RotatingFileSink } from "../util/rotatingFileSink.js";
import { createTraceSampler, DEFAULT_TRACE_FILE_MAX_BYTES } from "../util/traceSampler.js";
import { writeStatusFile } from "../util/statusFile.js";
-import { WsControlChannel } from "../server/wsControlChannel.js";
+import { WS_CONTROL_COMMAND_CONSUMED, WsControlChannel } from "../server/wsControlChannel.js";
import { CredentialBroker, startCredentialProxy } from "../credentials/index.js";
import { AgentProcessManager, AgentRouter, createTypingScopeTracker } from "../manager/index.js";
import type { ManagerTraceRecord, SessionFactory, TypingScopeTracker } from "../manager/index.js";
@@ -405,6 +406,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
const workingDirectoryBase = opts.workingDirectoryBase ?? fallbackBase;
const workdirFor = (agentId: string) => `${workingDirectoryBase}/${agentId}`;
const dailyTokenUsage = new DailyTokenUsageStore(workingDirectoryBase);
+ const localPiiStore = new PiiStore(opts.fsmTraceDir ?? workingDirectoryBase);
let stopping = false;
let usageSealed = false;
let stopPromise: Promise<void> | undefined;
@@ -556,6 +558,10 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
const broker = new CredentialBroker({ upstreamBaseUrl: opts.serverUrl });
const proxy = await startCredentialProxy(broker, {
+ pii: {
+ filterWithResult: (botId, text) => localPiiStore.filterWithResult(botId, text),
+ local: (botId, input) => localPiiStore.local(botId, input),
+ },
onInboxPullStart: (agentId) => ({
owner: managerRef?.timelineTurnOwner(agentId) ?? null,
}),
@@ -687,7 +693,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
>();
let botCacheReady = false;
- async function listMyBotsHttp(): Promise<
+ async function listMyBotsHttp(includeInactive = false): Promise<
Array<{
id: string;
name: string;
@@ -697,7 +703,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
ownerDiscriminator?: string;
}>
> {
- const res = await fetch(`${opts.serverUrl}/api/community/daemon/bots`, {
+ const res = await fetch(`${opts.serverUrl}/api/community/daemon/bots${includeInactive ? "?includeInactive=true" : ""}`, {
method: "GET",
headers: { authorization: `Bearer ${opts.machineKey}` },
});
@@ -1170,6 +1176,20 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
// Machine lifecycle commands are claimed before diagnostics, bot observers,
// and AgentRouter; updates never enter agent lifecycle routing.
+ channel.onCommand((command) => {
+ if (command.type !== "pii:manage") return;
+ void (async () => {
+ try {
+ const bots = await listMyBotsHttp(true);
+ const bound = bots.some((bot) => bot.id === command.botId);
+ channel.reportPiiResult({ type: "pii_result", botId: command.botId, requestId: command.requestId,
+ result: bound ? await localPiiStore.manage(command.botId, command.operation) : { status: "error", code: "PII_BOT_NOT_FOUND" } });
+ } catch {
+ channel.reportPiiResult({ type: "pii_result", botId: command.botId, requestId: command.requestId, result: { status: "error", code: "PII_UNAVAILABLE" } });
+ }
+ })();
+ return WS_CONTROL_COMMAND_CONSUMED;
+ });
channel.onCommand(createSelfUpdateCommandListener(opts.handleSelfUpdate));
channel.onCommand(createMachineStopCommandListener(opts.onStopRequested));
// Diagnostics likewise claims its machine-level command before bot/router.
@@ -1235,6 +1255,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
const failures: unknown[] = [];
stopPromise = Promise.resolve().then(async () => {
try {
+ try { await localPiiStore.close(); } catch (error) { failures.push(error); }
try { await proxy.close(); } catch (error) { failures.push(error); }
try { await manager.stopAll(); } catch (error) { failures.push(error); }
} finally {
diff --git a/src/daemon/src/drivers/systemPrompt.ts b/src/daemon/src/drivers/systemPrompt.ts
index 8038e9fe3..b6805ef08 100644
--- a/src/daemon/src/drivers/systemPrompt.ts
+++ b/src/daemon/src/drivers/systemPrompt.ts
@@ -173,6 +173,21 @@ function cliCommandsSection(): string {
`Use an online machine and its backend. The \`reason\` explains to your owner why the bot is needed and what work it will do; this text appears on the approval card. Repeat \`--create\` for up to 20 bots in one request. ` +
`The command submits one approval card to the DM between you and your owner. It creates no bots until your human owner approves the request.`,
"",
+ "### PII",
+ "",
+ "Alook PII filters your outgoing Alook message text locally, in pipeline order, before sending. " +
+ "Attachments and requests to AI models are outside this pipeline.",
+ "",
+ `1. \`${CLI} pii list\` — list this bot's filters in execution order.`,
+ `2. \`${CLI} pii add --file <path.ts> --description <text>\` — append a new filter to this bot's pipeline.`,
+ "",
+ "To add a filter, write a TS module with a pure, synchronous default export `(text: string) => string`. " +
+ "Use one sentence for its description. Plugins cannot import modules or access files, network, or environment variables.",
+ "Adding stores a source copy. Later edits to the input file do not update the installed filter. " +
+ "Do not edit daemon-managed copies.",
+ "The CLI only lists or appends filters; it cannot replace, reorder, enable, disable, or delete them. " +
+ "Your human owner manages enabled state and deletion in Privacy. Filter errors block sending.",
+ "",
"### Settings",
"",
`1. \`${CLI} setting profile --set-bio <text> --set-avatar <path>\` — update your public bio ` +
diff --git a/src/daemon/src/manager/agentRouter.ts b/src/daemon/src/manager/agentRouter.ts
index ad78105b7..3cb806a1f 100644
--- a/src/daemon/src/manager/agentRouter.ts
+++ b/src/daemon/src/manager/agentRouter.ts
@@ -19,7 +19,7 @@
* server recovers this host's state after a dropped control connection.
*/
import type { HostCommand, HostControlChannel, HostReady, HostReadyRuntime, UnreadNotice, AgentSessionReport, SessionErrorFrame, AgentId, HostAgentActivity } from "../server/contract.js";
-import type { ProviderQuotaSnapshot } from "@alook/shared";
+import { PII_CAPABILITY, type ProviderQuotaSnapshot } from "@alook/shared";
import { CONTROL_HEARTBEAT_CAPABILITY, MACHINE_STOP_CAPABILITY, RUNTIME_MODEL_DISCOVERY_CAPABILITY } from "../server/contract.js";
import { getDriver, type AgentBackend } from "../drivers/index.js";
import type { AgentProcessManager } from "./managerRuntime.js";
@@ -252,7 +252,7 @@ export class AgentRouter {
: this.opts.timeZone;
return {
runtimeReport: [...this.runtimes.values()],
- capabilities: [CONTROL_HEARTBEAT_CAPABILITY, ...(this.opts.supportsMachineStop ? [MACHINE_STOP_CAPABILITY] : []), ...(this.opts.channel.reportRuntimeModels ? [RUNTIME_MODEL_DISCOVERY_CAPABILITY] : [])],
+ capabilities: [...(this.opts.channel.reportPiiResult ? [PII_CAPABILITY] : []), CONTROL_HEARTBEAT_CAPABILITY, ...(this.opts.supportsMachineStop ? [MACHINE_STOP_CAPABILITY] : []), ...(this.opts.channel.reportRuntimeModels ? [RUNTIME_MODEL_DISCOVERY_CAPABILITY] : [])],
runningAgents: [...this.running],
hostname: this.opts.hostname,
platform: this.opts.platform,
diff --git a/src/daemon/src/pii/CREDENTIALS.md b/src/daemon/src/pii/CREDENTIALS.md
new file mode 100644
index 000000000..2cdc72433
--- /dev/null
+++ b/src/daemon/src/pii/CREDENTIALS.md
@@ -0,0 +1,32 @@
+# Keys
+
+New configurations contain one credential filter, `builtin.secrets`, named **Keys**. It runs the previous private-key, API-key, token and password/secret checks in their original order. It keeps `[REDACTED_PRIVATE_KEY]`, `[REDACTED_API_KEY]`, `[REDACTED_TOKEN]`, `[REDACTED_PASSWORD]` and `[REDACTED_SECRET]` distinct.
+
+This is offline format recognition, not a check that a key was issued or remains valid. Publishable/public keys are included because the owner requested that coverage; matching one does not mean it is a secret.
+
+| Format family | Recognized forms |
+| --- | --- |
+| Private key blocks | PEM private-key headers, including RSA/encrypted/OpenSSH and PGP private-key blocks |
+| Stripe, Clerk, Paystack | `pk_`, `sk_`, `rk_` with `live_` or `test_`, followed by at least 20 payload characters; base64/URL-safe payload characters and trailing padding are accepted |
+| Supabase | `sb_publishable_`, `sb_secret_` with at least 20 payload characters; legacy JWTs use the token check |
+| Webhook signing secrets | `whsec_` with at least 20 payload characters |
+| Existing provider-specific formats | OpenAI/Anthropic/OpenRouter-style `sk-` keys, provider `_sk_` forms, GitHub/GitLab, Google, Slack, AWS access-key IDs, Hugging Face, Groq, xAI, npm, PyPI, DigitalOcean and SendGrid; their prior length/boundary checks are retained |
+| Alook credentials | `cmk_`, `cmt_`, `crk_`, `vch_` formats |
+| Tokens | Recognizable JWT structure and Authorization/Bearer/Basic values |
+| Passwords/secrets | Connection URL password components and credential-like assignments with the existing length/letter/digit guards; identifier/reference/env-variable exclusions remain |
+
+An incomplete prefix, arbitrary opaque secret, a split/encoded key or an unsupported provider format can be missed. ASCII word/underscore boundaries prevent matching a key-looking fragment embedded in an ordinary reference. A match is replaced in full; surrounding labels and punctuation remain.
+
+Primary format references: [Stripe](https://docs.stripe.com/keys), [Clerk](https://clerk.com/docs/guides/development/clerk-environment-variables), [Paystack](https://paystack.com/docs/developer-tools/api-keys/), [Supabase](https://supabase.com/docs/guides/getting-started/api-keys). The shared prefix family does not distinguish those providers from each other.
+
+Saved configurations keep their installed sources, individual flags, deletions and custom rules. An upgrade does not automatically combine old nodes. A controlled synthetic preview conversion is separate from product migration.
+
+## Plugin results and send receipts
+
+A synchronous plugin returns `{ text: string, triggered: boolean }`. Set the bool from the plugin's accepted match path. The runner retains each node's bool, even if its output is unchanged or a later plugin restores earlier text. It does not infer new-protocol hits from the final pipeline difference. Invalid result fields, nonboolean flags, promises and exceptions block sending with safe errors; partial text and trigger records are not returned.
+
+String-returning plugins remain accepted for compatibility. For each such node, the runner compares that node's input and output to estimate its bool. This cannot represent a legacy trigger that leaves text unchanged. Existing sources are executed, not skipped or rewritten. Use the object protocol for explicit trigger semantics.
+
+On a successful canonical send, the local proxy adds `pii.triggeredFilters`, containing only filter IDs/names, when at least one node reports a hit. The CLI preserves it in its success JSON alongside the existing sent reference and reminder result. No-hit, blocked and failed sends receive no added PII field. The notice describes filtering of this local send attempt; it does not assert that a nonce-deduplicated canonical message was rewritten. No original values, matched substrings or plugin source are included.
+
+Only normal identity-encoded JSON sent receipts are annotated. Unknown, malformed or unexpectedly large upstream bodies are preserved without inventing a successful receipt. Response buffering is bounded; existing stream-reset/timeout behavior remains in force.
diff --git a/src/daemon/src/pii/ID_NUMBERS.md b/src/daemon/src/pii/ID_NUMBERS.md
new file mode 100644
index 000000000..05e228482
--- /dev/null
+++ b/src/daemon/src/pii/ID_NUMBERS.md
@@ -0,0 +1,58 @@
+# ID numbers
+
+`builtin.id-cards` is one default filter called **ID numbers**. It replaces matches with `[REDACTED_ID_CARD]`. Its persisted ID and replacement marker remain stable.
+
+This is local text detection. A matching structure or checksum does not establish that a document was issued, is current, or belongs to anyone. This filter does not cover every document in the listed countries, passports, document images, or attachments.
+
+## Supported formats
+
+“Label” means an immediately preceding document/type name, case-insensitive, with optional number / no. / nummer / n°, spaces, tab, colon, equals sign, hash sign or JSON quotes. Examples: `SSN: 219-09-9999` and `{"ssn": "219099999"}`. Labels must be on the same line and within 96 characters; intervening prose is not accepted. `other_ssn` and generic `id` do not qualify. Common local-language labels are supported as listed below.
+
+| Country / region | Document / number | Checks performed | Label needed |
+| --- | --- | --- | --- |
+| China (mainland) | 18-character resident identity number | encoded calendar date, nonzero sequence, MOD 11-2 checksum | No |
+| Hong Kong | HKID, one/two letters + 6 digits + check digit | weighted mod 11; check digit 0–9/A, with optional parentheses | No |
+| Taiwan | citizen ID: letter + 1/2 + 8 digits | official letter mapping and weighted mod 10 | No |
+| United States | SSN, 9 digits, compact or 3-2-4 grouping | exclude 000/666/900–999 area, 00 group, 0000 serial and repeated-digit placeholders; no checksum | SSN, Social Security number |
+| Canada | SIN, 9 digits, compact or 3-3-3 grouping | exclude 0/8 prefix; Luhn; repeated-digit placeholders | SIN, NAS, Social Insurance Number, numéro d’assurance sociale |
+| United Kingdom | NINO: 2 letters + 6 digits + A–D, optional pair spaces | HMRC allowed letters and excluded prefixes; no checksum | NINO, National Insurance number |
+| Germany | personal Steuer-ID, 11 digits, compact or 2-3-3-3 grouping | nonzero prefix; one digit repeated twice/three times, no triple run; MOD 11, 10 | Steuer-ID, Steueridentifikationsnummer, IdNr., German tax ID |
+| Germany | Personalausweis number, 9 allowed alphanumerics | **format only**; historical zeroes accepted | Personalausweis, Ausweisnummer, German ID |
+| France | NIR **with its 2-digit control key**, 15 characters; compact or 1-2-2-2-3-3-2 grouping | 1/2 sex prefix, month structure, mod 97; Corsica 2A/2B normalization | NIR, numéro de sécurité sociale, French Social Security |
+| France | CNI: old 12 digits or new 9 alphanumerics | **format only** | CNI, carte nationale d’identité, French ID |
+| Spain | DNI/NIF: 8 digits + letter; NIE: X/Y/Z + 7 digits + letter | mod 23 letter, NIE prefix conversion; optional space/hyphen before final letter | No |
+| Italy | personal codice fiscale, 16 alphanumerics | structure, encoded birth date, omocodia substitutions, parity checksum | No |
+| Italy | electronic identity card (CIE): 2 letters + 5 digits + 2 letters | **format only** | CIE, carta d’identità, Italian ID |
+| India | Aadhaar: 12 digits; compact or 4-4-4 grouping | prefix 2–9, Verhoeff, repeated-digit placeholders | Aadhaar, Aadhar, आधार, आधार संख्या, आधार नंबर |
+| Japan | My Number: 12 digits; compact or 4-4-4 grouping | official weighted checksum; repeated-digit placeholders | My Number, 個人番号, マイナンバー |
+| South Korea | resident/foreign registration number: 13 digits; compact or 6-7 grouping | birth date and 1–8 century/sex code; no legacy checksum requirement, so randomized numbers are included | RRN, Resident Registration number, 주민등록번호, 외국인등록번호 |
+| Singapore | NRIC/FIN: S/T/F/G/M + 7 digits + letter | **format only**; issuer checksum is not checked | NRIC, FIN |
+| Australia | modern TFN: 9 digits; compact or 3-3-3 grouping | weighted mod 11; repeated-digit placeholders | TFN, Tax File number |
+| Brazil | CPF: 11 digits; compact or dotted 3-3-3-2 form | both mod 11 check digits; repeated-digit placeholders | CPF, Cadastro de Pessoas Físicas |
+
+Unlabeled ambiguous numbers are left unchanged by **this filter**. Other enabled categories still apply independently: a number that also matches a phone or bank-card pattern can be redacted by that category. Ordinary reference tests exercise the whole default chain, not only this node. Matches embedded in longer ASCII words, underscore identifiers, connected paths/references, UUIDs or hashes are excluded. This is deliberately narrower than filtering every numeric string.
+
+## Rule sources
+
+Format references below come from issuing authorities, public standards, or jurisdiction-supplied OECD material. Supplementary algorithm references are identified separately; an official format reference does not imply the issuer published the implementation’s checksum algorithm. No third-party implementation is bundled.
+
+- China: [GB 11643-1999](https://zjw.beijing.gov.cn/Portals/0/zhuanti/fazhan/%E8%BA%AB%E4%BB%BD%E8%AF%81GB11643-1999.pdf).
+- Hong Kong: [Digital Policy Office HKID schema](https://www.digitalpolicy.gov.hk/en/our_work/data_governance/policies_standards/interoperability_framework/common_schemas/hkid_number/index.html). Supplementary algorithm reference: [hkid_ops author documentation](https://docs.rs/hkid_ops/latest/hkid_ops/index.html).
+- Taiwan: [Ministry of the Interior identity-number checking appendix](https://gazette.nat.gov.tw/EG_FileManager/eguploadpub/eg027002/ch04/type2/gov30/num3/images/BB.pdf). Citizen IDs only; resident certificate formats are outside this version.
+- US: [SSA impossible SSN groups](https://www.ssa.gov/employer/randomizationfaqs.html).
+- Canada: [Service Canada SIN](https://www.canada.ca/en/employment-social-development/services/sin.html). Supplementary prefix/Luhn reference: [python-stdnum SIN documentation](https://arthurdejong.org/python-stdnum/doc/2.1/stdnum.ca.sin).
+- UK: [HMRC NINO format and excluded prefixes](https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110).
+- Germany: [ELSTER structure and checksum, section 2.2](https://download.elster.de/download/schnittstellen/Pruefung_der_Steuer_und_Steueridentifikatsnummer.pdf); [Personalausweis permitted characters](https://www.gesetze-im-internet.de/pauswv/anlage_3.html). Card-number matching is structural, not MRZ verification.
+- France: [Service Public NIR structure](https://www.service-public.gouv.fr/particuliers/vosdroits/F33078), [Education Ministry mod97 explanation](https://pedagogie.ac-limoges.fr/maths/accueil/html/lycee/pages/Option-maths-TL/Arithm/insee.pdf), [PRADO old CNI specimen](https://www.consilium.europa.eu/prado/en/FRA-BO-02002/index.html), [PRADO new CNI specimen](https://www.consilium.europa.eu/prado/en/FRA-BO-03001/index.html). NIR without its control key, provisional prefixes and unlisted special encodings are outside this version. CNI matching is format only.
+- Spain: [Interior Ministry DNI/NIE control-letter algorithm](https://www.interior.gob.es/opencms/en/servicios-al-ciudadano/tramites-y-gestiones/dni/calculo-del-digito-de-control-del-nif-nie/index.html).
+- Italy: [Finance Ministry fiscal-code specification](https://def.finanze.it/DocTribFrontend/executePrintArticolo.do?codiceOrdinamento=60A+++++0000000&id=%7B8F0DD094-2E4C-4BBA-AD3D-92A2F6DD1050%7D&idAttoNormativo=%7B04AB9404-46E7-4072-B414-892639727A96%7D), [Interior Ministry CIE serial format](https://www.cartaidentita.interno.gov.it/cose-la-carta/caratteristiche-del-documento/).
+- India: [UIDAI compendium, Aadhaar/VID Verhoeff checksum](https://uidai.gov.in/images/resource/compendium_auth_28022018.pdf). VID itself is outside this version.
+- Japan: [e-Gov individual-number checksum regulation, Article 5](https://laws.e-gov.go.jp/law/426M60000008085); [jurisdiction-supplied OECD individual-number description](https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Japan-TIN.pdf).
+- South Korea: [jurisdiction-supplied OECD RRN format](https://www.oecd.org/content/dam/oecd/en/topics/policy-issue-focus/aeoi/korea-tin.pdf), [Interior Ministry2020 randomization change](https://mois.go.kr/plan2020/download/2020plan.pdf).
+- Singapore: [ICA S/T/F/G/M prefixes and FIN format](https://www.ica.gov.sg/news-and-publications/newsroom/media-release/new-m-fin-series-to-be-introduced-from-1-january-2022). Its example is used as format evidence, not as a claim about the checksum.
+- Australia: [jurisdiction-supplied OECD TFN format](https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Australia-TIN.pdf). Supplementary algorithm reference: [Clearwater validation author](https://clearwater.com.au/code/tfn). Legacy 8-digit TFNs are outside this version.
+- Brazil: [Receita Federal CPF checksum specification, page 17](https://www.gov.br/receitafederal/pt-br/centrais-de-conteudo/publicacoes/manuais/sped/manuais-e-financeira/versao-2.x/manual-e-financeira-anexo-v-versao-2-0-1-leiaute-modulo-de-repasse.pdf).
+
+## Saved configurations
+
+Default source is copied when a bot first initializes its local configuration. A daemon upgrade does not replace an initialized rule, enable a disabled rule, restore a deleted rule or overwrite custom filters. Existing configured bots retain their installed sources. This feature introduces no automatic migration or replacement CLI.
diff --git a/src/daemon/src/pii/PHONE_NUMBERS.md b/src/daemon/src/pii/PHONE_NUMBERS.md
new file mode 100644
index 000000000..b1b1d4583
--- /dev/null
+++ b/src/daemon/src/pii/PHONE_NUMBERS.md
@@ -0,0 +1,24 @@
+# Phone numbers
+
+The default `builtin.phones` keeps its original `[REDACTED_PHONE]` marker and reports an explicit trigger only when it replaces a number.
+
+## Recognition
+
+- Explicit `+` / full-width `+` international numbers use the country calling code and general national-number patterns from Google's pinned libphonenumber metadata. Spaces, parentheses, hyphens and dots are accepted; optional international `(0)` trunk notation is supported. International numbers must fit E.164's 15-digit maximum.
+- Phone-labelled national numbers use those same general patterns and national trunk prefixes. Labels include phone/phone number, telephone/tel, mobile/cell, and common French, German, Spanish, Portuguese, Russian, Chinese, Japanese and Korean terms, including quoted JSON keys. Labelled `00` international dialing notation is accepted.
+- Familiar formatted North American numbers and the previous standalone mainland-China mobile formats remain recognized without a label.
+- Other bare national digit strings are ambiguous and need a phone label or an international prefix. Ordinary order numbers, dates, IDs, versions, paths and embedded key fragments are not treated as phones merely because they contain enough digits. Short codes, emergency numbers and extensions are outside this rule's promised coverage.
+
+The snapshot contains 245 geographical regions plus nine nongeographical plans, including US/Canada, UK/Ireland, Europe, India, China/Hong Kong/Taiwan, Japan, South Korea, Southeast Asia, Australia/New Zealand, Latin America, Africa and the Middle East. Shared calling codes do not establish the number's geographical origin. This is offline general-format recognition, not proof that a subscriber number was allocated, is active or belongs to a person. Metadata can become stale; not every phone notation is recognized.
+
+## Provenance and maintenance
+
+Source: [Google libphonenumber PhoneNumberMetadata.xml](https://github.com/google/libphonenumber/blob/1ae0d623301b302f828628dfc368c983e8d27141/resources/PhoneNumberMetadata.xml), commit `1ae0d623301b302f828628dfc368c983e8d27141`, XML SHA256 `b6491939ddaeb9d585a34fe0f0ebd096e83c5ac9a1c5d6dddf38ba4b162b28ca`.
+
+`phone-number-plans.ts` contains deduplicated `[countryCode, generalDesc.nationalNumberPattern, nationalPrefix]` tuples, with XML pattern whitespace removed. The 254 XML territories yield 247 distinct tuples. `fixtures/phone-examples.json` preserves one upstream example per territory: fixed-line first, then mobile, then another available type. Tests exercise all 254 international examples through the actual bounded runner. These are published upstream test examples, not user data. Required attribution is in `THIRD_PARTY_NOTICES.md`; no runtime package or network lookup is added.
+
+The source is injected as a self-contained plugin within the existing 16KiB source limit and unchanged runtime deadlines. Keep the source pin, metadata tuples, examples and attribution together when refreshing it.
+
+International numbering limit: [ITU-T E.164 (February 2026)](https://www.itu.int/rec/T-REC-E.164-202602-I/en).
+
+Saved configurations keep their installed sources and customizations. This change does not silently replace existing pipelines. Controlled test-account updates verify exact known-default source bytes and preserve flags, order, custom nodes and deletions.
diff --git a/src/daemon/src/pii/defaults.ts b/src/daemon/src/pii/defaults.ts
new file mode 100644
index 000000000..53ce2da58
--- /dev/null
+++ b/src/daemon/src/pii/defaults.ts
@@ -0,0 +1,29 @@
+import { ID_NUMBERS_SOURCE } from "./id-numbers.js";
+import { PHONE_NUMBERS_SOURCE } from "./phone-numbers.js";
+
+export const PII_DEFAULTS = [
+ {
+ id: "builtin.secrets", name: "Keys", description: "Redacts keys, tokens and passwords.",
+ source: String.raw`export default (text: string) => { let triggered = false; const output = text
+ .replace(/-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----[\s\S]+?-----END (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----/g, () => { triggered = true; return "[REDACTED_PRIVATE_KEY]"; })
+ .replace(/(?<![A-Za-z0-9_-])(?:sk-(?:ant-|proj-|or-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}|[a-z]{2,4}_sk_[A-Za-z0-9_-]{20,}|gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,}|glpat-[A-Za-z0-9_-]{20,}|AIza[0-9A-Za-z_-]{35}|xox[abposr]-[0-9A-Za-z-]{10,}|(?:pk|sk|rk)_(?:live|test)_[A-Za-z0-9_+\/-]{20,}={0,2}|sb_(?:publishable|secret)_[A-Za-z0-9_-]{20,}|whsec_[A-Za-z0-9_-]{20,}|(?:AKIA|ASIA)[0-9A-Z]{16}|hf_[A-Za-z0-9]{30,}|gsk_[A-Za-z0-9]{40,}|xai-[A-Za-z0-9]{40,}|npm_[A-Za-z0-9]{36}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}|(?:cmk|cmt|crk|vch)_[A-Za-z0-9_-]{16,})(?![A-Za-z0-9_-])/g, () => { triggered = true; return "[REDACTED_API_KEY]"; })
+ .replace(/(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}(?![A-Za-z0-9_-])/g, () => { triggered = true; return "[REDACTED_TOKEN]"; }).replace(/(\b(?:Authorization\s*:\s*)?(?:Bearer|Basic)\s+)[A-Za-z0-9._~+\/-]+={0,2}/gi, (all, prefix) => { if (!/Authorization/i.test(prefix) && all.length-prefix.length < 8) return all; triggered = true; return prefix+"[REDACTED_TOKEN]"; })
+ .replace(/([A-Za-z][A-Za-z0-9+.-]*:\/\/[^\s:@/?#"'<>]*:)([^\s@/?#"'<>]{3,})(@)/g, (_all, prefix, _value, suffix) => { triggered = true; return prefix+"[REDACTED_PASSWORD]"+suffix; }).replace(/(\b[A-Za-z0-9_.-]*(?:password|passwd|secret|token|api[_-]?key|access[_-]?key|credential)[A-Za-z0-9_.-]*["']?\s*[:=]\s*["']?)([A-Za-z0-9_\-./+=~!@#%^&*]{8,})/gi, (all, prefix, value) => { const field = prefix.replace(/["'\s:=]+$/g, ""); if (/(?:id|ids|uuid|guid|hash|digest|checksum|cursor)$/i.test(field) || /^(?:process\.env|os\.)/.test(value) || !/[A-Za-z]/.test(value) || !/\d/.test(value)) return all; triggered = true; return prefix + "[REDACTED_SECRET]"; }); return { text: output, triggered }; };`,
+ },
+ {
+ id: "builtin.emails", name: "Email addresses", description: "Redacts email addresses.",
+ source: String.raw`export default (text: string) => { let triggered = false; const output = text.replace(/(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}(?![A-Za-z0-9_%+-]|\.[A-Za-z0-9-])/g, value => /\.(?:png|jpe?g|gif|webp|svg|txt|md|tsx?|jsx?|json|css|log)$/i.test(value) ? value : (triggered = true, "[REDACTED_EMAIL]")); return { text: output, triggered }; };`,
+ },
+ {
+ id: "builtin.id-cards", name: "ID numbers", description: "Redacts supported ID, social-security and personal tax numbers",
+ source: ID_NUMBERS_SOURCE,
+ },
+ {
+ id: "builtin.phones", name: "Phone numbers", description: "Redacts phone numbers",
+ source: PHONE_NUMBERS_SOURCE,
+ },
+ {
+ id: "builtin.bank-cards", name: "Bank cards", description: "Redacts bank card numbers.",
+ source: String.raw`export default (text: string) => { let triggered = false; const output = text.replace(/(?<![A-Za-z0-9])[3-6](?:[ -]?\d){12,18}(?![A-Za-z0-9])/g, (value,offset) => { const before=text.slice(0,offset).match(/[a-fA-F0-9-]+$/)?.[0]||"",after=text.slice(offset+value.length).match(/^[a-fA-F0-9-]+/)?.[0]||""; if(/^[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}$/.test(before+value+after))return value; const digits=value.replace(/[ -]/g,""); let sum=0,twice=false; for(let i=digits.length-1;i>=0;i--){let n=+digits[i];if(twice){n*=2;if(n>9)n-=9;}sum+=n;twice=!twice;}return sum%10===0?(triggered=true,"[REDACTED_BANK_CARD]"):value; }); return { text: output, triggered }; };`,
+ },
+] as const;
diff --git a/src/daemon/src/pii/evaluator.test.ts b/src/daemon/src/pii/evaluator.test.ts
new file mode 100644
index 000000000..e1fc20430
--- /dev/null
+++ b/src/daemon/src/pii/evaluator.test.ts
@@ -0,0 +1,66 @@
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { PII_SOURCE_MAX_BYTES } from "@alook/shared";
+
+const state = vi.hoisted(() => ({ fail: false, stall: false, malformed: false, workers: [] as Array<{ terminate: () => Promise<number> }> }));
+vi.mock("node:worker_threads", async () => {
+ const { EventEmitter } = await import("node:events");
+ return { Worker: class extends EventEmitter {
+ constructor() {
+ super();
+ if (state.fail) throw new Error("PRIVATE_WORKER_FAILURE");
+ state.workers.push(this);
+ queueMicrotask(() => this.emit("message", { ready: true }));
+ }
+ ref() { return this; }
+ unref() { return this; }
+ terminate = vi.fn(async () => 0);
+ postMessage(message: { taskId: number; text: string; plugins: Array<{ id: string }> }) {
+ queueMicrotask(() => this.emit("message", state.malformed ? { taskId: message.taskId } : state.stall
+ ? { taskId: message.taskId, active: message.plugins[0]!.id }
+ : { taskId: message.taskId, text: message.text, triggers: [] }));
+ }
+ } };
+});
+import { compilePiiSource, PiiRunner } from "./evaluator.js";
+const runners: PiiRunner[] = [];
+const plugins = [{ id: "fixture", code: "fixture" }];
+beforeEach(() => { state.fail = false; state.stall = false; state.malformed = false; state.workers.splice(0); });
+afterEach(async () => { await Promise.all(runners.splice(0).map(runner => runner.close())); vi.useRealTimers(); });
+
+it("rejects oversized and invalid source before execution", () => {
+ expect(() => compilePiiSource("x".repeat(PII_SOURCE_MAX_BYTES + 1))).toThrow("PII_SOURCE_TOO_LARGE");
+ expect(() => compilePiiSource("export default (")).toThrow("PII_INVALID_SOURCE");
+});
+
+it("fails safely when a worker cannot start and permits a subsequent retry", async () => {
+ const runner = new PiiRunner(); runners.push(runner);
+ state.fail = true;
+ await expect(runner.evaluate(plugins, "PRIVATE_SAMPLE")).rejects.toMatchObject({ message: "PII_UNAVAILABLE", code: "PII_UNAVAILABLE" });
+ state.fail = false;
+ expect(await runner.evaluate(plugins, "recovered")).toBe("recovered");
+});
+
+it("terminates an unresponsive worker at the parent deadline and recovers the next evaluation", async () => {
+ vi.useFakeTimers();
+ state.stall = true;
+ const runner = new PiiRunner(); runners.push(runner);
+ const pending = runner.evaluate(plugins, "PRIVATE_SAMPLE");
+ const rejected = expect(pending).rejects.toMatchObject({ code: "PII_PLUGIN_TIMEOUT", pluginId: "fixture" });
+ await vi.advanceTimersByTimeAsync(251);
+ await rejected;
+ expect(state.workers[0]!.terminate).toHaveBeenCalledOnce();
+ state.stall = false;
+ const recovered = runner.evaluate(plugins, "recovered");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(await recovered).toBe("recovered");
+ expect(state.workers).toHaveLength(2);
+});
+
+it("rejects a malformed worker reply instead of returning an unverified output", async () => {
+ state.malformed = true;
+ const runner = new PiiRunner(); runners.push(runner);
+ await expect(runner.evaluate(plugins, "PRIVATE_SAMPLE")).rejects.toMatchObject({ code: "PII_PLUGIN_FAILED" });
+ expect(state.workers[0]!.terminate).toHaveBeenCalledOnce();
+ state.malformed = false;
+ expect(await runner.evaluate(plugins, "recovered")).toBe("recovered");
+});
diff --git a/src/daemon/src/pii/evaluator.ts b/src/daemon/src/pii/evaluator.ts
new file mode 100644
index 000000000..0f40ce017
--- /dev/null
+++ b/src/daemon/src/pii/evaluator.ts
@@ -0,0 +1,188 @@
+import { createRequire } from "node:module";
+import { Worker } from "node:worker_threads";
+import { transform } from "sucrase";
+import { PII_SOURCE_MAX_BYTES } from "@alook/shared";
+
+export class PiiError extends Error {
+ constructor(readonly code: string, readonly pluginId?: string) {
+ super(pluginId ? `${code}: ${pluginId}` : code);
+ }
+}
+
+export function compilePiiSource(source: string): string {
+ if (Buffer.byteLength(source, "utf8") > PII_SOURCE_MAX_BYTES) throw new PiiError("PII_SOURCE_TOO_LARGE");
+ try {
+ return transform(source, { transforms: ["typescript", "imports"], disableESTransforms: true }).code;
+ } catch {
+ throw new PiiError("PII_INVALID_SOURCE");
+ }
+}
+
+const workerSource = String.raw`
+const { parentPort, workerData } = require("node:worker_threads");
+const { getQuickJS } = require(workerData.enginePath);
+getQuickJS().then(engine => {
+ parentPort.on("message", ({ taskId, plugins, text: input }) => {
+ const send = message => parentPort.postMessage({ ...message, taskId });
+ let text = input;
+ const triggers = [];
+ for (const plugin of plugins) {
+ const runtime = engine.newRuntime();
+ runtime.setMemoryLimit(8 * 1024 * 1024);
+ runtime.setMaxStackSize(256 * 1024);
+ const deadline = Date.now() + 100;
+ runtime.setInterruptHandler(() => Date.now() > deadline);
+ const context = runtime.newContext();
+ let result;
+ try {
+ send({ active: plugin.id });
+ const program = '(function(){"use strict";const exports={};\n' + plugin.code +
+ '\nif(typeof exports.default!=="function")throw 0;const input=' + JSON.stringify(text) + ';const value=exports.default(input);' +
+ 'if(typeof value==="string")return JSON.stringify({text:value,triggered:value!==input,legacy:true});' +
+ 'if(!value||typeof value!=="object"||Array.isArray(value)||typeof value.then==="function")return "null";' +
+ 'const output=value.text,triggered=value.triggered;if(typeof output!=="string"||typeof triggered!=="boolean")return "null";' +
+ 'return JSON.stringify({text:output,triggered});})()';
+ result = context.evalCode(program);
+ if (result.error) throw 0;
+ if (context.typeof(result.value) !== "string") {
+ send({ error: "PII_INVALID_RESULT", pluginId: plugin.id });
+ return;
+ }
+ const output = JSON.parse(context.getString(result.value));
+ if (!output || typeof output.text !== "string" || typeof output.triggered !== "boolean") {
+ send({ error: "PII_INVALID_RESULT", pluginId: plugin.id });
+ return;
+ }
+ text = output.text;
+ triggers.push({ pluginId: plugin.id, triggered: output.triggered, ...(output.legacy ? { legacy: true } : {}) });
+ if (Buffer.byteLength(text, "utf8") > 64 * 1024) {
+ send({ error: "PII_OUTPUT_TOO_LARGE", pluginId: plugin.id });
+ return;
+ }
+ } catch {
+ send({ error: "PII_PLUGIN_FAILED", pluginId: plugin.id });
+ return;
+ } finally {
+ if (result) (result.error || result.value).dispose();
+ context.dispose();
+ runtime.dispose();
+ }
+ }
+ send({ text, triggers });
+ });
+ parentPort.postMessage({ ready: true });
+}).catch(() => parentPort.postMessage({ error: "PII_UNAVAILABLE" }));
+`;
+
+type CompiledPlugin = { id: string; code: string };
+export type PiiEvaluation = { text: string; triggers: { pluginId: string; triggered: boolean; legacy?: true }[] };
+
+export class PiiRunner {
+ private worker: Worker | undefined;
+ private ready = false;
+ private closed = false;
+ private pending = 0;
+ private sequence = 0;
+ private tail: Promise<unknown> = Promise.resolve();
+
+ evaluate(plugins: readonly CompiledPlugin[], text: string): Promise<string> {
+ return this.evaluateWithResult(plugins, text).then(result => result.text);
+ }
+
+ evaluateWithResult(plugins: readonly CompiledPlugin[], text: string): Promise<PiiEvaluation> {
+ if (this.closed || this.pending >= 32) return Promise.reject(new PiiError("PII_UNAVAILABLE"));
+ this.pending++;
+ return new Promise((resolve, reject) => {
+ let waiting = true;
+ const timeout = setTimeout(() => {
+ if (!waiting) return;
+ waiting = false;
+ this.pending--;
+ reject(new PiiError("PII_UNAVAILABLE"));
+ }, 3_000);
+ this.tail = this.tail.then(async () => {
+ if (!waiting) return;
+ waiting = false;
+ this.pending--;
+ clearTimeout(timeout);
+ if (this.closed) { reject(new PiiError("PII_UNAVAILABLE")); return; }
+ try { resolve(plugins.length === 0 ? { text, triggers: [] } : await this.execute(plugins, text)); }
+ catch (error) { reject(error); }
+ });
+ });
+ }
+
+ async close(): Promise<void> {
+ this.closed = true;
+ await this.worker?.terminate();
+ await this.tail;
+ }
+
+ private execute(plugins: readonly CompiledPlugin[], text: string): Promise<PiiEvaluation> {
+ return new Promise((resolve, reject) => {
+ let worker = this.worker;
+ if (!worker) {
+ try {
+ worker = new Worker(workerSource, {
+ eval: true,
+ workerData: { enginePath: createRequire(import.meta.url).resolve("quickjs-emscripten") },
+ resourceLimits: { maxOldGenerationSizeMb: 64, maxYoungGenerationSizeMb: 16, stackSizeMb: 2 },
+ });
+ this.worker = worker;
+ const clear = () => {
+ if (this.worker === worker) { this.worker = undefined; this.ready = false; }
+ };
+ worker.on("error", clear);
+ worker.on("exit", clear);
+ } catch { reject(new PiiError("PII_UNAVAILABLE")); return; }
+ }
+ const current = worker;
+ const taskId = ++this.sequence;
+ current.ref();
+ let finished = false;
+ let active: string | undefined;
+ let deadline: ReturnType<typeof setTimeout>;
+ const finish = (error?: PiiError, output?: PiiEvaluation) => {
+ if (finished) return;
+ finished = true;
+ clearTimeout(deadline);
+ current.off("message", onMessage);
+ current.off("error", onError);
+ current.off("exit", onError);
+ if (error) {
+ if (this.worker === current) { this.worker = undefined; this.ready = false; }
+ void current.terminate().then(() => reject(error));
+ } else {
+ current.unref();
+ resolve(output!);
+ }
+ };
+ const arm = (milliseconds: number) => {
+ clearTimeout(deadline);
+ deadline = setTimeout(() => finish(new PiiError("PII_PLUGIN_TIMEOUT", active)), milliseconds);
+ };
+ const start = () => { arm(250); current.postMessage({ taskId, plugins, text }); };
+ const onError = () => finish(new PiiError("PII_PLUGIN_FAILED", active));
+ const onMessage = (message: { taskId?: number; ready?: boolean; active?: string; error?: string; pluginId?: string; text?: string; triggers?: PiiEvaluation["triggers"] }) => {
+ if (message.ready) { this.ready = true; start(); }
+ else if (!this.ready && message.error === "PII_UNAVAILABLE") finish(new PiiError("PII_UNAVAILABLE"));
+ else if (message.taskId !== taskId) return;
+ else if (message.active !== undefined) { active = message.active; arm(250); }
+ else if (message.error !== undefined) finish(new PiiError(message.error, message.pluginId));
+ else if (typeof message.text === "string" && Array.isArray(message.triggers)) finish(undefined, { text: message.text, triggers: message.triggers });
+ else onError();
+ };
+ current.on("message", onMessage);
+ current.on("error", onError);
+ current.on("exit", onError);
+ if (this.ready) start();
+ else arm(3_000);
+ });
+ }
+}
+
+export async function evaluatePii(plugins: readonly CompiledPlugin[], text: string): Promise<string> {
+ const runner = new PiiRunner();
+ try { return await runner.evaluate(plugins, text); }
+ finally { await runner.close(); }
+}
diff --git a/src/daemon/src/pii/fixtures/legacy-credentials.json b/src/daemon/src/pii/fixtures/legacy-credentials.json
new file mode 100644
index 000000000..b4935123b
--- /dev/null
+++ b/src/daemon/src/pii/fixtures/legacy-credentials.json
@@ -0,0 +1,30 @@
+[
+ {
+ "id": "builtin.private-keys",
+ "name": "Private keys",
+ "description": "Redacts PEM private key blocks.",
+ "source": "export default (text: string): string => text.replace(/-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----[\\s\\S]+?-----END (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----/g, \"[REDACTED_PRIVATE_KEY]\");",
+ "enabled": true
+ },
+ {
+ "id": "builtin.api-keys",
+ "name": "API keys",
+ "description": "Redacts recognized API key and Alook credential formats.",
+ "source": "export default (text: string): string => text.replace(/(?<![A-Za-z0-9_-])(?:sk-(?:ant-|proj-|or-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}|[a-z]{2,4}_sk_[A-Za-z0-9_-]{20,}|gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,}|glpat-[A-Za-z0-9_-]{20,}|AIza[0-9A-Za-z_-]{35}|xox[abposr]-[0-9A-Za-z-]{10,}|(?:sk|rk)_live_[0-9A-Za-z]{20,}|(?:AKIA|ASIA)[0-9A-Z]{16}|hf_[A-Za-z0-9]{30,}|gsk_[A-Za-z0-9]{40,}|xai-[A-Za-z0-9]{40,}|npm_[A-Za-z0-9]{36}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|SG\\.[A-Za-z0-9_-]{22}\\.[A-Za-z0-9_-]{43}|(?:cmk|cmt|crk|vch)_[A-Za-z0-9_-]{16,})(?![A-Za-z0-9_-])/g, \"[REDACTED_API_KEY]\");",
+ "enabled": true
+ },
+ {
+ "id": "builtin.tokens",
+ "name": "Tokens",
+ "description": "Redacts JWTs and Authorization bearer/basic values.",
+ "source": "export default (text: string): string => text.replace(/(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{8,}\\.eyJ[A-Za-z0-9_-]{8,}\\.[A-Za-z0-9_-]{8,}(?![A-Za-z0-9_-])/g, \"[REDACTED_TOKEN]\").replace(/(\\b(?:Authorization\\s*:\\s*)?(?:Bearer|Basic)\\s+)[A-Za-z0-9._~+\\/-]+={0,2}/gi, (all, prefix) => /Authorization/i.test(prefix) || all.length-prefix.length >= 8 ? prefix+\"[REDACTED_TOKEN]\" : all);",
+ "enabled": true
+ },
+ {
+ "id": "builtin.secrets",
+ "name": "Passwords and secrets",
+ "description": "Redacts credential assignments and passwords in connection URLs.",
+ "source": "export default (text: string): string => text.replace(/([A-Za-z][A-Za-z0-9+.-]*:\\/\\/[^\\s:@/?#\"'<>]*:)([^\\s@/?#\"'<>]{3,})(@)/g, \"$1[REDACTED_PASSWORD]$3\").replace(/(\\b[A-Za-z0-9_.-]*(?:password|passwd|secret|token|api[_-]?key|access[_-]?key|credential)[A-Za-z0-9_.-]*[\"']?\\s*[:=]\\s*[\"']?)([A-Za-z0-9_\\-./+=~!@#%^&*]{8,})/gi, (all, prefix, value) => { const field = prefix.replace(/[\"'\\s:=]+$/g, \"\"); return /(?:id|ids|uuid|guid|hash|digest|checksum|cursor)$/i.test(field) || /^(?:process\\.env|os\\.)/.test(value) || !/[A-Za-z]/.test(value) || !/\\d/.test(value) ? all : prefix + \"[REDACTED_SECRET]\"; });",
+ "enabled": true
+ }
+]
diff --git a/src/daemon/src/pii/fixtures/phone-examples.json b/src/daemon/src/pii/fixtures/phone-examples.json
new file mode 100644
index 000000000..b09140a75
--- /dev/null
+++ b/src/daemon/src/pii/fixtures/phone-examples.json
@@ -0,0 +1,1526 @@
+[
+ {
+ "region": "AC",
+ "callingCode": "247",
+ "national": "62889",
+ "prefix": ""
+ },
+ {
+ "region": "AD",
+ "callingCode": "376",
+ "national": "712345",
+ "prefix": ""
+ },
+ {
+ "region": "AE",
+ "callingCode": "971",
+ "national": "22345678",
+ "prefix": "0"
+ },
+ {
+ "region": "AF",
+ "callingCode": "93",
+ "national": "234567890",
+ "prefix": "0"
+ },
+ {
+ "region": "AG",
+ "callingCode": "1",
+ "national": "2684601234",
+ "prefix": "1"
+ },
+ {
+ "region": "AI",
+ "callingCode": "1",
+ "national": "2644612345",
+ "prefix": "1"
+ },
+ {
+ "region": "AL",
+ "callingCode": "355",
+ "national": "22345678",
+ "prefix": "0"
+ },
+ {
+ "region": "AM",
+ "callingCode": "374",
+ "national": "10123456",
+ "prefix": "0"
+ },
+ {
+ "region": "AO",
+ "callingCode": "244",
+ "national": "222123456",
+ "prefix": ""
+ },
+ {
+ "region": "AR",
+ "callingCode": "54",
+ "national": "1123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "AS",
+ "callingCode": "1",
+ "national": "6846221234",
+ "prefix": "1"
+ },
+ {
+ "region": "AT",
+ "callingCode": "43",
+ "national": "1234567890",
+ "prefix": "0"
+ },
+ {
+ "region": "AU",
+ "callingCode": "61",
+ "national": "212345678",
+ "prefix": "0"
+ },
+ {
+ "region": "AW",
+ "callingCode": "297",
+ "national": "5212345",
+ "prefix": ""
+ },
+ {
+ "region": "AX",
+ "callingCode": "358",
+ "national": "181234567",
+ "prefix": "0"
+ },
+ {
+ "region": "AZ",
+ "callingCode": "994",
+ "national": "123123456",
+ "prefix": "0"
+ },
+ {
+ "region": "BA",
+ "callingCode": "387",
+ "national": "30212345",
+ "prefix": "0"
+ },
+ {
+ "region": "BB",
+ "callingCode": "1",
+ "national": "2464123456",
+ "prefix": "1"
+ },
+ {
+ "region": "BD",
+ "callingCode": "880",
+ "national": "27111234",
+ "prefix": "0"
+ },
+ {
+ "region": "BE",
+ "callingCode": "32",
+ "national": "12345678",
+ "prefix": "0"
+ },
+ {
+ "region": "BF",
+ "callingCode": "226",
+ "national": "20491234",
+ "prefix": ""
+ },
+ {
+ "region": "BG",
+ "callingCode": "359",
+ "national": "2123456",
+ "prefix": "0"
+ },
+ {
+ "region": "BH",
+ "callingCode": "973",
+ "national": "17001234",
+ "prefix": ""
+ },
+ {
+ "region": "BI",
+ "callingCode": "257",
+ "national": "22201234",
+ "prefix": ""
+ },
+ {
+ "region": "BJ",
+ "callingCode": "229",
+ "national": "0120211234",
+ "prefix": ""
+ },
+ {
+ "region": "BL",
+ "callingCode": "590",
+ "national": "590271234",
+ "prefix": "0"
+ },
+ {
+ "region": "BM",
+ "callingCode": "1",
+ "national": "4414123456",
+ "prefix": "1"
+ },
+ {
+ "region": "BN",
+ "callingCode": "673",
+ "national": "2345678",
+ "prefix": ""
+ },
+ {
+ "region": "BO",
+ "callingCode": "591",
+ "national": "22123456",
+ "prefix": "0"
+ },
+ {
+ "region": "BQ",
+ "callingCode": "599",
+ "national": "7151234",
+ "prefix": ""
+ },
+ {
+ "region": "BR",
+ "callingCode": "55",
+ "national": "1123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "BS",
+ "callingCode": "1",
+ "national": "2423456789",
+ "prefix": "1"
+ },
+ {
+ "region": "BT",
+ "callingCode": "975",
+ "national": "2345678",
+ "prefix": ""
+ },
+ {
+ "region": "BW",
+ "callingCode": "267",
+ "national": "2401234",
+ "prefix": ""
+ },
+ {
+ "region": "BY",
+ "callingCode": "375",
+ "national": "152450911",
+ "prefix": "8"
+ },
+ {
+ "region": "BZ",
+ "callingCode": "501",
+ "national": "2221234",
+ "prefix": ""
+ },
+ {
+ "region": "CA",
+ "callingCode": "1",
+ "national": "5062345678",
+ "prefix": "1"
+ },
+ {
+ "region": "CC",
+ "callingCode": "61",
+ "national": "891621234",
+ "prefix": "0"
+ },
+ {
+ "region": "CD",
+ "callingCode": "243",
+ "national": "1234567",
+ "prefix": "0"
+ },
+ {
+ "region": "CF",
+ "callingCode": "236",
+ "national": "21612345",
+ "prefix": ""
+ },
+ {
+ "region": "CG",
+ "callingCode": "242",
+ "national": "222123456",
+ "prefix": ""
+ },
+ {
+ "region": "CH",
+ "callingCode": "41",
+ "national": "212345678",
+ "prefix": "0"
+ },
+ {
+ "region": "CI",
+ "callingCode": "225",
+ "national": "2123456789",
+ "prefix": ""
+ },
+ {
+ "region": "CK",
+ "callingCode": "682",
+ "national": "21234",
+ "prefix": ""
+ },
+ {
+ "region": "CL",
+ "callingCode": "56",
+ "national": "600123456",
+ "prefix": ""
+ },
+ {
+ "region": "CM",
+ "callingCode": "237",
+ "national": "222123456",
+ "prefix": ""
+ },
+ {
+ "region": "CN",
+ "callingCode": "86",
+ "national": "1012345678",
+ "prefix": "0"
+ },
+ {
+ "region": "CO",
+ "callingCode": "57",
+ "national": "6012345678",
+ "prefix": "0"
+ },
+ {
+ "region": "CR",
+ "callingCode": "506",
+ "national": "22123456",
+ "prefix": ""
+ },
+ {
+ "region": "CU",
+ "callingCode": "53",
+ "national": "71234567",
+ "prefix": "0"
+ },
+ {
+ "region": "CV",
+ "callingCode": "238",
+ "national": "2211234",
+ "prefix": ""
+ },
+ {
+ "region": "CW",
+ "callingCode": "599",
+ "national": "94351234",
+ "prefix": ""
+ },
+ {
+ "region": "CX",
+ "callingCode": "61",
+ "national": "891641234",
+ "prefix": "0"
+ },
+ {
+ "region": "CY",
+ "callingCode": "357",
+ "national": "22345678",
+ "prefix": ""
+ },
+ {
+ "region": "CZ",
+ "callingCode": "420",
+ "national": "212345678",
+ "prefix": ""
+ },
+ {
+ "region": "DE",
+ "callingCode": "49",
+ "national": "30123456",
+ "prefix": "0"
+ },
+ {
+ "region": "DJ",
+ "callingCode": "253",
+ "national": "21360003",
+ "prefix": ""
+ },
+ {
+ "region": "DK",
+ "callingCode": "45",
+ "national": "32123456",
+ "prefix": ""
+ },
+ {
+ "region": "DM",
+ "callingCode": "1",
+ "national": "7674201234",
+ "prefix": "1"
+ },
+ {
+ "region": "DO",
+ "callingCode": "1",
+ "national": "8092345678",
+ "prefix": "1"
+ },
+ {
+ "region": "DZ",
+ "callingCode": "213",
+ "national": "12345678",
+ "prefix": "0"
+ },
+ {
+ "region": "EC",
+ "callingCode": "593",
+ "national": "22123456",
+ "prefix": "0"
+ },
+ {
+ "region": "EE",
+ "callingCode": "372",
+ "national": "3212345",
+ "prefix": ""
+ },
+ {
+ "region": "EG",
+ "callingCode": "20",
+ "national": "234567890",
+ "prefix": "0"
+ },
+ {
+ "region": "EH",
+ "callingCode": "212",
+ "national": "528812345",
+ "prefix": "0"
+ },
+ {
+ "region": "ER",
+ "callingCode": "291",
+ "national": "8370362",
+ "prefix": "0"
+ },
+ {
+ "region": "ES",
+ "callingCode": "34",
+ "national": "810123456",
+ "prefix": ""
+ },
+ {
+ "region": "ET",
+ "callingCode": "251",
+ "national": "111112345",
+ "prefix": "0"
+ },
+ {
+ "region": "FI",
+ "callingCode": "358",
+ "national": "131234567",
+ "prefix": "0"
+ },
+ {
+ "region": "FJ",
+ "callingCode": "679",
+ "national": "3212345",
+ "prefix": ""
+ },
+ {
+ "region": "FK",
+ "callingCode": "500",
+ "national": "31234",
+ "prefix": ""
+ },
+ {
+ "region": "FM",
+ "callingCode": "691",
+ "national": "3201234",
+ "prefix": ""
+ },
+ {
+ "region": "FO",
+ "callingCode": "298",
+ "national": "201234",
+ "prefix": ""
+ },
+ {
+ "region": "FR",
+ "callingCode": "33",
+ "national": "123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "GA",
+ "callingCode": "241",
+ "national": "01441234",
+ "prefix": ""
+ },
+ {
+ "region": "GB",
+ "callingCode": "44",
+ "national": "1212345678",
+ "prefix": "0"
+ },
+ {
+ "region": "GD",
+ "callingCode": "1",
+ "national": "4732691234",
+ "prefix": "1"
+ },
+ {
+ "region": "GE",
+ "callingCode": "995",
+ "national": "322123456",
+ "prefix": "0"
+ },
+ {
+ "region": "GF",
+ "callingCode": "594",
+ "national": "594101234",
+ "prefix": "0"
+ },
+ {
+ "region": "GG",
+ "callingCode": "44",
+ "national": "1481256789",
+ "prefix": "0"
+ },
+ {
+ "region": "GH",
+ "callingCode": "233",
+ "national": "302345678",
+ "prefix": "0"
+ },
+ {
+ "region": "GI",
+ "callingCode": "350",
+ "national": "20012345",
+ "prefix": ""
+ },
+ {
+ "region": "GL",
+ "callingCode": "299",
+ "national": "321000",
+ "prefix": ""
+ },
+ {
+ "region": "GM",
+ "callingCode": "220",
+ "national": "5661234",
+ "prefix": ""
+ },
+ {
+ "region": "GN",
+ "callingCode": "224",
+ "national": "30241234",
+ "prefix": ""
+ },
+ {
+ "region": "GP",
+ "callingCode": "590",
+ "national": "590201234",
+ "prefix": "0"
+ },
+ {
+ "region": "GQ",
+ "callingCode": "240",
+ "national": "333091234",
+ "prefix": ""
+ },
+ {
+ "region": "GR",
+ "callingCode": "30",
+ "national": "2123456789",
+ "prefix": ""
+ },
+ {
+ "region": "GT",
+ "callingCode": "502",
+ "national": "22456789",
+ "prefix": ""
+ },
+ {
+ "region": "GU",
+ "callingCode": "1",
+ "national": "6713001234",
+ "prefix": "1"
+ },
+ {
+ "region": "GW",
+ "callingCode": "245",
+ "national": "443201234",
+ "prefix": ""
+ },
+ {
+ "region": "GY",
+ "callingCode": "592",
+ "national": "2201234",
+ "prefix": ""
+ },
+ {
+ "region": "HK",
+ "callingCode": "852",
+ "national": "21234567",
+ "prefix": ""
+ },
+ {
+ "region": "HN",
+ "callingCode": "504",
+ "national": "22123456",
+ "prefix": ""
+ },
+ {
+ "region": "HR",
+ "callingCode": "385",
+ "national": "12345678",
+ "prefix": "0"
+ },
+ {
+ "region": "HT",
+ "callingCode": "509",
+ "national": "22453300",
+ "prefix": ""
+ },
+ {
+ "region": "HU",
+ "callingCode": "36",
+ "national": "12345678",
+ "prefix": "06"
+ },
+ {
+ "region": "ID",
+ "callingCode": "62",
+ "national": "218350123",
+ "prefix": "0"
+ },
+ {
+ "region": "IE",
+ "callingCode": "353",
+ "national": "2212345",
+ "prefix": "0"
+ },
+ {
+ "region": "IL",
+ "callingCode": "972",
+ "national": "21234567",
+ "prefix": "0"
+ },
+ {
+ "region": "IM",
+ "callingCode": "44",
+ "national": "1624756789",
+ "prefix": "0"
+ },
+ {
+ "region": "IN",
+ "callingCode": "91",
+ "national": "7410410123",
+ "prefix": "0"
+ },
+ {
+ "region": "IO",
+ "callingCode": "246",
+ "national": "3709100",
+ "prefix": ""
+ },
+ {
+ "region": "IQ",
+ "callingCode": "964",
+ "national": "12345678",
+ "prefix": "0"
+ },
+ {
+ "region": "IR",
+ "callingCode": "98",
+ "national": "2123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "IS",
+ "callingCode": "354",
+ "national": "4101234",
+ "prefix": ""
+ },
+ {
+ "region": "IT",
+ "callingCode": "39",
+ "national": "0212345678",
+ "prefix": ""
+ },
+ {
+ "region": "JE",
+ "callingCode": "44",
+ "national": "1534456789",
+ "prefix": "0"
+ },
+ {
+ "region": "JM",
+ "callingCode": "1",
+ "national": "8765230123",
+ "prefix": "1"
+ },
+ {
+ "region": "JO",
+ "callingCode": "962",
+ "national": "62001234",
+ "prefix": "0"
+ },
+ {
+ "region": "JP",
+ "callingCode": "81",
+ "national": "312345678",
+ "prefix": "0"
+ },
+ {
+ "region": "KE",
+ "callingCode": "254",
+ "national": "202012345",
+ "prefix": "0"
+ },
+ {
+ "region": "KG",
+ "callingCode": "996",
+ "national": "312123456",
+ "prefix": "0"
+ },
+ {
+ "region": "KH",
+ "callingCode": "855",
+ "national": "23756789",
+ "prefix": "0"
+ },
+ {
+ "region": "KI",
+ "callingCode": "686",
+ "national": "31234",
+ "prefix": "0"
+ },
+ {
+ "region": "KM",
+ "callingCode": "269",
+ "national": "7712345",
+ "prefix": ""
+ },
+ {
+ "region": "KN",
+ "callingCode": "1",
+ "national": "8692361234",
+ "prefix": "1"
+ },
+ {
+ "region": "KP",
+ "callingCode": "850",
+ "national": "21234567",
+ "prefix": "0"
+ },
+ {
+ "region": "KR",
+ "callingCode": "82",
+ "national": "22123456",
+ "prefix": "0"
+ },
+ {
+ "region": "KW",
+ "callingCode": "965",
+ "national": "22345678",
+ "prefix": ""
+ },
+ {
+ "region": "KY",
+ "callingCode": "1",
+ "national": "3452221234",
+ "prefix": "1"
+ },
+ {
+ "region": "KZ",
+ "callingCode": "7",
+ "national": "7123456789",
+ "prefix": "8"
+ },
+ {
+ "region": "LA",
+ "callingCode": "856",
+ "national": "21212862",
+ "prefix": "0"
+ },
+ {
+ "region": "LB",
+ "callingCode": "961",
+ "national": "1123456",
+ "prefix": "0"
+ },
+ {
+ "region": "LC",
+ "callingCode": "1",
+ "national": "7584305678",
+ "prefix": "1"
+ },
+ {
+ "region": "LI",
+ "callingCode": "423",
+ "national": "2345678",
+ "prefix": "0"
+ },
+ {
+ "region": "LK",
+ "callingCode": "94",
+ "national": "112345678",
+ "prefix": "0"
+ },
+ {
+ "region": "LR",
+ "callingCode": "231",
+ "national": "21234567",
+ "prefix": "0"
+ },
+ {
+ "region": "LS",
+ "callingCode": "266",
+ "national": "22123456",
+ "prefix": ""
+ },
+ {
+ "region": "LT",
+ "callingCode": "370",
+ "national": "31234567",
+ "prefix": "0"
+ },
+ {
+ "region": "LU",
+ "callingCode": "352",
+ "national": "27123456",
+ "prefix": ""
+ },
+ {
+ "region": "LV",
+ "callingCode": "371",
+ "national": "63123456",
+ "prefix": ""
+ },
+ {
+ "region": "LY",
+ "callingCode": "218",
+ "national": "212345678",
+ "prefix": "0"
+ },
+ {
+ "region": "MA",
+ "callingCode": "212",
+ "national": "520123456",
+ "prefix": "0"
+ },
+ {
+ "region": "MC",
+ "callingCode": "377",
+ "national": "99123456",
+ "prefix": "0"
+ },
+ {
+ "region": "MD",
+ "callingCode": "373",
+ "national": "22212345",
+ "prefix": "0"
+ },
+ {
+ "region": "ME",
+ "callingCode": "382",
+ "national": "30234567",
+ "prefix": "0"
+ },
+ {
+ "region": "MF",
+ "callingCode": "590",
+ "national": "590271234",
+ "prefix": "0"
+ },
+ {
+ "region": "MG",
+ "callingCode": "261",
+ "national": "202123456",
+ "prefix": "0"
+ },
+ {
+ "region": "MH",
+ "callingCode": "692",
+ "national": "2471234",
+ "prefix": "1"
+ },
+ {
+ "region": "MK",
+ "callingCode": "389",
+ "national": "22012345",
+ "prefix": "0"
+ },
+ {
+ "region": "ML",
+ "callingCode": "223",
+ "national": "20212345",
+ "prefix": ""
+ },
+ {
+ "region": "MM",
+ "callingCode": "95",
+ "national": "1234567",
+ "prefix": "0"
+ },
+ {
+ "region": "MN",
+ "callingCode": "976",
+ "national": "53123456",
+ "prefix": "0"
+ },
+ {
+ "region": "MO",
+ "callingCode": "853",
+ "national": "28212345",
+ "prefix": ""
+ },
+ {
+ "region": "MP",
+ "callingCode": "1",
+ "national": "6702345678",
+ "prefix": "1"
+ },
+ {
+ "region": "MQ",
+ "callingCode": "596",
+ "national": "596301234",
+ "prefix": "0"
+ },
+ {
+ "region": "MR",
+ "callingCode": "222",
+ "national": "35123456",
+ "prefix": ""
+ },
+ {
+ "region": "MS",
+ "callingCode": "1",
+ "national": "6644912345",
+ "prefix": "1"
+ },
+ {
+ "region": "MT",
+ "callingCode": "356",
+ "national": "21001234",
+ "prefix": ""
+ },
+ {
+ "region": "MU",
+ "callingCode": "230",
+ "national": "54480123",
+ "prefix": ""
+ },
+ {
+ "region": "MV",
+ "callingCode": "960",
+ "national": "6701234",
+ "prefix": ""
+ },
+ {
+ "region": "MW",
+ "callingCode": "265",
+ "national": "1234567",
+ "prefix": "0"
+ },
+ {
+ "region": "MX",
+ "callingCode": "52",
+ "national": "2001234567",
+ "prefix": ""
+ },
+ {
+ "region": "MY",
+ "callingCode": "60",
+ "national": "323856789",
+ "prefix": "0"
+ },
+ {
+ "region": "MZ",
+ "callingCode": "258",
+ "national": "21123456",
+ "prefix": ""
+ },
+ {
+ "region": "NA",
+ "callingCode": "264",
+ "national": "61221234",
+ "prefix": "0"
+ },
+ {
+ "region": "NC",
+ "callingCode": "687",
+ "national": "201234",
+ "prefix": ""
+ },
+ {
+ "region": "NE",
+ "callingCode": "227",
+ "national": "20201234",
+ "prefix": ""
+ },
+ {
+ "region": "NF",
+ "callingCode": "672",
+ "national": "106609",
+ "prefix": ""
+ },
+ {
+ "region": "NG",
+ "callingCode": "234",
+ "national": "2033123456",
+ "prefix": "0"
+ },
+ {
+ "region": "NI",
+ "callingCode": "505",
+ "national": "21234567",
+ "prefix": ""
+ },
+ {
+ "region": "NL",
+ "callingCode": "31",
+ "national": "101234567",
+ "prefix": "0"
+ },
+ {
+ "region": "NO",
+ "callingCode": "47",
+ "national": "21234567",
+ "prefix": ""
+ },
+ {
+ "region": "NP",
+ "callingCode": "977",
+ "national": "14567890",
+ "prefix": "0"
+ },
+ {
+ "region": "NR",
+ "callingCode": "674",
+ "national": "4441234",
+ "prefix": ""
+ },
+ {
+ "region": "NU",
+ "callingCode": "683",
+ "national": "7012",
+ "prefix": ""
+ },
+ {
+ "region": "NZ",
+ "callingCode": "64",
+ "national": "32345678",
+ "prefix": "0"
+ },
+ {
+ "region": "OM",
+ "callingCode": "968",
+ "national": "23123456",
+ "prefix": ""
+ },
+ {
+ "region": "PA",
+ "callingCode": "507",
+ "national": "2001234",
+ "prefix": ""
+ },
+ {
+ "region": "PE",
+ "callingCode": "51",
+ "national": "11234567",
+ "prefix": "0"
+ },
+ {
+ "region": "PF",
+ "callingCode": "689",
+ "national": "40412345",
+ "prefix": ""
+ },
+ {
+ "region": "PG",
+ "callingCode": "675",
+ "national": "3123456",
+ "prefix": ""
+ },
+ {
+ "region": "PH",
+ "callingCode": "63",
+ "national": "232345678",
+ "prefix": "0"
+ },
+ {
+ "region": "PK",
+ "callingCode": "92",
+ "national": "2123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "PL",
+ "callingCode": "48",
+ "national": "123456789",
+ "prefix": ""
+ },
+ {
+ "region": "PM",
+ "callingCode": "508",
+ "national": "430123",
+ "prefix": "0"
+ },
+ {
+ "region": "PR",
+ "callingCode": "1",
+ "national": "7872345678",
+ "prefix": "1"
+ },
+ {
+ "region": "PS",
+ "callingCode": "970",
+ "national": "22234567",
+ "prefix": "0"
+ },
+ {
+ "region": "PT",
+ "callingCode": "351",
+ "national": "212345678",
+ "prefix": ""
+ },
+ {
+ "region": "PW",
+ "callingCode": "680",
+ "national": "2771234",
+ "prefix": ""
+ },
+ {
+ "region": "PY",
+ "callingCode": "595",
+ "national": "212345678",
+ "prefix": "0"
+ },
+ {
+ "region": "QA",
+ "callingCode": "974",
+ "national": "44123456",
+ "prefix": ""
+ },
+ {
+ "region": "RE",
+ "callingCode": "262",
+ "national": "262161234",
+ "prefix": "0"
+ },
+ {
+ "region": "RO",
+ "callingCode": "40",
+ "national": "211234567",
+ "prefix": "0"
+ },
+ {
+ "region": "RS",
+ "callingCode": "381",
+ "national": "10234567",
+ "prefix": "0"
+ },
+ {
+ "region": "RU",
+ "callingCode": "7",
+ "national": "3011234567",
+ "prefix": "8"
+ },
+ {
+ "region": "RW",
+ "callingCode": "250",
+ "national": "250123456",
+ "prefix": "0"
+ },
+ {
+ "region": "SA",
+ "callingCode": "966",
+ "national": "112345678",
+ "prefix": "0"
+ },
+ {
+ "region": "SB",
+ "callingCode": "677",
+ "national": "40123",
+ "prefix": ""
+ },
+ {
+ "region": "SC",
+ "callingCode": "248",
+ "national": "4217123",
+ "prefix": ""
+ },
+ {
+ "region": "SD",
+ "callingCode": "249",
+ "national": "153123456",
+ "prefix": "0"
+ },
+ {
+ "region": "SE",
+ "callingCode": "46",
+ "national": "8123456",
+ "prefix": "0"
+ },
+ {
+ "region": "SG",
+ "callingCode": "65",
+ "national": "61234567",
+ "prefix": ""
+ },
+ {
+ "region": "SH",
+ "callingCode": "290",
+ "national": "22158",
+ "prefix": ""
+ },
+ {
+ "region": "SI",
+ "callingCode": "386",
+ "national": "12345678",
+ "prefix": "0"
+ },
+ {
+ "region": "SJ",
+ "callingCode": "47",
+ "national": "79123456",
+ "prefix": ""
+ },
+ {
+ "region": "SK",
+ "callingCode": "421",
+ "national": "221234567",
+ "prefix": "0"
+ },
+ {
+ "region": "SL",
+ "callingCode": "232",
+ "national": "22221234",
+ "prefix": "0"
+ },
+ {
+ "region": "SM",
+ "callingCode": "378",
+ "national": "0549886377",
+ "prefix": ""
+ },
+ {
+ "region": "SN",
+ "callingCode": "221",
+ "national": "301012345",
+ "prefix": ""
+ },
+ {
+ "region": "SO",
+ "callingCode": "252",
+ "national": "4012345",
+ "prefix": "0"
+ },
+ {
+ "region": "SR",
+ "callingCode": "597",
+ "national": "211234",
+ "prefix": ""
+ },
+ {
+ "region": "SS",
+ "callingCode": "211",
+ "national": "181234567",
+ "prefix": "0"
+ },
+ {
+ "region": "ST",
+ "callingCode": "239",
+ "national": "2221234",
+ "prefix": ""
+ },
+ {
+ "region": "SV",
+ "callingCode": "503",
+ "national": "21234567",
+ "prefix": ""
+ },
+ {
+ "region": "SX",
+ "callingCode": "1",
+ "national": "7215425678",
+ "prefix": "1"
+ },
+ {
+ "region": "SY",
+ "callingCode": "963",
+ "national": "112345678",
+ "prefix": "0"
+ },
+ {
+ "region": "SZ",
+ "callingCode": "268",
+ "national": "22171234",
+ "prefix": ""
+ },
+ {
+ "region": "TA",
+ "callingCode": "290",
+ "national": "8999",
+ "prefix": ""
+ },
+ {
+ "region": "TC",
+ "callingCode": "1",
+ "national": "6497121234",
+ "prefix": "1"
+ },
+ {
+ "region": "TD",
+ "callingCode": "235",
+ "national": "22501234",
+ "prefix": ""
+ },
+ {
+ "region": "TG",
+ "callingCode": "228",
+ "national": "22212345",
+ "prefix": ""
+ },
+ {
+ "region": "TH",
+ "callingCode": "66",
+ "national": "21234567",
+ "prefix": "0"
+ },
+ {
+ "region": "TJ",
+ "callingCode": "992",
+ "national": "372123456",
+ "prefix": ""
+ },
+ {
+ "region": "TK",
+ "callingCode": "690",
+ "national": "3101",
+ "prefix": ""
+ },
+ {
+ "region": "TL",
+ "callingCode": "670",
+ "national": "2112345",
+ "prefix": ""
+ },
+ {
+ "region": "TM",
+ "callingCode": "993",
+ "national": "12345678",
+ "prefix": "8"
+ },
+ {
+ "region": "TN",
+ "callingCode": "216",
+ "national": "30010123",
+ "prefix": ""
+ },
+ {
+ "region": "TO",
+ "callingCode": "676",
+ "national": "20123",
+ "prefix": ""
+ },
+ {
+ "region": "TR",
+ "callingCode": "90",
+ "national": "2123456789",
+ "prefix": "0"
+ },
+ {
+ "region": "TT",
+ "callingCode": "1",
+ "national": "8682211234",
+ "prefix": "1"
+ },
+ {
+ "region": "TV",
+ "callingCode": "688",
+ "national": "20123",
+ "prefix": ""
+ },
+ {
+ "region": "TW",
+ "callingCode": "886",
+ "national": "221234567",
+ "prefix": "0"
+ },
+ {
+ "region": "TZ",
+ "callingCode": "255",
+ "national": "222345678",
+ "prefix": "0"
+ },
+ {
+ "region": "UA",
+ "callingCode": "380",
+ "national": "311234567",
+ "prefix": "0"
+ },
+ {
+ "region": "UG",
+ "callingCode": "256",
+ "national": "312345678",
+ "prefix": "0"
+ },
+ {
+ "region": "US",
+ "callingCode": "1",
+ "national": "2015550123",
+ "prefix": "1"
+ },
+ {
+ "region": "UY",
+ "callingCode": "598",
+ "national": "21231234",
+ "prefix": "0"
+ },
+ {
+ "region": "UZ",
+ "callingCode": "998",
+ "national": "669050123",
+ "prefix": ""
+ },
+ {
+ "region": "VA",
+ "callingCode": "39",
+ "national": "0669812345",
+ "prefix": ""
+ },
+ {
+ "region": "VC",
+ "callingCode": "1",
+ "national": "7842661234",
+ "prefix": "1"
+ },
+ {
+ "region": "VE",
+ "callingCode": "58",
+ "national": "2121234567",
+ "prefix": "0"
+ },
+ {
+ "region": "VG",
+ "callingCode": "1",
+ "national": "2842291234",
+ "prefix": "1"
+ },
+ {
+ "region": "VI",
+ "callingCode": "1",
+ "national": "3406421234",
+ "prefix": "1"
+ },
+ {
+ "region": "VN",
+ "callingCode": "84",
+ "national": "2101234567",
+ "prefix": "0"
+ },
+ {
+ "region": "VU",
+ "callingCode": "678",
+ "national": "22123",
+ "prefix": ""
+ },
+ {
+ "region": "WF",
+ "callingCode": "681",
+ "national": "721234",
+ "prefix": ""
+ },
+ {
+ "region": "WS",
+ "callingCode": "685",
+ "national": "22123",
+ "prefix": ""
+ },
+ {
+ "region": "XK",
+ "callingCode": "383",
+ "national": "28012345",
+ "prefix": "0"
+ },
+ {
+ "region": "YE",
+ "callingCode": "967",
+ "national": "1234567",
+ "prefix": "0"
+ },
+ {
+ "region": "YT",
+ "callingCode": "262",
+ "national": "269601234",
+ "prefix": "0"
+ },
+ {
+ "region": "ZA",
+ "callingCode": "27",
+ "national": "101234567",
+ "prefix": "0"
+ },
+ {
+ "region": "ZM",
+ "callingCode": "260",
+ "national": "211234567",
+ "prefix": "0"
+ },
+ {
+ "region": "ZW",
+ "callingCode": "263",
+ "national": "1312345",
+ "prefix": "0"
+ },
+ {
+ "region": "001",
+ "callingCode": "800",
+ "national": "12345678",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "808",
+ "national": "12345678",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "870",
+ "national": "301234567",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "878",
+ "national": "101234567890",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "881",
+ "national": "612345678",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "882",
+ "national": "3421234",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "883",
+ "national": "510012345",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "888",
+ "national": "12345678901",
+ "prefix": ""
+ },
+ {
+ "region": "001",
+ "callingCode": "979",
+ "national": "123456789",
+ "prefix": ""
+ }
+]
diff --git a/src/daemon/src/pii/id-numbers.test.ts b/src/daemon/src/pii/id-numbers.test.ts
new file mode 100644
index 000000000..380ca949a
--- /dev/null
+++ b/src/daemon/src/pii/id-numbers.test.ts
@@ -0,0 +1,151 @@
+import { afterAll, expect, it } from "vitest";
+import { PiiSnapshotSchema, PII_SOURCE_MAX_BYTES, PII_TEXT_MAX_BYTES } from "@alook/shared";
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { PiiStore } from "./store.js";
+import { PII_DEFAULTS } from "./defaults.js";
+import { compilePiiSource, PiiRunner } from "./evaluator.js";
+
+const plugin = PII_DEFAULTS.find(plugin => plugin.id === "builtin.id-cards")!;
+const runner = new PiiRunner();
+const compiled = [{ id: plugin.id, code: compilePiiSource(plugin.source) }];
+const marker = "[REDACTED_ID_CARD]";
+const filter = (text: string) => runner.evaluate(compiled, text);
+afterAll(() => runner.close());
+
+const cases = [
+ ["CN", "11010519491231002X", "110105194912310020"],
+ ["HK", "A123456(3)", "A123456(8)"],
+ ["HK two letters", "AB123456(9)", "AB123456(8)"],
+ ["TW", "A123456789", "A123456788"],
+ ["ES DNI", "12345678Z", "12345678A"],
+ ["ES NIE", "X1234567L", "X1234567A"],
+ ["IT CF", "RSSMRA85T10A562S", "RSSMRA85T10A562A"],
+ ["US", "SSN: 219-09-9999", "SSN: 666-09-9999"],
+ ["CA", "SIN: 123-456-782", "SIN: 123-456-783"],
+ ["UK", "NINO: AB 12 34 56 C", "NINO: BG 12 34 56 C"],
+ ["DE tax", "Steuer-ID: 36574261809", "Steuer-ID: 36574261890"],
+ ["DE card", "Personalausweis: L01X00T47", "Personalausweis: A01X00T47"],
+ ["FR NIR", "NIR: 1 84 12 76 451 089 46", "NIR: 1 84 12 76 451 089 45"],
+ ["FR Corsica A", "NIR: 180062A12345603", "NIR: 180062A12345604"],
+ ["FR Corsica B", "NIR: 180062B12345630", "NIR: 180062B12345631"],
+ ["FR old card", "CNI: 990112345678", "CNI: 9901123456789"],
+ ["FR new card", "CNI: A1B2C3D4E", "CNI: A1B2C3D4"],
+ ["IT card", "CIE: CA12345AA", "CIE: CA1234AA"],
+ ["IN", "Aadhaar: 2345 6789 0124", "Aadhaar: 2345 6789 0125"],
+ ["JP", "My Number: 6214 9832 0257", "My Number: 6214 9832 0258"],
+ ["KR", "RRN: 850102-1234567", "RRN: 850230-1234567"],
+ ["KR foreign", "외국인등록번호: 000229-7123456", "외국인등록번호: 000230-7123456"],
+ ["SG", "NRIC: S1234567D", "NRIC: S123456D"],
+ ["SG M FIN", "FIN: M1234567B", "FIN: M1234567BB"],
+ ["AU", "TFN: 876 543 210", "TFN: 876 543 211"],
+ ["BR", "CPF: 280.012.389-38", "CPF: 280.012.389-39"],
+] as const;
+
+it.each(cases)("redacts %s and preserves its invalid counterpart", async (_name, positive, negative) => {
+ const prefix = positive.slice(0, positive.lastIndexOf(":") + 1);
+ expect(await filter(positive)).toBe(prefix ? `${prefix} ${marker}` : marker);
+ expect(await filter(negative)).toBe(negative);
+});
+
+it("keeps the existing node and replacement IDs within the persisted/isolated contract", () => {
+ expect(plugin.name).toBe("ID numbers");
+ expect(Buffer.byteLength(plugin.source)).toBeLessThanOrEqual(PII_SOURCE_MAX_BYTES);
+ expect(PiiSnapshotSchema.safeParse({ revision: 0, plugins: PII_DEFAULTS.map(({ source: _source, ...item }) => ({ ...item, enabled: true })) }).success).toBe(true);
+});
+
+it.each([
+ ['{"ssn": "219099999"}', `{"ssn": "${marker}"}`],
+ ["'SiN': '123456782'", `'SiN': '${marker}'`],
+ ["social_security_number=219099999", `social_security_number=${marker}`],
+ ["Numéro d'assurance sociale: 123456782", `Numéro d'assurance sociale: ${marker}`],
+ ["国民の個人番号:621498320257", `国民の個人番号:${marker}`],
+ ["マイナンバー 621498320257", `マイナンバー ${marker}`],
+ ["주민등록번호: 8501021234567", `주민등록번호: ${marker}`],
+ ["numéro de sécurité sociale: 184127645108946", `numéro de sécurité sociale: ${marker}`],
+ ["Steueridentifikationsnummer 36574261809", `Steueridentifikationsnummer ${marker}`],
+ ["आधार संख्या: 234567890124", `आधार संख्या: ${marker}`],
+ ["CNI n°: A1B2C3D4E", `CNI n°: ${marker}`],
+ ["IdNr.: 36 574 261 809", `IdNr.: ${marker}`],
+ ["Steuer-ID: 65929970489", `Steuer-ID: ${marker}`],
+ ["a123456(3), 12345678z.", `${marker}, ${marker}.`],
+])("recognizes labeled, JSON, local-language and case variants: %s", async (input, output) => {
+ expect(await filter(input)).toBe(output);
+});
+
+it.each([
+ "order: 219099999", "order: 123456782", "order: 36574261809", "order: 184127645108946",
+ "order: 234567890124", "order: 621498320257", "order: 8501021234567", "order: 876543210", "order: 28001238938",
+ "SKU AB123456C", "SKU S1234567D", "SKU M1234567B", "SKU L01X00T47", "SKU A1B2C3D4E", "SKU CA12345AA",
+ "other_ssn: 219099999", "notssn: 219099999", "SSNish: 219099999", "ssn:\n219099999",
+ `ssn:${" ".repeat(100)}219099999`, `other_ssn:${" ".repeat(92)}219099999`, "SSN: order 219099999", "id: 219099999",
+ "SSN: 000-09-9999", "SSN: 900-09-9999", "SSN: 219-00-9999", "SSN: 219-09-0000", "SSN: 111-11-1111",
+ "CN 11010519490231002X", "CN 11010519000229002X", "RSSMRA85T40A562S",
+ "Steuer-ID: 11123456789", "Steuer-ID: 36554266806",
+ "SIN: 823456785", "NINO: QQ123456C", "NINO: AO123456C", "NINO: AB123456E",
+ "Aadhaar: 123456789010", "My Number: 000000000000", "TFN: 000000000", "CPF: 111.111.111-11",
+ "RRN: 000229-1123456", "RRN: 850102-9234567", "NIR: 1 84 13 76 451 089 35", "NIR: 1841276451089",
+ "x11010519491231002X", "11010519491231002Xy", "x_12345678Z", "12345678Z_y",
+ "order-12345678Z", "12345678Z-part", "/files/12345678Z/data", "12345678Z.json",
+ "SSN: 12190999990", "SIN: 0123456782", "00000000-0000-0000-0000-123456782000",
+ "aabbccdd11010519491231002Xaabbccdd", "SSN: 219-09-9999-01", "SIN: 123 456 782 9", "TFN: 876 543 210 9",
+])("preserves non-IDs, invalid structure and token/label boundaries: %s", async input => {
+ expect(await filter(input)).toBe(input);
+});
+
+it("handles a multi-country message and maximum-size adversarial numeric input inside the real QuickJS budget", async () => {
+ const input = 'SSN: 219099999; SIN: 123456782; NINO: AB123456C; 12345678Z; My Number: 621498320257; CPF: 28001238938.';
+ expect(await filter(input)).toBe(`SSN: ${marker}; SIN: ${marker}; NINO: ${marker}; ${marker}; My Number: ${marker}; CPF: ${marker}.`);
+ for (const input of ["9".repeat(PII_TEXT_MAX_BYTES), "A".repeat(PII_TEXT_MAX_BYTES), "1234 ".repeat(204)]) {
+ expect(await filter(input)).toBe(input);
+ }
+});
+
+
+it("preserves unlabeled ordinary references through the entire default filter chain, while redacting labeled IDs", async () => {
+ const directory = mkdtempSync(join(tmpdir(), "pii-id-chain-"));
+ const store = new PiiStore(directory);
+ try {
+ expect(await filter("13800138018")).toBe("13800138018");
+ expect(await store.filter("a", "13800138018")).toBe("[REDACTED_PHONE]");
+ expect(await store.filter("a", "CPF: 13800138018")).toBe(`CPF: ${marker}`);
+ const input = "orders: 219099999, 123456782, 36574261809, 184127645108946, 234567890124, 621498320257, 8501021234567, 876543210, 28001238938.";
+ expect(await store.filter("a", input)).toBe(input);
+ const mixed = 'SSN: 219099999; SIN: 123456782; NINO: AB123456C; 12345678Z; My Number: 621498320257; CPF: 28001238938; order 219099999.';
+ const output = `SSN: ${marker}; SIN: ${marker}; NINO: ${marker}; ${marker}; My Number: ${marker}; CPF: ${marker}; order 219099999.`;
+ expect(await store.filter("a", mixed)).toBe(output);
+ expect(await store.manage("a", { action: "preview", text: mixed, expectedRevision: 0 }))
+ .toMatchObject({ status: "complete", output });
+ await store.manage("a", { action: "setEnabled", pluginId: plugin.id, enabled: false, expectedRevision: 0 });
+ expect(await store.filter("a", mixed)).toBe(mixed);
+ await store.manage("a", { action: "remove", pluginId: plugin.id, expectedRevision: 1 });
+ expect(await store.filter("a", mixed)).toBe(mixed);
+ expect(await store.filter("a", "phone: 13800138000; card: 4111 1111 1111 1111")).toBe("phone: [REDACTED_PHONE]; card: [REDACTED_BANK_CARD]");
+ } finally { await store.close(); rmSync(directory, { recursive: true, force: true }); }
+});
+
+it("keeps initialized rule copies, flags, custom nodes and order unchanged on restart", async () => {
+ const directory = mkdtempSync(join(tmpdir(), "pii-id-existing-"));
+ const store = new PiiStore(directory);
+ let restarted: PiiStore | undefined;
+ try {
+ await store.local("a", { action: "add", name: "custom", description: "retained fixture", source: 'export default (text: string) => text.replaceAll("project-private", "[CUSTOM]");' });
+ await store.manage("a", { action: "setEnabled", pluginId: "builtin.emails", enabled: false, expectedRevision: 1 });
+ const file = join(directory, "a", "pii", "pipelines.json");
+ const saved = JSON.parse(readFileSync(file, "utf8"));
+ const idNode = saved.plugins.find((item: { id: string }) => item.id === plugin.id);
+ idNode.name = "Chinese ID cards";
+ idNode.description = "An already initialized local source copy";
+ idNode.source = 'export default (text: string) => text;';
+ const bytes = JSON.stringify(saved);
+ writeFileSync(file, bytes);
+ restarted = new PiiStore(directory);
+ expect(restarted.list("a").plugins.map(item => [item.id, item.enabled])).toEqual(saved.plugins.map((item: { id: string; enabled: boolean }) => [item.id, item.enabled]));
+ expect(restarted.list("a").plugins.find(item => item.id === plugin.id)?.name).toBe("Chinese ID cards");
+ expect(await restarted.filter("a", "SSN: 219099999 project-private [email protected]")).toBe("SSN: 219099999 [CUSTOM] [email protected]");
+ expect(readFileSync(file, "utf8")).toBe(bytes);
+ expect(restarted.list("fresh").plugins.find(item => item.id === plugin.id)?.name).toBe("ID numbers");
+ expect(await restarted.filter("fresh", "SSN: 219099999")).toBe(`SSN: ${marker}`);
+ } finally { await restarted?.close(); await store.close(); rmSync(directory, { recursive: true, force: true }); }
+});
diff --git a/src/daemon/src/pii/id-numbers.ts b/src/daemon/src/pii/id-numbers.ts
new file mode 100644
index 000000000..ad63ee8a8
--- /dev/null
+++ b/src/daemon/src/pii/id-numbers.ts
@@ -0,0 +1,113 @@
+export const ID_NUMBERS_SOURCE = String.raw`export default (text: string) => {
+ let triggered = false;
+ const marker = "[REDACTED_ID_CARD]";
+ const digits = (value: string) => [...value].map(Number);
+ const weighted = (value: string, weights: number[]) => digits(value).reduce((sum, n, i) => sum + n * weights[i], 0);
+ const date = (year: number, month: number, day: number) => {
+ const value = new Date(Date.UTC(year, month - 1, day));
+ return value.getUTCFullYear() === year && value.getUTCMonth() === month - 1 && value.getUTCDate() === day;
+ };
+ const label = (names: string) => new RegExp("(?:^|[^A-Za-z0-9_])(?:" + names + ")(?:[ _-]*(?:number|no\\.?|nummer|n[°º]))?[\"']?[ \\t]*[:=#:]?[ \\t]*[\"']?$", "i");
+ const redact = (pattern: RegExp, valid: (value: string) => boolean, context?: RegExp) => {
+ text = text.replace(pattern, (value: string, offset: number, body: string) => {
+ const before = body.slice(0, offset), after = body.slice(offset + value.length);
+ if (/[A-Za-z0-9_]$/.test(before) || /^[A-Za-z0-9_]/.test(after) || /[A-Za-z0-9_][./-]$/.test(before) || /^[./-][A-Za-z0-9_]/.test(after)) return value;
+ const contextMatch = context?.exec(before);
+ if (context && (!contextMatch || contextMatch[0].length > 96 || /^\d/.test(value) && /^[ .-]\d/.test(after))) return value;
+ if (!valid(value.replace(/[ .()-]/g, "").toUpperCase())) return value;
+ triggered = true;
+ return marker;
+ });
+ };
+ const varied = (value: string) => !/^(\d)\1+$/.test(value);
+
+ redact(/[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]/g, value =>
+ date(+value.slice(6, 10), +value.slice(10, 12), +value.slice(12, 14)) && value.slice(14, 17) !== "000" &&
+ "10X98765432"[weighted(value.slice(0, 17), [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]) % 11] === value[17]);
+
+ redact(/[A-Z]{1,2}\d{6}(?:\([\dA]\)|[\dA])/gi, value => {
+ const body = value.slice(0, -1).padStart(8, " ");
+ const sum = [...body].reduce((sum, c, i) => sum + (c === " " ? 36 : /[A-Z]/.test(c) ? c.charCodeAt(0) - 55 : +c) * (9 - i), 0);
+ return "0123456789A"[(11 - sum % 11) % 11] === value.at(-1);
+ });
+ redact(/[A-Z][12]\d{8}/gi, value => {
+ const code = [10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33][value.charCodeAt(0) - 65];
+ return (Math.floor(code / 10) + code % 10 * 9 + weighted(value.slice(1), [8,7,6,5,4,3,2,1,1])) % 10 === 0;
+ });
+ redact(/(?:\d{8}|[XYZ]\d{7})[ -]?[A-Z]/gi, value => {
+ const body = value.slice(0, -1).replace(/^[XYZ]/, c => String("XYZ".indexOf(c)));
+ return "TRWAGMYFPDXBNJZSQVHLCKE"[+body % 23] === value.at(-1);
+ });
+ redact(/[A-Z]{6}[\dLMNPQRSTUV]{2}[ABCDEHLMPRST][\dLMNPQRSTUV]{2}[A-Z][\dLMNPQRSTUV]{3}[A-Z]/gi, value => {
+ const decode = (s: string) => +s.replace(/[LMNPQRSTUV]/g, c => String("LMNPQRSTUV".indexOf(c)));
+ const day = decode(value.slice(9, 11));
+ if (!date(2000 + decode(value.slice(6, 8)), "ABCDEHLMPRST".indexOf(value[8]) + 1, day > 40 ? day - 40 : day)) return false;
+ const odd = [1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23];
+ const sum = [...value.slice(0, 15)].reduce((sum, c, i) => {
+ const n = /\d/.test(c) ? +c : c.charCodeAt(0) - 65;
+ return sum + (i % 2 === 0 ? odd[n] : n);
+ }, 0);
+ return String.fromCharCode(65 + sum % 26) === value[15];
+ });
+
+ redact(/\d{3}[- ]?\d{2}[- ]?\d{4}/g, value =>
+ varied(value) && !/^(?:000|666|9\d{2})/.test(value) && value.slice(3, 5) !== "00" && value.slice(5) !== "0000",
+ label("ssn|social[ _-]+security(?:[ _-]+number)?"));
+ redact(/\d{3}[- ]?\d{3}[- ]?\d{3}/g, value => {
+ const sum = digits(value).reduce((sum, n, i) => { const doubled = n * (i % 2 ? 2 : 1); return sum + (doubled > 9 ? doubled - 9 : doubled); }, 0);
+ return !/^[08]/.test(value) && varied(value) && sum % 10 === 0;
+ }, label("sin|nas|social[ _-]+insurance(?:[ _-]+number)?|numéro[ ]+d['’]assurance[ ]+sociale"));
+ redact(/[A-Z]{2}(?: ?\d{2}){3} ?[A-D]/gi, value =>
+ /^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]/.test(value) && !/^(?:BG|GB|KN|NK|NT|TN|ZZ)/.test(value),
+ label("nino|national[ _-]+insurance(?:[ _-]+number)?"));
+ redact(/[1-9]\d ?\d{3} ?\d{3} ?\d{3}/g, value => {
+ const counts = Array(10).fill(0);
+ for (const n of value.slice(0, 10)) counts[+n]++;
+ const sorted = counts.filter(n => n > 0).sort().join("");
+ if ((sorted !== "111111112" && sorted !== "11111113") || /(\d)\1\1/.test(value.slice(0, 10))) return false;
+ let product = 10;
+ for (const n of value.slice(0, 10)) product = 2 * ((+n + product) % 10 || 10) % 11;
+ return (11 - product) % 10 === +value[10];
+ }, label("steuer[ -]?id|steueridentifikationsnummer|idnr\\.?|german[ ]+tax[ ]+id"));
+ redact(/[CFGHJKLMNPRTVWXYZ][0-9CFGHJKLMNPRTVWXYZ]{8}/gi, () => true,
+ label("personalausweis(?:nummer)?|ausweisnummer|german[ ]+id"));
+ redact(/[12] ?\d{2} ?\d{2} ?(?:\d{2}|2[AB]) ?\d{3} ?\d{3} ?\d{2}/gi, value => {
+ const month = +value.slice(3, 5);
+ const numeric = value.slice(0, 13).replace("2A", "19").replace("2B", "18");
+ return (month >= 1 && month <= 12 || month >= 20 && month <= 99) && 97 - +numeric % 97 === +value.slice(13);
+ }, label("nir|numéro[ ]+(?:de[ ]+)?sécurité[ ]+sociale|french[ ]+social[ ]+security"));
+ redact(/(?:\d{12}|[A-Z0-9]{9})/gi, () => true,
+ label("cni|carte[ ]+nationale[ ]+d['’]identité|french[ ]+id"));
+ redact(/[A-Z]{2}\d{5}[A-Z]{2}/gi, () => true,
+ label("cie|carta[ ]+d['’]identità|italian[ ]+id"));
+
+ redact(/[2-9]\d{3}[ -]?\d{4}[ -]?\d{4}/g, value => {
+ const product = ["0123456789","1234067895","2340178956","3401289567","4012395678","5987604321","6598710432","7659821043","8765932104","9876543210"];
+ const permutation = ["0123456789","1576283094","5803796142","8916043527","9453126870","4286573901","2793806415","7046913258"];
+ let check = 0;
+ digits(value).reverse().forEach((n, i) => { check = +product[check][+permutation[i % 8][n]]; });
+ return varied(value) && check === 0;
+ }, label("aadhaar|aadhar|आधार(?:[ ]+(?:नंबर|संख्या))?"));
+ redact(/\d{4}[ -]?\d{4}[ -]?\d{4}/g, value => {
+ const remainder = weighted(value.slice(0, 11), [6,5,4,3,2,7,6,5,4,3,2]) % 11;
+ return varied(value) && (remainder <= 1 ? 0 : 11 - remainder) === +value[11];
+ }, label("my[ _-]+number|個人番号|マイナンバー"));
+ redact(/\d{6}[- ]?[1-8]\d{6}/g, value => {
+ const century = [1900,1900,2000,2000,1900,1900,2000,2000][+value[6] - 1];
+ return date(century + +value.slice(0, 2), +value.slice(2, 4), +value.slice(4, 6));
+ }, label("rrn|resident[ _-]+registration(?:[ _-]+number)?|주민등록번호|외국인등록번호"));
+ redact(/[STFGM]\d{7}[A-Z]/gi, () => true, label("nric|fin"));
+ redact(/\d{3} ?\d{3} ?\d{3}/g, value =>
+ varied(value) && weighted(value, [1,4,3,7,5,8,6,9,10]) % 11 === 0,
+ label("tfn|tax[ _-]+file(?:[ _-]+number)?"));
+ redact(/\d{3}\.?\d{3}\.?\d{3}-?\d{2}/g, value => {
+ if (!varied(value)) return false;
+ for (let size = 9; size <= 10; size++) {
+ const sum = digits(value.slice(0, size)).reduce((sum, n, i) => sum + n * (size + 1 - i), 0);
+ const remainder = sum % 11;
+ if ((remainder < 2 ? 0 : 11 - remainder) !== +value[size]) return false;
+ }
+ return true;
+ }, label("cpf|cadastro[ ]+de[ ]+pessoas[ ]+físicas"));
+ return { text, triggered };
+};`;
diff --git a/src/daemon/src/pii/passwords-and-keys.test.ts b/src/daemon/src/pii/passwords-and-keys.test.ts
new file mode 100644
index 000000000..3f3b58a0a
--- /dev/null
+++ b/src/daemon/src/pii/passwords-and-keys.test.ts
@@ -0,0 +1,102 @@
+import { afterAll, afterEach, expect, it } from "vitest";
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { tmpdir } from "node:os";
+import { PII_SOURCE_MAX_BYTES } from "@alook/shared";
+import { PII_DEFAULTS } from "./defaults.js";
+import { compilePiiSource, PiiRunner } from "./evaluator.js";
+import { PiiStore } from "./store.js";
+
+const legacy = JSON.parse(readFileSync(new URL("./fixtures/legacy-credentials.json", import.meta.url), "utf8")) as { id: string; name: string; description: string; source: string; enabled: boolean }[];
+const definition = PII_DEFAULTS[0];
+const runner = new PiiRunner();
+const directories: string[] = [];
+const stores: PiiStore[] = [];
+afterEach(async () => {
+ await Promise.all(stores.splice(0).map(store => store.close()));
+ directories.splice(0).forEach(path => rmSync(path, { recursive: true, force: true }));
+});
+
+const cases: [string, string, string][] = [
+ ["PEM", "before -----BEGIN PRIVATE KEY-----\nprivate material\n-----END PRIVATE KEY----- after", "before [REDACTED_PRIVATE_KEY] after"],
+ ["encrypted PEM", "-----BEGIN ENCRYPTED PRIVATE KEY-----\nopaque material\n-----END ENCRYPTED PRIVATE KEY-----", "[REDACTED_PRIVATE_KEY]"],
+ ["PGP", "-----BEGIN PGP PRIVATE KEY BLOCK-----\nopaque material\n-----END PGP PRIVATE KEY BLOCK-----", "[REDACTED_PRIVATE_KEY]"],
+ ["API key", `key sk-${"a".repeat(24)}.`, "key [REDACTED_API_KEY]."],
+ ["Alook credential", `credential vch_${"a".repeat(24)}`, "credential [REDACTED_API_KEY]"],
+ ["JWT", `eyJ${"a".repeat(12)}.eyJ${"b".repeat(12)}.${"c".repeat(12)}`, "[REDACTED_TOKEN]"],
+ ["Bearer", "Authorization: Bearer synthetic-token", "Authorization: Bearer [REDACTED_TOKEN]"],
+ ["Basic", "Authorization: Basic YWJjOmRlZg==", "Authorization: Basic [REDACTED_TOKEN]"],
+ ["password", 'DB_PASSWORD="aB12345678"', 'DB_PASSWORD="[REDACTED_SECRET]"'],
+ ["secret", "client_secret=aB12345678", "client_secret=[REDACTED_SECRET]"],
+ ["connection password", "postgres://user:pass1234@host/db", "postgres://user:[REDACTED_PASSWORD]@host/db"],
+ ["API assignment overlap", `api_key=sk-${"a".repeat(24)}`, "api_key=[REDACTED_API_KEY]"],
+ ["Bearer API overlap", `Bearer sk-${"a".repeat(24)}`, "Bearer [REDACTED_API_KEY]"],
+ ["private block overlap", `-----BEGIN RSA PRIVATE KEY-----\nBearer sk-${"a".repeat(24)}\n-----END RSA PRIVATE KEY-----`, "[REDACTED_PRIVATE_KEY]"],
+];
+
+const run = (source: string, text: string) => runner.evaluate([{ id: "fixture", code: compilePiiSource(source) }], text);
+
+it("initializes five real filters with one bounded credential source at the original first position", () => {
+ expect(PII_DEFAULTS.map(plugin => plugin.id)).toEqual(["builtin.secrets", "builtin.emails", "builtin.id-cards", "builtin.phones", "builtin.bank-cards"]);
+ expect(definition.name).toBe("Keys");
+ expect(Buffer.byteLength(definition.source)).toBeLessThanOrEqual(PII_SOURCE_MAX_BYTES);
+});
+
+it.each(cases)("preserves the original %s detection and replacement marker", async (_label, input, output) => {
+ expect(await run(definition.source, input)).toBe(output);
+});
+
+it("matches the legacy four-node sequence for mixed, overlapping and unrelated text, including maximum input", async () => {
+ const previous = legacy.map(plugin => ({ id: plugin.id, code: compilePiiSource(plugin.source) }));
+ const merged = [{ id: definition.id, code: compilePiiSource(definition.source) }];
+ const negatives = ["a public key", "sk-short", "a bearer of news", "token_id=abc123456789", "process.env.ACCESS_TOKEN", "password=process.env.DATABASE_PASSWORD", "secret=os.environ", "password=1234567890", "order=abc123456789", `[REDACTED_PRIVATE_KEY] [REDACTED_API_KEY] [REDACTED_TOKEN] [REDACTED_PASSWORD] [REDACTED_SECRET]`];
+ const mixed = cases.map(([, input]) => input).join("; ");
+ const inputs = [...negatives, mixed, " ".repeat(65536 - mixed.length - 1) + " " + mixed];
+ for (const input of inputs) {
+ const output = await runner.evaluate(merged, input);
+ expect(output).toBe(await runner.evaluate(previous, input));
+ expect(await runner.evaluate(merged, output)).toBe(output);
+ }
+ for (const input of negatives) expect(await runner.evaluate(merged, input)).toBe(input);
+});
+
+it("disables or removes all four credential classes together while preserving other and custom rules", async () => {
+ const path = mkdtempSync(join(tmpdir(), "pii-merged-controls-")); directories.push(path);
+ const store = new PiiStore(path); stores.push(store);
+ const input = cases.slice(0, 11).map(([, value]) => value).join("; ");
+ const snapshot = await store.local("a", { action: "add", name: "project", description: "custom", source: 'export default (text: string) => text.replace("Project Wren", "[INTERNAL]");' });
+ const enabled = await store.filter("a", input);
+ expect(enabled).not.toBe(input);
+ const disabled = await store.manage("a", { action: "setEnabled", pluginId: definition.id, enabled: false, expectedRevision: snapshot.revision });
+ expect(disabled.status).toBe("complete");
+ expect(await store.filter("a", input)).toBe(input);
+ expect(await store.filter("a", "[email protected] Project Wren")).toBe("[REDACTED_EMAIL] [INTERNAL]");
+ const removed = await store.manage("a", { action: "remove", pluginId: definition.id, expectedRevision: snapshot.revision + 1 });
+ expect(removed.status).toBe("complete");
+ const restarted = new PiiStore(path); stores.push(restarted);
+ expect(restarted.list("a").plugins.some(plugin => plugin.id === definition.id)).toBe(false);
+ expect(await restarted.filter("a", input)).toBe(input);
+ expect(await restarted.filter("a", "[email protected] Project Wren")).toBe("[REDACTED_EMAIL] [INTERNAL]");
+});
+
+it("retains saved legacy sources, disabled and deleted rules and custom order without automatic consolidation", async () => {
+ const path = mkdtempSync(join(tmpdir(), "pii-legacy-credentials-")); directories.push(path);
+ const directory = join(path, "a", "pii"); mkdirSync(directory, { recursive: true });
+ const plugins = [
+ { ...legacy[0]!, enabled: false },
+ legacy[2]!,
+ { ...legacy[3]!, enabled: false },
+ { id: "custom", name: "project", description: "custom", source: 'export default (text: string) => text.replace("Project Wren", "[INTERNAL]");', enabled: true },
+ ...PII_DEFAULTS.slice(1).map(plugin => ({ ...plugin, enabled: true })),
+ ];
+ const file = join(directory, "pipelines.json"), bytes = JSON.stringify({ version: 1, revision: 42, plugins }); writeFileSync(file, bytes);
+ const store = new PiiStore(path); stores.push(store);
+ const snapshot = store.list("a");
+ expect(snapshot.revision).toBe(42);
+ expect(snapshot.plugins.map(({ id, enabled }) => ({ id, enabled }))).toEqual(plugins.map(({ id, enabled }) => ({ id, enabled })));
+ const input = `-----BEGIN PRIVATE KEY-----\nprivate material\n-----END PRIVATE KEY----- sk-${"a".repeat(24)} DB_PASSWORD=aB12345678 Authorization: Bearer synthetic-token Project Wren`;
+ expect(await store.filter("a", input)).toBe(input.replace("synthetic-token", "[REDACTED_TOKEN]").replace("Project Wren", "[INTERNAL]"));
+ expect(readFileSync(file, "utf8")).toBe(bytes);
+});
+
+afterAll(async () => { await runner.close(); });
diff --git a/src/daemon/src/pii/phone-number-plans.ts b/src/daemon/src/pii/phone-number-plans.ts
new file mode 100644
index 000000000..274a8faf5
--- /dev/null
+++ b/src/daemon/src/pii/phone-number-plans.ts
@@ -0,0 +1,1237 @@
+export const PHONE_NUMBER_PLANS: readonly (readonly [string, string, string])[] = [
+ [
+ "247",
+ "(?:[01589]\\d|[2-467])\\d{4}",
+ ""
+ ],
+ [
+ "376",
+ "(?:1|6\\d)\\d{7}|[135-9]\\d{5}",
+ ""
+ ],
+ [
+ "971",
+ "(?:[4-7]\\d|9[0-689])\\d{7}|800\\d{2,9}|[2-4679]\\d{7}",
+ "0"
+ ],
+ [
+ "93",
+ "[2-7]\\d{8}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:268|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "1",
+ "(?:264|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "355",
+ "(?:700\\d\\d|900)\\d{3}|8\\d{5,7}|(?:[2-5]|6\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "374",
+ "(?:[1-489]\\d|55|60|77)\\d{6}",
+ "0"
+ ],
+ [
+ "244",
+ "[29]\\d{8}",
+ ""
+ ],
+ [
+ "54",
+ "(?:11|[89]\\d\\d)\\d{8}|[2368]\\d{9}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|684|900)\\d{7}",
+ "1"
+ ],
+ [
+ "43",
+ "1\\d{3,12}|2\\d{6,12}|43(?:(?:0\\d|5[02-9])\\d{3,9}|2\\d{4,5}|[3467]\\d{4}|8\\d{4,6}|9\\d{4,7})|5\\d{4,12}|8\\d{7,12}|9\\d{8,12}|(?:[367]\\d|4[0-24-9])\\d{4,11}",
+ "0"
+ ],
+ [
+ "61",
+ "1(?:[0-79]\\d{7}(?:\\d(?:\\d{2})?)?|8[0-24-9]\\d{7})|[2-478]\\d{8}|1\\d{4,7}",
+ "0"
+ ],
+ [
+ "297",
+ "(?:[25-79]\\d\\d|800)\\d{4}",
+ ""
+ ],
+ [
+ "358",
+ "2\\d{4,9}|35\\d{4,5}|(?:60\\d\\d|800)\\d{4,6}|7\\d{5,11}|(?:[14]\\d|3[0-46-9]|50)\\d{4,8}",
+ "0"
+ ],
+ [
+ "994",
+ "365\\d{6}|(?:[124579]\\d|60|88)\\d{7}",
+ "0"
+ ],
+ [
+ "387",
+ "6\\d{8}|(?:[35689]\\d|49|70)\\d{6}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:246|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "880",
+ "[1-469]\\d{9}|8[0-79]\\d{7,8}|[2-79]\\d{8}|[2-9]\\d{7}|[3-9]\\d{6}|[57-9]\\d{5}",
+ "0"
+ ],
+ [
+ "32",
+ "4\\d{8}|[1-9]\\d{7}",
+ "0"
+ ],
+ [
+ "226",
+ "[024-7]\\d{7}",
+ ""
+ ],
+ [
+ "359",
+ "00800\\d{7}|[2-7]\\d{6,7}|[89]\\d{6,8}|2\\d{5}",
+ "0"
+ ],
+ [
+ "973",
+ "[136-9]\\d{7}",
+ ""
+ ],
+ [
+ "257",
+ "(?:[267]\\d|31)\\d{6}",
+ ""
+ ],
+ [
+ "229",
+ "(?:01\\d|8)\\d{7}",
+ ""
+ ],
+ [
+ "590",
+ "7090\\d{5}|(?:[56]9|[89]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:441|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "673",
+ "[2-578]\\d{6}",
+ ""
+ ],
+ [
+ "591",
+ "(?:[2-7]\\d\\d|8001)\\d{5}",
+ "0"
+ ],
+ [
+ "599",
+ "(?:[34]1|7\\d)\\d{5}",
+ ""
+ ],
+ [
+ "55",
+ "[1-467]\\d{9,10}|55[0-46-9]\\d{8}|[34]\\d{7}|55\\d{7,8}|(?:5[0-46-9]|[89]\\d)\\d{7,9}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:242|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "975",
+ "[178]\\d{7}|[2-8]\\d{6}",
+ ""
+ ],
+ [
+ "267",
+ "90\\d{5}|(?:0800|[2-6]|(?:7|800)\\d)\\d{6}",
+ ""
+ ],
+ [
+ "375",
+ "(?:[12]\\d|33|44|902)\\d{7}|8(?:0[0-79]\\d{5,7}|[1-7]\\d{9})|8(?:1[0-489]|[5-79]\\d)\\d{7}|8[1-79]\\d{6,7}|8[0-79]\\d{5}|8\\d{5}",
+ "8"
+ ],
+ [
+ "501",
+ "(?:0800\\d|[2-8])\\d{6}",
+ ""
+ ],
+ [
+ "1",
+ "[2-9]\\d{9}|3\\d{6}",
+ "1"
+ ],
+ [
+ "61",
+ "1(?:[0-79]\\d{8}(?:\\d{2})?|8[0-24-9]\\d{7})|[148]\\d{8}|1\\d{5,7}",
+ "0"
+ ],
+ [
+ "243",
+ "(?:(?:[189]|5\\d)\\d|2)\\d{7}|[1-68]\\d{6}",
+ "0"
+ ],
+ [
+ "236",
+ "8776\\d{4}|(?:[27]\\d|61)\\d{6}",
+ ""
+ ],
+ [
+ "242",
+ "222\\d{6}|(?:0\\d|80)\\d{7}",
+ ""
+ ],
+ [
+ "41",
+ "8\\d{11}|[2-9]\\d{8}",
+ "0"
+ ],
+ [
+ "225",
+ "[02]\\d{9}",
+ ""
+ ],
+ [
+ "682",
+ "[2-578]\\d{4}",
+ ""
+ ],
+ [
+ "56",
+ "12300\\d{6}|6\\d{9,10}|[2-9]\\d{8}",
+ ""
+ ],
+ [
+ "237",
+ "[26]\\d{8}|88\\d{6,7}",
+ ""
+ ],
+ [
+ "86",
+ "(?:(?:1[03-689]|2\\d)\\d\\d|6)\\d{8}|1\\d{10}|[126]\\d{6}(?:\\d(?:\\d{2})?)?|86\\d{5,6}|(?:[3-579]\\d|8[0-57-9])\\d{5,9}",
+ "0"
+ ],
+ [
+ "57",
+ "(?:46|60\\d\\d)\\d{6}|(?:1\\d|[39])\\d{9}",
+ "0"
+ ],
+ [
+ "506",
+ "(?:8\\d|90)\\d{8}|(?:[24-8]\\d{3}|3005)\\d{4}",
+ ""
+ ],
+ [
+ "53",
+ "(?:[2-7]|8\\d\\d)\\d{7}|[2-47]\\d{6}|[34]\\d{5}",
+ "0"
+ ],
+ [
+ "238",
+ "(?:[2-59]\\d\\d|800)\\d{4}",
+ ""
+ ],
+ [
+ "599",
+ "(?:[34]1|60|(?:7|9\\d)\\d)\\d{5}",
+ ""
+ ],
+ [
+ "357",
+ "(?:[279]\\d|[58]0)\\d{6}",
+ ""
+ ],
+ [
+ "420",
+ "(?:[2-578]\\d|60)\\d{7}|9\\d{8,11}",
+ ""
+ ],
+ [
+ "49",
+ "[2579]\\d{5,14}|49(?:[34]0|69|8\\d)\\d\\d?|49(?:37|49|60|7[089]|9\\d)\\d{1,3}|49(?:2[024-9]|3[2-689]|7[1-7])\\d{1,8}|(?:1|[368]\\d|4[0-8])\\d{3,13}|49(?:[015]\\d|2[13]|31|[46][1-8])\\d{1,9}",
+ "0"
+ ],
+ [
+ "253",
+ "(?:2\\d|77)\\d{6}",
+ ""
+ ],
+ [
+ "45",
+ "[2-9]\\d{7}",
+ ""
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|767|900)\\d{7}",
+ "1"
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "213",
+ "(?:[1-4]|[5-79]\\d|80)\\d{7}",
+ "0"
+ ],
+ [
+ "593",
+ "1\\d{9,10}|(?:[2-7]|9\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "372",
+ "8\\d{9}|[4578]\\d{7}|(?:[3-8]\\d|90)\\d{5}",
+ ""
+ ],
+ [
+ "20",
+ "[189]\\d{8,9}|[24-6]\\d{8}|[135]\\d{7}",
+ "0"
+ ],
+ [
+ "212",
+ "[5-8]\\d{8}",
+ "0"
+ ],
+ [
+ "291",
+ "[178]\\d{6}",
+ "0"
+ ],
+ [
+ "34",
+ "400\\d{6}|(?:51|[6-9]\\d)\\d{7}",
+ ""
+ ],
+ [
+ "251",
+ "(?:11|[2-57-9]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "358",
+ "[1-35689]\\d{4}|7\\d{10,11}|(?:[124-7]\\d|3[0-46-9])\\d{8}|[1-9]\\d{5,8}",
+ "0"
+ ],
+ [
+ "679",
+ "45\\d{5}|(?:0800\\d|[235-9])\\d{6}",
+ ""
+ ],
+ [
+ "500",
+ "[2-7]\\d{4}",
+ ""
+ ],
+ [
+ "691",
+ "(?:[39]\\d\\d|820)\\d{4}",
+ ""
+ ],
+ [
+ "298",
+ "[2-9]\\d{5}",
+ ""
+ ],
+ [
+ "33",
+ "[1-9]\\d{8}",
+ "0"
+ ],
+ [
+ "241",
+ "(?:[067]\\d|11)\\d{6}|[2-7]\\d{6}",
+ ""
+ ],
+ [
+ "44",
+ "[1-357-9]\\d{9}|[18]\\d{8}|8\\d{6}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:473|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "995",
+ "(?:[3-57]\\d\\d|800)\\d{6}",
+ "0"
+ ],
+ [
+ "594",
+ "(?:694\\d|7093)\\d{5}|(?:59|[89]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "44",
+ "(?:1481|[357-9]\\d{3})\\d{6}|8\\d{6}(?:\\d{2})?",
+ "0"
+ ],
+ [
+ "233",
+ "[235]\\d{8}|800\\d{5,6}",
+ "0"
+ ],
+ [
+ "350",
+ "(?:[25]\\d|60)\\d{6}",
+ ""
+ ],
+ [
+ "299",
+ "(?:19|[2-689]\\d|70)\\d{4}",
+ ""
+ ],
+ [
+ "220",
+ "[48]\\d{8}|[2-9]\\d{6}",
+ ""
+ ],
+ [
+ "224",
+ "722\\d{6}|(?:3|6\\d)\\d{7}",
+ ""
+ ],
+ [
+ "240",
+ "222\\d{6}|(?:3\\d|55|[89]0)\\d{7}",
+ ""
+ ],
+ [
+ "30",
+ "5005000\\d{3}|8\\d{9,11}|(?:[269]\\d|70)\\d{8}",
+ ""
+ ],
+ [
+ "502",
+ "80\\d{6}|(?:1\\d{3}|[2-7])\\d{7}",
+ ""
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|671|900)\\d{7}",
+ "1"
+ ],
+ [
+ "245",
+ "[49]\\d{8}|4\\d{6}",
+ ""
+ ],
+ [
+ "592",
+ "(?:[2-8]\\d{3}|9008)\\d{3}",
+ ""
+ ],
+ [
+ "852",
+ "8[0-46-9]\\d{6,7}|9\\d{4,7}|(?:[2-7]|9\\d{3})\\d{7}",
+ ""
+ ],
+ [
+ "504",
+ "8\\d{10}|[237-9]\\d{7}",
+ ""
+ ],
+ [
+ "385",
+ "[2-69]\\d{8}|80\\d{5,7}|[1-79]\\d{7}|6\\d{6}",
+ "0"
+ ],
+ [
+ "509",
+ "[2-589]\\d{7}",
+ ""
+ ],
+ [
+ "36",
+ "[235-7]\\d{8}|[1-9]\\d{7}",
+ "06"
+ ],
+ [
+ "62",
+ "00[1-9]\\d{9,14}|(?:[1-36]|8\\d{5})\\d{6}|00\\d{9}|[1-9]\\d{8,10}|[2-9]\\d{7}",
+ "0"
+ ],
+ [
+ "353",
+ "(?:1\\d|[2569])\\d{6,8}|4\\d{6,9}|7\\d{8}|8\\d{8,9}",
+ "0"
+ ],
+ [
+ "972",
+ "1\\d{6}(?:\\d{3,5})?|[57]\\d{8}|[1-489]\\d{7}",
+ "0"
+ ],
+ [
+ "44",
+ "1624\\d{6}|(?:[3578]\\d|90)\\d{8}",
+ "0"
+ ],
+ [
+ "91",
+ "(?:000800|[2-9]\\d\\d)\\d{7}|1\\d{7,12}",
+ "0"
+ ],
+ [
+ "246",
+ "3\\d{6}",
+ ""
+ ],
+ [
+ "964",
+ "(?:1|7\\d\\d)\\d{7}|[2-6]\\d{7,8}",
+ "0"
+ ],
+ [
+ "98",
+ "[1-9]\\d{9}|(?:[1-8]\\d\\d|9)\\d{3,4}",
+ "0"
+ ],
+ [
+ "354",
+ "(?:38\\d|[4-9])\\d{6}",
+ ""
+ ],
+ [
+ "39",
+ "0\\d{5,11}|1\\d{8,10}|3(?:[0-8]\\d{7,10}|9\\d{7,8})|(?:43|55|70)\\d{8}|8\\d{5}(?:\\d{2,4})?",
+ ""
+ ],
+ [
+ "44",
+ "1534\\d{6}|(?:[3578]\\d|90)\\d{8}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|658|900)\\d{7}",
+ "1"
+ ],
+ [
+ "962",
+ "(?:(?:[2689]|7\\d)\\d|32|427|53)\\d{6}",
+ "0"
+ ],
+ [
+ "81",
+ "00[1-9]\\d{6,14}|[25-9]\\d{9}|(?:00|[1-9]\\d\\d)\\d{6}",
+ "0"
+ ],
+ [
+ "254",
+ "(?:[17]\\d\\d|900)\\d{6}|(?:2|80)0\\d{6,7}|[4-6]\\d{6,8}",
+ "0"
+ ],
+ [
+ "996",
+ "8\\d{9}|[235-9]\\d{8}",
+ "0"
+ ],
+ [
+ "855",
+ "1\\d{9}|[1-9]\\d{7,8}",
+ "0"
+ ],
+ [
+ "686",
+ "(?:[37]\\d|6[0-79])\\d{6}|(?:[2-48]\\d|50)\\d{3}",
+ "0"
+ ],
+ [
+ "269",
+ "[3478]\\d{6}",
+ ""
+ ],
+ [
+ "850",
+ "85\\d{6}|(?:19\\d|[2-7])\\d{7}",
+ "0"
+ ],
+ [
+ "82",
+ "00[1-9]\\d{8,11}|(?:[12]|5\\d{3})\\d{7}|[13-6]\\d{9}|(?:[1-6]\\d|80)\\d{7}|[3-6]\\d{4,5}|(?:00|7)0\\d{8}",
+ "0"
+ ],
+ [
+ "965",
+ "18\\d{5}|(?:[2569]\\d|41)\\d{6}",
+ ""
+ ],
+ [
+ "1",
+ "(?:345|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "7",
+ "8\\d{13}|[78]\\d{9}",
+ "8"
+ ],
+ [
+ "856",
+ "[23]\\d{9}|3\\d{8}|(?:[235-8]\\d|41)\\d{6}",
+ "0"
+ ],
+ [
+ "961",
+ "[27-9]\\d{7}|[13-9]\\d{6}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|758|900)\\d{7}",
+ "1"
+ ],
+ [
+ "423",
+ "[68]\\d{8}|(?:[2378]\\d|90)\\d{5}",
+ "0"
+ ],
+ [
+ "94",
+ "[1-9]\\d{8}",
+ "0"
+ ],
+ [
+ "231",
+ "(?:[2457]\\d|33|88)\\d{7}|(?:2\\d|[4-6])\\d{6}",
+ "0"
+ ],
+ [
+ "266",
+ "(?:[256]\\d\\d|800)\\d{5}",
+ ""
+ ],
+ [
+ "370",
+ "(?:[3469]\\d|52|[78]0)\\d{6}",
+ "0"
+ ],
+ [
+ "352",
+ "35[013-9]\\d{4,8}|6\\d{8}|35\\d{2,4}|(?:[2457-9]\\d|3[0-46-9])\\d{2,9}",
+ ""
+ ],
+ [
+ "371",
+ "(?:[268]\\d|78|90)\\d{6}",
+ ""
+ ],
+ [
+ "218",
+ "[2-9]\\d{8}",
+ "0"
+ ],
+ [
+ "377",
+ "(?:[3489]|[67]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "373",
+ "(?:[235-7]\\d|[89]0)\\d{6}",
+ "0"
+ ],
+ [
+ "382",
+ "(?:20|[3-79]\\d)\\d{6}|80\\d{6,7}",
+ "0"
+ ],
+ [
+ "261",
+ "[23]\\d{8}",
+ "0"
+ ],
+ [
+ "692",
+ "329\\d{4}|(?:[256]\\d|45)\\d{5}",
+ "1"
+ ],
+ [
+ "389",
+ "[2-578]\\d{7}",
+ "0"
+ ],
+ [
+ "223",
+ "[24-9]\\d{7}",
+ ""
+ ],
+ [
+ "95",
+ "1\\d{5,7}|95\\d{6}|(?:[4-7]|9[0-46-9])\\d{6,8}|(?:2|8\\d)\\d{5,8}",
+ "0"
+ ],
+ [
+ "976",
+ "[12]\\d{7,9}|[5-9]\\d{7}",
+ "0"
+ ],
+ [
+ "853",
+ "0800\\d{3}|(?:28|[68]\\d)\\d{6}",
+ ""
+ ],
+ [
+ "1",
+ "[58]\\d{9}|(?:67|90)0\\d{7}",
+ "1"
+ ],
+ [
+ "596",
+ "7091\\d{5}|(?:[56]9|[89]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "222",
+ "(?:[2-4]\\d\\d|800)\\d{5}",
+ ""
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|664|900)\\d{7}",
+ "1"
+ ],
+ [
+ "356",
+ "3550\\d{4}|(?:[2579]\\d\\d|800)\\d{5}",
+ ""
+ ],
+ [
+ "230",
+ "(?:[57]|8\\d\\d)\\d{7}|[2-468]\\d{6}",
+ ""
+ ],
+ [
+ "960",
+ "(?:800|9[0-57-9]\\d)\\d{7}|[34679]\\d{6}",
+ ""
+ ],
+ [
+ "265",
+ "(?:[1289]\\d|31|77)\\d{7}|1\\d{6}",
+ "0"
+ ],
+ [
+ "52",
+ "[2-9]\\d{9}",
+ ""
+ ],
+ [
+ "60",
+ "1\\d{8,9}|(?:3\\d|[4-9])\\d{7}",
+ "0"
+ ],
+ [
+ "258",
+ "(?:2|8\\d)\\d{7}",
+ ""
+ ],
+ [
+ "264",
+ "[68]\\d{7,8}",
+ "0"
+ ],
+ [
+ "687",
+ "(?:050|[2-57-9]\\d\\d)\\d{3}",
+ ""
+ ],
+ [
+ "227",
+ "[027-9]\\d{7}",
+ ""
+ ],
+ [
+ "672",
+ "[13]\\d{5}",
+ ""
+ ],
+ [
+ "234",
+ "(?:20|9\\d)\\d{8}|[78]\\d{9,13}",
+ "0"
+ ],
+ [
+ "505",
+ "(?:1800|[25-8]\\d{3})\\d{4}",
+ ""
+ ],
+ [
+ "31",
+ "(?:[124-7]\\d\\d|3(?:[02-9]\\d|1[0-8]))\\d{6}|8\\d{6,9}|9\\d{6,10}|1\\d{4,5}",
+ "0"
+ ],
+ [
+ "47",
+ "(?:0|[2-9]\\d{3})\\d{4}",
+ ""
+ ],
+ [
+ "977",
+ "(?:1\\d|9)\\d{9}|[1-9]\\d{7}",
+ "0"
+ ],
+ [
+ "674",
+ "(?:222|444|(?:55|8\\d)\\d|666|777|999)\\d{4}",
+ ""
+ ],
+ [
+ "683",
+ "(?:[4-7]|888\\d)\\d{3}",
+ ""
+ ],
+ [
+ "64",
+ "[1289]\\d{9}|50\\d{5}(?:\\d{2,3})?|[27-9]\\d{7,8}|(?:[34]\\d|6[0-35-9])\\d{6}|8\\d{4,6}",
+ "0"
+ ],
+ [
+ "968",
+ "(?:1505|[279]\\d{3}|500)\\d{4}|800\\d{5,6}",
+ ""
+ ],
+ [
+ "507",
+ "(?:00800|8\\d{3})\\d{6}|[68]\\d{7}|[1-57-9]\\d{6}",
+ ""
+ ],
+ [
+ "51",
+ "(?:[14-8]|9\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "689",
+ "4\\d{5}(?:\\d{2})?|8\\d{7,8}",
+ ""
+ ],
+ [
+ "675",
+ "(?:180|[78]\\d{3})\\d{4}|(?:[2-589]\\d|64)\\d{5}",
+ ""
+ ],
+ [
+ "63",
+ "(?:[2-7]|9\\d)\\d{8}|2\\d{5}|(?:1800|8)\\d{7,9}",
+ "0"
+ ],
+ [
+ "92",
+ "122\\d{6}|[24-8]\\d{10,11}|9(?:[013-9]\\d{8,10}|2(?:[01]\\d\\d|2(?:[06-8]\\d|1[01]))\\d{7})|(?:[2-8]\\d{3}|92(?:[0-7]\\d|8[1-9]))\\d{6}|[24-9]\\d{8}|[89]\\d{7}",
+ "0"
+ ],
+ [
+ "48",
+ "(?:6|8\\d\\d)\\d{7}|[1-9]\\d{6}(?:\\d{2})?|[26]\\d{5}",
+ ""
+ ],
+ [
+ "508",
+ "[78]\\d{8}|[2-9]\\d{5}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:[589]\\d\\d|787)\\d{7}",
+ "1"
+ ],
+ [
+ "970",
+ "[2489]2\\d{6}|(?:1\\d|5)\\d{8}",
+ "0"
+ ],
+ [
+ "351",
+ "1693\\d{5}|(?:[26-9]\\d|30)\\d{7}",
+ ""
+ ],
+ [
+ "680",
+ "(?:[24-8]\\d\\d|345|900)\\d{4}",
+ ""
+ ],
+ [
+ "595",
+ "[36-8]\\d{5,8}|4\\d{6,8}|59\\d{6}|9\\d{5,10}|(?:2\\d|5[0-8])\\d{6,7}",
+ "0"
+ ],
+ [
+ "974",
+ "800\\d{4}|(?:2|800)\\d{6}|(?:0080|[3-7])\\d{7}",
+ ""
+ ],
+ [
+ "262",
+ "709\\d{6}|(?:26|[689]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "40",
+ "(?:[236-8]\\d|90)\\d{7}|[23]\\d{5}",
+ "0"
+ ],
+ [
+ "381",
+ "38[02-9]\\d{6,9}|6\\d{7,9}|90\\d{4,8}|38\\d{5,6}|(?:7\\d\\d|800)\\d{3,9}|(?:[12]\\d|3[0-79])\\d{5,10}",
+ "0"
+ ],
+ [
+ "7",
+ "8\\d{13}|[347-9]\\d{9}",
+ "8"
+ ],
+ [
+ "250",
+ "(?:06|[27]\\d\\d|[89]00)\\d{6}",
+ "0"
+ ],
+ [
+ "966",
+ "(?:[15]\\d|800|92)\\d{7}",
+ "0"
+ ],
+ [
+ "677",
+ "[6-9]\\d{6}|[1-6]\\d{4}",
+ ""
+ ],
+ [
+ "248",
+ "(?:[2489]\\d|64)\\d{5}",
+ ""
+ ],
+ [
+ "249",
+ "[19]\\d{8}",
+ "0"
+ ],
+ [
+ "46",
+ "(?:[26]\\d\\d|9)\\d{9}|[1-9]\\d{8}|[1-689]\\d{7}|[1-4689]\\d{6}|2\\d{5}",
+ "0"
+ ],
+ [
+ "65",
+ "(?:(?:1\\d|8)\\d\\d|7000)\\d{7}|[3689]\\d{7}",
+ ""
+ ],
+ [
+ "290",
+ "(?:[2569]\\d|8)\\d{3}",
+ ""
+ ],
+ [
+ "386",
+ "[1-7]\\d{7}|8\\d{4,7}|90\\d{4,6}",
+ "0"
+ ],
+ [
+ "47",
+ "0\\d{4}|(?:[489]\\d|79)\\d{6}",
+ ""
+ ],
+ [
+ "421",
+ "[2-689]\\d{8}|[2-59]\\d{6}|[2-5]\\d{5}",
+ "0"
+ ],
+ [
+ "232",
+ "(?:[237-9]\\d|66)\\d{6}",
+ "0"
+ ],
+ [
+ "378",
+ "(?:0549|[5-7]\\d)\\d{6}",
+ ""
+ ],
+ [
+ "221",
+ "(?:[378]\\d|93)\\d{7}",
+ ""
+ ],
+ [
+ "252",
+ "[346-9]\\d{8}|[12679]\\d{7}|[1-5]\\d{6}|[1348]\\d{5}",
+ "0"
+ ],
+ [
+ "597",
+ "(?:[2-5]|[6-9]\\d)\\d{5}",
+ ""
+ ],
+ [
+ "211",
+ "[19]\\d{8}",
+ "0"
+ ],
+ [
+ "239",
+ "(?:22|9\\d)\\d{5}",
+ ""
+ ],
+ [
+ "503",
+ "[25-7]\\d{7}|(?:80\\d|900)\\d{4}(?:\\d{4})?",
+ ""
+ ],
+ [
+ "1",
+ "7215\\d{6}|(?:[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "963",
+ "[1-359]\\d{8}|[1-5]\\d{7}",
+ "0"
+ ],
+ [
+ "268",
+ "0800\\d{4}|(?:[237]\\d|900)\\d{6}",
+ ""
+ ],
+ [
+ "290",
+ "8\\d{3}",
+ ""
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|649|900)\\d{7}",
+ "1"
+ ],
+ [
+ "235",
+ "(?:22|[3689]\\d|77)\\d{6}",
+ ""
+ ],
+ [
+ "228",
+ "[279]\\d{7}",
+ ""
+ ],
+ [
+ "66",
+ "(?:001800|[2-57]|[689]\\d)\\d{7}|1\\d{7,9}",
+ "0"
+ ],
+ [
+ "992",
+ "(?:[0-57-9]\\d|66)\\d{7}",
+ ""
+ ],
+ [
+ "690",
+ "[2-47]\\d{3,6}",
+ ""
+ ],
+ [
+ "670",
+ "7\\d{7}|(?:[2-47]\\d|[89]0)\\d{5}",
+ ""
+ ],
+ [
+ "993",
+ "[1-7]\\d{7}",
+ "8"
+ ],
+ [
+ "216",
+ "[2-57-9]\\d{7}",
+ ""
+ ],
+ [
+ "676",
+ "(?:0800|(?:[5-8]\\d\\d|999)\\d)\\d{3}|[2-8]\\d{4}",
+ ""
+ ],
+ [
+ "90",
+ "4\\d{6}|8\\d{11,12}|(?:[2-58]\\d\\d|900)\\d{7}",
+ "0"
+ ],
+ [
+ "688",
+ "(?:2|7\\d\\d|90)\\d{4}",
+ ""
+ ],
+ [
+ "886",
+ "[2-689]\\d{8}|7\\d{9,10}|[2-8]\\d{7}|2\\d{6}",
+ "0"
+ ],
+ [
+ "255",
+ "(?:[25-8]\\d|41|90)\\d{7}",
+ "0"
+ ],
+ [
+ "380",
+ "[89]\\d{9}|[3-9]\\d{8}",
+ "0"
+ ],
+ [
+ "256",
+ "800\\d{6}|(?:[29]0|[347]\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "598",
+ "0004\\d{2,9}|[1249]\\d{7}|2\\d{3,4}|(?:[49]\\d|80)\\d{5}",
+ "0"
+ ],
+ [
+ "998",
+ "(?:20|33|[5-9]\\d)\\d{7}",
+ ""
+ ],
+ [
+ "39",
+ "0\\d{5,10}|3[0-8]\\d{7,10}|55\\d{8}|8\\d{5}(?:\\d{2,4})?|(?:1\\d|39)\\d{7,8}",
+ ""
+ ],
+ [
+ "1",
+ "(?:[58]\\d\\d|784|900)\\d{7}",
+ "1"
+ ],
+ [
+ "58",
+ "[68]00\\d{7}|(?:[24]\\d|[59]0)\\d{8}",
+ "0"
+ ],
+ [
+ "1",
+ "(?:284|[58]\\d\\d|900)\\d{7}",
+ "1"
+ ],
+ [
+ "1",
+ "[58]\\d{9}|(?:34|90)0\\d{7}",
+ "1"
+ ],
+ [
+ "84",
+ "[12]\\d{9}|[135-9]\\d{8}|[16]\\d{6,7}|7\\d{6}",
+ "0"
+ ],
+ [
+ "678",
+ "[57-9]\\d{6}|(?:[238]\\d|48)\\d{3}",
+ ""
+ ],
+ [
+ "681",
+ "(?:40|72|8\\d{4})\\d{4}|[89]\\d{5}",
+ ""
+ ],
+ [
+ "685",
+ "(?:[2-6]|8\\d{5})\\d{4}|[78]\\d{6}|[68]\\d{5}",
+ ""
+ ],
+ [
+ "383",
+ "2\\d{7,8}|3\\d{7,11}|(?:4\\d\\d|[89]00)\\d{5}",
+ "0"
+ ],
+ [
+ "967",
+ "(?:1|7\\d)\\d{7}|[1-7]\\d{6}",
+ "0"
+ ],
+ [
+ "262",
+ "(?:639\\d|7093)\\d{5}|(?:26|80|9\\d)\\d{7}",
+ "0"
+ ],
+ [
+ "27",
+ "[1-79]\\d{8}|8\\d{4,9}",
+ "0"
+ ],
+ [
+ "260",
+ "800\\d{6}|(?:21|[579]\\d|63)\\d{7}",
+ "0"
+ ],
+ [
+ "263",
+ "(?:13|8\\d{4})\\d{5}|[235-8]\\d{8}|[23568]\\d{6}",
+ "0"
+ ],
+ [
+ "800",
+ "(?:00|[1-9]\\d)\\d{6}",
+ ""
+ ],
+ [
+ "808",
+ "[1-9]\\d{7}",
+ ""
+ ],
+ [
+ "870",
+ "7\\d{11}|[235-7]\\d{8}",
+ ""
+ ],
+ [
+ "878",
+ "10\\d{10}",
+ ""
+ ],
+ [
+ "881",
+ "6\\d{9}|[0-36-9]\\d{8}",
+ ""
+ ],
+ [
+ "882",
+ "[13]\\d{6}(?:\\d{2,5})?|[19]\\d{7}|(?:[25]\\d\\d|4)\\d{7}(?:\\d{2})?",
+ ""
+ ],
+ [
+ "883",
+ "(?:[1-4]\\d|51)\\d{6,10}",
+ ""
+ ],
+ [
+ "888",
+ "\\d{11}",
+ ""
+ ],
+ [
+ "979",
+ "[1359]\\d{8}",
+ ""
+ ]
+];
diff --git a/src/daemon/src/pii/phone-numbers.test.ts b/src/daemon/src/pii/phone-numbers.test.ts
new file mode 100644
index 000000000..e8e567619
--- /dev/null
+++ b/src/daemon/src/pii/phone-numbers.test.ts
@@ -0,0 +1,70 @@
+import { afterAll, expect, it } from "vitest";
+import { readFileSync } from "node:fs";
+import { PII_DEFAULTS } from "./defaults.js";
+import { PHONE_NUMBERS_SOURCE } from "./phone-numbers.js";
+import { compilePiiSource, PiiRunner } from "./evaluator.js";
+
+const runner = new PiiRunner();
+afterAll(async () => { await runner.close(); });
+const phone = { id: "builtin.phones", code: compilePiiSource(PHONE_NUMBERS_SOURCE) };
+const apply = (text: string) => runner.evaluateWithResult([phone], text);
+const examples = JSON.parse(readFileSync(new URL("./fixtures/phone-examples.json", import.meta.url), "utf8")) as {region: string; callingCode: string; national: string; prefix: string}[];
+
+it("fits the existing plugin source limit", () => {
+ expect(Buffer.byteLength(PHONE_NUMBERS_SOURCE)).toBeLessThanOrEqual(16 * 1024);
+});
+
+it("recognizes the pinned upstream examples for every supported numbering plan", async () => {
+ for (const example of examples) {
+ const input = "+" + example.callingCode + example.national;
+ expect(await apply(input), example.region + "/" + example.callingCode).toEqual({text: "[REDACTED_PHONE]", triggers: [{pluginId: phone.id, triggered: true}]});
+ }
+}, 15000);
+
+it.each([
+ ["+1 (202) 555-0123", "[REDACTED_PHONE]"],
+ ["+44 20 7946 0018", "[REDACTED_PHONE]"],
+ ["+49 (0)30 123456", "[REDACTED_PHONE]"],
+ ["+33 1 42 68 53 00", "[REDACTED_PHONE]"],
+ ["+91 81234 56789", "[REDACTED_PHONE]"],
+ ["+81 3-1234-5678", "[REDACTED_PHONE]"],
+ ["+82 2-2123-4567", "[REDACTED_PHONE]"],
+ ["+61 2 1234 5678", "[REDACTED_PHONE]"],
+ ["+55 11 91234-5678", "[REDACTED_PHONE]"],
+ ["+852 2123 4567", "[REDACTED_PHONE]"],
+ ["Phone: 020 7946 0018", "Phone: [REDACTED_PHONE]"],
+ ['"phone_number": "03-1234-5678"', '"phone_number": "[REDACTED_PHONE]"'],
+ ["Téléphone: 01 42 68 53 00", "Téléphone: [REDACTED_PHONE]"],
+ ["電話: 03-1234-5678", "電話: [REDACTED_PHONE]"],
+ ["전화: 02-2123-4567", "전화: [REDACTED_PHONE]"],
+ ["tel=0044 20 7946 0018", "tel=[REDACTED_PHONE]"],
+ ["mobile: (020) 7946 0018", "mobile: [REDACTED_PHONE]"],
+ ["(202) 555-0123", "[REDACTED_PHONE]"],
+ ["202.555.0123", "[REDACTED_PHONE]"],
+ ["13800138000", "[REDACTED_PHONE]"],
+ ["8613800138000", "[REDACTED_PHONE]"],
+])("filters %s with an explicit hit", async (input, output) => {
+ expect((await apply(input)).text).toBe(output);
+ expect((await apply(input)).triggers).toEqual([{pluginId:phone.id,triggered:true}]);
+});
+
+it.each([
+ "order: 2025550123", "2024-10-26", "version 1.2.3", "SSN: 219-09-9999", "invoice: 02079460018",
+ "https://example.test/+12025550123", "file/+442079460018.txt", "prefix+12025550123suffix", "foo_phone: 2025550123",
+ "+999 123456789", "+1 123 456 7890", "+1 2025550123456789", "+44 2079 4600 1812 3456 7890", "Phone: 123", "Phone: 12345678901234567890",
+ "550e8400-e29b-41d4-a716-138001380000", "a13800138000b123456789012345678901234567890", "plain 219099999 and 2025550123",
+])("preserves nonphone or ambiguous input %s", async input => {
+ expect(await apply(input)).toEqual({text:input,triggers:[{pluginId:phone.id,triggered:false}]});
+});
+
+it("keeps ordinary references through the full default pipeline and reports only Phone numbers", async () => {
+ const plugins=PII_DEFAULTS.map(plugin=>({id:plugin.id,code:compilePiiSource(plugin.source)}));
+ const result=await runner.evaluateWithResult(plugins,"Call +44 20 7946 0018; order 2025550123; date 2024-10-26");
+ expect(result.text).toBe("Call [REDACTED_PHONE]; order 2025550123; date 2024-10-26");
+ expect(result.triggers.filter(trigger=>trigger.triggered)).toEqual([{pluginId:phone.id,triggered:true}]);
+ expect((await runner.evaluateWithResult(plugins,result.text)).text).toBe(result.text);
+});
+
+it("handles maximum ordinary input inside the unchanged sandbox deadline", async () => {
+ expect((await apply(" ".repeat(65536))).text).toHaveLength(65536);
+});
diff --git a/src/daemon/src/pii/phone-numbers.ts b/src/daemon/src/pii/phone-numbers.ts
new file mode 100644
index 000000000..ff5bba1fc
--- /dev/null
+++ b/src/daemon/src/pii/phone-numbers.ts
@@ -0,0 +1,32 @@
+import { PHONE_NUMBER_PLANS } from "./phone-number-plans.js";
+
+export const PHONE_NUMBERS_SOURCE = "const plans=" + JSON.stringify(PHONE_NUMBER_PLANS) + ";\n" + String.raw`
+export default (text: string) => {
+ let triggered = false;
+ const patterns = new Map();
+ const valid = (digits, plan) => {
+ if (!patterns.has(plan)) patterns.set(plan, new RegExp("^(?:" + plan[1] + ")$"));
+ return patterns.get(plan).test(digits);
+ };
+ const international = value => {
+ const digits = value.replace(/\(0\)/g, "").replace(/\D/g, "");
+ return digits.length <= 15 && plans.some(plan => digits.startsWith(plan[0]) && valid(digits.slice(plan[0].length), plan));
+ };
+ const national = value => {
+ if (/^[++]/.test(value)) return international(value);
+ const digits = value.replace(/\D/g, "");
+ if (digits.startsWith("00")) return international(digits.slice(2));
+ if (digits.length < 6 || digits.length > 15) return false;
+ return plans.some(plan => valid(digits, plan) || (plan[2] && valid(digits.replace(new RegExp("^(?:" + plan[2] + ")"), ""), plan)));
+ };
+ const redact = () => { triggered = true; return "[REDACTED_PHONE]"; };
+ let output = text.replace(/(?<![\p{L}\p{N}_@/.-])[++]\d[\d ().-]{3,48}\d(?![\p{L}\p{N}_@/-]|[ ().-]*\d)/gu, value => international(value) ? redact() : value);
+ output = output.replace(/(?<![\p{L}\p{N}_])((?:phone(?:[ _-]?(?:number|no))?|telephone|tel|mobile(?:[ _-]?(?:number|no))?|cell(?:[ _-]?(?:phone|number|no))?|telefon|téléphone|teléfono|telefone|celular|телефон|手机号?|电话|電話|携帯|휴대폰|전화)["']?[ \t]*(?:[:=:][ \t]*["']?[ \t]*|[ \t]+))((?:[++]?\d|\(\d{1,5}\))[\d ().-]{3,48}\d)(?![\p{L}\p{N}_@/-]|[ ().-]*\d)/giu, (all, prefix, value) => national(value) ? prefix + redact() : all);
+ output = output.replace(/(?<![A-Za-z0-9_./-])(?:1[ .-]?)?(?:\([2-9]\d{2}\)[ .-]?|[2-9]\d{2}[ .-])[2-9]\d{2}[ .-]\d{4}(?![A-Za-z0-9_/-])/g, redact);
+ output = output.replace(/(?<![A-Za-z0-9])(?:\+?86[- ]?)?1[3-9]\d{9}(?![A-Za-z0-9])/g, (value, offset) => {
+ const before=output.slice(0,offset).match(/[a-fA-F0-9-]+$/)?.[0]||"", after=output.slice(offset+value.length).match(/^[a-fA-F0-9-]+/)?.[0]||"";
+ const whole=before+value+after;
+ return /^[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}$/.test(whole)||/^[a-fA-F0-9]{32,}$/.test(whole)?value:redact();
+ });
+ return { text: output, triggered };
+};`;
diff --git a/src/daemon/src/pii/protocol.test.ts b/src/daemon/src/pii/protocol.test.ts
new file mode 100644
index 000000000..9fa44ad66
--- /dev/null
+++ b/src/daemon/src/pii/protocol.test.ts
@@ -0,0 +1,79 @@
+import { afterAll, expect, it } from "vitest";
+import { compilePiiSource, PiiRunner } from "./evaluator.js";
+import { PII_DEFAULTS } from "./defaults.js";
+
+const runner = new PiiRunner();
+afterAll(async () => { await runner.close(); });
+const node = (id: string, source: string) => ({ id, code: compilePiiSource(source) });
+
+it("uses each explicit bool even if a later plugin restores the original text", async () => {
+ const result = await runner.evaluateWithResult([
+ node("first", 'export default () => ({ text: "changed", triggered: true });'),
+ node("restore", 'export default () => ({ text: "original", triggered: false });'),
+ node("same", 'export default (text: string) => ({ text, triggered: true });'),
+ ], "original");
+ expect(result).toEqual({ text: "original", triggers: [
+ { pluginId: "first", triggered: true }, { pluginId: "restore", triggered: false }, { pluginId: "same", triggered: true },
+ ] });
+});
+
+it("executes legacy string plugins with per-node compatibility estimates", async () => {
+ expect(await runner.evaluateWithResult([
+ node("legacy-first", 'export default () => "changed";'),
+ node("legacy-restore", 'export default () => "original";'),
+ node("legacy-same", 'export default (text: string) => text;'),
+ ], "original")).toEqual({ text: "original", triggers: [
+ { pluginId: "legacy-first", triggered: true, legacy: true },
+ { pluginId: "legacy-restore", triggered: true, legacy: true },
+ { pluginId: "legacy-same", triggered: false, legacy: true },
+ ] });
+});
+
+it.each(['null', '[]', '{}', '({text: "x"})', '({triggered: true})', '({text: 1, triggered: true})', '({text: "x", triggered: "true"})', '({text: "x", triggered: 1})', 'Promise.resolve({text: "x", triggered: true})'])
+ ("rejects malformed structured result %s without exposing raw text", async value => {
+ const result = runner.evaluateWithResult([node("bad", `export default () => ${value};`)], "PRIVATE_INPUT");
+ await expect(result).rejects.toMatchObject({ code: "PII_INVALID_RESULT", pluginId: "bad" });
+ await expect(result).rejects.not.toThrow("PRIVATE_INPUT");
+ });
+
+it("does not expose partial trigger results or text when a later plugin throws", async () => {
+ await expect(runner.evaluateWithResult([
+ node("hit", 'export default () => ({text: "PRIVATE_INTERMEDIATE", triggered: true});'),
+ node("fail", 'export default (text: string) => { throw new Error(text); };'),
+ ], "PRIVATE_INPUT")).rejects.toMatchObject({ code: "PII_PLUGIN_FAILED", pluginId: "fail", message: "PII_PLUGIN_FAILED: fail" });
+});
+
+it("validates getters and bounded text inside the isolated runtime", async () => {
+ await expect(runner.evaluateWithResult([node("getter", 'export default () => ({ get text(){ throw new Error("PRIVATE_INPUT"); }, triggered:true });')], ""))
+ .rejects.toMatchObject({ code: "PII_PLUGIN_FAILED", pluginId: "getter" });
+ await expect(runner.evaluateWithResult([node("expand", 'export default () => ({ text: "x".repeat(65537), triggered:true });')], ""))
+ .rejects.toMatchObject({ code: "PII_OUTPUT_TOO_LARGE", pluginId: "expand" });
+});
+
+it.each(PII_DEFAULTS)("builtin $id explicitly reports no trigger for ordinary text", async plugin => {
+ expect(await runner.evaluateWithResult([node(plugin.id, plugin.source)], "ordinary reference")).toEqual({ text: "ordinary reference", triggers: [{ pluginId: plugin.id, triggered: false }] });
+});
+
+it.each(["pk_live_", "pk_test_", "sk_live_", "sk_test_", "rk_live_", "rk_test_", "sb_publishable_", "sb_secret_", "whsec_"])
+ ("redacts full synthetic %s values and reports the merged filter", async prefix => {
+ const key = prefix + "Ab9x".repeat(25), plugin = PII_DEFAULTS[0];
+ const nodes = [node(plugin.id, plugin.source)];
+ expect(await runner.evaluateWithResult(nodes, `key: "${key}".`)).toEqual({ text: 'key: "[REDACTED_API_KEY]".', triggers: [{ pluginId: plugin.id, triggered: true }] });
+ for (const text of [prefix + "short", "reference_" + key]) {
+ expect(await runner.evaluateWithResult(nodes, text)).toEqual({ text, triggers: [{ pluginId: plugin.id, triggered: false }] });
+ }
+ });
+
+it("retains recognized provider families alongside the expanded publishable/secret formats", async () => {
+ const keys = [
+ "sk-proj-" + "a".repeat(32), "sk-ant-" + "a".repeat(32), "sk-or-" + "a".repeat(32),
+ "ghp_" + "a".repeat(36), "github_pat_" + "a".repeat(48), "glpat-" + "a".repeat(24),
+ "AIza" + "a".repeat(35), "xoxb-" + "a".repeat(24), "AKIA" + "A1".repeat(8),
+ "hf_" + "a".repeat(32), "gsk_" + "a".repeat(48), "xai-" + "a".repeat(48),
+ "npm_" + "a".repeat(36), "pypi-" + "a".repeat(54), "dop_v1_" + "a".repeat(64),
+ "SG." + "a".repeat(22) + "." + "b".repeat(43), "cmk_" + "a".repeat(24),
+ "pk_test_" + "ZXhhbXBsZS5hY2NvdW50cy5kZXYk", "pk_live_" + "aB1+/".repeat(8) + "==",
+ ];
+ const plugin = PII_DEFAULTS[0];
+ for (const key of keys) expect(await runner.evaluateWithResult([node(plugin.id, plugin.source)], key)).toEqual({ text: "[REDACTED_API_KEY]", triggers: [{ pluginId: plugin.id, triggered: true }] });
+});
diff --git a/src/daemon/src/pii/proxy.test.ts b/src/daemon/src/pii/proxy.test.ts
new file mode 100644
index 000000000..afa17243c
--- /dev/null
+++ b/src/daemon/src/pii/proxy.test.ts
@@ -0,0 +1,196 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { createServer } from "node:http";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { CredentialBroker, startCredentialProxy } from "../credentials/credentialProxy.js";
+import { PiiStore } from "./store.js";
+import { LOCAL_PII_PATH } from "./proxy.js";
+import { createProxyServerApi } from "../cli/proxyServerApi.js";
+
+const cleanup: (() => void | Promise<void>)[] = [];
+afterEach(async () => { vi.restoreAllMocks(); for (const close of cleanup.splice(0).reverse()) await close(); });
+
+async function fixture(withPii = true, reply?: { status?: number; body?: string; abort?: boolean }) {
+ const directory = mkdtempSync(join(tmpdir(), "pii-proxy-"));
+ cleanup.push(() => rmSync(directory, { force: true, recursive: true }));
+ const seen: { body: Record<string, unknown>; headers: Record<string, unknown>; path: string }[] = [];
+ const upstream = createServer(async (req, res) => {
+ const chunks: Buffer[] = [];
+ for await (const chunk of req) chunks.push(Buffer.from(chunk));
+ seen.push({ body: JSON.parse(Buffer.concat(chunks).toString()) as Record<string, unknown>, headers: req.headers, path: req.url! });
+ res.setHeader("content-type", "application/json");
+ res.statusCode = reply?.status ?? 200;
+ if (reply?.abort) { res.write('{"state":"sent","message":'); setImmediate(() => res.destroy()); return; }
+ if (reply?.body !== undefined) { res.end(reply.body); return; }
+ res.end(JSON.stringify({ state: "sent", message: { seq: "#1", channel: "/test#1234/general" } }));
+ });
+ await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve));
+ cleanup.push(() => new Promise<void>((resolve) => upstream.close(() => resolve())));
+ const address = upstream.address();
+ if (!address || typeof address === "string") throw new Error("missing address");
+ const store = new PiiStore(join(directory, "state"));
+ cleanup.push(() => store.close());
+ const broker = new CredentialBroker({ upstreamBaseUrl: `http://127.0.0.1:${address.port}`, voucherDir: join(directory, "vouchers") });
+ const a = broker.mint("a", "launch-a", ["send", "read"], "synthetic-a");
+ const b = broker.mint("b", "launch-b", ["send", "read"], "synthetic-b");
+ const proxy = await startCredentialProxy(broker, withPii ? { pii: store } : {});
+ cleanup.push(() => proxy.close());
+ const request = (path: string, body: unknown, voucher = a.voucher) => fetch(`${proxy.url}${path}`, {
+ method: "POST", headers: { "content-type": "application/json", authorization: `Bearer ${voucher}` }, body: JSON.stringify(body),
+ });
+ return { store, proxy, a, b, seen, request };
+}
+
+it("filters the real CLI API transport and direct canonical POST, preserving the send envelope and UTF-8 length", async () => {
+ const { proxy, a, seen, request } = await fixture();
+ const body = { agentId: "a", channel: "/test#1234/general", content: { text: "给 [email protected] 发消息", extra: "keep" }, nonce: "stable", replyToSeq: 4, attachments: ["existing-file"], force: true };
+ const api = createProxyServerApi({ proxyUrl: proxy.url, voucher: a.voucher });
+ const receipt = await api.send(body);
+ expect(receipt).toMatchObject({ state: "sent", pii: { triggeredFilters: [{ id: "builtin.emails", name: "Email addresses" }] } });
+ expect(seen[0]!.body).toEqual({ ...body, agentId: undefined, content: { text: "给 [REDACTED_EMAIL] 发消息", extra: "keep" } });
+ expect(Number(seen[0]!.headers["content-length"])).toBe(Buffer.byteLength(JSON.stringify(seen[0]!.body)));
+ expect(seen[0]!.headers["transfer-encoding"]).toBeUndefined();
+ expect(seen[0]!.headers["x-agent-id"]).toBe("a");
+ expect((await request("/api/community/channels/channel-id/messages", body)).status).toBe(200);
+ expect(JSON.stringify(seen)).not.toContain("[email protected]");
+});
+
+it("binds local list/add to voucher identity and never exposes remove/disable/upsert or owner routes", async () => {
+ const { request, store, a, b, seen, proxy } = await fixture();
+ for (const action of ["remove", "setEnabled", "update"]) {
+ expect((await request(LOCAL_PII_PATH, { action, botId: "b", pluginId: "builtin.emails", enabled: false })).status).toBe(422);
+ }
+ expect((await request(LOCAL_PII_PATH, { action: "add", botId: "b", name: "wrong", description: "cross bot", source: "export default (x: string) => x" })).status).toBe(422);
+ const response = await request(LOCAL_PII_PATH, { action: "add", name: "append", description: "append x", source: 'export default (text: string) => text + "x";' });
+ expect(response.status).toBe(200);
+ expect(store.list("a").plugins).toHaveLength(6);
+ expect(store.list("b").plugins).toHaveLength(5);
+ expect((await request("/api/community/bots/b/pii", { action: "remove" })).status).toBe(403);
+ expect((await request("/api/community/bots/a/pii", { action: "list" }, b.voucher)).status).toBe(403);
+ expect((await fetch(`${proxy.url}${LOCAL_PII_PATH}`, { headers: { authorization: `Bearer ${a.voucher}` } })).status).toBe(200);
+ expect(seen).toEqual([]);
+});
+
+it("blocks failures, empty output, invalid/oversized input and oversized UTF-8 output before any upstream request", async () => {
+ const { store, request, seen } = await fixture();
+ const source = 'export default (text: string) => { if(text === "throw-secret") throw new Error(text); if(text === "expand") return "中".repeat(400); if(text === "erase") return ""; return text; };';
+ await store.local("a", { action: "add", name: "failure", description: "synthetic behavior", source });
+ for (const text of ["throw-secret", "expand", "erase"]) {
+ const response = await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text } });
+ expect(response.status).toBe(422);
+ expect(await response.text()).not.toContain(text);
+ }
+ expect((await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text: "x".repeat(70_000) } })).status).toBe(422);
+ expect(seen).toEqual([]);
+ expect((await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text: "erase" }, attachments: ["file"] })).status).toBe(200);
+ expect(seen[0]!.body.content).toEqual({ text: "" });
+});
+
+
+it("rejects unsupported encodings and malformed JSON before contacting upstream", async () => {
+ const { proxy, a, seen } = await fixture();
+ for (const [headers, body] of [
+ [{}, "PRIVATE_BODY"],
+ [{ "content-type": "text/plain" }, "{}"],
+ [{ "content-type": "application/json", "content-encoding": "gzip" }, "{}"],
+ [{ "content-type": "application/json" }, "{PRIVATE_BODY"],
+ [{ "content-type": "application/json" }, "null"],
+ [{ "content-type": "application/json" }, "[]"],
+ ] as const) {
+ const response = await fetch(`${proxy.url}/api/community/channels/resolve/messages`, {
+ method: "POST", headers: { ...headers, authorization: `Bearer ${a.voucher}` }, body: Buffer.from(body),
+ });
+ expect(response.status).toBe(422);
+ expect(await response.json()).toEqual({ error: "PII_INVALID_REQUEST", code: "PII_INVALID_REQUEST" });
+ }
+ expect(seen).toEqual([]);
+});
+
+it("rejects invalid message content but permits an attachment without text", async () => {
+ const { request, seen } = await fixture();
+ for (const content of [undefined, null, [], "PRIVATE_BODY", { text: 17 }]) {
+ expect((await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content })).status).toBe(422);
+ }
+ expect(seen).toEqual([]);
+ expect((await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: {}, attachments: ["file"] })).status).toBe(200);
+ expect(seen[0]!.body.content).toEqual({ text: "" });
+ expect(seen[0]!.body.attachments).toEqual(["file"]);
+});
+
+it("returns a safe unavailable error when no local PII handler exists or the handler fails", async () => {
+ const missing = await fixture(false);
+ const get = (f: Awaited<ReturnType<typeof fixture>>) => fetch(`${f.proxy.url}${LOCAL_PII_PATH}`, { headers: { authorization: `Bearer ${f.a.voucher}` } });
+ expect(await (await get(missing)).json()).toEqual({ error: "PII_UNAVAILABLE", code: "PII_UNAVAILABLE" });
+ const available = await fixture();
+ vi.spyOn(available.store, "local").mockRejectedValueOnce(new Error("PRIVATE_FAILURE"));
+ expect(await (await get(available)).json()).toEqual({ error: "PII_UNAVAILABLE", code: "PII_UNAVAILABLE" });
+ expect(missing.seen).toEqual([]);
+ expect(available.seen).toEqual([]);
+});
+
+
+it("filters multinational IDs over the actual send transport and preserves unlabeled ordinary references", async () => {
+ const { proxy, a, seen, request } = await fixture();
+ const text = 'SSN: 219099999; SIN: 123456782; 12345678Z; My Number: 621498320257; order: 219099999.';
+ const expected = 'SSN: [REDACTED_ID_CARD]; SIN: [REDACTED_ID_CARD]; [REDACTED_ID_CARD]; My Number: [REDACTED_ID_CARD]; order: 219099999.';
+ const body = { channel: "/test#1234/general", nonce: "id-numbers", content: { text } };
+ await createProxyServerApi({ proxyUrl: proxy.url, voucher: a.voucher }).send(body);
+ expect(seen[0]!.body.content).toEqual({ text: expected });
+ expect((await request("/api/community/channels/channel-id/messages", { ...body, nonce: "id-numbers-direct" })).status).toBe(200);
+ expect(seen[1]!.body.content).toEqual({ text: expected });
+});
+
+
+it("filters all combined credential classes on the canonical send transport", async () => {
+ const { request, seen } = await fixture();
+ const text = `-----BEGIN PRIVATE KEY-----\nprivate material\n-----END PRIVATE KEY-----; sk-${"a".repeat(24)}; Authorization: Bearer synthetic-token; DB_PASSWORD=aB12345678; postgres://u:pass1234@host/db; order=abc123456789.`;
+ const expected = "[REDACTED_PRIVATE_KEY]; [REDACTED_API_KEY]; Authorization: Bearer [REDACTED_TOKEN]; DB_PASSWORD=[REDACTED_SECRET]; postgres://u:[REDACTED_PASSWORD]@host/db; order=abc123456789.";
+ expect((await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text }, nonce: "combined-credentials" })).status).toBe(200);
+ expect(seen).toHaveLength(1);
+ expect(seen[0]!.body).toMatchObject({ content: { text: expected }, nonce: "combined-credentials" });
+ expect(JSON.stringify(seen)).not.toContain("private material");
+ expect(JSON.stringify(seen)).not.toContain("synthetic-token");
+ expect(JSON.stringify(seen)).not.toContain("aB12345678");
+});
+
+
+it("omits PII metadata for no-hit sends and lists only enabled filters that trigger", async () => {
+ const { request, store } = await fixture();
+ const send = (text: string) => request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text } });
+ expect(await (await send("ordinary reference")).json()).not.toHaveProperty("pii");
+ const receipt = await (await send(`sk_test_${"a".repeat(32)} [email protected]`)).json();
+ expect(receipt.pii).toEqual({ triggeredFilters: [{ id: "builtin.secrets", name: "Keys" }, { id: "builtin.emails", name: "Email addresses" }] });
+ await store.manage("a", { action: "setEnabled", pluginId: "builtin.secrets", enabled: false, expectedRevision: 0 });
+ expect(await (await send(`sk_test_${"a".repeat(32)}`)).json()).not.toHaveProperty("pii");
+});
+
+it("keeps explicit trigger notices after later restoration and supports existing custom string plugins", async () => {
+ const { request, store } = await fixture();
+ await store.local("a", { action: "add", name: "first", description: "explicit trigger", source: 'export default () => ({text:"changed",triggered:true});' });
+ await store.local("a", { action: "add", name: "restore", description: "restore", source: 'export default () => ({text:"original",triggered:false});' });
+ const send = () => request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text: "original" } });
+ const receipt = await (await send()).json();
+ expect(receipt.pii.triggeredFilters.map((filter: { name: string }) => filter.name)).toEqual(["first"]);
+ await store.local("a", { action: "add", name: "legacy", description: "stored string plugin", source: 'export default (text: string) => text + " legacy";' });
+ expect((await (await send()).json()).pii.triggeredFilters.map((filter: { name: string }) => filter.name)).toEqual(["first", "legacy"]);
+});
+
+it.each([
+ [200, '{"state":"blocked","reason":"unaligned","unreadCount":1,"latestSeq":2}'],
+ [422, '{"error":"denied"}'],
+ [503, '{"error":"unavailable"}'],
+ [200, 'not JSON'],
+ [200, '{"state":"sent"}'],
+ [200, JSON.stringify({ state: "sent", message: { channel: "/test#1234/general", seq: "#1" }, padding: "x".repeat(65537) })],
+])("preserves upstream status/body without a fabricated notice for status %s", async (status, body) => {
+ const { request } = await fixture(true, { status, body });
+ const response = await request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text: "[email protected]" } });
+ expect(response.status).toBe(status);
+ expect(await response.text()).toBe(body);
+});
+
+it("does not report a successful send when the annotated upstream response breaks mid-body", async () => {
+ const { request } = await fixture(true, { abort: true });
+ await expect(request("/api/community/channels/resolve/messages", { channel: "/test#1234/general", content: { text: "[email protected]" } }).then(response => response.text())).rejects.toThrow();
+});
diff --git a/src/daemon/src/pii/proxy.ts b/src/daemon/src/pii/proxy.ts
new file mode 100644
index 000000000..e909bf1fb
--- /dev/null
+++ b/src/daemon/src/pii/proxy.ts
@@ -0,0 +1,123 @@
+import type { IncomingMessage, ServerResponse } from "node:http";
+import { CommunityAgentSendRequestSchema, PiiLocalRequestSchema, PII_TEXT_MAX_BYTES } from "@alook/shared";
+import type { PiiStore } from "./store.js";
+import { PiiError } from "./evaluator.js";
+
+export const LOCAL_PII_PATH = "/__alook/local/pii";
+export type PiiProxy = Pick<PiiStore, "filterWithResult" | "local">;
+
+async function readJson(request: IncomingMessage): Promise<Record<string, unknown>> {
+ if (!request.headers["content-type"]?.toLowerCase().startsWith("application/json")
+ || (request.headers["content-encoding"] && request.headers["content-encoding"] !== "identity")) {
+ request.resume();
+ throw new PiiError("PII_INVALID_REQUEST");
+ }
+ const chunks: Buffer[] = [];
+ let bytes = 0;
+ return new Promise((resolve, reject) => {
+ let finished = false;
+ const fail = () => {
+ if (finished) return;
+ finished = true;
+ clearTimeout(timer);
+ request.resume();
+ reject(new PiiError("PII_INVALID_REQUEST"));
+ };
+ const timer = setTimeout(fail, 3_000);
+ request.on("data", (chunk: Buffer) => {
+ bytes += chunk.byteLength;
+ if (bytes > 64 * 1024) fail();
+ else if (!finished) chunks.push(chunk);
+ });
+ request.on("error", fail);
+ request.on("aborted", fail);
+ request.on("end", () => {
+ if (finished) return;
+ clearTimeout(timer);
+ try {
+ const value: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8"));
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw 0;
+ finished = true;
+ resolve(value as Record<string, unknown>);
+ } catch { fail(); }
+ });
+ });
+}
+
+export function writePiiError(response: ServerResponse, error: unknown): void {
+ const safe = error instanceof PiiError ? error : new PiiError("PII_UNAVAILABLE");
+ response.writeHead(422, { "content-type": "application/json" });
+ response.end(JSON.stringify({ error: safe.message, code: safe.code, ...(safe.pluginId ? { pluginId: safe.pluginId } : {}) }));
+}
+
+export async function handleLocalPii(request: IncomingMessage, response: ServerResponse, botId: string, pii?: PiiProxy): Promise<void> {
+ try {
+ if (!pii) throw new PiiError("PII_UNAVAILABLE");
+ const input = request.method === "GET" ? { action: "list" } : await readJson(request);
+ const parsed = PiiLocalRequestSchema.safeParse(input);
+ if (!parsed.success) throw new PiiError("PII_INVALID_REQUEST");
+ const snapshot = await pii.local(botId, parsed.data);
+ response.writeHead(200, { "content-type": "application/json" });
+ response.end(JSON.stringify(snapshot));
+ } catch (error) { writePiiError(response, error); }
+}
+
+export async function filterPiiRequest(request: IncomingMessage, botId: string, pii: PiiProxy): Promise<{ body: Buffer; triggeredFilters: { id: string; name: string }[] }> {
+ const body = await readJson(request);
+ const content = body.content;
+ if (!content || typeof content !== "object" || Array.isArray(content)
+ || ((content as { text?: unknown }).text !== undefined && typeof (content as { text?: unknown }).text !== "string")) {
+ throw new PiiError("PII_INVALID_REQUEST");
+ }
+ const { text, triggeredFilters } = await pii.filterWithResult(botId, (content as { text?: string }).text ?? "");
+ if (Buffer.byteLength(text, "utf8") > PII_TEXT_MAX_BYTES) throw new PiiError("PII_OUTPUT_TOO_LARGE");
+ body.content = { ...content, text };
+ if (!CommunityAgentSendRequestSchema.safeParse(body).success) throw new PiiError("PII_INVALID_RESULT");
+ return { body: Buffer.from(JSON.stringify(body)), triggeredFilters };
+}
+
+
+export function forwardPiiSendResponse(upstream: IncomingMessage, response: ServerResponse, triggeredFilters: { id: string; name: string }[]): void {
+ const chunks: Buffer[] = [];
+ let bytes = 0, streaming = false;
+ const write = (chunk: Buffer) => {
+ if (!response.write(chunk)) {
+ upstream.pause();
+ response.once("drain", () => upstream.resume());
+ }
+ };
+ upstream.on("data", (chunk: Buffer) => {
+ if (streaming) { write(chunk); return; }
+ chunks.push(Buffer.from(chunk));
+ bytes += chunk.byteLength;
+ if (bytes > 64 * 1024) {
+ streaming = true;
+ response.writeHead(upstream.statusCode ?? 502, upstream.headers);
+ chunks.forEach(write);
+ chunks.length = 0;
+ }
+ });
+ upstream.on("end", () => {
+ if (streaming) { response.end(); return; }
+ const body = Buffer.concat(chunks);
+ let output = body;
+ try {
+ const value: unknown = JSON.parse(body.toString("utf8"));
+ if (value && typeof value === "object" && !Array.isArray(value)
+ && (value as { state?: unknown }).state === "sent"
+ && (value as { message?: unknown }).message && typeof (value as { message: unknown }).message === "object"
+ && typeof (value as { message: { channel?: unknown } }).message.channel === "string"
+ && typeof (value as { message: { seq?: unknown } }).message.seq === "string") {
+ output = Buffer.from(JSON.stringify({ ...value, pii: { triggeredFilters } }));
+ }
+ } catch {}
+ const headers = { ...upstream.headers };
+ if (output !== body) {
+ delete headers["transfer-encoding"];
+ delete headers.etag;
+ headers["content-length"] = String(output.byteLength);
+ }
+ response.writeHead(upstream.statusCode ?? 502, headers);
+ response.end(output);
+ });
+}
diff --git a/src/daemon/src/pii/store.test.ts b/src/daemon/src/pii/store.test.ts
new file mode 100644
index 000000000..3631073f1
--- /dev/null
+++ b/src/daemon/src/pii/store.test.ts
@@ -0,0 +1,339 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { PiiStore } from "./store.js";
+import { compilePiiSource, evaluatePii, PiiRunner } from "./evaluator.js";
+import { PII_DEFAULTS } from "./defaults.js";
+
+vi.mock("node:fs", async (importOriginal) => {
+ const original = await importOriginal<typeof import("node:fs")>();
+ return { ...original, readFileSync: vi.fn(original.readFileSync) };
+});
+vi.mock("./evaluator.js", async (importOriginal) => {
+ const original = await importOriginal<typeof import("./evaluator.js")>();
+ return { ...original, compilePiiSource: vi.fn(original.compilePiiSource) };
+});
+
+const directories: string[] = [];
+const stores: PiiStore[] = [];
+const storeAt = (path: string) => { const store = new PiiStore(path); stores.push(store); return store; };
+const create = () => {
+ const directory = mkdtempSync(join(tmpdir(), "pii-test-"));
+ directories.push(directory);
+ const path = join(directory, "machine");
+ return { store: storeAt(path), path };
+};
+const fileFor = (path: string, botId = "a") => join(path, botId, "pii", "pipelines.json");
+afterEach(async () => {
+ await Promise.all(stores.splice(0).map(store => store.close()));
+ directories.splice(0).forEach(directory => rmSync(directory, { recursive: true, force: true }));
+});
+
+describe("PII pipeline", () => {
+ it("composes TS modules in order, isolates bots, skips disabled nodes and retains explicit deletion", async () => {
+ const { store, path } = create();
+ let snapshot = store.list("a");
+ for (const plugin of snapshot.plugins) {
+ const result = await store.manage("a", { action: "remove", pluginId: plugin.id, expectedRevision: snapshot.revision });
+ if (result.status !== "complete") throw new Error("remove failed");
+ snapshot = result.snapshot;
+ }
+ expect(storeAt(path).list("a").plugins).toEqual([]);
+ expect(store.list("b").plugins).toHaveLength(5);
+ snapshot = await store.local("a", { action: "add", name: "first", description: "replace seed", source: 'export default (text: string): string => text.replace("seed", "middle");' });
+ snapshot = await store.local("a", { action: "add", name: "second", description: "replace middle", source: 'export default function(text: string): string { return text.replace("middle", "done"); }' });
+ expect(await store.filter("a", "seed")).toBe("done");
+ expect(await store.filter("b", "seed")).toBe("seed");
+ expect(await store.manage("a", { action: "setEnabled", pluginId: snapshot.plugins[0]!.id, enabled: false, expectedRevision: snapshot.revision - 1 })).toMatchObject({ status: "error", code: "PII_CONFLICT", snapshot });
+ expect(await store.manage("a", { action: "setEnabled", pluginId: snapshot.plugins[0]!.id, enabled: false, expectedRevision: snapshot.revision })).toMatchObject({ status: "complete" });
+ expect(await storeAt(path).filter("a", "seed")).toBe("seed");
+ expect(readFileSync(fileFor(path), "utf8")).toContain("export default");
+ expect(snapshot.plugins[0]).not.toHaveProperty("source");
+ });
+
+ it("does not lose concurrent additions and does not accept invalid plugins", async () => {
+ const { store } = create();
+ const input = { action: "add" as const, name: "extra", description: "identity", source: 'export default (text: string) => text;' };
+ await Promise.all([store.local("a", input), store.local("a", input)]);
+ const snapshot = store.list("a");
+ expect(snapshot.plugins).toHaveLength(7);
+ expect(new Set(snapshot.plugins.map((plugin) => plugin.id)).size).toBe(7);
+ for (const source of ['export default () => 1;', 'export default async () => "x";', 'export default () => process.env;', 'import fs from "node:fs"; export default () => fs.readFileSync("x");']) {
+ await expect(store.local("a", { ...input, source })).rejects.toThrow(/^PII_/);
+ }
+ expect(store.list("a")).toEqual(snapshot);
+ });
+
+ it("previews the same ordered runner at a captured revision without persisting input, output or defaults", async () => {
+ const { store, path } = create();
+ const file = fileFor(path);
+ expect(existsSync(path)).toBe(false);
+ expect(await store.manage("a", { action: "preview", text: "[email protected]", expectedRevision: 0 }))
+ .toMatchObject({ status: "complete", output: "[REDACTED_EMAIL]", snapshot: { revision: 0 } });
+ expect(existsSync(path)).toBe(false);
+ const snapshot = await store.local("a", { action: "add", name: "last", description: "transform previous output", source: 'export default (text: string) => text.replace("[REDACTED_EMAIL]", "[FINAL]");' });
+ const before = readFileSync(file, "utf8");
+ const pending = store.manage("a", { action: "preview", text: "[email protected]", expectedRevision: snapshot.revision });
+ const changed = await store.manage("a", { action: "setEnabled", pluginId: "builtin.emails", enabled: false, expectedRevision: snapshot.revision });
+ expect(changed.status).toBe("complete");
+ expect(await pending).toEqual({ status: "complete", output: "[FINAL]", snapshot, triggeredFilters: snapshot.plugins.filter(p => p.id === "builtin.emails" || p.name === "last").map(({id, name}) => ({id, name})) });
+ expect(await store.manage("a", { action: "preview", text: "[email protected]", expectedRevision: snapshot.revision }))
+ .toMatchObject({ status: "error", code: "PII_CONFLICT", snapshot: { revision: snapshot.revision + 1 } });
+ const after = readFileSync(file, "utf8");
+ expect(after).not.toBe(before);
+ expect(after).not.toContain("[email protected]");
+ expect(await store.manage("a", { action: "preview", text: "[email protected]", expectedRevision: snapshot.revision + 1 }))
+ .toMatchObject({ status: "complete", output: "[email protected]", triggeredFilters: [] });
+ expect(readFileSync(file, "utf8")).toBe(after);
+ });
+
+ it("previews empty chains and returns bounded safe failures with no state writes", async () => {
+ const { store, path } = create();
+ let snapshot = store.list("a");
+ for (const plugin of snapshot.plugins) {
+ const result = await store.manage("a", { action: "remove", pluginId: plugin.id, expectedRevision: snapshot.revision });
+ if (result.status !== "complete") throw new Error("remove failed");
+ snapshot = result.snapshot;
+ }
+ expect(await store.manage("a", { action: "preview", text: "unchanged", expectedRevision: snapshot.revision }))
+ .toEqual({ status: "complete", output: "unchanged", snapshot, triggeredFilters: [] });
+ snapshot = await store.local("a", { action: "add", name: "fixture", description: "safe error fixtures", source: 'export default (text: string) => { if(!text)return text; if(text==="expand")return "中".repeat(400); if(text==="erase")return ""; if(text==="loop"){while(true){}} if(text==="number")return 1; throw new Error(text); };' });
+ const before = readFileSync(fileFor(path), "utf8");
+ for (const [text, code] of [["expand", "PII_OUTPUT_TOO_LARGE"], ["erase", "PII_INVALID_RESULT"], ["number", "PII_INVALID_RESULT"], ["loop", "PII_PLUGIN_FAILED"], ["private-error-text", "PII_PLUGIN_FAILED"]]) {
+ const result = await store.manage("a", { action: "preview", text: text!, expectedRevision: snapshot.revision });
+ expect(result).toMatchObject({ status: "error", code });
+ expect(result).not.toHaveProperty("output");
+ expect(JSON.stringify(result)).not.toContain(text);
+ }
+ expect(await store.manage("a", { action: "preview", text: "中".repeat(400), expectedRevision: snapshot.revision }))
+ .toEqual({ status: "error", code: "PII_INVALID_REQUEST" });
+ expect(readFileSync(fileFor(path), "utf8")).toBe(before);
+ });
+
+ it("fails closed on corrupt and missing established config", async () => {
+ const { store, path } = create();
+ store.list("a");
+ writeFileSync(fileFor(path), "invalid");
+ await expect(storeAt(path).filter("a", "private input")).rejects.toThrow("PII_CONFIG_UNAVAILABLE");
+ expect(await storeAt(path).manage("a", { action: "list" })).toEqual({ status: "error", code: "PII_UNAVAILABLE" });
+ rmSync(fileFor(path));
+ expect(() => storeAt(path).list("a")).toThrow("PII_CONFIG_UNAVAILABLE");
+ });
+
+ it("reads and writes only the selected bot file, even when another bot file is corrupt or missing", async () => {
+ const { store, path } = create();
+ const a = store.list("a");
+ const b = store.list("b");
+ const bFile = fileFor(path, "b");
+ const bBytes = readFileSync(bFile, "utf8");
+ const added = await store.local("a", { action: "add", name: "a-only", description: "only A", source: 'export default (text: string) => text.replace("ALPHA_ONLY", "A_FILTERED");' });
+ expect(added.revision).toBe(a.revision + 1);
+ expect(readFileSync(bFile, "utf8")).toBe(bBytes);
+ expect(store.list("b")).toEqual(b);
+ expect(JSON.parse(readFileSync(fileFor(path), "utf8"))).toMatchObject({ version: 1, revision: 1 });
+ expect(JSON.parse(bBytes)).not.toHaveProperty("bots");
+ expect(bBytes).not.toContain("ALPHA_ONLY");
+ expect(existsSync(join(path, "pii", "pipelines.json"))).toBe(false);
+ if (process.platform !== "win32") {
+ expect(statSync(fileFor(path)).mode & 0o777).toBe(0o600);
+ expect(statSync(join(path, "a", "pii")).mode & 0o777).toBe(0o700);
+ }
+ writeFileSync(bFile, "corrupt B");
+ expect(await storeAt(path).filter("a", "ALPHA_ONLY")).toBe("A_FILTERED");
+ await expect(storeAt(path).filter("b", "ALPHA_ONLY")).rejects.toThrow("PII_CONFIG_UNAVAILABLE");
+ rmSync(bFile);
+ expect(await storeAt(path).filter("a", "ALPHA_ONLY")).toBe("A_FILTERED");
+ expect(() => storeAt(path).list("b")).toThrow("PII_CONFIG_UNAVAILABLE");
+ writeFileSync(bFile, bBytes);
+ writeFileSync(fileFor(path), "corrupt A");
+ expect(await storeAt(path).filter("b", "ALPHA_ONLY")).toBe("ALPHA_ONLY");
+ });
+
+ it("keeps the same bot isolated across daemon roots and rejects unsafe path segments before touching disk", async () => {
+ const first = create();
+ const second = create();
+ const defaults = second.store.list("a");
+ await first.store.local("a", { action: "add", name: "first", description: "first daemon only", source: 'export default (text: string) => text.replace("seed", "first");' });
+ expect(await first.store.filter("a", "seed")).toBe("first");
+ expect(await second.store.filter("a", "seed")).toBe("seed");
+ expect(second.store.list("a")).toEqual(defaults);
+ const fresh = create();
+ const parentBefore = readdirSync(join(fresh.path, ".."));
+ for (const id of ["", ".", "..", "../a", "a/b", "a\\b", "/tmp/a", "a".repeat(129), "a\0b"]) {
+ expect(() => fresh.store.list(id)).toThrow("PII_INVALID_REQUEST");
+ await expect(fresh.store.local(id, { action: "add", name: "safe", description: "identity", source: 'export default (text: string) => text;' })).rejects.toThrow("PII_INVALID_REQUEST");
+ }
+ expect(existsSync(fresh.path)).toBe(false);
+ expect(readdirSync(join(fresh.path, ".."))).toEqual(parentBefore);
+ });
+
+ it("reuses loaded configuration and compiled filters without disk reads or compilation on repeated sends", async () => {
+ const { store, path } = create();
+ store.list("a");
+ const restarted = storeAt(path);
+ expect(await restarted.filter("a", "[email protected]")).toBe("[REDACTED_EMAIL]");
+ const reads = vi.mocked(readFileSync).mock.calls.length;
+ const compilations = vi.mocked(compilePiiSource).mock.calls.length;
+ for (let i = 0; i < 3; i++) expect(await restarted.filter("a", "[email protected]")).toBe("[REDACTED_EMAIL]");
+ expect(vi.mocked(readFileSync).mock.calls).toHaveLength(reads);
+ expect(vi.mocked(compilePiiSource).mock.calls).toHaveLength(compilations);
+ const changed = await restarted.manage("a", { action: "setEnabled", pluginId: "builtin.emails", enabled: false, expectedRevision: 0 });
+ expect(changed.status).toBe("complete");
+ expect(await restarted.filter("a", "[email protected]")).toBe("[email protected]");
+ const afterChange = vi.mocked(compilePiiSource).mock.calls.length;
+ expect(await restarted.filter("a", "[email protected]")).toBe("[email protected]");
+ expect(vi.mocked(compilePiiSource).mock.calls).toHaveLength(afterChange);
+ expect(vi.mocked(readFileSync).mock.calls).toHaveLength(reads);
+ expect(await storeAt(path).filter("a", "[email protected]")).toBe("[email protected]");
+ });
+
+ it("publishes a new memory snapshot only after persistence succeeds", async () => {
+ const { store, path } = create();
+ const before = store.list("a");
+ const file = fileFor(path);
+ const persisted = readFileSync(file, "utf8");
+ rmSync(file);
+ mkdirSync(file);
+ expect(await store.manage("a", { action: "setEnabled", pluginId: "builtin.emails", enabled: false, expectedRevision: before.revision }))
+ .toEqual({ status: "error", code: "PII_UNAVAILABLE" });
+ await expect(store.local("a", { action: "add", name: "unpersisted", description: "must not apply", source: 'export default (text: string) => text + "CHANGED";' })).rejects.toThrow("PII_CONFIG_UNAVAILABLE");
+ expect(store.list("a")).toEqual(before);
+ expect(await store.filter("a", "[email protected]")).toBe("[REDACTED_EMAIL]");
+ await expect(storeAt(path).filter("a", "[email protected]")).rejects.toThrow("PII_CONFIG_UNAVAILABLE");
+ rmSync(file, { recursive: true });
+ writeFileSync(file, persisted);
+ expect(storeAt(path).list("a")).toEqual(before);
+ });
+
+ it("reuses only the engine, isolates globals, serializes jobs and recovers queued work after failure", async () => {
+ const runner = new PiiRunner();
+ const node = (source: string) => [{ id: "fixture", code: compilePiiSource(source) }];
+ const isolated = node('export default (text: string) => { globalThis.marker = (globalThis.marker || 0) + 1; return text + globalThis.marker; };');
+ try {
+ expect(await runner.evaluate(isolated, "a")).toBe("a1");
+ expect(await runner.evaluate(isolated, "b")).toBe("b1");
+ const failed = runner.evaluate(node('export default (text: string) => /^(a+)+$/.test(text) ? text : "no";'), "a".repeat(100) + "!");
+ const queued = runner.evaluate(isolated, "queued");
+ await expect(failed).rejects.toThrow(/^PII_/);
+ expect(await queued).toBe("queued1");
+ expect(await Promise.all([runner.evaluate(isolated, "first"), runner.evaluate(isolated, "second")])).toEqual(["first1", "second1"]);
+ } finally { await runner.close(); }
+ await expect(runner.evaluate(isolated, "stopped")).rejects.toThrow("PII_UNAVAILABLE");
+ });
+
+ it("bounds waiting jobs and queue time when slow plugins contend", async () => {
+ const runner = new PiiRunner();
+ const plugins = [{ id: "slow", code: compilePiiSource('export default () => { while(true) {} };') }];
+ const started = Date.now();
+ try {
+ const results = await Promise.allSettled(Array.from({ length: 34 }, () => runner.evaluate(plugins, "PRIVATE_QUEUE")));
+ expect(results[0]).toMatchObject({ status: "rejected", reason: { code: "PII_PLUGIN_FAILED" } });
+ expect(results[31]).toMatchObject({ status: "rejected", reason: { code: "PII_UNAVAILABLE" } });
+ expect(results[33]).toMatchObject({ status: "rejected", reason: { code: "PII_UNAVAILABLE" } });
+ expect(Date.now() - started).toBeLessThan(4_500);
+ expect(JSON.stringify(results)).not.toContain("PRIVATE_QUEUE");
+ expect(await runner.evaluate([{ id: "ok", code: compilePiiSource('export default (text: string) => text;') }], "recovered")).toBe("recovered");
+ } finally { await runner.close(); }
+ });
+
+ it("closes active and queued evaluations without leaving worker work running", async () => {
+ const runner = new PiiRunner();
+ const plugins = [{ id: "loop", code: compilePiiSource('export default () => { while(true) {} };') }];
+ const active = runner.evaluate(plugins, "");
+ const queued = runner.evaluate(plugins, "");
+ const results = Promise.allSettled([active, queued]);
+ await new Promise(resolve => setTimeout(resolve, 20));
+ await runner.close();
+ expect((await results).map(result => result.status)).toEqual(["rejected", "rejected"]);
+ });
+
+ it("bounds infinite loops, complex regular expressions and allocations without exposing raw exceptions", async () => {
+ const sources = [
+ 'export default () => { while(true) {} };',
+ 'export default (text: string) => /^(a+)+$/.test(text) ? text : "no";',
+ 'export default () => "x".repeat(100000000);',
+ 'export default (text: string) => { throw new Error(text); };',
+ ];
+ for (const source of sources) {
+ const started = Date.now();
+ await expect(evaluatePii([{ id: "bounded", code: compilePiiSource(source) }], "a".repeat(100) + "!PRIVATE")).rejects.toThrow(/^PII_/);
+ expect(Date.now() - started).toBeLessThan(4_000);
+ }
+ expect(await evaluatePii([{ id: "host", code: compilePiiSource('export default () => [typeof process,typeof require,typeof fetch,typeof Bun,typeof console].join(",");') }], "")).toBe("undefined,undefined,undefined,undefined,undefined");
+ });
+});
+
+describe("PII configuration recovery", () => {
+ it("keeps missing-filter removal idempotent and refuses to enable a missing filter", async () => {
+ const { store, path } = create();
+ const snapshot = store.list("a");
+ const before = readFileSync(fileFor(path), "utf8");
+ expect(await store.manage("a", { action: "remove", pluginId: "missing", expectedRevision: snapshot.revision }))
+ .toEqual({ status: "complete", snapshot });
+ expect(await store.manage("a", { action: "setEnabled", pluginId: "missing", enabled: true, expectedRevision: snapshot.revision }))
+ .toEqual({ status: "error", code: "PII_PLUGIN_NOT_FOUND" });
+ expect(readFileSync(fileFor(path), "utf8")).toBe(before);
+ });
+
+ it("blocks installed source corruption without leaking the sample or replacing the stored rules", async () => {
+ const { store, path } = create();
+ const snapshot = store.list("a");
+ const file = fileFor(path);
+ const persisted = JSON.parse(readFileSync(file, "utf8"));
+ persisted.plugins[0].source = "export default (";
+ const bytes = JSON.stringify(persisted);
+ writeFileSync(file, bytes);
+ const restarted = storeAt(path);
+ await expect(restarted.filter("a", "PRIVATE_SAMPLE")).rejects.toMatchObject({ code: "PII_INVALID_SOURCE", pluginId: snapshot.plugins[0]!.id });
+ const result = await restarted.manage("a", { action: "preview", text: "PRIVATE_SAMPLE", expectedRevision: snapshot.revision });
+ expect(result).toEqual({ status: "error", code: "PII_INVALID_SOURCE", pluginId: snapshot.plugins[0]!.id });
+ expect(JSON.stringify(result)).not.toContain("PRIVATE_SAMPLE");
+ expect(readFileSync(file, "utf8")).toBe(bytes);
+ });
+});
+
+describe("default filter categories", () => {
+ it("redacts email before sentence punctuation without taking the punctuation or matching filenames", async () => {
+ const { store } = create();
+ for (const punctuation of [".", "...", ".\nNext sentence", ",", "。", ",", ";", "!", "?"]) {
+ expect(await store.filter("a", `Email me at [email protected]${punctuation}`)).toBe(`Email me at [REDACTED_EMAIL]${punctuation}`);
+ }
+ expect(await store.filter("a", "[email protected]. Notes follow.")).toBe("[email protected]. Notes follow.");
+ });
+ const fixtures = [
+ ["builtin.secrets", "before -----BEGIN PRIVATE KEY-----\nprivate material\n-----END PRIVATE KEY----- after", "before [REDACTED_PRIVATE_KEY] after", "a public key"],
+ ["builtin.secrets", `key sk-${"a".repeat(24)}`, "key [REDACTED_API_KEY]", "sk-short"],
+ ["builtin.secrets", "Authorization: Bearer synthetic-token", "Authorization: Bearer [REDACTED_TOKEN]", "a bearer of news"],
+ ["builtin.secrets", 'DB_PASSWORD="aB12345678" postgres://u:pass1234@host/db', 'DB_PASSWORD="[REDACTED_SECRET]" postgres://u:[REDACTED_PASSWORD]@host/db', "token_id=abc123456789 process.env.ACCESS_TOKEN"],
+ ["builtin.emails", "mail [email protected] please", "mail [REDACTED_EMAIL] please", "@gus#1813 [email protected]"],
+ ["builtin.id-cards", "id 11010519491231002X", "id [REDACTED_ID_CARD]", "id 110105194912310021"],
+ ["builtin.phones", "call +86 13800138000", "call [REDACTED_PHONE]", "prefix13800138000suffix"],
+ ["builtin.bank-cards", "card 4111 1111 1111 1111", "card [REDACTED_BANK_CARD]", "4111 1111 1111 1112"],
+ ];
+ it.each(fixtures)("%s preserves surrounding and unrelated text", async (id, input, output, negative) => {
+ const definition = PII_DEFAULTS.find((plugin) => plugin.id === id)!;
+ const plugins = [{ id: definition.id, code: compilePiiSource(definition.source) }];
+ expect(await evaluatePii(plugins, input!)).toBe(output);
+ expect(await evaluatePii(plugins, negative!)).toBe(negative);
+ });
+ it("is idempotent and preserves UUIDs across nodes", async () => {
+ const { store } = create();
+ const input = "[email protected] +86 13800138000 aaaa4111-1111-1111-9007-123456789012";
+ const once = await store.filter("a", input);
+ expect(once).toContain("aaaa4111-1111-1111-9007-123456789012");
+ expect(await store.filter("a", once)).toBe(once);
+ });
+});
+
+it("reports preview matches even when a later filter restores the original text", async () => {
+ const { store } = create();
+ await store.local("a", { action: "add", name: "first", description: "first change", source: 'export default (text: string) => text.replace("seed", "middle");' });
+ const snapshot = await store.local("a", { action: "add", name: "second", description: "restore", source: 'export default (text: string) => text.replace("middle", "seed");' });
+ const expected = snapshot.plugins.filter(plugin => plugin.name === "first" || plugin.name === "second").map(({ id, name }) => ({ id, name }));
+ expect(await store.manage("a", { action: "preview", text: "seed", expectedRevision: snapshot.revision }))
+ .toMatchObject({ status: "complete", output: "seed", triggeredFilters: expected });
+ expect(await store.filterWithResult("a", "seed")).toEqual({ text: "seed", triggeredFilters: expected });
+ expect(await store.manage("a", { action: "preview", text: "ordinary", expectedRevision: snapshot.revision }))
+ .toMatchObject({ status: "complete", output: "ordinary", triggeredFilters: [] });
+});
diff --git a/src/daemon/src/pii/store.ts b/src/daemon/src/pii/store.ts
new file mode 100644
index 000000000..e9f29b52e
--- /dev/null
+++ b/src/daemon/src/pii/store.ts
@@ -0,0 +1,150 @@
+import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { randomUUID } from "node:crypto";
+import { z } from "zod";
+import { PiiPluginSchema, PiiPreviewTextSchema, PiiErrorCodeSchema, PII_MAX_PLUGINS, PII_SOURCE_MAX_BYTES, PII_TEXT_MAX_BYTES, type PiiLocalRequest, type PiiOperation, type PiiResult, type PiiSnapshot } from "@alook/shared";
+import { PII_DEFAULTS } from "./defaults.js";
+import { compilePiiSource, PiiError, PiiRunner } from "./evaluator.js";
+
+const storedPlugin = PiiPluginSchema.extend({ source: z.string().min(1).max(PII_SOURCE_MAX_BYTES) });
+const storedPipeline = z.strictObject({ version: z.literal(1), revision: z.number().int().nonnegative(), plugins: z.array(storedPlugin).max(PII_MAX_PLUGINS) });
+type Pipeline = z.infer<typeof storedPipeline>;
+
+export class PiiStore {
+ private readonly runner = new PiiRunner();
+ private readonly pipelines = new Map<string, Pipeline>();
+ private readonly compiled = new WeakMap<Pipeline, readonly { id: string; code: string }[]>();
+ constructor(private readonly directory: string) {}
+
+ close(): Promise<void> { return this.runner.close(); }
+
+ private directoryFor(botId: string): string {
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(botId)) throw new PiiError("PII_INVALID_REQUEST");
+ return join(this.directory, botId, "pii");
+ }
+
+ private read(botId: string, persist = true): Pipeline {
+ const directory = this.directoryFor(botId);
+ const cached = this.pipelines.get(botId);
+ if (cached) return cached;
+ if (!existsSync(directory)) {
+ const created: Pipeline = { version: 1, revision: 0, plugins: PII_DEFAULTS.map((plugin) => ({ ...plugin, enabled: true })) };
+ if (persist) this.save(botId, created);
+ return created;
+ }
+ try {
+ const file = join(directory, "pipelines.json");
+ if (statSync(file).size > 16 * 1024 * 1024) throw 0;
+ const pipeline = storedPipeline.parse(JSON.parse(readFileSync(file, "utf8")));
+ this.pipelines.set(botId, pipeline);
+ return pipeline;
+ } catch {
+ throw new PiiError("PII_CONFIG_UNAVAILABLE");
+ }
+ }
+
+ private save(botId: string, pipeline: Pipeline): void {
+ const directory = this.directoryFor(botId);
+ const temporary = join(directory, `.${randomUUID()}.tmp`);
+ try {
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
+ chmodSync(directory, 0o700);
+ writeFileSync(temporary, JSON.stringify(pipeline), { mode: 0o600, flag: "wx" });
+ renameSync(temporary, join(directory, "pipelines.json"));
+ this.pipelines.set(botId, pipeline);
+ } catch {
+ throw new PiiError("PII_CONFIG_UNAVAILABLE");
+ } finally {
+ rmSync(temporary, { force: true });
+ }
+ }
+
+ private snapshot(pipeline: Pipeline): PiiSnapshot {
+ return {
+ revision: pipeline.revision,
+ plugins: pipeline.plugins.map(({ id, name, description, enabled }) => ({ id, name, description, enabled })),
+ };
+ }
+
+ list(botId: string): PiiSnapshot {
+ return this.snapshot(this.read(botId));
+ }
+
+ async local(botId: string, input: PiiLocalRequest): Promise<PiiSnapshot> {
+ if (input.action === "list") return this.list(botId);
+ const code = compilePiiSource(input.source);
+ await this.runner.evaluate([{ id: "new-plugin", code }], "");
+ const current = this.read(botId, false);
+ const pipeline = { ...current, plugins: [...current.plugins] };
+ if (pipeline.plugins.length >= PII_MAX_PLUGINS) throw new PiiError("PII_PLUGIN_LIMIT");
+ pipeline.plugins.push({ id: randomUUID(), name: input.name, description: input.description, enabled: true, source: input.source });
+ pipeline.revision++;
+ this.save(botId, pipeline);
+ return this.snapshot(pipeline);
+ }
+
+ async manage(botId: string, operation: PiiOperation): Promise<PiiResult> {
+ try {
+ const pipeline = this.read(botId, operation.action === "list");
+ if (operation.action === "list") return { status: "complete", snapshot: this.snapshot(pipeline) };
+ if (operation.expectedRevision !== pipeline.revision) return { status: "error", code: "PII_CONFLICT", snapshot: this.snapshot(pipeline) };
+ if (operation.action === "preview") {
+ if (!PiiPreviewTextSchema.safeParse(operation.text).success) throw new PiiError("PII_INVALID_REQUEST");
+ const { text: output, triggeredFilters } = await this.filterPipelineWithResult(pipeline, operation.text);
+ if (Buffer.byteLength(output, "utf8") > PII_TEXT_MAX_BYTES) throw new PiiError("PII_OUTPUT_TOO_LARGE");
+ if (!output.trim()) throw new PiiError("PII_INVALID_RESULT");
+ return { status: "complete", snapshot: this.snapshot(pipeline), output, triggeredFilters };
+ }
+ const index = pipeline.plugins.findIndex((plugin) => plugin.id === operation.pluginId);
+ if (index === -1) return operation.action === "remove"
+ ? { status: "complete", snapshot: this.snapshot(pipeline) }
+ : { status: "error", code: "PII_PLUGIN_NOT_FOUND" };
+ const plugins = [...pipeline.plugins];
+ if (operation.action === "remove") plugins.splice(index, 1);
+ else plugins[index] = { ...plugins[index]!, enabled: operation.enabled };
+ const updated = { ...pipeline, revision: pipeline.revision + 1, plugins };
+ this.save(botId, updated);
+ return { status: "complete", snapshot: this.snapshot(updated) };
+ } catch (error) {
+ const code = error instanceof PiiError ? PiiErrorCodeSchema.safeParse(error.code) : undefined;
+ if (operation.action === "preview" && code?.success) return {
+ status: "error", code: code.data, ...(error instanceof PiiError && error.pluginId ? { pluginId: error.pluginId } : {}),
+ };
+ return { status: "error", code: "PII_UNAVAILABLE" };
+ }
+ }
+
+ async filter(botId: string, text: string): Promise<string> {
+ const pipeline = this.read(botId);
+ return this.run(pipeline, text);
+ }
+
+ async filterWithResult(botId: string, text: string): Promise<{ text: string; triggeredFilters: { id: string; name: string }[] }> {
+ const pipeline = this.read(botId);
+ return this.filterPipelineWithResult(pipeline, text);
+ }
+
+ private async filterPipelineWithResult(pipeline: Pipeline, text: string) {
+ const result = await this.runWithResult(pipeline, text);
+ return { text: result.text, triggeredFilters: result.triggers.filter(trigger => trigger.triggered).map(trigger => {
+ const plugin = pipeline.plugins.find(plugin => plugin.id === trigger.pluginId)!;
+ return { id: plugin.id, name: plugin.name };
+ }) };
+ }
+
+ private run(pipeline: Pipeline, text: string): Promise<string> {
+ return this.runWithResult(pipeline, text).then(result => result.text);
+ }
+
+ private runWithResult(pipeline: Pipeline, text: string) {
+ let plugins = this.compiled.get(pipeline);
+ if (!plugins) {
+ plugins = pipeline.plugins.filter((plugin) => plugin.enabled).map((plugin) => {
+ try { return { id: plugin.id, code: compilePiiSource(plugin.source) }; }
+ catch { throw new PiiError("PII_INVALID_SOURCE", plugin.id); }
+ });
+ this.compiled.set(pipeline, plugins);
+ }
+ return this.runner.evaluateWithResult(plugins, text);
+ }
+}
diff --git a/src/daemon/src/server/wsControlChannel.ts b/src/daemon/src/server/wsControlChannel.ts
index bfd9e1751..ae9b879c1 100644
--- a/src/daemon/src/server/wsControlChannel.ts
+++ b/src/daemon/src/server/wsControlChannel.ts
@@ -22,7 +22,7 @@
* (reconnect/heartbeat and frame (de)serialization) end to end, rather than
* shortcut in-process.
*/
-import { BotAuditEventAckFrameSchema } from "@alook/shared";
+import { type PiiResultFrame, BotAuditEventAckFrameSchema } from "@alook/shared";
import type {
HostControlChannel,
HostCommand,
@@ -147,6 +147,7 @@ type OutboundFrame =
| { type: "diagnostics_ack"; reportId: string }
| HostBotAuditEventFrame
| HostRuntimeModelsFrame
+ | PiiResultFrame
| SessionErrorFrame;
type ResyncProvider = () => {
@@ -345,6 +346,8 @@ export class WsControlChannel implements HostControlChannel {
this.sendFrame(frame);
}
+ reportPiiResult(frame: PiiResultFrame): void { this.sendFrame(frame); }
+
reportRuntimeModels(frame: HostRuntimeModelsFrame): void {
this.sendFrame(frame);
}
diff --git a/src/shared/package.json b/src/shared/package.json
index 0c04dcf87..a981cb006 100644
--- a/src/shared/package.json
+++ b/src/shared/package.json
@@ -1,6 +1,7 @@
{
"name": "@alook/shared",
"version": "0.1.59",
+ "type": "module",
"private": true,
"exports": {
".": "./src/index.ts",
diff --git a/src/shared/src/community-cli-contract.ts b/src/shared/src/community-cli-contract.ts
index 87926873b..ced0df53d 100644
--- a/src/shared/src/community-cli-contract.ts
+++ b/src/shared/src/community-cli-contract.ts
@@ -1,3 +1,4 @@
+import { PiiCommandSchema, type PiiCommand, type PiiResultFrame } from "./pii-contract";
import type { PlatformMachineRequest, PlatformBotRequest } from "./community-platform";
/**
* Server API contract — the agent ⇄ server boundary.
@@ -407,6 +408,7 @@ export type SendResponse =
| {
state: "sent";
message: Message;
+ pii?: { triggeredFilters: { id: string; name: string }[] };
/**
* True when this send matched an existing (author, nonce) message — i.e.
* a retry of an already-committed send. `message` is the ORIGINAL (its
@@ -781,6 +783,7 @@ export type AgentInterruptRequest = z.infer<typeof AgentInterruptRequestSchema>;
export type HostCommand =
| { type: "machine:heartbeat"; nonce: string }
| RuntimeModelDiscoveryCommand
+ | PiiCommand
| AgentInterruptRequest
| {
type: "agent:wake";
@@ -1035,6 +1038,7 @@ export interface HostControlChannel {
/** Announce this host + its agents to the server (on connect AND on reconnect). */
reportReady(ready: HostReady): Promise<void>;
reportRuntimeModels?(frame: HostRuntimeModelsFrame): void;
+ reportPiiResult?(frame: PiiResultFrame): void;
/**
* On-demand resend of the current `ready` snapshot. Used by AgentRouter's
* runtime-health mutations to push an updated report without waiting for a
@@ -1493,6 +1497,7 @@ export function formatSeq(seq: Seq): string {
// a forward-compatible server field into a hard drop on an older daemon.
export const HostCommandSchema = z.discriminatedUnion("type", [
RuntimeModelDiscoveryCommandSchema,
+ PiiCommandSchema,
z.strictObject({
type: z.literal("machine:heartbeat"),
nonce: z.string().min(1).max(128),
diff --git a/src/shared/src/community-ws-events.ts b/src/shared/src/community-ws-events.ts
index b75192cf4..5e0d819d1 100644
--- a/src/shared/src/community-ws-events.ts
+++ b/src/shared/src/community-ws-events.ts
@@ -1,3 +1,4 @@
+import { PiiResultSchema } from "./pii-contract"
import { z } from "zod"
import type { CommunityContract } from "./community-contract"
import { CommunityMachineSummarySchema, RuntimeModelDiscoveryResultSchema } from "./schemas"
@@ -455,7 +456,16 @@ const communityRuntimeModelsSchema = z.strictObject({
result: RuntimeModelDiscoveryResultSchema,
})
+const communityBotPiiSchema = z.strictObject({
+ type: z.literal("community:bot.pii"),
+ machineId: z.string().min(1),
+ botId: z.string().min(1),
+ requestId: z.string().min(1).max(128),
+ result: PiiResultSchema,
+})
+
const commonCommunityEventSchemas = [
+ communityBotPiiSchema,
communityMessageUpdatedSchema,
communityMessageEditedSchema,
communityReactionAddSchema,
@@ -676,6 +686,7 @@ export const WS_EVENTS = {
MACHINE_STATUS: "community:machine.status",
MACHINE_UPDATED: "community:machine.updated",
RUNTIME_MODELS: "community:runtime.models",
+ BOT_PII: "community:bot.pii",
MACHINE_REMOVED: "community:machine.removed",
BOT_AUDIT_EVENT: "community:bot.audit_event",
} as const satisfies Record<string, CommunityWsEvent["type"]>
diff --git a/src/shared/src/db/queries/community/bot.ts b/src/shared/src/db/queries/community/bot.ts
index e7b7e21a4..7d660c2ad 100644
--- a/src/shared/src/db/queries/community/bot.ts
+++ b/src/shared/src/db/queries/community/bot.ts
@@ -889,7 +889,8 @@ export async function getBotWakeContext(db: Database, botUserId: string): Promis
*/
export async function listBotsForMachine(
db: Database,
- machineId: string
+ machineId: string,
+ options: { includeInactive?: boolean } = {}
): Promise<
Array<{
id: string;
@@ -926,7 +927,7 @@ export async function listBotsForMachine(
.where(
and(
eq(communityBotBinding.machineId, machineId),
- eq(communityBotBinding.isActive, true),
+ options.includeInactive ? undefined : eq(communityBotBinding.isActive, true),
eq(user.isBot, true),
isNull(user.deletedAt),
isNull(owner.deletedAt)
diff --git a/src/shared/src/index.ts b/src/shared/src/index.ts
index 67a2583de..f7897d951 100644
--- a/src/shared/src/index.ts
+++ b/src/shared/src/index.ts
@@ -840,3 +840,5 @@ export { readBoundedJsonRequest } from "./http-json";
export { channelReorderRequestSchema, type ChannelReorderRequest, type ChannelCategoryMove } from "./community-channel-order";
export type { PlatformMachineRequest, PlatformBotRequest } from "./community-platform";
+
+export * from "./pii-contract";
diff --git a/src/shared/src/pii-contract.ts b/src/shared/src/pii-contract.ts
new file mode 100644
index 000000000..983e04b3c
--- /dev/null
+++ b/src/shared/src/pii-contract.ts
@@ -0,0 +1,73 @@
+import { z } from "zod";
+
+export const PII_CAPABILITY = "pii-v1";
+export const PII_MAX_PLUGINS = 32;
+export const PII_SOURCE_MAX_BYTES = 16 * 1024;
+export const PII_TEXT_MAX_BYTES = 1024;
+export const PII_REQUEST_MAX_BYTES = 8 * 1024;
+export const PiiPreviewTextSchema = z.string().max(PII_TEXT_MAX_BYTES)
+ .refine(text => new TextEncoder().encode(text).byteLength <= PII_TEXT_MAX_BYTES)
+ .refine(text => text.trim().length > 0);
+export const PiiPluginSchema = z.strictObject({
+ id: z.string().min(1).max(128),
+ name: z.string().trim().min(1).max(80),
+ description: z.string().trim().min(1).max(256),
+ enabled: z.boolean(),
+});
+export const PiiSnapshotSchema = z.strictObject({
+ revision: z.number().int().nonnegative(),
+ plugins: z.array(PiiPluginSchema).max(PII_MAX_PLUGINS),
+});
+const pluginId = z.string().min(1).max(128);
+const expectedRevision = z.number().int().nonnegative();
+export const PiiOperationSchema = z.discriminatedUnion("action", [
+ z.strictObject({ action: z.literal("list") }),
+ z.strictObject({ action: z.literal("setEnabled"), pluginId, enabled: z.boolean(), expectedRevision }),
+ z.strictObject({ action: z.literal("remove"), pluginId, expectedRevision }),
+ z.strictObject({ action: z.literal("preview"), text: PiiPreviewTextSchema, expectedRevision }),
+]);
+export const PiiRequestSchema = z.strictObject({
+ requestId: z.string().min(1).max(128),
+ operation: PiiOperationSchema,
+});
+export const PiiCommandSchema = PiiRequestSchema.extend({
+ type: z.literal("pii:manage"),
+ botId: z.string().min(1).max(128),
+});
+export const PiiErrorCodeSchema = z.enum([
+ "PII_CONFLICT", "PII_UNAVAILABLE", "PII_PLUGIN_NOT_FOUND", "PII_BOT_NOT_FOUND",
+ "PII_PLUGIN_FAILED", "PII_PLUGIN_TIMEOUT", "PII_INVALID_RESULT", "PII_OUTPUT_TOO_LARGE",
+ "PII_INVALID_SOURCE", "PII_CONFIG_UNAVAILABLE", "PII_SOURCE_TOO_LARGE", "PII_INVALID_REQUEST",
+]);
+export const PiiResultSchema = z.discriminatedUnion("status", [
+ z.strictObject({ status: z.literal("complete"), snapshot: PiiSnapshotSchema, output: PiiPreviewTextSchema.optional(), triggeredFilters: z.array(PiiPluginSchema.pick({ id: true, name: true })).max(PII_MAX_PLUGINS).optional() }),
+ z.strictObject({
+ status: z.literal("error"),
+ code: PiiErrorCodeSchema,
+ pluginId: pluginId.optional(),
+ snapshot: PiiSnapshotSchema.optional(),
+ }),
+]);
+export const PiiResultFrameSchema = z.strictObject({
+ type: z.literal("pii_result"),
+ botId: z.string().min(1).max(128),
+ requestId: z.string().min(1).max(128),
+ result: PiiResultSchema,
+});
+export const PiiLocalRequestSchema = z.discriminatedUnion("action", [
+ z.strictObject({ action: z.literal("list") }),
+ z.strictObject({
+ action: z.literal("add"),
+ name: PiiPluginSchema.shape.name,
+ description: PiiPluginSchema.shape.description,
+ source: z.string().min(1).max(PII_SOURCE_MAX_BYTES),
+ }),
+]);
+export type PiiPlugin = z.infer<typeof PiiPluginSchema>;
+export type PiiSnapshot = z.infer<typeof PiiSnapshotSchema>;
+export type PiiOperation = z.infer<typeof PiiOperationSchema>;
+export type PiiRequest = z.infer<typeof PiiRequestSchema>;
+export type PiiCommand = z.infer<typeof PiiCommandSchema>;
+export type PiiResult = z.infer<typeof PiiResultSchema>;
+export type PiiResultFrame = z.infer<typeof PiiResultFrameSchema>;
+export type PiiLocalRequest = z.infer<typeof PiiLocalRequestSchema>;
diff --git a/src/shared/test/community-ws-bundle.contract.test.ts b/src/shared/test/community-ws-bundle.contract.test.ts
index a86d98cf8..ed5a2e618 100644
--- a/src/shared/test/community-ws-bundle.contract.test.ts
+++ b/src/shared/test/community-ws-bundle.contract.test.ts
@@ -145,7 +145,7 @@ describe("community WS batch transport contract", () => {
expect(isCommunityDeliveryDigest("a".repeat(63))).toBe(false)
})
- it("round-trips one strict outer frame without entering the 48-event union", async () => {
+ it("round-trips one strict outer frame without entering the 49-event union", async () => {
const operationId = await deriveCommunityDeliveryOperationId("message-1")
const prepared = await prepareCommunityDeliveryEvents(children)
expect(prepared.ok).toBe(true)
@@ -173,7 +173,7 @@ describe("community WS batch transport contract", () => {
})
expect(isCommunityBrowserEventBatchCandidate(encoded.batch)).toBe(true)
expect(Object.values(WS_EVENTS)).not.toContain(COMMUNITY_BROWSER_EVENT_BATCH_TYPE)
- expect(Object.values(WS_EVENTS)).toHaveLength(48)
+ expect(Object.values(WS_EVENTS)).toHaveLength(49)
})
it("rejects invalid count, child, operation metadata, digest mismatch, and strict outer keys", async () => {
diff --git a/src/shared/test/community-ws-events.contract.test.ts b/src/shared/test/community-ws-events.contract.test.ts
index a10ff6c69..60bd52625 100644
--- a/src/shared/test/community-ws-events.contract.test.ts
+++ b/src/shared/test/community-ws-events.contract.test.ts
@@ -65,13 +65,13 @@ describe("Community browser event runtime contract", () => {
const fixtures = Object.values(communityWsEventFixtures)
const names = Object.values(WS_EVENTS).sort()
- it("locks the exact 48-event inventory", () => {
- expect(names).toHaveLength(48)
+ it("locks the exact 49-event inventory", () => {
+ expect(names).toHaveLength(49)
expect(Object.keys(communityWsEventFixtures).sort()).toEqual(names)
expect(Object.keys(requiredFixturePaths).sort()).toEqual(names)
})
- it("decodes and canonically encodes all 48 current event fixtures", () => {
+ it("decodes and canonically encodes all 49 current event fixtures", () => {
for (const fixture of fixtures) {
expect(decodeCommunityBrowserEvent(fixture)).toEqual({ ok: true, event: fixture })
const encoded = encodeCommunityBrowserEvent(fixture)
diff --git a/src/shared/test/community-ws-events.fixtures.ts b/src/shared/test/community-ws-events.fixtures.ts
index 5660fbdfd..e7bb255c1 100644
--- a/src/shared/test/community-ws-events.fixtures.ts
+++ b/src/shared/test/community-ws-events.fixtures.ts
@@ -282,6 +282,7 @@ export const communityWsEventFixtures = {
lastSeenAt: "2026-01-01T00:00:00.000Z",
},
"community:machine.updated": { type: "community:machine.updated", machine },
+ "community:bot.pii": { type: "community:bot.pii", machineId: "machine-1", botId: "bot-1", requestId: "request-1", result: { status: "complete", snapshot: { revision: 1, plugins: [{ id: "plugin-1", name: "Email", description: "Redacts emails", enabled: true }] } } },
"community:runtime.models": { type: "community:runtime.models", machineId: "machine-1", requestId: "request-1", runtimeId: "codex", result: { status: "complete", catalog: null } },
"community:machine.removed": { type: "community:machine.removed", machineId: "machine-1" },
"community:bot.audit_event": {
@@ -343,9 +344,10 @@ export const requiredFixturePaths = {
"community:machine.status": ["lastSeenAt"],
"community:machine.updated": ["machine"],
"community:runtime.models": ["requestId"],
+ "community:bot.pii": ["requestId"],
"community:machine.removed": ["machineId"],
"community:bot.audit_event": ["payload"],
-} satisfies Record<CommunityWsEvent["type"], readonly [string, ...string[]]>
+} satisfies Record<CommunityWsEvent["type"], readonly [] | readonly [string, ...string[]]>
export const communityResourceChannelFixture = {
id: "channel-1", type: "text", serverId: "server-1", categoryId: null,
diff --git a/src/shared/test/community-ws-events.test.ts b/src/shared/test/community-ws-events.test.ts
index f29ff6bfb..24aef5086 100644
--- a/src/shared/test/community-ws-events.test.ts
+++ b/src/shared/test/community-ws-events.test.ts
@@ -64,7 +64,7 @@ describe("CommunityMessageEdited address contract", () => {
describe("isCommunityEvent", () => {
it("accepts every exact event declared by WS_EVENTS", () => {
- expect(Object.values(WS_EVENTS)).toHaveLength(48);
+ expect(Object.values(WS_EVENTS)).toHaveLength(49);
for (const type of Object.values(WS_EVENTS)) {
expect(isCommunityEvent({ type })).toBe(true);
}
diff --git a/src/shared/test/pii-contract.test.ts b/src/shared/test/pii-contract.test.ts
new file mode 100644
index 000000000..b62b83b28
--- /dev/null
+++ b/src/shared/test/pii-contract.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest"
+import { PiiCommandSchema, PiiLocalRequestSchema, PiiResultFrameSchema } from "../src/pii-contract"
+
+describe("PII preview contract", () => {
+ const command = { type: "pii:manage", botId: "bot", requestId: "request", operation: { action: "preview", text: "test input", expectedRevision: 0 } }
+ it("requires a captured revision, bounds UTF-8 and exposes no preview through the agent local API", () => {
+ expect(PiiCommandSchema.safeParse(command).success).toBe(true)
+ expect(PiiLocalRequestSchema.safeParse(command.operation).success).toBe(false)
+ for (const operation of [{ ...command.operation, text: "中".repeat(400) }, { ...command.operation, text: " " }, { action: "preview", text: "test" }, { ...command.operation, source: "private code" }]) {
+ expect(PiiCommandSchema.safeParse({ ...command, operation }).success).toBe(false)
+ }
+ })
+ it("bounds preview output and error metadata without accepting original exception fields", () => {
+ const frame = { type: "pii_result", botId: "bot", requestId: "request", result: { status: "complete", snapshot: { revision: 3, plugins: [] }, output: "filtered output" } }
+ expect(PiiResultFrameSchema.safeParse(frame).success).toBe(true)
+ for (const output of ["", " ", "中".repeat(400)]) expect(PiiResultFrameSchema.safeParse({ ...frame, result: { ...frame.result, output } }).success).toBe(false)
+ expect(PiiResultFrameSchema.safeParse({ ...frame, result: { status: "error", code: "PII_PLUGIN_FAILED", pluginId: "plugin" } }).success).toBe(true)
+ expect(PiiResultFrameSchema.safeParse({ ...frame, result: { status: "error", code: "PII_PLUGIN_FAILED", message: "original exception" } }).success).toBe(false)
+ })
+})
+
+it("preserves bounded matched-filter metadata and accepts legacy responses", () => {
+ const result = { status: "complete", snapshot: { revision: 1, plugins: [] }, output: "filtered" }
+ const frame = { type: "pii_result", botId: "bot", requestId: "req", result }
+ const triggeredFilters = [{ id: "email", name: "Email addresses" }]
+ expect(PiiResultFrameSchema.parse({ ...frame, result: { ...result, triggeredFilters } }).result).toMatchObject({ triggeredFilters })
+ expect(PiiResultFrameSchema.safeParse(frame).success).toBe(true)
+ expect(PiiResultFrameSchema.safeParse({ ...frame, result: { ...result, triggeredFilters: [] } }).success).toBe(true)
+ for (const invalid of [[{ id: "email", name: "" }], Array(33).fill(triggeredFilters[0]), [{ ...triggeredFilters[0], source: "secret" }]]) {
+ expect(PiiResultFrameSchema.safeParse({ ...frame, result: { ...result, triggeredFilters: invalid } }).success).toBe(false)
+ }
+})
diff --git a/src/shared/test/queries/community-bot.test.ts b/src/shared/test/queries/community-bot.test.ts
index c693d1023..cb9cfdb3f 100644
--- a/src/shared/test/queries/community-bot.test.ts
+++ b/src/shared/test/queries/community-bot.test.ts
@@ -430,6 +430,27 @@ describe("updateBotRuntimeConfig", () => {
})
describe("listBotsForMachine", () => {
+ it("allows local configuration of inactive bots only when explicitly requested, retaining machine/deletion guards", async () => {
+ const sqlite = new Sqlite(":memory:")
+ sqlite.exec(`
+ CREATE TABLE user (id TEXT, name TEXT, discriminator TEXT, isBot INTEGER, ownerUserId TEXT, deletedAt TEXT);
+ CREATE TABLE community_bot_binding (user_id TEXT, machine_id TEXT, runtime TEXT, model_name TEXT, reasoning_effort TEXT, runtime_config_revision INTEGER, instruction TEXT, is_active INTEGER);
+ INSERT INTO user VALUES ('owner', 'Owner', '0001', 0, NULL, NULL),
+ ('active', 'Active', '0002', 1, 'owner', NULL), ('inactive', 'Inactive', '0003', 1, 'owner', NULL),
+ ('foreign', 'Foreign', '0004', 1, 'owner', NULL), ('deleted', 'Deleted', '0005', 1, 'owner', 'deleted');
+ INSERT INTO community_bot_binding VALUES ('active', 'machine', 'codex', NULL, NULL, 0, '', 1),
+ ('inactive', 'machine', 'codex', NULL, NULL, 0, '', 0), ('foreign', 'other-machine', 'codex', NULL, NULL, 0, '', 0),
+ ('deleted', 'machine', 'codex', NULL, NULL, 0, '', 0);
+ `)
+ const db = drizzleSqlite(sqlite) as unknown as Parameters<typeof q.listBotsForMachine>[0]
+ try {
+ expect((await q.listBotsForMachine(db, "machine")).map(bot => bot.id)).toEqual(["active"])
+ expect((await q.listBotsForMachine(db, "machine", { includeInactive: true })).map(bot => bot.id)).toEqual(["active", "inactive"])
+ sqlite.prepare("UPDATE user SET deletedAt = 'deleted' WHERE id = 'owner'").run()
+ expect(await q.listBotsForMachine(db, "machine", { includeInactive: true })).toEqual([])
+ } finally { sqlite.close() }
+ })
+
function makeJoinChain(rows: unknown[]) {
const chain: any = {}
chain.select = vi.fn(() => chain)
diff --git a/src/web/src/app/api/community/bots/[id]/pii/route.test.ts b/src/web/src/app/api/community/bots/[id]/pii/route.test.ts
new file mode 100644
index 000000000..8db1c9c65
--- /dev/null
+++ b/src/web/src/app/api/community/bots/[id]/pii/route.test.ts
@@ -0,0 +1,100 @@
+import { beforeEach, describe, expect, it, vi } from "vitest"
+import { NextRequest } from "next/server"
+const mocks = vi.hoisted(() => ({ session: vi.fn(), liveUser: vi.fn(), bot: vi.fn(), machine: vi.fn(), dispatch: vi.fn() }))
+vi.mock("@opennextjs/cloudflare", () => ({ getCloudflareContext: vi.fn(async () => ({ env: { DB: {} }, ctx: { waitUntil: vi.fn() } })) }))
+vi.mock("@/lib/db", () => ({ getPrimaryDb: vi.fn(() => ({})), getDb: vi.fn(() => ({})) }))
+vi.mock("@/lib/auth", () => ({ getAuth: () => ({ api: { getSession: mocks.session, signOut: vi.fn() } }) }))
+vi.mock("@/lib/broadcast", () => ({ wsDoFetch: mocks.dispatch }))
+vi.mock("@alook/shared", async (original) => ({
+ ...await original<typeof import("@alook/shared")>(),
+ queries: {
+ user: { getUserInternal: mocks.liveUser },
+ communityBot: { getBotOwnedBy: mocks.bot },
+ communityMachine: { getMachineByIdForUser: mocks.machine },
+ },
+}))
+import { POST } from "./route"
+const body = { requestId: "request", operation: { action: "list" } }
+const request = (value: unknown = body, headers: Record<string, string> = {}) => new NextRequest("https://alook.ai/api/community/bots/bot/pii", {
+ method: "POST", headers: { Origin: "https://alook.ai", "content-type": "application/json", ...headers }, body: JSON.stringify(value),
+})
+const context = { params: Promise.resolve({ id: "bot" }) }
+describe("human-only PII management", () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ mocks.session.mockResolvedValue({ headers: new Headers(), response: { user: { id: "owner", email: "[email protected]", isBot: false, deletedAt: null } } })
+ mocks.liveUser.mockResolvedValue({ id: "owner", email: "[email protected]", isBot: false, deletedAt: null })
+ mocks.bot.mockResolvedValue({ machineId: "machine" })
+ mocks.machine.mockResolvedValue({ id: "machine", status: "online" })
+ mocks.dispatch.mockImplementation(async () => Response.json({ sent: 1, unsupported: 0 }))
+ })
+ it.each(["cmk_test", "crk_test", "vch_test", "al_test"])("rejects %s even alongside a valid human cookie before lookup or dispatch", async token => {
+ expect((await POST(request(body, { Authorization: `Bearer ${token}` }), context)).status).toBe(401)
+ expect(mocks.session).not.toHaveBeenCalled()
+ expect(mocks.bot).not.toHaveBeenCalled()
+ expect(mocks.dispatch).not.toHaveBeenCalled()
+ })
+ it("rejects cross-origin and non-human sessions", async () => {
+ expect((await POST(request(body, { Origin: "https://other.example" }), context)).status).toBe(403)
+ mocks.session.mockResolvedValue({ headers: new Headers(), response: { user: { id: "bot", isBot: true } } })
+ expect((await POST(request(), context)).status).toBe(401)
+ expect(mocks.dispatch).not.toHaveBeenCalled()
+ })
+ it("scopes ownership before machine access and rejects offline targets", async () => {
+ mocks.bot.mockResolvedValue(null)
+ expect((await POST(request(), context)).status).toBe(404)
+ expect(mocks.bot).toHaveBeenCalledWith({}, "bot", "owner")
+ expect(mocks.machine).not.toHaveBeenCalled()
+ mocks.bot.mockResolvedValue({ machineId: "machine" })
+ mocks.machine.mockResolvedValue({ id: "machine", status: "offline" })
+ expect((await POST(request(), context)).status).toBe(409)
+ expect(mocks.dispatch).not.toHaveBeenCalled()
+ })
+ it("validates operations and revision before dispatch", async () => {
+ for (const operation of [{ action: "add", source: "private source" }, { action: "remove", pluginId: "p" }, { action: "list", source: "private source" }]) {
+ expect((await POST(request({ requestId: "r", operation }), context)).status).toBe(400)
+ }
+ expect(mocks.dispatch).not.toHaveBeenCalled()
+ })
+ it("returns only a dispatch receipt and forwards the exact owner-selected operation", async () => {
+ const value = { requestId: "r", operation: { action: "setEnabled", pluginId: "p", enabled: false, expectedRevision: 2 } }
+ const response = await POST(request(value), context)
+ expect(response.status).toBe(202)
+ expect(response.headers.get("Cache-Control")).toBe("no-store")
+ expect(await response.json()).toEqual({ dispatched: true, requestId: "r" })
+ expect(mocks.dispatch.mock.calls[0][1]).toBe("/community-machine/by-id/machine/pii")
+ expect(JSON.parse(mocks.dispatch.mock.calls[0][2].body)).toEqual({ type: "pii:manage", botId: "bot", ...value })
+ })
+ it("relays preview transiently, bounds UTF-8 input and never returns its plaintext in the HTTP receipt", async () => {
+ const preview = { requestId: "preview", operation: { action: "preview", text: "private-test-input", expectedRevision: 0 } }
+ const response = await POST(request(preview), context)
+ expect(response.status).toBe(202)
+ expect(response.headers.get("Cache-Control")).toBe("no-store")
+ expect(await response.json()).toEqual({ dispatched: true, requestId: "preview" })
+ expect(JSON.parse(mocks.dispatch.mock.calls[0][2].body)).toEqual({ type: "pii:manage", botId: "bot", ...preview })
+ for (const operation of [{ ...preview.operation, text: "中".repeat(400) }, { ...preview.operation, text: " " }, { action: "preview", text: "text" }]) {
+ expect((await POST(request({ requestId: "invalid", operation }), context)).status).toBe(400)
+ }
+ const escaped = await POST(request({ requestId: "escaped", operation: { ...preview.operation, text: "\u0000".repeat(1024) } }), context)
+ expect(escaped.status).toBe(202)
+ expect(mocks.dispatch).toHaveBeenCalledTimes(2)
+ })
+ it("does not claim dispatch or expose private data when the machine relay fails", async () => {
+ for (const response of [new Response("PRIVATE_RESPONSE", { status: 503 }), new Response("PRIVATE_RESPONSE", { status: 200 })]) {
+ mocks.dispatch.mockResolvedValueOnce(response)
+ const result = await POST(request(), context)
+ expect(result.status).toBe(503)
+ expect(await result.json()).toEqual({ error: "PII_UNAVAILABLE" })
+ }
+ mocks.dispatch.mockRejectedValueOnce(new Error("PRIVATE_EXCEPTION"))
+ const result = await POST(request(), context)
+ expect(result.status).toBe(503)
+ expect(await result.json()).toEqual({ error: "PII_UNAVAILABLE" })
+ })
+ it.each([[{ sent: 0, unsupported: 1 }, 409, "UPGRADE_REQUIRED"], [{ sent: 0, unsupported: 0 }, 409, "MACHINE_OFFLINE"], [{ sent: "1" }, 503, "PII_UNAVAILABLE"]])("reports %j without claiming application", async (receipt, status, error) => {
+ mocks.dispatch.mockResolvedValue(Response.json(receipt))
+ const response = await POST(request(), context)
+ expect(response.status).toBe(status)
+ expect(await response.json()).toEqual({ error })
+ })
+})
diff --git a/src/web/src/app/api/community/bots/[id]/pii/route.ts b/src/web/src/app/api/community/bots/[id]/pii/route.ts
new file mode 100644
index 000000000..897f905db
--- /dev/null
+++ b/src/web/src/app/api/community/bots/[id]/pii/route.ts
@@ -0,0 +1,30 @@
+import { PII_REQUEST_MAX_BYTES, PiiRequestSchema, queries, readBoundedJsonRequest } from "@alook/shared"
+import { getPrimaryDb } from "@/lib/db"
+import { withCookieHumanAuth } from "@/lib/middleware/auth"
+import { writeError, writeJSON } from "@/lib/middleware/helpers"
+import { wsDoFetch } from "@/lib/broadcast"
+
+export const POST = withCookieHumanAuth(async (request, context) => {
+ const input = await readBoundedJsonRequest(request, PII_REQUEST_MAX_BYTES)
+ const payload = PiiRequestSchema.safeParse(input.ok ? input.value : null)
+ if (!payload.success) return writeError("INVALID_PII_REQUEST", 400)
+ const db = getPrimaryDb(context.env.DB)
+ const botId = context.params?.id ?? ""
+ const bot = await queries.communityBot.getBotOwnedBy(db, botId, context.userId)
+ if (!bot?.machineId) return writeError("BOT_NOT_FOUND", 404)
+ const machine = await queries.communityMachine.getMachineByIdForUser(db, context.userId, bot.machineId)
+ if (!machine || machine.status !== "online") return writeError("MACHINE_OFFLINE", 409)
+ try {
+ const response = await wsDoFetch(context.env, `/community-machine/by-id/${encodeURIComponent(machine.id)}/pii`, {
+ method: "POST", headers: { "content-type": "application/json" },
+ body: JSON.stringify({ type: "pii:manage", botId, ...payload.data }),
+ })
+ if (!response.ok) return writeError("PII_UNAVAILABLE", 503)
+ const receipt = await response.json() as { sent?: unknown; unsupported?: unknown }
+ if (typeof receipt.sent !== "number" || !Number.isSafeInteger(receipt.sent) || receipt.sent < 0) return writeError("PII_UNAVAILABLE", 503)
+ if (receipt.sent === 0) return writeError(typeof receipt.unsupported === "number" && receipt.unsupported > 0 ? "UPGRADE_REQUIRED" : "MACHINE_OFFLINE", 409)
+ const result = writeJSON({ dispatched: true, requestId: payload.data.requestId }, 202)
+ result.headers.set("Cache-Control", "no-store")
+ return result
+ } catch { return writeError("PII_UNAVAILABLE", 503) }
+})
diff --git a/src/web/src/app/api/community/daemon/bots/route.test.ts b/src/web/src/app/api/community/daemon/bots/route.test.ts
new file mode 100644
index 000000000..05acd1f97
--- /dev/null
+++ b/src/web/src/app/api/community/daemon/bots/route.test.ts
@@ -0,0 +1,15 @@
+import { describe, expect, it, vi } from "vitest"
+import { NextRequest } from "next/server"
+const mocks = vi.hoisted(() => ({ list: vi.fn(async () => []) }))
+vi.mock("@/lib/db", () => ({ getDb: () => "normal-db", getPrimaryDb: () => "primary-db" }))
+vi.mock("@alook/shared", () => ({ queries: { communityBot: { listBotsForMachine: mocks.list } } }))
+vi.mock("@/lib/middleware/community-daemon-auth", () => ({ withCommunityDaemonAuth: (handler: (req: NextRequest, ctx: unknown) => Promise<Response>) => (req: NextRequest) => handler(req, { env: { DB: {} }, machineId: "bound-machine" }) }))
+import { GET } from "./route"
+describe("daemon bound bot inventory", () => {
+ it("keeps normal warmup active-only and scopes both modes to the authenticated machine", async () => {
+ await GET(new NextRequest("https://alook.ai/api/community/daemon/bots"))
+ expect(mocks.list).toHaveBeenLastCalledWith("normal-db", "bound-machine", { includeInactive: false })
+ await GET(new NextRequest("https://alook.ai/api/community/daemon/bots?includeInactive=true"))
+ expect(mocks.list).toHaveBeenLastCalledWith("primary-db", "bound-machine", { includeInactive: true })
+ })
+})
diff --git a/src/web/src/app/api/community/daemon/bots/route.ts b/src/web/src/app/api/community/daemon/bots/route.ts
index 973b25cfe..a2ba2eda4 100644
--- a/src/web/src/app/api/community/daemon/bots/route.ts
+++ b/src/web/src/app/api/community/daemon/bots/route.ts
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server"
import { queries } from "@alook/shared"
-import { getDb } from "@/lib/db"
+import { getDb, getPrimaryDb } from "@/lib/db"
import { withCommunityDaemonAuth } from "@/lib/middleware/community-daemon-auth"
/**
@@ -14,8 +14,9 @@ import { withCommunityDaemonAuth } from "@/lib/middleware/community-daemon-auth"
* into the bot's global handle; `ownerName`+`ownerDiscriminator` into the
* owner's). No avatar (display-only, server does that).
*/
-export const GET = withCommunityDaemonAuth(async (_req, ctx) => {
- const db = getDb(ctx.env.DB)
- const bots = await queries.communityBot.listBotsForMachine(db, ctx.machineId)
+export const GET = withCommunityDaemonAuth(async (req, ctx) => {
+ const includeInactive = req.nextUrl.searchParams.get("includeInactive") === "true"
+ const db = includeInactive ? getPrimaryDb(ctx.env.DB) : getDb(ctx.env.DB)
+ const bots = await queries.communityBot.listBotsForMachine(db, ctx.machineId, { includeInactive })
return NextResponse.json({ bots })
})
diff --git a/src/web/src/components/community/bots/bot-list-controller.dom.test.ts b/src/web/src/components/community/bots/bot-list-controller.dom.test.ts
index c088c78aa..f1fc0b8d5 100644
--- a/src/web/src/components/community/bots/bot-list-controller.dom.test.ts
+++ b/src/web/src/components/community/bots/bot-list-controller.dom.test.ts
@@ -293,7 +293,7 @@ describe("useBotListController", () => {
"setActive",
"dm",
])
- expect(mocks.hookOrder[10]).toBe("onboarding")
+ expect(mocks.hookOrder.slice(10, 13)).toEqual(["dm", "router", "onboarding"])
const source = readWebSource("src/components/community/bots/bot-list-controller.ts")
expect(source).not.toMatch(/useState\(/)
diff --git a/src/web/src/components/community/bots/bot-list-controller.ts b/src/web/src/components/community/bots/bot-list-controller.ts
index 91f5c1347..0949d6e3d 100644
--- a/src/web/src/components/community/bots/bot-list-controller.ts
+++ b/src/web/src/components/community/bots/bot-list-controller.ts
@@ -5,11 +5,9 @@ import { useCommunityRuntime } from "@/stores/community/runtime"
import { createStore, useSelector, useAtom, useCreateAtom } from "@tanstack/react-store";
import { useMutationState, useQueryClient } from "@tanstack/react-query"
import { communityKeys } from "@/lib/query-keys"
-import { useCurrentUser } from "@/contexts/community/current-user"
-import { useDmMessageSender } from "@/hooks/community/use-dm-message-sender"
+import { useStartBotChat } from "@/hooks/community/use-start-bot-chat"
import { useCommunityViewSource } from "@/hooks/community/use-community-view-source"
-import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef } from "react"
-import { CommunityRouteContext } from "@/contexts/community/route-frame"
+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react"
import { useCommunityModuleLocation } from "@/contexts/community/module-route"
import { isPresenceOnline } from "@alook/shared"
import { toast } from "sonner"
@@ -46,7 +44,6 @@ export function useBotListController(): BotListController {
const source = useCommunityViewSource("bot-list")
const uiHandlers = useUiHandlers()
const { router, searchParams, routeCommitted } = useCommunityModuleLocation()
- const navigation = useContext(CommunityRouteContext)?.navigation
const botsQuery = useBots()
const { bots } = botsQuery
const isLoading = botsQuery.isPending || botsQuery.profilesPending === true
@@ -98,10 +95,7 @@ export function useBotListController(): BotListController {
const resetMachineAgents = useResetMachineAgents()
const setActive = useSetBotActive()
const createOrGetDm = useCreateOrGetDm()
- const currentUser = useCurrentUser()
- const { accept: acceptDmMessage } = useDmMessageSender()
- const [startingBotChat, setStartingBotChat] = useAtom(useCreateAtom(false))
- const botChatInFlight = useRef(false)
+ const { start: startBotChat, pending: startingBotChat } = useStartBotChat(source)
const onboardingState = useCommunityOnboarding()
const guidedActive = onboardingState?.status === "active" && onboardingState.stage === "bot"
const guidedPendingBotId = guidedActive ? onboardingState.botId : undefined
@@ -142,33 +136,12 @@ export function useBotListController(): BotListController {
const askBotToCreate = async () => {
const bot = bots.find((item) => item.isActive && profilesByUserId.get(item.id)?.presence === "online") ?? bots[0]
- if (!bot || botChatInFlight.current) return
- const assert = source.capture()
- botChatInFlight.current = true
- setStartingBotChat(true)
- try {
- assert()
- uiHandlers.cancelPendingNavigation?.()
- const intentCurrent = navigation?.captureIntent() ?? (() => true)
- const data = await createOrGetDm.mutateAsync({ userId: bot.id, assertActive: assert })
- assert()
- if (!intentCurrent()) throw new DOMException("Retired bot chat navigation", "AbortError")
- const receipt = acceptDmMessage({
- dmId: data.conversation.id,
- content: "Help me create a bot. Ask me what I need.",
- author: { id: currentUser.id, name: currentUser.name, avatar: currentUser.avatar },
- assertActive: assert,
- })
- if (!receipt.accepted) throw new Error("Failed to send message")
- setCreateOpen(false)
- router.push(`/c/me/${data.conversation.id}`)
- await receipt.committed
- } catch (error) {
- toastApiError(error, "Failed to start bot creation chat", assert)
- } finally {
- botChatInFlight.current = false
- setStartingBotChat(false)
- }
+ if (bot) await startBotChat({
+ botId: bot.id,
+ content: "Help me create a bot. Ask me what I need.",
+ onAccepted: () => setCreateOpen(false),
+ errorMessage: "Failed to start bot creation chat",
+ })
}
const openGuidedBotDm = async (botId: string) => {
diff --git a/src/web/src/components/community/bots/bot-pii-chat-action.dom.test.tsx b/src/web/src/components/community/bots/bot-pii-chat-action.dom.test.tsx
new file mode 100644
index 000000000..857810f6a
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-chat-action.dom.test.tsx
@@ -0,0 +1,51 @@
+import { beforeEach, expect, it, vi } from "vitest"
+import { fireEvent, render, screen, waitFor } from "@/test/react-dom-harness"
+import { BotPiiChatAction } from "./bot-pii-chat-action"
+
+const mocks = vi.hoisted(() => ({ create: vi.fn(), accept: vi.fn(), push: vi.fn(), toast: vi.fn(), assert: vi.fn(), cancel: vi.fn() }))
+vi.mock("@/contexts/community/current-user", () => ({ useCurrentUser: () => ({ id: "owner", name: "Owner", avatar: null }) }))
+vi.mock("@/contexts/community/module-route", () => ({ useCommunityRouter: () => ({ push: mocks.push }) }))
+vi.mock("@/hooks/community/mutations", () => ({ useCreateOrGetDm: () => ({ mutateAsync: mocks.create }) }))
+vi.mock("@/hooks/community/use-dm-message-sender", () => ({ useDmMessageSender: () => ({ accept: mocks.accept }) }))
+vi.mock("@/hooks/community/use-community-view-source", () => ({ useCommunityViewSource: () => ({ capture: () => mocks.assert }) }))
+vi.mock("@/stores/community", () => ({ useUiHandlers: () => ({ cancelPendingNavigation: mocks.cancel }) }))
+vi.mock("@/lib/api/client", () => ({ toastApiError: mocks.toast }))
+beforeEach(() => {
+ vi.resetAllMocks()
+ mocks.create.mockResolvedValue({ conversation: { id: "dm-selected" } })
+ mocks.accept.mockReturnValue({ accepted: true, committed: Promise.resolve({ ok: true }) })
+})
+const click = () => fireEvent.click(screen.getByRole("button", { name: "Ask your bot to add a PII filter." }))
+it("sends the requirements-first request to the selected bot before navigating", async () => {
+ render(<BotPiiChatAction botId="selected-bot" />)
+ click()
+ await waitFor(() => expect(mocks.push).toHaveBeenCalledWith("/c/me/dm-selected"))
+ expect(mocks.create).toHaveBeenCalledWith({ userId: "selected-bot", assertActive: mocks.assert })
+ expect(mocks.accept).toHaveBeenCalledWith(expect.objectContaining({ dmId: "dm-selected", content: "Help me create and add a PII filter for yourself. First ask me what information I want you to filter.", author: expect.objectContaining({ id: "owner" }) }))
+ expect(mocks.accept.mock.invocationCallOrder[0]).toBeLessThan(mocks.push.mock.invocationCallOrder[0])
+})
+it("prevents a second message while starting the chat", async () => {
+ let resolve!: (value: unknown) => void
+ mocks.create.mockReturnValue(new Promise(done => { resolve = done }))
+ render(<BotPiiChatAction botId="selected-bot" />)
+ click(); click()
+ expect(mocks.create).toHaveBeenCalledOnce()
+ resolve({ conversation: { id: "dm-selected" } })
+ await waitFor(() => expect(mocks.accept).toHaveBeenCalledOnce())
+})
+it("does not navigate if message acceptance fails and allows retry", async () => {
+ mocks.accept.mockReturnValue({ accepted: false })
+ render(<BotPiiChatAction botId="selected-bot" />)
+ click()
+ await waitFor(() => expect(mocks.toast).toHaveBeenCalled())
+ expect(mocks.push).not.toHaveBeenCalled()
+ expect(screen.getByRole("button")).not.toBeDisabled()
+})
+it("does not send or navigate after the originating view retires", async () => {
+ mocks.assert.mockImplementationOnce(() => {}).mockImplementation(() => { throw new DOMException("Retired", "AbortError") })
+ render(<BotPiiChatAction botId="selected-bot" />)
+ click()
+ await waitFor(() => expect(mocks.toast).toHaveBeenCalled())
+ expect(mocks.accept).not.toHaveBeenCalled()
+ expect(mocks.push).not.toHaveBeenCalled()
+})
diff --git a/src/web/src/components/community/bots/bot-pii-chat-action.tsx b/src/web/src/components/community/bots/bot-pii-chat-action.tsx
new file mode 100644
index 000000000..626aa5625
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-chat-action.tsx
@@ -0,0 +1,11 @@
+"use client"
+
+import { useCommunityViewSource } from "@/hooks/community/use-community-view-source"
+import { useStartBotChat } from "@/hooks/community/use-start-bot-chat"
+
+export function BotPiiChatAction({ botId }: { botId: string }) {
+ const source = useCommunityViewSource(`pii-chat:${botId}`)
+ const { start, pending } = useStartBotChat(source)
+
+ return <button type="button" onClick={() => void start({ botId, content: "Help me create and add a PII filter for yourself. First ask me what information I want you to filter.", errorMessage: "Couldn’t start the filter chat. Try again." })} disabled={pending} aria-busy={pending} data-testid="pii-ask-bot" className="min-h-11 w-fit max-w-full rounded-sm text-left text-sm text-muted-foreground underline underline-offset-4 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-wait disabled:opacity-50">Ask your bot to add a PII filter.</button>
+}
diff --git a/src/web/src/components/community/bots/bot-pii-list.dom.test.tsx b/src/web/src/components/community/bots/bot-pii-list.dom.test.tsx
new file mode 100644
index 000000000..7011456df
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-list.dom.test.tsx
@@ -0,0 +1,50 @@
+import { describe, expect, it, vi } from "vitest"
+import { fireEvent, render, screen, waitFor } from "@/test/react-dom-harness"
+import { BotPiiList } from "./bot-pii-list"
+
+const plugins = [
+ { id: "secrets", name: "Secrets", description: "Masks access tokens", enabled: true },
+ { id: "email", name: "Email addresses", description: "Masks email addresses", enabled: false },
+]
+const props = () => ({ botName: "Alli", plugins, pending: false, onSetEnabled: vi.fn(), onDelete: vi.fn().mockResolvedValue(true) })
+
+describe("PII plugin controls", () => {
+ it("preserves pipeline order and waits for confirmed state after toggling", () => {
+ const input = props()
+ render(<BotPiiList {...input} />)
+ expect(screen.getAllByRole("listitem").map(row => row.getAttribute("data-testid"))).toEqual(["pii-plugin-secrets", "pii-plugin-email"])
+ fireEvent.click(screen.getByRole("switch", { name: "Secrets filter" }))
+ expect(input.onSetEnabled).toHaveBeenCalledWith("secrets", false)
+ expect(screen.getByRole("switch", { name: "Secrets filter" })).toBeChecked()
+ })
+
+ it.each([{ pending: true }, { unavailable: "Machine is offline. Reconnect to manage filters." }])("blocks writes when unavailable: %j", overrides => {
+ const input = { ...props(), ...overrides }
+ render(<BotPiiList {...input} />)
+ expect(screen.getByRole("switch", { name: "Secrets filter" })).toHaveAttribute("aria-disabled", "true")
+ fireEvent.click(screen.getByRole("switch", { name: "Secrets filter" }))
+ expect(screen.getByRole("button", { name: "Actions for Secrets" })).toBeDisabled()
+ expect(input.onSetEnabled).not.toHaveBeenCalled()
+ })
+
+ it("requires explicit delete confirmation and keeps failures open", async () => {
+ const input = props()
+ input.onDelete.mockResolvedValue(false)
+ const view = render(<BotPiiList {...input} />)
+ fireEvent.click(screen.getByRole("button", { name: "Actions for Secrets" }))
+ fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }))
+ expect(input.onDelete).not.toHaveBeenCalled()
+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }))
+ expect(input.onDelete).not.toHaveBeenCalled()
+ await waitFor(() => expect(screen.queryByRole("alertdialog")).toBeNull())
+ fireEvent.click(screen.getByRole("button", { name: "Actions for Secrets" }))
+ fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }))
+ fireEvent.click(screen.getByRole("button", { name: "Delete filter" }))
+ await waitFor(() => expect(input.onDelete).toHaveBeenCalledWith("secrets"))
+ view.rerender(<BotPiiList {...input} error="Could not confirm the change. Reload filters." />)
+ expect(screen.getByRole("alertdialog")).toBeVisible()
+ input.onDelete.mockResolvedValue(true)
+ fireEvent.click(screen.getByRole("button", { name: "Delete filter" }))
+ await waitFor(() => expect(screen.queryByRole("alertdialog")).toBeNull())
+ })
+})
diff --git a/src/web/src/components/community/bots/bot-pii-list.tsx b/src/web/src/components/community/bots/bot-pii-list.tsx
new file mode 100644
index 000000000..412a40f57
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-list.tsx
@@ -0,0 +1,91 @@
+"use client"
+
+import type { PiiPlugin } from "@alook/shared"
+import { useAtom, useCreateAtom } from "@tanstack/react-store"
+import { MoreHorizontal, Trash2 } from "lucide-react"
+import { Button } from "@/components/ui/button"
+import { Switch } from "@/components/ui/switch"
+import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
+import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog"
+
+
+export function BotPiiList({
+ botName,
+ plugins,
+ pending,
+ unavailable,
+ error,
+ onSetEnabled,
+ onDelete,
+}: {
+ botName: string
+ plugins: readonly PiiPlugin[]
+ pending: boolean
+ unavailable?: string
+ error?: string
+ onSetEnabled: (id: string, enabled: boolean) => void
+ onDelete: (id: string) => Promise<boolean>
+}) {
+ const [deletingId, setDeletingId] = useAtom(useCreateAtom<string | null>(null))
+ const deleting = plugins.find((plugin) => plugin.id === deletingId)
+ const disabled = pending || Boolean(unavailable)
+
+ return (
+ <div className="flex flex-col gap-4" data-testid="pii-pipeline" aria-busy={pending}>
+ {unavailable ? <p role="status" className="text-sm text-muted-foreground">{unavailable}</p> : null}
+ {error ? <p role="alert" className="text-sm text-destructive">{error}</p> : null}
+ {plugins.length === 0 ? (
+ <div className="py-8">
+ <h4 className="font-medium">No filters</h4>
+ <p className="mt-1 max-w-prose text-sm text-muted-foreground">{botName} sends message text without PII filtering. Ask your bot to add a filter with Alook PII.</p>
+ </div>
+ ) : (
+ <>
+ <ul className="flex flex-col gap-4">
+ {plugins.map((plugin) => (
+ <li key={plugin.id} className="group/pii-node relative flex flex-col gap-1 pl-6" data-testid={`pii-plugin-${plugin.id}`}>
+ <span aria-hidden="true" className="absolute -bottom-10 left-1 top-6 w-px bg-border group-last/pii-node:hidden" />
+ <span aria-hidden="true" className={`absolute left-0 top-5 size-2 rounded-full border ${plugin.enabled ? "border-muted-foreground bg-muted-foreground" : "border-muted-foreground bg-background"}`} />
+ <div className="flex min-w-0 items-center gap-2">
+ <div className="min-w-0 flex-1">
+ <h4 className="wrap-break-word text-base font-medium">{plugin.name}</h4>
+ </div>
+ <div className="flex shrink-0 items-center gap-1">
+ <div className="flex size-11 items-center justify-center">
+ <Switch className="after:-inset-x-1.5 after:-inset-y-3.5" checked={plugin.enabled} disabled={disabled} onCheckedChange={(enabled) => onSetEnabled(plugin.id, enabled)} aria-label={`${plugin.name} filter`} aria-describedby={`pii-description-${plugin.id}`} data-testid={`pii-plugin-${plugin.id}-switch`} />
+ </div>
+ <DropdownMenu>
+ <DropdownMenuTrigger render={<Button variant="ghost" size="icon" className="size-11" disabled={disabled} aria-label={`Actions for ${plugin.name}`} data-testid={`pii-plugin-${plugin.id}-menu`} />}>
+ <MoreHorizontal className="size-4" />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem variant="destructive" className="whitespace-nowrap" onClick={() => setDeletingId(plugin.id)}><Trash2 className="size-4" />Delete</DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ </div>
+ <p id={`pii-description-${plugin.id}`} className="max-w-prose wrap-break-word text-sm leading-relaxed text-muted-foreground">{plugin.description}</p>
+ </li>
+ ))}
+ </ul>
+ </>
+ )}
+ <AlertDialog open={Boolean(deleting)} onOpenChange={(open) => { if (!open && !pending) setDeletingId(null) }}>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Delete {deleting?.name}?</AlertDialogTitle>
+ <AlertDialogDescription>This removes the filter from {botName}’s filters on this machine. Disable it instead if you may need it again.</AlertDialogDescription>
+ </AlertDialogHeader>
+ {error ? <p role="alert" className="text-sm text-destructive">{error}</p> : null}
+ {unavailable ? <p role="status" className="text-sm text-muted-foreground">{unavailable}</p> : null}
+ <AlertDialogFooter>
+ <AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel>
+ <AlertDialogAction disabled={disabled} variant="outline" className="border-destructive text-foreground hover:text-foreground" onClick={async () => { if (deleting && await onDelete(deleting.id)) setDeletingId(null) }}>
+ {pending ? "Deleting…" : "Delete filter"}
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ </div>
+ )
+}
diff --git a/src/web/src/components/community/bots/bot-pii-panel.dom.test.tsx b/src/web/src/components/community/bots/bot-pii-panel.dom.test.tsx
new file mode 100644
index 000000000..b0405a9d8
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-panel.dom.test.tsx
@@ -0,0 +1,26 @@
+import { expect, it, vi } from "vitest"
+import { render, screen, within, setupUser } from "@/test/react-dom-harness"
+import { BotPiiPanel } from "./bot-pii-panel"
+
+vi.mock("@/components/avatar", () => ({ AgentAvatar: ({ seed, avatarUrl }: { seed: string; avatarUrl?: string }) => <span data-testid="bot-avatar" data-seed={seed} data-image={avatarUrl} /> }))
+
+it("pairs the selected bot name with its own avatar", () => {
+ render(<BotPiiPanel bots={[{ id: "bot-1", name: "Alli", image: "/alli.png" }]} selectedId="bot-1" onSelect={vi.fn()}><p>Filters</p></BotPiiPanel>)
+ expect(screen.getByTestId("pii-bot-select")).toHaveTextContent("Alli")
+ expect(screen.getByTestId("bot-avatar")).toHaveAttribute("data-seed", "bot-1")
+ expect(screen.getByTestId("bot-avatar")).toHaveAttribute("data-image", "/alli.png")
+})
+
+it("scopes the bot selector and its change action to the PII Filters tab", async () => {
+ const user = setupUser()
+ const onSelect = vi.fn()
+ const view = render(<BotPiiPanel bots={[{ id: "a", name: "First" }, { id: "b", name: "Second" }]} selectedId="a" onSelect={onSelect}><p>Pipeline content</p></BotPiiPanel>)
+ const panel = screen.getByRole("tabpanel", { name: "PII Filters" })
+ expect(within(panel).getByText("Filter sensitive information from messages your bot sends to Alook. Messages people send to your bot are not filtered. All filtering runs on your computer.")).toBeVisible()
+ const selector = within(panel).getByTestId("pii-bot-select")
+ expect(view.container.querySelector("header")).not.toContainElement(selector)
+ expect(within(panel).getByText("Pipeline content")).toBeVisible()
+ await user.click(selector)
+ await user.click(screen.getByRole("option", { name: "Second" }))
+ expect(onSelect).toHaveBeenCalledWith("b")
+})
diff --git a/src/web/src/components/community/bots/bot-pii-panel.tsx b/src/web/src/components/community/bots/bot-pii-panel.tsx
new file mode 100644
index 000000000..883b3c631
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-panel.tsx
@@ -0,0 +1,50 @@
+"use client"
+
+import type { ReactNode } from "react"
+import { AgentAvatar } from "@/components/avatar"
+import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs"
+import { Select, SelectTrigger, SelectValue } from "@/components/ui/select"
+import { BotSelectMenuContent, BotSelectMenuItem } from "./bot-select-menu"
+
+export function BotPiiPanel({ bots, selectedId, onSelect, children }: {
+ bots: readonly { id: string; name: string; image?: string | null }[]
+ selectedId: string
+ onSelect: (id: string) => void
+ children: ReactNode
+}) {
+ const selected = bots.find(bot => bot.id === selectedId)
+ return (
+ <section className="@container flex min-h-0 flex-1 flex-col gap-6 overflow-y-auto p-6 thin-scrollbar" aria-labelledby="pii-heading">
+ <header className="flex flex-col items-start gap-2">
+ <h1 id="pii-heading" className="text-xl font-medium text-foreground">Privacy</h1>
+ </header>
+ <Tabs defaultValue="filters" className="gap-6">
+ <TabsList variant="line" aria-label="Privacy tools" className="h-auto min-h-12 w-full border-b p-0 thin-scrollbar">
+ <TabsTrigger value="filters" className="min-h-12 px-4 text-base group-data-horizontal/tabs:after:bottom-0">PII Filters</TabsTrigger>
+ </TabsList>
+ <TabsContent value="filters" className="mx-auto flex w-full max-w-2xl flex-col gap-8">
+ <div className="flex flex-col gap-2">
+ <h2 className="text-lg font-medium">Filters for bot message</h2>
+ <p className="text-sm leading-relaxed text-muted-foreground">Filter sensitive information from messages your bot sends to Alook. Messages people send to your bot are not filtered. All filtering runs on your computer.</p>
+ </div>
+ {bots.length ? <section className="flex flex-col gap-4" aria-labelledby="pii-bot-section-heading">
+ <h3 id="pii-bot-section-heading" className="text-base font-medium">Which bot to set up</h3>
+ <div className="w-full sm:max-w-xs">
+ <label id="pii-bot-label" className="sr-only">Bot</label>
+ <Select value={selectedId} onValueChange={value => { if (value) onSelect(value) }} items={bots.map(bot => ({ value: bot.id, label: bot.name }))}>
+ <SelectTrigger aria-labelledby="pii-bot-label" data-testid="pii-bot-select" className="min-h-11 w-full border-0 px-2 shadow-none hover:bg-accent dark:bg-transparent dark:hover:bg-accent"><SelectValue>{selected ? <><AgentAvatar name={selected.name} avatarUrl={selected.image} seed={selected.id} size={32} className="size-8 [&_svg]:size-full!" alt="" /><span className="truncate text-base font-medium">{selected.name}</span></> : null}</SelectValue></SelectTrigger>
+ <BotSelectMenuContent>{bots.map(bot => <BotSelectMenuItem key={bot.id} value={bot.id}><span className="flex min-w-0 items-center gap-2"><AgentAvatar name={bot.name} avatarUrl={bot.image} seed={bot.id} size={24} className="size-6 [&_svg]:size-full!" alt="" /><span className="truncate">{bot.name}</span></span></BotSelectMenuItem>)}</BotSelectMenuContent>
+ </Select>
+ </div>
+ </section> : null}
+ {bots.length ? children : (
+ <div className="py-8">
+ <h2 className="font-medium">Create a bot first</h2>
+ <p className="mt-1 text-sm text-muted-foreground">Open My Bots to create a bot, then manage its filters here.</p>
+ </div>
+ )}
+ </TabsContent>
+ </Tabs>
+ </section>
+ )
+}
diff --git a/src/web/src/components/community/bots/bot-pii-playground.dom.test.tsx b/src/web/src/components/community/bots/bot-pii-playground.dom.test.tsx
new file mode 100644
index 000000000..120e0b589
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-playground.dom.test.tsx
@@ -0,0 +1,63 @@
+import { expect, it, vi } from "vitest"
+import { fireEvent, render, screen } from "@/test/react-dom-harness"
+import { BotPiiPlayground } from "./bot-pii-playground"
+
+it("runs only nonempty input and displays returned text as text", () => {
+ const onRun = vi.fn(), onValueChange = vi.fn()
+ const view = render(<BotPiiPlayground value="" onValueChange={onValueChange} onRun={onRun} pending={false} />)
+ expect(screen.getByRole("button", { name: "Run test" })).toBeDisabled()
+ fireEvent.change(screen.getByLabelText("Original message"), { target: { value: "private sample" } })
+ expect(onValueChange).toHaveBeenCalledWith("private sample")
+ view.rerender(<BotPiiPlayground value="private sample" onValueChange={onValueChange} onRun={onRun} pending={false} result="<script>sample</script>" />)
+ fireEvent.click(screen.getByRole("button", { name: "Run test" }))
+ expect(onRun).toHaveBeenCalledOnce()
+ expect(screen.getByText("<script>sample</script>")).toBeVisible()
+ expect(screen.getAllByRole("textbox")).toHaveLength(1)
+ expect(view.container.querySelector("output")).toHaveTextContent("<script>sample</script>")
+ expect(view.container.querySelector("script")).toBeNull()
+ view.rerender(<BotPiiPlayground value="private sample" onValueChange={onValueChange} onRun={onRun} pending />)
+ expect(screen.getByRole("button", { name: "Running…" })).toBeDisabled()
+})
+
+it("explains the test transport and blocks input beyond the UTF-8 byte limit", () => {
+ const onRun = vi.fn()
+ render(<BotPiiPlayground value={"汉".repeat(342)} onValueChange={vi.fn()} onRun={onRun} pending={false} />)
+ expect(screen.getByText("We don’t store this message. It is sent to your computer for testing.")).toBeVisible()
+ expect(screen.getByRole("alert")).toHaveTextContent("1,024 UTF-8 bytes")
+ expect(screen.getByRole("button", { name: "Run test" })).toBeDisabled()
+ fireEvent.click(screen.getByRole("button", { name: "Run test" }))
+ expect(onRun).not.toHaveBeenCalled()
+})
+
+it("links processing to the real pending state and reveals only returned output", () => {
+ const props = { value: "[email protected]", onValueChange: vi.fn(), onRun: vi.fn() }
+ const view = render(<BotPiiPlayground {...props} pending result="old result" />)
+ expect(view.container.querySelector('[data-phase="running"]')).not.toBeNull()
+ expect(view.container.querySelector("output")).toBeNull()
+ expect(screen.getByRole("status")).toHaveTextContent("Filtering on your computer")
+ view.rerender(<BotPiiPlayground {...props} pending={false} result="[EMAIL]" />)
+ expect(view.container.querySelector('[data-phase="complete"]')).not.toBeNull()
+ expect(view.container.querySelector("output")).toHaveTextContent("[EMAIL]")
+ view.rerender(<BotPiiPlayground {...props} pending={false} error="Could not run filters. Try again." />)
+ expect(view.container.querySelector('[data-phase="error"]')).not.toBeNull()
+ expect(view.container.querySelector("output")).toBeNull()
+ expect(screen.getByRole("alert")).toHaveTextContent("Try again")
+ fireEvent.click(screen.getByRole("button", { name: "Run test" }))
+ expect(props.onRun).toHaveBeenCalledOnce()
+})
+
+it("shows actual matched names as text and distinguishes empty from legacy metadata", () => {
+ const props = { value: "sample", onValueChange: vi.fn(), onRun: vi.fn(), pending: false, result: "filtered" }
+ const triggeredFilters = [{ id: "one", name: "Email addresses" }, { id: "two", name: "<b>Custom</b>" }]
+ const view = render(<BotPiiPlayground {...props} triggeredFilters={triggeredFilters} />)
+ expect(screen.getByTestId("pii-triggered-filters")).toHaveTextContent("Matched filters: Email addresses, <b>Custom</b>")
+ expect(view.container.querySelector("b")).toBeNull()
+ view.rerender(<BotPiiPlayground {...props} triggeredFilters={[]} />)
+ expect(screen.getByTestId("pii-triggered-filters")).toHaveTextContent("No filters matched.")
+ view.rerender(<BotPiiPlayground {...props} />)
+ expect(screen.queryByTestId("pii-triggered-filters")).toBeNull()
+ view.rerender(<BotPiiPlayground {...props} triggeredFilters={triggeredFilters} pending />)
+ expect(screen.queryByTestId("pii-triggered-filters")).toBeNull()
+ view.rerender(<BotPiiPlayground {...props} triggeredFilters={triggeredFilters} error="Failed" />)
+ expect(screen.queryByTestId("pii-triggered-filters")).toBeNull()
+})
diff --git a/src/web/src/components/community/bots/bot-pii-playground.module.css b/src/web/src/components/community/bots/bot-pii-playground.module.css
new file mode 100644
index 000000000..f1d926dd6
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-playground.module.css
@@ -0,0 +1,69 @@
+.workspace {
+ transition: border-color 180ms ease-out;
+}
+
+.workspace[data-phase="running"] {
+ border-color: var(--ring);
+}
+
+.workspace[data-phase="complete"] .result {
+ animation: receive 300ms cubic-bezier(0.16, 1, 0.3, 1) both;
+}
+
+@keyframes receive {
+ from { clip-path: inset(100% 0 0); transform: translateY(8px); }
+ to { clip-path: inset(0); transform: translateY(0); }
+}
+
+.processing {
+ container-type: inline-size;
+ position: relative;
+ display: flex;
+ align-items: center;
+ justify-content: space-around;
+ height: 48px;
+ max-width: 320px;
+ overflow: hidden;
+}
+
+.filter {
+ z-index: 1;
+ display: grid;
+ place-items: center;
+ width: 40px;
+ height: 40px;
+ border-radius: var(--radius-lg);
+ background: var(--background);
+ color: var(--muted-foreground);
+ animation: filter-pass 1080ms ease-in-out infinite;
+}
+
+.packet {
+ position: absolute;
+ left: 0;
+ width: 12px;
+ height: 4px;
+ border-radius: var(--radius-sm);
+ background: var(--foreground);
+ animation: message-pass 1080ms linear infinite;
+}
+
+.packetSecond { animation-delay: -540ms; }
+
+@keyframes message-pass {
+ 0% { transform: translateX(-12px); opacity: 0; }
+ 10%, 85% { opacity: 0.7; }
+ 100% { transform: translateX(100cqw); opacity: 0; }
+}
+
+@keyframes filter-pass {
+ 0%, 70%, 100% { opacity: 0.4; transform: scale(0.94); }
+ 35% { opacity: 1; transform: scale(1); }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .workspace { transition: none; }
+ .filter, .packet { animation: none; }
+ .packet { display: none; }
+ .workspace[data-phase="complete"] .result { animation: none; }
+}
diff --git a/src/web/src/components/community/bots/bot-pii-playground.tsx b/src/web/src/components/community/bots/bot-pii-playground.tsx
new file mode 100644
index 000000000..7ab0eff84
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-playground.tsx
@@ -0,0 +1,57 @@
+"use client"
+
+import { PII_TEXT_MAX_BYTES, utf8ByteLength } from "@alook/shared"
+import { Check, ListFilter, Play } from "lucide-react"
+import { Button } from "@/components/ui/button"
+import { Textarea } from "@/components/ui/textarea"
+import styles from "./bot-pii-playground.module.css"
+
+export function BotPiiPlayground({ value, onValueChange, result, triggeredFilters, pending, error, disabled, onRun }: {
+ value: string
+ onValueChange: (value: string) => void
+ result?: string
+ triggeredFilters?: { id: string; name: string }[]
+ pending: boolean
+ error?: string
+ disabled?: boolean
+ onRun: () => void
+}) {
+ const tooLong = utf8ByteLength(value) > PII_TEXT_MAX_BYTES
+ const phase = pending ? "running" : error ? "error" : result !== undefined ? "complete" : "idle"
+ return (
+ <section className="flex min-w-0 flex-col gap-3" aria-label="Playground">
+ <div className={`${styles.workspace} overflow-hidden rounded-xl border border-input`} data-phase={phase}>
+ <div className="flex flex-col gap-4 bg-muted/30 p-4 sm:p-6" aria-live="polite" aria-busy={pending}>
+ <div className="flex items-center justify-between gap-3">
+ <h4 id="pii-output-label" className="text-sm font-medium">Public message</h4>
+ {phase === "complete" ? <Check aria-hidden="true" className="size-5 text-muted-foreground" /> : null}
+ </div>
+ <div className="max-h-64 min-h-24 overflow-y-auto thin-scrollbar">
+ {pending ? <div className="flex min-h-24 flex-col items-center justify-center gap-3 text-center">
+ <div className={`${styles.processing} w-full`} aria-hidden="true">
+ <span className={styles.packet} /><span className={`${styles.packet} ${styles.packetSecond}`} />
+ {[0, 1, 2].map(index => <span key={index} className={styles.filter} style={{ animationDelay: `${index * 180}ms` }}><ListFilter className="size-6" /></span>)}
+ </div>
+ <p role="status" className="text-sm text-muted-foreground">Filtering on your computer…</p>
+ </div>
+ : error ? <p role="alert" className="text-sm leading-relaxed text-destructive">{error}</p>
+ : result !== undefined ? <output aria-labelledby="pii-output-label" className={`${styles.result} block whitespace-pre-wrap wrap-anywhere text-sm leading-relaxed`}>{result || "The filtered message is empty."}</output>
+ : <p className="flex min-h-24 items-center justify-center text-center text-sm leading-relaxed text-muted-foreground">Your filtered text will appear here.</p>}
+ </div>
+ {phase === "complete" && triggeredFilters !== undefined ? <p data-testid="pii-triggered-filters" className="wrap-anywhere text-sm leading-relaxed text-muted-foreground">
+ {triggeredFilters.length ? <>Matched filters: {triggeredFilters.map(filter => filter.name).join(", ")}</> : "No filters matched."}
+ </p> : null}
+ </div>
+ <form className="flex flex-col gap-4 p-4 sm:p-6" onSubmit={event => { event.preventDefault(); if (!pending && !disabled && !tooLong && value.trim()) onRun() }}>
+ <label htmlFor="pii-playground-input" className="text-sm font-medium">Original message</label>
+ <Textarea aria-describedby="pii-playground-notice" id="pii-playground-input" data-testid="pii-playground-input" value={value} onChange={event => onValueChange(event.target.value)} placeholder="Enter text to test your filters…" className="max-h-64 min-h-28 resize-y overflow-y-auto rounded-sm border-0 bg-transparent p-0 text-base leading-relaxed shadow-none focus-visible:ring-0 dark:bg-transparent sm:text-base" disabled={pending} />
+ <div className="flex justify-end">
+ <Button type="submit" disabled={pending || disabled || tooLong || !value.trim()} className="min-h-11 gap-2 px-4" data-testid="pii-playground-run"><Play className="size-4" />{pending ? "Running…" : "Run test"}</Button>
+ </div>
+ {tooLong ? <p role="alert" className="text-sm text-destructive">Use 1,024 UTF-8 bytes or fewer.</p> : null}
+ </form>
+ </div>
+ <p id="pii-playground-notice" className="text-sm leading-relaxed text-muted-foreground">We don’t store this message. It is sent to your computer for testing.</p>
+ </section>
+ )
+}
diff --git a/src/web/src/components/community/bots/bot-pii-view.dom.test.tsx b/src/web/src/components/community/bots/bot-pii-view.dom.test.tsx
new file mode 100644
index 000000000..ec4ba5953
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-view.dom.test.tsx
@@ -0,0 +1,91 @@
+import { beforeEach, expect, it, vi } from "vitest"
+import { fireEvent, render, screen, waitFor } from "@/test/react-dom-harness"
+import { ConversationActivityContext } from "@/contexts/community/conversation-activity"
+import { BotPiiView } from "./bot-pii-view"
+
+const state = vi.hoisted(() => ({ botId: "b1", viewer: "owner", epoch: 0, clearPreview: vi.fn(), reload: vi.fn(), preview: vi.fn(), pending: false, previewPending: false }))
+vi.mock("@/hooks/community/use-bots", () => ({ useBots: () => ({ bots: [{ id: state.botId, name: "Preview bot", machineId: "m1" }], isPending: false, profilesPending: false, isError: false }) }))
+vi.mock("@/hooks/community/use-machines", () => ({ useMachines: () => ({ machines: [{ id: "m1", status: "online" }], isPending: false }) }))
+vi.mock("@/stores/community/ws", () => ({ useCommunityWsStore: (select: (value: unknown) => unknown) => select({ profileViewerId: state.viewer, profileAccountEpoch: state.epoch }) }))
+vi.mock("./bot-pii-chat-action", () => ({ BotPiiChatAction: () => <button>Ask your bot to add a PII filter.</button> }))
+vi.mock("@/components/avatar", () => ({ AgentAvatar: () => <span /> }))
+vi.mock("@/hooks/community/use-bot-pii", () => ({ useBotPii: () => ({ snapshot: { revision: 0, plugins: [] }, pending: state.pending, previewPending: state.previewPending, preview: state.preview, clearPreview: state.clearPreview, reload: state.reload }) }))
+beforeEach(() => { state.pending = false; state.previewPending = false; state.botId = "b1"; state.viewer = "owner"; state.epoch = 0; state.reload.mockReset(); state.clearPreview.mockReset(); state.preview.mockReset() })
+
+it("sends only an explicit run and clears a result when input changes", () => {
+ render(<BotPiiView />)
+ expect(screen.getByRole("textbox")).toBeVisible()
+ fireEvent.change(screen.getByLabelText("Original message"), { target: { value: "[email protected]" } })
+ expect(state.clearPreview).toHaveBeenCalledOnce()
+ expect(state.preview).not.toHaveBeenCalled()
+ fireEvent.click(screen.getByRole("button", { name: "Run test" }))
+ expect(state.preview).toHaveBeenCalledWith("[email protected]")
+})
+
+it("clears draft input when switching bots, account epochs, or leaving an inactive view", async () => {
+ const page = (active = true) => <ConversationActivityContext.Provider value={active}><BotPiiView /></ConversationActivityContext.Provider>
+ const h = render(page())
+ const type = () => fireEvent.change(screen.getByLabelText("Original message"), { target: { value: "private draft" } })
+ type()
+ state.botId = "b2"
+ h.rerender(page())
+ expect(screen.getByLabelText("Original message")).toHaveValue("")
+ type()
+ state.epoch = 1
+ h.rerender(page())
+ expect(screen.getByLabelText("Original message")).toHaveValue("")
+ type()
+ h.rerender(page(false))
+ await waitFor(() => expect(screen.getByLabelText("Original message")).toHaveValue(""))
+ expect(state.clearPreview).toHaveBeenCalled()
+})
+
+it("shows filters and playground together and spins refresh while waiting", () => {
+ const view = render(<BotPiiView />)
+ expect(screen.getAllByRole("tab")).toHaveLength(1)
+ expect(screen.getByRole("tab", { name: "PII Filters" })).toHaveAttribute("aria-selected", "true")
+ expect(screen.queryByRole("tab", { name: "Playground" })).toBeNull()
+ expect(screen.getByRole("heading", { name: "Filters for bot message", level: 2 })).toBeVisible()
+ const headings = Array.from(screen.getByRole("tabpanel").querySelectorAll("h2,h3")).map(heading => heading.textContent)
+ expect(headings).toEqual(["Filters for bot message", "Which bot to set up", "PII steps", "Playground"])
+ expect(screen.getByRole("heading", { name: "Playground", level: 3 })).toBeVisible()
+ expect(screen.getByLabelText("Original message")).toBeVisible()
+ state.pending = true
+ view.rerender(<BotPiiView />)
+ const reload = screen.getByRole("button", { name: "Reload filters" })
+ expect(reload).toBeDisabled()
+ expect(reload).toHaveAttribute("aria-busy", "true")
+ expect(reload.querySelector("svg")).toHaveClass("animate-spin", "motion-reduce:animate-none")
+ expect(screen.queryByText("Waiting for this machine…")).toBeNull()
+ state.previewPending = true
+ view.rerender(<BotPiiView />)
+ expect(reload).toHaveAttribute("aria-busy", "false")
+ expect(reload.querySelector("svg")).not.toHaveClass("animate-spin")
+ state.pending = false
+ state.previewPending = false
+ view.rerender(<BotPiiView />)
+ expect(reload).toHaveAttribute("aria-busy", "false")
+ expect(reload.querySelector("svg")).not.toHaveClass("animate-spin")
+})
+
+it("reloads on an enabled click and ignores clicks while configuration or preview is pending", () => {
+ const view = render(<BotPiiView />)
+ const reload = screen.getByRole("button", { name: "Reload filters" })
+ fireEvent.click(reload)
+ expect(state.reload).toHaveBeenCalledOnce()
+ state.pending = true
+ view.rerender(<BotPiiView />)
+ expect(reload).toBeDisabled()
+ fireEvent.click(reload)
+ expect(state.reload).toHaveBeenCalledOnce()
+ state.previewPending = true
+ view.rerender(<BotPiiView />)
+ fireEvent.click(reload)
+ expect(state.reload).toHaveBeenCalledOnce()
+ state.pending = false
+ state.previewPending = false
+ view.rerender(<BotPiiView />)
+ expect(reload).toBeEnabled()
+ fireEvent.click(reload)
+ expect(state.reload).toHaveBeenCalledTimes(2)
+})
diff --git a/src/web/src/components/community/bots/bot-pii-view.tsx b/src/web/src/components/community/bots/bot-pii-view.tsx
new file mode 100644
index 000000000..4cb6870a1
--- /dev/null
+++ b/src/web/src/components/community/bots/bot-pii-view.tsx
@@ -0,0 +1,66 @@
+"use client"
+
+import { useEffect } from "react"
+import { RefreshCw } from "lucide-react"
+import { useConversationActivity } from "@/contexts/community/conversation-activity"
+import { useCommunityWsStore } from "@/stores/community/ws"
+import { useAtom, useCreateAtom } from "@tanstack/react-store"
+import { isPresenceOnline } from "@alook/shared"
+import { useBots } from "@/hooks/community/use-bots"
+import { useMachines } from "@/hooks/community/use-machines"
+import { useBotPii } from "@/hooks/community/use-bot-pii"
+import { Button } from "@/components/ui/button"
+import { Skeleton } from "@/components/ui/skeleton"
+import { BotPiiPanel } from "./bot-pii-panel"
+import { BotPiiList } from "./bot-pii-list"
+import { BotPiiChatAction } from "./bot-pii-chat-action"
+import { BotPiiPlayground } from "./bot-pii-playground"
+
+function BotPipeline({ botId, botName, machineId, online }: { botId: string; botName: string; machineId: string; online: boolean }) {
+ const pii = useBotPii(botId, machineId, online)
+ const [input, setInput] = useAtom(useCreateAtom(""))
+ const active = useConversationActivity()
+ const { clearPreview } = pii
+ useEffect(() => { if (!active) { setInput(""); clearPreview() } }, [active, setInput, clearPreview])
+ return (
+ <>
+ <section className="flex min-w-0 flex-col gap-4" aria-labelledby="pii-filters-heading">
+ <div className="flex flex-col">
+ <div className="flex items-center gap-2">
+ <h3 id="pii-filters-heading" className="text-base font-medium">PII steps</h3>
+ <Button variant="ghost" size="icon" className="size-11 sm:size-8" aria-label="Reload filters" aria-busy={pii.pending && !pii.previewPending} title="Reload filters" disabled={pii.pending || !online} onClick={() => { void pii.reload() }}><RefreshCw aria-hidden="true" className={pii.pending && !pii.previewPending ? "animate-spin motion-reduce:animate-none" : undefined} /></Button>
+ </div>
+ <BotPiiChatAction botId={botId} />
+ </div>
+ {pii.snapshot ? <BotPiiList botName={botName} plugins={pii.snapshot.plugins} pending={pii.pending} error={pii.error} unavailable={pii.unavailable}
+ onSetEnabled={(id, enabled) => { void pii.setEnabled(id, enabled) }} onDelete={pii.remove} />
+ : pii.unavailable || pii.error ? <p role={pii.error ? "alert" : "status"} className="text-sm text-muted-foreground">{pii.unavailable ?? pii.error}</p>
+ : <div role="status" aria-label="Loading filters" className="flex flex-col gap-4">{[0, 1, 2].map(id => <Skeleton key={id} className="h-20 w-full rounded-md" />)}</div>}
+ </section>
+ <section className="flex min-w-0 flex-col gap-4" aria-labelledby="pii-playground-heading">
+ <h3 id="pii-playground-heading" className="text-base font-medium">Playground</h3>
+ <BotPiiPlayground value={input} onValueChange={value => { setInput(value); pii.clearPreview() }}
+ result={pii.output} triggeredFilters={pii.triggeredFilters} pending={pii.previewPending} error={pii.previewError ?? pii.unavailable}
+ disabled={pii.pending || !pii.snapshot || Boolean(pii.unavailable)} onRun={() => { void pii.preview(input) }} />
+ </section>
+ </>
+ )
+}
+
+export function BotPiiView() {
+ const owner = useCommunityWsStore(state => `${state.profileViewerId}:${state.profileAccountEpoch}`)
+ const bots = useBots()
+ const machines = useMachines()
+ const [chosenId, setChosenId] = useAtom(useCreateAtom(""))
+ const selected = bots.bots.find(bot => bot.id === chosenId) ?? bots.bots[0]
+ if (bots.isPending || bots.profilesPending) return <div role="status" aria-label="Loading bots" className="p-6"><Skeleton className="h-8 w-48" /></div>
+ if (bots.isError && !bots.data) return <div role="alert" className="flex flex-col items-start gap-3 p-6"><p>Could not load your bots.</p><Button variant="outline" className="min-h-11" onClick={() => { void bots.refetch() }}>Retry</Button></div>
+ const machine = machines.machines.find(item => item.id === selected?.machineId)
+ return (
+ <BotPiiPanel bots={bots.bots} selectedId={selected?.id ?? ""} onSelect={setChosenId}>
+ {selected && machines.isPending ? <Skeleton className="h-20 w-full" />
+ : selected && machine?.status === undefined ? <div role="status" className="flex flex-col items-start gap-3"><p className="text-sm text-muted-foreground">Could not confirm this machine’s status.</p><Button variant="outline" className="min-h-11" onClick={() => { void machines.refetch() }}>Retry</Button></div>
+ : selected ? <BotPipeline key={`${owner}:${selected.id}:${selected.machineId}`} botId={selected.id} botName={selected.name} machineId={selected.machineId} online={isPresenceOnline(machine?.status)} /> : null}
+ </BotPiiPanel>
+ )
+}
diff --git a/src/web/src/components/community/bots/me-privacy-route.tsx b/src/web/src/components/community/bots/me-privacy-route.tsx
new file mode 100644
index 000000000..3ccd4cd89
--- /dev/null
+++ b/src/web/src/components/community/bots/me-privacy-route.tsx
@@ -0,0 +1,17 @@
+"use client"
+
+import { ChevronLeft } from "lucide-react"
+import { Button } from "@/components/ui/button"
+import { useUiHandlers } from "@/stores/community"
+import { BotPiiView } from "./bot-pii-view"
+
+export default function MePrivacyPage() {
+ const handlers = useUiHandlers()
+ return <div className="flex min-h-0 flex-1 flex-col">
+ <header className="flex h-12 shrink-0 items-center gap-2 border-b border-border/40 px-6 sm:hidden">
+ <Button variant="ghost" size="icon-sm" className="size-11" aria-label="Back" onClick={() => handlers.goBackMobile?.()}><ChevronLeft className="size-5" /></Button>
+ <span className="ml-1 truncate text-base font-semibold">Privacy</span>
+ </header>
+ <BotPiiView />
+ </div>
+}
diff --git a/src/web/src/components/community/channels/dm-sidebar.prefetch.dom.test.ts b/src/web/src/components/community/channels/dm-sidebar.prefetch.dom.test.ts
index 6936d6f58..ec1e2a7e3 100644
--- a/src/web/src/components/community/channels/dm-sidebar.prefetch.dom.test.ts
+++ b/src/web/src/components/community/channels/dm-sidebar.prefetch.dom.test.ts
@@ -86,6 +86,21 @@ describe("DmSidebar navigation intent", () => {
})
+ it("places Privacy directly after My Bots without navigating on hover", () => {
+ const onShowPrivacy = vi.fn()
+ const view = render(createElement(DmSidebar, { dms: [], activeDm: null, onPickDm: vi.fn(), onShowFriends: vi.fn(), onShowMachines: vi.fn(), onShowBots: vi.fn(), onShowPrivacy, privacyActive: true }))
+ const links = view.getAllByRole("link")
+ expect(links.map(link => link.textContent)).toEqual(["Friends", "Machines", "My Bots", "Privacy"])
+ const privacy = view.getByRole("link", { name: "Privacy" })
+ expect(privacy).toHaveAttribute("href", "/c/me/privacy")
+ expect(privacy).toHaveAttribute("aria-current", "page")
+ expect(view.getByRole("link", { name: "Friends" })).not.toHaveAttribute("aria-current")
+ fireEvent.pointerEnter(privacy)
+ expect(onShowPrivacy).not.toHaveBeenCalled()
+ fireEvent.click(privacy)
+ expect(onShowPrivacy).toHaveBeenCalledOnce()
+ })
+
it("selects the intended DM only on click or Enter", async () => {
const onPickDm = vi.fn()
const renderer = render(createElement(DmSidebar, {
diff --git a/src/web/src/components/community/channels/dm-sidebar.tsx b/src/web/src/components/community/channels/dm-sidebar.tsx
index 24edfd5aa..b2060051d 100644
--- a/src/web/src/components/community/channels/dm-sidebar.tsx
+++ b/src/web/src/components/community/channels/dm-sidebar.tsx
@@ -3,7 +3,7 @@
import { CommunityNavigationLink } from "../shell/community-navigation-link"
import { dmHref } from "@/lib/community/community-route"
import { memo } from "react"
-import { Users, Ban, Monitor, Bot } from "lucide-react"
+import { Users, Ban, Monitor, Bot, Shield } from "lucide-react"
import { ConversationResolutionErrorFrame } from "./conversation-resolution-error-frame"
import { Avatar } from "../avatar"
import { Skeleton } from "@/components/ui/skeleton"
@@ -13,8 +13,8 @@ import { selectUnreadPresentation } from "@/hooks/community/unread-presentation"
import { compactRequestCount } from "@/lib/community/friend-requests"
export const DmSidebar = memo(function DmSidebar({
- dms, activeDm, blockedUserIds, loading, onPickDm, onShowFriends, onShowMachines, onShowBots,
- friendsActive, machinesActive, botsActive,
+ dms, activeDm, blockedUserIds, loading, onPickDm, onShowFriends, onShowMachines, onShowBots, onShowPrivacy,
+ friendsActive, machinesActive, botsActive, privacyActive,
friendRequestCount = 0,
loadError,
}: {
@@ -27,12 +27,14 @@ export const DmSidebar = memo(function DmSidebar({
onShowFriends: () => void
onShowMachines?: () => void
onShowBots?: () => void
+ onShowPrivacy?: () => void
friendsActive?: boolean
friendRequestCount?: number
machinesActive?: boolean
botsActive?: boolean
+ privacyActive?: boolean
}) {
- const isFriendsActive = friendsActive ?? (activeDm === null && !machinesActive && !botsActive)
+ const isFriendsActive = friendsActive ?? (activeDm === null && !machinesActive && !botsActive && !privacyActive)
const requestCount = compactRequestCount(friendRequestCount)
return (
<aside className="flex min-h-0 min-w-0 flex-1 flex-col">
@@ -90,6 +92,21 @@ export const DmSidebar = memo(function DmSidebar({
<span className="min-w-0 flex-1 truncate">My Bots</span>
</CommunityNavigationLink>
)}
+ {onShowPrivacy && (
+ <CommunityNavigationLink
+ href="/c/me/privacy"
+ prefetchMode="visible"
+ active={privacyActive}
+ onActivate={onShowPrivacy}
+ className={[
+ "mb-2 flex h-11 w-full sm:h-9 items-center gap-2 rounded-md px-2 text-sm font-medium focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none",
+ privacyActive ? "bg-sidebar-accent text-foreground" : "text-muted-foreground hover:bg-sidebar-accent/60 hover:text-foreground",
+ ].join(" ")}
+ >
+ <Shield className="size-5 shrink-0" />
+ <span className="min-w-0 flex-1 truncate">Privacy</span>
+ </CommunityNavigationLink>
+ )}
<div className="my-2 h-px bg-border" />
</div>
<div data-slot="dm-sidebar-list" className="min-h-0 flex-1 overflow-y-auto thin-scrollbar px-2 pb-4">
@@ -150,6 +167,7 @@ export function DmSidebarSkeleton() {
<Skeleton className="mb-1 h-9 w-full rounded-md" />
<Skeleton className="mb-1 h-9 w-full rounded-md" />
<Skeleton className="mb-2 h-9 w-full rounded-md" />
+ <Skeleton className="mb-2 h-9 w-full rounded-md" />
<div className="my-2 h-px bg-border" />
</div>
<div className="min-h-0 flex-1 overflow-y-auto thin-scrollbar px-2 pb-4">
diff --git a/src/web/src/components/community/shell/community-module-route.tsx b/src/web/src/components/community/shell/community-module-route.tsx
index 1447b947a..9474bb933 100644
--- a/src/web/src/components/community/shell/community-module-route.tsx
+++ b/src/web/src/components/community/shell/community-module-route.tsx
@@ -8,6 +8,7 @@ import { CommunityPendingFrame } from "./community-pending-frame"
import { ServerConversationRoute } from "../channels/server-conversation-route"
import FriendsRoute from "../social/friends-route"
+import PrivacyRoute from "../bots/me-privacy-route"
import BotsRoute from "../bots/me-bots-route"
import MachinesRoute from "../machines/me-machines-route"
import CommunityRootRoute from "./community-root-route"
@@ -27,6 +28,7 @@ export function CommunityModuleRoute({ href, routeCommitted }: { href: string; r
<ServerConversationRoute serverId={plan.main.serverId} serverParam={encodeURIComponent(plan.main.serverId)}
channelId={plan.main.leafId} href={target.href} routeCommitted={routeCommitted} />
) : plan.main.kind === "friends" ? <FriendsRoute />
+ : plan.main.kind === "privacy" ? <PrivacyRoute />
: plan.main.kind === "bots" ? <BotsRoute />
: plan.main.kind === "machines" ? <MachinesRoute />
: plan.main.kind === "me-root" ? <MeRootRoute />
diff --git a/src/web/src/components/community/shell/community-pending-frame.tsx b/src/web/src/components/community/shell/community-pending-frame.tsx
index fd2e99974..c3f4d6df1 100644
--- a/src/web/src/components/community/shell/community-pending-frame.tsx
+++ b/src/web/src/components/community/shell/community-pending-frame.tsx
@@ -51,6 +51,9 @@ export function CommunityPendingFrame({
)
let content: ReactNode
switch (plan.main.kind) {
+ case "privacy":
+ content = <RouteResolutionPendingFrame />
+ break
case "me-root":
content = <MeRootPendingFrame />
break
diff --git a/src/web/src/components/community/shell/dm-sidebar-slot.tsx b/src/web/src/components/community/shell/dm-sidebar-slot.tsx
index 8148b5e5a..9a5b64de4 100644
--- a/src/web/src/components/community/shell/dm-sidebar-slot.tsx
+++ b/src/web/src/components/community/shell/dm-sidebar-slot.tsx
@@ -46,6 +46,7 @@ function MeSidebar({ pathname, routeCommitted }: { pathname: string; routeCommit
useFriendsPresence(routeCommitted)
const machinesActive = pathname === "/c/me/machines"
+ const privacyActive = pathname === "/c/me/privacy"
const botsActive = pathname === "/c/me/bots"
const friendsActive = pathname === "/c/me/friends"
@@ -70,6 +71,11 @@ function MeSidebar({ pathname, routeCommitted }: { pathname: string; routeCommit
communityRuntime.ui.get().uiHandlers.navigatePath?.("/c/me/bots")
}, [communityRuntime.ui])
+ const onShowPrivacy = useCallback(() => {
+ communityRuntime.ui.actions.setCurrentChannelId(null)
+ communityRuntime.ui.get().uiHandlers.navigatePath?.("/c/me/privacy")
+ }, [communityRuntime.ui])
+
const blockedUserIds = useMemo(
() => new Set(blocked.map((b) => b.userId ?? b.id)),
[blocked],
@@ -89,11 +95,13 @@ function MeSidebar({ pathname, routeCommitted }: { pathname: string; routeCommit
friendRequestCount={friendRequestCount}
onShowMachines={onShowMachines}
onShowBots={onShowBots}
+ onShowPrivacy={onShowPrivacy}
+ privacyActive={privacyActive}
friendsActive={friendsActive}
machinesActive={machinesActive}
botsActive={botsActive}
/>
- ), [dms, currentChannelId, dmsLoading, initialError, isFetching, retryDms, blockedUserIds, enterDm, onShowFriends, friendRequestCount, onShowMachines, onShowBots, friendsActive, machinesActive, botsActive])
+ ), [dms, currentChannelId, dmsLoading, initialError, isFetching, retryDms, blockedUserIds, enterDm, onShowFriends, friendRequestCount, onShowMachines, onShowBots, onShowPrivacy, privacyActive, friendsActive, machinesActive, botsActive])
return sidebar()
}
diff --git a/src/web/src/hooks/community/community-ws/pii-events.ts b/src/web/src/hooks/community/community-ws/pii-events.ts
new file mode 100644
index 000000000..25ff2f66f
--- /dev/null
+++ b/src/web/src/hooks/community/community-ws/pii-events.ts
@@ -0,0 +1,30 @@
+import type { PiiResult } from "@alook/shared"
+import { communityKeys } from "@/lib/query-keys"
+import type { BotPiiState } from "../use-bot-pii"
+import type { PresenceMachineEventContext } from "./handler-context"
+
+export function handleBotPii(event: { machineId: string; botId: string; requestId: string; result: PiiResult }, { queryClient }: PresenceMachineEventContext) {
+ const key = communityKeys.botPii(event.machineId, event.botId)
+ const pending = queryClient.getQueryData<BotPiiState>(key)
+ if (pending?.status !== "pending" || pending.requestId !== event.requestId) return
+ const result = event.result
+ const preview = pending.operation === "preview"
+ if (result.status === "complete") {
+ queryClient.setQueryData<BotPiiState>(key, {
+ requestId: event.requestId, operation: pending.operation, snapshot: result.snapshot,
+ status: preview && result.output === undefined ? "error" : "complete",
+ output: preview ? result.output : undefined,
+ triggeredFilters: preview && result.output !== undefined ? result.triggeredFilters : undefined,
+ error: preview && result.output === undefined ? "Could not read the test result. Run it again." : undefined,
+ })
+ return
+ }
+ queryClient.setQueryData<BotPiiState>(key, {
+ requestId: event.requestId, operation: pending.operation, status: "error", snapshot: result.snapshot ?? pending.snapshot,
+ uncertain: !preview && !result.snapshot,
+ error: result.code === "PII_CONFLICT" ? "Filters changed elsewhere. Review the updated list and try again."
+ : result.code === "PII_PLUGIN_NOT_FOUND" ? "This plugin was removed. Reload filters."
+ : preview ? "The filters could not process this message. Check the filters and try again."
+ : "Filters are unavailable. Reload to try again.",
+ })
+}
diff --git a/src/web/src/hooks/community/community-ws/registry.test.ts b/src/web/src/hooks/community/community-ws/registry.test.ts
index 8322037f6..b32ffd49d 100644
--- a/src/web/src/hooks/community/community-ws/registry.test.ts
+++ b/src/web/src/hooks/community/community-ws/registry.test.ts
@@ -21,6 +21,8 @@ const handlers = vi.hoisted(() => new Proxy({} as Record<string, ReturnType<type
},
}))
+vi.mock("./pii-events", () => ({ handleBotPii: handlers.handleBotPii }))
+
vi.mock("./server-rail-reconciliation", () => ({ handleServerRailChanged: handlers.handleServerRailChanged }))
vi.mock("./message-events", () => ({
@@ -90,7 +92,7 @@ describe("community WebSocket registry", () => {
it("has exactly one entry for each runtime event type", () => {
const eventTypes = Object.values(WS_EVENTS).sort()
const registryTypes = Object.keys(communityWsRegistry).sort()
- expect(eventTypes).toHaveLength(48)
+ expect(eventTypes).toHaveLength(49)
expect(registryTypes).toEqual(eventTypes)
})
@@ -140,6 +142,7 @@ describe("community WebSocket registry", () => {
["community:profile.update", "handleProfileUpdate"],
["community:machine.created", "handleMachineCreated"],
["community:runtime.models", "handleRuntimeModels"],
+ ["community:bot.pii", "handleBotPii"],
["community:bot.audit_event", "handleBotAuditEvent"],
] as const)("dispatches %s through its existing handler group", (type, handlerName) => {
const context = dispatchContext()
diff --git a/src/web/src/hooks/community/community-ws/registry.ts b/src/web/src/hooks/community/community-ws/registry.ts
index 04faa17e3..6eca5b92e 100644
--- a/src/web/src/hooks/community/community-ws/registry.ts
+++ b/src/web/src/hooks/community/community-ws/registry.ts
@@ -1,3 +1,4 @@
+import { handleBotPii } from "./pii-events"
import { handleServerRailChanged } from "./server-rail-reconciliation"
import { getCommunityRuntime } from "@/stores/community/runtime"
@@ -114,6 +115,7 @@ export const communityWsRegistry = {
"community:machine.status": { handler: handleMachineStatus, reconnectPolicies: ["machines"] },
"community:machine.updated": { handler: handleMachineUpdated, reconnectPolicies: ["machines"] },
"community:machine.removed": { handler: handleMachineRemoved, reconnectPolicies: ["machines"] },
+ "community:bot.pii": { handler: handleBotPii, reconnectPolicies: ["machines"] },
"community:runtime.models": { handler: handleRuntimeModels, reconnectPolicies: ["machines"] },
"community:bot.audit_event": { handler: handleBotAuditEvent, reconnectPolicies: ["bot-audits"] },
} satisfies CommunityWsRegistry
diff --git a/src/web/src/hooks/community/use-bot-pii.dom.test.tsx b/src/web/src/hooks/community/use-bot-pii.dom.test.tsx
new file mode 100644
index 000000000..dfc2345e4
--- /dev/null
+++ b/src/web/src/hooks/community/use-bot-pii.dom.test.tsx
@@ -0,0 +1,304 @@
+import { createElement, type PropsWithChildren } from "react"
+import { QueryClient } from "@tanstack/react-query"
+import { beforeEach, describe, expect, it, vi } from "vitest"
+import { getCommunityDbRegistry } from "@/lib/community-db/collections"
+import { CommunityTestProvider } from "@/test/community-owner-fixture"
+import { act, renderHook, waitFor } from "@/test/react-dom-harness"
+import { communityKeys } from "@/lib/query-keys"
+import { shouldDehydrateCommunityQuery } from "@/lib/query-persister"
+import { handleBotPii } from "./community-ws/pii-events"
+import { useBotPii, type BotPiiState } from "./use-bot-pii"
+
+const apiFetch = vi.fn()
+vi.mock("@/lib/api/client", () => ({ apiFetch: (...args: unknown[]) => apiFetch(...args) }))
+beforeEach(() => { apiFetch.mockReset(); apiFetch.mockResolvedValue({ dispatched: true }) })
+const snapshot = { revision: 3, plugins: [{ id: "email", name: "Email", description: "Masks email addresses", enabled: true }] }
+function mount(online = true) {
+ const client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ const wrapper = ({ children }: PropsWithChildren) => createElement(CommunityTestProvider, { client }, children)
+ const hook = renderHook(({ botId, machineId, online }) => useBotPii(botId, machineId, online), { wrapper, initialProps: { botId: "b1", machineId: "m1", online } })
+ const state = (botId = "b1", machineId = "m1") => client.getQueryData<BotPiiState>(communityKeys.botPii(machineId, botId))
+ const reply = (requestId: string, botId = "b1", machineId = "m1") => act(() => handleBotPii({ botId, machineId, requestId, result: { status: "complete", snapshot } }, { queryClient: client }))
+ return { ...hook, client, state, reply }
+}
+
+describe("local bot PII management", () => {
+ it("loads only the selected bot and accepts only a matching local result", async () => {
+ const h = mount()
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(1))
+ const requestId = h.state()!.requestId
+ expect(apiFetch.mock.calls[0][0]).toBe("/api/community/bots/b1/pii")
+ expect(JSON.parse(apiFetch.mock.calls[0][1].body)).toEqual({ requestId, operation: { action: "list" } })
+ expect(h.result.current.snapshot).toBeUndefined()
+ await h.reply("old")
+ await h.reply(requestId, "other")
+ await h.reply(requestId, "b1", "other")
+ expect(h.state()?.status).toBe("pending")
+ await h.reply(requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ expect(shouldDehydrateCommunityQuery({ queryKey: communityKeys.botPii("m1", "b1"), state: { status: "success", data: h.state() } })).toBe(false)
+ })
+
+ it("uses the confirmed revision and blocks duplicate mutations until acknowledgement", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ let pending: Promise<boolean>
+ act(() => { pending = h.result.current.setEnabled("email", false) })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ expect(JSON.parse(apiFetch.mock.calls[1][1].body).operation).toEqual({ action: "setEnabled", pluginId: "email", enabled: false, expectedRevision: 3 })
+ expect(h.result.current.snapshot?.plugins[0].enabled).toBe(true)
+ await act(async () => { expect(await h.result.current.remove("email")).toBe(false) })
+ expect(apiFetch).toHaveBeenCalledTimes(2)
+ await act(async () => {
+ handleBotPii({ botId: "b1", machineId: "m1", requestId: h.state()!.requestId, result: { status: "error", code: "PII_CONFLICT", snapshot: { ...snapshot, revision: 4 } } }, { queryClient: h.client })
+ expect(await pending!).toBe(false)
+ })
+ await waitFor(() => expect(h.result.current.snapshot?.revision).toBe(4))
+ expect(h.result.current.error).toMatch(/changed elsewhere/)
+ })
+
+ it("retires old bot requests and never starts a request while offline", async () => {
+ const h = mount(false)
+ expect(apiFetch).not.toHaveBeenCalled()
+ h.rerender({ botId: "b1", machineId: "m1", online: true })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(1))
+ const old = h.state()!.requestId
+ h.rerender({ botId: "b2", machineId: "m2", online: true })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ await h.reply(old)
+ expect(h.result.current.snapshot).toBeUndefined()
+ expect(h.state()?.status).not.toBe("complete")
+ await h.reply(h.state("b2", "m2")!.requestId, "b2", "m2")
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ })
+
+
+ it("blocks further writes after a lost acknowledgement until a fresh list arrives", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ apiFetch.mockRejectedValueOnce(new Error("NETWORK_ERROR"))
+ await act(async () => { expect(await h.result.current.remove("email")).toBe(false) })
+ await waitFor(() => expect(h.result.current.unavailable).toMatch(/Reload filters/))
+ expect(h.result.current.snapshot).toEqual(snapshot)
+ await act(async () => { expect(await h.result.current.setEnabled("email", false)).toBe(false) })
+ expect(apiFetch).toHaveBeenCalledTimes(2)
+ act(() => { void h.result.current.reload() })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(3))
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.unavailable).toBeUndefined())
+ })
+
+ it("does not turn failed loading into an empty list", async () => {
+ apiFetch.mockRejectedValueOnce(new Error("UPGRADE_REQUIRED"))
+ const h = mount()
+ await waitFor(() => expect(h.result.current.error).toMatch(/Update this machine/))
+ expect(h.result.current.snapshot).toBeUndefined()
+ expect(h.result.current.pending).toBe(false)
+ })
+})
+
+describe("PII playground", () => {
+ it("correlates preview output, sends the current revision, and never persists test text", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ let run: Promise<boolean>
+ act(() => { run = h.result.current.preview("[email protected]") })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ const id = h.state()!.requestId
+ expect(JSON.parse(apiFetch.mock.calls[1][1].body).operation).toEqual({ action: "preview", text: "[email protected]", expectedRevision: 3 })
+ expect(JSON.stringify(h.state())).not.toContain("[email protected]")
+ await act(async () => { expect(await h.result.current.preview("duplicate")).toBe(false) })
+ await act(async () => {
+ handleBotPii({ botId: "b1", machineId: "m1", requestId: "old", result: { status: "complete", snapshot, output: "stale" } }, { queryClient: h.client })
+ })
+ expect(h.result.current.output).toBeUndefined()
+ await act(async () => {
+ handleBotPii({ botId: "b1", machineId: "m1", requestId: id, result: { status: "complete", snapshot, output: "[EMAIL]", triggeredFilters: [{ id: "email", name: "Email" }] } }, { queryClient: h.client })
+ expect(await run!).toBe(true)
+ })
+ await waitFor(() => expect(h.result.current.output).toBe("[EMAIL]"))
+ expect(h.result.current.triggeredFilters).toEqual([{ id: "email", name: "Email" }])
+ expect(shouldDehydrateCommunityQuery({ queryKey: communityKeys.botPii("m1", "b1"), state: { status: "success", data: h.state() } })).toBe(false)
+ act(() => h.result.current.clearPreview())
+ expect(h.state()?.output).toBeUndefined()
+ expect(h.state()?.triggeredFilters).toBeUndefined()
+ await waitFor(() => expect(h.result.current.output).toBeUndefined())
+ expect(h.result.current.snapshot).toEqual(snapshot)
+ })
+
+ it("clears previous output before a new run and makes failed previews retryable", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ act(() => { void h.result.current.preview("first") })
+ await act(async () => handleBotPii({ botId: "b1", machineId: "m1", requestId: h.state()!.requestId, result: { status: "complete", snapshot, output: "first result" } }, { queryClient: h.client }))
+ await waitFor(() => expect(h.result.current.output).toBe("first result"))
+ act(() => { void h.result.current.preview("second") })
+ expect(h.state()?.output).toBeUndefined()
+ expect(h.state()?.triggeredFilters).toBeUndefined()
+ await waitFor(() => expect(h.result.current.output).toBeUndefined())
+ await act(async () => handleBotPii({ botId: "b1", machineId: "m1", requestId: h.state()!.requestId, result: { status: "error", code: "PII_PLUGIN_FAILED" } }, { queryClient: h.client }))
+ await waitFor(() => expect(h.result.current.previewError).toMatch(/could not process/))
+ expect(h.result.current.unavailable).toBeUndefined()
+ expect(h.result.current.output).toBeUndefined()
+ act(() => { void h.result.current.preview("retry") })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(4))
+ })
+
+ it("rejects oversize UTF-8 input and discards a cleared pending result", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ await act(async () => { expect(await h.result.current.preview("汉".repeat(342))).toBe(false) })
+ expect(apiFetch).toHaveBeenCalledTimes(1)
+ let run: Promise<boolean>
+ act(() => { run = h.result.current.preview("sample") })
+ const id = h.state()!.requestId
+ await act(async () => { h.result.current.clearPreview(); expect(await run!).toBe(false) })
+ await act(async () => handleBotPii({ botId: "b1", machineId: "m1", requestId: id, result: { status: "complete", snapshot, output: "discard me" } }, { queryClient: h.client }))
+ expect(h.result.current.output).toBeUndefined()
+ })
+
+ it("retires previews on disconnect without replaying test text", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ let run: Promise<boolean>
+ act(() => { run = h.result.current.preview("sample") })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ const id = h.state()!.requestId
+ h.rerender({ botId: "b1", machineId: "m1", online: false })
+ await act(async () => { expect(await run!).toBe(false) })
+ await act(async () => handleBotPii({ botId: "b1", machineId: "m1", requestId: id, result: { status: "complete", snapshot, output: "late" } }, { queryClient: h.client }))
+ expect(h.result.current.output).toBeUndefined()
+ h.rerender({ botId: "b1", machineId: "m1", online: true })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(3))
+ expect(JSON.parse(apiFetch.mock.calls[2][1].body).operation).toEqual({ action: "list" })
+ })
+
+ it("times out a preview without locking configuration or exposing an old result", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ vi.useFakeTimers()
+ try {
+ let run: Promise<boolean>
+ act(() => { run = h.result.current.preview("sample") })
+ await act(async () => { await vi.advanceTimersByTimeAsync(45_001); expect(await run!).toBe(false) })
+ expect(h.result.current.previewError).toMatch(/timed out/)
+ expect(h.result.current.output).toBeUndefined()
+ expect(h.result.current.unavailable).toBeUndefined()
+ } finally { vi.useRealTimers() }
+ })
+})
+
+
+describe("PII failure recovery", () => {
+ it("rejects callbacks from a retired bot view and ignores its late request failure", async () => {
+ let reject!: (error: Error) => void
+ apiFetch.mockImplementationOnce(() => new Promise((_, fail) => { reject = fail }))
+ const h = mount()
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(1))
+ const reload = h.result.current.reload
+ h.rerender({ botId: "b2", machineId: "m2", online: true })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ await act(async () => {
+ expect(await reload()).toBe(false)
+ reject(new Error("NETWORK_ERROR"))
+ })
+ expect(apiFetch).toHaveBeenCalledTimes(2)
+ expect(h.state()).toBeUndefined()
+ expect(h.state("b2", "m2")?.status).toBe("pending")
+ expect(h.result.current.error).toBeUndefined()
+ })
+
+ it.each([
+ [new Error("MACHINE_OFFLINE"), "This machine is offline. Reconnect and reload filters."],
+ ["network rejected", "Could not load filters. Reload to try again."],
+ ])("keeps failed loading retryable for %s", async (failure, message) => {
+ apiFetch.mockRejectedValueOnce(failure)
+ const h = mount()
+ await waitFor(() => expect(h.result.current.error).toBe(message))
+ expect(h.result.current.snapshot).toBeUndefined()
+ await act(async () => { expect(await h.result.current.remove("email")).toBe(false) })
+ act(() => { void h.result.current.reload() })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ expect(h.result.current.error).toBeUndefined()
+ })
+
+ it.each(["list", "remove"] as const)("requires reload after a %s timeout", async (action) => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ vi.useFakeTimers()
+ try {
+ let run!: Promise<boolean>
+ act(() => { run = action === "list" ? h.result.current.reload() : h.result.current.remove("email") })
+ const id = h.state()!.requestId
+ await act(async () => { await vi.advanceTimersByTimeAsync(45_001); expect(await run).toBe(false) })
+ expect(h.result.current.error).toBe(action === "list"
+ ? "Could not load filters. Reload to try again."
+ : "Could not confirm the change. Reload filters before making another change.")
+ expect(h.result.current.unavailable).toBe(action === "list" ? undefined : "Reload filters to check the current state.")
+ await h.reply(id)
+ expect(h.state()?.status).toBe("error")
+ expect(h.result.current.snapshot).toEqual(snapshot)
+ if (action === "remove") await act(async () => { expect(await h.result.current.remove("email")).toBe(false) })
+ } finally { vi.useRealTimers() }
+ })
+
+ it("blocks management while the connection is lost and reloads on reconnect", async () => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ const ws = getCommunityDbRegistry(h.client)!.runtime.ws
+ act(() => ws.setState(state => ({ ...state, connectionStatus: "reconnecting" })))
+ await waitFor(() => expect(h.result.current.unavailable).toBe("Connection lost. Reconnect to manage filters."))
+ await act(async () => { expect(await h.result.current.reload()).toBe(false) })
+ expect(apiFetch).toHaveBeenCalledTimes(1)
+ act(() => ws.setState(state => ({ ...state, connectionStatus: "connected" })))
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2))
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.unavailable).toBeUndefined())
+ })
+
+ it.each(["PII_PLUGIN_NOT_FOUND", "PII_PLUGIN_FAILED"] as const)("reports %s without claiming a mutation succeeded", async (code) => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ let run!: Promise<boolean>
+ act(() => { run = h.result.current.remove("email") })
+ await act(async () => {
+ handleBotPii({ botId: "b1", machineId: "m1", requestId: h.state()!.requestId, result: { status: "error", code } }, { queryClient: h.client })
+ expect(await run).toBe(false)
+ })
+ await waitFor(() => expect(h.result.current.error).toBe(code === "PII_PLUGIN_NOT_FOUND"
+ ? "This plugin was removed. Reload filters."
+ : "Filters are unavailable. Reload to try again."))
+ expect(h.result.current.snapshot).toEqual(snapshot)
+ expect(h.result.current.unavailable).toMatch(/Reload filters/)
+ })
+
+ it.each(["request", "missing output"])("allows preview retry after %s failure", async (failure) => {
+ const h = mount()
+ await h.reply(h.state()!.requestId)
+ await waitFor(() => expect(h.result.current.snapshot).toEqual(snapshot))
+ if (failure === "request") apiFetch.mockRejectedValueOnce(new Error("NETWORK_ERROR"))
+ let run!: Promise<boolean>
+ act(() => { run = h.result.current.preview("sample") })
+ if (failure === "missing output") await h.reply(h.state()!.requestId)
+ await act(async () => { expect(await run).toBe(false) })
+ await waitFor(() => expect(h.result.current.previewError).toBe(failure === "request"
+ ? "Could not run the test. Try again."
+ : "Could not read the test result. Run it again."))
+ expect(h.result.current.output).toBeUndefined()
+ expect(h.result.current.unavailable).toBeUndefined()
+ act(() => { void h.result.current.preview("retry") })
+ await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(3))
+ })
+})
diff --git a/src/web/src/hooks/community/use-bot-pii.ts b/src/web/src/hooks/community/use-bot-pii.ts
new file mode 100644
index 000000000..8b42363b3
--- /dev/null
+++ b/src/web/src/hooks/community/use-bot-pii.ts
@@ -0,0 +1,105 @@
+"use client"
+
+import { useCallback, useEffect, useMemo } from "react"
+import { skipToken, useQuery, useQueryClient } from "@tanstack/react-query"
+import { PII_TEXT_MAX_BYTES, utf8ByteLength, type PiiOperation, type PiiSnapshot } from "@alook/shared"
+import { communityKeys } from "@/lib/query-keys"
+import { useCommunityWsStore } from "@/stores/community/ws"
+import { useCommunityMutationOrigin } from "./community-origin"
+import { useCommunityViewSource } from "./use-community-view-source"
+
+export type BotPiiState = {
+ requestId: string
+ status: "pending" | "complete" | "error"
+ snapshot?: PiiSnapshot
+ error?: string
+ uncertain?: boolean
+ operation?: PiiOperation["action"]
+ output?: string
+ triggeredFilters?: { id: string; name: string }[]
+}
+
+export function useBotPii(botId: string, machineId: string, online: boolean) {
+ const client = useQueryClient()
+ const origin = useCommunityMutationOrigin()
+ const connected = useCommunityWsStore(state => state.connectionStatus === "connected")
+ const source = useCommunityViewSource(`pii:${machineId}:${botId}`, online && connected)
+ const key = useMemo(() => communityKeys.botPii(machineId, botId), [machineId, botId])
+ const { data } = useQuery<BotPiiState>({ queryKey: key, queryFn: skipToken, gcTime: 0 })
+
+ const run = useCallback(async (operation: PiiOperation): Promise<boolean> => {
+ if (!online || !connected || client.getQueryData<BotPiiState>(key)?.status === "pending") return false
+ const assert = source.capture()
+ const { token } = origin.begin()
+ try { assert() } catch { return false }
+ const requestId = crypto.randomUUID()
+ const previous = client.getQueryData<BotPiiState>(key)
+ client.setQueryData<BotPiiState>(key, { requestId, status: "pending", operation: operation.action, snapshot: previous?.snapshot })
+ return new Promise<boolean>(resolve => {
+ const complete = (success: boolean) => { clearTimeout(timer); unsubscribe(); assert.signal.removeEventListener("abort", cancel); resolve(success) }
+ const fail = (error: string) => {
+ try { assert() } catch { complete(false); return }
+ const current = client.getQueryData<BotPiiState>(key)
+ if (current?.requestId === requestId && current.status === "pending") {
+ client.setQueryData<BotPiiState>(key, { ...current, status: "error", error, uncertain: operation.action === "setEnabled" || operation.action === "remove" })
+ }
+ complete(false)
+ }
+ const cancel = () => {
+ complete(false)
+ if (client.getQueryData<BotPiiState>(key)?.requestId === requestId) client.removeQueries({ queryKey: key, exact: true })
+ }
+ const unsubscribe = client.getQueryCache().subscribe(() => {
+ const current = client.getQueryData<BotPiiState>(key)
+ if (current?.requestId !== requestId) complete(false)
+ else if (current.status !== "pending") complete(current.status === "complete")
+ })
+ const timer = setTimeout(() => fail(operation.action === "preview"
+ ? "The test timed out. Run it again when your machine is available."
+ : operation.action === "list"
+ ? "Could not load filters. Reload to try again."
+ : "Could not confirm the change. Reload filters before making another change."), 45_000)
+ assert.signal.addEventListener("abort", cancel, { once: true })
+ void origin.request(token, `/api/community/bots/${encodeURIComponent(botId)}/pii`, {
+ method: "POST", body: JSON.stringify({ requestId, operation }), signal: assert.signal, assertActive: assert,
+ }).catch((error: unknown) => {
+ const code = error instanceof Error ? error.message : ""
+ fail(code === "UPGRADE_REQUIRED" ? "Update this machine’s daemon to manage PII filters."
+ : code === "MACHINE_OFFLINE" ? "This machine is offline. Reconnect and reload filters."
+ : operation.action === "preview" ? "Could not run the test. Try again."
+ : operation.action === "list" ? "Could not load filters. Reload to try again."
+ : "Could not confirm the change. Reload filters before making another change.")
+ })
+ })
+ }, [botId, client, connected, key, online, origin, source])
+
+ useEffect(() => {
+ if (online && connected) void run({ action: "list" })
+ }, [run, online, connected])
+
+ const clearPreview = useCallback(() => {
+ const current = client.getQueryData<BotPiiState>(key)
+ if (current?.operation === "preview") client.setQueryData<BotPiiState>(key, { requestId: "", status: "complete", snapshot: current.snapshot })
+ }, [client, key])
+
+ return {
+ snapshot: data?.snapshot,
+ pending: data?.status === "pending",
+ error: data?.operation === "preview" ? undefined : data?.error,
+ previewError: data?.operation === "preview" ? data.error : undefined,
+ output: online && connected && data?.operation === "preview" ? data.output : undefined,
+ triggeredFilters: online && connected && data?.operation === "preview" ? data.triggeredFilters : undefined,
+ previewPending: data?.operation === "preview" && data.status === "pending",
+ clearPreview,
+ preview: (text: string) => data?.snapshot && !data.uncertain && text.trim() && utf8ByteLength(text) <= PII_TEXT_MAX_BYTES
+ ? run({ action: "preview", text, expectedRevision: data.snapshot.revision }) : Promise.resolve(false),
+ unavailable: !online ? "This machine is offline. Reconnect to manage filters."
+ : !connected ? "Connection lost. Reconnect to manage filters."
+ : data?.uncertain ? "Reload filters to check the current state." : undefined,
+ reload: () => run({ action: "list" }),
+ setEnabled: (pluginId: string, enabled: boolean) => data?.snapshot && !data.uncertain
+ ? run({ action: "setEnabled", pluginId, enabled, expectedRevision: data.snapshot.revision }) : Promise.resolve(false),
+ remove: (pluginId: string) => data?.snapshot && !data.uncertain
+ ? run({ action: "remove", pluginId, expectedRevision: data.snapshot.revision }) : Promise.resolve(false),
+ }
+}
diff --git a/src/web/src/hooks/community/use-me-route-memory.ts b/src/web/src/hooks/community/use-me-route-memory.ts
index d28c5a869..e17b5beaa 100644
--- a/src/web/src/hooks/community/use-me-route-memory.ts
+++ b/src/web/src/hooks/community/use-me-route-memory.ts
@@ -8,7 +8,7 @@ import { commitLastCommunityRoute } from "@/lib/community/last-community-route"
export function useMeRouteMemory(pathname: string, active: boolean) {
const currentUser = useCurrentUser()
useEffect(() => {
- if (!active || !["/c/me/friends", "/c/me/machines", "/c/me/bots"].includes(pathname)
+ if (!active || !["/c/me/friends", "/c/me/machines", "/c/me/bots", "/c/me/privacy"].includes(pathname)
|| !isRememberableMeLocation(pathname)) return
setLastMeLocation(pathname)
commitLastCommunityRoute(currentUser.id, pathname)
diff --git a/src/web/src/hooks/community/use-start-bot-chat.ts b/src/web/src/hooks/community/use-start-bot-chat.ts
new file mode 100644
index 000000000..1de8fae8c
--- /dev/null
+++ b/src/web/src/hooks/community/use-start-bot-chat.ts
@@ -0,0 +1,55 @@
+"use client"
+
+import { useContext, useRef } from "react"
+import { useAtom, useCreateAtom } from "@tanstack/react-store"
+import { useCurrentUser } from "@/contexts/community/current-user"
+import { CommunityRouteContext } from "@/contexts/community/route-frame"
+import { useCommunityRouter } from "@/contexts/community/module-route"
+import { useCreateOrGetDm } from "@/hooks/community/mutations"
+import { useDmMessageSender } from "@/hooks/community/use-dm-message-sender"
+import type { useCommunityViewSource } from "@/hooks/community/use-community-view-source"
+import { useUiHandlers } from "@/stores/community"
+import { toastApiError } from "@/lib/api/client"
+
+export function useStartBotChat(source: ReturnType<typeof useCommunityViewSource>) {
+ const currentUser = useCurrentUser()
+ const createOrGetDm = useCreateOrGetDm()
+ const { accept } = useDmMessageSender()
+ const router = useCommunityRouter()
+ const navigation = useContext(CommunityRouteContext)?.navigation
+ const uiHandlers = useUiHandlers()
+ const inFlight = useRef(false)
+ const [pending, setPending] = useAtom(useCreateAtom(false))
+
+ const start = async ({ botId, content, onAccepted, errorMessage }: { botId: string; content: string; onAccepted?: () => void; errorMessage: string }) => {
+ if (inFlight.current) return
+ const assert = source.capture()
+ inFlight.current = true
+ setPending(true)
+ try {
+ assert()
+ uiHandlers.cancelPendingNavigation?.()
+ const intentCurrent = navigation?.captureIntent() ?? (() => true)
+ const data = await createOrGetDm.mutateAsync({ userId: botId, assertActive: assert })
+ assert()
+ if (!intentCurrent()) throw new DOMException("Retired bot chat navigation", "AbortError")
+ const receipt = accept({
+ dmId: data.conversation.id,
+ content,
+ author: { id: currentUser.id, name: currentUser.name, avatar: currentUser.avatar },
+ assertActive: assert,
+ })
+ if (!receipt.accepted) throw new Error("Failed to send message")
+ onAccepted?.()
+ router.push(`/c/me/${data.conversation.id}`)
+ await receipt.committed
+ } catch (error) {
+ toastApiError(error, errorMessage, assert)
+ } finally {
+ inFlight.current = false
+ setPending(false)
+ }
+ }
+
+ return { start, pending }
+}
diff --git a/src/web/src/lib/community/community-route.test.ts b/src/web/src/lib/community/community-route.test.ts
index 151387a81..84cfe3863 100644
--- a/src/web/src/lib/community/community-route.test.ts
+++ b/src/web/src/lib/community/community-route.test.ts
@@ -31,6 +31,7 @@ describe("community route", () => {
["/c/me/friends", "me-friends", "detail", "me", "friends"],
["/c/me/machines", "me-machines", "detail", "me", "machines"],
["/c/me/bots", "me-bots", "detail", "me", "bots"],
+ ["/c/me/privacy", "me-privacy", "detail", "me", "privacy"],
["/c/me/dm_1", "dm-detail", "detail", "me", "dm"],
["/c/channels/s1", "server-root", "list", "server", "server-landing"],
["/c/channels/s1/c1", "server-detail", "detail", "server", "server-conversation"],
diff --git a/src/web/src/lib/community/community-route.ts b/src/web/src/lib/community/community-route.ts
index c26bfb09c..a6b3282f2 100644
--- a/src/web/src/lib/community/community-route.ts
+++ b/src/web/src/lib/community/community-route.ts
@@ -7,6 +7,7 @@ type CommunityModuleRoute =
| "me-friends"
| "me-machines"
| "me-bots"
+ | "me-privacy"
| "dm-detail"
| "message-resolution"
| "server-root"
@@ -20,6 +21,7 @@ type CommunityModuleMain =
| { kind: "friends" }
| { kind: "machines" }
| { kind: "bots" }
+ | { kind: "privacy" }
| { kind: "dm"; dmId: string }
| { kind: "server-landing"; serverId: string }
| { kind: "server-conversation"; serverId: string; leafId: string }
@@ -72,7 +74,7 @@ export type CommunityCheckpointPlan = {
| { kind: "keep" }
| { kind: "server-skeleton"; serverId: string }
| { kind: "me-skeleton" }
- main: { kind: "keep" } | { kind: "server-route"; href: string } | { kind: "module-route"; href: string; module: "friends" | "bots" | "machines" | "me-root" | "server-landing" | "route-resolution" } | { kind: "dm-route"; href: string; dmId: string } | {
+ main: { kind: "keep" } | { kind: "server-route"; href: string } | { kind: "module-route"; href: string; module: "friends" | "bots" | "privacy" | "machines" | "me-root" | "server-landing" | "route-resolution" } | { kind: "dm-route"; href: string; dmId: string } | {
kind: "target-skeleton"
href: string
}
@@ -175,6 +177,9 @@ export function resolveCommunityModulePlan(href: string): CommunityModulePlan {
main: { kind: "machines" },
}
}
+ if (leaf === "privacy") {
+ return { route: "me-privacy", surface: "detail", rail: "community", sidebar: { kind: "me" }, main: { kind: "privacy" } }
+ }
if (leaf === "bots") {
return {
route: "me-bots",
diff --git a/src/web/src/lib/community/last-community-route.ts b/src/web/src/lib/community/last-community-route.ts
index ead9b9c5d..ba56b71d6 100644
--- a/src/web/src/lib/community/last-community-route.ts
+++ b/src/web/src/lib/community/last-community-route.ts
@@ -31,6 +31,7 @@ export function canonicalCommunityLeafPathname(href: string): string | null {
if (plan.route === "me-friends") return "/c/me/friends"
if (plan.route === "me-machines") return "/c/me/machines"
+ if (plan.route === "me-privacy") return "/c/me/privacy"
if (plan.route === "me-bots") return "/c/me/bots"
if (plan.main.kind === "dm") {
return `/c/me/${encodeURIComponent(plan.main.dmId)}`
diff --git a/src/web/src/lib/observability/coverage.ts b/src/web/src/lib/observability/coverage.ts
index 489ce492e..15fbc3f9e 100644
--- a/src/web/src/lib/observability/coverage.ts
+++ b/src/web/src/lib/observability/coverage.ts
@@ -30,6 +30,7 @@ export const pageRoutes = [
] as const
export const apiRoutes = [
+ "/api/community/bots/[id]/pii",
"/api/auth/[...all]",
"/api/auth/native/attempt",
"/api/auth/native/cancel",
diff --git a/src/web/src/lib/query-keys.ts b/src/web/src/lib/query-keys.ts
index 8497bc4c3..d15cc0469 100644
--- a/src/web/src/lib/query-keys.ts
+++ b/src/web/src/lib/query-keys.ts
@@ -144,6 +144,7 @@ export const communityKeys = {
// ── Bots ────────────────────────────────────────────────────────────────
bots: () => [...communityKeys.all, "bots"] as const,
+ botPii: (machineId: string, botId: string) => [...communityKeys.all, "pii", machineId, botId] as const,
billing: () => [...communityKeys.all, "billing"] as const,
bugReport: (reportId: string) =>
[...communityKeys.all, "bug-report", reportId] as const,
diff --git a/src/ws-do/src/community-browser-event-ingress.test.ts b/src/ws-do/src/community-browser-event-ingress.test.ts
index ede330568..49087d734 100644
--- a/src/ws-do/src/community-browser-event-ingress.test.ts
+++ b/src/ws-do/src/community-browser-event-ingress.test.ts
@@ -69,9 +69,9 @@ function worstEscapingTarget(index: number) {
}
describe("community browser event ingress", () => {
- it("normalizes all 48 current fixtures without a contract version field", () => {
+ it("normalizes all 49 current fixtures without a contract version field", () => {
const fixtures = Object.values(communityWsEventFixtures)
- expect(fixtures).toHaveLength(48)
+ expect(fixtures).toHaveLength(49)
for (const fixture of fixtures) {
const normalized = normalizeCommunityBrowserEvent(fixture)
expect(normalized).toMatchObject({ ok: true, event: fixture, envelope: fixture })
diff --git a/src/ws-do/src/community-content-access.ts b/src/ws-do/src/community-content-access.ts
index b148fb667..74cdb3ec9 100644
--- a/src/ws-do/src/community-content-access.ts
+++ b/src/ws-do/src/community-content-access.ts
@@ -49,6 +49,7 @@ export const communityContentAccessKinds = {
"community:machine.status": "control",
"community:machine.updated": "control",
"community:runtime.models": "control",
+ "community:bot.pii": "control",
"community:machine.removed": "control",
"community:bot.audit_event": "control",
} as const satisfies Record<CommunityWsEvent["type"], AccessKind>
diff --git a/src/ws-do/src/index.test.ts b/src/ws-do/src/index.test.ts
index a78bde484..43fe7db5d 100644
--- a/src/ws-do/src/index.test.ts
+++ b/src/ws-do/src/index.test.ts
@@ -84,6 +84,7 @@ describe("ws-do router", () => {
"handleAuditBroadcast",
"handleBatchPresence",
"handleSinglePresence",
+ "handleMachinePii",
"handleMachineModels",
"handleMachinePush",
"handleMachineWake",
diff --git a/src/ws-do/src/index.ts b/src/ws-do/src/index.ts
index 39336c0d5..0d334762c 100644
--- a/src/ws-do/src/index.ts
+++ b/src/ws-do/src/index.ts
@@ -1,3 +1,4 @@
+import { handleMachinePii } from "./routes/community-machine-pii"
import { handleMachineModels } from "./routes/community-machine-models"
import { handleAuditBroadcast } from "./routes/audit-broadcast"
import {
@@ -67,6 +68,8 @@ export default {
if (response) return response
response = await handleSinglePresence(context)
if (response) return response
+ response = await handleMachinePii(context)
+ if (response) return response
response = await handleMachineModels(context)
if (response) return response
response = await handleMachinePush(context)
diff --git a/src/ws-do/src/routes/community-machine-pii.test.ts b/src/ws-do/src/routes/community-machine-pii.test.ts
new file mode 100644
index 000000000..2589cc871
--- /dev/null
+++ b/src/ws-do/src/routes/community-machine-pii.test.ts
@@ -0,0 +1,25 @@
+import { beforeEach, describe, expect, it, vi } from "vitest"
+import { createRouterTestContext, getSharedMocks, loadRouter } from "./test-harness"
+const mocks = getSharedMocks()
+const command = { type: "pii:manage", requestId: "request", botId: "bot", operation: { action: "list" } }
+const request = (body: unknown = command) => new Request("http://localhost/community-machine/by-id/machine/pii", { method: "POST", body: JSON.stringify(body) })
+describe("PII control routing", () => {
+ beforeEach(() => { vi.clearAllMocks(); mocks.getActiveDoNamesForMachine.mockResolvedValue(["credential"]) })
+ it("forwards only validated metadata operations to the current machine DO", async () => {
+ const { env, doMock } = createRouterTestContext()
+ doMock.stubFetch.mockResolvedValue(Response.json({ sent: 1, unsupported: 0 }))
+ const response = await (await loadRouter()).fetch(request(), { ...env, DB: {} as D1Database })
+ expect(await response.json()).toEqual({ sent: 1, unsupported: 0 })
+ expect(doMock.idFromName).toHaveBeenCalledWith("community-machine:credential")
+ const forwarded = doMock.stubFetch.mock.calls[0]![0] as Request
+ expect(new URL(forwarded.url).pathname).toBe("/pii")
+ expect(await forwarded.json()).toEqual(command)
+ })
+ it("rejects source/add fields and corrupt receipts", async () => {
+ const { env, doMock } = createRouterTestContext()
+ expect((await (await loadRouter()).fetch(request({ ...command, source: "private" }), { ...env, DB: {} as D1Database })).status).toBe(400)
+ expect(doMock.stubFetch).not.toHaveBeenCalled()
+ doMock.stubFetch.mockResolvedValue(Response.json({ sent: "1", unsupported: 0 }))
+ expect((await (await loadRouter()).fetch(request(), { ...env, DB: {} as D1Database })).status).toBe(503)
+ })
+})
diff --git a/src/ws-do/src/routes/community-machine-pii.ts b/src/ws-do/src/routes/community-machine-pii.ts
new file mode 100644
index 000000000..ac8333c1f
--- /dev/null
+++ b/src/ws-do/src/routes/community-machine-pii.ts
@@ -0,0 +1,29 @@
+import { createDb, PII_REQUEST_MAX_BYTES, PiiCommandSchema, queries } from "@alook/shared"
+import { readBoundedJsonRequest } from "../community-browser-event-ingress"
+import type { RouterContext } from "../router-context"
+
+export async function handleMachinePii({ request, env, url }: RouterContext): Promise<Response | null> {
+ const match = url.pathname.match(/^\/community-machine\/by-id\/([^/]+)\/pii$/)
+ if (!match || request.method !== "POST") return null
+ const input = await readBoundedJsonRequest(request, PII_REQUEST_MAX_BYTES)
+ const payload = PiiCommandSchema.safeParse(input.ok ? input.value : null)
+ if (!payload.success) return Response.json({ error: "invalid request" }, { status: 400 })
+ try {
+ const doNames = await queries.communityMachine.getActiveDoNamesForMachine(createDb(env.DB), decodeURIComponent(match[1]))
+ let sent = 0
+ let unsupported = 0
+ for (const name of doNames) {
+ const stub = env.WS_DO.get(env.WS_DO.idFromName("community-machine:" + name))
+ const response = await stub.fetch(new Request("http://internal/pii", {
+ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload.data),
+ }))
+ if (!response.ok) throw 0
+ const receipt = await response.json() as { sent?: unknown; unsupported?: unknown }
+ if (!Number.isSafeInteger(receipt.sent) || (receipt.sent as number) < 0
+ || !Number.isSafeInteger(receipt.unsupported) || (receipt.unsupported as number) < 0) throw 0
+ sent += receipt.sent as number
+ unsupported += receipt.unsupported as number
+ }
+ return Response.json({ sent, unsupported })
+ } catch { return Response.json({ error: "PII dispatch unavailable" }, { status: 503 }) }
+}
diff --git a/src/ws-do/src/ws-durable/internal.ts b/src/ws-do/src/ws-durable/internal.ts
index fbb02b8b9..7adba1b08 100644
--- a/src/ws-do/src/ws-durable/internal.ts
+++ b/src/ws-do/src/ws-durable/internal.ts
@@ -29,6 +29,7 @@ export type ConnectionState =
machineId: string
userId: string
authenticated: boolean
+ pii?: boolean
modelDiscovery?: boolean
machineStop?: boolean
controlHeartbeat?: boolean
diff --git a/src/ws-do/src/ws-durable/machine-control.test.ts b/src/ws-do/src/ws-durable/machine-control.test.ts
index 63f173156..b5c641db0 100644
--- a/src/ws-do/src/ws-durable/machine-control.test.ts
+++ b/src/ws-do/src/ws-durable/machine-control.test.ts
@@ -55,6 +55,22 @@ describe("WebSocketDurableObject", () => {
beforeEach(() => resetHarness())
afterEach(() => cleanupHarness())
+ it("forwards preview input only to PII-capable machines without persisting it", async () => {
+ const { durable, getWebSockets, storage, store } = createDO()
+ const supported = createMockWebSocket()
+ const legacy = createMockWebSocket()
+ supported.serializeAttachment({ type: "community-machine", authenticated: true, machineId: "machine", userId: "owner", pii: true })
+ legacy.serializeAttachment({ type: "community-machine", authenticated: true, machineId: "machine", userId: "owner" })
+ getWebSockets.mockReturnValue([supported, legacy])
+ const command = { type: "pii:manage", botId: "bot", requestId: "preview", operation: { action: "preview", text: "private-input-fixture", expectedRevision: 1 } }
+ const response = await durable.fetch(new Request("http://internal/pii", { method: "POST", body: JSON.stringify(command) }))
+ expect(await response.json()).toEqual({ sent: 1, unsupported: 1 })
+ expect(JSON.parse(supported.send.mock.calls[0]![0] as string)).toEqual(command)
+ expect(legacy.send).not.toHaveBeenCalled()
+ expect(storage.put).not.toHaveBeenCalled()
+ expect(JSON.stringify([...store])).not.toContain(command.operation.text)
+ })
+
describe("machine Stop dispatch", () => {
it("sends only to authenticated stop-capable machines without force-closing or clearing state", async () => {
const { durable, store, getWebSockets } = createDO()
diff --git a/src/ws-do/src/ws-durable/machine-control.ts b/src/ws-do/src/ws-durable/machine-control.ts
index 7f356ecb3..64e2a1b90 100644
--- a/src/ws-do/src/ws-durable/machine-control.ts
+++ b/src/ws-do/src/ws-durable/machine-control.ts
@@ -1,5 +1,5 @@
import { readBoundedJsonRequest } from "../community-browser-event-ingress"
-import { createDb, DiagnosticCollectCommandSchema, RuntimeModelDiscoveryCommandSchema, queries } from "@alook/shared"
+import { createDb, PII_REQUEST_MAX_BYTES, PiiCommandSchema, DiagnosticCollectCommandSchema, RuntimeModelDiscoveryCommandSchema, queries } from "@alook/shared"
import {
HANDLE_KEY,
IDENTITY_KEY,
@@ -42,6 +42,13 @@ export async function handleMachineControlFetch(
return new Response(JSON.stringify({ sent, unsupported }), { headers: { "Content-Type": "application/json" } })
}
+ if (url.pathname === "/pii" && request.method === "POST") {
+ const input = await readBoundedJsonRequest(request, PII_REQUEST_MAX_BYTES)
+ const command = PiiCommandSchema.safeParse(input.ok ? input.value : null)
+ if (!command.success) return new Response(JSON.stringify({ error: "invalid request" }), { status: 400, headers: { "Content-Type": "application/json" } })
+ return new Response(JSON.stringify(forwardToCommunityMachine(context, JSON.stringify(command.data), "pii")), { headers: { "Content-Type": "application/json" } })
+ }
+
if (url.pathname === "/stop" && request.method === "POST") {
const result = forwardToCommunityMachine(context, JSON.stringify({ type: "machine:stop" }), "machineStop")
return new Response(JSON.stringify(result), { headers: { "Content-Type": "application/json" } })
@@ -277,7 +284,7 @@ function rejectCommunityMachine(ws: WebSocket, reason: string): void {
try { ws.close(1008, "Unauthorized") } catch { /* ok */ }
}
-function forwardToCommunityMachine(context: WsDurableContext, message: string, capability?: "modelDiscovery" | "machineStop"): { sent: number; unsupported: number } {
+function forwardToCommunityMachine(context: WsDurableContext, message: string, capability?: "modelDiscovery" | "machineStop" | "pii"): { sent: number; unsupported: number } {
let sent = 0
let unsupported = 0
for (const ws of context.ctx.getWebSockets()) {
diff --git a/src/ws-do/src/ws-durable/machine-frames.ts b/src/ws-do/src/ws-durable/machine-frames.ts
index 8360c4aed..0b522d55f 100644
--- a/src/ws-do/src/ws-durable/machine-frames.ts
+++ b/src/ws-do/src/ws-durable/machine-frames.ts
@@ -1,3 +1,4 @@
+import { handlePiiResultFrame } from "./machine-pii"
import { handleRuntimeModelsFrame } from "./machine-models"
import { IDENTITY_KEY } from "./internal"
import type { CommunityMachineIdentity, WsDurableContext } from "./internal"
@@ -77,6 +78,7 @@ export async function handleCommunityMachineMessage(
parsed,
identity,
)) return
+ if (await handlePiiResultFrame(context, parsed, identity)) return
if (await handleRuntimeModelsFrame(context, parsed, identity)) return
await handleReadyFrame(
context,
diff --git a/src/ws-do/src/ws-durable/machine-pii.test.ts b/src/ws-do/src/ws-durable/machine-pii.test.ts
new file mode 100644
index 000000000..d08671c53
--- /dev/null
+++ b/src/ws-do/src/ws-durable/machine-pii.test.ts
@@ -0,0 +1,51 @@
+import { beforeEach, describe, expect, it, vi } from "vitest"
+import type { CommunityMachineIdentity, WsDurableContext } from "./internal"
+const mocks = vi.hoisted(() => ({ bot: vi.fn(), epoch: vi.fn(), notify: vi.fn() }))
+vi.mock("@alook/shared", async original => ({
+ ...await original<typeof import("@alook/shared")>(), createDb: () => ({}),
+ queries: { communityBot: { getBotOwnedBy: mocks.bot }, communityMachineSession: { transitionMachineSessionEpoch: mocks.epoch } },
+}))
+vi.mock("./presence-typing", () => ({ notifyUserDO: mocks.notify }))
+import { handlePiiResultFrame } from "./machine-pii"
+const identity: CommunityMachineIdentity = { userId: "owner", machineId: "machine", credentialHash: "synthetic-hash" }
+const context = { env: { DB: {} }, log: { warn: vi.fn() } } as unknown as WsDurableContext
+const frame = { type: "pii_result", botId: "bot", requestId: "request", result: { status: "complete", snapshot: { revision: 1, plugins: [] } } }
+describe("PII results", () => {
+ beforeEach(() => { vi.clearAllMocks(); mocks.bot.mockResolvedValue({ machineId: "machine" }); mocks.epoch.mockResolvedValue({ type: "transitioned" }) })
+ it("checks current owner, binding and credential epoch before routing metadata only to the owner", async () => {
+ expect(await handlePiiResultFrame(context, frame, identity)).toBe(true)
+ expect(mocks.bot).toHaveBeenCalledWith({}, "bot", "owner")
+ expect(mocks.epoch).toHaveBeenCalledWith({}, { type: "renew", epoch: identity })
+ expect(mocks.notify).toHaveBeenCalledWith(context, "owner", { ...frame, type: "community:bot.pii", machineId: "machine" })
+ })
+ it("drops removed/rebound bots and revoked epochs", async () => {
+ for (const bot of [null, { machineId: "other" }]) {
+ mocks.bot.mockResolvedValue(bot)
+ expect(await handlePiiResultFrame(context, frame, identity)).toBe(true)
+ }
+ expect(mocks.epoch).not.toHaveBeenCalled()
+ mocks.bot.mockResolvedValue({ machineId: "machine" })
+ mocks.epoch.mockResolvedValue({ type: "stale_epoch" })
+ await handlePiiResultFrame(context, frame, identity)
+ expect(mocks.notify).not.toHaveBeenCalled()
+ })
+ it("rejects results carrying source or invalid schema without querying or notifying", async () => {
+ const result = { status: "complete", snapshot: { revision: 1, plugins: [{ id: "p", name: "p", description: "d", enabled: true, source: "private code" }] } }
+ await handlePiiResultFrame(context, { ...frame, result }, identity)
+ await handlePiiResultFrame(context, { ...frame, requestId: "" }, identity)
+ expect(mocks.bot).not.toHaveBeenCalled()
+ expect(mocks.notify).not.toHaveBeenCalled()
+ })
+ it("contains owner-notification failures without logging preview text or raw exceptions", async () => {
+ mocks.notify.mockRejectedValueOnce(new Error("PRIVATE_EXCEPTION"))
+ const preview = { ...frame, result: { ...frame.result, output: "PRIVATE_SAMPLE" } }
+ expect(await handlePiiResultFrame(context, preview, identity)).toBe(true)
+ expect(context.log.warn).toHaveBeenCalledWith("PII result unavailable", { machineId: "machine" })
+ expect(JSON.stringify(vi.mocked(context.log.warn).mock.calls)).not.toContain("PRIVATE_")
+ })
+ it("relays bounded preview output with the captured revision through the same owner checks", async () => {
+ const preview = { ...frame, result: { ...frame.result, output: "filtered-output", triggeredFilters: [{ id: "email", name: "Email addresses" }] } }
+ await handlePiiResultFrame(context, preview, identity)
+ expect(mocks.notify).toHaveBeenCalledWith(context, "owner", { ...preview, type: "community:bot.pii", machineId: "machine" })
+ })
+})
diff --git a/src/ws-do/src/ws-durable/machine-pii.ts b/src/ws-do/src/ws-durable/machine-pii.ts
new file mode 100644
index 000000000..80afbd12e
--- /dev/null
+++ b/src/ws-do/src/ws-durable/machine-pii.ts
@@ -0,0 +1,18 @@
+import { createDb, PiiResultFrameSchema, queries, WS_EVENTS } from "@alook/shared"
+import type { CommunityMachineIdentity, WsDurableContext } from "./internal"
+import { notifyUserDO } from "./presence-typing"
+
+export async function handlePiiResultFrame(context: WsDurableContext, parsed: unknown, identity: CommunityMachineIdentity): Promise<boolean> {
+ if (!parsed || typeof parsed !== "object" || (parsed as { type?: unknown }).type !== "pii_result") return false
+ const input = PiiResultFrameSchema.safeParse(parsed)
+ if (!input.success) return true
+ try {
+ const db = createDb(context.env.DB)
+ const bot = await queries.communityBot.getBotOwnedBy(db, input.data.botId, identity.userId)
+ if (bot?.machineId !== identity.machineId) return true
+ const epoch = await queries.communityMachineSession.transitionMachineSessionEpoch(db, { type: "renew", epoch: identity })
+ if (epoch.type === "stale_epoch") return true
+ await notifyUserDO(context, identity.userId, { ...input.data, type: WS_EVENTS.BOT_PII, machineId: identity.machineId })
+ } catch { context.log.warn("PII result unavailable", { machineId: identity.machineId }) }
+ return true
+}
diff --git a/src/ws-do/src/ws-durable/machine-ready.ts b/src/ws-do/src/ws-durable/machine-ready.ts
index 8c8a45d26..83ba81361 100644
--- a/src/ws-do/src/ws-durable/machine-ready.ts
+++ b/src/ws-do/src/ws-durable/machine-ready.ts
@@ -1,3 +1,4 @@
+import { PII_CAPABILITY } from "@alook/shared"
import {
CONTROL_HEARTBEAT_CAPABILITY,
MACHINE_STOP_CAPABILITY,
@@ -96,6 +97,7 @@ export async function handleReadyFrame(
ws.serializeAttachment({
...state,
controlHeartbeat,
+ pii: ready.capabilities.includes(PII_CAPABILITY),
modelDiscovery: ready.capabilities.includes(RUNTIME_MODEL_DISCOVERY_CAPABILITY),
machineStop: ready.capabilities.includes(MACHINE_STOP_CAPABILITY),
...(controlHeartbeat ? { lastHeartbeatAckAt: Date.now() } : {}),
diff --git a/src/ws-do/src/ws-durable/user-auth.test.ts b/src/ws-do/src/ws-durable/user-auth.test.ts
index bacffc3e8..14f1aa527 100644
--- a/src/ws-do/src/ws-durable/user-auth.test.ts
+++ b/src/ws-do/src/ws-durable/user-auth.test.ts
@@ -69,6 +69,26 @@ describe("WebSocketDurableObject", () => {
beforeEach(() => resetHarness())
afterEach(() => cleanupHarness())
+ it("delivers preview output to current owner sockets without DO storage or replay progress", async () => {
+ const { durable, getWebSockets, storage, store } = createDO()
+ const owner = createMockWebSocket()
+ const other = createMockWebSocket()
+ const ownerState = { type: "user", userId: "owner", authenticated: true, sessionId: "test-session", sessionValidUntil: Date.now() + 60_000 }
+ owner.serializeAttachment(ownerState)
+ other.serializeAttachment({ ...ownerState, userId: "other" })
+ getWebSockets.mockReturnValue([owner, other])
+ const event = { type: "community:bot.pii", machineId: "machine", botId: "bot", requestId: "preview", result: { status: "complete", snapshot: { revision: 1, plugins: [] }, output: "private-output-fixture" } }
+ const response = await durable.fetch(new Request("http://internal/community-broadcast", {
+ method: "POST", headers: { [INTERNAL_USER_TARGET_HEADER]: "owner" }, body: JSON.stringify(event),
+ }))
+ expect(await response.json()).toEqual({ sent: 1 })
+ expect(owner.send).toHaveBeenCalledWith(JSON.stringify(event))
+ expect(other.send).not.toHaveBeenCalled()
+ expect(storage.put).not.toHaveBeenCalled()
+ expect(owner.deserializeAttachment()).toEqual(ownerState)
+ expect(JSON.stringify([...store])).not.toContain(event.result.output)
+ })
+
describe("fetch — strict community broadcast", () => {
const event = {
type: "community:status.update",