fix(admin): preserve ready agent no-op saves

go
codex 3 weeks ago
parent 2e339f7c82
commit 5fdfe96895

@ -132,6 +132,7 @@ final class CloudAgentSettingsService
if ($actualRevision !== $expectedRevision) {
throw new CloudError('Agent 配置已被修改,请刷新页面后重试', 409);
}
$isNew = $setting === null;
if ($setting === null) {
if ($secret === '') {
throw new CloudError('首次保存 Agent 配置必须填写 API 密钥');
@ -142,6 +143,12 @@ final class CloudAgentSettingsService
} elseif ($secret === '' && trim((string) $setting->sealed_api_secret) === '') {
throw new CloudError('启用 Agent 前必须配置 API 密钥');
}
$enabledChanged = $isNew
|| (bool) $setting->enabled !== $enabled;
$secretChanged = $secret !== '';
if (!$enabledChanged && !$secretChanged) {
return $setting;
}
$setting->enabled = $enabled;
if ($secret !== '') {
$setting->sealed_api_secret = CloudAgentSecretCipher::encrypt($secret, $hostId);

@ -1345,6 +1345,30 @@
margin-top: 10px;
}
.cloud-agent-rotation-confirm {
display: flex;
align-items: flex-start;
gap: 9px;
margin-top: 12px;
padding: 11px 12px;
border: 1px solid #f4c7a1;
border-radius: 8px;
color: #9a4f14;
background: #fff8ef;
font-size: 12px;
font-weight: 750;
line-height: 1.55;
}
.cloud-agent-rotation-confirm[hidden] {
display: none;
}
.cloud-agent-rotation-confirm .form-check-input {
flex: 0 0 auto;
margin-top: 2px;
}
.cloud-agent-config-box {
display: grid;
gap: 10px;

@ -6,6 +6,7 @@ const state = {
selected: null, busy: false, expandedRegions: new Set(), regionsInitialized: false,
drawerOpener: null, drawerScroll: null,
secretDraft: '', secretDraftHostId: null, secretVisible: false,
rotationConfirmed: false,
};
function notice(message, error = false) {
@ -245,6 +246,7 @@ function clearSecretDraft(update = true) {
state.secretDraft = '';
state.secretDraftHostId = null;
state.secretVisible = false;
state.rotationConfirmed = false;
if (update) renderSecretDraft(selectedAgent());
}
@ -262,6 +264,12 @@ function renderSecretDraft(agent) {
byId('agent-settings-copy-secret').disabled = !canManage || !hasDraft;
byId('agent-settings-clear-secret').disabled = !canManage || !hasDraft;
byId('agent-settings-copy-config').disabled = !canManage || !hasDraft;
const rotating = hasDraft && agent?.secret_configured === true;
const confirmation = byId('agent-settings-rotation-confirm-box');
confirmation.hidden = !rotating;
byId('agent-settings-rotation-confirm').checked = rotating && state.rotationConfirmed;
byId('agent-settings-save').disabled = !canManage || !agent || state.busy
|| (rotating && !state.rotationConfirmed);
const box = byId('agent-settings-config-box');
box.hidden = !hasDraft;
byId('agent-settings-config-preview').textContent = hasDraft ? configFragment(agent, secret) : '';
@ -360,6 +368,7 @@ async function save(event) {
};
if (!Number.isSafeInteger(body.expected_revision) || body.expected_revision < 0) return notice('配置版本无效,请刷新页面。', true);
if (body.api_secret && (body.api_secret.length < 32 || body.api_secret.length > 256 || /\s/.test(body.api_secret))) return notice('API 密钥需为 32 至 256 位无空格字符。', true);
if (body.api_secret && selectedAgent()?.secret_configured && !state.rotationConfirmed) return notice('请先确认密钥轮换会立即使旧密钥失效。', true);
state.busy = true; fill(selectedAgent(), { preserveDraft: true }); notice('正在保存 Agent API 配置…');
let savedSuccessfully = false;
try {
@ -397,6 +406,7 @@ if (root) {
byId('agent-settings-secret').addEventListener('input', event => {
state.secretDraft = event.currentTarget.value;
state.secretDraftHostId = state.selected;
state.rotationConfirmed = false;
renderSecretDraft(selectedAgent());
});
byId('agent-settings-generate').addEventListener('click', () => {
@ -404,11 +414,13 @@ if (root) {
state.secretDraft = generateAgentSecret();
state.secretDraftHostId = state.selected;
state.secretVisible = true;
state.rotationConfirmed = false;
renderSecretDraft(selectedAgent());
notice('已在本页生成一次性安全密钥;请复制到宿主机并保存面板配置。');
} catch (error) { notice(error.message, true); }
});
byId('agent-settings-reveal').addEventListener('click', () => { state.secretVisible = !state.secretVisible; renderSecretDraft(selectedAgent()); });
byId('agent-settings-rotation-confirm').addEventListener('change', event => { state.rotationConfirmed = event.currentTarget.checked; renderSecretDraft(selectedAgent()); });
byId('agent-settings-clear-secret').addEventListener('click', () => clearSecretDraft());
byId('agent-settings-copy-secret').addEventListener('click', () => { void copyDraft(state.secretDraft, '密钥').catch(error => notice(error.message, true)); });
byId('agent-settings-copy-config').addEventListener('click', () => { void copyDraft(configFragment(selectedAgent(), state.secretDraft), '配置片段').catch(error => notice(error.message, true)); });

@ -75,6 +75,7 @@
<button id="agent-settings-copy-secret" class="btn btn-light btn-sm" type="button" disabled><i class="mdi mdi-content-copy me-1"></i>复制密钥</button>
<button id="agent-settings-clear-secret" class="btn btn-light btn-sm" type="button" disabled><i class="mdi mdi-close-circle-outline me-1"></i>清除草稿</button>
</div>
<label id="agent-settings-rotation-confirm-box" class="cloud-agent-rotation-confirm" hidden><input id="agent-settings-rotation-confirm" class="form-check-input" type="checkbox"><span>我确认已准备同步更新宿主机;保存后旧密钥会立即失效,必须重新测试才能恢复调度。</span></label>
<div id="agent-settings-config-box" class="cloud-agent-config-box" hidden>
<div><strong>一次性宿主机配置片段</strong><span>仅将这 3 个字段合并到该宿主机现有 <code>config.json</code>;关闭抽屉后不再保留。</span></div>
<pre id="agent-settings-config-preview" class="admin-mono" aria-label="一次性 Agent 配置片段"></pre>

@ -1,2 +1,2 @@
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
{assign var=venix_asset_version value='2026103107' scope='global'}
{assign var=venix_asset_version value='2026103108' scope='global'}

@ -29,6 +29,7 @@ $assert(str_contains($template, 'id="agent-settings-summary"')
&& str_contains($template, 'id="agent-settings-generate"')
&& str_contains($template, 'id="agent-settings-copy-secret"')
&& str_contains($template, 'id="agent-settings-copy-config"')
&& str_contains($template, 'id="agent-settings-rotation-confirm"')
&& str_contains($template, 'aria-controls="agent-settings-list"'),
'Agent page is missing its summary or region filters.');
$assert(str_contains($script, 'state.regions = Array.isArray(payload.regions)')
@ -44,6 +45,7 @@ $assert(str_contains($script, 'state.regions = Array.isArray(payload.regions)')
&& str_contains($script, 'window.crypto.getRandomValues(bytes)')
&& str_contains($script, 'function configFragment(')
&& str_contains($script, 'navigator.clipboard?.writeText')
&& str_contains($script, 'state.rotationConfirmed')
&& !str_contains($script, 'Math.random')
&& !str_contains($script, 'localStorage')
&& !str_contains($script, 'sessionStorage')
@ -56,7 +58,7 @@ $assert(str_contains($styles, '.cloud-admin-agent-directory')
&& str_contains($styles, '.cloud-agent-config-box'),
'Agent region layout styles are missing.');
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets,
$match) === 1 && (int) $match[1] >= 2026103107,
$match) === 1 && (int) $match[1] >= 2026103108,
'Agent region UI requires a new shared asset version.');
echo "Administrator Agent region UI contracts passed.\n";

@ -118,7 +118,6 @@ $assert($ready['ready'] === true && $ready['tested_revision'] === 1
$host->default_network = 'public';
$host->save();
$assert(CloudAgentSettingsService::read((int) $host->id)['ready'] === false
&& CloudAgentSettingsService::listing()['agents'][0]['ready'] === false
&& CloudAgentSettingsService::enabledHostIds() === [],
'A healthy legacy network alias remained ready or schedulable.');
try {
@ -146,20 +145,45 @@ $assert($invalidated['revision'] === 2
'Changing the host selector did not revoke the prior Agent readiness.');
$recordTest->invoke(null, (int) $host->id, 2, 'healthy', '', 'test-2');
$unchanged = CloudAgentSettingsService::save((int) $host->id, [
'enabled' => true,
'api_secret' => '',
'expected_revision' => 2,
]);
$assert($unchanged['revision'] === 2
&& $unchanged['tested_revision'] === 2
&& $unchanged['last_test_status'] === 'healthy'
&& $unchanged['ready'] === true
&& CloudAgentSettingsService::enabledHostIds() === [(int) $host->id],
'Saving unchanged Agent settings revoked a valid readiness result.');
$rotated = CloudAgentSettingsService::save((int) $host->id, [
'enabled' => true,
'api_secret' => str_repeat('r', 32),
'expected_revision' => 2,
]);
$assert($rotated['revision'] === 3
&& $rotated['tested_revision'] === 0
&& $rotated['last_test_status'] === 'never'
&& $rotated['ready'] === false
&& CloudAgentSettingsService::enabledHostIds() === [],
'Rotating an Agent secret retained the prior readiness result.');
$recordTest->invoke(null, (int) $host->id, 3, 'healthy', '', 'test-3');
$changed = CloudAgentSettingsService::save((int) $host->id, [
'enabled' => false,
'api_secret' => '',
'expected_revision' => 2,
'expected_revision' => 3,
]);
$assert($changed['revision'] === 3 && $changed['enabled'] === false
$assert($changed['revision'] === 4 && $changed['enabled'] === false
&& $changed['tested_revision'] === 0
&& $changed['last_test_status'] === 'never'
&& $changed['ready'] === false
&& CloudAgentSettingsService::enabledHostIds() === [],
'Changing an Agent setting retained the prior revision readiness.');
$recordTest->invoke(null, (int) $host->id, 3, 'healthy', '', 'test-3');
$recordTest->invoke(null, (int) $host->id, 3, 'unhealthy',
$recordTest->invoke(null, (int) $host->id, 4, 'healthy', '', 'test-4');
$recordTest->invoke(null, (int) $host->id, 4, 'unhealthy',
'agent_unavailable', '');
$failed = CloudHostAgent::find((int) $host->id);
$assert($failed !== null && !$failed->isReady()

Loading…
Cancel
Save