Metadata-Version: 2.5
Name: perceptkit
Version: 0.2.3
Summary: 从设备信号判断:有没有发生值得留意的事、值不值得叫醒一次 agent、以及该怎么把此刻的状况讲给它听。不采集数据、不碰存储、不调模型。
License: 
                                         Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
Keywords: agent,context,perception,proactive,wake
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == 'dev'
Description-Content-Type: text/markdown

# perceptkit

**一套可插拔的主动感知：从设备上报，到"该戳一下 agent 了"。**

设备把采到的东西按标准格式交给它，它负责校验、标准化、去重、落库编排、
按你配的规则判断"这算不算一件事"、生成事件、可靠地送到你的 agent runtime。

你要写的只有两个接口：**数据存哪儿**（`StoragePort`）和**事件交给谁**（`WakePort`）。

```python
from perceptkit import PerceptionKit, IngestContext

kit = PerceptionKit(storage=my_storage, wake=my_runtime, definitions=my_rules)

result = kit.ingest(report, context=IngestContext(
    subject_id=user_id,        # 从已认证连接来,绝不信客户端自报
    received_at=now,           # 你的钟
))
# 事件已经落地了。投递由你自己的 worker 驱动:
kit.dispatch_pending(worker_id="w1", now=now)
```

它**不采集数据、不实现数据库、不做加解密、不调模型、不读时钟** —— 那些是宿主的事。
零第三方依赖（有 AST 测试盯着这条）。

**顺序不在你手里，这是有意的。** "先落地再投递""迟到的数据不覆盖当前值"
"同一件事崩溃重投也只提醒一次" —— 这些是正确性问题，留给每个宿主自己发挥的结果是
同一个包装到四个宿主上、四种可靠性。所以顺序在包里定死，你只填被调用的方法体。
就像你写 React 组件但不决定什么时候渲染。

**现状：还没发布到 PyPI**，`pip install perceptkit` 现在装不到东西。
能确认好用的两条路：

```bash
git clone git@github.com:teleport-computer/perceptkit.git
cd perceptkit
uv run pytest                        # 283 条测试
uv run python3 examples/quickstart.py   # 一分钟看它怎么判断
```

想在别的项目里依赖它：

```toml
# 另一个项目的 pyproject.toml
perceptkit = { git = "ssh://git@github.com/teleport-computer/perceptkit.git" }
```

或者本地路径依赖（两个仓库在同一台机器上时）：

```bash
uv add --editable ../perceptkit
```

---

## 〇、包里有什么

```
contracts/    四个信封(上报 / 观测 / 事件 / 回执) + 三态 + 可信上下文
              + 八个逻辑存储对象 + 投递状态机
manifest/     每个字段的单一声明处(类型/单位/TTL/保留期/聚合/趋势/隐私)
              + 四条自动检查,让"忘了声明"变成一条红色的测试
ports/        StoragePort / WakePort —— 你要实现的两个接口
processing/   校验 → 标准化 → 去重 → 落库 → 当前值 → 聚合 → 求值 → 发件箱
rules/        九种内置规则 + 生命周期 + 自定义 evaluator 接口
queries/      读取侧八个函数(带 TTL 判定、趋势模型选择、缺数据显式化、隐私投影)
conformance/  十条一致性保证的检查套件 + 内存版存储(测试工具)
algorithms    归属日期 / 连续天数 / 趋势模型 / 日聚合 / 去重键 …(原有纯函数)
```

**一句话分工**：宿主决定怎么存、怎么投、说不说话；这个包规定存哪些事实、
按什么顺序、满足什么保证。

---

## 一、它跟这几类东西不是一回事

```
地理围栏 / 系统事件检测 SDK   它们负责"测到了什么"（到没到某个地点、
（Radar、CoreLocation region      解没解锁、屏幕有没有变化）；这个包接手
monitoring、系统健康/屏幕时间框架）  的是下一步——"测到的这个变化，够不够
                                  资格戳一下 agent"，它自己不做检测。

推送时机优化 / 用户参与度引擎     它们的目标函数是"提高打开率/参与度"；
（send-time optimization、          这个包没有目标函数，只回答一道是非题：
engagement 引擎）                    值不值得，不掺杂"怎么让人多点开"。

健康看板 / Quantified-Self App    它们的核心产品就是把数字摆给你看
（Apple 健康、Oura、WHOOP 这类）    （心率曲线、睡眠时长）；这个包的 glance
                                  刻意反过来——从不吐出一个数字。

主动性 agent 运行时 / 编排框架    "戳醒之后该不该开口、开口说什么"，这些
                                  是宿主运行时的职责；这个包只回答到
                                  "值不值得戳"为止，一步都不往前多走。
```

它是**要不要打扰的判断力**，不是感知系统、不是增长引擎、不是健康 App、
也不是对话运行时。

---

## 二、一分钟跑通

```bash
uv run python3 examples/quickstart.py
```

