@joenandez/tldr 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/bin/tldr-agent +11 -0
  4. package/hooks/tldr-agent-capture-session-identity.sh +3 -2
  5. package/install/tldr-agent-bootstrap.sh +7 -0
  6. package/package.json +10 -6
  7. package/release/activation-manifest.json +6 -6
  8. package/release/payloads/arm64/TldrAgentAegis.pkg +0 -0
  9. package/release/payloads/arm64/tldr-agent.tar.gz +0 -0
  10. package/release/starport-release.json +14 -9
  11. package/skills/tldr/SKILL.md +43 -31
  12. package/src/lib/aegis_native_distribution.mjs +1 -1
  13. package/src/lib/aegis_protocol.mjs +1 -1
  14. package/src/lib/starport_aegis_package.mjs +53 -6
  15. package/src/lib/starport_onboarding.mjs +303 -6
  16. package/src/lib/starport_orchestrator.mjs +21 -20
  17. package/src/lib/starport_release_contract.mjs +48 -10
  18. package/src/lib/starport_runtime.mjs +131 -13
  19. package/src/lib/tightbeam_channel.mjs +83 -123
  20. package/src/lib/tightbeam_channel_runtime.mjs +131 -33
  21. package/src/lib/tightbeam_email_adapter.mjs +10 -0
  22. package/src/lib/tightbeam_email_delivery_channel.mjs +107 -133
  23. package/src/lib/tightbeam_email_provider_map.mjs +337 -0
  24. package/src/lib/tightbeam_inbound_publish.mjs +84 -0
  25. package/src/lib/tightbeam_welcome_dispatch.mjs +129 -33
  26. package/src/lib/tldr_agent_daemon_health.mjs +2 -1
  27. package/src/lib/tldr_agent_daemon_phase_timeouts.mjs +3 -5
  28. package/src/lib/tldr_agent_diagnostic_schema.mjs +1 -0
  29. package/src/lib/tldr_agent_launchd_definition.mjs +2 -0
  30. package/src/lib/tldr_agent_launchd_lifecycle.mjs +41 -10
  31. package/src/lib/tldr_agent_runtime_identity.mjs +14 -2
  32. package/src/lib/tldr_agent_source_lifecycle.mjs +18 -4
  33. package/src/lib/transports/email/render/email.mjs +66 -180
  34. package/src/lib/transports/email/render/html.mjs +37 -80
  35. package/src/lib/transports/email/render/text.mjs +2 -2
  36. package/src/lib/transports/email/render/tldr.mjs +10 -88
  37. package/src/lib/transports/email/render/tokens.mjs +56 -75
  38. package/src/lib/transports/email/templates.mjs +38 -27
  39. package/src/lib/transports/email.mjs +21 -1
  40. package/src/starport.mjs +12 -0
  41. package/src/lib/transports/email/render/glyphs.mjs +0 -66
