infixspeed-cli 0.0.16__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.
- infix_cli/__init__.py +5 -0
- infix_cli/__main__.py +6 -0
- infix_cli/agent-guide.txt +71 -0
- infix_cli/cli_framework.py +96 -0
- infix_cli/command_contract.py +157 -0
- infix_cli/commands/__init__.py +1 -0
- infix_cli/commands/_artifacts.py +142 -0
- infix_cli/commands/_comparison.py +443 -0
- infix_cli/commands/_confirm.py +26 -0
- infix_cli/commands/_control.py +290 -0
- infix_cli/commands/_create.py +351 -0
- infix_cli/commands/_create_output.py +139 -0
- infix_cli/commands/_delete.py +288 -0
- infix_cli/commands/_download.py +459 -0
- infix_cli/commands/_fusion_common.py +191 -0
- infix_cli/commands/_fusion_config.py +394 -0
- infix_cli/commands/_fusion_feedback.py +319 -0
- infix_cli/commands/_fusion_tasks.py +507 -0
- infix_cli/commands/_fusion_tensorboard.py +132 -0
- infix_cli/commands/_jobs.py +312 -0
- infix_cli/commands/_logs.py +210 -0
- infix_cli/commands/_publish.py +123 -0
- infix_cli/commands/_register.py +600 -0
- infix_cli/commands/_register_output.py +231 -0
- infix_cli/commands/_storage_tree.py +62 -0
- infix_cli/commands/admin.py +668 -0
- infix_cli/commands/asset.py +2012 -0
- infix_cli/commands/auth.py +246 -0
- infix_cli/commands/deploy.py +850 -0
- infix_cli/commands/evaluation.py +360 -0
- infix_cli/commands/examples.py +138 -0
- infix_cli/commands/fusion.py +1842 -0
- infix_cli/commands/merge.py +709 -0
- infix_cli/commands/resource.py +127 -0
- infix_cli/commands/spark.py +347 -0
- infix_cli/commands/storage.py +329 -0
- infix_cli/commands/training.py +748 -0
- infix_cli/config.py +311 -0
- infix_cli/config_input.py +217 -0
- infix_cli/context.py +84 -0
- infix_cli/discovery.py +459 -0
- infix_cli/domain/__init__.py +1 -0
- infix_cli/domain/dto/__init__.py +6 -0
- infix_cli/domain/dto/admin.py +413 -0
- infix_cli/domain/dto/asset.py +390 -0
- infix_cli/domain/dto/common.py +136 -0
- infix_cli/domain/dto/evaluation.py +296 -0
- infix_cli/domain/dto/logs.py +98 -0
- infix_cli/domain/dto/resource.py +108 -0
- infix_cli/domain/dto/serving.py +156 -0
- infix_cli/domain/dto/spark.py +241 -0
- infix_cli/domain/dto/storage.py +75 -0
- infix_cli/domain/dto/training.py +272 -0
- infix_cli/domain/enums.py +359 -0
- infix_cli/domain/hyperparams.py +190 -0
- infix_cli/domain/refs.py +108 -0
- infix_cli/domain/resolve.py +619 -0
- infix_cli/errors.py +158 -0
- infix_cli/example_data/__init__.py +1 -0
- infix_cli/example_data/fusion/README.md +128 -0
- infix_cli/example_data/fusion/fusion.json +149 -0
- infix_cli/example_data/fusion/prompts/code.jsonl +16 -0
- infix_cli/example_data/fusion/prompts/math.jsonl +16 -0
- infix_cli/example_data/fusion.zip +0 -0
- infix_cli/example_data/merge/README.md +89 -0
- infix_cli/example_data/merge/merge.json +26 -0
- infix_cli/example_data/merge.zip +0 -0
- infix_cli/http/__init__.py +1 -0
- infix_cli/http/browser_auth.py +160 -0
- infix_cli/http/client.py +575 -0
- infix_cli/http/pagination.py +84 -0
- infix_cli/http/upload.py +254 -0
- infix_cli/main.py +432 -0
- infix_cli/pipeline_config_schema.py +122 -0
- infix_cli/render/__init__.py +79 -0
- infix_cli/render/json_out.py +123 -0
- infix_cli/render/progress.py +62 -0
- infix_cli/render/text.py +233 -0
- infix_cli/update_check.py +177 -0
- infix_cli/waiter.py +93 -0
- infix_cli/workspace.py +158 -0
- infixspeed_cli-0.0.16.dist-info/METADATA +58 -0
- infixspeed_cli-0.0.16.dist-info/RECORD +85 -0
- infixspeed_cli-0.0.16.dist-info/WHEEL +4 -0
- infixspeed_cli-0.0.16.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""Shared job-command plumbing: status validation and result exit codes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Callable
|
|
6
|
+
|
|
7
|
+
from infix_cli.commands import _create_output
|
|
8
|
+
from infix_cli.domain import enums
|
|
9
|
+
from infix_cli.domain.dto import evaluation as eval_dto
|
|
10
|
+
from infix_cli.domain.dto import spark as spark_dto
|
|
11
|
+
from infix_cli.domain.dto import training as training_dto
|
|
12
|
+
from infix_cli.errors import ExitCode, UsageError
|
|
13
|
+
from infix_cli.render import text
|
|
14
|
+
|
|
15
|
+
# States that only exist while waiting: evaluation_status() below synthesises
|
|
16
|
+
# them, so they are not backend statuses and never reach `get`.
|
|
17
|
+
_WAIT_ONLY_FAILURE = {"evaluation": frozenset({"collection_exhausted"})}
|
|
18
|
+
|
|
19
|
+
_TERMINALS = {
|
|
20
|
+
"training": (training_dto.TERMINAL_SUCCESS, training_dto.TERMINAL_FAILURE),
|
|
21
|
+
"evaluation": (eval_dto.TERMINAL_SUCCESS, eval_dto.TERMINAL_FAILURE),
|
|
22
|
+
"spark": (spark_dto.TERMINAL_SUCCESS, spark_dto.TERMINAL_FAILURE),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def terminal_states(module: str) -> frozenset[str]:
|
|
27
|
+
"""Every state a job of this module does not leave on its own."""
|
|
28
|
+
success, failure = _TERMINALS[module]
|
|
29
|
+
return frozenset(success | failure)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def validate_status(module: str, value: str | None) -> str | None:
|
|
33
|
+
"""Check ``--status`` against the module's own state set and map it back.
|
|
34
|
+
|
|
35
|
+
The sets genuinely differ — ``timeout``/``created`` exist only for
|
|
36
|
+
training, and spark has no ``stopping``/``canceling`` — so one shared enum
|
|
37
|
+
would either reject valid filters or accept impossible ones.
|
|
38
|
+
"""
|
|
39
|
+
if not value:
|
|
40
|
+
return None
|
|
41
|
+
allowed = enums.STATUSES_BY_MODULE[module]
|
|
42
|
+
word = value.strip().lower()
|
|
43
|
+
if word not in allowed:
|
|
44
|
+
raise UsageError(
|
|
45
|
+
f'Invalid --status "{value}" for {module}.',
|
|
46
|
+
f"Valid: {', '.join(allowed)}.",
|
|
47
|
+
)
|
|
48
|
+
return enums.status_to_backend(word)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def validate_visibility(value: str | None) -> str | None:
|
|
52
|
+
"""``--visibility`` -> the backend's capitalised value."""
|
|
53
|
+
if not value:
|
|
54
|
+
return None
|
|
55
|
+
word = value.strip().lower()
|
|
56
|
+
if word not in enums.VISIBILITY.cli_values:
|
|
57
|
+
raise UsageError(
|
|
58
|
+
f'Invalid --visibility "{value}".',
|
|
59
|
+
f"Valid: {', '.join(enums.VISIBILITY.cli_values)}.",
|
|
60
|
+
)
|
|
61
|
+
return enums.VISIBILITY.to_backend(word)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def result_exit_code(module: str, status: str) -> ExitCode:
|
|
65
|
+
"""内部执行状态映射;查询命令不调用此映射。"""
|
|
66
|
+
success, failure = _TERMINALS[module]
|
|
67
|
+
if status in success:
|
|
68
|
+
return ExitCode.OK
|
|
69
|
+
if status in failure:
|
|
70
|
+
return ExitCode.JOB_FAILED
|
|
71
|
+
return ExitCode.JOB_RUNNING
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def finish(run: Any, module: str, status: str) -> None:
|
|
75
|
+
import typer
|
|
76
|
+
|
|
77
|
+
code = run.resolve_exit(result_exit_code(module, status))
|
|
78
|
+
if code:
|
|
79
|
+
raise typer.Exit(code)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def submit_and_report(
|
|
83
|
+
run: Any,
|
|
84
|
+
module: str,
|
|
85
|
+
*,
|
|
86
|
+
path: str,
|
|
87
|
+
payload: dict,
|
|
88
|
+
detail_path: Callable[[str], str],
|
|
89
|
+
build_dto: Callable[[dict], Any],
|
|
90
|
+
wait: bool,
|
|
91
|
+
timeout: str | None,
|
|
92
|
+
poll_status: Callable[[dict], str] | None = None,
|
|
93
|
+
echo: dict[str, str] | None = None,
|
|
94
|
+
dry_run: bool = False,
|
|
95
|
+
) -> None:
|
|
96
|
+
"""POST a job, optionally wait for it, render it, exit with its result.
|
|
97
|
+
|
|
98
|
+
Without --wait the exit code says only "submitted" (0): the job has not run
|
|
99
|
+
yet, so there is no result to report. With --wait the command blocks to a
|
|
100
|
+
terminal state and the exit code is the job's outcome.
|
|
101
|
+
"""
|
|
102
|
+
from infix_cli import render, waiter
|
|
103
|
+
from infix_cli.errors import JobFailed
|
|
104
|
+
|
|
105
|
+
waiter.parse_timeout(timeout)
|
|
106
|
+
if dry_run:
|
|
107
|
+
from infix_cli.config_input import redact
|
|
108
|
+
|
|
109
|
+
checks = {"input": "passed", "references": "resolved", "server_admission": "not_checked"}
|
|
110
|
+
render.render_object(run, {
|
|
111
|
+
"dry_run": True, "module": module, "request": {"method": "POST", "path": path, "body": redact(payload)},
|
|
112
|
+
"checks": checks,
|
|
113
|
+
}, [
|
|
114
|
+
("Dry run", module),
|
|
115
|
+
("Input validation", checks["input"]),
|
|
116
|
+
("Reference resolution", checks["references"]),
|
|
117
|
+
("Server admission", "not checked"),
|
|
118
|
+
], quiet_value=module)
|
|
119
|
+
return
|
|
120
|
+
client = run.client()
|
|
121
|
+
from infix_cli.errors import SubmissionUnknown
|
|
122
|
+
|
|
123
|
+
try:
|
|
124
|
+
created = client.post(path, json=payload)
|
|
125
|
+
except SubmissionUnknown as exc:
|
|
126
|
+
exc.details = {**(exc.details or {}), "module": module, "name": payload.get("name")}
|
|
127
|
+
if payload.get("name"):
|
|
128
|
+
exc.details["query_argv"] = ["infix", module, "get", payload["name"]]
|
|
129
|
+
exc.hint = f"Query infix {module} get with the submitted name before retrying."
|
|
130
|
+
raise
|
|
131
|
+
if not isinstance(created, dict) or not created.get("id"):
|
|
132
|
+
raise UsageError("Submission returned an unexpected response.")
|
|
133
|
+
job_id = str(created["id"])
|
|
134
|
+
name = str(created.get("name") or job_id)
|
|
135
|
+
|
|
136
|
+
if run.output == "json":
|
|
137
|
+
report_existing(
|
|
138
|
+
run, module, job_id=job_id, name=name, detail_path=detail_path,
|
|
139
|
+
build_dto=build_dto, wait=wait, timeout=timeout, poll_status=poll_status,
|
|
140
|
+
seed=created,
|
|
141
|
+
)
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
dto = build_dto(created)
|
|
145
|
+
render.message(run, _create_output.created_title(module, name, waiting=wait))
|
|
146
|
+
if not wait:
|
|
147
|
+
# PRD §3.4: confirm what was accepted, then say what to run next.
|
|
148
|
+
text.print_labeled(
|
|
149
|
+
run, _create_output.created_rows(module, dto, echo=echo), gutter=1,
|
|
150
|
+
)
|
|
151
|
+
_print_next_steps(run, _create_output.next_steps(module, name))
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
_wait_and_report(
|
|
155
|
+
run, module, job_id=job_id, name=name, detail_path=detail_path,
|
|
156
|
+
build_dto=build_dto, timeout=timeout, poll_status=poll_status,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _print_next_steps(run: Any, lines: list[str]) -> None:
|
|
161
|
+
if run.quiet or not lines:
|
|
162
|
+
return
|
|
163
|
+
print()
|
|
164
|
+
for line in lines:
|
|
165
|
+
print(line)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _wait_and_report(
|
|
169
|
+
run: Any,
|
|
170
|
+
module: str,
|
|
171
|
+
*,
|
|
172
|
+
job_id: str,
|
|
173
|
+
name: str,
|
|
174
|
+
detail_path: Callable[[str], str],
|
|
175
|
+
build_dto: Callable[[dict], Any],
|
|
176
|
+
timeout: str | None,
|
|
177
|
+
poll_status: Callable[[dict], str] | None,
|
|
178
|
+
) -> None:
|
|
179
|
+
"""Block to a terminal state, showing the states passed through."""
|
|
180
|
+
from infix_cli import render, waiter
|
|
181
|
+
from infix_cli.errors import JobFailed
|
|
182
|
+
|
|
183
|
+
client = run.client()
|
|
184
|
+
success, failure = _TERMINALS[module]
|
|
185
|
+
failure = failure | _WAIT_ONLY_FAILURE.get(module, frozenset())
|
|
186
|
+
trail = _create_output.StatusTrail(run)
|
|
187
|
+
|
|
188
|
+
def fetch() -> tuple[str, dict]:
|
|
189
|
+
current = client.get(detail_path(job_id))
|
|
190
|
+
current = current if isinstance(current, dict) else {}
|
|
191
|
+
return (poll_status or _plain_status)(current), current
|
|
192
|
+
|
|
193
|
+
try:
|
|
194
|
+
status, raw = waiter.wait_until_terminal(
|
|
195
|
+
fetch,
|
|
196
|
+
terminal=frozenset(success | failure),
|
|
197
|
+
ctx=run,
|
|
198
|
+
timeout=waiter.parse_timeout(timeout),
|
|
199
|
+
label=f"{module} job {name}",
|
|
200
|
+
recheck_hint=f'Check again with "infix {module} get {job_id}".',
|
|
201
|
+
announce=trail,
|
|
202
|
+
)
|
|
203
|
+
finally:
|
|
204
|
+
trail.close()
|
|
205
|
+
|
|
206
|
+
dto = build_dto(raw)
|
|
207
|
+
if status in failure:
|
|
208
|
+
raise JobFailed(f'{module.capitalize()} job "{name}" ended as {status}.', details={"id": job_id, "name": name, "status": status})
|
|
209
|
+
|
|
210
|
+
render.message(run, _create_output.completion_title(dto.duration_seconds))
|
|
211
|
+
rows = _create_output.completion_rows(module, dto)
|
|
212
|
+
if rows:
|
|
213
|
+
text.print_labeled(run, rows, gutter=1)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def report_existing(
|
|
217
|
+
run: Any,
|
|
218
|
+
module: str,
|
|
219
|
+
*,
|
|
220
|
+
job_id: str,
|
|
221
|
+
name: str,
|
|
222
|
+
detail_path: Callable[[str], str],
|
|
223
|
+
build_dto: Callable[[dict], Any],
|
|
224
|
+
wait: bool,
|
|
225
|
+
timeout: str | None,
|
|
226
|
+
poll_status: Callable[[dict], str] | None = None,
|
|
227
|
+
seed: dict | None = None,
|
|
228
|
+
) -> None:
|
|
229
|
+
"""Render a job that already exists, optionally waiting for it first.
|
|
230
|
+
|
|
231
|
+
Shared by create and restart: both end with "show me where this landed",
|
|
232
|
+
and both map a failed terminal state onto exit code 7.
|
|
233
|
+
"""
|
|
234
|
+
from infix_cli import render, waiter
|
|
235
|
+
from infix_cli.errors import JobFailed
|
|
236
|
+
|
|
237
|
+
client = run.client()
|
|
238
|
+
raw = seed if isinstance(seed, dict) else {}
|
|
239
|
+
status = enums.status_to_cli(raw.get("status")) if raw else ""
|
|
240
|
+
success, failure = _TERMINALS[module]
|
|
241
|
+
failure = failure | _WAIT_ONLY_FAILURE.get(module, frozenset())
|
|
242
|
+
|
|
243
|
+
if wait:
|
|
244
|
+
def fetch() -> tuple[str, dict]:
|
|
245
|
+
current = client.get(detail_path(job_id))
|
|
246
|
+
current = current if isinstance(current, dict) else {}
|
|
247
|
+
return (poll_status or _plain_status)(current), current
|
|
248
|
+
|
|
249
|
+
status, raw = waiter.wait_until_terminal(
|
|
250
|
+
fetch,
|
|
251
|
+
terminal=frozenset(success | failure),
|
|
252
|
+
ctx=run,
|
|
253
|
+
timeout=waiter.parse_timeout(timeout),
|
|
254
|
+
label=f"{module} job {name}",
|
|
255
|
+
recheck_hint=f'Check again with "infix {module} get {job_id}".',
|
|
256
|
+
)
|
|
257
|
+
elif not raw:
|
|
258
|
+
current = client.get(detail_path(job_id))
|
|
259
|
+
raw = current if isinstance(current, dict) else {}
|
|
260
|
+
status = enums.status_to_cli(raw.get("status"))
|
|
261
|
+
|
|
262
|
+
render.render_detail(run, build_dto(raw))
|
|
263
|
+
|
|
264
|
+
if wait and status in failure:
|
|
265
|
+
raise JobFailed(f'{module.capitalize()} job "{name}" ended as {status}.', details={"id": job_id, "name": name, "status": status})
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def wait_existing(
|
|
269
|
+
run: Any,
|
|
270
|
+
module: str,
|
|
271
|
+
*,
|
|
272
|
+
job_id: str,
|
|
273
|
+
name: str,
|
|
274
|
+
detail_path: Callable[[str], str],
|
|
275
|
+
build_dto: Callable[[dict], Any],
|
|
276
|
+
timeout: str | None,
|
|
277
|
+
poll_status: Callable[[dict], str] | None = None,
|
|
278
|
+
) -> None:
|
|
279
|
+
"""``<module> wait``: attach to a job that is already running.
|
|
280
|
+
|
|
281
|
+
The same wait and the same exit codes as ``create --wait`` (0 / 6 / 7), for
|
|
282
|
+
when that wait was interrupted or never asked for. A job already finished
|
|
283
|
+
returns at once with its outcome.
|
|
284
|
+
"""
|
|
285
|
+
if run.output == "json":
|
|
286
|
+
report_existing(
|
|
287
|
+
run, module, job_id=job_id, name=name, detail_path=detail_path,
|
|
288
|
+
build_dto=build_dto, wait=True, timeout=timeout, poll_status=poll_status,
|
|
289
|
+
)
|
|
290
|
+
return
|
|
291
|
+
_wait_and_report(
|
|
292
|
+
run, module, job_id=job_id, name=name, detail_path=detail_path,
|
|
293
|
+
build_dto=build_dto, timeout=timeout, poll_status=poll_status,
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _plain_status(raw: dict) -> str:
|
|
298
|
+
return enums.status_to_cli(raw.get("status"))
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def evaluation_status(raw: dict) -> str:
|
|
302
|
+
"""Evaluation has a state between Completed and "has results".
|
|
303
|
+
|
|
304
|
+
A job whose status is Completed but whose results have not been collected
|
|
305
|
+
has no score yet; returning a non-terminal word here keeps --wait polling
|
|
306
|
+
until the results land (or collection gives up), so the caller never gets a
|
|
307
|
+
"successful" job with an empty score.
|
|
308
|
+
"""
|
|
309
|
+
status = enums.status_to_cli(raw.get("status"))
|
|
310
|
+
if status == "completed" and not raw.get("results_collected", True):
|
|
311
|
+
return "collection_exhausted" if raw.get("collection_exhausted") else "collecting"
|
|
312
|
+
return status
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""Shared log fetching, following and rendering for the three job modules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import time
|
|
7
|
+
from typing import Any, Callable, Iterator
|
|
8
|
+
|
|
9
|
+
from infix_cli.domain.dto import logs as log_dto
|
|
10
|
+
from infix_cli.domain.dto.logs import LogLineDTO
|
|
11
|
+
from infix_cli.errors import CliError, UsageError
|
|
12
|
+
|
|
13
|
+
# Reconnect window: how many recent lines are remembered when a stream is
|
|
14
|
+
# resumed, so a re-delivered tail is not printed twice. The backend replays up
|
|
15
|
+
# to 200 lines per connection, so the window has to be comfortably larger than
|
|
16
|
+
# one replay for the de-duplication to cover it.
|
|
17
|
+
_DEDUP_WINDOW = 500
|
|
18
|
+
_FOLLOW_INTERVAL = 2.0
|
|
19
|
+
_MAX_CONSECUTIVE_FAILURES = 3
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def fetch(client: Any, module: str, job_id: str, *, worker: str | None, tail: int) -> list[LogLineDTO]:
|
|
23
|
+
"""One-shot log read, normalised to LogLineDTO."""
|
|
24
|
+
if module == "training":
|
|
25
|
+
payload = client.get(
|
|
26
|
+
f"/training/jobs/{job_id}/logs",
|
|
27
|
+
params={"worker": worker or "master", "tail": tail},
|
|
28
|
+
)
|
|
29
|
+
return log_dto.from_training(payload if isinstance(payload, dict) else {})
|
|
30
|
+
if module == "spark":
|
|
31
|
+
payload = client.get(
|
|
32
|
+
f"/data-processing/{job_id}/logs",
|
|
33
|
+
params={"worker": worker, "tail_lines": tail, "log_type": "all"},
|
|
34
|
+
)
|
|
35
|
+
return log_dto.from_spark(payload if isinstance(payload, dict) else {})
|
|
36
|
+
payload = client.get(f"/evaluation/jobs/{job_id}/logs", params={"tail_lines": tail})
|
|
37
|
+
return log_dto.from_evaluation(payload if isinstance(payload, dict) else {})
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def stream_once(
|
|
41
|
+
client: Any, module: str, job_id: str, *, worker: str | None, since: str | None = None,
|
|
42
|
+
) -> Iterator[LogLineDTO]:
|
|
43
|
+
"""Consume one SSE connection, yielding lines until the server says done.
|
|
44
|
+
|
|
45
|
+
Training's stream is not a long-lived connection: it sends the current tail
|
|
46
|
+
and closes with an ``event: done``, so following means reconnecting.
|
|
47
|
+
|
|
48
|
+
The server's ``since`` parameter is deliberately NOT used. It filters raw
|
|
49
|
+
container lines by a lexicographic comparison against nanosecond
|
|
50
|
+
timestamps, while the timestamps the client receives have been reformatted
|
|
51
|
+
to second precision in the server's local timezone. Feeding one back would
|
|
52
|
+
drop every line in the cursor's own second (``.`` sorts before ``Z``), and
|
|
53
|
+
on any deployment whose backend runs outside UTC it would drop everything
|
|
54
|
+
after the first reconnect. De-duplication happens client-side instead,
|
|
55
|
+
against the window above.
|
|
56
|
+
"""
|
|
57
|
+
path = (
|
|
58
|
+
f"/training/jobs/{job_id}/logs/stream"
|
|
59
|
+
if module == "training"
|
|
60
|
+
else f"/data-processing/{job_id}/logs/stream"
|
|
61
|
+
)
|
|
62
|
+
params: dict[str, Any] = {}
|
|
63
|
+
params["worker"] = worker or ("master" if module == "training" else None)
|
|
64
|
+
if module == "spark":
|
|
65
|
+
params["log_type"] = "all"
|
|
66
|
+
|
|
67
|
+
event_name = ""
|
|
68
|
+
for raw in client.stream_lines("GET", path, params=params):
|
|
69
|
+
line = raw.rstrip("\r")
|
|
70
|
+
if not line:
|
|
71
|
+
event_name = ""
|
|
72
|
+
continue
|
|
73
|
+
if line.startswith("event:"):
|
|
74
|
+
event_name = line.split(":", 1)[1].strip()
|
|
75
|
+
continue
|
|
76
|
+
if not line.startswith("data:"):
|
|
77
|
+
continue
|
|
78
|
+
payload = line.split(":", 1)[1].strip()
|
|
79
|
+
if event_name == "done":
|
|
80
|
+
return
|
|
81
|
+
try:
|
|
82
|
+
event = json.loads(payload)
|
|
83
|
+
except ValueError:
|
|
84
|
+
continue
|
|
85
|
+
if isinstance(event, dict) and event.get("text") is not None:
|
|
86
|
+
yield log_dto.from_sse(event, worker or "master")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def follow(
|
|
90
|
+
run: Any,
|
|
91
|
+
stream: Callable[[str | None], Iterator[LogLineDTO]],
|
|
92
|
+
*,
|
|
93
|
+
keyword: str | None = None,
|
|
94
|
+
seed: list[LogLineDTO] | None = None,
|
|
95
|
+
) -> None:
|
|
96
|
+
"""Print lines as they arrive, reconnecting until interrupted."""
|
|
97
|
+
from infix_cli.render import text as text_render
|
|
98
|
+
|
|
99
|
+
seen: list[tuple[str, str]] = []
|
|
100
|
+
seen_set: set[tuple[str, str]] = set()
|
|
101
|
+
failures = 0
|
|
102
|
+
|
|
103
|
+
def emit(entry: LogLineDTO) -> bool:
|
|
104
|
+
key = entry.dedup_key()
|
|
105
|
+
if key in seen_set:
|
|
106
|
+
return False
|
|
107
|
+
seen.append(key)
|
|
108
|
+
seen_set.add(key)
|
|
109
|
+
if len(seen) > _DEDUP_WINDOW:
|
|
110
|
+
seen_set.discard(seen.pop(0))
|
|
111
|
+
if keyword and keyword not in entry.line:
|
|
112
|
+
return True
|
|
113
|
+
_emit_line(run, entry)
|
|
114
|
+
return True
|
|
115
|
+
|
|
116
|
+
for entry in seed or []:
|
|
117
|
+
emit(entry)
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
while True:
|
|
121
|
+
try:
|
|
122
|
+
for entry in stream(None):
|
|
123
|
+
emit(entry)
|
|
124
|
+
failures = 0
|
|
125
|
+
except CliError as exc:
|
|
126
|
+
failures += 1
|
|
127
|
+
if failures >= _MAX_CONSECUTIVE_FAILURES:
|
|
128
|
+
raise
|
|
129
|
+
if run.output != "json":
|
|
130
|
+
text_render.print_error(
|
|
131
|
+
f"Log stream interrupted ({exc.message}); reconnecting...",
|
|
132
|
+
no_color=run.no_color,
|
|
133
|
+
)
|
|
134
|
+
time.sleep(_FOLLOW_INTERVAL)
|
|
135
|
+
except KeyboardInterrupt:
|
|
136
|
+
return
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def render(run: Any, lines: list[LogLineDTO], *, keyword: str | None = None) -> None:
|
|
140
|
+
"""Print a fixed set of lines, explaining an empty result rather than
|
|
141
|
+
printing nothing — silence reads as a broken command."""
|
|
142
|
+
shown = filter_lines(lines, keyword)
|
|
143
|
+
for entry in shown:
|
|
144
|
+
_emit_line(run, entry)
|
|
145
|
+
if shown:
|
|
146
|
+
return
|
|
147
|
+
from infix_cli import render as _render
|
|
148
|
+
|
|
149
|
+
if lines and keyword:
|
|
150
|
+
_render.note(run, f'No lines matching "{keyword}" in the last {len(lines)} lines.')
|
|
151
|
+
else:
|
|
152
|
+
_render.note(
|
|
153
|
+
run,
|
|
154
|
+
"No log lines available. The pods may not have produced output yet, "
|
|
155
|
+
"or the log collector has nothing for them.",
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def filter_lines(lines: list[LogLineDTO], keyword: str | None) -> list[LogLineDTO]:
|
|
160
|
+
"""Local keyword filter.
|
|
161
|
+
|
|
162
|
+
The backends have no keyword parameter, so this is applied to whatever
|
|
163
|
+
``--tail`` returned — equivalent to ``logs --tail N | grep``, and unable to
|
|
164
|
+
find anything older than those N lines. Documented as such.
|
|
165
|
+
"""
|
|
166
|
+
if not keyword:
|
|
167
|
+
return lines
|
|
168
|
+
return [entry for entry in lines if keyword in entry.line]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _emit_line(run: Any, entry: LogLineDTO) -> None:
|
|
172
|
+
if run.output == "json":
|
|
173
|
+
from infix_cli.render import json_out
|
|
174
|
+
|
|
175
|
+
json_out.print_ndjson(entry.to_json())
|
|
176
|
+
return
|
|
177
|
+
print(entry.line)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def download(run: Any, module: str, job_id: str, *, worker: str | None, tail: int, dest: str) -> None:
|
|
181
|
+
"""Save the full log to a file.
|
|
182
|
+
|
|
183
|
+
Only training and spark expose a download endpoint; evaluation does not.
|
|
184
|
+
"""
|
|
185
|
+
from pathlib import Path
|
|
186
|
+
|
|
187
|
+
if module == "evaluation":
|
|
188
|
+
raise UsageError(
|
|
189
|
+
"--download is not available for evaluation jobs.",
|
|
190
|
+
"The evaluation API has no log download endpoint; "
|
|
191
|
+
'use "infix evaluation logs <job> --tail 5000 > file.log".',
|
|
192
|
+
)
|
|
193
|
+
path = (
|
|
194
|
+
f"/training/jobs/{job_id}/logs/download"
|
|
195
|
+
if module == "training"
|
|
196
|
+
else f"/data-processing/{job_id}/logs/download"
|
|
197
|
+
)
|
|
198
|
+
params: dict[str, Any] = {"tail": tail} if module == "training" else {"tail_lines": tail}
|
|
199
|
+
params["worker"] = worker or ("master" if module == "training" else None)
|
|
200
|
+
|
|
201
|
+
target = Path(dest).expanduser()
|
|
202
|
+
written = 0
|
|
203
|
+
with target.open("w", encoding="utf-8") as handle:
|
|
204
|
+
for chunk in run.client().stream_lines("GET", path, params=params):
|
|
205
|
+
handle.write(chunk + "\n")
|
|
206
|
+
written += 1
|
|
207
|
+
|
|
208
|
+
from infix_cli import render as render_mod
|
|
209
|
+
|
|
210
|
+
render_mod.message(run, f"Wrote {written} lines to {target}.")
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""publish: make a private asset, image or job public to the workspace.
|
|
2
|
+
|
|
3
|
+
Named for what it does rather than ``set-visibility``, because the platform only
|
|
4
|
+
goes one way: every visibility endpoint accepts Private → Public and nothing
|
|
5
|
+
offers the reverse (even storage's unshare is disabled, 405). A command that
|
|
6
|
+
looked like a toggle would promise an undo that does not exist.
|
|
7
|
+
|
|
8
|
+
The five endpoints disagree on almost everything — verb, body, what "already
|
|
9
|
+
public" returns (403, 400, or a silent no-op) — so that is settled here, before
|
|
10
|
+
the request: an already-public target is reported as such and exits 0.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from infix_cli import render
|
|
18
|
+
from infix_cli.commands import _confirm
|
|
19
|
+
from infix_cli.domain import enums, resolve
|
|
20
|
+
from infix_cli.errors import ApiError, ForbiddenError
|
|
21
|
+
|
|
22
|
+
_IRREVERSIBLE = ("Note", "This cannot be undone: public cannot be made private again.")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def publish_asset(run: Any, kind: str, plain: str, *, yes: bool) -> None:
|
|
26
|
+
client = run.client()
|
|
27
|
+
asset = resolve.find_asset(client, kind, plain, "workspace")
|
|
28
|
+
label = f'{kind.capitalize()} "{plain}"'
|
|
29
|
+
if _is_public(asset):
|
|
30
|
+
_already(run, label, {"type": kind, "id": str(asset["id"]), "name": plain})
|
|
31
|
+
return
|
|
32
|
+
_confirm.require(
|
|
33
|
+
run,
|
|
34
|
+
title=f"{label} will be made public to everyone in the workspace.",
|
|
35
|
+
rows=[("Versions", "all, including ones added later"), _IRREVERSIBLE],
|
|
36
|
+
yes=yes,
|
|
37
|
+
)
|
|
38
|
+
client.post(f"/assets/{resolve.asset_path(kind)}/{asset['id']}/share")
|
|
39
|
+
_published(run, label, {"type": kind, "id": str(asset["id"]), "name": plain})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def publish_image(run: Any, image: dict, *, yes: bool) -> None:
|
|
43
|
+
image_id = str(image.get("id"))
|
|
44
|
+
name = str(image.get("display_name") or image_id)
|
|
45
|
+
label = f'Image "{name}"'
|
|
46
|
+
# Images call their visibility "scope" on the wire.
|
|
47
|
+
if enums.VISIBILITY.to_cli(image.get("scope")) == "public":
|
|
48
|
+
_already(run, label, {"type": "image", "id": image_id, "name": name})
|
|
49
|
+
return
|
|
50
|
+
_confirm.require(
|
|
51
|
+
run,
|
|
52
|
+
title=f"{label} will be made public to everyone in the workspace.",
|
|
53
|
+
rows=[("URI", str(image.get("image_url") or "--")), _IRREVERSIBLE],
|
|
54
|
+
yes=yes,
|
|
55
|
+
)
|
|
56
|
+
client = run.client()
|
|
57
|
+
client.post(f"/images/{image_id}/share", json={"workspace_id": client.workspace_id})
|
|
58
|
+
_published(run, label, {"type": "image", "id": image_id, "name": name})
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def publish_job(run: Any, module: str, name: str, *, yes: bool) -> None:
|
|
62
|
+
client = run.client()
|
|
63
|
+
job = resolve.find_job(client, module, name)
|
|
64
|
+
job_id = str(job.get("id"))
|
|
65
|
+
job_name = str(job.get("name") or job_id)
|
|
66
|
+
label = f'{module.capitalize()} job "{job_name}"'
|
|
67
|
+
record = {"type": module, "id": job_id, "name": job_name}
|
|
68
|
+
if _is_public(job):
|
|
69
|
+
_already(run, label, record)
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
output = job.get("storage_path") or job.get("output_path")
|
|
73
|
+
rows = [("Output", f"{output} (shared with the job)" if output else "shared with the job")]
|
|
74
|
+
_confirm.require(
|
|
75
|
+
run,
|
|
76
|
+
title=f"{label} will be made public to everyone in the workspace.",
|
|
77
|
+
rows=rows + [_IRREVERSIBLE],
|
|
78
|
+
yes=yes,
|
|
79
|
+
)
|
|
80
|
+
try:
|
|
81
|
+
if module == "spark":
|
|
82
|
+
client.post(f"/data-processing/{job_id}/share")
|
|
83
|
+
else:
|
|
84
|
+
path = "/training/jobs" if module == "training" else "/evaluation/jobs"
|
|
85
|
+
client.patch(f"{path}/{job_id}/visibility", json={"visibility": "Public"})
|
|
86
|
+
except ForbiddenError as exc:
|
|
87
|
+
if module == "training":
|
|
88
|
+
# Training alone has no admin override: only the creator may.
|
|
89
|
+
raise ForbiddenError(exc.message, "Only the job's creator can publish it.") from exc
|
|
90
|
+
raise
|
|
91
|
+
except ApiError as exc:
|
|
92
|
+
if module == "spark" and "Failed to share job" in exc.message:
|
|
93
|
+
# The backend folds every failure, a missing output directory
|
|
94
|
+
# included, into one 500.
|
|
95
|
+
raise ApiError(
|
|
96
|
+
exc.message,
|
|
97
|
+
"The job's output directory may have been deleted from Workspace Storage.",
|
|
98
|
+
) from exc
|
|
99
|
+
raise
|
|
100
|
+
_published(run, label, record)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _is_public(raw: dict) -> bool:
|
|
104
|
+
return enums.VISIBILITY.to_cli(raw.get("visibility")) == "public"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _already(run: Any, label: str, record: dict) -> None:
|
|
108
|
+
"""Already public is the outcome asked for, so it is not an error."""
|
|
109
|
+
_output(run, record, f"{label} is already public.", changed=False)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _published(run: Any, label: str, record: dict) -> None:
|
|
113
|
+
_output(run, record, f"{label} is now public.", changed=True)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _output(run: Any, record: dict, line: str, *, changed: bool) -> None:
|
|
117
|
+
if run.output == "json":
|
|
118
|
+
from infix_cli.render import json_out
|
|
119
|
+
|
|
120
|
+
json_out.print_raw({**record, "visibility": "public", "changed": changed})
|
|
121
|
+
return
|
|
122
|
+
render.message(run, line)
|
|
123
|
+
|