verifiableplanformat 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- verifiableplanformat-0.1.0.dist-info/METADATA +85 -0
- verifiableplanformat-0.1.0.dist-info/RECORD +13 -0
- verifiableplanformat-0.1.0.dist-info/WHEEL +4 -0
- verifiableplanformat-0.1.0.dist-info/entry_points.txt +2 -0
- verifiableplanformat-0.1.0.dist-info/licenses/LICENSE +202 -0
- vpf/__init__.py +32 -0
- vpf/__main__.py +7 -0
- vpf/check.py +378 -0
- vpf/cli.py +86 -0
- vpf/py.typed +0 -0
- vpf/reasons.py +38 -0
- vpf/types.py +89 -0
- vpf/words.py +95 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: verifiableplanformat
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read, check and show plans in the Verifiable Plan Format (VPF).
|
|
5
|
+
Project-URL: Homepage, https://verifiableplanformat.org
|
|
6
|
+
Project-URL: Repository, https://github.com/verifiableplanformat/python-sdk
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: agents,plan,verification,vpf,workflow
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Typing :: Typed
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# VPF for Python
|
|
17
|
+
|
|
18
|
+
Read, check and show plans in the [Verifiable Plan Format](https://verifiableplanformat.org) (VPF), from Python.
|
|
19
|
+
Every step of a plan says what is wanted, who does it and what proves it is done.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
pip install verifiableplanformat
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The package is imported as `vpf`. It needs Python 3.11 or later and nothing else.
|
|
26
|
+
|
|
27
|
+
## Check a plan
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from vpf import check
|
|
31
|
+
|
|
32
|
+
report = check(text)
|
|
33
|
+
if report.may_run:
|
|
34
|
+
print(report.plan["wish"])
|
|
35
|
+
else:
|
|
36
|
+
for problem in report.problems:
|
|
37
|
+
print(problem) # reason: step: sentence (detail)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`check` tells every problem the specification names, each with its reason (such as `step.id`), the step it is
|
|
41
|
+
about (`None` for the whole plan), its sentence and, at times, a detail. A plan with problems may not run. A plan
|
|
42
|
+
is the plain JSON object, typed with `TypedDict`.
|
|
43
|
+
|
|
44
|
+
## Show a plan in words
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from vpf import check, words, words_text
|
|
48
|
+
|
|
49
|
+
plan = check(text).plan
|
|
50
|
+
if plan is not None:
|
|
51
|
+
print(words_text(plan)) # the plan as plain text
|
|
52
|
+
print(words(plan)) # the same, step by step, as data
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## The command
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
vpf check plan.vpf.json # every problem; ends with 1 when a plan may not run
|
|
59
|
+
vpf check --json *.vpf.json # the same, in JSON
|
|
60
|
+
vpf show plan.vpf.json # the plan in words
|
|
61
|
+
python -m vpf check plan.vpf.json # the same, without the script
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The command, its output and the words are the same as those of the
|
|
65
|
+
[Rust SDK](https://github.com/verifiableplanformat/rust-sdk).
|
|
66
|
+
|
|
67
|
+
## Conformance
|
|
68
|
+
|
|
69
|
+
`conformance/` and `examples/` are copied from the [specification](https://github.com/verifiableplanformat/vpf),
|
|
70
|
+
and the tests check every case and every example. `scripts/sync-conformance.sh path/to/vpf` copies them again.
|
|
71
|
+
|
|
72
|
+
## Development
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
pip install pytest ruff mypy
|
|
76
|
+
ruff check . && ruff format --check .
|
|
77
|
+
mypy
|
|
78
|
+
pytest
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Commits follow [Conventional Commits](https://www.conventionalcommits.org) and carry a `Signed-off-by` line.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
[Apache License 2.0](LICENSE).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
vpf/__init__.py,sha256=RSXfpqhDBAKsyZDvfopIwr5bpEmAbMphBnUyCDAxavo,636
|
|
2
|
+
vpf/__main__.py,sha256=jy0ipYWIhNc8yU-d1uOw95Y_EDJ_Ozp8K97bmTxsHDs,99
|
|
3
|
+
vpf/check.py,sha256=o-qtDr-EHFPyWvJRc7p6H-3jgWr9DVyICOON9m8I3Us,14819
|
|
4
|
+
vpf/cli.py,sha256=2_b07_R2ttzGEZmZD5gfBtzZZbUknaskENsHmSNOY5A,2884
|
|
5
|
+
vpf/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
vpf/reasons.py,sha256=0P4ddpcQeanmidSsewUTKF9dAhRaTcRRxK5YU8wt35k,2330
|
|
7
|
+
vpf/types.py,sha256=oci8QbdbHfOidoDKzRrzO0DtZgeLnExOFhJmF6Bnh7s,2295
|
|
8
|
+
vpf/words.py,sha256=ikw4HBvrjJs7tz9VANAlqHEL0oUMdINKmQPlTw2No7E,3224
|
|
9
|
+
verifiableplanformat-0.1.0.dist-info/METADATA,sha256=Lz2Au64KBlv_crKJWEGHZQB4jbjgFU1UmwlN4iODh_A,2556
|
|
10
|
+
verifiableplanformat-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
11
|
+
verifiableplanformat-0.1.0.dist-info/entry_points.txt,sha256=cSDaJ9cW7G29wwd8-2dHfC1_L6V_tXIGICpfxxCDjxM,37
|
|
12
|
+
verifiableplanformat-0.1.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
13
|
+
verifiableplanformat-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
vpf/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Read, check and show plans in the Verifiable Plan Format (VPF)."""
|
|
2
|
+
|
|
3
|
+
from .check import Problem, Report, check
|
|
4
|
+
from .reasons import REASONS
|
|
5
|
+
from .types import SCHEMA, By, Day, Limits, Need, Plan, Proof, Prover, Start, Step, Tool, proofs_of
|
|
6
|
+
from .words import ProofWords, StepWords, words, words_text
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.0"
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"REASONS",
|
|
12
|
+
"SCHEMA",
|
|
13
|
+
"By",
|
|
14
|
+
"Day",
|
|
15
|
+
"Limits",
|
|
16
|
+
"Need",
|
|
17
|
+
"Plan",
|
|
18
|
+
"Problem",
|
|
19
|
+
"Proof",
|
|
20
|
+
"ProofWords",
|
|
21
|
+
"Prover",
|
|
22
|
+
"Report",
|
|
23
|
+
"Start",
|
|
24
|
+
"Step",
|
|
25
|
+
"StepWords",
|
|
26
|
+
"Tool",
|
|
27
|
+
"__version__",
|
|
28
|
+
"check",
|
|
29
|
+
"proofs_of",
|
|
30
|
+
"words",
|
|
31
|
+
"words_text",
|
|
32
|
+
]
|
vpf/__main__.py
ADDED
vpf/check.py
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"""The checker of VPF 1.
|
|
2
|
+
|
|
3
|
+
It follows the specification: first whether the file is a plan at all, then the problems of the plan, then what its
|
|
4
|
+
texts name. Each problem has a reason that does not change, the step it is about (None for the whole plan) and, at
|
|
5
|
+
times, a detail.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import math
|
|
12
|
+
import re
|
|
13
|
+
from collections.abc import Callable
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Any, cast
|
|
16
|
+
|
|
17
|
+
from .reasons import REASONS
|
|
18
|
+
from .types import Plan, Proof, Step, proofs_of
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class Problem:
|
|
23
|
+
"""One reason a plan may not run."""
|
|
24
|
+
|
|
25
|
+
#: The reason, as the specification names it, such as `step.id`.
|
|
26
|
+
reason: str
|
|
27
|
+
#: The id of the step it is about, or None for the whole plan.
|
|
28
|
+
step: str | None
|
|
29
|
+
#: More about this case, such as the reference that names nothing.
|
|
30
|
+
detail: str | None = None
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def message(self) -> str:
|
|
34
|
+
"""The reason in words."""
|
|
35
|
+
return REASONS[self.reason]
|
|
36
|
+
|
|
37
|
+
def __str__(self) -> str:
|
|
38
|
+
"""The problem on one line: `reason: step: sentence (detail)`."""
|
|
39
|
+
detail = "" if self.detail is None else f" ({self.detail})"
|
|
40
|
+
return f"{self.reason}: {self.step if self.step is not None else 'the whole plan'}: {self.message}{detail}"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass
|
|
44
|
+
class Report:
|
|
45
|
+
"""What the checker found."""
|
|
46
|
+
|
|
47
|
+
#: Every problem.
|
|
48
|
+
problems: list[Problem] = field(default_factory=list)
|
|
49
|
+
#: The plan, when the file has the shape of one, even with problems.
|
|
50
|
+
plan: Plan | None = None
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def may_run(self) -> bool:
|
|
54
|
+
"""Whether the plan may run: it was read and has no problem."""
|
|
55
|
+
return self.plan is not None and not self.problems
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Test = Callable[[Any], bool]
|
|
59
|
+
|
|
60
|
+
BY = ("agent", "algorithm", "person", "plan")
|
|
61
|
+
PROVER = ("algorithm", "agent", "person")
|
|
62
|
+
TOOLS = ("read", "write", "commands", "internet", "screen")
|
|
63
|
+
DAYS = ("mon", "tue", "wed", "thu", "fri", "sat", "sun")
|
|
64
|
+
START: dict[str, tuple[str, ...]] = {
|
|
65
|
+
"asked": (),
|
|
66
|
+
"schedule": ("days", "time"),
|
|
67
|
+
"every": ("minutes",),
|
|
68
|
+
"files": ("paths",),
|
|
69
|
+
"request": (),
|
|
70
|
+
}
|
|
71
|
+
NEED: dict[str, tuple[tuple[str, ...], tuple[str, ...]]] = {
|
|
72
|
+
"files": (("path",), ("at_least",)),
|
|
73
|
+
"folder": (("path",), ()),
|
|
74
|
+
"names": (("path", "files"), ()),
|
|
75
|
+
"headings": (("path", "headings"), ()),
|
|
76
|
+
"repository": (("path",), ("branch",)),
|
|
77
|
+
}
|
|
78
|
+
STEP_FIELDS = ("id", "do", "by", "proof", "run", "on_fail", "agent", "model", "tools", "plan")
|
|
79
|
+
PROOF_FIELDS = ("by", "run", "needs", "check", "agent", "model", "ask")
|
|
80
|
+
PROOF_KIND_FIELDS = {"algorithm": ("run", "needs"), "agent": ("check", "agent", "model"), "person": ("ask",)}
|
|
81
|
+
REFERENCE = re.compile(r"\{\{\s*([^{}\s]+)\s*\}\}")
|
|
82
|
+
TIME = re.compile(r"([01][0-9]|2[0-3]):[0-5][0-9]")
|
|
83
|
+
ID = re.compile(r"[a-z0-9-]{1,24}")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _is_text(value: Any) -> bool:
|
|
87
|
+
return isinstance(value, str)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _is_count(value: Any) -> bool:
|
|
91
|
+
return isinstance(value, int) and not isinstance(value, bool) and value >= 0
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _is_number(value: Any) -> bool:
|
|
95
|
+
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _is_texts(value: Any) -> bool:
|
|
99
|
+
return isinstance(value, list) and all(isinstance(item, str) for item in value)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _is_list(value: Any) -> bool:
|
|
103
|
+
return isinstance(value, list)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _one_of(allowed: tuple[str, ...]) -> Test:
|
|
107
|
+
return lambda value: isinstance(value, str) and value in allowed
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _blank(value: Any) -> bool:
|
|
111
|
+
return not isinstance(value, str) or not value.strip()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class _Shape:
|
|
115
|
+
"""Gathers what makes a parsed file not a plan."""
|
|
116
|
+
|
|
117
|
+
def __init__(self) -> None:
|
|
118
|
+
self.wrong: list[str] = []
|
|
119
|
+
|
|
120
|
+
def only(self, value: dict[str, Any], allowed: tuple[str, ...], where: str) -> None:
|
|
121
|
+
self.wrong.extend(f"{where}{key} is not a field of VPF 1" for key in value if key not in allowed)
|
|
122
|
+
|
|
123
|
+
def need(self, value: dict[str, Any], key: str, test: Test, where: str) -> None:
|
|
124
|
+
if key not in value:
|
|
125
|
+
self.wrong.append(f"{where}{key} is missing")
|
|
126
|
+
elif not test(value[key]):
|
|
127
|
+
self.wrong.append(f"{where}{key} has the wrong type")
|
|
128
|
+
|
|
129
|
+
def may(self, value: dict[str, Any], key: str, test: Test, where: str) -> None:
|
|
130
|
+
if key in value and not test(value[key]):
|
|
131
|
+
self.wrong.append(f"{where}{key} has the wrong type")
|
|
132
|
+
|
|
133
|
+
def start(self, start: Any) -> None:
|
|
134
|
+
if not isinstance(start, dict) or start.get("on") not in START:
|
|
135
|
+
self.wrong.append("start.on is not a kind of start")
|
|
136
|
+
return
|
|
137
|
+
fields = START[start["on"]]
|
|
138
|
+
self.only(start, ("on", *fields), "start.")
|
|
139
|
+
tests: dict[str, Test] = {
|
|
140
|
+
"days": lambda v: isinstance(v, list) and all(_one_of(DAYS)(d) for d in v),
|
|
141
|
+
"time": _is_text,
|
|
142
|
+
"minutes": _is_count,
|
|
143
|
+
"paths": _is_texts,
|
|
144
|
+
}
|
|
145
|
+
for key in fields:
|
|
146
|
+
self.need(start, key, tests[key], "start.")
|
|
147
|
+
|
|
148
|
+
def limits(self, limits: Any) -> None:
|
|
149
|
+
if not isinstance(limits, dict):
|
|
150
|
+
self.wrong.append("limits has the wrong type")
|
|
151
|
+
return
|
|
152
|
+
self.only(limits, ("repeats", "minutes", "cost", "month"), "limits.")
|
|
153
|
+
self.may(limits, "repeats", _is_count, "limits.")
|
|
154
|
+
self.may(limits, "minutes", _is_count, "limits.")
|
|
155
|
+
self.may(limits, "cost", _is_number, "limits.")
|
|
156
|
+
self.may(limits, "month", _is_number, "limits.")
|
|
157
|
+
|
|
158
|
+
def step(self, step: Any, where: str) -> None:
|
|
159
|
+
if not isinstance(step, dict):
|
|
160
|
+
self.wrong.append(f"{where}is not an object")
|
|
161
|
+
return
|
|
162
|
+
self.only(step, STEP_FIELDS, where)
|
|
163
|
+
self.need(step, "id", _is_text, where)
|
|
164
|
+
self.need(step, "do", _is_text, where)
|
|
165
|
+
self.need(step, "by", _one_of(BY), where)
|
|
166
|
+
for key in ("run", "agent", "model", "plan"):
|
|
167
|
+
self.may(step, key, _is_text, where)
|
|
168
|
+
self.may(step, "on_fail", lambda v: v == "agent", where)
|
|
169
|
+
self.may(step, "tools", lambda v: isinstance(v, list) and all(_one_of(TOOLS)(t) for t in v), where)
|
|
170
|
+
proof = step.get("proof")
|
|
171
|
+
self.may(
|
|
172
|
+
step,
|
|
173
|
+
"proof",
|
|
174
|
+
lambda v: isinstance(v, dict) or (isinstance(v, list) and all(isinstance(p, dict) for p in v)),
|
|
175
|
+
where,
|
|
176
|
+
)
|
|
177
|
+
if "proof" in step and not isinstance(proof, (dict, list)):
|
|
178
|
+
return
|
|
179
|
+
proofs = [] if proof is None else proof if isinstance(proof, list) else [proof]
|
|
180
|
+
for item in proofs:
|
|
181
|
+
if isinstance(item, dict):
|
|
182
|
+
self.proof(item, where)
|
|
183
|
+
|
|
184
|
+
def proof(self, proof: dict[str, Any], where: str) -> None:
|
|
185
|
+
at = f"{where}proof."
|
|
186
|
+
self.only(proof, PROOF_FIELDS, at)
|
|
187
|
+
self.need(proof, "by", _one_of(PROVER), at)
|
|
188
|
+
for key in ("run", "check", "agent", "model", "ask"):
|
|
189
|
+
self.may(proof, key, _is_text, at)
|
|
190
|
+
if "needs" not in proof:
|
|
191
|
+
return
|
|
192
|
+
needs = proof["needs"]
|
|
193
|
+
if not isinstance(needs, list):
|
|
194
|
+
self.wrong.append(f"{at}needs has the wrong type")
|
|
195
|
+
return
|
|
196
|
+
tests: dict[str, Test] = {
|
|
197
|
+
"path": _is_text,
|
|
198
|
+
"files": _is_text,
|
|
199
|
+
"headings": _is_texts,
|
|
200
|
+
"at_least": _is_count,
|
|
201
|
+
"branch": _is_text,
|
|
202
|
+
}
|
|
203
|
+
for entry in needs:
|
|
204
|
+
if not isinstance(entry, dict) or entry.get("need") not in NEED:
|
|
205
|
+
self.wrong.append(f"{where}a need is not a kind of need")
|
|
206
|
+
continue
|
|
207
|
+
required, optional = NEED[entry["need"]]
|
|
208
|
+
self.only(entry, ("need", *required, *optional), f"{where}need.")
|
|
209
|
+
for key in required:
|
|
210
|
+
self.need(entry, key, tests[key], f"{where}need.")
|
|
211
|
+
for key in optional:
|
|
212
|
+
self.may(entry, key, tests[key], f"{where}need.")
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _shape(plan: Any) -> list[str]:
|
|
216
|
+
"""What makes a parsed file not a plan, as short details; empty when it is one."""
|
|
217
|
+
if not isinstance(plan, dict):
|
|
218
|
+
return ["the file is not one object"]
|
|
219
|
+
if plan.get("$schema") != "vpf/1":
|
|
220
|
+
if "$schema" in plan:
|
|
221
|
+
return [f'$schema is {json.dumps(plan["$schema"], ensure_ascii=False)}, not "vpf/1"']
|
|
222
|
+
return ["$schema is missing"]
|
|
223
|
+
shape = _Shape()
|
|
224
|
+
shape.only(plan, ("$schema", "wish", "start", "limits", "steps"), "")
|
|
225
|
+
shape.need(plan, "wish", _is_text, "")
|
|
226
|
+
shape.need(plan, "steps", _is_list, "")
|
|
227
|
+
if "start" in plan:
|
|
228
|
+
shape.start(plan["start"])
|
|
229
|
+
if "limits" in plan:
|
|
230
|
+
shape.limits(plan["limits"])
|
|
231
|
+
steps = plan.get("steps")
|
|
232
|
+
for n, step in enumerate(steps if isinstance(steps, list) else []):
|
|
233
|
+
shape.step(step, f"step {n + 1}: ")
|
|
234
|
+
return shape.wrong
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _inside(path: str) -> bool:
|
|
238
|
+
"""Whether a path stays inside the folder."""
|
|
239
|
+
if _blank(path) or path.startswith(("/", "\\")) or re.match(r"[A-Za-z]:", path):
|
|
240
|
+
return False
|
|
241
|
+
return ".." not in re.split(r"[\\/]", path)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _texts(step: Step) -> tuple[list[str], list[str]]:
|
|
245
|
+
"""The texts of a step that may hold references: its work's, then its proofs'."""
|
|
246
|
+
proofs: list[str | None] = []
|
|
247
|
+
for proof in proofs_of(step):
|
|
248
|
+
proofs.extend([proof.get("run"), proof.get("check"), proof.get("ask")])
|
|
249
|
+
proofs.extend(need["path"] for need in proof.get("needs", []))
|
|
250
|
+
work: list[str | None] = [step["do"], step.get("run")]
|
|
251
|
+
return [text for text in work if isinstance(text, str)], [text for text in proofs if isinstance(text, str)]
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _proof_problems(step: Step, proof: Proof, add: Callable[[str, str | None], None]) -> None:
|
|
255
|
+
fields = PROOF_KIND_FIELDS[proof["by"]]
|
|
256
|
+
if any(key != "by" and key in proof and key not in fields for key in PROOF_FIELDS):
|
|
257
|
+
add("proof.fields", None)
|
|
258
|
+
if proof["by"] == "algorithm":
|
|
259
|
+
by_command = "run" in proof
|
|
260
|
+
by_needs = len(proof.get("needs", [])) > 0
|
|
261
|
+
if by_command == by_needs or (by_command and _blank(proof.get("run"))):
|
|
262
|
+
add("proof.algorithm", None)
|
|
263
|
+
for need in proof.get("needs", []):
|
|
264
|
+
if not _inside(need["path"]):
|
|
265
|
+
add("need.path", need["path"])
|
|
266
|
+
if need["need"] == "files" and need.get("at_least") == 0:
|
|
267
|
+
add("need.at_least", None)
|
|
268
|
+
if need["need"] == "names" and _blank(need.get("files")):
|
|
269
|
+
add("need.files", None)
|
|
270
|
+
headings = need.get("headings", [])
|
|
271
|
+
if need["need"] == "headings" and (not headings or any(_blank(h) for h in headings)):
|
|
272
|
+
add("need.headings", None)
|
|
273
|
+
if proof["by"] == "agent":
|
|
274
|
+
if _blank(proof.get("check")):
|
|
275
|
+
add("proof.agent", None)
|
|
276
|
+
if "agent" in proof and "agent" in step and proof["agent"] == step["agent"]:
|
|
277
|
+
add("proof.same_agent", None)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _step_problems(step: Step, seen: set[str], add: Callable[[str, str | None], None]) -> None:
|
|
281
|
+
if not ID.fullmatch(step["id"]) or step["id"] == "asked":
|
|
282
|
+
add("step.id", None)
|
|
283
|
+
if step["id"] in seen:
|
|
284
|
+
add("step.duplicate", None)
|
|
285
|
+
seen.add(step["id"])
|
|
286
|
+
if _blank(step["do"]):
|
|
287
|
+
add("step.do", None)
|
|
288
|
+
if _blank(step.get("run")) if step["by"] == "algorithm" else "run" in step:
|
|
289
|
+
add("step.run", None)
|
|
290
|
+
if _blank(step.get("plan")) if step["by"] == "plan" else "plan" in step:
|
|
291
|
+
add("step.plan", None)
|
|
292
|
+
if step["by"] != "agent" and any(key in step for key in ("agent", "model", "tools")):
|
|
293
|
+
add("step.agent", None)
|
|
294
|
+
if "on_fail" in step and step["by"] != "algorithm":
|
|
295
|
+
add("step.on_fail", None)
|
|
296
|
+
proofs = proofs_of(step)
|
|
297
|
+
if step["by"] == "agent" and not proofs:
|
|
298
|
+
add("proof.missing", None)
|
|
299
|
+
if step["by"] in ("person", "plan") and proofs:
|
|
300
|
+
add("proof.misplaced", None)
|
|
301
|
+
if len(proofs) > 8:
|
|
302
|
+
add("proof.count", None)
|
|
303
|
+
for proof in proofs:
|
|
304
|
+
_proof_problems(step, proof, add)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _plan_problems(plan: Plan, add: Callable[[str | None, str, str | None], None]) -> None:
|
|
308
|
+
if _blank(plan["wish"]):
|
|
309
|
+
add(None, "plan.wish", None)
|
|
310
|
+
if not 1 <= len(plan["steps"]) <= 200:
|
|
311
|
+
add(None, "plan.steps", None)
|
|
312
|
+
start = plan.get("start", {"on": "asked"})
|
|
313
|
+
if start["on"] == "schedule":
|
|
314
|
+
if not start.get("days"):
|
|
315
|
+
add(None, "start.days", None)
|
|
316
|
+
if not TIME.fullmatch(start.get("time", "")):
|
|
317
|
+
add(None, "start.time", None)
|
|
318
|
+
if start["on"] == "every" and start.get("minutes") == 0:
|
|
319
|
+
add(None, "start.minutes", None)
|
|
320
|
+
if start["on"] == "files" and all(_blank(path) for path in start.get("paths", [])):
|
|
321
|
+
add(None, "start.paths", None)
|
|
322
|
+
limits = plan.get("limits", {})
|
|
323
|
+
if "repeats" in limits and not 1 <= limits["repeats"] <= 100:
|
|
324
|
+
add(None, "limits.repeats", None)
|
|
325
|
+
if "minutes" in limits and not 1 <= limits["minutes"] <= 10080:
|
|
326
|
+
add(None, "limits.minutes", None)
|
|
327
|
+
if "cost" in limits and not limits["cost"] > 0:
|
|
328
|
+
add(None, "limits.cost", None)
|
|
329
|
+
if "month" in limits and not limits["month"] > 0:
|
|
330
|
+
add(None, "limits.month", None)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def _for_step(add: Callable[[str | None, str, str | None], None], step: str) -> Callable[[str, str | None], None]:
|
|
334
|
+
return lambda reason, detail: add(step, reason, detail)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def check(text: str) -> Report:
|
|
338
|
+
"""Check a plan given as text, and tell every problem."""
|
|
339
|
+
report = Report()
|
|
340
|
+
|
|
341
|
+
def add(step: str | None, reason: str, detail: str | None) -> None:
|
|
342
|
+
if not any(p.step == step and p.reason == reason for p in report.problems):
|
|
343
|
+
report.problems.append(Problem(reason=reason, step=step, detail=detail))
|
|
344
|
+
|
|
345
|
+
try:
|
|
346
|
+
parsed = json.loads(text)
|
|
347
|
+
except ValueError as error:
|
|
348
|
+
add(None, "file.invalid", f"not JSON: {error}")
|
|
349
|
+
return report
|
|
350
|
+
wrong = _shape(parsed)
|
|
351
|
+
if wrong:
|
|
352
|
+
add(None, "file.invalid", "; ".join(wrong))
|
|
353
|
+
return report
|
|
354
|
+
plan = cast(Plan, parsed)
|
|
355
|
+
report.plan = plan
|
|
356
|
+
|
|
357
|
+
_plan_problems(plan, add)
|
|
358
|
+
seen: set[str] = set()
|
|
359
|
+
for n, step in enumerate(plan["steps"]):
|
|
360
|
+
_step_problems(step, seen, _for_step(add, step["id"] or f"#{n + 1}"))
|
|
361
|
+
if report.problems:
|
|
362
|
+
return report
|
|
363
|
+
|
|
364
|
+
ids = [step["id"] for step in plan["steps"]]
|
|
365
|
+
for n, step in enumerate(plan["steps"]):
|
|
366
|
+
work, proofs = _texts(step)
|
|
367
|
+
# A proof comes after the work it proves, so it may also name its own step's answer.
|
|
368
|
+
look_at = [(text, n) for text in work] + [(text, n + 1) for text in proofs]
|
|
369
|
+
for text_, before in look_at:
|
|
370
|
+
for match in REFERENCE.finditer(text_):
|
|
371
|
+
name = match.group(1)
|
|
372
|
+
if name == "asked":
|
|
373
|
+
continue
|
|
374
|
+
if name not in ids:
|
|
375
|
+
add(step["id"], "reference.unknown", f"{{{{{name}}}}}")
|
|
376
|
+
elif ids.index(name) >= before:
|
|
377
|
+
add(step["id"], "reference.not_before", f"{{{{{name}}}}}")
|
|
378
|
+
return report
|
vpf/cli.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""`vpf`: check plans in the Verifiable Plan Format and show them in words."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .check import Report, check
|
|
12
|
+
from .words import words_text
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _read(file: str) -> str | None:
|
|
16
|
+
try:
|
|
17
|
+
return Path(file).read_text(encoding="utf-8")
|
|
18
|
+
except OSError as error:
|
|
19
|
+
print(f"vpf: cannot read {file}: {error.strerror or error}", file=sys.stderr)
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _print_problems(file: str, report: Report) -> None:
|
|
24
|
+
count = len(report.problems)
|
|
25
|
+
print(f"{file}: {count} {'problem' if count == 1 else 'problems'}")
|
|
26
|
+
for problem in report.problems:
|
|
27
|
+
print(f" {problem}")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _check(files: list[str], as_json: bool) -> int:
|
|
31
|
+
answers = []
|
|
32
|
+
all_run = True
|
|
33
|
+
for file in files:
|
|
34
|
+
text = _read(file)
|
|
35
|
+
if text is None:
|
|
36
|
+
return 2
|
|
37
|
+
report = check(text)
|
|
38
|
+
all_run = all_run and report.may_run
|
|
39
|
+
if as_json:
|
|
40
|
+
problems = [
|
|
41
|
+
{"reason": p.reason, "step": p.step, "message": p.message, "detail": p.detail} for p in report.problems
|
|
42
|
+
]
|
|
43
|
+
answers.append({"file": file, "may_run": report.may_run, "problems": problems})
|
|
44
|
+
elif report.may_run:
|
|
45
|
+
print(f"{file}: no problems, it may run")
|
|
46
|
+
else:
|
|
47
|
+
_print_problems(file, report)
|
|
48
|
+
if as_json:
|
|
49
|
+
print(json.dumps(answers, indent=2, ensure_ascii=False))
|
|
50
|
+
return 0 if all_run else 1
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _show(file: str) -> int:
|
|
54
|
+
text = _read(file)
|
|
55
|
+
if text is None:
|
|
56
|
+
return 2
|
|
57
|
+
report = check(text)
|
|
58
|
+
if report.plan is not None and report.may_run:
|
|
59
|
+
sys.stdout.write(words_text(report.plan))
|
|
60
|
+
return 0
|
|
61
|
+
_print_problems(file, report)
|
|
62
|
+
return 1
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def main(argv: list[str] | None = None) -> int:
|
|
66
|
+
"""Run the command, and give the code it ends with."""
|
|
67
|
+
parser = argparse.ArgumentParser(
|
|
68
|
+
prog="vpf", description="Check plans in the Verifiable Plan Format (VPF) and show them in words."
|
|
69
|
+
)
|
|
70
|
+
parser.add_argument("--version", action="version", version=f"vpf {__version__}")
|
|
71
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
72
|
+
checking = commands.add_parser(
|
|
73
|
+
"check", help="Check plans and tell every problem. Ends with 1 when one may not run."
|
|
74
|
+
)
|
|
75
|
+
checking.add_argument("files", nargs="+", help="The plan files.")
|
|
76
|
+
checking.add_argument("--json", action="store_true", help="Answer in JSON.")
|
|
77
|
+
showing = commands.add_parser("show", help="Say a plan in words: what to do, who does it and what proves it.")
|
|
78
|
+
showing.add_argument("file", help="The plan file.")
|
|
79
|
+
arguments = parser.parse_args(argv)
|
|
80
|
+
if arguments.command == "check":
|
|
81
|
+
return _check(arguments.files, arguments.json)
|
|
82
|
+
return _show(arguments.file)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
sys.exit(main())
|
vpf/py.typed
ADDED
|
File without changes
|
vpf/reasons.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Every reason a checker gives, as the specification names them, with the sentence that tells it."""
|
|
2
|
+
|
|
3
|
+
from typing import Final
|
|
4
|
+
|
|
5
|
+
#: Every reason, with its sentence.
|
|
6
|
+
REASONS: Final[dict[str, str]] = {
|
|
7
|
+
"file.invalid": "The file is not a plan in VPF 1.",
|
|
8
|
+
"plan.wish": "The plan does not say what the person wishes.",
|
|
9
|
+
"plan.steps": "The plan has no step, or more than 200.",
|
|
10
|
+
"start.days": "A schedule has no day.",
|
|
11
|
+
"start.time": "A schedule's time is not HH:MM from 00:00 to 23:59.",
|
|
12
|
+
"start.minutes": "A run every so many minutes is set to 0.",
|
|
13
|
+
"start.paths": "A start on files names no path.",
|
|
14
|
+
"limits.repeats": "Repeats are not 1 to 100.",
|
|
15
|
+
"limits.minutes": "The minutes of a run are not 1 to 10080.",
|
|
16
|
+
"limits.cost": "The cost of a run is not more than 0.",
|
|
17
|
+
"limits.month": "The cost of a month is not more than 0.",
|
|
18
|
+
"step.id": "An id is not 1 to 24 small letters, digits or -, or is asked.",
|
|
19
|
+
"step.duplicate": "Two steps have the same id.",
|
|
20
|
+
"step.do": "A step does not say what to do.",
|
|
21
|
+
"step.run": "An algorithm has no command, or a step done by someone else has one.",
|
|
22
|
+
"step.plan": "A step done by a plan names none, or a step done by someone else names one.",
|
|
23
|
+
"step.agent": "A step no agent does chooses an agent, a model or tools.",
|
|
24
|
+
"step.on_fail": "A step that is not an algorithm has someone behind it.",
|
|
25
|
+
"proof.missing": "A step an agent does has no proof.",
|
|
26
|
+
"proof.misplaced": "A step a person or another plan does carries a proof.",
|
|
27
|
+
"proof.count": "A step has more than 8 proofs.",
|
|
28
|
+
"proof.algorithm": "A proof by an algorithm has neither a command nor needs, or has both.",
|
|
29
|
+
"proof.agent": "A proof by an agent does not say what to look for.",
|
|
30
|
+
"proof.same_agent": "The agent that does a step is the one that judges it.",
|
|
31
|
+
"proof.fields": "A proof has a field of another kind of proof.",
|
|
32
|
+
"need.path": "A need has no path, or its path is outside the folder.",
|
|
33
|
+
"need.at_least": "A need of files asks for at least 0.",
|
|
34
|
+
"need.files": "A need of names does not say which files.",
|
|
35
|
+
"need.headings": "A need of headings names no heading.",
|
|
36
|
+
"reference.unknown": "A text names something that is neither asked nor a step.",
|
|
37
|
+
"reference.not_before": "A text names its own step or a later one where it may not.",
|
|
38
|
+
}
|
vpf/types.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""A plan in VPF 1, as its JSON holds it."""
|
|
2
|
+
|
|
3
|
+
from typing import Literal, NotRequired, TypedDict
|
|
4
|
+
|
|
5
|
+
SCHEMA: Literal["vpf/1"] = "vpf/1"
|
|
6
|
+
|
|
7
|
+
By = Literal["agent", "algorithm", "person", "plan"]
|
|
8
|
+
Prover = Literal["algorithm", "agent", "person"]
|
|
9
|
+
Tool = Literal["read", "write", "commands", "internet", "screen"]
|
|
10
|
+
Day = Literal["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Start(TypedDict):
|
|
14
|
+
"""When a run starts: `asked`, `schedule` (days, time), `every` (minutes), `files` (paths) or `request`."""
|
|
15
|
+
|
|
16
|
+
on: Literal["asked", "schedule", "every", "files", "request"]
|
|
17
|
+
days: NotRequired[list[Day]]
|
|
18
|
+
time: NotRequired[str]
|
|
19
|
+
minutes: NotRequired[int]
|
|
20
|
+
paths: NotRequired[list[str]]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Limits(TypedDict, total=False):
|
|
24
|
+
"""How far one run may go."""
|
|
25
|
+
|
|
26
|
+
repeats: int
|
|
27
|
+
minutes: int
|
|
28
|
+
cost: float
|
|
29
|
+
month: float
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Need(TypedDict):
|
|
33
|
+
"""One thing the work must leave in the folder: `files`, `folder`, `names`, `headings` or `repository`."""
|
|
34
|
+
|
|
35
|
+
need: Literal["files", "folder", "names", "headings", "repository"]
|
|
36
|
+
path: str
|
|
37
|
+
at_least: NotRequired[int]
|
|
38
|
+
files: NotRequired[str]
|
|
39
|
+
headings: NotRequired[list[str]]
|
|
40
|
+
branch: NotRequired[str]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Proof(TypedDict):
|
|
44
|
+
"""What proves a step's work."""
|
|
45
|
+
|
|
46
|
+
by: Prover
|
|
47
|
+
run: NotRequired[str]
|
|
48
|
+
needs: NotRequired[list[Need]]
|
|
49
|
+
check: NotRequired[str]
|
|
50
|
+
agent: NotRequired[str]
|
|
51
|
+
model: NotRequired[str]
|
|
52
|
+
ask: NotRequired[str]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Step(TypedDict):
|
|
56
|
+
"""One step of a plan."""
|
|
57
|
+
|
|
58
|
+
id: str
|
|
59
|
+
do: str
|
|
60
|
+
by: By
|
|
61
|
+
proof: NotRequired[Proof | list[Proof]]
|
|
62
|
+
run: NotRequired[str]
|
|
63
|
+
on_fail: NotRequired[Literal["agent"]]
|
|
64
|
+
agent: NotRequired[str]
|
|
65
|
+
model: NotRequired[str]
|
|
66
|
+
tools: NotRequired[list[Tool]]
|
|
67
|
+
plan: NotRequired[str]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# Built by call, since `$schema` is no Python name.
|
|
71
|
+
Plan = TypedDict(
|
|
72
|
+
"Plan",
|
|
73
|
+
{
|
|
74
|
+
"$schema": Literal["vpf/1"],
|
|
75
|
+
"wish": str,
|
|
76
|
+
"start": NotRequired[Start],
|
|
77
|
+
"limits": NotRequired[Limits],
|
|
78
|
+
"steps": list[Step],
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
Plan.__doc__ = "A plan: a wish, and the steps that get there."
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def proofs_of(step: Step) -> list[Proof]:
|
|
85
|
+
"""The proofs of a step, as a list."""
|
|
86
|
+
proof = step.get("proof")
|
|
87
|
+
if proof is None:
|
|
88
|
+
return []
|
|
89
|
+
return proof if isinstance(proof, list) else [proof]
|
vpf/words.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""A plan as a person reads it: for each step, what to do, who does it and what proves it."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from .types import Plan, Proof, proofs_of
|
|
8
|
+
|
|
9
|
+
NONE = {
|
|
10
|
+
"person": "Their word stands.",
|
|
11
|
+
"algorithm": "The command ends without an error.",
|
|
12
|
+
"plan": "The other plan's own proofs.",
|
|
13
|
+
"agent": "Nothing yet.",
|
|
14
|
+
}
|
|
15
|
+
BEHIND = "If the command fails, an agent fixes what stands in the way, and the command runs again as the proof."
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class ProofWords:
|
|
20
|
+
"""One proof, in words."""
|
|
21
|
+
|
|
22
|
+
#: What must happen, such as "A command must pass:".
|
|
23
|
+
says: str
|
|
24
|
+
#: The command or the paths, when there are.
|
|
25
|
+
code: str | None = None
|
|
26
|
+
#: What an agent must find true, or what the person is asked.
|
|
27
|
+
quote: str | None = None
|
|
28
|
+
|
|
29
|
+
def __str__(self) -> str:
|
|
30
|
+
"""The proof on one line."""
|
|
31
|
+
line = self.says
|
|
32
|
+
if self.code is not None:
|
|
33
|
+
line += f" `{self.code}`"
|
|
34
|
+
if self.quote is not None:
|
|
35
|
+
line += f" “{self.quote}”"
|
|
36
|
+
return line
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class StepWords:
|
|
41
|
+
"""One step, in words."""
|
|
42
|
+
|
|
43
|
+
#: The step's place, from 1.
|
|
44
|
+
number: int
|
|
45
|
+
#: The step's id.
|
|
46
|
+
id: str
|
|
47
|
+
#: What to do.
|
|
48
|
+
what: str
|
|
49
|
+
#: Who does it, such as "An algorithm runs `make test`".
|
|
50
|
+
who: str
|
|
51
|
+
#: What proves it, one line per proof.
|
|
52
|
+
proofs: list[ProofWords] = field(default_factory=list)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _proof_words(proof: Proof) -> ProofWords:
|
|
56
|
+
if proof["by"] == "algorithm":
|
|
57
|
+
if "run" in proof:
|
|
58
|
+
return ProofWords("A command must pass:", code=proof["run"])
|
|
59
|
+
return ProofWords("These must be there:", code=", ".join(need["path"] for need in proof.get("needs", [])))
|
|
60
|
+
if proof["by"] == "agent":
|
|
61
|
+
return ProofWords("Another agent must find this true:", quote=proof.get("check"))
|
|
62
|
+
ask = proof.get("ask")
|
|
63
|
+
if ask is not None and ask.strip():
|
|
64
|
+
return ProofWords("The person answers yes to:", quote=ask)
|
|
65
|
+
return ProofWords("The person approves.")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def words(plan: Plan) -> list[StepWords]:
|
|
69
|
+
"""The steps of a plan, in words."""
|
|
70
|
+
steps = []
|
|
71
|
+
for n, step in enumerate(plan["steps"]):
|
|
72
|
+
who = {
|
|
73
|
+
"agent": "An agent",
|
|
74
|
+
"algorithm": f"An algorithm runs `{step.get('run', '')}`",
|
|
75
|
+
"person": "A person",
|
|
76
|
+
"plan": f"Another plan: `{step.get('plan', '')}`",
|
|
77
|
+
}[step["by"]]
|
|
78
|
+
proofs = [_proof_words(proof) for proof in proofs_of(step)]
|
|
79
|
+
if not proofs:
|
|
80
|
+
proofs.append(ProofWords(NONE[step["by"]]))
|
|
81
|
+
if step.get("on_fail") == "agent":
|
|
82
|
+
proofs.append(ProofWords(BEHIND))
|
|
83
|
+
steps.append(StepWords(number=n + 1, id=step["id"], what=step["do"], who=who, proofs=proofs))
|
|
84
|
+
return steps
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def words_text(plan: Plan) -> str:
|
|
88
|
+
"""The plan in words, as plain text."""
|
|
89
|
+
text = f"The wish: {plan['wish']}\n"
|
|
90
|
+
for step in words(plan):
|
|
91
|
+
text += f"\nStep {step.number} · {step.id}\n What to do: {step.what}\n Who does it: {step.who}\n"
|
|
92
|
+
for i, proof in enumerate(step.proofs):
|
|
93
|
+
label = "What proves it:" if i == 0 else ""
|
|
94
|
+
text += f" {label:<15} {proof}\n"
|
|
95
|
+
return text
|