Files
cowork-local/tests/fakes/fake_tool_policy.py
T
f9f6bc01fd
CI / test (push) Canceled after 0s
Feature/delta team/epic r04 (#7)
## Summary

epic r04 - begin refactor

## Change Type

- [x] Cowork feature
- [ ] Bug fix
- [ ] Core AI contribution
- [ ] Test / hardening
- [ ] Performance
- [ ] Documentation

## Related Work

Cowork Task:

Core Repo: http://34.143.229.138/gitea-admin/fsg-ai-core-assets

Core AI Issue:

Core Task:

Related PR:

## Scope

What is intentionally included?

What is intentionally NOT included?

## Validation

- [ ] Unit tests
- [ ] Integration tests
- [ ] Manual verification
- [ ] Regression check

Commands / evidence:

## Security Impact

Permission / credential / network / customer data impact:

## Compatibility

- [ ] No breaking change
- [ ] Breaking change documented

## Reviewer Notes

Anything Cowork reviewers should pay attention to.

---------

Co-authored-by: Anh Tran Nguyen Minh <anhtnm1@fpt.com>
Co-authored-by: Huong Le Thi Thien <huongltt35@fpt.com>
Co-authored-by: Nam Pham Dinh Thanh <nampdt@fpt.com>
Co-authored-by: Vu Dam Tuan <vudt15@fpt.com>
Co-authored-by: Hiep Ha Van <hiephv3@fpt.com>
Co-authored-by: Lam Hoang Van <lamhv7@fpt.com>
Reviewed-on: #7
Co-authored-by: Duy Le Huu <duylh19@fpt.com>
2026-08-31 05:15:13 +00:00

45 lines
1.7 KiB
Python

"""ToolPolicyGateway giả — để N3 (Co4E) chạy được khi Team Hoa chưa cài đặt.
Mặc định cho qua hết, vì phần lớn test Co4E quan tâm tới luồng workflow chứ
không phải chính sách. Test nào cần kiểm nhánh bị chặn thì lập trình câu trả
lời::
gate = FakeToolPolicyGateway(rules={"run_command": deny("cấm trong Co4E")})
"""
from __future__ import annotations
from typing import Callable, Dict
from cowork_local.domain.security.tool_policy import (
PolicyDecision, ToolCallRequest, allow,
)
class FakeToolPolicyGateway:
"""Cổng chính sách trong bộ nhớ, có ghi lại đã hỏi những gì."""
def __init__(self, rules: Dict[str, PolicyDecision] | None = None,
default: PolicyDecision | None = None,
decide: Callable[[ToolCallRequest], PolicyDecision] | None = None):
#: {tên tool: quyết định} — tra trước default
self.rules = dict(rules or {})
self.default = default or allow()
#: hàm tự quyết, dùng khi cần logic phức tạp hơn tra bảng
self._decide = decide
#: mọi lời gọi đã đi qua — để test khẳng định "có hỏi cổng không"
self.seen: list[ToolCallRequest] = []
def check(self, request: ToolCallRequest) -> PolicyDecision:
self.seen.append(request)
if self._decide is not None:
return self._decide(request)
return self.rules.get(request.name, self.default)
# ---- tiện cho test --------------------------------------------------
def asked_for(self, name: str) -> bool:
return any(r.name == name for r in self.seen)
@property
def call_count(self) -> int:
return len(self.seen)