不联网、不需要 API key、不碰任何存储。走一个具体场景：老王最近几晚睡得
不好，此刻手机检测到他到了常去的"公司"——从一条原始测量，一路走到
"该怎么把此刻讲给模型听"。

> 注意：直接 `python3 examples/quickstart.py`（不经过 `uv run`）会报
> `ModuleNotFoundError: No module named 'perceptkit'`——这是预期的，本包
> 是 src-layout，没有发布安装包时只能通过 `uv run`（或先 `uv sync`/
> `pip install -e .`）让解释器找到它。

最小代码：

```python
from perceptkit.observation import classify
from perceptkit.wake import should_wake
from perceptkit.glance import build_perception_glance

# 一条测量：有值、有观测到零值、没测到、还是不可用？四态分开判断，
# 不能拿"没测到"直接当"测到是 0"，两者会喂出截然不同的错误结论。
state = classify(180.0)   # -> "observed"

# 这次到达值不值得戳一下 agent（30 秒内的第二次到达会被防抖拦下）
ok, reason = should_wake(
    "arrival", enabled_sources=("arrival",),
    last_wake_ts=1000.0, now=1030.0, debounce_sec=60.0,
)

# 此刻的状况压成一份摘要——只有 True/False，没有一个具体数字
glance = build_perception_glance({"location": {"place_label": "公司"}})
```

---

## 三、两个真正的关键判断

### 1. 戳醒 ≠ 该开口了

戳醒之后，agent 继续睡 / 只看一眼 / 开口说话，是三个平行且同等合法的
结局。这个包只回答"值不值得戳一下"，不参与"戳完之后该怎么办"，
也不产出任何"该说话了"式的措辞。

```
之前（如果 wake 直接等于开口）
  用户到公司 → 库判定"该说话" → agent 不管场合硬接一句"你到公司啦"
  → 用户在开会，手机弹了句废话

之后（这个包的实际语义）
  用户到公司 → should_wake() 只回答"这次到达值得关注"
  → 具体开不开口、说不说话，完全交给宿主运行时按当下场合自己判断
  （库的返回值里连"建议怎么说"这种字段都没有——不是没写全，是故意不给）
```

### 2. 摘要只出布尔值，从不出数字

```
之前（把数字直接喂给模型）
  "你昨晚睡了 5 小时 12 分钟"
  → 模型像念体检报告一样把这句话复述给用户，而不是自己判断要不要在意

之后（build_perception_glance 的实际产出）
  {"health": {"available": True, "notable_change": True}}
  → 模型自己决定"睡眠有显著变化"值不值得多看一眼；
    值得的话，它主动去查工具拿具体数字——这时候数字才是"被用"，
    而不是"被念出来"
```

四个信号态（`observation.classify`）、防抖与开关（`wake.should_wake`）、
连续偏离的迟滞判断（`streaks.should_trigger`）、按指标类型选对趋势读法
（`trend_models.model_for`）——都是在为这份摘要和这次判断兜底，细节见
下面目录里对应模块的模块级文档字符串。

---

## 四、库替你做的判断，与不替你做的判断

```
在库里    这个信号有哪些字段、多久算新鲜(catalog) · agent 能看哪些字段、
          该不该给(fields) · 此刻是否有事发生，只出 bool(glance) ·
          按天汇总、看出什么趋势变化(history) · 该怎么把这些讲给模型(prompts) ·
          这次变化值不值得叫醒一次 agent(wake) · 一条测量到底是"有观测到零值"
          还是"压根没测到"(observation) · 一条测量该记在本地日历的哪一天
          (attribution) · "连续 N 天"怎么断续、怎么只在跨入异常时触发一次
          (streaks) · 三种趋势判读方式：波动/漂移/周期性(trend_models) ·
          每类信号的历史该留多久(retention) · 一条测量的去重键怎么造
          (identity)

不在库里  采集数据 · 存储 · 加解密 · 账号身份与鉴权 · 定时器/调度 ·
          真正调模型 · 叫醒之后该说什么话
```

---

## 五、目录

```
src/perceptkit/
  catalog.py       信号有哪些字段、多久算新鲜
  fields.py        agent 能看哪些字段 + 权限判断
  glance.py        此刻的纯 bool 摘要
  history.py       按天汇总、显著变化
  prompts.py       该怎么把这些讲给模型
  wake.py          值不值得叫醒一次 agent
  observation.py   四态：observed / observed-zero / not-observed / unavailable
  attribution.py   一条测量归本地日历的哪一天
  streaks.py       "连续 N 天"的可执行定义
  trend_models.py  三种趋势判读方式：波动/漂移/周期性
  retention.py     每类信号的历史留多久
  identity.py      一条测量的去重键
```

## 六、现状

107 条测试全绿（含 `test_purity.py` 的 AST 扫描：本包只依赖标准库，
以及 `test_no_host_leakage.py`：不带宿主内部痕迹）。`examples/quickstart.py`
本身也有一条测试盯着（`tests/test_examples.py`）——它必须一直能跑，
不能悄悄烂掉。

## 许可

Apache-2.0
