@giamme/forge 0.14.0 → 0.15.0
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.
- package/CHANGELOG.md +26 -0
- package/SKILL.md +47 -14
- package/VERSION +1 -1
- package/docs/artifacts.md +2 -1
- package/docs/commands.md +301 -7
- package/docs/models.md +53 -0
- package/package.json +1 -1
- package/references/dataset.md +12 -3
- package/references/decompose.md +108 -7
- package/references/harnesses.md +21 -1
- package/references/planning.md +59 -0
- package/references/ui.md +39 -1
- package/scripts/forge-compare.py +6 -3
- package/scripts/forge-config.py +531 -4
- package/scripts/forge-dispatch.sh +132 -22
- package/scripts/forge-lib-plans.sh +19 -2
- package/scripts/forge-lib-review.sh +5 -3
- package/scripts/forge-lib-task.sh +119 -0
- package/scripts/forge-parallel-options.sh +12 -6
- package/scripts/forge-parallel.sh +572 -26
- package/scripts/forge-plan-check.py +591 -8
- package/scripts/forge-planner.py +480 -0
- package/scripts/forge-route.py +72 -23
- package/scripts/forge-runtime.py +157 -1
- package/scripts/forge-shadow.py +51 -7
- package/scripts/forge-solo.sh +81 -7
- package/scripts/forge_dataset/cli.py +10 -0
- package/scripts/forge_dataset/efforts.py +215 -0
- package/scripts/forge_dataset/harvest.py +44 -0
- package/scripts/forge_decide/__init__.py +19 -1
- package/scripts/forge_decide/builtin_profiles.json +25 -0
- package/scripts/forge_decide/cli.py +12 -0
- package/scripts/forge_decide/profiles.py +866 -0
- package/scripts/forge_decide/resolve.py +38 -6
- package/ui/docs/api.md +87 -8
- package/ui/forge_ui/control.py +173 -12
- package/ui/forge_ui/executions.py +28 -8
- package/ui/forge_ui/models.py +60 -1
- package/ui/forge_ui/reader.py +21 -1
- package/ui/forge_ui/static/leaderboard.js +5 -1
- package/ui/forge_ui/static/modelstats.js +25 -1
- package/ui/forge_ui/static/panel.js +19 -2
- package/ui/forge_ui/static/settings.js +788 -25
- package/ui/forge_ui/static/style.css +23 -0
package/scripts/forge-config.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""forge config <command>: one view of every config source, and safe edits for the per-repo ones.
|
|
3
3
|
|
|
4
|
-
show [--repo DIR] [--json] registry, Jev, decide, the repo's .forge files, dataset and FORGE_* env
|
|
4
|
+
show [--repo DIR] [--json] registry, Jev, decide, profiles, the repo's .forge files, dataset and FORGE_* env
|
|
5
5
|
set <key> <value> [--repo DIR] [--json] repo.verify | repo.setup | repo.verify-subset | decide.gate.<gate>[.provider|.model]
|
|
6
6
|
unset <key> [--repo DIR] [--json] repo.verify | repo.setup | repo.verify-subset
|
|
7
|
+
profile list|show|create|edit|set|unset|rm|use [--repo DIR] [--json]
|
|
8
|
+
profiles in decide.json; --repo writes the repo file, otherwise the user file
|
|
7
9
|
|
|
8
10
|
A value of `-` reads stdin for the per-repo files. repo.verify-subset must contain `{files}` (the
|
|
9
11
|
selected test files) or `{changed}` (the changed paths, for a runner with a related-tests mode).
|
|
@@ -22,13 +24,22 @@ A provider or model written to a gate that is `on` and not forced needs a passin
|
|
|
22
24
|
new provider and model, as `forge decide gate G on` does; without one nothing is written and the
|
|
23
25
|
exit code is 3 (with --json, an error object on stdout too).
|
|
24
26
|
|
|
27
|
+
Profiles are the named routing, policy, gates and budget bundles in decide.json. Built-ins
|
|
28
|
+
(balanced, lite) can be listed, shown and copied, and not edited. `profile set` and `unset` take
|
|
29
|
+
one dotted routing.* or policy.* key; a gate inside a profile stays on `forge decide gate
|
|
30
|
+
--profile`. Every profile write, including selecting one with `profile use`, is checked with
|
|
31
|
+
profiles.validate before it is saved, and a failure leaves the file untouched. `--json` on a
|
|
32
|
+
profile write prints {"ok": true, "name", "level", "body"} or {"ok": false, "error"}.
|
|
33
|
+
|
|
25
34
|
Exit codes: 0 ok, 2 usage or invalid input (including an unsafe path), 3 refused by the promotion
|
|
26
35
|
rule; any other code is the decide CLI's own, passed through.
|
|
27
36
|
"""
|
|
28
37
|
from __future__ import annotations
|
|
29
38
|
|
|
30
39
|
import argparse
|
|
40
|
+
import copy
|
|
31
41
|
import json
|
|
42
|
+
import math
|
|
32
43
|
import os
|
|
33
44
|
from pathlib import Path
|
|
34
45
|
import re
|
|
@@ -39,7 +50,8 @@ import sys
|
|
|
39
50
|
SKILL = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
40
51
|
sys.path.insert(0, os.path.join(SKILL, 'scripts'))
|
|
41
52
|
|
|
42
|
-
from forge_decide import GATES, MODES, load_user, save_user, user_path
|
|
53
|
+
from forge_decide import GATES, MODES, load_repo, load_user, save_repo, save_user, user_path
|
|
54
|
+
from forge_decide import profiles
|
|
43
55
|
from forge_decide.migrate import run as migrate_run
|
|
44
56
|
from forge_decide.providers import spec_for_site
|
|
45
57
|
|
|
@@ -63,6 +75,10 @@ class Invalid(Exception):
|
|
|
63
75
|
"""Usage or invalid input, an unsafe path included: exit 2."""
|
|
64
76
|
|
|
65
77
|
|
|
78
|
+
class ReadOnly(Exception):
|
|
79
|
+
"""A write to a built-in profile that has no copy at that level: refused, exit 3."""
|
|
80
|
+
|
|
81
|
+
|
|
66
82
|
def git_top(path):
|
|
67
83
|
try:
|
|
68
84
|
out = subprocess.run(['git', '-C', path, 'rev-parse', '--show-toplevel'],
|
|
@@ -329,7 +345,8 @@ def _print_decide(decide):
|
|
|
329
345
|
|
|
330
346
|
def cmd_show(args):
|
|
331
347
|
root = repo_root(args.repo, required=False)
|
|
332
|
-
data = dict(registry=registry_block(), jev=jev_block(), decide=decide_block(root)
|
|
348
|
+
data = dict(registry=registry_block(), jev=jev_block(), decide=decide_block(root),
|
|
349
|
+
profiles=profiles_payload(show_profiles_repo(root)))
|
|
333
350
|
if root:
|
|
334
351
|
data['repo'] = repo_block(root)
|
|
335
352
|
data['dataset'] = dataset_block()
|
|
@@ -346,6 +363,7 @@ def cmd_show(args):
|
|
|
346
363
|
print('jev: %s (%s, %s)' % ('enabled' if jev['enabled'] else 'disabled', jev['path'],
|
|
347
364
|
'present' if jev['exists'] else 'absent'))
|
|
348
365
|
_print_decide(data['decide'])
|
|
366
|
+
print(profiles_text(data['profiles'], heading=True))
|
|
349
367
|
if root:
|
|
350
368
|
print('repo: %s' % root)
|
|
351
369
|
for name, entry in data['repo']['files'].items():
|
|
@@ -520,10 +538,511 @@ def set_decide(args):
|
|
|
520
538
|
return 0
|
|
521
539
|
|
|
522
540
|
|
|
541
|
+
# ------------------------------------------------------------------------------------ profiles
|
|
542
|
+
|
|
543
|
+
_PROFILE_NAME = re.compile(r'^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$')
|
|
544
|
+
_PROFILE_TEXT_MAX = 1024 * 1024
|
|
545
|
+
_PROFILE_KEYS = (
|
|
546
|
+
'routing.dwarf.<tier>, routing.qa.<tier> (tier is any, low, medium or high), '
|
|
547
|
+
'routing.planner, routing.fractal_planner, routing.final_reviewer, '
|
|
548
|
+
'routing.escalate_after, routing.escalate_effort, policy.qa_threshold, '
|
|
549
|
+
'policy.retry_failed, policy.infra_retries, policy.verify_retries, policy.timeout, '
|
|
550
|
+
'policy.explore, policy.shadow_rate, policy.shadow_qa, policy.fast')
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
def show_profiles_repo(root):
|
|
554
|
+
"""Repo whose profiles `show` lists, or None when it has no repo or .forge is not a real directory.
|
|
555
|
+
|
|
556
|
+
show reports a symlinked .forge without reading it, so the profiles block omits that repo file too.
|
|
557
|
+
"""
|
|
558
|
+
if root is None:
|
|
559
|
+
return None
|
|
560
|
+
try:
|
|
561
|
+
forge_dir(root)
|
|
562
|
+
except Invalid:
|
|
563
|
+
return None
|
|
564
|
+
return root
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def profiles_payload(repo):
|
|
568
|
+
"""The `profile list --json` object: the default in effect, and one row per winning profile."""
|
|
569
|
+
found = profiles.available(repo)
|
|
570
|
+
name, level = profiles.default_name(repo)
|
|
571
|
+
rows = [dict(name=profile_name, level=entry['level'], shadowed=list(entry['shadowed']),
|
|
572
|
+
default=profile_name == name)
|
|
573
|
+
for profile_name, entry in sorted(found.items())]
|
|
574
|
+
return dict(default=dict(name=name, level=level), profiles=rows)
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
def profile_row_text(row):
|
|
578
|
+
text = '%s %s' % (row['name'], row['level'])
|
|
579
|
+
if row['default']:
|
|
580
|
+
text += ' default'
|
|
581
|
+
if row['shadowed']:
|
|
582
|
+
text += ' shadows %s' % ', '.join(row['shadowed'])
|
|
583
|
+
return text
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def profiles_text(payload, heading):
|
|
587
|
+
lines = []
|
|
588
|
+
if heading:
|
|
589
|
+
default = payload['default']
|
|
590
|
+
if default.get('name'):
|
|
591
|
+
lines.append('profiles: default %s (%s)' % (default['name'], default['level']))
|
|
592
|
+
else:
|
|
593
|
+
lines.append('profiles: default none')
|
|
594
|
+
for row in payload['profiles']:
|
|
595
|
+
line = profile_row_text(row)
|
|
596
|
+
lines.append((' ' + line) if heading else line)
|
|
597
|
+
return '\n'.join(lines)
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def profile_repo(args):
|
|
601
|
+
"""Git top for an explicit --repo, else None (the user file).
|
|
602
|
+
|
|
603
|
+
An empty --repo is a bad path, not "no --repo": an empty shell variable must not select the
|
|
604
|
+
user file. A symlinked .forge or a non-regular decide.json is refused here: save_repo would
|
|
605
|
+
otherwise create or write through the link.
|
|
606
|
+
"""
|
|
607
|
+
repo = getattr(args, 'repo', None)
|
|
608
|
+
if repo is None:
|
|
609
|
+
return None
|
|
610
|
+
root = repo_root(repo, required=True)
|
|
611
|
+
safe_target(root, 'decide.json')
|
|
612
|
+
return root
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def profile_level(args):
|
|
616
|
+
repo = profile_repo(args)
|
|
617
|
+
return ('repo', repo) if repo is not None else ('user', None)
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
def load_level(level, repo):
|
|
621
|
+
data, error = load_repo(repo) if level == 'repo' else load_user()
|
|
622
|
+
if error:
|
|
623
|
+
raise Invalid(error + '; fix or remove it before changing settings')
|
|
624
|
+
return data
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def save_level(level, repo, data):
|
|
628
|
+
if level == 'repo':
|
|
629
|
+
safe_target(repo, 'decide.json')
|
|
630
|
+
save_repo(repo, data)
|
|
631
|
+
else:
|
|
632
|
+
save_user(data)
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
def profile_providers(level, data):
|
|
636
|
+
"""Providers a profile's gates are checked against: always the user file's, even for a repo profile."""
|
|
637
|
+
if level != 'user':
|
|
638
|
+
data, error = load_user()
|
|
639
|
+
if error:
|
|
640
|
+
raise Invalid(error + '; fix or remove it before changing settings')
|
|
641
|
+
stored = data.get('providers') if isinstance(data, dict) else None
|
|
642
|
+
return stored if isinstance(stored, dict) else {}
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def require_valid(body, providers):
|
|
646
|
+
reasons = profiles.validate(body, providers)
|
|
647
|
+
if reasons:
|
|
648
|
+
raise Invalid('\n'.join(reasons))
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def has_dict_profile(data, name):
|
|
652
|
+
raw = data.get('profiles') if isinstance(data, dict) else None
|
|
653
|
+
return isinstance(raw, dict) and isinstance(raw.get(name), dict)
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
def builtin_message(name):
|
|
657
|
+
return ("built-in profile %r is read-only; copy it: forge config profile create NAME --from %s"
|
|
658
|
+
% (name, name))
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
def builtin_readonly(name, level, data):
|
|
662
|
+
"""True when `name` is a built-in and neither the user file nor this repo file has its own copy.
|
|
663
|
+
|
|
664
|
+
`data` is the file this command writes. At repo level the user file is checked too, so a user
|
|
665
|
+
copy of a built-in name is not reported as read-only when the repo simply lacks that name.
|
|
666
|
+
"""
|
|
667
|
+
if name not in profiles.builtin_names():
|
|
668
|
+
return False
|
|
669
|
+
if level == 'user':
|
|
670
|
+
user = data
|
|
671
|
+
else:
|
|
672
|
+
user, error = load_user()
|
|
673
|
+
if error:
|
|
674
|
+
raise Invalid(error + '; fix or remove it before changing settings')
|
|
675
|
+
if has_dict_profile(user, name):
|
|
676
|
+
return False
|
|
677
|
+
if level == 'repo' and has_dict_profile(data, name):
|
|
678
|
+
return False
|
|
679
|
+
return True
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
def refuse_missing(name, level, data):
|
|
683
|
+
if builtin_readonly(name, level, data):
|
|
684
|
+
raise ReadOnly(builtin_message(name))
|
|
685
|
+
raise Invalid('no profile %r at the %s level' % (name, level))
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
def open_mutable(args, name):
|
|
689
|
+
"""(level, repo, data, body) of a profile this level already stores. Built-ins with no copy are refused."""
|
|
690
|
+
level, repo = profile_level(args)
|
|
691
|
+
data = load_level(level, repo)
|
|
692
|
+
raw = data.get('profiles')
|
|
693
|
+
if not isinstance(raw, dict):
|
|
694
|
+
if raw is not None:
|
|
695
|
+
raise Invalid('profiles is not an object')
|
|
696
|
+
refuse_missing(name, level, data)
|
|
697
|
+
body = raw.get(name)
|
|
698
|
+
if isinstance(body, dict):
|
|
699
|
+
return level, repo, data, body
|
|
700
|
+
if name in raw:
|
|
701
|
+
raise Invalid('profile %r at the %s level is not an object' % (name, level))
|
|
702
|
+
refuse_missing(name, level, data)
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
def require_known(repo, name):
|
|
706
|
+
found = profiles.available(repo)
|
|
707
|
+
if name not in found:
|
|
708
|
+
known = ', '.join(sorted(found)) or 'none'
|
|
709
|
+
raise Invalid('profile %r not found (known: %s)' % (name, known))
|
|
710
|
+
return found[name]
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
def check_profile_name(name):
|
|
714
|
+
if not _PROFILE_NAME.fullmatch(name or ''):
|
|
715
|
+
raise Invalid("profile name %r must be 1-64 characters of letters, digits, '.', '_' or '-', "
|
|
716
|
+
"starting with a letter or digit" % (name,))
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
def profile_key_kind(key):
|
|
720
|
+
parts = (key or '').split('.')
|
|
721
|
+
if (len(parts) == 3 and parts[0] == 'routing' and parts[1] in profiles.POOLS
|
|
722
|
+
and parts[2] in profiles.TIERS):
|
|
723
|
+
return 'specs'
|
|
724
|
+
if len(parts) == 2 and parts[0] == 'routing' and parts[1] in profiles.ROUTING_SPECS:
|
|
725
|
+
return 'spec'
|
|
726
|
+
if key == 'routing.escalate_after' or (len(parts) == 2 and parts[0] == 'policy'
|
|
727
|
+
and parts[1] in profiles.POLICY_INTS):
|
|
728
|
+
return 'int'
|
|
729
|
+
if key == 'routing.escalate_effort':
|
|
730
|
+
return 'bool'
|
|
731
|
+
if key == 'policy.qa_threshold':
|
|
732
|
+
return 'threshold'
|
|
733
|
+
if key in ('policy.explore', 'policy.shadow_rate'):
|
|
734
|
+
return 'number'
|
|
735
|
+
if key == 'policy.shadow_qa':
|
|
736
|
+
return 'specs'
|
|
737
|
+
if key == 'policy.fast':
|
|
738
|
+
return 'roles'
|
|
739
|
+
return None
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
def unknown_profile_key(key):
|
|
743
|
+
raise Invalid('unknown key %r; valid keys: %s' % (key, _PROFILE_KEYS))
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
def comma_list(value):
|
|
747
|
+
return [part.strip() for part in str(value).split(',') if part.strip()]
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
def coerce_number(text):
|
|
751
|
+
"""A finite int or float, or `text` when it is not one.
|
|
752
|
+
|
|
753
|
+
A digit string stays an int, so JSON keeps `1` rather than `1.0`. Every other form float()
|
|
754
|
+
accepts (`.25`, `1e-1`) becomes a float. Non-finite values stay text and validate() refuses them.
|
|
755
|
+
"""
|
|
756
|
+
if re.fullmatch(r'[0-9]+', text):
|
|
757
|
+
return int(text)
|
|
758
|
+
try:
|
|
759
|
+
number = float(text)
|
|
760
|
+
except (ValueError, OverflowError):
|
|
761
|
+
return text
|
|
762
|
+
if not math.isfinite(number):
|
|
763
|
+
return text
|
|
764
|
+
return number
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
def coerce_profile_value(kind, value):
|
|
768
|
+
text = value.strip() if isinstance(value, str) else str(value)
|
|
769
|
+
if kind in ('specs', 'roles'):
|
|
770
|
+
return comma_list(text)
|
|
771
|
+
if kind in ('spec', 'threshold'):
|
|
772
|
+
return text
|
|
773
|
+
if kind == 'bool':
|
|
774
|
+
if text == 'on':
|
|
775
|
+
return True
|
|
776
|
+
if text == 'off':
|
|
777
|
+
return False
|
|
778
|
+
return text
|
|
779
|
+
if kind == 'int' and re.fullmatch(r'[0-9]+', text):
|
|
780
|
+
return int(text)
|
|
781
|
+
if kind == 'number':
|
|
782
|
+
return coerce_number(text)
|
|
783
|
+
return text
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
def put_profile_key(body, key, value):
|
|
787
|
+
parts = key.split('.')
|
|
788
|
+
node = body
|
|
789
|
+
for part in parts[:-1]:
|
|
790
|
+
child = node.get(part)
|
|
791
|
+
if child is None:
|
|
792
|
+
child = {}
|
|
793
|
+
node[part] = child
|
|
794
|
+
elif not isinstance(child, dict):
|
|
795
|
+
raise Invalid('%s: %s is not an object' % (key, part))
|
|
796
|
+
node = child
|
|
797
|
+
node[parts[-1]] = value
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
def drop_profile_key(body, key):
|
|
801
|
+
parts = key.split('.')
|
|
802
|
+
node = body
|
|
803
|
+
stack = []
|
|
804
|
+
for part in parts[:-1]:
|
|
805
|
+
child = node.get(part) if isinstance(node, dict) else None
|
|
806
|
+
if not isinstance(child, dict):
|
|
807
|
+
raise Invalid('%s is not set' % key)
|
|
808
|
+
stack.append((node, part))
|
|
809
|
+
node = child
|
|
810
|
+
if not isinstance(node, dict) or parts[-1] not in node:
|
|
811
|
+
raise Invalid('%s is not set' % key)
|
|
812
|
+
del node[parts[-1]]
|
|
813
|
+
for parent, part in reversed(stack):
|
|
814
|
+
child = parent.get(part)
|
|
815
|
+
if isinstance(child, dict) and not child:
|
|
816
|
+
del parent[part]
|
|
817
|
+
else:
|
|
818
|
+
break
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
def read_profile_text(path):
|
|
822
|
+
if path == '-':
|
|
823
|
+
try:
|
|
824
|
+
text = sys.stdin.read(_PROFILE_TEXT_MAX + 1)
|
|
825
|
+
except UnicodeDecodeError:
|
|
826
|
+
raise Invalid('stdin: not valid UTF-8')
|
|
827
|
+
else:
|
|
828
|
+
try:
|
|
829
|
+
with open(path, 'r', encoding='utf-8') as handle:
|
|
830
|
+
text = handle.read(_PROFILE_TEXT_MAX + 1)
|
|
831
|
+
except UnicodeDecodeError:
|
|
832
|
+
raise Invalid('%s: not valid UTF-8' % path)
|
|
833
|
+
except OSError as err:
|
|
834
|
+
raise Invalid('%s: %s' % (path, err.strerror))
|
|
835
|
+
if len(text) > _PROFILE_TEXT_MAX:
|
|
836
|
+
raise Invalid('profile body is larger than 1 MiB')
|
|
837
|
+
return text
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
def parse_profile_body(text):
|
|
841
|
+
try:
|
|
842
|
+
body = json.loads(text)
|
|
843
|
+
except (ValueError, RecursionError) as err:
|
|
844
|
+
raise Invalid('not valid JSON (%s)' % err)
|
|
845
|
+
if not isinstance(body, dict):
|
|
846
|
+
raise Invalid('profile: not an object')
|
|
847
|
+
return body
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
def profile_source_body(args, repo):
|
|
851
|
+
if args.from_profile is not None:
|
|
852
|
+
return copy.deepcopy(require_known(repo, args.from_profile)['body'])
|
|
853
|
+
if args.from_run is not None:
|
|
854
|
+
if not os.path.isdir(args.from_run):
|
|
855
|
+
raise Invalid('--from-run %s is not a directory' % args.from_run)
|
|
856
|
+
return profiles.from_plan(args.from_run)
|
|
857
|
+
return parse_profile_body(read_profile_text(args.file))
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
def emit_profile(args, name, level, body, action):
|
|
861
|
+
if getattr(args, 'json', False):
|
|
862
|
+
print(json.dumps(dict(ok=True, name=name, level=level, body=body)))
|
|
863
|
+
else:
|
|
864
|
+
print('profile %s: %s (%s)' % (name if name else 'none', action, level))
|
|
865
|
+
return 0
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
def cmd_profile_list(args):
|
|
869
|
+
payload = profiles_payload(profile_repo(args))
|
|
870
|
+
if args.json:
|
|
871
|
+
print(json.dumps(payload, indent=2))
|
|
872
|
+
else:
|
|
873
|
+
text = profiles_text(payload, heading=False)
|
|
874
|
+
if text:
|
|
875
|
+
print(text)
|
|
876
|
+
return 0
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
def cmd_profile_show(args):
|
|
880
|
+
repo = profile_repo(args)
|
|
881
|
+
entry = require_known(repo, args.name)
|
|
882
|
+
default_name, _level = profiles.default_name(repo)
|
|
883
|
+
if args.json:
|
|
884
|
+
print(json.dumps(dict(name=args.name, level=entry['level'], shadowed=list(entry['shadowed']),
|
|
885
|
+
default=args.name == default_name, body=entry['body']), indent=2))
|
|
886
|
+
return 0
|
|
887
|
+
print('profile: %s' % args.name)
|
|
888
|
+
print('level: %s' % entry['level'])
|
|
889
|
+
print('shadows: %s' % (', '.join(entry['shadowed']) if entry['shadowed'] else 'none'))
|
|
890
|
+
print(json.dumps(entry['body'], indent=2))
|
|
891
|
+
return 0
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
def cmd_profile_create(args):
|
|
895
|
+
check_profile_name(args.name)
|
|
896
|
+
if sum(source is not None for source in (args.from_profile, args.from_run, args.file)) != 1:
|
|
897
|
+
raise Invalid('create needs exactly one of --from, --from-run or --file')
|
|
898
|
+
level, repo = profile_level(args)
|
|
899
|
+
data = load_level(level, repo)
|
|
900
|
+
raw = data.get('profiles')
|
|
901
|
+
if raw is None:
|
|
902
|
+
raw = {}
|
|
903
|
+
elif not isinstance(raw, dict):
|
|
904
|
+
raise Invalid('profiles is not an object')
|
|
905
|
+
if args.name in raw:
|
|
906
|
+
raise Invalid('profile %r already exists at the %s level' % (args.name, level))
|
|
907
|
+
body = profile_source_body(args, repo)
|
|
908
|
+
require_valid(body, profile_providers(level, data))
|
|
909
|
+
raw[args.name] = body
|
|
910
|
+
data['profiles'] = raw
|
|
911
|
+
save_level(level, repo, data)
|
|
912
|
+
return emit_profile(args, args.name, level, body, 'created')
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
def cmd_profile_edit(args):
|
|
916
|
+
level, repo, data, _body = open_mutable(args, args.name)
|
|
917
|
+
body = parse_profile_body(read_profile_text(args.file))
|
|
918
|
+
require_valid(body, profile_providers(level, data))
|
|
919
|
+
data['profiles'][args.name] = body
|
|
920
|
+
save_level(level, repo, data)
|
|
921
|
+
return emit_profile(args, args.name, level, body, 'edited')
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
def cmd_profile_set(args):
|
|
925
|
+
level, repo, data, body = open_mutable(args, args.name)
|
|
926
|
+
kind = profile_key_kind(args.key)
|
|
927
|
+
if kind is None:
|
|
928
|
+
unknown_profile_key(args.key)
|
|
929
|
+
body = copy.deepcopy(body)
|
|
930
|
+
put_profile_key(body, args.key, coerce_profile_value(kind, args.value))
|
|
931
|
+
require_valid(body, profile_providers(level, data))
|
|
932
|
+
data['profiles'][args.name] = body
|
|
933
|
+
save_level(level, repo, data)
|
|
934
|
+
return emit_profile(args, args.name, level, body, 'set %s' % args.key)
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
def cmd_profile_unset(args):
|
|
938
|
+
level, repo, data, body = open_mutable(args, args.name)
|
|
939
|
+
if profile_key_kind(args.key) is None:
|
|
940
|
+
unknown_profile_key(args.key)
|
|
941
|
+
body = copy.deepcopy(body)
|
|
942
|
+
drop_profile_key(body, args.key)
|
|
943
|
+
require_valid(body, profile_providers(level, data))
|
|
944
|
+
data['profiles'][args.name] = body
|
|
945
|
+
save_level(level, repo, data)
|
|
946
|
+
return emit_profile(args, args.name, level, body, 'unset %s' % args.key)
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
def cmd_profile_rm(args):
|
|
950
|
+
level, repo = profile_level(args)
|
|
951
|
+
data = load_level(level, repo)
|
|
952
|
+
raw = data.get('profiles')
|
|
953
|
+
if not isinstance(raw, dict) or args.name not in raw:
|
|
954
|
+
if raw is not None and not isinstance(raw, dict):
|
|
955
|
+
raise Invalid('profiles is not an object')
|
|
956
|
+
refuse_missing(args.name, level, data)
|
|
957
|
+
# A default that names the profile goes with it, unless another copy of that name is left (a
|
|
958
|
+
# lower level's, or a built-in): every later plan and solo run would stop on a missing default.
|
|
959
|
+
entry = profiles.available(repo).get(args.name) or {}
|
|
960
|
+
left = entry.get('level') != level or bool(entry.get('shadowed'))
|
|
961
|
+
del raw[args.name]
|
|
962
|
+
if not raw:
|
|
963
|
+
data.pop('profiles')
|
|
964
|
+
cleared = data.get('default_profile') == args.name and not left
|
|
965
|
+
if cleared:
|
|
966
|
+
data.pop('default_profile')
|
|
967
|
+
save_level(level, repo, data)
|
|
968
|
+
return emit_profile(args, args.name, level, None, 'removed, and the default cleared' if cleared else 'removed')
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
def cmd_profile_use(args):
|
|
972
|
+
if args.none and args.name:
|
|
973
|
+
raise Invalid('use NAME or use --none, not both')
|
|
974
|
+
if not args.none and not args.name:
|
|
975
|
+
raise Invalid('use NAME, or use --none to clear the default')
|
|
976
|
+
level, repo = profile_level(args)
|
|
977
|
+
data = load_level(level, repo)
|
|
978
|
+
if args.none:
|
|
979
|
+
if 'default_profile' in data:
|
|
980
|
+
data.pop('default_profile')
|
|
981
|
+
save_level(level, repo, data)
|
|
982
|
+
return emit_profile(args, None, level, None, 'cleared')
|
|
983
|
+
entry = require_known(repo, args.name)
|
|
984
|
+
# Selecting a profile writes default_profile. The body a run would use has to validate first;
|
|
985
|
+
# a hand-edited invalid body is refused and the file is left byte-identical.
|
|
986
|
+
require_valid(entry['body'], profile_providers(level, data))
|
|
987
|
+
if data.get('default_profile') != args.name:
|
|
988
|
+
data['default_profile'] = args.name
|
|
989
|
+
save_level(level, repo, data)
|
|
990
|
+
return emit_profile(args, args.name, level, entry['body'], 'default')
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
def add_level_flags(sub):
|
|
994
|
+
sub.add_argument('--repo', help="that repo's .forge/decide.json (the git top level); otherwise the user file")
|
|
995
|
+
sub.add_argument('--json', action='store_true')
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
def add_profile_parser(sub):
|
|
999
|
+
p = sub.add_parser('profile', help='list and edit named profiles in decide.json')
|
|
1000
|
+
commands = p.add_subparsers(dest='profile_cmd', required=True)
|
|
1001
|
+
s = commands.add_parser('list', help='name, level, default marker and shadowed levels')
|
|
1002
|
+
add_level_flags(s)
|
|
1003
|
+
s.set_defaults(fn=cmd_profile_list)
|
|
1004
|
+
s = commands.add_parser('show', help='the winning body, its level and what it shadows')
|
|
1005
|
+
s.add_argument('name')
|
|
1006
|
+
add_level_flags(s)
|
|
1007
|
+
s.set_defaults(fn=cmd_profile_show)
|
|
1008
|
+
s = commands.add_parser('create', help='copy a profile, a plan dir, or a JSON file into a new profile')
|
|
1009
|
+
s.add_argument('name')
|
|
1010
|
+
s.add_argument('--from', dest='from_profile', metavar='OTHER')
|
|
1011
|
+
s.add_argument('--from-run', dest='from_run', metavar='PLAN')
|
|
1012
|
+
s.add_argument('--file', metavar='F', help="JSON body, or '-' for stdin")
|
|
1013
|
+
add_level_flags(s)
|
|
1014
|
+
s.set_defaults(fn=cmd_profile_create)
|
|
1015
|
+
s = commands.add_parser('edit', help='replace the body stored at this level')
|
|
1016
|
+
s.add_argument('name')
|
|
1017
|
+
s.add_argument('--file', required=True, metavar='F', help="JSON body, or '-' for stdin")
|
|
1018
|
+
add_level_flags(s)
|
|
1019
|
+
s.set_defaults(fn=cmd_profile_edit)
|
|
1020
|
+
s = commands.add_parser('set', help='set one dotted routing or policy key')
|
|
1021
|
+
s.add_argument('name')
|
|
1022
|
+
s.add_argument('key')
|
|
1023
|
+
s.add_argument('value')
|
|
1024
|
+
add_level_flags(s)
|
|
1025
|
+
s.set_defaults(fn=cmd_profile_set)
|
|
1026
|
+
s = commands.add_parser('unset', help='remove one dotted key; empty sections are dropped')
|
|
1027
|
+
s.add_argument('name')
|
|
1028
|
+
s.add_argument('key')
|
|
1029
|
+
add_level_flags(s)
|
|
1030
|
+
s.set_defaults(fn=cmd_profile_unset)
|
|
1031
|
+
s = commands.add_parser('rm', help='delete the profile stored at this level')
|
|
1032
|
+
s.add_argument('name')
|
|
1033
|
+
add_level_flags(s)
|
|
1034
|
+
s.set_defaults(fn=cmd_profile_rm)
|
|
1035
|
+
s = commands.add_parser('use', help='set or clear default_profile at this level')
|
|
1036
|
+
s.add_argument('name', nargs='?')
|
|
1037
|
+
s.add_argument('--none', action='store_true', help='remove default_profile')
|
|
1038
|
+
add_level_flags(s)
|
|
1039
|
+
s.set_defaults(fn=cmd_profile_use)
|
|
1040
|
+
|
|
1041
|
+
|
|
523
1042
|
def parser():
|
|
524
1043
|
p = argparse.ArgumentParser(prog='forge config', description=__doc__.split('\n\n')[0])
|
|
525
1044
|
sub = p.add_subparsers(dest='cmd', required=True)
|
|
526
|
-
s = sub.add_parser('show', help="every config source: registry, decide, the repo's .forge files, dataset, FORGE_* env")
|
|
1045
|
+
s = sub.add_parser('show', help="every config source: registry, decide, profiles, the repo's .forge files, dataset, FORGE_* env")
|
|
527
1046
|
s.add_argument('--repo')
|
|
528
1047
|
s.add_argument('--json', action='store_true')
|
|
529
1048
|
s.set_defaults(fn=cmd_show)
|
|
@@ -538,6 +1057,7 @@ def parser():
|
|
|
538
1057
|
s.add_argument('--repo')
|
|
539
1058
|
s.add_argument('--json', action='store_true')
|
|
540
1059
|
s.set_defaults(fn=cmd_unset)
|
|
1060
|
+
add_profile_parser(sub)
|
|
541
1061
|
return p
|
|
542
1062
|
|
|
543
1063
|
|
|
@@ -547,7 +1067,14 @@ def main(argv=None):
|
|
|
547
1067
|
return args.fn(args)
|
|
548
1068
|
except Invalid as err:
|
|
549
1069
|
print('forge config: %s' % err, file=sys.stderr)
|
|
1070
|
+
if getattr(args, 'json', False) and getattr(args, 'cmd', None) == 'profile':
|
|
1071
|
+
print(json.dumps(dict(ok=False, error=str(err))))
|
|
550
1072
|
return 2
|
|
1073
|
+
except ReadOnly as err:
|
|
1074
|
+
print('forge config: %s' % err, file=sys.stderr)
|
|
1075
|
+
if getattr(args, 'json', False):
|
|
1076
|
+
print(json.dumps(dict(ok=False, error=str(err))))
|
|
1077
|
+
return 3
|
|
551
1078
|
except Refused as err:
|
|
552
1079
|
print('forge config: %s' % err, file=sys.stderr)
|
|
553
1080
|
if getattr(args, 'json', False):
|