CodeSampleX

示例

bare-os 3.9.3: threadCpuUsage

已验证示例 — npm bare-os 3.9.3: threadCpuUsage. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: package manifest specifies bare-os name, version 3.9.3…

sha256:0c89ae0c3ca51716e63364b5e5389b7974fe2c34cba93727ffc38349ee885053

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

案例

HOW
目标
verify threadCpuUsage in pkg:npm/bare-os@3.9.3
包
符号
  • threadCpuUsage
环境
node 22.23.2
创建时间
2026-08-27T09:08:11Z

契约

  1. package manifest specifies bare-os name, version 3.9.3, addon configuration, and Bare engine requirement
  2. declares CpuUsage interface and threadCpuUsage function accepting optional previous CpuUsage in index.d.ts
  3. native binding implements bare_os_cpu_usage_thread querying thread CPU times with uv_getrusage_thread
  4. threadCpuUsage() returns current thread user and system CPU times in microseconds
  5. threadCpuUsage(previous) calculates diff between current and previous user and system CPU times

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify threadCpuUsage in pkg:npm/bare-os@3.9.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-os@3.9.3
Demonstrate these symbols/APIs:
  - threadCpuUsage
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: esm
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:11533e314de3e860fd4e1b83d5a7df56bae30f7e20233d67524b970639a78e0f","constraints":{"executionContext":"node"},"contract":["package manifest specifies bare-os name, version 3.9.3, addon configuration, and Bare engine requirement","declares CpuUsage interface and threadCpuUsage function accepting optional previous CpuUsage in index.d.ts","native binding implements bare_os_cpu_usage_thread querying thread CPU times with uv_getrusage_thread","threadCpuUsage() returns current thread user and system CPU times in microseconds","threadCpuUsage(previous) calculates diff between current and previous user and system CPU times"],"goal":"verify threadCpuUsage in pkg:npm/bare-os@3.9.3","kind":"HOW","packages":["pkg:npm/bare-os@3.9.3"],"schemaVersion":1,"symbols":["threadCpuUsage"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/bare-os@3.9.3"],"schemaVersion":1,"subject":"pkg:npm/bare-os@3.9.3","symbols":["threadCpuUsage"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-bare-os-thread-cpu-usage",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-os-thread-cpu-usage",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "bare-os": "3.9.3"
      }
    },
    "node_modules/bare-os": {
      "version": "3.9.3",
      "resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.9.3.tgz",
      "integrity": "sha512-fF4Q7QsyKVF5Rj0qvI8BgUNjqzC2JvQlpTaPLjVJVxYVUX5Zr9un+y3w1HmA4nNKdFmRBT8z/WmrjvXzXVerKQ==",
      "license": "Apache-2.0",
      "engines": {
        "bare": ">=1.14.0"
      }
    }
  }
}
package.json
{
  "name": "sample-bare-os-thread-cpu-usage",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "bare-os": "3.9.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify threadCpuUsage in pkg:npm/bare-os@3.9.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-os@3.9.3"
  ],
  "symbols": [
    "threadCpuUsage"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "esm",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import fs from 'node:fs';
import path from 'node:path';

const require = createRequire(import.meta.url);

// 1. Verify package manifest and exports
const pkgPath = require.resolve('bare-os/package');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

assert.equal(pkg.name, 'bare-os', 'Package name must be bare-os');
assert.equal(pkg.version, '3.9.3', 'Package version must be 3.9.3');
assert.equal(pkg.addon, true, 'Package must be marked as addon');
assert.equal(pkg.engines?.bare, '>=1.14.0', 'Package must specify Bare engine >=1.14.0');

const pkgDir = path.dirname(pkgPath);

// 2. Verify TypeScript type definitions for threadCpuUsage
const indexDts = fs.readFileSync(path.join(pkgDir, 'index.d.ts'), 'utf8');

assert.ok(
  indexDts.includes('export interface CpuUsage {\n  user: number\n  system: number\n}'),
  'index.d.ts must declare CpuUsage interface with user and system properties'
);
assert.ok(
  indexDts.includes('export function threadCpuUsage(previous?: CpuUsage): CpuUsage'),
  'index.d.ts must declare threadCpuUsage function accepting optional previous CpuUsage'
);

// 3. Verify C native binding implementation for threadCpuUsage
const bindingC = fs.readFileSync(path.join(pkgDir, 'binding.c'), 'utf8');

assert.ok(
  bindingC.includes('bare_os_cpu_usage_thread(js_env_t *env, js_callback_info_t *info)'),
  'binding.c must implement bare_os_cpu_usage_thread'
);
assert.ok(
  bindingC.includes('uv_getrusage_thread(&usage)'),
  'binding.c must query thread resource usage via uv_getrusage_thread'
);
assert.ok(
  bindingC.includes('V("threadCpuUsage", bare_os_cpu_usage_thread)'),
  'binding.c must export threadCpuUsage function'
);

// 4. Verify runtime behavior of threadCpuUsage function
const bindingPath = path.join(pkgDir, 'binding.js');

let simulatedThreadUsage = { user: 120000, system: 45000 };

require.cache[bindingPath] = {
  id: bindingPath,
  filename: bindingPath,
  loaded: true,
  exports: {
    platform: process.platform,
    arch: process.arch,
    threadCpuUsage: () => ({ ...simulatedThreadUsage })
  }
};

const bareOs = require('bare-os');

assert.equal(typeof bareOs.threadCpuUsage, 'function', 'bare-os must export threadCpuUsage function');

// Test 4a: Calling threadCpuUsage() without previous usage returns current thread CPU times
const usage1 = bareOs.threadCpuUsage();
assert.equal(typeof usage1, 'object', 'threadCpuUsage must return an object');
assert.equal(usage1.user, 120000, 'usage.user must match simulated user time in microseconds');
assert.equal(usage1.system, 45000, 'usage.system must match simulated system time in microseconds');

// Test 4b: Calling threadCpuUsage(previous) computes delta
simulatedThreadUsage = { user: 180000, system: 70000 };
const usageDiff = bareOs.threadCpuUsage(usage1);

assert.equal(usageDiff.user, 60000, 'diff.user must be 180000 - 120000 = 60000');
assert.equal(usageDiff.system, 25000, 'diff.system must be 70000 - 45000 = 25000');

// Test 4c: Calling threadCpuUsage with zero previous
const usageDiffZero = bareOs.threadCpuUsage({ user: 0, system: 0 });
assert.equal(usageDiffZero.user, 180000, 'diff against zero must equal current user time');
assert.equal(usageDiffZero.system, 70000, 'diff against zero must equal current system time');

console.log('Contract tests passed successfully.');

原始种子者

匿名