@gaoding/cli 1.9.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/contracts/operations/agent.send/output.schema.json +1 -1
  2. package/dist/bin/gd-cli.js +31 -24
  3. package/dist/bin/insmind-cli.js +31 -24
  4. package/dist/bin/task-worker.js +4 -1
  5. package/dist/src/analytics/device.js +2 -2
  6. package/dist/src/bootstrap/create-cli.js +7 -0
  7. package/dist/src/bootstrap/create-runtime.js +3 -7
  8. package/dist/src/bootstrap/run-task-worker.js +5 -1
  9. package/dist/src/bootstrap/validators.js +11 -4
  10. package/dist/src/cli/dam-commands.js +3 -3
  11. package/dist/src/cli/errors.js +60 -12
  12. package/dist/src/cli/observed-errors.js +3 -1
  13. package/dist/src/features/agent/creative-agent-adapter.js +37 -11
  14. package/dist/src/features/agent/creative-protocol.js +9 -5
  15. package/dist/src/features/agent/creative-stream.js +54 -6
  16. package/dist/src/features/agent/result-reader.js +3 -1
  17. package/dist/src/features/auth/credential-store.js +6 -10
  18. package/dist/src/features/auth/sso-service.js +22 -6
  19. package/dist/src/features/auth/state.js +6 -0
  20. package/dist/src/features/auth/use-cases.js +90 -32
  21. package/dist/src/features/dam/asset-projection.js +10 -1
  22. package/dist/src/features/dam/dam-api-adapter.js +30 -15
  23. package/dist/src/features/dam/input.js +23 -0
  24. package/dist/src/features/dam/use-cases.js +32 -22
  25. package/dist/src/features/task/store.js +5 -5
  26. package/dist/src/features/task/task-background.js +67 -19
  27. package/dist/src/features/task/use-cases.js +34 -6
  28. package/dist/src/features/tool/canvas-media.js +82 -27
  29. package/dist/src/features/tool/dynamic-schema.js +8 -4
  30. package/dist/src/features/tool/matting-billing.js +38 -26
  31. package/dist/src/features/tool/tool-api-adapter.js +17 -14
  32. package/dist/src/platform/file-lock.js +58 -15
  33. package/dist/src/platform/input-uri.js +105 -0
  34. package/dist/src/platform/local-json-file.js +7 -4
  35. package/dist/src/platform/remote-error-evidence.js +23 -1
  36. package/dist/src/platform/safe-upload-file.js +18 -4
  37. package/dist/src/platform/schema-issues.js +18 -0
  38. package/dist/src/platform/signed-http-transport.js +77 -16
  39. package/dist/src/platform/text-output.js +55 -13
  40. package/dist/src/telemetry/failure-summary.js +53 -2
  41. package/dist/src/telemetry/host-diagnostics.js +95 -0
  42. package/dist/src/telemetry/invocation.js +24 -3
  43. package/dist/src/telemetry/process-errors.js +2 -1
  44. package/dist/src/telemetry/sls-sink.js +66 -18
  45. package/package.json +4 -4
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { readdir, readFile, unlink, mkdir, chmod } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { cleanDiagnostic } from "./sanitize.js";
5
+ import { safeFailureSummary } from "./failure-summary.js";
5
6
  import { createLocalJsonFile } from "../platform/local-json-file.js";
6
7
  import { withFileLock } from "../platform/file-lock.js";
7
8
  const TTL = 7 * 86_400_000;
@@ -17,6 +18,13 @@ export function eventKey(event) {
17
18
  }
18
19
  export function wireEvent(event) {
19
20
  const safe = cleanDiagnostic(event);
21
+ if ("diagnostic_summary" in safe) {
22
+ const summary = safeFailureSummary(safe.diagnostic_summary);
23
+ if (summary)
24
+ safe.diagnostic_summary = summary;
25
+ else
26
+ delete safe.diagnostic_summary;
27
+ }
20
28
  const wire = {};
21
29
  for (const [key, value] of Object.entries(safe)) {
22
30
  if (value !== undefined)
@@ -40,8 +48,12 @@ export function wireEvent(event) {
40
48
  break;
41
49
  }
42
50
  // Keep the envelope valid even for unexpectedly large identity or adapter fields.
43
- for (const [key, value] of Object.entries(wire))
44
- wire[key] = utf8Prefix(value, 1024);
51
+ for (const [key, value] of Object.entries(wire)) {
52
+ // The bounded summary is structured JSON and contains the primary
53
+ // correlation evidence. Slicing it would make every field unreadable.
54
+ if (key !== "diagnostic_summary")
55
+ wire[key] = utf8Prefix(value, 1024);
56
+ }
45
57
  }
46
58
  return wire;
47
59
  }
