Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

1.8 KB
Raw Blame
import 'dart:convert';

import 'package:cryptography/dart.dart';

import 'usage_window.dart';

/// An account, as both ends of `usage_read` name it: the first 16 hex
/// characters of sha256(`<provider>:<id>`).
///
/// Hashed rather than carried raw because RPC answers are LOGGED — `WsConn`
/// writes every reply to `~/.harness/logs` — and an account id has no business
/// there. Equality survives hashing, and equality is all the strip needs: it is
/// what decides whether a remote machine's subscription is this one again or a
/// different one worth its own figure.
///
/// ⚠️ **A cross-language contract.** A remote machine computes the SAME key in
/// `autonomous-harness/cli/src/lib/accountUsage.ts` (`accountKey`), and the two
/// must agree byte for byte — drift, and every remote reading looks like a
/// different account, so the strip prints one subscription twice. Both suites
/// pin the same vector (`test/usage_account_key_test.dart` here,
/// `accountUsage.spec.ts` there) for exactly that reason.
String usageAccountKey(UsageProvider provider, String accountId) =>
    _sha16('${provider.name}:$accountId');

/// A Swarm profile (an engine account of a machine), as the CLI, the backend and
/// this app all name it: the first 16 hex characters of
/// sha256(`<machineId>:<engine>:<realpath>`), or `...:default` for the engine's
/// default account. Same contract as the CLI's `profileId`.
String swarmProfileId(String machineId, String engine, String pathOrDefault) =>
    _sha16('$machineId:$engine:$pathOrDefault');

String _sha16(String text) {
  final digest = const DartSha256().hashSync(utf8.encode(text));
  final hex = StringBuffer();
  for (final byte in digest.bytes) {
    hex.write(byte.toRadixString(16).padLeft(2, '0'));
  }
  return hex.toString().substring(0, 16);
}