@nowcrew/daemon 0.6.2 → 0.6.4

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.
@@ -8,7 +8,7 @@ import { abilityWorkspaceProjectionPath, DaemonAbilityLockSchema, DaemonAbilityM
8
8
  import { loadAgentAbilityRuntimeContext } from "./runtime-context.js";
9
9
  import { parseSkillFrontmatter } from "../skill-frontmatter.js";
10
10
  import { isProjectSkillName, MAX_PROJECT_SKILL_DESCRIPTION_LENGTH, MAX_PROJECT_SKILL_FILE_BYTES, } from "../project-skills/types.js";
11
- import { createAgentProjectionCoordinator, } from "../project-skills/agent-projection-coordinator.js";
11
+ import { createAgentProjectionCoordinator, runtimeProjectionLifetime, } from "../project-skills/agent-projection-coordinator.js";
12
12
  const exists = (path) => lstat(path).then(() => true, () => false);
13
13
  const repositoryKey = (url) => createHash("sha256").update(url).digest("hex");
14
14
  const posixPath = (value) => value.split(sep).join("/");
@@ -208,6 +208,16 @@ const verifyCache = async (agentsRoot, ability) => {
208
208
  };
209
209
  const layerRelativePath = (path, layer) => path.startsWith(`${layer}/`) ? path.slice(layer.length + 1) : path;
210
210
  const readManagedCatalog = async (trainingRoot) => JSON.parse(await readFile(join(trainingRoot, "ACTIVE_RELEASE.json"), "utf8"));
211
+ const activeTrainingRoot = async (agentsRoot, handle, ability) => {
212
+ const trainingRoot = join(agentsRoot, handle, "training");
213
+ const catalog = await readManagedCatalog(trainingRoot).catch(() => null);
214
+ return catalog?.managedBy === "nowcrew-agent-training"
215
+ && catalog.releaseId === ability.releaseId
216
+ && catalog.rootCommit === ability.rootCommit
217
+ && catalog.artifactDigest === ability.artifactDigest
218
+ ? trainingRoot
219
+ : null;
220
+ };
211
221
  const materializeWorkspaceTraining = async (agentsRoot, handle, ability) => {
212
222
  let verified;
213
223
  try {
@@ -372,11 +382,22 @@ export function createAgentAbilityMaterializer(expectedAgentsRoot, coordinator =
372
382
  }
373
383
  });
374
384
  },
375
- async prepareAndLaunch(agentsRoot, handle, ability, launch) {
376
- return withAgentTurn(handle, async () => {
385
+ prepareAndLaunch(agentsRoot, handle, ability, launch) {
386
+ if (agentsRoot !== expectedAgentsRoot)
387
+ return Promise.reject(new Error("ability_agents_root_mismatch"));
388
+ return coordinator.runExclusiveUntil(expectedAgentsRoot, handle, async () => {
377
389
  const started = Date.now();
378
390
  try {
379
- const trainingRoot = await apply(agentsRoot, handle, ability).then(() => join(agentsRoot, handle, "training"));
391
+ const existingTrainingRoot = await activeTrainingRoot(agentsRoot, handle, ability);
392
+ let trainingRoot;
393
+ if (existingTrainingRoot !== null) {
394
+ trainingRoot = existingTrainingRoot;
395
+ await projectWorkspaceTraining(agentsRoot, handle, ability, existingTrainingRoot);
396
+ }
397
+ else {
398
+ await apply(agentsRoot, handle, ability);
399
+ trainingRoot = join(agentsRoot, handle, "training");
400
+ }
380
401
  const context = await loadAgentAbilityRuntimeContext(join(agentsRoot, handle), trainingRoot, ability);
381
402
  dslog("ability.execution.materialized", "Agent ability release materialized", {
382
403
  level: "INFO", agent_handle: handle, release_id: ability.releaseId,
@@ -392,7 +413,7 @@ export function createAgentAbilityMaterializer(expectedAgentsRoot, coordinator =
392
413
  });
393
414
  throw error;
394
415
  }
395
- });
416
+ }, runtimeProjectionLifetime);
396
417
  },
397
418
  };
398
419
  }
@@ -1,5 +1,11 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { createKeyedPromiseTail } from "../promise-tail.js";
3
+ export const runtimeProjectionLifetime = (value) => {
4
+ if (typeof value !== "object" || value === null || !("exit" in value))
5
+ return Promise.resolve();
6
+ const exit = value.exit;
7
+ return exit instanceof Promise ? exit.catch(() => undefined) : Promise.resolve();
8
+ };
3
9
  export function createAgentProjectionCoordinator() {
4
10
  const tails = createKeyedPromiseTail();
5
11
  const heldKeys = new AsyncLocalStorage();
@@ -11,5 +17,30 @@ export function createAgentProjectionCoordinator() {
11
17
  return operation();
12
18
  return tails.enqueue(key, () => heldKeys.run(new Set([...(held ?? []), key]), operation));
13
19
  },
20
+ runExclusiveUntil(agentsRoot, handle, operation, releaseWhen) {
21
+ const key = JSON.stringify([agentsRoot, handle]);
22
+ const held = heldKeys.getStore();
23
+ if (held?.has(key))
24
+ return operation();
25
+ let resolveStarted;
26
+ let rejectStarted;
27
+ const started = new Promise((resolve, reject) => {
28
+ resolveStarted = resolve;
29
+ rejectStarted = reject;
30
+ });
31
+ void tails.enqueue(key, () => heldKeys.run(new Set([...(held ?? []), key]), async () => {
32
+ let value;
33
+ try {
34
+ value = await operation();
35
+ }
36
+ catch (error) {
37
+ rejectStarted(error);
38
+ throw error;
39
+ }
40
+ resolveStarted(value);
41
+ await releaseWhen(value);
42
+ })).catch(() => { });
43
+ return started;
44
+ },
14
45
  };
15
46
  }
@@ -1,6 +1,7 @@
1
1
  import { cp, lstat, mkdir, readdir, readlink, rename, rm, stat, symlink, writeFile } from "node:fs/promises";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { dirname, join, resolve, sep } from "node:path";
4
+ import { runtimeProjectionLifetime, } from "./agent-projection-coordinator.js";
4
5
  export class ProjectProjectionError extends Error {
5
6
  code;
6
7
  constructor(code) {
@@ -135,10 +136,10 @@ export function createProjectSkillsReconciler(deps) {
135
136
  if (agentsRoot !== deps.agentsRoot) {
136
137
  return Promise.reject(new ProjectProjectionError("skill_projection_failed"));
137
138
  }
138
- return deps.coordinator.runExclusive(deps.agentsRoot, handle, async () => {
139
+ return deps.coordinator.runExclusiveUntil(deps.agentsRoot, handle, async () => {
139
140
  await reconcileUnlocked(handle, bindings);
140
141
  return launch();
141
- });
142
+ }, runtimeProjectionLifetime);
142
143
  },
143
144
  };
144
145
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nowcrew/daemon",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "type": "module",
5
5
  "description": "crew daemon — 运行在用户机器:拉起/管理 agent 进程,注入 crew CLI,归一化 runtime 事件",
6
6
  "license": "Apache-2.0",