@@ -62,10 +74,13 @@ export function createSlsTelemetrySink(options) {
62
74
  let writes = Promise.resolve();
63
75
  let enqueueFailures = 0;
64
76
  let lastEnqueueError;
65
- const file = (id) => createLocalJsonFile({ directory: options.directory, filename: `${id}.json` });
77
+ let status = { count: 0, delivery_failures: 0 };
78
+ let healthReady = false;
79
+ const statusFile = () => createLocalJsonFile({ directory: options.directory, filename: "delivery-status.json", role: "sls_queue" });
80
+ const file = (id) => createLocalJsonFile({ directory: options.directory, filename: `${id}.json`, role: "sls_queue" });
66
81
  let exitDeadline = Infinity;
67
82
  const locked = (run, deadline) => options.directory
68
- ? withFileLock(join(options.directory, ".lock"), run, { deadline: Math.min(deadline ?? Infinity, exitDeadline) }) : run();
83
+ ? withFileLock(join(options.directory, ".lock"), run, { deadline: Math.min(deadline ?? Infinity, exitDeadline), action: "sls_queue" }) : run();
69
84
  function checkBudget(deadline) {
70
85
  if (Date.now() >= deadline)
71
86
  throw new Error("Telemetry delivery budget exhausted.");
@@ -76,6 +91,22 @@ export function createSlsTelemetrySink(options) {
76
91
  entries.clear();
77
92
  await mkdir(options.directory, { recursive: true, mode: 0o700 });
78
93
  await chmod(options.directory, 0o700);
94
+ let rawStatus;
95
+ try {
96
+ rawStatus = JSON.parse(await statusFile().readText() ?? "{}");
97
+ }
98
+ catch { /* A malformed health file must not prevent replaying intact events. */ }
99
+ if (rawStatus && typeof rawStatus === "object" && !Array.isArray(rawStatus)) {
100
+ const value = rawStatus;
101
+ const count = (key) => typeof value[key] === "number" && Number.isSafeInteger(value[key]) && value[key] >= 0 ? value[key] : 0;
102
+ status = { count: count("count"), delivery_failures: count("delivery_failures"),
103
+ ...(["interrupted_write", "corrupt", "expired", "capacity", "http_rejected"].includes(String(value.reason)) ? { reason: value.reason }
104
+ : /^http_4\d\d$/u.test(String(value.reason)) ? { reason: "http_rejected" } : {}),
105
+ ...(["accepted", "http_rejected", "http_retryable", "network_error"].includes(String(value.result)) ? { result: value.result } : {}),
106
+ ...(typeof value.http_status === "number" && value.http_status >= 100 && value.http_status <= 599 ? { http_status: value.http_status } : {}),
107
+ ...(count("first_at_ms") ? { first_at_ms: count("first_at_ms") } : {}),
108
+ ...(count("last_at_ms") ? { last_at_ms: count("last_at_ms") } : {}) };
109
+ }
79
110
  for (const name of await readdir(options.directory)) {
80
111
  checkBudget(deadline);
81
112
  // All writers hold the same lock, so remaining atomic-write temporaries are orphaned.
@@ -99,18 +130,16 @@ export function createSlsTelemetrySink(options) {
99
130
  await dropped("corrupt", 1);
100
131
  }
101
132
  }
133
+ healthReady = true;
134
+ }
135
+ async function saveStatus() {
136
+ // Health recording must never stop the original event acknowledgement/backoff path.
137
+ if (options.directory)
138
+ await statusFile().writeTextAtomically(JSON.stringify(status)).catch(() => undefined);
102
139
  }
103
140
  async function dropped(reason, count) {
104
- if (!options.directory)
105
- return;
106
- const status = createLocalJsonFile({ directory: options.directory, filename: "delivery-status.json" });
107
- let prior = {};
108
- try {
109
- prior = JSON.parse(await status.readText() ?? "{}");
110
- }
111
- catch { /* Replace corrupt diagnostic status. */ }
112
- await status.writeTextAtomically(JSON.stringify({ count: (prior.count ?? 0) + count,
113
- first_at_ms: prior.first_at_ms ?? now(), last_at_ms: now(), reason }));
141
+ status = { ...status, count: status.count + count, first_at_ms: status.first_at_ms ?? now(), last_at_ms: now(), reason };
142
+ await saveStatus();
114
143
  }
115
144
  async function remove(id) {
116
145
  entries.delete(id);
@@ -132,7 +161,7 @@ export function createSlsTelemetrySink(options) {
132
161
  }
133
162
  let size = [...entries.values()].reduce((sum, e) => sum + Buffer.byteLength(JSON.stringify(e)), 0);
134
163
  const priority = (e) => e.event.event_name === "task_state" || e.event.event_name === "request_sent" || e.event.event_name === "result_ready" ? 2
135
- : "outcome" in e.event && e.event.outcome !== "success" ? 1 : 0;
164
+ : e.event.event_name === "host_diagnostic" || "outcome" in e.event && e.event.outcome !== "success" ? 1 : 0;
136
165
  for (const [id, entry] of [...entries].sort((a, b) => priority(a[1]) - priority(b[1]) || a[1].queuedAt - b[1].queuedAt)) {
137
166
  checkBudget(deadline);
138
167
  if (size <= (options.maxBytes ?? CAPACITY))
@@ -208,6 +237,11 @@ export function createSlsTelemetrySink(options) {
208
237
  return; // Original records remain safe for at-least-once replay.
209
238
  await locked(async () => {
210
239
  await load(deadline);
240
+ status = { ...status, delivery_failures: status.delivery_failures + (accepted ? 0 : 1),
241
+ result: accepted ? "accepted" : rejected ? "http_rejected" : response ? "http_retryable" : "network_error",
242
+ ...(response ? { http_status: response.status } : {}) };
243
+ if (!response)
244
+ delete status.http_status;
211
245
  for (const [id, original] of batch) {
212
246
  checkBudget(deadline);
213
247
  const entry = entries.get(id);
@@ -220,7 +254,9 @@ export function createSlsTelemetrySink(options) {
220
254
  nextAttempt: now() + Math.max(retryAfter, attempt === 0 ? 0 : Math.min(60_000, 1000 * 2 ** Math.min(6, original.attempts))) });
221
255
  }
222
256
  if (rejected)
223
- await dropped(`http_${response.status}`, batch.length);
257
+ await dropped("http_rejected", batch.length);
258
+ else
259
+ await saveStatus();
224
260
  }, deadline);
225
261
  if (!accepted && (retryAfter > 0 || rejected))
226
262
  return;
@@ -231,9 +267,21 @@ export function createSlsTelemetrySink(options) {
231
267
  return sending;
232
268
  }
233
269
  return { enqueue,
234
- // This snapshot describes enqueue attempts before final flush, not SLS acceptance.
270
+ async refreshDiagnostics() {
271
+ // Read persisted facts without triggering an extra send or adding business events.
272
+ const deadline = Date.now() + 100;
273
+ await writes;
274
+ await locked(() => load(deadline), deadline).catch(() => undefined);
275
+ },
276
+ // The final flush is observable by a later invocation, never retroactively on this event.
235
277
  diagnostics: () => ({ sls_enqueue_failures: enqueueFailures,
236
- ...(lastEnqueueError ? { sls_last_enqueue_error: lastEnqueueError } : {}) }),
278
+ ...(lastEnqueueError ? { sls_last_enqueue_error: lastEnqueueError } : {}),
279
+ ...(healthReady ? { sls_queue_size: entries.size,
280
+ ...(entries.size ? { sls_queue_oldest_age_ms: Math.max(0, now() - Math.min(...[...entries.values()].map(entry => entry.queuedAt))) } : {}),
281
+ sls_delivery_failures: status.delivery_failures, sls_dropped_events: status.count,
282
+ ...(status.result ? { sls_last_delivery_result: status.result } : {}),
283
+ ...(status.http_status ? { sls_last_delivery_http_status: status.http_status } : {}),
284
+ ...(status.reason ? { sls_last_drop_reason: status.reason } : {}) } : {}) }),
237
285
  async send(events) {
238
286
  const deadline = exitDeadline = Date.now() + BUDGET;
239
287
  await enqueue(events, deadline);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gaoding/cli",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "Gaoding command-line interface for agents and people.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -57,8 +57,8 @@
57
57
  "vitest": "4.1.10"
58
58
  },
59
59
  "gaodingRelease": {
60
- "sourceCommit": "2836058427780a95d4b9ed152f122b81997df34e",
61
- "pipelineId": "231960",
62
- "pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/231960"
60
+ "sourceCommit": "ad14b59e9789e8e8b7af7b7e38916d920ee0e646",
61
+ "pipelineId": "238371",
62
+ "pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/238371"
63
63
  }
64
64
  }