@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,5 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
- import { existsSync, readFileSync, statSync } from "node:fs";
2
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
3
3
  import { homedir } from "node:os";
4
4
  import { basename, join, resolve } from "node:path";
5
5
 
@@ -11,7 +11,12 @@ import {
11
11
  } from "./identity_state.mjs";
12
12
  import { resolveTldrAgentScope } from "./store.mjs";
13
13
  import { installVerifiedLocalAegisPackage } from "./starport_aegis_package.mjs";
14
- import { createStarportOnboarding } from "./starport_onboarding.mjs";
14
+ import {
15
+ _internals as onboardingInternals,
16
+ createSessionWelcomeDispatcher,
17
+ createStarportOnboarding,
18
+ createWelcomeEvidenceStore,
19
+ } from "./starport_onboarding.mjs";
15
20
  import { createStarportOrchestrator } from "./starport_orchestrator.mjs";
16
21
  import { createTightbeamChannel } from "./tightbeam_channel.mjs";
17
22
 
@@ -57,6 +62,30 @@ function matchesFileRecord(root, record) {
57
62
  }
58
63
  }
59
64
 
65
+ function matchesLocalFileRecord(root, record) {
66
+ if (
67
+ !root ||
68
+ !record ||
69
+ typeof record.path !== "string" ||
70
+ record.path.startsWith("/") ||
71
+ record.path.split("/").includes("..")
72
+ ) {
73
+ return false;
74
+ }
75
+ try {
76
+ const path = resolve(root, record.path);
77
+ if (!path.startsWith(`${resolve(root)}/`) || !statSync(path).isFile()) {
78
+ return false;
79
+ }
80
+ return (
81
+ createHash("sha256").update(readFileSync(path)).digest("hex") ===
82
+ record.sha256
83
+ );
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+
60
89
  function releaseMatchesSource(sourceRoot, release) {
61
90
  const packageJson = safeJson(join(sourceRoot, "package.json"));
62
91
  return (
@@ -64,6 +93,34 @@ function releaseMatchesSource(sourceRoot, release) {
64
93
  );
65
94
  }
66
95
 
96
+ function localSnapshotMatches(root) {
97
+ const manifest = safeJson(join(root || "", "LOCAL-SNAPSHOT-MANIFEST.json"));
98
+ if (!manifest?.sourceDigest || !Array.isArray(manifest.files)) return false;
99
+ try {
100
+ for (const entry of manifest.files) {
101
+ if (!matchesLocalFileRecord(root, entry)) return false;
102
+ }
103
+ const listed = manifest.files.map((entry) => entry.path).sort();
104
+ const walk = (path, base = path) =>
105
+ readdirSync(path, { withFileTypes: true })
106
+ .flatMap((entry) => {
107
+ const child = join(path, entry.name);
108
+ return entry.isDirectory()
109
+ ? walk(child, base)
110
+ : entry.isFile() || entry.isSymbolicLink()
111
+ ? [child.slice(base.length + 1)]
112
+ : [];
113
+ })
114
+ .sort();
115
+ const actual = walk(root).filter(
116
+ (path) => path !== "LOCAL-SNAPSHOT-MANIFEST.json",
117
+ );
118
+ return JSON.stringify(actual) === JSON.stringify(listed);
119
+ } catch {
120
+ return false;
121
+ }
122
+ }
123
+
67
124
  function resolveSetupSession(env = process.env) {
68
125
  const explicit = String(env.CLAUDE_CODE_SESSION_ID ?? "").trim();
69
126
  if (explicit) {
@@ -79,10 +136,16 @@ function resolveSetupSession(env = process.env) {
79
136
  function unavailableOnboarding() {
80
137
  return Object.freeze({
81
138
  async status() {
82
- return Object.freeze({ status: "failed" });
139
+ return Object.freeze({
140
+ status: "failed",
141
+ remediation: onboardingInternals.SAFE_WELCOME_REMEDIATION,
142
+ });
83
143
  },
84
144
  async send() {
85
- return Object.freeze({ status: "failed" });
145
+ return Object.freeze({
146
+ status: "failed",
147
+ remediation: onboardingInternals.SAFE_WELCOME_REMEDIATION,
148
+ });
86
149
  },
87
150
  });
88
151
  }
@@ -148,6 +211,40 @@ export function createStarportComponentInspector({
148
211
  };
149
212
  }
150
213
 
214
+ export function createLocalSnapshotComponentInspector({
215
+ home,
216
+ sourceRoot,
217
+ pluginRoot,
218
+ aegisApp = AEGIS_APP,
219
+ launchAgent = join(
220
+ homedir(),
221
+ "Library",
222
+ "LaunchAgents",
223
+ "ai.tldr-agent.daemon.plist",
224
+ ),
225
+ nodeExecutable = process.execPath,
226
+ } = {}) {
227
+ return async () => {
228
+ const snapshotHealthy =
229
+ localSnapshotMatches(pluginRoot) &&
230
+ resolve(pluginRoot) === resolve(sourceRoot);
231
+ const installedNode = join(home, "install", "runtime", "bin", "node");
232
+ const nativeInstalled = existsSync(aegisApp);
233
+ return Object.freeze({
234
+ plugin: snapshotHealthy,
235
+ runtime:
236
+ snapshotHealthy && resolve(nodeExecutable) === resolve(installedNode),
237
+ hooks: snapshotHealthy,
238
+ service:
239
+ snapshotHealthy &&
240
+ existsSync(launchAgent) &&
241
+ readFileSync(launchAgent, "utf8").includes(resolve(sourceRoot)),
242
+ aegis: nativeInstalled,
243
+ outbound: nativeInstalled,
244
+ });
245
+ };
246
+ }
247
+
151
248
  function bundledAegisInstaller({
152
249
  home,
153
250
  architecture = normalizedArchitecture(),
@@ -196,7 +293,10 @@ export async function createProductionStarportRuntime({
196
293
  env.HELM_CANONICAL_SQLITE = "1";
197
294
  delete env.HELM_CANONICAL_SQLITE_KILL_SWITCH;
198
295
 
199
- const sourceRoot = join(home, "install", "tldr-agent");
296
+ const localSnapshotRoot = env.TLDR_AGENT_LOCAL_SNAPSHOT_ROOT
297
+ ? resolve(env.TLDR_AGENT_LOCAL_SNAPSHOT_ROOT)
298
+ : null;
299
+ const sourceRoot = localSnapshotRoot || join(home, "install", "tldr-agent");
200
300
  const activationManifest = join(home, "install", "activation-manifest.json");
201
301
  const activePluginRoot = env.TLDR_AGENT_PLUGIN_ROOT
202
302
  ? resolve(env.TLDR_AGENT_PLUGIN_ROOT)
@@ -228,6 +328,17 @@ export async function createProductionStarportRuntime({
228
328
  tldrAgentHome: home,
229
329
  });
230
330
  const tightbeamService = tightbeam ?? createTightbeamChannel({ home });
331
+ const welcomeEvidence = createWelcomeEvidenceStore({
332
+ home,
333
+ findProviderAcceptanceByDelivery:
334
+ tightbeamService.findProviderAcceptanceByDelivery,
335
+ });
336
+ const dispatchWelcome = createSessionWelcomeDispatcher({
337
+ command: env.TIGHTBEAM_BIN,
338
+ stateRoot: env.TIGHTBEAM_STATE_ROOT,
339
+ session,
340
+ evidence: welcomeEvidence,
341
+ });
231
342
  const onboardingService =
232
343
  onboarding ??
233
344
  (session
@@ -235,18 +346,24 @@ export async function createProductionStarportRuntime({
235
346
  sessionId: session.sessionId,
236
347
  scope,
237
348
  home,
238
- dispatchMessage: ({ body, idempotencyKey }) =>
239
- tightbeamService.dispatchWelcome({ body, idempotencyKey }),
349
+ readEvidence: () => welcomeEvidence.read(),
350
+ dispatchMessage: dispatchWelcome,
240
351
  })
241
352
  : unavailableOnboarding());
242
353
  const inspector =
243
354
  inspectComponents ??
244
- createStarportComponentInspector({
245
- home,
246
- sourceRoot,
247
- pluginRoot: activePluginRoot,
248
- activationManifest,
249
- });
355
+ (localSnapshotRoot
356
+ ? createLocalSnapshotComponentInspector({
357
+ home,
358
+ sourceRoot,
359
+ pluginRoot: activePluginRoot,
360
+ })
361
+ : createStarportComponentInspector({
362
+ home,
363
+ sourceRoot,
364
+ pluginRoot: activePluginRoot,
365
+ activationManifest,
366
+ }));
250
367
  return createStarportOrchestrator({
251
368
  inspectComponents: inspector,
252
369
  native: nativeService,
@@ -262,4 +379,5 @@ export const _internals = Object.freeze({
262
379
  normalizedArchitecture,
263
380
  resolveSetupSession,
264
381
  shouldInstallBundledAegis,
382
+ unavailableOnboarding,
265
383
  });
@@ -1,26 +1,41 @@
1
1
  import { homedir } from "node:os";
2
2
  import { join, resolve } from "node:path";
3
3
  import { createTightbeamEmailDeliveryChannel } from "./tightbeam_email_delivery_channel.mjs";
4
+ import { createTightbeamInboundPublisher } from "./tightbeam_inbound_publish.mjs";
4
5
  import {
5
6
  APPLICATION,
6
7
  AUTHORITY,
7
8
  ENDPOINT_SESSION,
8
9
  PRINCIPAL_REF,
9
- TIGHTBEAM_CONTRACT,
10
+ TIGHTBEAM_COMPATIBILITY,
11
+ WELCOME_APPLICATION,
12
+ WELCOME_ENDPOINT_SESSION,
10
13
  commandFailure,
14
+ debugIdentity,
11
15
  defaultIdentityStore,
12
16
  expectedPreflightArgs,
13
- inboundKey,
17
+ identityInvalid,
18
+ launcherUnavailable,
14
19
  productionRun,
15
20
  unavailable,
16
21
  } from "./tightbeam_channel_runtime.mjs";
17
- import { createTightbeamWelcomeDispatcher } from "./tightbeam_welcome_dispatch.mjs";
22
+ import {
23
+ createTightbeamWelcomeDispatcher,
24
+ createTightbeamWelcomeIdentityManager,
25
+ } from "./tightbeam_welcome_dispatch.mjs";
18
26
 
19
27
  export function createTightbeamChannel({
20
28
  run = productionRun,
21
29
  loadIdentity,
22
30
  saveIdentity,
23
- command = process.env.TIGHTBEAM_BIN || "tightbeam",
31
+ command = process.env.TIGHTBEAM_BIN ||
32
+ join(
33
+ process.env.HOME ?? homedir(),
34
+ ".tightbeam",
35
+ "install",
36
+ "bin",
37
+ "tightbeam",
38
+ ),
24
39
  home = resolve(process.env.TLDR_AGENT_HOME || join(homedir(), ".tldr-agent")),
25
40
  } = {}) {
26
41
  const store = defaultIdentityStore(home);
@@ -28,28 +43,62 @@ export function createTightbeamChannel({
28
43
  const persistIdentity = saveIdentity ?? ((identity) => store.save(identity));
29
44
  const execute = (options) => run({ command, ...options });
30
45
 
46
+ async function acquireApplicationCredential(application, identityKind) {
47
+ let result = await execute({
48
+ admin: true,
49
+ credentials: null,
50
+ args: ["app", "register", application],
51
+ });
52
+ if (result?.error?.code !== "TIGHTBEAM_APPLICATION_IDENTITY_CONFLICT") {
53
+ return { result, reregistered: false };
54
+ }
55
+ debugIdentity(`${identityKind}_identity_reregistering`, { application });
56
+ result = await execute({
57
+ admin: true,
58
+ credentials: null,
59
+ args: ["app", "reregister", application],
60
+ });
61
+ return { result, reregistered: true };
62
+ }
63
+
31
64
  async function preflight() {
32
65
  const result = await execute({
33
66
  admin: true,
34
67
  credentials: null,
35
68
  args: expectedPreflightArgs(),
36
69
  });
37
- return commandFailure(result) ? unavailable() : Object.freeze({ ok: true });
70
+ if (result === null) return launcherUnavailable();
71
+ if (result?.result?.compatible === false) {
72
+ return unavailable(
73
+ (result.result.checks ?? [])
74
+ .filter((check) => check?.ok === false)
75
+ .map((check) => check.field),
76
+ );
77
+ }
78
+ return result?.ok === true ? Object.freeze({ ok: true }) : result;
38
79
  }
39
80
 
40
81
  async function registerEmailChannel(route) {
41
- let identity = await readIdentity();
82
+ let identity;
83
+ try {
84
+ identity = await readIdentity();
85
+ } catch (error) {
86
+ if (error?.code !== "TLDR_EMAIL_IDENTITY_INVALID") throw error;
87
+ debugIdentity("identity_integrity_failed", { code: error.code });
88
+ return identityInvalid();
89
+ }
42
90
  if (!identity) {
43
- const created = await execute({
44
- admin: true,
45
- credentials: null,
46
- args: ["app", "register", APPLICATION],
47
- });
91
+ const acquisition = await acquireApplicationCredential(
92
+ APPLICATION,
93
+ "bridge",
94
+ );
95
+ const created = acquisition.result;
48
96
  if (
49
97
  commandFailure(created) ||
50
98
  typeof created.result?.app_id !== "string" ||
51
99
  typeof created.result?.app_secret !== "string"
52
100
  ) {
101
+ if (acquisition.reregistered && created?.ok === false) return created;
53
102
  return unavailable();
54
103
  }
55
104
  identity = {
@@ -160,126 +209,29 @@ export function createTightbeamChannel({
160
209
  : Object.freeze({ ok: true });
161
210
  }
162
211
 
163
- async function publishInboundEmail({
164
- binding,
165
- provider_message_id: providerMessageId,
166
- body,
167
- }) {
168
- if (
169
- !binding ||
170
- typeof binding.conversation_id !== "string" ||
171
- typeof binding.route_id !== "string" ||
172
- typeof binding.origin_endpoint_id !== "string" ||
173
- typeof providerMessageId !== "string" ||
174
- typeof body !== "string"
175
- ) {
176
- return Object.freeze({ ok: false, code: "thread_unmapped" });
177
- }
178
- const identity = await readIdentity();
179
- if (!identity) return Object.freeze({ ok: false, code: "not_configured" });
180
-
181
- const principal = await execute({
182
- admin: false,
183
- credentials: identity,
184
- args: [
185
- "principal",
186
- "register",
187
- "--authority",
188
- AUTHORITY,
189
- "--external-ref",
190
- PRINCIPAL_REF,
191
- "--display-name",
192
- "Owner",
193
- ],
194
- });
195
- if (
196
- commandFailure(principal) ||
197
- typeof principal.result?.principal_id !== "string"
198
- ) {
199
- return Object.freeze({ ok: false, code: "identity_unavailable" });
200
- }
201
- const endpoint = await execute({
202
- admin: false,
203
- credentials: identity,
204
- args: [
205
- "endpoint",
206
- "register",
207
- "--authority",
208
- AUTHORITY,
209
- "--principal",
210
- principal.result.principal_id,
211
- "--runtime",
212
- "reference",
213
- "--session",
214
- ENDPOINT_SESSION,
215
- "--reference",
216
- AUTHORITY,
217
- ],
218
- });
219
- if (
220
- commandFailure(endpoint) ||
221
- typeof endpoint.result?.endpoint_id !== "string" ||
222
- !Number.isInteger(endpoint.result?.process_generation)
223
- ) {
224
- return Object.freeze({ ok: false, code: "identity_unavailable" });
225
- }
226
- const routes = await execute({
227
- admin: false,
228
- credentials: identity,
229
- args: ["channel", "route", "list"],
230
- });
231
- const route = routes?.result?.routes?.find(
232
- (candidate) =>
233
- candidate?.selector === "email" &&
234
- candidate?.route_id === binding.route_id,
235
- );
236
- if (commandFailure(routes) || !route) {
237
- return Object.freeze({ ok: false, code: "route_mismatch" });
238
- }
239
- const committed = await execute({
240
- admin: false,
241
- credentials: identity,
242
- args: [
243
- "message",
244
- "commit",
245
- "--conversation",
246
- binding.conversation_id,
247
- "--from",
248
- principal.result.principal_id,
249
- "--body",
250
- body,
251
- "--key",
252
- inboundKey(providerMessageId),
253
- "--origin-channel-route",
254
- binding.route_id,
255
- "--inbound-target-endpoint",
256
- binding.origin_endpoint_id,
257
- "--effect",
258
- "none",
259
- "--sender-endpoint",
260
- endpoint.result.endpoint_id,
261
- "--process-generation",
262
- String(endpoint.result.process_generation),
263
- ],
264
- });
265
- if (
266
- commandFailure(committed) ||
267
- typeof committed.result?.message_id !== "string"
268
- ) {
269
- return Object.freeze({ ok: false, code: "publish_failed" });
270
- }
271
- return Object.freeze({ ok: true, message_id: committed.result.message_id });
272
- }
212
+ const ensureWelcomeIdentity = createTightbeamWelcomeIdentityManager({
213
+ readIdentity,
214
+ persistIdentity,
215
+ execute,
216
+ commandFailure,
217
+ unavailable,
218
+ identityInvalid,
219
+ debugIdentity,
220
+ acquireApplicationCredential,
221
+ application: WELCOME_APPLICATION,
222
+ authority: AUTHORITY,
223
+ });
273
224
 
274
225
  const dispatchWelcome = createTightbeamWelcomeDispatcher({
275
226
  registerEmailChannel,
227
+ ensureWelcomeIdentity,
276
228
  readIdentity,
277
229
  execute,
278
230
  commandFailure,
279
231
  unavailable,
280
232
  authority: AUTHORITY,
281
233
  principalRef: PRINCIPAL_REF,
282
- endpointSession: ENDPOINT_SESSION,
234
+ welcomeEndpointSession: WELCOME_ENDPOINT_SESSION,
283
235
  });
284
236
 
285
237
  const delivery = createTightbeamEmailDeliveryChannel({
@@ -291,6 +243,12 @@ export function createTightbeamChannel({
291
243
  principalRef: PRINCIPAL_REF,
292
244
  endpointSession: ENDPOINT_SESSION,
293
245
  });
246
+ const publishInboundEmail = createTightbeamInboundPublisher({
247
+ execute,
248
+ readIdentity,
249
+ commandFailure,
250
+ delivery,
251
+ });
294
252
 
295
253
  return Object.freeze({
296
254
  preflight,
@@ -306,5 +264,7 @@ export const _internals = Object.freeze({
306
264
  AUTHORITY,
307
265
  ENDPOINT_SESSION,
308
266
  PRINCIPAL_REF,
309
- TIGHTBEAM_CONTRACT,
267
+ TIGHTBEAM_COMPATIBILITY,
268
+ WELCOME_APPLICATION,
269
+ WELCOME_ENDPOINT_SESSION,
310
270
  });