@abelxiaoxing/cadence 2.2.0 → 2.2.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.
@@ -2817,6 +2817,9 @@ function unitEnvironment(text2) {
2817
2817
  }
2818
2818
  return out;
2819
2819
  }
2820
+ function generatedUnit(s) {
2821
+ return readGenerated(unitPath(s), MARKER);
2822
+ }
2820
2823
  function directory(path2) {
2821
2824
  try {
2822
2825
  return isAbsolute3(path2) && statSync2(path2).isDirectory();
@@ -3923,6 +3926,69 @@ import {
3923
3926
  } from "node:crypto";
3924
3927
  import { dirname as dirname7, isAbsolute as isAbsolute5, join as join7 } from "node:path";
3925
3928
 
3929
+ // src/image-content.mjs
3930
+ var IMAGE_LIMITS = Object.freeze({
3931
+ chunkChars: 256 * 1024,
3932
+ idleMs: 10 * 60 * 1000
3933
+ });
3934
+ var IMAGE_TYPES = Object.freeze([
3935
+ "image/png",
3936
+ "image/jpeg",
3937
+ "image/webp"
3938
+ ]);
3939
+
3940
+ // src/service/contracts/workflow.ts
3941
+ var WORKFLOW_LIMITS = {
3942
+ tasks: 16,
3943
+ route: 48,
3944
+ questions: 32,
3945
+ checks: 16,
3946
+ bytes: 256 * 1024,
3947
+ context: 32768,
3948
+ reason: 1024,
3949
+ historyScanEntries: 200,
3950
+ historyPage: 20,
3951
+ historyScans: 16,
3952
+ historyIdleMs: 120000
3953
+ };
3954
+ // src/service/contracts/workbench.ts
3955
+ var FILE_PART_BYTES = 192 * 1024;
3956
+ var FILE_DOWNLOAD_BYTES = 64 * 1024 * 1024;
3957
+ var CONVERSATION_SEND_BYTES = 16 * 1024;
3958
+ var UNDELIVERED_TEXT_BYTES = 2 * CONVERSATION_SEND_BYTES;
3959
+ var workflowTurnStatuses = [
3960
+ "queued",
3961
+ "running",
3962
+ "completed",
3963
+ "failed",
3964
+ "canceled",
3965
+ "timeout",
3966
+ "unknown"
3967
+ ];
3968
+ var CHAT_STATUSES = ["starting", ...workflowTurnStatuses];
3969
+ var STEP_TEXT_BYTES = 16 * 1024;
3970
+
3971
+ // src/service-wire.ts
3972
+ var pageReaders = [
3973
+ "tasks",
3974
+ "runs",
3975
+ "messages",
3976
+ "tools",
3977
+ "contents",
3978
+ "events",
3979
+ "checks",
3980
+ "changes",
3981
+ "reviews"
3982
+ ];
3983
+ var HISTORY_CSRF_HEADER = "x-cadence-history-csrf";
3984
+ var retainedReaders = [
3985
+ ...pageReaders,
3986
+ "entity",
3987
+ "graph",
3988
+ "detail",
3989
+ "search"
3990
+ ];
3991
+
3926
3992
  // src/service/config.ts
3927
3993
  import { isIP } from "node:net";
3928
3994
  function decodeHttpPolicy(value) {
@@ -4238,18 +4304,41 @@ function readExternal(path2) {
4238
4304
  class HttpAuthentication {
4239
4305
  material;
4240
4306
  epoch;
4307
+ workspaceId;
4241
4308
  localCapability;
4242
- csrf = randomUUID4();
4309
+ executionCsrf = randomUUID4();
4310
+ get csrf() {
4311
+ return this.executionCsrf;
4312
+ }
4313
+ history;
4243
4314
  cookieName;
4244
4315
  constructor(material, epoch, workspaceId, localCapability) {
4245
4316
  this.material = material;
4246
4317
  this.epoch = epoch;
4318
+ this.workspaceId = workspaceId;
4247
4319
  this.localCapability = localCapability;
4248
4320
  decodeId(epoch);
4249
4321
  decodeId(workspaceId);
4250
4322
  if (!/^[A-Za-z0-9_-]{43}$/.test(localCapability))
4251
4323
  throw new ContractError("invalid");
4252
4324
  this.cookieName = `cadence_${workspaceId.replaceAll("-", "")}`;
4325
+ material.onRevoke(() => {
4326
+ this.history = undefined;
4327
+ this.executionCsrf = randomUUID4();
4328
+ });
4329
+ }
4330
+ historyChallenge(workspace) {
4331
+ if (workspace.workspaceId !== this.workspaceId || workspace.epoch !== this.epoch)
4332
+ throw new ContractError("stale");
4333
+ if (this.history?.storeId !== workspace.storeId)
4334
+ this.history = {
4335
+ storeId: decodeId(workspace.storeId),
4336
+ csrf: randomUUID4()
4337
+ };
4338
+ return this.history.csrf;
4339
+ }
4340
+ get historyStore() {
4341
+ return this.history?.storeId;
4253
4342
  }
4254
4343
  bootstrap(value, boundary) {
4255
4344
  this.material.assertCurrent();
@@ -4282,14 +4371,14 @@ class HttpAuthentication {
4282
4371
  }
4283
4372
  return false;
4284
4373
  }
4285
- authenticate(boundary, attachmentId, mutation = false, websocket = false) {
4374
+ authenticate(boundary, attachmentId, post = false, websocket = false) {
4286
4375
  this.material.assertCurrent();
4287
4376
  if (!this.material.available)
4288
4377
  throw new ContractError("quarantine");
4289
4378
  const h = boundary.headers;
4290
4379
  const local = h.get("x-cadence-local");
4291
4380
  if (local !== undefined) {
4292
- if (!boundary.local || h.has("cookie") || [...h.keys()].some((name) => name.startsWith("sec-fetch-") && (name !== "sec-fetch-mode" || h.get(name) !== "cors")) || !sameSecret(local, this.localCapability))
4381
+ if (post === "retained-history" || !boundary.local || h.has("cookie") || [...h.keys()].some((name) => name.startsWith("sec-fetch-") && (name !== "sec-fetch-mode" || h.get(name) !== "cors")) || !sameSecret(local, this.localCapability))
4293
4382
  throw new ContractError("forbidden");
4294
4383
  } else {
4295
4384
  if (h.has("authorization") || h.has("x-cadence-token"))
@@ -4298,10 +4387,14 @@ class HttpAuthentication {
4298
4387
  throw new ContractError("unauthenticated");
4299
4388
  if (h.has("sec-fetch-site") && !["same-origin", "none"].includes(h.get("sec-fetch-site") ?? ""))
4300
4389
  throw new ContractError("forbidden");
4301
- if ((mutation || websocket) && h.get("origin") !== boundary.origin)
4390
+ if ((post || websocket) && h.get("origin") !== boundary.origin)
4302
4391
  throw new ContractError("forbidden");
4303
- if (mutation && h.get("x-cadence-csrf") !== this.csrf)
4392
+ if (post === "retained-history") {
4393
+ if (!this.history || h.get(HISTORY_CSRF_HEADER) !== this.history.csrf || h.has("x-cadence-attachment") || h.has("x-cadence-csrf"))
4394
+ throw new ContractError("forbidden");
4395
+ } else if (post && h.get("x-cadence-csrf") !== this.csrf) {
4304
4396
  throw new ContractError("forbidden");
4397
+ }
4305
4398
  }
4306
4399
  return {
4307
4400
  attachmentId: decodeId(attachmentId),
@@ -4668,28 +4761,28 @@ function object2(value, at) {
4668
4761
  return fail(at, "must be a plain object without reserved keys");
4669
4762
  }
4670
4763
  }
4671
- function shape(fields2, required = [], retired = {}) {
4764
+ function shape(fields3, required = [], retired = {}) {
4672
4765
  const check = (value, at) => {
4673
4766
  const o = object2(value, at);
4674
4767
  for (const key of Object.keys(o))
4675
- if (!Object.hasOwn(fields2, key))
4768
+ if (!Object.hasOwn(fields3, key))
4676
4769
  fail(child(at, key), Object.hasOwn(retired, key) ? retired[key] : UNSUPPORTED);
4677
4770
  for (const key of required)
4678
4771
  if (o[key] === undefined)
4679
4772
  fail(child(at, key), "is required");
4680
- for (const [key, field] of Object.entries(fields2))
4773
+ for (const [key, field] of Object.entries(fields3))
4681
4774
  if (o[key] !== undefined)
4682
4775
  field(o[key], child(at, key));
4683
4776
  };
4684
- check.fields = fields2;
4777
+ check.fields = fields3;
4685
4778
  return check;
4686
4779
  }
4687
- function record(entries) {
4780
+ function record(entries2) {
4688
4781
  const check = (value, at) => {
4689
4782
  for (const [key, entry] of Object.entries(object2(value, at)))
4690
- entries(entry, child(at, key));
4783
+ entries2(entry, child(at, key));
4691
4784
  };
4692
- check.entries = entries;
4785
+ check.entries = entries2;
4693
4786
  return check;
4694
4787
  }
4695
4788
  function list2(items, maximum = ENTRY_LIMIT) {
@@ -5123,7 +5216,7 @@ function decodeRecoveryEvent(value) {
5123
5216
  archive
5124
5217
  };
5125
5218
  }
5126
- function entries(path2, maximum = 4096) {
5219
+ function entries2(path2, maximum = 4096) {
5127
5220
  const out = [];
5128
5221
  let dir;
5129
5222
  try {
@@ -5151,7 +5244,7 @@ function size(path2, limit = { left: 1e5 }) {
5151
5244
  if (!st.isDirectory())
5152
5245
  return st.size;
5153
5246
  let total = 0;
5154
- for (const name of entries(path2, 1e5))
5247
+ for (const name of entries2(path2, 1e5))
5155
5248
  total += size(join10(path2, name), limit);
5156
5249
  return total;
5157
5250
  }
@@ -5211,7 +5304,7 @@ class RecoveryLog {
5211
5304
  return value;
5212
5305
  }
5213
5306
  latestFile() {
5214
- for (const name of entries(this.logDirectory).reverse()) {
5307
+ for (const name of entries2(this.logDirectory).reverse()) {
5215
5308
  if (!/^[0-9]{13}-[0-9a-f-]{36}\.json$/.test(name))
5216
5309
  continue;
5217
5310
  const path2 = join10(this.logDirectory, name);
@@ -5228,7 +5321,7 @@ class RecoveryLog {
5228
5321
  }
5229
5322
  events(limit = 20) {
5230
5323
  const out = [];
5231
- for (const name of entries(this.logDirectory).reverse()) {
5324
+ for (const name of entries2(this.logDirectory).reverse()) {
5232
5325
  if (out.length >= limit)
5233
5326
  break;
5234
5327
  if (!/^[0-9]{13}-[0-9a-f-]{36}\.json$/.test(name))
@@ -5242,10 +5335,10 @@ class RecoveryLog {
5242
5335
  return out;
5243
5336
  }
5244
5337
  prune(now = Date.now()) {
5245
- const names2 = entries(this.logDirectory);
5338
+ const names2 = entries2(this.logDirectory);
5246
5339
  for (const name of names2.slice(0, Math.max(0, names2.length - RECOVERY_EVENTS)))
5247
5340
  rmSync2(join10(this.logDirectory, name), { force: true });
5248
- const archived = entries(this.archiveDirectory).filter((name) => /^[0-9]{13}-[0-9a-f-]{36}$/.test(name));
5341
+ const archived = entries2(this.archiveDirectory).filter((name) => /^[0-9]{13}-[0-9a-f-]{36}$/.test(name));
5249
5342
  const kept = [];
5250
5343
  for (const name of archived) {
5251
5344
  const path2 = join10(this.archiveDirectory, name);
@@ -5278,7 +5371,7 @@ class RecoveryLog {
5278
5371
  }));
5279
5372
  }
5280
5373
  usage() {
5281
- const names2 = entries(this.archiveDirectory);
5374
+ const names2 = entries2(this.archiveDirectory);
5282
5375
  return {
5283
5376
  entries: names2.length,
5284
5377
  bytes: names2.reduce((sum, name) => sum + size(join10(this.archiveDirectory, name)), 0)
@@ -5286,7 +5379,7 @@ class RecoveryLog {
5286
5379
  }
5287
5380
  }
5288
5381
  function strayTemporaries(directory2) {
5289
- return entries(directory2).filter((name) => /^pending-[0-9a-f-]{36}\.json$/.test(name)).map((name) => join10(directory2, name));
5382
+ return entries2(directory2).filter((name) => /^pending-[0-9a-f-]{36}\.json$/.test(name)).map((name) => join10(directory2, name));
5290
5383
  }
5291
5384
 
5292
5385
  // src/service/runtime.ts
@@ -5503,7 +5596,7 @@ function describePiProblem(problem) {
5503
5596
  }
5504
5597
  function decodePiProblem(value) {
5505
5598
  const o = closed(value, ["file", "check", "field", "reason"]);
5506
- const words = (v) => {
5599
+ const words2 = (v) => {
5507
5600
  const s = text(v, 512, true);
5508
5601
  if (/[\p{Cc}\p{Cf}]/u.test(s))
5509
5602
  throw new ContractError("invalid");
@@ -5512,8 +5605,8 @@ function decodePiProblem(value) {
5512
5605
  return {
5513
5606
  file: oneOf(o.file, Object.keys(PI_SOURCE_STAGES)),
5514
5607
  check: oneOf(o.check, ["file", "json", "schema", "sdk"]),
5515
- field: o.field === null ? null : words(o.field),
5516
- reason: words(o.reason)
5608
+ field: o.field === null ? null : words2(o.field),
5609
+ reason: words2(o.reason)
5517
5610
  };
5518
5611
  }
5519
5612
  function decodeStartupReport(value) {
@@ -6161,14 +6254,14 @@ class RegistryHealer {
6161
6254
  archive.keep(this.path(name));
6162
6255
  }
6163
6256
  }
6164
- const entries2 = [];
6257
+ const entries3 = [];
6165
6258
  for (const row of liveReservations(this.c))
6166
- entries2.push(row);
6259
+ entries3.push(row);
6167
6260
  this.c.files.create(this.path("format.json"), encoded({
6168
6261
  schema: "cadence-service-registry-format/1",
6169
6262
  stateHome: this.c.stateHome
6170
6263
  }));
6171
- this.c.files.create(this.path("roots.json"), encoded({ schema: "cadence-service-registry/1", entries: entries2 }), 1024 * 1024);
6264
+ this.c.files.create(this.path("roots.json"), encoded({ schema: "cadence-service-registry/1", entries: entries3 }), 1024 * 1024);
6172
6265
  } finally {
6173
6266
  this.c.files.remove(lockPath, lock);
6174
6267
  }
@@ -7942,10 +8035,10 @@ async function serviceStatus(c, diagnose = false) {
7942
8035
  };
7943
8036
  }
7944
8037
  }
7945
- async function stopService(c) {
8038
+ async function stopService(c, confirmedWork) {
7946
8039
  let epoch;
7947
8040
  try {
7948
- const reply = closed(await nativeCall(c, "stop"), ["status", "epoch"]);
8041
+ const reply = closed(await nativeCall(c, "stop", false, confirmedWork ? { confirmedWork } : undefined), ["status", "epoch"]);
7949
8042
  if (reply.status !== "accepted")
7950
8043
  throw new ContractError("unavailable");
7951
8044
  epoch = decodeId(reply.epoch);
@@ -8266,12 +8359,17 @@ async function serverAction(s, action, requested, linger) {
8266
8359
  return { status: "unsupported" };
8267
8360
  }
8268
8361
  }
8269
- function upgrade(s, to) {
8362
+ function upgrade(s, to, stopConfirmed) {
8270
8363
  if (s.platform !== "linux")
8271
8364
  return Promise.resolve({ status: "unsupported" });
8272
8365
  return upgradeService(s, to, {
8273
8366
  commands: packageCommands(s),
8274
- stop: () => serverAction(s, "stop", undefined, false),
8367
+ stop: async () => {
8368
+ if (stopConfirmed && !await stopConfirmed())
8369
+ return { status: "unconfirmed" };
8370
+ stopConfirmed = undefined;
8371
+ return serverAction(s, "stop", undefined, false);
8372
+ },
8275
8373
  restart: () => restartInstalled(s)
8276
8374
  });
8277
8375
  }
@@ -8466,7 +8564,7 @@ async function serverMain(argv, hostUrl) {
8466
8564
  let linger = process.env.CADENCE_LINGER !== "0";
8467
8565
  let scope;
8468
8566
  let name, port;
8469
- let to, job2;
8567
+ let to, job2, confirmedWork;
8470
8568
  const seen = new Set;
8471
8569
  while (argv.length) {
8472
8570
  const flag = argv.shift();
@@ -8491,9 +8589,13 @@ async function serverMain(argv, hostUrl) {
8491
8589
  to = decodeVersion(argv.shift());
8492
8590
  else if (flag === "--job" && (action === "restart" || action === "upgrade") && argv[0])
8493
8591
  job2 = decodeId(argv.shift());
8592
+ else if (flag === "--confirmed-work" && argv[0])
8593
+ confirmedWork = decodeHash(argv.shift());
8494
8594
  else
8495
8595
  throw new ContractError("invalid");
8496
8596
  }
8597
+ if (job2 === undefined !== (confirmedWork === undefined))
8598
+ throw new ContractError("invalid");
8497
8599
  const s = hostSystem(dirname14(dirname14(fileURLToPath2(hostUrl))), ownerControl, {
8498
8600
  scope,
8499
8601
  name
@@ -8538,8 +8640,22 @@ async function serverMain(argv, hostUrl) {
8538
8640
  }
8539
8641
  }
8540
8642
  let result;
8643
+ const stopConfirmed = confirmedWork && job2 ? async () => {
8644
+ try {
8645
+ const unit = generatedUnit(s);
8646
+ if (typeof unit !== "string" || unitWorkspace(unit) !== null)
8647
+ return false;
8648
+ const store = new GatewayStore(serviceConfigPath(s));
8649
+ const recorded = new MaintenanceRecord(store).read();
8650
+ if (recorded?.id !== job2 || recorded.state !== "running" || recorded.action !== action || recorded.to !== (to ?? null) || recorded.from !== installedVersion(s.root))
8651
+ return false;
8652
+ return (await stopService(store, confirmedWork)).status === "stopped";
8653
+ } catch {
8654
+ return false;
8655
+ }
8656
+ } : undefined;
8541
8657
  try {
8542
- result = action === "upgrade" ? await upgrade(s, to) : await serverAction(s, action, requested, linger);
8658
+ result = action === "upgrade" ? await upgrade(s, to, stopConfirmed) : stopConfirmed && !await stopConfirmed() ? { status: "unconfirmed" } : await serverAction(s, action, requested, linger);
8543
8659
  } catch (error) {
8544
8660
  if (error instanceof ContractError && error.code === "invalid")
8545
8661
  process.stderr.write(`Cadence server install needs an existing absolute --workspace directory.