Python API Reference¶
Top-level exports¶
humanproof
¶
humanproof - Motor-noise fingerprinting + HITL approval gates for agents.
CalibratedMotorScorer(noise_threshold, correction_threshold)
¶
Bases: MotorScorer
A MotorScorer with calibrated thresholds from labeled examples.
Source code in src/humanproof/calibration.py
ApprovalError
¶
Bases: ValueError
Raised when an action is refused for missing/invalid approval.
ApprovalSession(*, high_risk_actions=None, max_high_risk_per_session=None, max_mass_actions_per_session=DEFAULT_MAX_MASS_ACTIONS_PER_SESSION, max_recipients_per_mass=DEFAULT_MAX_RECIPIENTS, session_id=None)
¶
In-process approval ledger for one agent run / publish session.
Enforces
- required token for high-risk actions
- single-use (or capped) consumption
- optional runaway budget (max high-risk successes per session)
This is the load-bearing reader for APPROVAL-GATE - not a flag file.
Source code in src/humanproof/closed_loop.py
classify(action)
¶
Return high_risk or safe for action.
Source code in src/humanproof/closed_loop.py
issue(action='*', *, ttl_seconds=3600.0, max_uses=1, issuer='owner', metadata=None)
¶
Mint a human approval token. Call only from owner / HITL UI.
Source code in src/humanproof/closed_loop.py
ApprovalToken(token_id, secret, action, issued_at, expires_at=None, max_uses=1, uses=0, issuer='owner', metadata=dict())
dataclass
¶
Single-use (or multi-use) human-issued approval credential.
Agents must never create these for themselves in production; only a human
(or an out-of-band owner control plane) calls :meth:ApprovalSession.issue.
GateOutcome(ok, verdict, reason, exit_code, action=None, risk=None, human_required=False, token_id=None, approvals_remaining=None, recipient_count=0, mass_action_count=0)
dataclass
¶
Result of an approval or mass-action gate check.
Attributes:
| Name | Type | Description |
|---|---|---|
ok |
bool
|
True only when the action may proceed. |
verdict |
str
|
|
reason |
str
|
Human-readable explanation (always non-empty). |
exit_code |
int
|
0 PASS, 1 FAIL (policy deny), 2 FAIL_LOUD (missing/empty). |
action |
str | None
|
Canonical action name that was gated. |
risk |
str | None
|
|
human_required |
bool
|
True when a human must issue a token. |
token_id |
str | None
|
Consumed or matched token id when present. |
approvals_remaining |
int | None
|
Budget remaining in the session after this check. |
recipient_count |
int
|
Recipients / targets in a mass-action gate. |
mass_action_count |
int
|
Mass actions already passed this session. |
MotorFeatures(mean_speed, speed_std, noise_ratio, correction_rate, jerk_mean, jerk_std, max_speed, smoothness)
dataclass
¶
Statistical features extracted from an input trajectory.
Attributes:
| Name | Type | Description |
|---|---|---|
mean_speed |
float
|
Mean speed in pixels/ms. |
speed_std |
float
|
Standard deviation of speed. |
noise_ratio |
float
|
std/mean speed. Humans ~0.4-0.8, AIs ~0.05-0.2. |
correction_rate |
float
|
Corrections per sample. Humans ~0.15-0.35. |
jerk_mean |
float
|
Mean absolute jerk. |
jerk_std |
float
|
Standard deviation of jerk. |
max_speed |
float
|
Maximum speed. |
smoothness |
float
|
Inverse of mean jerk (higher = smoother, more AI-like). |
to_dict()
¶
Serialize to a JSON-compatible dict.
Source code in src/humanproof/scorer.py
MotorScore(trajectory_id, features, human_score, ai_score, verdict, flags)
dataclass
¶
The scored result for a single trajectory.
Attributes:
| Name | Type | Description |
|---|---|---|
trajectory_id |
str
|
ID of the scored trajectory. |
features |
MotorFeatures
|
Extracted motor features. |
human_score |
float
|
Probability of human input [0.0, 1.0]. |
ai_score |
float
|
Probability of AI input (1.0 - human_score). |
verdict |
str
|
"human", "ai", or "uncertain". |
flags |
list[str]
|
List of flagged anomalies. |
to_dict()
¶
Serialize to a JSON-compatible dict.
Source code in src/humanproof/scorer.py
MotorScorer
¶
Score trajectories using threshold-based heuristics on motor features.
Human ranges: noise_ratio > 0.3, correction_rate > 0.1, smoothness < 5.0 AI ranges: noise_ratio < 0.15, correction_rate < 0.05, smoothness > 8.0
extract_features(traj)
¶
Extract MotorFeatures from a trajectory.
Source code in src/humanproof/scorer.py
score(traj)
¶
Score a trajectory and return a MotorScore.
Source code in src/humanproof/scorer.py
batch_score(trajs)
¶
Score multiple trajectories and return one MotorScore per trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajs
|
list[InputTrajectory]
|
List of InputTrajectory objects to score. |
required |
Returns:
| Type | Description |
|---|---|
list[MotorScore]
|
List of MotorScore objects in the same order as trajs. |
Source code in src/humanproof/scorer.py
InputSample(dx, dy, dt, timestamp=0.0)
dataclass
¶
A single mouse/aim input sample.
Attributes:
| Name | Type | Description |
|---|---|---|
dx |
float
|
X-axis delta (pixels). |
dy |
float
|
Y-axis delta (pixels). |
dt |
float
|
Time delta in milliseconds (must be > 0). |
timestamp |
float
|
Absolute timestamp in milliseconds. |
InputTrajectory(samples, session_id='')
dataclass
¶
A sequence of input samples forming a trajectory.
Attributes:
| Name | Type | Description |
|---|---|---|
samples |
list[InputSample]
|
Ordered list of input samples. |
session_id |
str
|
Optional session identifier string. |
id |
str
|
SHA-256[:16] fingerprint computed from session_id, length, first/last sample. |
velocity_profile()
¶
acceleration_profile()
¶
Compute acceleration at each step (pixels/ms^2). Length = n-1.
jerk_profile()
¶
correction_count()
¶
Count direction reversals (velocity sign flips in x or y).
Source code in src/humanproof/trajectory.py
noise_ratio()
¶
Compute std(velocity) / mean(abs(velocity)). Returns 0.0 if mean is 0.
Source code in src/humanproof/trajectory.py
to_dict()
¶
Serialize to a JSON-compatible dict.
from_dict(d)
classmethod
¶
Deserialize from a dict produced by to_dict().
Source code in src/humanproof/trajectory.py
batch_score(trajectories, scorer=None)
¶
Score a list of trajectories and return aggregated BatchScoreResult.
Source code in src/humanproof/batch.py
score_from_csv(csv_path)
¶
Load trajectories from CSV (columns: trajectory_id,t,x,y,button) and score them.
Each unique trajectory_id forms one InputTrajectory. Rows are sorted by t. x,y are treated as absolute positions; dx/dy are computed from consecutive rows. button column is ignored (reserved for future use).
Source code in src/humanproof/batch.py
apply_calibration(scorer, calibration)
¶
Return a CalibratedMotorScorer with calibrated thresholds.
Source code in src/humanproof/calibration.py
calibrate(human_trajectories, ai_trajectories)
¶
Find optimal decision thresholds via grid search over noise and correction thresholds.
Source code in src/humanproof/calibration.py
assert_approved(action, token=None, **kwargs)
¶
Gate and raise :class:ApprovalError unless outcome is ok.
Source code in src/humanproof/closed_loop.py
assert_mass_action_ok(action, recipients=None, **kwargs)
¶
Raise :class:ApprovalError unless :func:gate_mass_action is ok.
Source code in src/humanproof/closed_loop.py
gate_approval(action, token=None, *, session=None, secret=None, consume=True)
¶
Gate a proposed action: high-risk requires a valid human token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
Proposed action name (e.g. |
required |
token
|
ApprovalToken | str | None
|
:class: |
None
|
session
|
ApprovalSession | None
|
Session ledger; created empty if omitted (then only safe actions pass). |
None
|
secret
|
str | None
|
Optional secret if token is a token_id string (constant-time check). |
None
|
consume
|
bool
|
If True (default), successful high-risk checks increment token uses. |
True
|
Returns:
| Type | Description |
|---|---|
GateOutcome
|
class: |
Source code in src/humanproof/closed_loop.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
gate_mass_action(action, recipients=None, *, token=None, session=None, secret=None, max_recipients=None, require_inventory=True, consume=True)
¶
Block unattended bulk email/delete (OpenClaw mass-email class).
Load-bearing controls:
- Classify - action must be mass/bulk class (or still go through
:func:
gate_approvalif high-risk single). - Inventory - named recipients/targets required when
require_inventory(empty list → FAIL_LOUD). - Bulk limit - recipient count over session/default max without a valid approval token → FAIL_LOUD.
- Approval - always requires human token via :func:
gate_approvalfor mass actions (never unattended). - Session mass budget - max mass actions per session (AgentWatch class).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
e.g. |
required |
recipients
|
Sequence[str] | None
|
Explicit list of email addresses / targets / ids. |
None
|
token
|
ApprovalToken | str | None
|
Human approval token (required for mass actions). |
None
|
session
|
ApprovalSession | None
|
Approval session (mass + high-risk budgets). |
None
|
secret
|
str | None
|
Optional secret when token is a token_id string. |
None
|
max_recipients
|
int | None
|
Override max recipients (default session or 50). |
None
|
require_inventory
|
bool
|
If True, empty recipients FAIL_LOUD. |
True
|
consume
|
bool
|
Pass-through to :func: |
True
|
Source code in src/humanproof/closed_loop.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
is_mass_action(action)
¶
True if action is a bulk external side effect (email/delete class).
Source code in src/humanproof/closed_loop.py
require_token_for(action, session)
¶
Convenience: gate without a token - always FAIL_LOUD for high-risk.
Useful in tests and CI to prove the unattended path is blocked.
Source code in src/humanproof/closed_loop.py
analyze_session(session_id, trajectories)
¶
Analyze a gaming session consisting of multiple trajectories.
Source code in src/humanproof/session.py
detect_shift(scores, window=3, threshold=0.3)
¶
Detect the index where mean score changed by more than threshold.
Compares the mean of the window before vs after each index. Returns the first index where the shift exceeds the threshold.