@kingsoftcloud/agentkit-harness-node 0.0.19 → 0.0.21

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.
@@ -18,7 +18,7 @@ import { publicErrorMessage, publicErrorCode, publicFailureContent } from '@king
18
18
  */
19
19
  import { createHash } from 'node:crypto';
20
20
  import {
21
- chmodSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readdirSync,
21
+ chmodSync, closeSync, constants, existsSync, lstatSync, mkdirSync, openSync, readdirSync,
22
22
  readFileSync, readlinkSync, readSync, realpathSync, rmdirSync, statSync,
23
23
  unlinkSync,
24
24
  } from 'node:fs';
@@ -59,6 +59,8 @@ const READ_ONLY = new Set([
59
59
 
60
60
  const OBSERVE_MAX_ENTRIES = 2048;
61
61
  const OBSERVE_MAX_HASHED = 64 * 1024 * 1024;
62
+ const DELIVERY_MAX_FILE_BYTES = 128 * 1024;
63
+ const DELIVERY_MAX_TEXT_BYTES = 4 * 1024 * 1024;
62
64
 
63
65
  class CaptureError extends Error {
64
66
  constructor(reason) { super(reason); this.code = reason; this.name = 'CaptureError'; }
@@ -1331,9 +1333,9 @@ export function createNativeWorkspaceTools({
1331
1333
  return 'sha256:' + captureStable(real).digest;
1332
1334
  }
1333
1335
 
1334
- function captureStable(real) {
1335
- const first = captureInventory(real);
1336
- const second = captureInventory(real);
1336
+ function captureStable(real, delivery = false) {
1337
+ const first = captureInventory(real, false, delivery);
1338
+ const second = captureInventory(real, delivery, delivery);
1337
1339
  if (first.digest !== second.digest
1338
1340
  || first.entryCount !== second.entryCount
1339
1341
  || !entriesEqual(first.entries, second.entries)) {
@@ -1342,13 +1344,14 @@ export function createNativeWorkspaceTools({
1342
1344
  return second;
1343
1345
  }
1344
1346
 
1345
- function captureInventory(real) {
1347
+ function captureInventory(real, delivery = false, ignoreGit = false) {
1346
1348
  const hash = createHash('sha256');
1347
1349
  hash.update('ksadk.directory-workspace/v1\0');
1348
- const state = { entries: 0, hashedBytes: 0, list: [], byPath: Object.create(null) };
1350
+ const state = { entries: 0, hashedBytes: 0, list: [], byPath: Object.create(null),
1351
+ delivery: delivery ? Object.create(null) : null, deliveryBytes: 0, ignoreGit };
1349
1352
  scanDir(real, '', hash, state);
1350
1353
  state.list.sort();
1351
- return { digest: hash.digest('hex'), entryCount: state.entries, entries: state.list, byPath: state.byPath };
1354
+ return { digest: hash.digest('hex'), entryCount: state.entries, entries: state.list, byPath: state.byPath, delivery: state.delivery };
1352
1355
  }
1353
1356
 
1354
1357
  /** Compare two sorted entry-identity lists for exact equality. */
@@ -1364,6 +1367,7 @@ export function createNativeWorkspaceTools({
1364
1367
  try { dirents = readdirSync(dir, { withFileTypes: true }); } catch { throw new CaptureError('capture_failed'); }
1365
1368
  dirents.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
1366
1369
  for (const d of dirents) {
1370
+ if (!rel && state.ignoreGit && d.name === '.git') continue;
1367
1371
  const entryRel = rel ? `${rel}/${d.name}` : d.name;
1368
1372
  if (entryRel.includes('\\') || entryRel.includes('\0')) throw new CaptureError('unsupported_workspace_path');
1369
1373
  state.entries += 1;
@@ -1391,7 +1395,11 @@ export function createNativeWorkspaceTools({
1391
1395
  } else if (info.isFile()) {
1392
1396
  if (state.hashedBytes + Number(info.size) > OBSERVE_MAX_HASHED) throw new CaptureError('capture_limit_exceeded');
1393
1397
  const fileHash = createHash('sha256');
1394
- const fd = openSync(abs, 'r');
1398
+ const size = Number(info.size);
1399
+ const deliveryFile = state.delivery;
1400
+ const retained = deliveryFile && size <= DELIVERY_MAX_FILE_BYTES && state.deliveryBytes + size <= DELIVERY_MAX_TEXT_BYTES
1401
+ ? Buffer.alloc(size) : null;
1402
+ const fd = openSync(abs, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
1395
1403
  let readBytes = 0;
1396
1404
  try {
1397
1405
  let remaining = Number(info.size);
@@ -1399,6 +1407,7 @@ export function createNativeWorkspaceTools({
1399
1407
  while (remaining > 0) {
1400
1408
  const n = readSync(fd, buf, 0, Math.min(buf.length, remaining), null);
1401
1409
  if (n === 0) break; // short read: file shrank under us
1410
+ if (retained) buf.copy(retained, readBytes, 0, n);
1402
1411
  readBytes += n;
1403
1412
  state.hashedBytes += n;
1404
1413
  if (state.hashedBytes > OBSERVE_MAX_HASHED) throw new CaptureError('capture_limit_exceeded');
@@ -1415,6 +1424,15 @@ export function createNativeWorkspaceTools({
1415
1424
  const fileDigest = fileHash.digest();
1416
1425
  hash.update('\0file\0');
1417
1426
  hash.update(fileDigest);
1427
+ if (deliveryFile) {
1428
+ state.delivery[entryRel] = { digest: fileDigest.toString('hex') };
1429
+ if (retained && !retained.includes(0)) {
1430
+ try {
1431
+ state.delivery[entryRel].text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(retained);
1432
+ state.deliveryBytes += size;
1433
+ } catch { /* Binary files carry their identity only. */ }
1434
+ }
1435
+ }
1418
1436
  state.list.push(`${entryRel}\tfile\t${before}\t${fileDigest.toString('hex')}`);
1419
1437
  } else {
1420
1438
  throw new CaptureError('unsupported_workspace_entry');
@@ -1589,6 +1607,12 @@ export function createNativeWorkspaceTools({
1589
1607
  }),
1590
1608
  // Host-only change capture for a governed command. It shares the bounded,
1591
1609
  // stable observer; paths remain structured even when names contain tabs.
1610
+ captureDelivery() {
1611
+ checkRootBoundary();
1612
+ const captured = captureStable(pinnedRootReal, true);
1613
+ checkRootBoundary();
1614
+ return captured.delivery;
1615
+ },
1592
1616
  capture(request) {
1593
1617
  if (request.sessionId !== store.sessionId) throw new CaptureError('workspace_session_mismatch');
1594
1618
  checkRootBoundary(); const captured = captureStable(pinnedRootReal);