@@ -1,13 +1,288 @@
1
+ import { execFile } from "node:child_process";
2
+ import { readFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { writeTextAtomic } from "./durable_file_io.mjs";
1
7
  import { renderTldrAgentWelcomeEmail } from "./transports/email/templates.mjs";
2
8
 
3
9
  export const WELCOME_IDEMPOTENCY_KEY = "tldr-agent-welcome:v1";
10
+ export const WELCOME_REASON =
11
+ "Confirm the setup session can receive owner replies";
12
+ const execFileAsync = promisify(execFile);
13
+ const WELCOME_EVIDENCE_VERSION = 1;
14
+ const WELCOME_EVIDENCE_FILE = "welcome-evidence.json";
15
+ const SAFE_WELCOME_REMEDIATION =
16
+ "Start a new supported agent session, then rerun tldr; setup.";
17
+ const DEFINITE_TIGHTBEAM_FAILURE_CODES = new Set([
18
+ "session_unavailable",
19
+ "route_unavailable",
20
+ "permission_denied",
21
+ "bootstrap_required",
22
+ ]);
4
23
 
5
24
  async function noWelcomeEvidence() {
6
25
  return "not-started";
7
26
  }
8
27
 
9
- function status(value) {
10
- return Object.freeze({ status: value });
28
+ function status(value, remediation = null) {
29
+ return Object.freeze({
30
+ status: value,
31
+ ...(typeof remediation === "string" && remediation.length > 0
32
+ ? { remediation }
33
+ : {}),
34
+ });
35
+ }
36
+
37
+ function sessionEnvironment(session) {
38
+ const sessionId = session?.sessionId;
39
+ if (typeof sessionId !== "string" || sessionId.length === 0) return null;
40
+ return session?.identity?.runtime === "codex"
41
+ ? { CODEX_THREAD_ID: sessionId }
42
+ : { CLAUDE_CODE_SESSION_ID: sessionId };
43
+ }
44
+
45
+ function welcomeData(result) {
46
+ const value = result?.result ?? result?.data ?? result;
47
+ if (!value || typeof value !== "object") return {};
48
+ const delivery = Array.isArray(value.channel_deliveries)
49
+ ? value.channel_deliveries.find(
50
+ (item) =>
51
+ item?.selector === "email" && typeof item.delivery_id === "string",
52
+ )
53
+ : null;
54
+ return Object.fromEntries(
55
+ [
56
+ ["message_id", value.message_id],
57
+ ["conversation_id", value.conversation_id],
58
+ ["delivery_id", value.delivery_id ?? delivery?.delivery_id],
59
+ ["outcome", value.outcome],
60
+ ].filter(
61
+ ([, fieldValue]) =>
62
+ typeof fieldValue === "string" && fieldValue.length > 0,
63
+ ),
64
+ );
65
+ }
66
+
67
+ async function productionSessionRun({
68
+ command,
69
+ args,
70
+ env,
71
+ exec = execFileAsync,
72
+ }) {
73
+ try {
74
+ const { stdout } = await exec(command, args, {
75
+ encoding: "utf8",
76
+ maxBuffer: 64 * 1024,
77
+ env,
78
+ });
79
+ return JSON.parse(stdout);
80
+ } catch (error) {
81
+ const output = String(error?.stdout ?? "").trim();
82
+ if (output) {
83
+ try {
84
+ return JSON.parse(output);
85
+ } catch {
86
+ // The launch completed but did not return an interpretable result.
87
+ }
88
+ }
89
+ const stderr = String(error?.stderr ?? "");
90
+ const stableCode = stderr.match(/(?:^|\n)error ([a-z][a-z0-9_]*):/i)?.[1];
91
+ if (
92
+ /(?:^|\n)usage error:/i.test(stderr) ||
93
+ DEFINITE_TIGHTBEAM_FAILURE_CODES.has(stableCode)
94
+ ) {
95
+ return {
96
+ ok: false,
97
+ error: {
98
+ code: stableCode ?? "tightbeam_usage_error",
99
+ ambiguous: false,
100
+ },
101
+ };
102
+ }
103
+ return {
104
+ ok: false,
105
+ error: {
106
+ code:
107
+ error?.code === "ENOENT"
108
+ ? "tightbeam_unavailable"
109
+ : "welcome_send_failed",
110
+ ambiguous: error?.code !== "ENOENT",
111
+ },
112
+ };
113
+ }
114
+ }
115
+
116
+ export function createSessionWelcomeDispatcher({
117
+ command = process.env.TIGHTBEAM_BIN ||
118
+ join(
119
+ process.env.HOME ?? homedir(),
120
+ ".tightbeam",
121
+ "install",
122
+ "bin",
123
+ "tightbeam",
124
+ ),
125
+ stateRoot = process.env.TIGHTBEAM_STATE_ROOT,
126
+ session,
127
+ run = productionSessionRun,
128
+ evidence = null,
129
+ } = {}) {
130
+ const identityEnvironment = sessionEnvironment(session);
131
+ return async ({ subject, body } = {}) => {
132
+ if (!identityEnvironment) {
133
+ return {
134
+ ok: false,
135
+ error: {
136
+ code: "session_unavailable",
137
+ ambiguous: false,
138
+ remediation: SAFE_WELCOME_REMEDIATION,
139
+ },
140
+ };
141
+ }
142
+ try {
143
+ await evidence?.begin();
144
+ } catch {
145
+ return {
146
+ ok: false,
147
+ error: { code: "welcome_evidence_unavailable", ambiguous: true },
148
+ };
149
+ }
150
+ const result = await run({
151
+ command,
152
+ args: [
153
+ "--json",
154
+ "agent",
155
+ "send",
156
+ "--to",
157
+ "user",
158
+ "--channel",
159
+ "email",
160
+ "--subject",
161
+ subject,
162
+ "--reason",
163
+ WELCOME_REASON,
164
+ "--body",
165
+ body,
166
+ ],
167
+ env: {
168
+ PATH: process.env.PATH,
169
+ TIGHTBEAM_STATE_ROOT: stateRoot,
170
+ ...identityEnvironment,
171
+ },
172
+ });
173
+ const unavailable = result?.error?.code === "session_unavailable";
174
+ const normalized = unavailable
175
+ ? {
176
+ ok: false,
177
+ error: {
178
+ code: "session_unavailable",
179
+ ambiguous: false,
180
+ remediation: SAFE_WELCOME_REMEDIATION,
181
+ },
182
+ }
183
+ : result?.ok === true
184
+ ? { ok: true, data: welcomeData(result) }
185
+ : result;
186
+ try {
187
+ await evidence?.complete(normalized);
188
+ } catch {
189
+ return {
190
+ ok: false,
191
+ error: { code: "welcome_evidence_unavailable", ambiguous: true },
192
+ };
193
+ }
194
+ return normalized;
195
+ };
196
+ }
197
+
198
+ function evidenceRecord(value) {
199
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
200
+ const allowed = new Set([
201
+ "version",
202
+ "status",
203
+ "timestamp",
204
+ "message_id",
205
+ "conversation_id",
206
+ "delivery_id",
207
+ "outcome",
208
+ ]);
209
+ if (
210
+ Object.keys(value).some((key) => !allowed.has(key)) ||
211
+ value.version !== WELCOME_EVIDENCE_VERSION ||
212
+ typeof value.status !== "string" ||
213
+ typeof value.timestamp !== "string"
214
+ ) {
215
+ return null;
216
+ }
217
+ return value;
218
+ }
219
+
220
+ export function createWelcomeEvidenceStore({
221
+ home,
222
+ path = join(home || "", WELCOME_EVIDENCE_FILE),
223
+ now = () => new Date().toISOString(),
224
+ write = writeTextAtomic,
225
+ read = readFile,
226
+ findProviderAcceptanceByDelivery = async () => null,
227
+ } = {}) {
228
+ function writeRecord(record) {
229
+ write(path, `${JSON.stringify(record)}\n`, { durable: true, mode: 0o600 });
230
+ }
231
+
232
+ async function readRecord() {
233
+ try {
234
+ return evidenceRecord(JSON.parse(await read(path, "utf8")));
235
+ } catch (error) {
236
+ return error?.code === "ENOENT" ? undefined : null;
237
+ }
238
+ }
239
+
240
+ return Object.freeze({
241
+ path,
242
+ async begin() {
243
+ writeRecord({
244
+ version: WELCOME_EVIDENCE_VERSION,
245
+ status: "dispatching",
246
+ timestamp: now(),
247
+ });
248
+ },
249
+ async complete(result) {
250
+ const data = welcomeData(result);
251
+ const definiteFailure =
252
+ result?.ok === false && result?.error?.ambiguous !== true;
253
+ writeRecord({
254
+ version: WELCOME_EVIDENCE_VERSION,
255
+ status: definiteFailure
256
+ ? "failed"
257
+ : result?.ok === true
258
+ ? "returned"
259
+ : "ambiguous",
260
+ timestamp: now(),
261
+ ...data,
262
+ });
263
+ },
264
+ async read() {
265
+ const record = await readRecord();
266
+ if (record === undefined) return "not-started";
267
+ if (record === null) return "ambiguous";
268
+ if (record.status === "failed") return "failed";
269
+ if (typeof record.delivery_id !== "string") return "ambiguous";
270
+ try {
271
+ return (await findProviderAcceptanceByDelivery(record.delivery_id))
272
+ ? "accepted"
273
+ : "ambiguous";
274
+ } catch {
275
+ return "ambiguous";
276
+ }
277
+ },
278
+ });
279
+ }
280
+
281
+ function debugSetupTemplateDrift(stage, data) {
282
+ if (process.env.TLDR_AGENT_DEBUG_TEMPLATE_DRIFT !== "1") return;
283
+ process.stderr.write(
284
+ `[🪳 TEMP SETUP_EMAIL_TEMPLATE_DRIFT] ${stage} ${JSON.stringify(data)}\n`,
285
+ );
11
286
  }
12
287
 
13
288
  export function createStarportOnboarding({
@@ -22,6 +297,8 @@ export function createStarportOnboarding({
22
297
  throw new TypeError("Starport onboarding requires setup session and scope");
23
298
  }
24
299
 
300
+ let failedRemediation = null;
301
+
25
302
  async function readStatus() {
26
303
  const evidence = await readEvidence({
27
304
  scope,
@@ -37,9 +314,15 @@ export function createStarportOnboarding({
37
314
 
38
315
  async function send() {
39
316
  const before = await readStatus();
40
- if (before.status === "accepted") return before;
317
+ if (before.status === "accepted" || before.status === "ambiguous") {
318
+ return before;
319
+ }
41
320
  if (typeof dispatchMessage !== "function") return status("failed");
42
321
  const template = renderTldrAgentWelcomeEmail();
322
+ debugSetupTemplateDrift("setup-welcome-dispatch", {
323
+ template_version: template.template_version,
324
+ idempotency_key: WELCOME_IDEMPOTENCY_KEY,
325
+ });
43
326
  let delivery;
44
327
  try {
45
328
  delivery = await dispatchMessage({
@@ -60,10 +343,14 @@ export function createStarportOnboarding({
60
343
  return status(error?.ambiguous === true ? "ambiguous" : "failed");
61
344
  }
62
345
  if (delivery?.ok === false && delivery.error?.ambiguous !== true) {
346
+ failedRemediation =
347
+ typeof delivery.error?.remediation === "string"
348
+ ? delivery.error.remediation
349
+ : null;
63
350
  const afterFailure = await readStatus();
64
351
  return afterFailure.status === "not-started"
65
- ? status("failed")
66
- : afterFailure;
352
+ ? status("failed", failedRemediation)
353
+ : status(afterFailure.status, failedRemediation);
67
354
  }
68
355
  if (delivery?.error?.ambiguous === true) return status("ambiguous");
69
356
  const after = await readStatus();
@@ -73,4 +360,14 @@ export function createStarportOnboarding({
73
360
  return Object.freeze({ status: readStatus, send });
74
361
  }
75
362
 
76
- export const _internals = Object.freeze({ noWelcomeEvidence });
363
+ export const _internals = Object.freeze({
364
+ DEFINITE_TIGHTBEAM_FAILURE_CODES,
365
+ noWelcomeEvidence,
366
+ SAFE_WELCOME_REMEDIATION,
367
+ WELCOME_EVIDENCE_FILE,
368
+ WELCOME_EVIDENCE_VERSION,
369
+ evidenceRecord,
370
+ sessionEnvironment,
371
+ productionSessionRun,
372
+ welcomeData,
373
+ });
@@ -118,6 +118,18 @@ export function createStarportOrchestrator({
118
118
  "Check tldr;",
119
119
  );
120
120
  }
121
+ if (
122
+ welcome?.status === "failed" &&
123
+ typeof welcome.remediation === "string" &&
124
+ welcome.remediation.length > 0
125
+ ) {
126
+ return success(
127
+ "welcome-delivery-failed",
128
+ COMPONENTS,
129
+ "start-supported-session",
130
+ welcome.remediation,
131
+ );
132
+ }
121
133
  return success(
122
134
  "ready-unverified",
123
135
  COMPONENTS,
@@ -131,18 +143,9 @@ export function createStarportOrchestrator({
131
143
  if (preflightFailure) return preflightFailure;
132
144
  const before = await status();
133
145
  if (before.data.state === "onboarding-verified") {
134
- const configured = await native.configure?.();
135
- if (configured?.ok === false) return configured;
136
- const after = await status();
137
- const registrationFailure = await registerReadyEmailChannel(
138
- after.data.state,
139
- );
140
- return registrationFailure ?? after;
146
+ return before;
141
147
  }
142
- if (
143
- before.data.state === "ready-unverified" ||
144
- before.data.state === "onboarding-reconciling"
145
- ) {
148
+ if (before.data.state === "ready-unverified") {
146
149
  const registrationFailure = await registerReadyEmailChannel(
147
150
  before.data.state,
148
151
  );
@@ -150,13 +153,13 @@ export function createStarportOrchestrator({
150
153
  await onboarding.send?.();
151
154
  return status();
152
155
  }
156
+ if (before.data.state === "onboarding-reconciling") return before;
157
+ if (before.data.state === "welcome-delivery-failed") return before;
158
+ const registrationFailure = await registerEmailChannel();
159
+ if (registrationFailure) return registrationFailure;
153
160
  await source?.install?.();
154
161
  await native.setup?.();
155
162
  const after = await status();
156
- const registrationFailure = await registerReadyEmailChannel(
157
- after.data.state,
158
- );
159
- if (registrationFailure) return registrationFailure;
160
163
  if (after.data.state === "ready-unverified") {
161
164
  await onboarding.send?.();
162
165
  return status();
@@ -179,13 +182,11 @@ export function createStarportOrchestrator({
179
182
  async repair() {
180
183
  const preflightFailure = await preflightTightbeam();
181
184
  if (preflightFailure) return preflightFailure;
185
+ const registrationFailure = await registerEmailChannel();
186
+ if (registrationFailure) return registrationFailure;
182
187
  await source?.install?.();
183
188
  await native.repair?.();
184
- const after = await status();
185
- const registrationFailure = await registerReadyEmailChannel(
186
- after.data.state,
187
- );
188
- return registrationFailure ?? after;
189
+ return status();
189
190
  },
190
191
  async uninstall() {
191
192
  const result = await native.uninstall?.();
@@ -1,8 +1,13 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { TIGHTBEAM_COMPATIBILITY } from "./tightbeam_channel_runtime.mjs";
2
3
 
3
4
  const SHA256 = /^[a-f0-9]{64}$/;
4
5
  const SEMVER = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
5
6
 
7
+ // The target-only release lane updates this explicit source contract together
8
+ // with the runtime and release descriptor; a literal avoids a hidden pin.
9
+ export const TIGHTBEAM_TARGET_VERSION = "0.2.0";
10
+
6
11
  export const STARPORT_ACCEPTANCE_STEPS = Object.freeze([
7
12
  "marketplace-add",
8
13
  "plugin-install",
@@ -32,18 +37,37 @@ function sameVersion(packageJson, plugin, marketplace, release) {
32
37
  );
33
38
  }
34
39
 
35
- function tightbeamContractComplete(tightbeam) {
40
+ function tightbeamContractComplete(
41
+ tightbeam,
42
+ expectedTargetVersion,
43
+ expectedQualification,
44
+ ) {
45
+ const compatibility = tightbeam?.compatibility;
46
+ const qualification = tightbeam?.qualification;
36
47
  return Boolean(
37
- tightbeam?.package === "tightbeam" &&
38
- tightbeam.version === "0.2.0" &&
39
- tightbeam.protocol?.min === "1.0" &&
40
- tightbeam.protocol?.max === "1" &&
41
- tightbeam.state_schema_version === 14 &&
42
- tightbeam.required_capability === "channels.v1",
48
+ compatibility?.protocol?.min === TIGHTBEAM_COMPATIBILITY.minProtocol &&
49
+ compatibility.protocol.max === TIGHTBEAM_COMPATIBILITY.maxProtocol &&
50
+ JSON.stringify(compatibility.requiredCapabilities) ===
51
+ JSON.stringify(TIGHTBEAM_COMPATIBILITY.requiredCapabilities) &&
52
+ qualification?.version === expectedTargetVersion &&
53
+ /^[a-f0-9]{40}$/.test(qualification.sourceCommit ?? "") &&
54
+ /^sha256:[a-f0-9]{64}$/.test(qualification.treeDigest ?? "") &&
55
+ SHA256.test(qualification.tarballSha256 ?? "") &&
56
+ (expectedQualification === undefined ||
57
+ JSON.stringify(qualification) ===
58
+ JSON.stringify(expectedQualification)),
43
59
  );
44
60
  }
45
61
 
46
- function sourceContractComplete({ packageJson, plugin, marketplace, release }) {
62
+ function sourceContractComplete({
63
+ packageJson,
64
+ packageLock,
65
+ plugin,
66
+ marketplace,
67
+ release,
68
+ expectedTargetVersion,
69
+ expectedQualification,
70
+ }) {
47
71
  const source = marketplace.plugins?.[0]?.source;
48
72
  return Boolean(
49
73
  packageJson.name === "@joenandez/tldr" &&
@@ -59,6 +83,9 @@ function sourceContractComplete({ packageJson, plugin, marketplace, release }) {
59
83
  source?.source === "npm" &&
60
84
  source.package === packageJson.name &&
61
85
  release.npm?.package === packageJson.name &&
86
+ (packageLock === undefined ||
87
+ (packageLock?.version === packageJson.version &&
88
+ packageLock?.packages?.[""]?.version === packageJson.version)) &&
62
89
  sameVersion(packageJson, plugin, marketplace, release) &&
63
90
  release.schema_version === 1 &&
64
91
  release.phase === "source" &&
@@ -76,7 +103,11 @@ function sourceContractComplete({ packageJson, plugin, marketplace, release }) {
76
103
  release.aegis?.minimum_macos === "13.0" &&
77
104
  JSON.stringify(release.aegis?.architectures) ===
78
105
  JSON.stringify(["arm64"]) &&
79
- tightbeamContractComplete(release.tightbeam) &&
106
+ tightbeamContractComplete(
107
+ release.tightbeam,
108
+ expectedTargetVersion,
109
+ expectedQualification,
110
+ ) &&
80
111
  release.dependencies?.yaml === "2.8.3" &&
81
112
  release.dependencies?.agentmail === "0.4.20" &&
82
113
  release.dependencies?.c8 === "10.1.3",
@@ -100,7 +131,14 @@ function publicationComplete(release) {
100
131
  }
101
132
 
102
133
  export function validateStarportReleaseContract(input = {}) {
103
- if (!sourceContractComplete(input)) refuse("source coordinates unresolved");
134
+ const expectedTargetVersion =
135
+ input.expectedTargetVersion ?? TIGHTBEAM_TARGET_VERSION;
136
+ if (
137
+ !SEMVER.test(expectedTargetVersion) ||
138
+ !sourceContractComplete({ ...input, expectedTargetVersion })
139
+ ) {
140
+ refuse("source coordinates unresolved");
141
+ }
104
142
  if (input.requirePublished === true && !publicationComplete(input.release)) {
105
143
  refuse("publication evidence unresolved");
106
144
  }